diff options
-rw-r--r-- | src/cmocka.c | 8 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/test_buffer.c | 48 |
3 files changed, 57 insertions, 0 deletions
diff --git a/src/cmocka.c b/src/cmocka.c index 0cbc342..ca67fd5 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -1636,6 +1636,14 @@ static int string_not_equal_display_error( return 0; } +static bool all_zero(const uint8_t *buf, size_t len) +{ + if (buf == NULL || len == 0) { + return true; + } + + return buf[0] == '\0' && memcmp(buf, buf + 1, len - 1) == 0; +} /* * Determine whether the specified areas of memory are equal. If they're equal diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index b048637..01ebf31 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -17,6 +17,7 @@ endif() set(CMOCKA_TESTS test_alloc + test_buffer test_expect_check test_expect_u_int_in_set test_expect_check_fail diff --git a/tests/test_buffer.c b/tests/test_buffer.c new file mode 100644 index 0000000..00627bd --- /dev/null +++ b/tests/test_buffer.c @@ -0,0 +1,48 @@ +#include "config.h" + +#include <stdarg.h> +#include <stddef.h> +#include <setjmp.h> +#include <stdint.h> +#include <cmocka.h> +#include <cmocka_private.h> + +#include "../src/cmocka.c" + +static const uint8_t buf0[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static const uint8_t buf1[] = { + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, +}; + +static void test_all_zero(void **state) +{ + (void)state; + + assert_true(all_zero(buf0, sizeof(buf0))); + + assert_true(all_zero(NULL, 0)); + + assert_true(all_zero(buf0, 0)); + + assert_false(all_zero(buf1, sizeof(buf1))); +} + +int main(int argc, char **argv) { + const struct CMUnitTest buffer_tests[] = { + cmocka_unit_test(test_all_zero), + }; + + (void)argc; + (void)argv; + + return cmocka_run_group_tests(buffer_tests, NULL, NULL); +} |