Skip to content

Commit 1bfa302

Browse files
committed
cmocka: add mod_alloc() APIs to cmocka tests.
Add support for cmocka usage of mod_alloc() APIs directly and indirectly via fast_get() APIs. This change means adding module API support into the fast-get-test since the module will track the resource usage. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent 2ef1514 commit 1bfa302

File tree

7 files changed

+132
-40
lines changed

7 files changed

+132
-40
lines changed

test/cmocka/src/audio/mixer/mixer_test.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ static int test_setup(void **state)
8080
module_adapter_test_setup(test_data);
8181

8282
mod_data = &test_data->mod->priv;
83-
md = test_malloc(sizeof(*md));
83+
md = mod_alloc(test_data->mod, sizeof(*md));
8484
mod_data->private = md;
8585

8686
md->mix_func = mixer_get_processing_function(test_data->mod->dev, test_data->sinks[0]);
@@ -95,7 +95,7 @@ static int test_teardown(void **state)
9595
struct processing_module_test_data *test_data = *state;
9696
struct mixer_data *md = module_get_private_data(test_data->mod);
9797

98-
test_free(md);
98+
mod_free(test_data->mod, md);
9999
module_adapter_test_free(test_data);
100100
test_free(test_data);
101101

test/cmocka/src/audio/module_adapter_test.c

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,14 @@ int module_adapter_test_setup(struct processing_module_test_data *test_data)
2929
dev->frames = parameters->frames;
3030
mod->dev = dev;
3131
dev->mod = mod;
32+
mod_resource_init(mod);
3233

33-
test_data->sinks = test_calloc(test_data->num_sinks, sizeof(struct comp_buffer *));
34-
test_data->sources = test_calloc(test_data->num_sources, sizeof(struct comp_buffer *));
34+
test_data->sinks = mod_alloc(mod, test_data->num_sinks * sizeof(struct comp_buffer *));
35+
test_data->sources = mod_alloc(mod, test_data->num_sources * sizeof(struct comp_buffer *));
3536

