From 797a77fcc8655a641630758dc4022f215008f23b Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Sun, 20 Apr 2025 12:45:42 -0600 Subject: libmisc: log.h: Add a "log_" prefix to errorf/infof/debugf --- libmisc/tests/test_log.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_log.c b/libmisc/tests/test_log.c index 49a76ca..02b95d6 100644 --- a/libmisc/tests/test_log.c +++ b/libmisc/tests/test_log.c @@ -1,6 +1,6 @@ /* libmisc/tests/test_log.c - Tests for * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ @@ -54,17 +54,17 @@ int __lm_printf(const char *format, ...) { int main() { should_print("error: FROBNICATE: val=42\n", - errorf("val=%d", 42)); + log_errorf("val=%d", 42)); should_print("info : FROBNICATE: val=0\n", - infof("val=%d", 0)); + log_infof("val=%d", 0)); #ifndef NDEBUG #define CONFIG_FROBNICATE_DEBUG 1 should_print("debug: FROBNICATE: val=-2\n", - debugf("val=%d", -2)); + log_debugf("val=%d", -2)); #undef CONFIG_FROBNICATE_DEBUG #define CONFIG_FROBNICATE_DEBUG 0 should_print(NULL, - debugf("val=%d", -2)); + log_debugf("val=%d", -2)); #endif return 0; } -- cgit v1.2.3-2-g168b From 39d8fd2161d0a505c5b25add023aad833714b980 Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Tue, 22 Apr 2025 15:06:50 -0600 Subject: Use C23 This gives us: - [[maybe_unused]] instead of [[gnu::unused]] - bool/true/false are predefined, so no need for --- libmisc/tests/test_assert.c | 1 - libmisc/tests/test_rand.c | 3 +-- 2 files changed, 1 insertion(+), 3 deletions(-) (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c index 15f9446..c6d2dc1 100644 --- a/libmisc/tests/test_assert.c +++ b/libmisc/tests/test_assert.c @@ -6,7 +6,6 @@ #include #include /* for va_list, va_start(), va_end() */ -#include #include #include diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c index 8076155..3596d94 100644 --- a/libmisc/tests/test_rand.c +++ b/libmisc/tests/test_rand.c @@ -1,10 +1,9 @@ /* libmisc/tests/test_rand.c - Tests for * - * Copyright (C) 2024 Luke T. Shumaker + * Copyright (C) 2024-2025 Luke T. Shumaker * SPDX-License-Identifier: AGPL-3.0-or-later */ -#include #include #include -- cgit v1.2.3-2-g168b From a83c95e9f46ef695a55fc7a6911e11846da9903c Mon Sep 17 00:00:00 2001 From: "Luke T. Shumaker" Date: Wed, 23 Apr 2025 08:01:36 -0600 Subject: Merge libobj into libmisc --- libmisc/tests/test_obj.c | 61 ++++++++++++++++++++++++++++++++++++ libmisc/tests/test_obj_nest.c | 73 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 134 insertions(+) create mode 100644 libmisc/tests/test_obj.c create mode 100644 libmisc/tests/test_obj_nest.c (limited to 'libmisc/tests') diff --git a/libmisc/tests/test_obj.c b/libmisc/tests/test_obj.c new file mode 100644 index 0000000..687ad4e --- /dev/null +++ b/libmisc/tests/test_obj.c @@ -0,0 +1,61 @@ +/* libmisc/tests/test_obj.c - Tests for + * + * Copyright (C) 2024-2025 Luke T. Shumaker + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include + +#include "test.h" + +/* `lo_inteface frobber` header ***********************************************/ + +#define frobber_LO_IFACE \ + /** Basic function. */ \ + LO_FUNC(int, frob) \ + /** Function that takes 1 argument. */ \ + LO_FUNC(int, frob1, int) \ + /** Function that returns nothing. */ \ + LO_FUNC(void, frob0) +LO_INTERFACE(frobber); + +/* `struct myclass` header ****************************************************/ + +struct myclass { + int a; +}; +LO_IMPLEMENTATION_H(frobber, struct myclass, myclass); + +/* `struct myclass` implementation ********************************************/ + +LO_IMPLEMENTATION_C(frobber, struct myclass, myclass, static); + +static int myclass_frob(struct myclass *self) { + test_assert(self); + return self->a; +} + +static int myclass_frob1(struct myclass *self, int arg) { + test_assert(self); + return arg; +} + +static void myclass_frob0(struct myclass *self) { + test_assert(self); +} + +/* main test body *************************************************************/ + +#define MAGIC1 909837 +#define MAGIC2 657441 + +int main() { + struct myclass obj = { + .a = MAGIC1, + }; + 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); + return 0; +} diff --git a/libmisc/tests/test_obj_nest.c b/libmisc/tests/test_obj_nest.c new file mode 100644 index 0000000..bb9d6de --- /dev/null +++ b/libmisc/tests/test_obj_nest.c @@ -0,0 +1,73 @@ +/* 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(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 = {0}; + lo_interface read_writer obj = lo_box_myclass_as_read_writer(&_obj); + test_assert(LO_CALL(obj, write, "Hello", 6) == 6); + char buf[6] = {0}; + test_assert(LO_CALL(obj, read, buf, 3) == 3); + test_assert(memcmp(buf, "Hel\0\0\0", 6) == 0); + return 0; +} -- cgit v1.2.3-2-g168b