diff options
Diffstat (limited to 'libmisc/tests')
-rw-r--r-- | libmisc/tests/test.h | 21 | ||||
-rw-r--r-- | libmisc/tests/test_assert.c | 94 | ||||
-rw-r--r-- | libmisc/tests/test_assert_min.c | 17 | ||||
-rw-r--r-- | libmisc/tests/test_endian.c | 41 | ||||
-rw-r--r-- | libmisc/tests/test_hash.c | 30 | ||||
-rw-r--r-- | libmisc/tests/test_log.c | 70 | ||||
-rw-r--r-- | libmisc/tests/test_macro.c | 54 | ||||
-rw-r--r-- | libmisc/tests/test_map.c | 60 | ||||
-rw-r--r-- | libmisc/tests/test_private.c | 29 | ||||
-rw-r--r-- | libmisc/tests/test_rand.c | 79 |
10 files changed, 495 insertions, 0 deletions
diff --git a/libmisc/tests/test.h b/libmisc/tests/test.h new file mode 100644 index 0000000..ea13d3c --- /dev/null +++ b/libmisc/tests/test.h @@ -0,0 +1,21 @@ +/* libmisc/tests/test.h - Common test utilities + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#ifndef _LIBMISC_TESTS_TEST_H_ +#define _LIBMISC_TESTS_TEST_H_ + +#include <stdio.h> +#include <stdlib.h> /* for exit() */ + +#define test_assert(expr) do { \ + if (!(expr)) { \ + printf("test failure: %s:%d:%s: %s\n", \ + __FILE__, __LINE__, __func__, #expr); \ + exit(1); \ + } \ + } while (0) + +#endif /* _LIBMISC_TESTS_TEST_H_ */ diff --git a/libmisc/tests/test_assert.c b/libmisc/tests/test_assert.c new file mode 100644 index 0000000..15f9446 --- /dev/null +++ b/libmisc/tests/test_assert.c @@ -0,0 +1,94 @@ +/* libmisc/tests/test_assert.c - Tests for <libmisc/assert.h> + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <setjmp.h> +#include <stdarg.h> /* for va_list, va_start(), va_end() */ +#include <stdbool.h> +#include <stdlib.h> +#include <string.h> + +#include <libmisc/macro.h> +#include <libmisc/assert.h> +#include <libmisc/_intercept.h> + +#include "test.h" + +/* Intercept failures and logging *********************************************/ + +bool global_failed; +char *global_log; +jmp_buf global_env; + +#define with_intercept() ({ \ + global_failed = false; \ + if (global_log) \ + free(global_log); \ + global_log = NULL; \ + setjmp(global_env) == 0; \ + }) + +void __lm_abort(void) { + global_failed = true; + longjmp(global_env, 1); +} + +int __lm_light_printf(const char *format, ...) { + va_list va; + va_start(va, format); + int ret = vasprintf(&global_log, format, va); + va_end(va); + return ret; +} + +#define __builtin_unreachable() test_assert(0) + +/* Utilities ******************************************************************/ + +#define test_should_succeed(test) do { \ + if (with_intercept()) { \ + test; \ + } \ + test_assert(global_failed == false); \ + test_assert(global_log == NULL); \ + } while (0) + +#define test_should_fail(test, exp_log) do { \ + if (with_intercept()) { \ + test; \ + } \ + test_assert(global_failed == true); \ + if (!(global_log != NULL && \ + strcmp(global_log, exp_log) == 0)) { \ + printf("exp = \"%s\"\n" \ + "act = \"%s\"\n", \ + exp_log, global_log); \ + test_assert(0); \ + } \ + } while (0) + +/* Actual tests ***************************************************************/ + +static_assert(sizeof(char) == 1); + +int main() { +#ifndef NDEBUG + test_should_succeed(assert(true)); + test_should_fail(assert(false), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"false\" failed\n"); + + test_should_succeed(assert_msg(true, "foo")); + test_should_fail(assert_msg(false, "foo"), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"false\" failed: foo\n"); + test_should_succeed(assert_msg(true, NULL)); + test_should_fail(assert_msg(false, NULL), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"false\" failed\n"); + + test_should_fail(assert_notreached("xxx"), "error: ASSERT: "__FILE__":"LM_STR_(__LINE__)":main(): assertion \"notreached\" failed: xxx\n"); + + if (global_log) { + free(global_log); + global_log = NULL; + } +#endif + return 0; +} diff --git a/libmisc/tests/test_assert_min.c b/libmisc/tests/test_assert_min.c new file mode 100644 index 0000000..9c0394b --- /dev/null +++ b/libmisc/tests/test_assert_min.c @@ -0,0 +1,17 @@ +/* libmisc/tests/test_assert_min.c - Tests for minimal <libmisc/assert.h> + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +/* Don't include *anything* else. */ +#include <libmisc/assert.h> + +static_assert(1 == 1); + +int main() { + assert_msg(1, "foo"); + assert(1); + return 0; + assert_notreached("ret"); +} diff --git a/libmisc/tests/test_endian.c b/libmisc/tests/test_endian.c new file mode 100644 index 0000000..dcb3cc2 --- /dev/null +++ b/libmisc/tests/test_endian.c @@ -0,0 +1,41 @@ +/* libmisc/tests/test_endian.c - Tests for <libmisc/endian.h> + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <string.h> /* for memcmp() */ + +#include <libmisc/endian.h> + +#include "test.h" + +int main() { + uint8_t act[(2+4+8)*2] = {0}; + size_t pos = 0; + pos += uint16be_encode(&act[pos], UINT16_C(0x1234)); + pos += uint32be_encode(&act[pos], UINT32_C(0x56789ABC)); + pos += uint64be_encode(&act[pos], UINT64_C(0xAC589A93278CB30A)); + pos += uint16le_encode(&act[pos], UINT16_C(0x1234)); + pos += uint32le_encode(&act[pos], UINT32_C(0x56789ABC)); + pos += uint64le_encode(&act[pos], UINT64_C(0xAC589A93278CB30A)); + + test_assert(pos == sizeof(act)); + uint8_t exp[(2+4+8)*2] = { 0x12, 0x34, + 0x56, 0x78, 0x9A, 0xBC, + 0xAC, 0x58, 0x9A, 0x93, 0x27, 0x8C, 0xB3, 0x0A, + 0x34, 0x12, + 0xBC, 0x9A, 0x78, 0x56, + 0x0A, 0xB3, 0x8C, 0x27, 0x93, 0x9A, 0x58, 0xAC}; + test_assert(memcmp(act, exp, sizeof(act)) == 0); + + pos = 0; + test_assert(uint16be_decode(&act[pos]) == UINT16_C(0x1234)); pos += 2; + test_assert(uint32be_decode(&act[pos]) == UINT32_C(0x56789ABC)); pos += 4; + test_assert(uint64be_decode(&act[pos]) == UINT64_C(0xAC589A93278CB30A)); pos += 8; + test_assert(uint16le_decode(&act[pos]) == UINT16_C(0x1234)); pos += 2; + test_assert(uint32le_decode(&act[pos]) == UINT32_C(0x56789ABC)); pos += 4; + test_assert(uint64le_decode(&act[pos]) == UINT64_C(0xAC589A93278CB30A)); pos += 8; + + return 0; +} diff --git a/libmisc/tests/test_hash.c b/libmisc/tests/test_hash.c new file mode 100644 index 0000000..c1af385 --- /dev/null +++ b/libmisc/tests/test_hash.c @@ -0,0 +1,30 @@ +/* libmisc/tests/test_hash.c - Tests for <libmisc/hash.h> + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libmisc/hash.h> + +#include "test.h" + +int main() { + test_assert(hash("hello", sizeof("hello")) == hash("hello", sizeof("hello"))); + test_assert(hash("hello", sizeof("hello")) != hash("Hello", sizeof("Hello"))); + int one = 1; + int two = 2; + test_assert(hash(&one, sizeof(int)) != hash("hello", sizeof("hello"))); + test_assert(hash(&one, sizeof(int)) == hash(&one, sizeof(int))); + test_assert(hash(&one, sizeof(int)) != hash(&two, sizeof(int))); + + hash_t myhash; + hash_init(&myhash); + hash_write(&myhash, "hello", 5); + test_assert(myhash == hash("hello", 5)); + hash_t myhash_a = myhash; + hash_write(&myhash, "world", 5); + test_assert(myhash != myhash_a); + test_assert(myhash == hash("helloworld", 10)); + + return 0; +} diff --git a/libmisc/tests/test_log.c b/libmisc/tests/test_log.c new file mode 100644 index 0000000..49a76ca --- /dev/null +++ b/libmisc/tests/test_log.c @@ -0,0 +1,70 @@ +/* libmisc/tests/test_log.c - Tests for <libmisc/log.h> + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#define _GNU_SOURCE /* for vasprintf() */ +#include <stdarg.h> /* for va_list */ +#include <stdio.h> /* for vasprintf() */ +#include <stdlib.h> /* for free() */ +#include <string.h> /* for strcmp() */ + +#define LOG_NAME FROBNICATE +#include <libmisc/log.h> + +#include <libmisc/_intercept.h> + +#include "test.h" + +/* Intercept output ***********************************************************/ + +static char *log_output = NULL; + +int __lm_printf(const char *format, ...) { + va_list va; + va_start(va, format); + int ret = vasprintf(&log_output, format, va); + va_end(va); + return ret; +} + +/* Actual tests ***************************************************************/ + +#define should_print(_exp, cmd) do { \ + char *exp = _exp; \ + test_assert(!log_output); \ + cmd; \ + if (!exp) \ + test_assert(!log_output); \ + else { \ + test_assert(log_output); \ + if (strcmp(log_output, exp)) { \ + printf("exp = \"%s\"\n" \ + "act = \"%s\"\n", \ + exp, log_output); \ + test_assert(0); \ + } \ + } \ + if (log_output) { \ + free(log_output); \ + log_output = NULL; \ + } \ + } while (0) + +int main() { + should_print("error: FROBNICATE: val=42\n", + errorf("val=%d", 42)); + should_print("info : FROBNICATE: val=0\n", + infof("val=%d", 0)); +#ifndef NDEBUG +#define CONFIG_FROBNICATE_DEBUG 1 + should_print("debug: FROBNICATE: val=-2\n", + debugf("val=%d", -2)); +#undef CONFIG_FROBNICATE_DEBUG +#define CONFIG_FROBNICATE_DEBUG 0 + should_print(NULL, + debugf("val=%d", -2)); +#endif + return 0; +} diff --git a/libmisc/tests/test_macro.c b/libmisc/tests/test_macro.c new file mode 100644 index 0000000..1320eb3 --- /dev/null +++ b/libmisc/tests/test_macro.c @@ -0,0 +1,54 @@ +/* libmisc/tests/test_macro.c - Tests for <libmisc/macro.h> + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libmisc/macro.h> + +#include "test.h" + +int main() { + printf("== LM_NEXT_POWER_OF_2 =====================================\n"); + /* valid down to 0. */ + test_assert(LM_NEXT_POWER_OF_2(0) == 1); + test_assert(LM_NEXT_POWER_OF_2(1) == 2); + test_assert(LM_NEXT_POWER_OF_2(2) == 4); + test_assert(LM_NEXT_POWER_OF_2(3) == 4); + test_assert(LM_NEXT_POWER_OF_2(4) == 8); + test_assert(LM_NEXT_POWER_OF_2(5) == 8); + test_assert(LM_NEXT_POWER_OF_2(6) == 8); + test_assert(LM_NEXT_POWER_OF_2(7) == 8); + test_assert(LM_NEXT_POWER_OF_2(8) == 16); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(16) == 32); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(0x7000000000000000) == 0x8000000000000000); + /* ... */ + test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000-1) == 0x8000000000000000); + /* Valid up to 0x8000000000000000-1 = (1<<63)-1 */ + + printf("== LM_FLOORLOG2 ===========================================\n"); + /* valid down to 1. */ + test_assert(LM_FLOORLOG2(1) == 0); + test_assert(LM_FLOORLOG2(2) == 1); + test_assert(LM_FLOORLOG2(3) == 1); + test_assert(LM_FLOORLOG2(4) == 2); + test_assert(LM_FLOORLOG2(5) == 2); + test_assert(LM_FLOORLOG2(6) == 2); + test_assert(LM_FLOORLOG2(7) == 2); + test_assert(LM_FLOORLOG2(8) == 3); + /* ... */ + test_assert(LM_FLOORLOG2(16) == 4); + /* ... */ + test_assert(LM_FLOORLOG2(0x80000000) == 31); + /* ... */ + test_assert(LM_FLOORLOG2(0xFFFFFFFF) == 31); + test_assert(LM_FLOORLOG2(0x100000000) == 32); + /* ... */ + test_assert(LM_FLOORLOG2(0x8000000000000000) == 63); + /* ... */ + test_assert(LM_FLOORLOG2(0xFFFFFFFFFFFFFFFF) == 63); + + return 0; +} diff --git a/libmisc/tests/test_map.c b/libmisc/tests/test_map.c new file mode 100644 index 0000000..855dace --- /dev/null +++ b/libmisc/tests/test_map.c @@ -0,0 +1,60 @@ +/* libmisc/tests/test_map.c - Tests for <libmisc/map.h> + * + * Copyright (C) 2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libmisc/map.h> + +#include "test.h" + +MAP_DECLARE(intmap, int, int); + +int main() { + struct intmap m = {}; + test_assert(map_len(&m) == 0); + + int *v = map_store(&m, 3, 8); + test_assert(v && *v == 8); + test_assert(map_len(&m) == 1); + + v = map_load(&m, 3); + test_assert(v); + test_assert(*v == 8); + + v = NULL; + + test_assert(map_del(&m, 3)); + test_assert(map_len(&m) == 0); + test_assert(!map_del(&m, 3)); + + map_store(&m, 1, 11); + map_store(&m, 2, 12); + map_store(&m, 3, 13); + test_assert(map_len(&m) == 3); + bool seen_1 = false, seen_2 = false, seen_3 = false; + MAP_FOREACH(&m, ik, iv) { + switch (ik) { + case 1: seen_1 = true; break; + case 2: seen_2 = true; break; + case 3: seen_3 = true; break; + } + switch (ik) { + case 1: case 2: case 3: + map_store(&m, ik+20, (*iv)+20); + test_assert(map_del(&m, ik)); + test_assert(!map_del(&m, ik)); + test_assert(map_load(&m, ik) == NULL); + break; + } + } + test_assert(map_len(&m) == 3); + test_assert(seen_1); v = map_load(&m, 21); test_assert(v && *v == 31); v = map_load(&m, 1); test_assert(!v); + test_assert(seen_2); v = map_load(&m, 22); test_assert(v && *v == 32); v = map_load(&m, 2); test_assert(!v); + test_assert(seen_3); v = map_load(&m, 23); test_assert(v && *v == 33); v = map_load(&m, 3); test_assert(!v); + + map_free(&m); + test_assert(map_len(&m) == 0); + + return 0; +} diff --git a/libmisc/tests/test_private.c b/libmisc/tests/test_private.c new file mode 100644 index 0000000..024dddb --- /dev/null +++ b/libmisc/tests/test_private.c @@ -0,0 +1,29 @@ +/* libmisc/tests/test_private.c - Tests for <libmisc/private.h> + * + * Copyright (C) 2024-2025 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <libmisc/private.h> + +struct a { + int foo; + BEGIN_PRIVATE(A); + int bar; + END_PRIVATE(A); +}; + +#define IMPLEMENTATION_FOR_B YES + +struct b { + int foo; + BEGIN_PRIVATE(B); + int bar; + END_PRIVATE(B); +}; + +int main() { + struct b obj; + obj.bar = 0; + return obj.bar; +} diff --git a/libmisc/tests/test_rand.c b/libmisc/tests/test_rand.c new file mode 100644 index 0000000..8076155 --- /dev/null +++ b/libmisc/tests/test_rand.c @@ -0,0 +1,79 @@ +/* libmisc/tests/test_rand.c - Tests for <libmisc/rand.h> + * + * Copyright (C) 2024 Luke T. Shumaker <lukeshu@lukeshu.com> + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +#include <stdbool.h> +#include <setjmp.h> + +#include <libmisc/rand.h> +#include <libmisc/_intercept.h> + +#include "test.h" + +/* Intercept failures *********************************************************/ + +#ifndef NDEBUG +jmp_buf *__catch; + +void __lm_abort(void) { + if (__catch) + longjmp(*__catch, 1); + abort(); +} + +#define should_abort(cmd) do { \ + jmp_buf *old_catch = __catch; \ + jmp_buf env; \ + __catch = &env; \ + if (!setjmp(env)) { \ + cmd; \ + __catch = old_catch; \ + test_assert(false); \ + } else { \ + __catch = old_catch; \ + } \ + } while (0); +#endif + +/* Actual tests ***************************************************************/ + +#define ROUNDS 4096 +#define MAX_SEE_ALL 128 + +static void test_n(uint64_t cnt) { + if (cnt == 0 || cnt > UINT64_C(1)<<63) { +#ifndef NDEBUG + should_abort(rand_uint63n(cnt)); +#else + return; +#endif + } else { + double sum = 0; + bool seen[MAX_SEE_ALL] = {0}; + for (int i = 0; i < ROUNDS; i++) { + uint64_t val = rand_uint63n(cnt); + sum += val; + test_assert(val < cnt); + if (cnt < MAX_SEE_ALL) + seen[val] = true; + } + if (cnt > 1) { + test_assert(sum/ROUNDS > 0.45*(cnt-1)); + test_assert(sum/ROUNDS < 0.55*(cnt-1)); + } + if (cnt < MAX_SEE_ALL) { + for (uint64_t i = 0; i < cnt; i++) + test_assert(seen[i]); + } + } +} + +int main() { + for (uint8_t i = 0; i < 64; i++) + test_n(UINT64_C(1)<<i); + for (uint64_t j = 0; j < MAX_SEE_ALL; j++) + test_n(j); + return 0; +} |