Skip to content

Commit adae954

Browse files
authored
Merge pull request #55 from slawblauciak/alloc_ut
test: alloc tests
2 parents 709d630 + b7a9f0f commit adae954

3 files changed

Lines changed: 391 additions & 0 deletions

File tree

test/cmocka/Makefile.am

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,13 @@ LDFLAGS := $(filter-out -nostdlib,$(LDFLAGS))
3131

3232
LDADD = -lcmocka
3333

34+
# memory allocator test
35+
36+
if BUILD_XTENSA
37+
check_PROGRAMS += alloc
38+
alloc_SOURCES = src/lib/alloc/alloc.c src/lib/alloc/mock.c ../../src/lib/alloc.c ../../src/platform/apollolake/memory.c
39+
endif
40+
3441
# volume tests
3542

3643
check_PROGRAMS += volume_process

test/cmocka/src/lib/alloc/alloc.c

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,330 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the Intel Corporation nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
* Author: Slawomir Blauciak <slawomir.blauciak@linux.intel.com>
29+
*/
30+
31+
#include <stdint.h>
32+
#include <stdarg.h>
33+
#include <stddef.h>
34+
#include <setjmp.h>
35+
#include <cmocka.h>
36+
37+
#include <sof/sof.h>
38+
#include <sof/alloc.h>
39+
40+
static struct sof *sof;
41+
42+
enum test_type {
43+
TEST_BULK = 0,
44+
TEST_ZERO,
45+
TEST_IMMEDIATE_FREE
46+
};
47+
48+
struct test_case {
49+
size_t alloc_size;
50+
int alloc_zone;
51+
uint32_t alloc_caps;
52+
uint16_t alloc_num;
53+
enum test_type type;
54+
const char *name;
55+
};
56+
57+
#define TEST_CASE(bytes, zone, caps, num, type, name_base) \
58+
{(bytes), (zone), (caps), (num), (type), \
59+
(name_base "__" #zone "__" #bytes "x" #num)}
60+
61+
static struct test_case test_cases[] = {
62+
/*
63+
* rmalloc tests
64+
*/
65+
66+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 1024, TEST_IMMEDIATE_FREE,
67+
"rmalloc"),
68+
69+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_BULK, "rmalloc"),
70+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_BULK, "rmalloc"),
71+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_BULK, "rmalloc"),
72+
73+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_BULK, "rmalloc"),
74+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_BULK, "rmalloc"),
75+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_BULK, "rmalloc"),
76+
77+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_BULK, "rmalloc"),
78+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_BULK, "rmalloc"),
79+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_BULK, "rmalloc"),
80+
81+
TEST_CASE(16, RZONE_SYS, SOF_MEM_CAPS_RAM, 128, TEST_BULK,
82+
"rmalloc"),
83+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 256, TEST_BULK,
84+
"rmalloc"),
85+
86+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
87+
"rmalloc"),
88+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
89+
"rmalloc"),
90+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
91+
"rmalloc"),
92+
93+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
94+
"rmalloc"),
95+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
96+
"rmalloc"),
97+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
98+
"rmalloc"),
99+
100+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
101+
"rmalloc"),
102+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
103+
"rmalloc"),
104+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
105+
"rmalloc"),
106+
107+
TEST_CASE(16, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 128, TEST_BULK,
108+
"rmalloc"),
109+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 256, TEST_BULK,
110+
"rmalloc"),
111+
112+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
113+
TEST_BULK, "rmalloc_dma"),
114+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
115+
TEST_BULK, "rmalloc_dma"),
116+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
117+
TEST_BULK, "rmalloc_dma"),
118+
119+
/*
120+
* rzalloc tests
121+
*/
122+
123+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_ZERO, "rzalloc"),
124+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_ZERO, "rzalloc"),
125+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 2, TEST_ZERO, "rzalloc"),
126+
127+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_ZERO, "rzalloc"),
128+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_ZERO, "rzalloc"),
129+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 4, TEST_ZERO, "rzalloc"),
130+
131+
TEST_CASE(1, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_ZERO, "rzalloc"),
132+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_ZERO, "rzalloc"),
133+
TEST_CASE(256, RZONE_SYS, SOF_MEM_CAPS_RAM, 8, TEST_ZERO, "rzalloc"),
134+
135+
TEST_CASE(16, RZONE_SYS, SOF_MEM_CAPS_RAM, 128, TEST_ZERO, "rzalloc"),
136+
TEST_CASE(4, RZONE_SYS, SOF_MEM_CAPS_RAM, 256, TEST_ZERO, "rzalloc"),
137+
138+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_ZERO,
139+
"rzalloc"),
140+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_ZERO,
141+
"rzalloc"),
142+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 2, TEST_ZERO,
143+
"rzalloc"),
144+
145+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_ZERO,
146+
"rzalloc"),
147+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_ZERO,
148+
"rzalloc"),
149+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 4, TEST_ZERO,
150+
"rzalloc"),
151+
152+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_ZERO,
153+
"rzalloc"),
154+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_ZERO,
155+
"rzalloc"),
156+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 8, TEST_ZERO,
157+
"rzalloc"),
158+
159+
TEST_CASE(16, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 128, TEST_ZERO,
160+
"rzalloc"),
161+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM, 512, TEST_ZERO,
162+
"rzalloc"),
163+
164+
TEST_CASE(1, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
165+
TEST_ZERO, "rzalloc_dma"),
166+
TEST_CASE(4, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
167+
TEST_ZERO, "rzalloc_dma"),
168+
TEST_CASE(256, RZONE_RUNTIME, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
169+
TEST_ZERO, "rzalloc_dma"),
170+
171+
/*
172+
* rballoc tests
173+
*/
174+
175+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 1024,
176+
TEST_IMMEDIATE_FREE, "rballoc"),
177+
178+
TEST_CASE(1, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
179+
"rballoc"),
180+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
181+
"rballoc"),
182+
TEST_CASE(256, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 2, TEST_BULK,
183+
"rballoc"),
184+
185+
TEST_CASE(1, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
186+
"rballoc"),
187+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
188+
"rballoc"),
189+
TEST_CASE(256, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 4, TEST_BULK,
190+
"rballoc"),
191+
192+
TEST_CASE(1, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
193+
"rballoc"),
194+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
195+
"rballoc"),
196+
TEST_CASE(256, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 8, TEST_BULK,
197+
"rballoc"),
198+
199+
TEST_CASE(16, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 64, TEST_BULK,
200+
"rballoc"),
201+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM, 64, TEST_BULK,
202+
"rballoc"),
203+
204+
TEST_CASE(1, RZONE_BUFFER, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
205+
TEST_BULK, "rballoc_dma"),
206+
TEST_CASE(4, RZONE_BUFFER, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
207+
TEST_BULK, "rballoc_dma"),
208+
TEST_CASE(256, RZONE_BUFFER, SOF_MEM_CAPS_RAM | SOF_MEM_CAPS_DMA, 2,
209+
TEST_BULK, "rballoc_dma")
210+
};
211+
212+
static int setup(void **state)
213+
{
214+
sof = malloc(sizeof(struct sof));
215+
init_heap(sof);
216+
217+
return 0;
218+
}
219+
220+
static int teardown(void **state)
221+
{
222+
free(sof);
223+
224+
return 0;
225+
}
226+
227+
static void *alloc(struct test_case *tc)
228+
{
229+
void *mem;
230+
231+
if (tc->alloc_zone == RZONE_BUFFER)
232+
mem = rballoc(tc->alloc_zone, tc->alloc_caps, tc->alloc_size);
233+
else
234+
mem = rmalloc(tc->alloc_zone, tc->alloc_caps, tc->alloc_size);
235+
236+
return mem;
237+
}
238+
239+
static void test_alloc_bulk_free(struct test_case *tc)
240+
{
241+
void **all_mem = malloc(sizeof(void *) * tc->alloc_num);
242+
int i;
243+
244+
for (i = 0; i < tc->alloc_num; ++i) {
245+
void *mem = alloc(tc);
246+
247+
assert_non_null(mem);
248+
all_mem[i] = mem;
249+
}
250+
251+
for (i = 0; i < tc->alloc_num; ++i)
252+
rfree(all_mem[i]);
253+
254+
rfree(all_mem);
255+
}
256+
257+
static void test_alloc_immediate_free(struct test_case *tc)
258+
{
259+
int i;
260+
261+
for (i = 0; i < tc->alloc_num; ++i) {
262+
void *mem = alloc(tc);
263+
264+
assert_non_null(mem);
265+
rfree(mem);
266+
}
267+
}
268+
269+
static void test_alloc_zero(struct test_case *tc)
270+
{
271+
void **all_mem = malloc(sizeof(void *) * tc->alloc_num);
272+
int i;
273+
274+
for (i = 0; i < tc->alloc_num; ++i) {
275+
char *mem = rzalloc(tc->alloc_zone, tc->alloc_caps,
276+
tc->alloc_size);
277+
int j;
278+
279+
assert_non_null(mem);
280+
all_mem[i] = mem;
281+
282+
for (j = 0; j < tc->alloc_size; ++j)
283+
assert_int_equal(mem[j], 0);
284+
}
285+
286+
for (i = 0; i < tc->alloc_num; ++i)
287+
rfree(all_mem[i]);
288+
289+
rfree(all_mem);
290+
}
291+
292+
static void test_alloc(void **state)
293+
{
294+
struct test_case *tc = *((struct test_case **)state);
295+
296+
switch (tc->type) {
297+
case TEST_BULK:
298+
test_alloc_bulk_free(tc);
299+
break;
300+
301+
case TEST_ZERO:
302+
test_alloc_zero(tc);
303+
break;
304+
305+
case TEST_IMMEDIATE_FREE:
306+
test_alloc_immediate_free(tc);
307+
break;
308+
}
309+
}
310+
311+
int main(void)
312+
{
313+
struct CMUnitTest tests[ARRAY_SIZE(test_cases)];
314+
315+
int i;
316+
317+
for (i = 0; i < ARRAY_SIZE(test_cases); ++i) {
318+
struct CMUnitTest *t = &tests[i];
319+
320+
t->name = test_cases[i].name;
321+
t->test_func = test_alloc;
322+
t->initial_state = &test_cases[i];
323+
t->setup_func = NULL;
324+
t->teardown_func = NULL;
325+
}
326+
327+
cmocka_set_message_output(CM_OUTPUT_TAP);
328+
329+
return cmocka_run_group_tests(tests, setup, teardown);
330+
}

