Skip to content

Commit ff46f79

Browse files
Jyri Sarhalyakh
authored andcommitted
test: cmocka: Add module tests for fast_get() and fast_put()
And simple module tests for fast_get and fast_put() implemented in src/lib/fast-get/fast-get.c. Signed-off-by: Jyri Sarha <jyri.sarha@linux.intel.com>
1 parent 8a595c2 commit ff46f79

File tree

3 files changed

+208
-0
lines changed

3 files changed

+208
-0
lines changed

test/cmocka/src/lib/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22

33
add_subdirectory(alloc)
44
add_subdirectory(lib)
5+
add_subdirectory(fast-get)
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# SPDX-License-Identifier: BSD-3-Clause
2+
3+
cmocka_test(fast-get-tests
4+
fast-get-tests.c
5+
${PROJECT_SOURCE_DIR}/zephyr/lib/fast-get.c
6+
${PROJECT_SOURCE_DIR}/src/lib/alloc.c
7+
${PROJECT_SOURCE_DIR}/src/platform/library/lib/memory.c
8+
${PROJECT_SOURCE_DIR}/src/spinlock.c
9+
)
10+
11+
target_link_libraries(fast-get-tests PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree")
Lines changed: 196 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,196 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
//
3+
// Copyright(c) 2024 Intel Corporation. All rights reserved.
4+
//
5+
// Author: Jyri Sarha <jyri.sarha@linux.intel.com>
6+
7+
#include <sof/lib/fast-get.h>
8+
#include <rtos/sof.h>
9+
#include <rtos/alloc.h>
10+
#include <sof/lib/mm_heap.h>
11+
#include <sof/lib/memory.h>
12+
#include <sof/common.h>
13+
14+
#include <stdarg.h>
15+
#include <stddef.h>
16+
#include <setjmp.h>
17+
#include <stdint.h>
18+
#include <stdlib.h>
19+
#include <string.h>
20+
#include <cmocka.h>
21+
#include <assert.h>
22+
23+
static const int testdata[33][100] = {
24+
{
25+
1, 2, 3, 4, 5, 6, 7, 9, 0,
26+
1, 2, 3, 4, 5, 6, 7, 9, 0,
27+
1, 2, 3, 4, 5, 6, 7, 9, 0,
28+
1, 2, 3, 4, 5, 6, 7, 9, 0,
29+
1, 2, 3, 4, 5, 6, 7, 9, 0,
30+
1, 2, 3, 4, 5, 6, 7, 9, 0,
31+
1, 2, 3, 4, 5, 6, 7, 9, 0,
32+
1, 2, 3, 4, 5, 6, 7, 9, 0,
33+
1, 2, 3, 4, 5, 6, 7, 9, 0,
34+
1, 2, 3, 4, 5, 6, 7, 9, 0,
35+
},
36+
{ 2 },
37+
{ 3 },
38+
{ 4 },
39+
{ 5 },
40+
{ 6 },
41+
{ 7 },
42+
{ 8 },
43+
{ 9 },
44+
{ 10 },
45+
{ 11 },
46+
{ 12 },
47+
{ 13 },
48+
{ 14 },
49+
{ 15 },
50+
{ 16 },
51+
{ 17 },
52+
{ 18 },
53+
{ 19 },
54+
{ 20 },
55+
{ 21 },
56+
{ 23 },
57+
{ 24 },
58+
{ 25 },
59+
{ 26 },
60+
{ 27 },
61+
{ 28 },
62+
{ 29 },
63+
{ 30 },
64+
{ 31 },
65+
{ 32 },
66+
{ 33 },
67+
};
68+
69+
static void test_simple_fast_get_put(void **state)
70+
{
71+
const void *ret;
72+
73+
(void)state; /* unused */
74+
75+
ret = fast_get(testdata[0], sizeof(testdata[0]));
76+
77+
assert(ret);
78+
assert(!memcmp(ret, testdata[0], sizeof(testdata[0])));
79+
80+
fast_put(ret);
81+
}
82+
83+
static void test_fast_get_size_missmatch_test(void **state)
84+
{
85+
const void *ret[2];
86+
87+
(void)state; /* unused */
88+
89+
ret[0] = fast_get(testdata[0], sizeof(testdata[0]));
90+
91+
assert(ret[0]);
92+
assert(!memcmp(ret[0], testdata[0], sizeof(testdata[0])));
93+
94+
ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
95+
assert(!ret[1]);
96+
97+
fast_put(ret);
98+
}
99+
100+
static void test_over_32_fast_gets_and_puts(void **state)
101+
{
102+
const void *copy[ARRAY_SIZE(testdata)];
103+
int i;
104+
105+
(void)state; /* unused */
106+
107+
for (i = 0; i < ARRAY_SIZE(copy); i++)
108+
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
109+
110+
for (i = 0; i < ARRAY_SIZE(copy); i++)
111+
assert(!memcmp(copy[i], testdata[i], sizeof(testdata[0])));
112+
113+
for (i = 0; i < ARRAY_SIZE(copy); i++)
114+
fast_put(copy[i]);
115+
}
116+
117+
static void test_fast_get_refcounting(void **state)
118+
{
119+
const void *copy[2][ARRAY_SIZE(testdata)];
120+
int i;
121+
(void)state; /* unused */
122+
123+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
124+
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
125+
126+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
127+
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
128+
129+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
130+
assert(copy[0][i] == copy[1][i]);
131+
132+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
133+
assert(!memcmp(copy[0][i], testdata[i], sizeof(testdata[0])));
134+
135+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
136+
fast_put(copy[0][i]);
137+
138+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
139+
assert(!memcmp(copy[1][i], testdata[i], sizeof(testdata[0])));
140+
141+
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
142+
fast_put(copy[1][i]);
143+
}
144+
145+
int main(void)
146+
{
147+
const struct CMUnitTest tests[] = {
148+
cmocka_unit_test(test_simple_fast_get_put),
149+
cmocka_unit_test(test_fast_get_size_missmatch_test),
150+
cmocka_unit_test(test_over_32_fast_gets_and_puts),
151+
cmocka_unit_test(test_fast_get_refcounting),
152+
};
153+
154+
cmocka_set_message_output(CM_OUTPUT_TAP);
155+
156+
return cmocka_run_group_tests(tests, NULL, NULL);
157+
}
158+
159+
void *__wrap_rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
160+
void *__wrap_rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes);
161+
void __wrap_rfree(void *ptr);
162+
163+
void *__wrap_rzalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)
164+
{
165+
void *ret;
166+
(void)zone;
167+
(void)flags;
168+
(void)caps;
169+
170+
ret = malloc(bytes);
171+
172+
assert(ret);
173+
174+
memset(ret, 0, bytes);
175+
176+
return ret;
177+
}
178+
179+
void *__wrap_rmalloc(enum mem_zone zone, uint32_t flags, uint32_t caps, size_t bytes)
180+
{
181+
void *ret;
182+
(void)zone;
183+
(void)flags;
184+
(void)caps;
185+
186+
ret = malloc(bytes);
187+
188+
assert(ret);
189+
190+
return ret;
191+
}
192+
193+
void __wrap_rfree(void *ptr)
194+
{
195+
free(ptr);
196+
}

0 commit comments

Comments
 (0)