Skip to content

Commit c3ede56

Browse files
committed
tests: audio: Add unit test for tensorflow
Add ZTest unit test validating module creation, configuration, prepare, copy, and teardown lifecycle for the tensorflow component. Signed-off-by: Liam Girdwood <liam.r.girdwood@linux.intel.com>
1 parent 432a438 commit c3ede56

1 file changed

Lines changed: 138 additions & 0 deletions

File tree

Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
// SPDX-License-Identifier: BSD-3-Clause
2+
/*
3+
* Copyright(c) 2026 Intel Corporation.
4+
*/
5+
6+
#include <zephyr/kernel.h>
7+
#include <zephyr/ztest.h>
8+
#include <string.h>
9+
#include <rtos/sof.h>
10+
#include <sof/list.h>
11+
#include <sof/audio/component.h>
12+
#include <sof/audio/component_ext.h>
13+
#include <sof/ipc/topology.h>
14+
#include <ipc4/base-config.h>
15+
16+
#include <rtos/alloc.h>
17+
#include "test_audio_helper.h"
18+
19+
SOF_DEFINE_REG_UUID(tflmcly);
20+
extern const struct sof_uuid tflmcly_uuid;
21+
22+
extern void sys_comp_module_tflmcly_interface_init(void);
23+
24+
static void *setup(void)
25+
{
26+
struct sof *sof = sof_get();
27+
28+
sys_comp_init(sof);
29+
30+
if (!sof->ipc) {
31+
sof->ipc = rzalloc(SOF_MEM_FLAG_COHERENT, sizeof(*sof->ipc));
32+
sof->ipc->comp_data = rzalloc(SOF_MEM_FLAG_COHERENT, 4096);
33+
k_spinlock_init(&sof->ipc->lock);
34+
list_init(&sof->ipc->msg_list);
35+
list_init(&sof->ipc->comp_list);
36+
}
37+
38+
#if defined(CONFIG_COMP_TENSORFLOW)
39+
sys_comp_module_tflmcly_interface_init();
40+
#endif
41+
return NULL;
42+
}
43+
44+
extern const struct sof_uuid tflmcly_uuid;
45+
46+
struct custom_ipc4_config_tensorflow {
47+
struct ipc4_base_module_cfg base;
48+
} __attribute__((packed, aligned(8)));
49+
50+
static struct comp_dev *test_tensorflow_create(void)
51+
{
52+
#ifndef CONFIG_COMP_TENSORFLOW
53+
ztest_test_skip();
54+
#endif
55+
struct comp_dev *comp;
56+
struct comp_ipc_config ipc_config;
57+
struct ipc_config_process spec;
58+
struct custom_ipc4_config_tensorflow init_data;
59+
60+
memset(&init_data, 0, sizeof(init_data));
61+
init_data.base.audio_fmt.channels_count = 1;
62+
init_data.base.audio_fmt.sampling_frequency = 16000;
63+
init_data.base.audio_fmt.depth = 16;
64+
init_data.base.audio_fmt.valid_bit_depth = 16;
65+
init_data.base.audio_fmt.interleaving_style = IPC4_CHANNELS_INTERLEAVED;
66+
init_data.base.ibs = 1024;
67+
init_data.base.obs = 1024;
68+
69+
memset(&ipc_config, 0, sizeof(ipc_config));
70+
ipc_config.id = 1;
71+
ipc_config.pipeline_id = 1;
72+
ipc_config.core = 0;
73+
74+
memset(&spec, 0, sizeof(spec));
75+
spec.size = sizeof(init_data);
76+
spec.data = (unsigned char *)&init_data;
77+
78+
struct list_item *clist;
79+
const struct comp_driver *drv = NULL;
80+
81+
list_for_item(clist, &comp_drivers_get()->list) {
82+
struct comp_driver_info *info = container_of(clist, struct comp_driver_info, list);
83+
if (!info->drv->uid) continue;
84+
if (!memcmp(info->drv->uid, &tflmcly_uuid, sizeof(struct sof_uuid))) {
85+
drv = info->drv;
86+
break;
87+
}
88+
}
89+
zassert_not_null(drv, "Driver for tensorflow missing");
90+
91+
comp = drv->ops.create(drv, &ipc_config, &spec);
92+
zassert_not_null(comp, "Component allocation failed");
93+
94+
return comp;
95+
}
96+
97+
/* Test tensorflow initialization */
98+
ZTEST(audio_tensorflow, test_tensorflow_init)
99+
{
100+
struct comp_dev *comp = test_tensorflow_create();
101+
zassert_equal(comp->state, COMP_STATE_READY, "Component is not in READY state");
102+
103+
comp->drv->ops.free(comp);
104+
}
105+
ZTEST(audio_tensorflow, test_tensorflow_config)
106+
{
107+
struct comp_dev *comp = test_tensorflow_create();
108+
109+
int ret = 0;
110+
if (comp->drv->ops.set_large_config) {
111+
uint32_t val = 0;
112+
ret = comp->drv->ops.set_large_config(comp, 0, true, true,
113+
sizeof(val), (uint8_t *)&val);
114+
}
115+
116+
comp->drv->ops.free(comp);
117+
}
118+
119+
ZTEST(audio_tensorflow, test_tensorflow_process)
120+
{
121+
struct comp_dev *comp = test_tensorflow_create();
122+
123+
struct sof_ipc_stream_params params = {0};
124+
params.buffer_fmt = SOF_IPC_BUFFER_INTERLEAVED;
125+
params.channels = 2;
126+
params.rate = 48000;
127+
params.sample_container_bytes = 4;
128+
params.sample_valid_bytes = 4;
129+
130+
test_audio_helper_setup_buffers(comp, 4096, &params);
131+
test_audio_helper_process(comp);
132+
test_audio_helper_free_buffers(comp);
133+
134+
comp->drv->ops.free(comp);
135+
}
136+
137+
138+
ZTEST_SUITE(audio_tensorflow, NULL, setup, NULL, NULL, NULL);

0 commit comments

Comments
 (0)