test/cmocka/src/lib/alloc/mock.c

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
/*
2+
* Copyright (c) 2018, Intel Corporation
3+
* All rights reserved.
4+
*
5+
* Redistribution and use in source and binary forms, with or without
6+
* modification, are permitted provided that the following conditions are met:
7+
* * Redistributions of source code must retain the above copyright
8+
* notice, this list of conditions and the following disclaimer.
9+
* * Redistributions in binary form must reproduce the above copyright
10+
* notice, this list of conditions and the following disclaimer in the
11+
* documentation and/or other materials provided with the distribution.
12+
* * Neither the name of the Intel Corporation nor the
13+
* names of its contributors may be used to endorse or promote products
14+
* derived from this software without specific prior written permission.
15+
*
16+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17+
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18+
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19+
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
20+
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21+
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22+
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23+
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24+
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25+
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26+
* POSSIBILITY OF SUCH DAMAGE.
27+
*
28+
* Author: Slawomir Blauciak <slawomir.blauciak@linux.intel.com>
29+
*/
30+
31+
#include <stdint.h>
32+
33+
#include <sof/alloc.h>
34+
35+
struct dma_copy;
36+
struct dma_sg_config;
37+
38+
int dma_copy_from_host(struct dma_copy *dc, struct dma_sg_config *host_sg,
39+
int32_t host_offset, void *local_ptr, int32_t size)
40+
{
41+
}
42+
43+
int dma_copy_to_host(struct dma_copy *dc, struct dma_sg_config *host_sg,
44+
int32_t host_offset, void *local_ptr, int32_t size)
45+
{
46+
}
47+
48+
void _trace_event_mbox_atomic(uint32_t e)
49+
{
50+
}
51+
52+
void trace_flush(void)
53+
{
54+
}

0 commit comments

Comments
 (0)