summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJakub Czapiga <jacz@semihalf.com>2021-02-16 11:13:32 +0100
committerPatrick Georgi <pgeorgi@google.com>2021-02-18 19:12:46 +0000
commitc4ca8c355678dc329ec713a3079e6e56956c9bd9 (patch)
tree6af02e870e9de2316973dd10305128ab70f9a502
parent6410a0002f95db5e4fcf277cfcc3e9cbab6c3bf3 (diff)
tests: Add lib/memcmp-test test case
Signed-off-by: Jakub Czapiga <jacz@semihalf.com> Change-Id: Ib63123a36179127af4e3720ed01ca2611daa607e Reviewed-on: https://review.coreboot.org/c/coreboot/+/50785 Tested-by: build bot (Jenkins) <no-reply@coreboot.org> Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
-rw-r--r--tests/lib/Makefile.inc4
-rw-r--r--tests/lib/memcmp-test.c73
2 files changed, 77 insertions, 0 deletions
diff --git a/tests/lib/Makefile.inc b/tests/lib/Makefile.inc
index 23d8d34d45..9f4a6cad5a 100644
--- a/tests/lib/Makefile.inc
+++ b/tests/lib/Makefile.inc
@@ -15,6 +15,7 @@ tests-y += imd_cbmem-ramstage-test
tests-y += region_file-test
tests-y += stack-test
tests-y += memset-test
+tests-y += memcmp-test
string-test-srcs += tests/lib/string-test.c
string-test-srcs += src/lib/string.c
@@ -80,3 +81,6 @@ stack-test-srcs += tests/stubs/console.c
memset-test-srcs += tests/lib/memset-test.c
memset-test-srcs += src/lib/memset.c
+
+memcmp-test-srcs += tests/lib/memcmp-test.c
+
diff --git a/tests/lib/memcmp-test.c b/tests/lib/memcmp-test.c
new file mode 100644
index 0000000000..f45b7f43ff
--- /dev/null
+++ b/tests/lib/memcmp-test.c
@@ -0,0 +1,73 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+
+/* Include memcmp() source code and alter its name to compare results with libc memcmp() */
+#define memcmp cb_memcmp
+
+#include "../lib/memcmp.c"
+
+#undef cb_memcmp
+
+#include <tests/test.h>
+#include <string.h>
+
+const char test_data1[] = "TEST_DATA @4321 !@#$%^&*\\/";
+const size_t test_data1_sz = sizeof(test_data1);
+
+const char test_data2[] = "TEST_DATA @8765 !@#$%^&*\\/";
+const char test_data2_sz = sizeof(test_data2);
+
+static void test_data_correctness(void **state)
+{
+ assert_int_equal(sizeof(test_data1), test_data1_sz);
+ assert_int_equal(sizeof(test_data2), test_data2_sz);
+ assert_int_equal(test_data1_sz, test_data2_sz);
+}
+
+static void test_memcmp_equal(void **state)
+{
+ const int res_cb = cb_memcmp(test_data1, test_data1, test_data1_sz);
+ const int res_std = memcmp(test_data1, test_data1, test_data1_sz);
+
+ assert_int_equal(0, res_cb);
+ assert_int_equal(res_cb, res_std);
+}
+
+static void test_memcmp_first_not_matching_lower(void **state)
+{
+ const int res_cb = cb_memcmp(test_data1, test_data2, test_data1_sz);
+ const int res_std = memcmp(test_data1, test_data2, test_data1_sz);
+
+ assert_true(res_cb < 0);
+ assert_int_equal(res_cb, res_std);
+}
+
+static void test_memcmp_first_not_matching_higher(void **state)
+{
+ const int res_cb = cb_memcmp(test_data2, test_data1, test_data1_sz);
+ const int res_std = memcmp(test_data2, test_data1, test_data1_sz);
+
+ assert_true(res_cb > 0);
+ assert_int_equal(res_cb, res_std);
+}
+
+static void test_memcmp_zero_size(void **state)
+{
+ const int res_cb = cb_memcmp(test_data1, test_data2, 0);
+ const int res_std = memcmp(test_data1, test_data2, 0);
+
+ assert_int_equal(0, res_cb);
+ assert_int_equal(res_cb, res_std);
+}
+
+int main(void)
+{
+ const struct CMUnitTest tests[] = {
+ cmocka_unit_test(test_data_correctness),
+ cmocka_unit_test(test_memcmp_equal),
+ cmocka_unit_test(test_memcmp_first_not_matching_lower),
+ cmocka_unit_test(test_memcmp_first_not_matching_higher),
+ cmocka_unit_test(test_memcmp_zero_size),
+ };
+
+ return cmocka_run_group_tests(tests, NULL, NULL);
+}