summaryrefslogtreecommitdiff
path: root/tests/lib/memcmp-test.c
blob: 59ca86c0cb3b37667f45b5f366ef11667d2c37b4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
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 cb_run_group_tests(tests, NULL, NULL);
}