Skip to content

Commit d3b76a0

Browse files
tests: audio: Add get_caps API tests for I2S and DMIC drivers
Add test for the `get_caps()` API in both I2S and DMIC driver test suites. Tests validate successful operation, capability value ranges, and error handling with NULL parameters. Tests handle drivers that don't implement get_caps by gracefully skipping when -ENOSYS is returned, ensuring compatibility across different driver implementations. Signed-off-by: Michal Chvatal <[email protected]>
1 parent 474ffb3 commit d3b76a0

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

tests/drivers/audio/dmic_api/src/main.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
*/
1111

1212
#include <zephyr/kernel.h>
13+
#include <zephyr/audio/audio_caps.h>
1314
#include <zephyr/audio/dmic.h>
1415
#include <zephyr/ztest.h>
1516

@@ -291,4 +292,64 @@ ZTEST(dmic, test_bad_pair)
291292
"non adjacent channels in map");
292293
}
293294

295+
/**
296+
* @brief Test the dmic_get_caps API function
297+
*
298+
* This test validates the DMIC get_caps API functionality by testing both
299+
* successful operation and error handling scenarios.
300+
*/
301+
ZTEST(dmic, test_get_caps)
302+
{
303+
int ret;
304+
struct audio_caps caps;
305+
306+
zassert_true(device_is_ready(dmic_dev), "DMIC device is not ready");
307+
308+
/**
309+
* Test Case 1: Normal operation - Valid parameters
310+
* Expected: Function should return 0 (success) or -ENOSYS (not implemented)
311+
*/
312+
ret = dmic_get_caps(dmic_dev, &caps);
313+
314+
/* Handle case where driver doesn't implement get_caps */
315+
if (ret == -ENOSYS) {
316+
TC_PRINT("DMIC get_caps not implemented by driver\n");
317+
ztest_test_skip();
318+
return;
319+
}
320+
321+
/* Verify successful execution */
322+
zassert_equal(ret, 0, "dmic_get_caps should return 0, got %d", ret);
323+
324+
/**
325+
* Test Case 2: Capability value validation
326+
* Verify that returned capability values are within reasonable ranges
327+
*/
328+
zassert_true(caps.min_total_channels >= 1, "min_total_channels should be >= 1, got %d",
329+
caps.min_total_channels);
330+
331+
zassert_true(caps.max_total_channels >= caps.min_total_channels,
332+
"max_total_channels (%u) should be >= min_total_channels (%u)",
333+
caps.max_total_channels, caps.min_total_channels);
334+
335+
zassert_not_equal(caps.supported_sample_rates, 0, "supported_sample_rates should not be 0");
336+
337+
zassert_not_equal(caps.supported_bit_widths, 0, "supported_bit_widths should not be 0");
338+
339+
zassert_true(caps.min_num_buffers >= 1, "min_num_buffers should be >= 1, got %u",
340+
caps.min_num_buffers);
341+
342+
zassert_true(caps.max_frame_interval >= caps.min_frame_interval,
343+
"max_frame_interval (%u) should be >= min_frame_interval (%u)",
344+
caps.max_frame_interval, caps.min_frame_interval);
345+
346+
/**
347+
* Test Case 3: Error handling - NULL caps pointer
348+
* Expected: Function should return -EINVAL for invalid parameter
349+
*/
350+
ret = dmic_get_caps(dmic_dev, NULL);
351+
zassert_equal(ret, -EINVAL,
352+
"dmic_get_caps should return -EINVAL for NULL caps pointer, got %d", ret);
353+
}
354+
294355
ZTEST_SUITE(dmic, NULL, NULL, NULL, NULL, NULL);

tests/drivers/i2s/i2s_api/src/test_i2s_errors.c

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,10 @@
66

77
#include <zephyr/kernel.h>
88
#include <zephyr/ztest.h>
9+
10+
#include <zephyr/audio/audio_caps.h>
911
#include <zephyr/drivers/i2s.h>
12+
1013
#include "i2s_api_test.h"
1114

1215
#define INVALID_TRIGGER_SETTING 7
@@ -154,3 +157,61 @@ ZTEST_USER(i2s_errors, test_i2s_improper_block_size_write)
154157
err, 0,
155158
"I2S attempting write with incorrect block size did not raise error, err=%d", err);
156159
}
160+
161+
/**
162+
* @brief Test the i2s_get_caps API function
163+
*
164+
* This test validates the I2S get_caps API functionality by testing both
165+
* successful operation and error handling scenarios.
166+
*/
167+
ZTEST_USER(i2s_errors, test_i2s_get_caps)
168+
{
169+
int ret;
170+
struct audio_caps caps;
171+
172+
/**
173+
* Test Case 1: Normal operation - Valid parameters
174+
* Expected: Function should return 0 (success) or -ENOSYS (not implemented)
175+
*/
176+
ret = i2s_get_caps(dev_i2s, &caps);
177+
178+
/* Handle case where driver doesn't implement get_caps */
179+
if (ret == -ENOSYS) {
180+
TC_PRINT("I2S get_caps not implemented by driver\n");
181+
ztest_test_skip();
182+
return;
183+
}
184+
185+
/* Verify successful execution */
186+
zassert_equal(ret, 0, "i2s_get_caps should return 0, got %d", ret);
187+
188+
/**
189+
* Test Case 2: Capability value validation
190+
* Verify that returned capability values are within reasonable ranges
191+
*/
192+
zassert_true(caps.min_total_channels >= 1, "min_total_channels should be >= 1, got %u",
193+
caps.min_total_channels);
194+
195+
zassert_true(caps.max_total_channels >= caps.min_total_channels,
196+
"max_total_channels (%u) should be >= min_total_channels (%u)",
197+
caps.max_total_channels, caps.min_total_channels);
198+
199+
zassert_not_equal(caps.supported_sample_rates, 0, "supported_sample_rates should not be 0");
200+
201+
zassert_not_equal(caps.supported_bit_widths, 0, "supported_bit_widths should not be 0");
202+
203+
zassert_true(caps.min_num_buffers >= 1, "min_num_buffers should be >= 1, got %u",
204+
caps.min_num_buffers);
205+
206+
zassert_true(caps.max_frame_interval >= caps.min_frame_interval,
207+
"max_frame_interval (%u) should be >= min_frame_interval (%u)",
208+
caps.max_frame_interval, caps.min_frame_interval);
209+
210+
/**
211+
* Test Case 3: Error handling - NULL caps pointer
212+
* Expected: Function should return -EINVAL for invalid parameter
213+
*/
214+
ret = i2s_get_caps(dev_i2s, NULL);
215+
zassert_equal(ret, -EINVAL,
216+
"i2s_get_caps should return -EINVAL for NULL caps pointer, got %d", ret);
217+
}

0 commit comments

Comments
 (0)