Jakub Czapiga | c4ca8c3 | 2021-02-16 11:13:32 +0100 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0-only */ |
| 2 | |
| 3 | /* Include memcmp() source code and alter its name to compare results with libc memcmp() */ |
| 4 | #define memcmp cb_memcmp |
| 5 | |
| 6 | #include "../lib/memcmp.c" |
| 7 | |
| 8 | #undef cb_memcmp |
| 9 | |
| 10 | #include <tests/test.h> |
| 11 | #include <string.h> |
| 12 | |
| 13 | const char test_data1[] = "TEST_DATA @4321 !@#$%^&*\\/"; |
| 14 | const size_t test_data1_sz = sizeof(test_data1); |
| 15 | |
| 16 | const char test_data2[] = "TEST_DATA @8765 !@#$%^&*\\/"; |
| 17 | const char test_data2_sz = sizeof(test_data2); |
| 18 | |
| 19 | static void test_data_correctness(void **state) |
| 20 | { |
| 21 | assert_int_equal(sizeof(test_data1), test_data1_sz); |
| 22 | assert_int_equal(sizeof(test_data2), test_data2_sz); |
| 23 | assert_int_equal(test_data1_sz, test_data2_sz); |
| 24 | } |
| 25 | |
| 26 | static void test_memcmp_equal(void **state) |
| 27 | { |
| 28 | const int res_cb = cb_memcmp(test_data1, test_data1, test_data1_sz); |
| 29 | const int res_std = memcmp(test_data1, test_data1, test_data1_sz); |
| 30 | |
| 31 | assert_int_equal(0, res_cb); |
| 32 | assert_int_equal(res_cb, res_std); |
| 33 | } |
| 34 | |
| 35 | static void test_memcmp_first_not_matching_lower(void **state) |
| 36 | { |
| 37 | const int res_cb = cb_memcmp(test_data1, test_data2, test_data1_sz); |
| 38 | const int res_std = memcmp(test_data1, test_data2, test_data1_sz); |
| 39 | |
| 40 | assert_true(res_cb < 0); |
| 41 | assert_int_equal(res_cb, res_std); |
| 42 | } |
| 43 | |
| 44 | static void test_memcmp_first_not_matching_higher(void **state) |
| 45 | { |
| 46 | const int res_cb = cb_memcmp(test_data2, test_data1, test_data1_sz); |
| 47 | const int res_std = memcmp(test_data2, test_data1, test_data1_sz); |
| 48 | |
| 49 | assert_true(res_cb > 0); |
| 50 | assert_int_equal(res_cb, res_std); |
| 51 | } |
| 52 | |
| 53 | static void test_memcmp_zero_size(void **state) |
| 54 | { |
| 55 | const int res_cb = cb_memcmp(test_data1, test_data2, 0); |
| 56 | const int res_std = memcmp(test_data1, test_data2, 0); |
| 57 | |
| 58 | assert_int_equal(0, res_cb); |
| 59 | assert_int_equal(res_cb, res_std); |
| 60 | } |
| 61 | |
| 62 | int main(void) |
| 63 | { |
| 64 | const struct CMUnitTest tests[] = { |
| 65 | cmocka_unit_test(test_data_correctness), |
| 66 | cmocka_unit_test(test_memcmp_equal), |
| 67 | cmocka_unit_test(test_memcmp_first_not_matching_lower), |
| 68 | cmocka_unit_test(test_memcmp_first_not_matching_higher), |
| 69 | cmocka_unit_test(test_memcmp_zero_size), |
| 70 | }; |
| 71 | |
Jakub Czapiga | 7c6081e | 2021-08-25 16:27:35 +0200 | [diff] [blame] | 72 | return cb_run_group_tests(tests, NULL, NULL); |
Jakub Czapiga | c4ca8c3 | 2021-02-16 11:13:32 +0100 | [diff] [blame] | 73 | } |