36-
test_data->input_buffers = test_calloc(test_data->num_sources,
37+
test_data->input_buffers = mod_alloc(mod, test_data->num_sources *
3738
sizeof(struct input_stream_buffer *));
38-
test_data->output_buffers = test_calloc(test_data->num_sinks,
39+
test_data->output_buffers = mod_alloc(mod, test_data->num_sinks *
3940
sizeof(struct output_stream_buffer *));
4041

4142
list_init(&dev->bsource_list);
@@ -48,7 +49,7 @@ int module_adapter_test_setup(struct processing_module_test_data *test_data)
4849
for (i = 0; i < test_data->num_sinks; i++) {
4950
test_data->sinks[i] = create_test_sink(dev, 0, parameters->sink_format,
5051
parameters->channels, size);
51-
test_data->output_buffers[i] = test_malloc(sizeof(struct output_stream_buffer));
52+
test_data->output_buffers[i] = mod_alloc(mod, sizeof(struct output_stream_buffer));
5253
test_data->output_buffers[i]->data = &test_data->sinks[i]->stream;
5354
}
5455

@@ -58,7 +59,7 @@ int module_adapter_test_setup(struct processing_module_test_data *test_data)
5859
for (i = 0; i < test_data->num_sources; i++) {
5960
test_data->sources[i] = create_test_source(dev, 0, parameters->source_format,
6061
parameters->channels, size);
61-
test_data->input_buffers[i] = test_malloc(sizeof(struct input_stream_buffer));
62+
test_data->input_buffers[i] = mod_alloc(mod, sizeof(struct input_stream_buffer));
6263
test_data->input_buffers[i]->data = &test_data->sources[i]->stream;
6364
}
6465

@@ -69,22 +70,24 @@ int module_adapter_test_setup(struct processing_module_test_data *test_data)
6970

7071
void module_adapter_test_free(struct processing_module_test_data *test_data)
7172
{
73+
struct processing_module *mod = test_data->mod;
7274
int i;
7375

7476
for (i = 0; i < test_data->num_sinks; i++) {
7577
free_test_sink(test_data->sinks[i]);
76-
test_free(test_data->output_buffers[i]);
78+
mod_free(mod, test_data->output_buffers[i]);
7779
}
7880

7981
for (i = 0; i < test_data->num_sources; i++) {
8082
free_test_source(test_data->sources[i]);
81-
test_free(test_data->input_buffers[i]);
83+
mod_free(mod, test_data->input_buffers[i]);
8284
}
8385

84-
test_free(test_data->input_buffers);
85-
test_free(test_data->output_buffers);
86-
test_free(test_data->sinks);
87-
test_free(test_data->sources);
86+
mod_free(mod, test_data->input_buffers);
87+
mod_free(mod, test_data->output_buffers);
88+
mod_free(mod, test_data->sinks);
89+
mod_free(mod, test_data->sources);
90+
mod_free_all(mod);
8891
test_free(test_data->mod->dev);
8992
test_free(test_data->mod);
9093
}

test/cmocka/src/audio/mux/demux_copy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ static int teardown_test_case(void **state)
188188
struct test_data *td = *((struct test_data **)state);
189189
int i;
190190

191-
rfree(td->mod->input_buffers);
192-
rfree(td->mod->output_buffers);
191+
mod_free(td->mod, td->mod->input_buffers);
192+
mod_free(td->mod, td->mod->output_buffers);
193193

194194
free_test_source(td->source);
195195

test/cmocka/src/audio/mux/mux_copy.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,8 +211,8 @@ static int teardown_test_case(void **state)
211211
struct test_data *td = *((struct test_data **)state);
212212
int i;
213213

214-
rfree(td->mod->input_buffers);
215-
rfree(td->mod->output_buffers);
214+
mod_free(td->mod, td->mod->input_buffers);
215+
mod_free(td->mod, td->mod->output_buffers);
216216

217217
for (i = 0; i < MUX_MAX_STREAMS; ++i)
218218
free_test_source(td->sources[i]);

test/cmocka/src/audio/volume/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ add_library(audio_for_volume STATIC
4242
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c
4343
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c
4444
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c
45+
${PROJECT_SOURCE_DIR}/src/audio/data_blob.c
4546
${PROJECT_SOURCE_DIR}/src/audio/component.c
4647
${PROJECT_SOURCE_DIR}/src/math/numbers.c
4748
)

test/cmocka/src/lib/fast-get/CMakeLists.txt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,39 @@
22

33
cmocka_test(fast-get-tests
44
fast-get-tests.c
5+
${PROJECT_SOURCE_DIR}/src/audio/volume/volume.c
6+
${PROJECT_SOURCE_DIR}/src/audio/volume/volume_ipc3.c
7+
${PROJECT_SOURCE_DIR}/src/audio/volume/volume_generic.c
8+
${PROJECT_SOURCE_DIR}/test/cmocka/src/audio/module_adapter_test.c
59
${PROJECT_SOURCE_DIR}/zephyr/lib/fast-get.c
610
${PROJECT_SOURCE_DIR}/src/lib/alloc.c
711
${PROJECT_SOURCE_DIR}/src/platform/library/lib/memory.c
12+
${PROJECT_SOURCE_DIR}/src/audio/component.c
13+
${PROJECT_SOURCE_DIR}/src/audio/buffers/comp_buffer.c
14+
${PROJECT_SOURCE_DIR}/src/audio/buffers/audio_buffer.c
15+
${PROJECT_SOURCE_DIR}/src/audio/source_api_helper.c
16+
${PROJECT_SOURCE_DIR}/src/audio/sink_api_helper.c
17+
${PROJECT_SOURCE_DIR}/src/audio/sink_source_utils.c
18+
${PROJECT_SOURCE_DIR}/src/audio/audio_stream.c
19+
${PROJECT_SOURCE_DIR}/src/module/audio/source_api.c
20+
${PROJECT_SOURCE_DIR}/src/module/audio/sink_api.c
21+
${PROJECT_SOURCE_DIR}/src/math/numbers.c
22+
${PROJECT_SOURCE_DIR}/src/ipc/ipc3/helper.c
23+
${PROJECT_SOURCE_DIR}/src/ipc/ipc-helper.c
24+
${PROJECT_SOURCE_DIR}/src/ipc/ipc-common.c
25+
${PROJECT_SOURCE_DIR}/test/cmocka/src/notifier_mocks.c
26+
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter.c
27+
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module_adapter_ipc3.c
28+
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/generic.c
29+
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/memory-common.c
30+
${PROJECT_SOURCE_DIR}/src/audio/module_adapter/module/memory-heap.c
31+
${PROJECT_SOURCE_DIR}/src/audio/data_blob.c
32+
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-graph.c
33+
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-params.c
34+
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-schedule.c
35+
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-stream.c
36+
${PROJECT_SOURCE_DIR}/src/audio/pipeline/pipeline-xrun.c
837
)
938

1039
target_link_libraries(fast-get-tests PRIVATE "-Wl,--wrap=rzalloc,--wrap=rmalloc,--wrap=rfree")
40+
target_include_directories(fast-get-tests PRIVATE ${PROJECT_SOURCE_DIR}/src/audio)

test/cmocka/src/lib/fast-get/fast-get-tests.c

Lines changed: 80 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,19 @@
44
//
55
// Author: Jyri Sarha <jyri.sarha@linux.intel.com>
66

7+
#include "../../util.h"
8+
#include "../../audio/module_adapter.h"
9+
10+
#include <sof/audio/module_adapter/module/generic.h>
11+
#include <sof/audio/component_ext.h>
12+
#include <sof/audio/format.h>
713
#include <sof/lib/fast-get.h>
814
#include <rtos/sof.h>
915
#include <rtos/alloc.h>
1016
#include <sof/lib/mm_heap.h>
1117
#include <sof/lib/memory.h>
1218
#include <sof/common.h>
19+
#include <volume/volume.h>
1320

1421
#include <stdarg.h>
1522
#include <stddef.h>
@@ -66,65 +73,116 @@ static const int testdata[33][100] = {
6673
{ 33 },
6774
};
6875

76+
struct test_data {
77+
struct comp_dev *dev;
78+
struct processing_module *mod;
79+
struct comp_data *cd;
80+
struct processing_module_test_data *vol_state;
81+
};
82+
83+
struct test_data _td;
84+
85+
static int setup(void **state)
86+
{
87+
struct test_data *td = &_td;
88+
struct module_data *md;
89+
struct vol_data *cd;
90+
91+
/* allocate new state */
92+
td->vol_state = test_malloc(sizeof(struct processing_module_test_data));
93+
td->vol_state->num_sources = 1;
94+
td->vol_state->num_sinks = 1;
95+
module_adapter_test_setup(td->vol_state);
96+
97+
/* allocate and set new data */
98+
cd = test_malloc(sizeof(*cd));
99+
md = &td->vol_state->mod->priv;
100+
md->private = cd;
101+
cd->is_passthrough = false;
102+
td->dev = td->vol_state->mod->dev;
103+
td->mod = td->vol_state->mod;
104+
105+
/* malloc memory to store current volume 4 times to ensure the address
106+
* is 8-byte aligned for multi-way xtensa intrinsic operations.
107+
*/
108+
const size_t vol_size = sizeof(int32_t) * SOF_IPC_MAX_CHANNELS * 4;
109+
110+
cd->vol = test_malloc(vol_size);
111+
cd->ramp_type = SOF_VOLUME_LINEAR;
112+
*state = td;
113+
114+
return 0;
115+
}
116+
117+
static int teardown(void **state)
118+
{
119+
struct test_data *td = &_td;
120+
struct vol_data *cd = module_get_private_data(td->vol_state->mod);
121+
122+
test_free(cd->vol);
123+
test_free(cd);
124+
module_adapter_test_free(td->vol_state);
125+
test_free(td->vol_state);
126+
127+
return 0;
128+
}
129+
69130
static void test_simple_fast_get_put(void **state)
70131
{
132+
struct test_data *td = *((struct test_data **)state);
71133
const void *ret;
72134

73-
(void)state; /* unused */
74-
75-
ret = fast_get(testdata[0], sizeof(testdata[0]));
135+
ret = fast_get(td->mod, testdata[0], sizeof(testdata[0]));
76136

77137
assert(ret);
78138
assert(!memcmp(ret, testdata[0], sizeof(testdata[0])));
79139

80-
fast_put(ret);
140+
fast_put(td->mod, ret);
81141
}
82142

83143
static void test_fast_get_size_missmatch_test(void **state)
84144
{
145+
struct test_data *td = *((struct test_data **)state);
85146
const void *ret[2];
86147

87-
(void)state; /* unused */
88-
89-
ret[0] = fast_get(testdata[0], sizeof(testdata[0]));
148+
ret[0] = fast_get(td->mod, testdata[0], sizeof(testdata[0]));
90149

91150
assert(ret[0]);
92151
assert(!memcmp(ret[0], testdata[0], sizeof(testdata[0])));
93152

94-
ret[1] = fast_get(testdata[0], sizeof(testdata[0]) + 1);
153+
/* this test is designed to test size mismatch handling */
154+
ret[1] = fast_get(td->mod, testdata[0], sizeof(testdata[0]) + 1);
95155
assert(!ret[1]);
96-
97-
fast_put(ret);
156+
fast_put(td->mod, ret);
98157
}
99158

100159
static void test_over_32_fast_gets_and_puts(void **state)
101160
{
161+
struct test_data *td = *((struct test_data **)state);
102162
const void *copy[ARRAY_SIZE(testdata)];
103163
int i;
104164

105-
(void)state; /* unused */
106-
107165
for (i = 0; i < ARRAY_SIZE(copy); i++)
108-
copy[i] = fast_get(testdata[i], sizeof(testdata[0]));
166+
copy[i] = fast_get(td->mod, testdata[i], sizeof(testdata[i]));
109167

110168
for (i = 0; i < ARRAY_SIZE(copy); i++)
111-
assert(!memcmp(copy[i], testdata[i], sizeof(testdata[0])));
169+
assert(!memcmp(copy[i], testdata[i], sizeof(testdata[i])));
112170

113171
for (i = 0; i < ARRAY_SIZE(copy); i++)
114-
fast_put(copy[i]);
172+
fast_put(td->mod, copy[i]);
115173
}
116174

117175
static void test_fast_get_refcounting(void **state)
118176
{
177+
struct test_data *td = *((struct test_data **)state);
119178
const void *copy[2][ARRAY_SIZE(testdata)];
120179
int i;
121-
(void)state; /* unused */
122180

123181
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
124-
copy[0][i] = fast_get(testdata[i], sizeof(testdata[0]));
182+
copy[0][i] = fast_get(td->mod, testdata[i], sizeof(testdata[0]));
125183

126184
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
127-
copy[1][i] = fast_get(testdata[i], sizeof(testdata[0]));
185+
copy[1][i] = fast_get(td->mod, testdata[i], sizeof(testdata[0]));
128186

129187
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
130188
assert(copy[0][i] == copy[1][i]);
@@ -133,18 +191,18 @@ static void test_fast_get_refcounting(void **state)
133191
assert(!memcmp(copy[0][i], testdata[i], sizeof(testdata[0])));
134192

135193
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
136-
fast_put(copy[0][i]);
194+
fast_put(td->mod, copy[0][i]);
137195

138196
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
139197
assert(!memcmp(copy[1][i], testdata[i], sizeof(testdata[0])));
140198

141199
for (i = 0; i < ARRAY_SIZE(copy[0]); i++)
142-
fast_put(copy[1][i]);
200+
fast_put(td->mod, copy[1][i]);
143201
}
144202

145203
int main(void)
146204
{
147-
const struct CMUnitTest tests[] = {
205+
struct CMUnitTest tests[] = {
148206
cmocka_unit_test(test_simple_fast_get_put),
149207
cmocka_unit_test(test_fast_get_size_missmatch_test),
150208
cmocka_unit_test(test_over_32_fast_gets_and_puts),
@@ -153,7 +211,7 @@ int main(void)
153211

154212
cmocka_set_message_output(CM_OUTPUT_TAP);
155213

156-
return cmocka_run_group_tests(tests, NULL, NULL);
214+
return cmocka_run_group_tests(tests, setup, teardown);
157215
}
158216

159217
void *__wrap_rzalloc(uint32_t flags, size_t bytes);

0 commit comments

Comments
 (0)