sb/intel/i82801gx/bootblock.c: Clean up includes
[coreboot.git] / tests / lib / memset-test.c
blobb0be91f4772bb587f2d3deed7c9b968b67a8f32f
1 /* SPDX-License-Identifier: GPL-2.0-only */
3 #include <stdlib.h>
4 #include <tests/test.h>
5 #include <commonlib/helpers.h>
6 #include <types.h>
7 #include <string.h>
9 #define MEMSET_BUFFER_SZ (4 * KiB)
11 struct memset_test_state {
12 u8 *base_buffer;
13 u8 *helper_buffer;
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)
22 goto error;
24 s->base_buffer = buf;
25 s->helper_buffer = helper_buf;
26 *state = s;
28 return 0;
30 error:
31 free(buf);
32 free(helper_buf);
33 free(s);
34 return -1;
37 static int teardown_test(void **state)
39 struct memset_test_state *s = *state;
41 if (s) {
42 free(s->base_buffer);
43 free(s->helper_buffer);
46 free(s);
48 return 0;
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;
65 int i = 0;
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);
106 int main(void)
108 const struct CMUnitTest tests[] = {
109 cmocka_unit_test_setup_teardown(test_memset_full_range, setup_test,
110 teardown_test),
111 cmocka_unit_test_setup_teardown(test_memset_subrange, setup_test,
112 teardown_test),
113 cmocka_unit_test_setup_teardown(test_memset_zero_size, setup_test,
114 teardown_test),
115 cmocka_unit_test_setup_teardown(test_memset_one_byte, setup_test,
116 teardown_test),
119 return cb_run_group_tests(tests, NULL, NULL);