/* libmisc/tests/test_obj_autobox.c - Generated by `libmisc/tests/test_obj_nest.c libmisc/tests/test_obj_autobox.c`. DO NOT EDIT! */ /* libmisc/tests/test_obj_nest.c - Tests for nesting * * Copyright (C) 2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ #include /* for memcpy() */ #include #include "test.h" /* interfaces *****************************************************************/ #define reader_LO_IFACE \ LO_FUNC(size_t, read, void *, size_t) LO_INTERFACE(reader); #define writer_LO_IFACE \ LO_FUNC(size_t, write, void *, size_t) LO_INTERFACE(writer); #define read_writer_LO_IFACE \ LO_NEST(reader) \ LO_NEST(writer) LO_INTERFACE(read_writer); /* implementation *************************************************************/ struct myclass { size_t len; char buf[512]; }; LO_IMPLEMENTATION_STATIC(reader, struct myclass, myclass); LO_IMPLEMENTATION_STATIC(writer, struct myclass, myclass); LO_IMPLEMENTATION_STATIC(read_writer, struct myclass, myclass); static size_t myclass_read(struct myclass *self, void *buf, size_t count) { test_assert(self); if (count > self->len) count = self->len; memcpy(buf, self->buf, count); return count; } static size_t myclass_write(struct myclass *self, void *buf, size_t count) { test_assert(self); if (self->len) return 0; if (count > sizeof(self->buf)) count = sizeof(self->buf); memcpy(self->buf, buf, count); self->len = count; return count; } /* main test body *************************************************************/ int main() { struct myclass _obj = {}; lo_interface read_writer obj = LO_BOX(read_writer, &_obj); test_assert(LO_CALL(obj, write, "Hello", 6) == 6); char buf[6] = {}; test_assert(LO_CALL(obj, read, buf, 3) == 3); test_assert(memcmp(buf, "Hel\0\0\0", 6) == 0); lo_interface reader rd = LO_BOX(reader, &_obj); rd = LO_BOX(reader, obj); (void) rd; lo_interface writer wr = LO_BOX(writer, &_obj); wr = LO_BOX(writer, obj); (void) wr; return 0; }