diff options
| author | Mosder <xmosder12@gmail.com> | 2026-08-21 11:50:51 +0200 |
|---|---|---|
| committer | Mosder <xmosder12@gmail.com> | 2026-08-21 11:50:51 +0200 |
| commit | b6b7a01987dca604a161c8c73a240ec4430baed0 (patch) | |
| tree | b9fdeb674ad767ccb16d1a3cdc2c6c975ea11737 /tests | |
| parent | 890a52b9af6e24015048eb2754552b9613484b3f (diff) | |
refactor: renamed mem/alloc.h to safe.h and made it be a collection of "safe" wrappers for "unsafe" functions
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/mem/alloc_tests.h | 3 | ||||
| -rw-r--r-- | tests/safe_tests.c (renamed from tests/mem/alloc_tests.c) | 27 | ||||
| -rw-r--r-- | tests/safe_tests.h | 3 | ||||
| -rw-r--r-- | tests/tests.c | 4 |
4 files changed, 25 insertions, 12 deletions
diff --git a/tests/mem/alloc_tests.h b/tests/mem/alloc_tests.h deleted file mode 100644 index aaf70a4..0000000 --- a/tests/mem/alloc_tests.h +++ /dev/null @@ -1,3 +0,0 @@ -#include <moslib/tester.h> - -extern void mem_alloc_tests(Tester *tester); diff --git a/tests/mem/alloc_tests.c b/tests/safe_tests.c index 5b71887..1d48a48 100644 --- a/tests/mem/alloc_tests.c +++ b/tests/safe_tests.c @@ -1,6 +1,5 @@ -#include "alloc_tests.h" -#define MOS_MAX_ALLOC_ATTEMPTS 1 -#include "moslib/mem/alloc.h" +#include "safe_tests.h" +#include "moslib/safe.h" #include <limits.h> #include <stdlib.h> @@ -13,7 +12,7 @@ Test test_safe_malloc() { void *p = safe_malloc(1024); test_assert(p != NULL, "Failed to malloc 1KiB"); - suppress_output(&stderr); + suppress_output(stderr); test_assert_exit(malloc_exit, EXIT_FAILURE); unsuppress_outputs(); } @@ -23,6 +22,19 @@ Test realloc_exit() { safe_realloc(p, ULLONG_MAX); } +Test calloc_exit() { + safe_calloc(ULLONG_MAX, ULLONG_MAX); +} + +Test test_safe_calloc() { + void *p = safe_calloc(16, 16); + test_assert(p != NULL, "Failed to calloc 16 items of 16 bytes"); + + suppress_output(stderr); + test_assert_exit(calloc_exit, EXIT_FAILURE); + unsuppress_outputs(); +} + Test test_safe_realloc() { void *p = NULL; p = safe_realloc(p, 1024); @@ -31,13 +43,14 @@ Test test_safe_realloc() { p = safe_realloc(p, 4096); test_assert(p != NULL, "Failed to realloc 1KiB to 4KiB"); - suppress_output(&stderr); + suppress_output(stderr); test_assert_exit(realloc_exit, EXIT_FAILURE); unsuppress_outputs(); } -void mem_alloc_tests(Tester *tester) { - TestGroup *group = add_test_group(tester, "moslib/mem/alloc.h"); +void safe_tests(Tester *tester) { + TestGroup *group = add_test_group(tester, "moslib/safe.h"); add_test(group, test_safe_malloc); + add_test(group, test_safe_calloc); add_test(group, test_safe_realloc); } diff --git a/tests/safe_tests.h b/tests/safe_tests.h new file mode 100644 index 0000000..8127b4c --- /dev/null +++ b/tests/safe_tests.h @@ -0,0 +1,3 @@ +#include <moslib/tester.h> + +extern void safe_tests(Tester *tester); diff --git a/tests/tests.c b/tests/tests.c index d7ce15b..648ab4c 100644 --- a/tests/tests.c +++ b/tests/tests.c @@ -2,13 +2,13 @@ #include "ds/array_tests.h" #include "ds/hashmap_tests.h" -#include "mem/alloc_tests.h" +#include "safe_tests.h" #include "string_tests.h" int main(void) { Tester *tester = new_tester(); - mem_alloc_tests(tester); + safe_tests(tester); ds_array_tests(tester); string_tests(tester); ds_hashmap_tests(tester); |
