diff options
author | David Robillard <d@drobilla.net> | 2024-06-26 18:48:02 -0400 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2024-06-26 18:52:29 -0400 |
commit | aec0d760835219f91eb483c68ef7b2c8f9e8597e (patch) | |
tree | e732377408502d555e53cd67ddf86986da3cee2c /test/test_string_view.c | |
parent | c147881d999fa966ea266d6feac2a3fabeef511b (diff) | |
download | zix-aec0d760835219f91eb483c68ef7b2c8f9e8597e.tar.gz zix-aec0d760835219f91eb483c68ef7b2c8f9e8597e.tar.bz2 zix-aec0d760835219f91eb483c68ef7b2c8f9e8597e.zip |
Add zix_string_view_equals()
Diffstat (limited to 'test/test_string_view.c')
-rw-r--r-- | test/test_string_view.c | 118 |
1 files changed, 118 insertions, 0 deletions
diff --git a/test/test_string_view.c b/test/test_string_view.c new file mode 100644 index 0000000..52b824d --- /dev/null +++ b/test/test_string_view.c @@ -0,0 +1,118 @@ +// Copyright 2021-2024 David Robillard <d@drobilla.net> +// SPDX-License-Identifier: ISC + +#undef NDEBUG + +#include "failing_allocator.h" + +#include "zix/string_view.h" + +#include <assert.h> +#include <string.h> + +static void +test_static_init(void) +{ + static const ZixStringView a = ZIX_STATIC_STRING("a"); + static const ZixStringView ab = ZIX_STATIC_STRING("ab"); + + assert(a.length == 1U); + assert(a.data); + assert(a.data[0] == 'a'); + assert(a.data[1] == '\0'); + + assert(ab.length == 2U); + assert(ab.data[0] == 'a'); + assert(ab.data[1] == 'b'); + assert(ab.data[2] == '\0'); +} + +static void +test_empty(void) +{ + const ZixStringView empty = zix_empty_string(); + + assert(!empty.length); + assert(empty.data); + assert(empty.data[0] == '\0'); +} + +static void +test_string(void) +{ + const ZixStringView nodata = zix_string(NULL); + const ZixStringView empty = zix_string(""); + + assert(!nodata.length); + assert(nodata.data); + assert(nodata.data[0] == '\0'); + + assert(!empty.length); + assert(empty.data); + assert(empty.data[0] == '\0'); +} + +static void +test_equals(void) +{ + static const char* const prefix_str = "prefix"; + + const ZixStringView prefix = zix_string(prefix_str); + const ZixStringView pre = zix_substring(prefix_str, 3U); + const ZixStringView fix = zix_substring(prefix_str + 3U, 3U); + const ZixStringView suffix1 = zix_substring("suffix_1", 6U); + const ZixStringView suffix2 = zix_substring("suffix_2", 6U); + + assert(prefix.length == 6U); + assert(pre.length == 3U); + assert(fix.length == 3U); + assert(suffix1.length == 6U); + assert(suffix2.length == 6U); + + assert(zix_string_view_equals(prefix, zix_string("prefix"))); + assert(zix_string_view_equals(pre, zix_string("pre"))); + assert(zix_string_view_equals(fix, zix_string("fix"))); + assert(zix_string_view_equals(suffix1, zix_string("suffix"))); + assert(zix_string_view_equals(suffix2, zix_string("suffix"))); + + assert(zix_string_view_equals(prefix, prefix)); + assert(zix_string_view_equals(suffix1, suffix2)); + + assert(!zix_string_view_equals(prefix, pre)); + assert(!zix_string_view_equals(pre, prefix)); + assert(!zix_string_view_equals(pre, fix)); + assert(!zix_string_view_equals(fix, prefix)); + assert(!zix_string_view_equals(suffix1, prefix)); + assert(!zix_string_view_equals(prefix, suffix1)); +} + +static void +test_copy(void) +{ + static const ZixStringView orig = ZIX_STATIC_STRING("string"); + + ZixFailingAllocator allocator = zix_failing_allocator(); + + // Copying a string takes exactly one allocation + allocator.n_remaining = 1U; + + char* const copy = zix_string_view_copy(&allocator.base, orig); + assert(copy); + assert(!strcmp(copy, "string")); + zix_free(&allocator.base, copy); + + // Check that allocation failure is handled gracefully + allocator.n_remaining = 0U; + assert(!zix_string_view_copy(&allocator.base, orig)); +} + +int +main(void) +{ + test_static_init(); + test_empty(); + test_string(); + test_equals(); + test_copy(); + return 0; +} |