diff options
author | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-08 04:02:52 -0600 |
---|---|---|
committer | Luke T. Shumaker <lukeshu@lukeshu.com> | 2025-06-08 04:02:52 -0600 |
commit | ef5956962675d7cb1bdb76a2fc45760b002a570c (patch) | |
tree | 54fa10580413b4b1dfbd9b22a52fffca834f772e /libmisc/tests | |
parent | 53d0b11262177cd212803fc9330e055871c348b3 (diff) | |
parent | db1b4b4cc41c84ec4c8e11418431a1ab2a1001c5 (diff) |
Merge branch 'lukeshu/fix-obj-box'
Diffstat (limited to 'libmisc/tests')
-rw-r--r-- | libmisc/tests/test_obj.c | 2 | ||||
-rw-r--r-- | libmisc/tests/test_obj_autobox.c | 83 | ||||
-rwxr-xr-x | libmisc/tests/test_obj_autobox.c.gen | 17 | ||||
-rw-r--r-- | libmisc/tests/test_obj_nest.c | 11 |
4 files changed, 111 insertions, 2 deletions
diff --git a/libmisc/tests/test_obj.c b/libmisc/tests/test_obj.c index a13b8c9..687ad4e 100644 --- a/libmisc/tests/test_obj.c +++ b/libmisc/tests/test_obj.c @@ -53,7 +53,7 @@ int main() { struct myclass obj = { .a = MAGIC1, }; - lo_interface frobber iface = LO_BOX(frobber, &obj); + lo_interface frobber iface = lo_box_myclass_as_frobber(&obj); test_assert(LO_CALL(iface, frob) == MAGIC1); test_assert(LO_CALL(iface, frob1, MAGIC2) == MAGIC2); LO_CALL(iface, frob0); diff --git a/libmisc/tests/test_obj_autobox.c b/libmisc/tests/test_obj_autobox.c new file mode 100644 index 0000000..1bf442d --- /dev/null +++ b/libmisc/tests/test_obj_autobox.c @@ -0,0 +1,83 @@ +/* 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 <libmisc/obj.h> nesting + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <string.h> /* for memcpy() */ + +#include <libmisc/obj.h> + +#include "test.h" + +/* interfaces *****************************************************************/ + +#define reader_LO_IFACE \ + LO_FUNC(ssize_t, read, void *, size_t) +LO_INTERFACE(reader); + +#define writer_LO_IFACE \ + LO_FUNC(ssize_t, write, void *, size_t) +LO_INTERFACE(writer); + +#define read_writer_LO_IFACE \ + LO_NEST(reader) \ + LO_NEST(writer) +LO_INTERFACE(read_writer); + +/* implementation header ******************************************************/ + +struct myclass { + size_t len; + char buf[512]; +}; +LO_IMPLEMENTATION_H(reader, struct myclass, myclass); +LO_IMPLEMENTATION_H(writer, struct myclass, myclass); +LO_IMPLEMENTATION_H(read_writer, struct myclass, myclass); + +/* implementation main ********************************************************/ + +LO_IMPLEMENTATION_C(reader, struct myclass, myclass, static); +LO_IMPLEMENTATION_C(writer, struct myclass, myclass, static); +LO_IMPLEMENTATION_C(read_writer, struct myclass, myclass, static); + +static ssize_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 ssize_t myclass_write(struct myclass *self, void *buf, size_t count) { + test_assert(self); + if (self->len) + return -1; + 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; +} diff --git a/libmisc/tests/test_obj_autobox.c.gen b/libmisc/tests/test_obj_autobox.c.gen new file mode 100755 index 0000000..3cfa3d4 --- /dev/null +++ b/libmisc/tests/test_obj_autobox.c.gen @@ -0,0 +1,17 @@ +#!/bin/sh +# libmisc/tests/test_obj_autobox.c.gen - Generate tests for LO_BOX() +# +# Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> +# SPDX-License-Identifier: AGPL-3.0-or-later + +infile=$1 +outfile=$2 + +grep --quiet lo_box_ -- "$infile" + +{ + echo "/* ${outfile} - Generated by \`$*\`. DO NOT EDIT! */" + sed -E 's/lo_box_([a-z0-9_]+)_as_([a-z0-9_]+)\(/LO_BOX(\2, /g' <"$infile" +} >"$outfile" + +! grep -H lo_box_ -- "$outfile" diff --git a/libmisc/tests/test_obj_nest.c b/libmisc/tests/test_obj_nest.c index ba5ac37..66c1efd 100644 --- a/libmisc/tests/test_obj_nest.c +++ b/libmisc/tests/test_obj_nest.c @@ -64,10 +64,19 @@ static ssize_t myclass_write(struct myclass *self, void *buf, size_t count) { int main() { struct myclass _obj = {}; - lo_interface read_writer obj = LO_BOX(read_writer, &_obj); + lo_interface read_writer obj = lo_box_myclass_as_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_myclass_as_reader(&_obj); + rd = lo_box_read_writer_as_reader(obj); + (void) rd; + + lo_interface writer wr = lo_box_myclass_as_writer(&_obj); + wr = lo_box_read_writer_as_writer(obj); + (void) wr; + return 0; } |