1 /* SPDX-License-Identifier: GPL-2.0-only */
4 #include <tests/test.h>
5 #include <commonlib/helpers.h>
9 #define MEMSET_BUFFER_SZ (4 * KiB)
11 struct memset_test_state
{
16 static int setup_test(void **state
)
18 struct memset_test_state
*s
= malloc(sizeof(struct memset_test_state
));
19 u8
*buf
= malloc(MEMSET_BUFFER_SZ
);
20 u8
*helper_buf
= malloc(MEMSET_BUFFER_SZ
);
21 if (!s
|| !buf
|| !helper_buf
)
25 s
->helper_buffer
= helper_buf
;
37 static int teardown_test(void **state
)
39 struct memset_test_state
*s
= *state
;
43 free(s
->helper_buffer
);
51 static void test_memset_full_range(void **state
)
53 struct memset_test_state
*s
= *state
;
55 for (int i
= 0; i
< MEMSET_BUFFER_SZ
; ++i
)
56 s
->helper_buffer
[i
] = 0x42;
58 memset(s
->base_buffer
, 0x42, MEMSET_BUFFER_SZ
);
59 assert_memory_equal(s
->base_buffer
, s
->helper_buffer
, MEMSET_BUFFER_SZ
);
62 static void test_memset_subrange(void **state
)
64 struct memset_test_state
*s
= *state
;
67 for (; i
< MEMSET_BUFFER_SZ
/ 2; ++i
)
68 s
->helper_buffer
[i
] = 0xEF;
69 for (; i
< MEMSET_BUFFER_SZ
; ++i
)
70 s
->helper_buffer
[i
] = 0xCD;
72 /* Expect correct buffer subranges to be filled */
73 memset(s
->base_buffer
, 0xEF, MEMSET_BUFFER_SZ
/ 2);
74 memset(s
->base_buffer
+ MEMSET_BUFFER_SZ
/ 2, 0xCD, MEMSET_BUFFER_SZ
/ 2);
75 assert_memory_equal(s
->base_buffer
, s
->helper_buffer
, MEMSET_BUFFER_SZ
);
78 static void test_memset_zero_size(void **state
)
80 struct memset_test_state
*s
= *state
;
82 for (int i
= 0; i
< MEMSET_BUFFER_SZ
; ++i
) {
83 s
->base_buffer
[i
] = 0xFF;
84 s
->helper_buffer
[i
] = 0xFF;
87 /* Expect no change in buffer after calling memset with zero size */
88 memset(s
->base_buffer
, 0xAA, (0));
89 assert_memory_equal(s
->base_buffer
, s
->helper_buffer
, MEMSET_BUFFER_SZ
);
92 static void test_memset_one_byte(void **state
)
94 struct memset_test_state
*s
= *state
;
96 for (int i
= 0; i
< MEMSET_BUFFER_SZ
; ++i
) {
97 s
->base_buffer
[i
] = 0x66;
98 s
->helper_buffer
[i
] = 0x66;
101 s
->helper_buffer
[MEMSET_BUFFER_SZ
/ 4] = 0xCC;
102 memset(&s
->base_buffer
[MEMSET_BUFFER_SZ
/ 4], 0xCC, 1);
103 assert_memory_equal(s
->base_buffer
, s
->helper_buffer
, MEMSET_BUFFER_SZ
);
108 const struct CMUnitTest tests
[] = {
109 cmocka_unit_test_setup_teardown(test_memset_full_range
, setup_test
,
111 cmocka_unit_test_setup_teardown(test_memset_subrange
, setup_test
,
113 cmocka_unit_test_setup_teardown(test_memset_zero_size
, setup_test
,
115 cmocka_unit_test_setup_teardown(test_memset_one_byte
, setup_test
,
119 return cb_run_group_tests(tests
, NULL
, NULL
);