diff options
Diffstat (limited to 'test')
-rw-r--r-- | test/.clang-tidy | 5 | ||||
-rw-r--r-- | test/failing_allocator.c | 18 | ||||
-rw-r--r-- | test/failing_allocator.h | 13 | ||||
-rw-r--r-- | test/meson.build | 2 | ||||
-rw-r--r-- | test/test_allocator.c | 2 | ||||
-rw-r--r-- | test/test_btree.c | 4 | ||||
-rw-r--r-- | test/test_filesystem.c | 4 | ||||
-rw-r--r-- | test/test_hash.c | 4 | ||||
-rw-r--r-- | test/test_ring.c | 4 | ||||
-rw-r--r-- | test/test_string_view.c | 5 | ||||
-rw-r--r-- | test/test_tree.c | 4 |
11 files changed, 41 insertions, 24 deletions
diff --git a/test/.clang-tidy b/test/.clang-tidy index 2d684a6..f869db0 100644 --- a/test/.clang-tidy +++ b/test/.clang-tidy @@ -1,16 +1,17 @@ -# Copyright 2020-2022 David Robillard <d@drobilla.net> +# Copyright 2020-2024 David Robillard <d@drobilla.net> # SPDX-License-Identifier: 0BSD OR ISC Checks: > + -*-macro-to-enum, -*-magic-numbers, -android-cloexec-fopen, -bugprone-easily-swappable-parameters, + -bugprone-multi-level-implicit-pointer-conversion, -cert-err33-c, -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling, -cppcoreguidelines-avoid-non-const-global-variables, -google-readability-casting, -llvm-header-guard, - -modernize-macro-to-enum, -performance-no-int-to-ptr, -readability-function-cognitive-complexity, InheritParentConfig: true diff --git a/test/failing_allocator.c b/test/failing_allocator.c index 684a8ec..53a5216 100644 --- a/test/failing_allocator.c +++ b/test/failing_allocator.c @@ -1,10 +1,10 @@ -// Copyright 2021 David Robillard <d@drobilla.net> +// Copyright 2021-2024 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC #include "failing_allocator.h" -#include "zix/allocator.h" -#include "zix/attributes.h" +#include <zix/allocator.h> +#include <zix/attributes.h> #include <stdbool.h> #include <stddef.h> @@ -107,3 +107,15 @@ zix_failing_allocator(void) return failing_allocator; } + +size_t +zix_failing_allocator_reset(ZixFailingAllocator* const allocator, + const size_t n_allowed) +{ + const size_t n_allocations = allocator->n_allocations; + + allocator->n_allocations = 0U; + allocator->n_remaining = n_allowed; + + return n_allocations; +} diff --git a/test/failing_allocator.h b/test/failing_allocator.h index 982874d..5f6d220 100644 --- a/test/failing_allocator.h +++ b/test/failing_allocator.h @@ -1,8 +1,8 @@ -// Copyright 2021 David Robillard <d@drobilla.net> +// Copyright 2021-2024 David Robillard <d@drobilla.net> // SPDX-License-Identifier: ISC -#ifndef ZIX_FAILING_ALLOCATOR_H -#define ZIX_FAILING_ALLOCATOR_H +#ifndef ZIX_TEST_FAILING_ALLOCATOR_H +#define ZIX_TEST_FAILING_ALLOCATOR_H #include "zix/allocator.h" @@ -15,7 +15,12 @@ typedef struct { size_t n_remaining; ///< Number of remaining successful allocations } ZixFailingAllocator; +/// Return an allocator configured by default to succeed ZixFailingAllocator zix_failing_allocator(void); -#endif // ZIX_FAILING_ALLOCATOR_H +/// Reset an allocator to fail after some number of "allowed" allocations +size_t +zix_failing_allocator_reset(ZixFailingAllocator* allocator, size_t n_allowed); + +#endif // ZIX_TEST_FAILING_ALLOCATOR_H diff --git a/test/meson.build b/test/meson.build index 96fe455..360d61d 100644 --- a/test/meson.build +++ b/test/meson.build @@ -44,12 +44,12 @@ if not meson.is_subproject() and get_option('lint') 'cppcheck', cppcheck, args: [ - '--check-level=exhaustive', '--enable=warning,style,performance,portability', '--error-exitcode=1', '--project=' + compdb_path, '--suppress=constParameterCallback', '--suppress=constParameterPointer', + '--suppress=normalCheckLevelMaxBranches', '--suppress=unreadVariable', '-q', ], diff --git a/test/test_allocator.c b/test/test_allocator.c index 9ecbfa0..2677624 100644 --- a/test/test_allocator.c +++ b/test/test_allocator.c @@ -122,7 +122,7 @@ static void test_failing_allocator(void) { ZixFailingAllocator allocator = zix_failing_allocator(); - allocator.n_remaining = 0; + zix_failing_allocator_reset(&allocator, 0); assert(!zix_malloc(&allocator.base, 16U)); assert(!zix_calloc(&allocator.base, 16U, 1U)); diff --git a/test/test_btree.c b/test/test_btree.c index e918799..a3183c6 100644 --- a/test/test_btree.c +++ b/test/test_btree.c @@ -563,9 +563,9 @@ test_failed_alloc(void) assert(!stress(&allocator.base, 0, 4096)); // Test that each allocation failing is handled gracefully - const size_t n_new_allocs = allocator.n_allocations; + const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0); for (size_t i = 0U; i < n_new_allocs; ++i) { - allocator.n_remaining = i; + zix_failing_allocator_reset(&allocator, i); assert(stress(&allocator.base, 0, 4096)); } } diff --git a/test/test_filesystem.c b/test/test_filesystem.c index 87a3d15..e6180fb 100644 --- a/test/test_filesystem.c +++ b/test/test_filesystem.c @@ -172,9 +172,7 @@ test_file_type(void) assert(!zix_remove(file_path)); close(fd); } - } else { - fprintf(stderr, "warning: Skipped socket test with oddly long TMPDIR\n"); - } + } // otherwise, TMPDIR is oddly long, skip test close(sock); free(addr); diff --git a/test/test_hash.c b/test/test_hash.c index 3d3ca95..8ee6b82 100644 --- a/test/test_hash.c +++ b/test/test_hash.c @@ -366,9 +366,9 @@ test_failed_alloc(void) assert(!stress(&allocator.base, 16)); // Test that each allocation failing is handled gracefully - const size_t n_new_allocs = allocator.n_allocations; + const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0); for (size_t i = 0U; i < n_new_allocs; ++i) { - allocator.n_remaining = i; + zix_failing_allocator_reset(&allocator, i); assert(stress(&allocator.base, 16)); } } diff --git a/test/test_ring.c b/test/test_ring.c index b1845ce..06d50ee 100644 --- a/test/test_ring.c +++ b/test/test_ring.c @@ -170,9 +170,9 @@ test_failed_alloc(void) assert(ring); // Test that each allocation failing is handled gracefully - const size_t n_new_allocs = allocator.n_allocations; + const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0); for (size_t i = 0U; i < n_new_allocs; ++i) { - allocator.n_remaining = i; + zix_failing_allocator_reset(&allocator, i); assert(!zix_ring_new(&allocator.base, 512)); } diff --git a/test/test_string_view.c b/test/test_string_view.c index 52b824d..764edcb 100644 --- a/test/test_string_view.c +++ b/test/test_string_view.c @@ -5,6 +5,7 @@ #include "failing_allocator.h" +#include "zix/allocator.h" #include "zix/string_view.h" #include <assert.h> @@ -94,7 +95,7 @@ test_copy(void) ZixFailingAllocator allocator = zix_failing_allocator(); // Copying a string takes exactly one allocation - allocator.n_remaining = 1U; + zix_failing_allocator_reset(&allocator, 1U); char* const copy = zix_string_view_copy(&allocator.base, orig); assert(copy); @@ -102,7 +103,7 @@ test_copy(void) zix_free(&allocator.base, copy); // Check that allocation failure is handled gracefully - allocator.n_remaining = 0U; + zix_failing_allocator_reset(&allocator, 0U); assert(!zix_string_view_copy(&allocator.base, orig)); } diff --git a/test/test_tree.c b/test/test_tree.c index fae6d85..ff63999 100644 --- a/test/test_tree.c +++ b/test/test_tree.c @@ -211,9 +211,9 @@ test_failed_alloc(void) assert(!stress(&allocator.base, 0, 16)); // Test that each allocation failing is handled gracefully - const size_t n_new_allocs = allocator.n_allocations; + const size_t n_new_allocs = zix_failing_allocator_reset(&allocator, 0); for (size_t i = 0U; i < n_new_allocs; ++i) { - allocator.n_remaining = i; + zix_failing_allocator_reset(&allocator, i); assert(stress(&allocator.base, 0, 16)); } } |