Skip to content

Commit 22eeb8f

Browse files
broonietiwai
authored andcommitted
kselftest/alsa: Refactor pcm-test to list the tests to run in a struct
In order to help make the list of tests a bit easier to maintain refactor things so we pass the tests around as a struct with the parameters in, enabling us to add new tests by adding to a table with comments saying what each of the number are. We could also use named initializers if we get more parameters. Signed-off-by: Mark Brown <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Takashi Iwai <[email protected]>
1 parent 4e9050e commit 22eeb8f

File tree

1 file changed

+33
-20
lines changed

1 file changed

+33
-20
lines changed

tools/testing/selftests/alsa/pcm-test.c

Lines changed: 33 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,15 @@ struct pcm_data *pcm_list = NULL;
3737
int num_missing = 0;
3838
struct pcm_data *pcm_missing = NULL;
3939

40+
struct time_test_def {
41+
const char *cfg_prefix;
42+
const char *format;
43+
long rate;
44+
long channels;
45+
long period_size;
46+
long buffer_size;
47+
};
48+
4049
void timestamp_now(timestamp_t *tstamp)
4150
{
4251
if (clock_gettime(CLOCK_MONOTONIC_RAW, tstamp))
@@ -220,9 +229,7 @@ static void find_pcms(void)
220229
}
221230

222231
static void test_pcm_time1(struct pcm_data *data,
223-
const char *cfg_prefix, const char *sformat,
224-
long srate, long schannels,
225-
long speriod_size, long sbuffer_size)
232+
const struct time_test_def *test)
226233
{
227234
char name[64], key[128], msg[256];
228235
const char *cs;
@@ -244,20 +251,20 @@ static void test_pcm_time1(struct pcm_data *data,
244251
snd_pcm_hw_params_alloca(&hw_params);
245252
snd_pcm_sw_params_alloca(&sw_params);
246253

247-
cs = conf_get_string(data->pcm_config, cfg_prefix, "format", sformat);
254+
cs = conf_get_string(data->pcm_config, test->cfg_prefix, "format", test->format);
248255
format = snd_pcm_format_value(cs);
249256
if (format == SND_PCM_FORMAT_UNKNOWN)
250257
ksft_exit_fail_msg("Wrong format '%s'\n", cs);
251-
rate = conf_get_long(data->pcm_config, cfg_prefix, "rate", srate);
252-
channels = conf_get_long(data->pcm_config, cfg_prefix, "channels", schannels);
253-
period_size = conf_get_long(data->pcm_config, cfg_prefix, "period_size", speriod_size);
254-
buffer_size = conf_get_long(data->pcm_config, cfg_prefix, "buffer_size", sbuffer_size);
258+
rate = conf_get_long(data->pcm_config, test->cfg_prefix, "rate", test->rate);
259+
channels = conf_get_long(data->pcm_config, test->cfg_prefix, "channels", test->channels);
260+
period_size = conf_get_long(data->pcm_config, test->cfg_prefix, "period_size", test->period_size);
261+
buffer_size = conf_get_long(data->pcm_config, test->cfg_prefix, "buffer_size", test->buffer_size);
255262

256-
automatic = strcmp(sformat, snd_pcm_format_name(format)) == 0 &&
257-
srate == rate &&
258-
schannels == channels &&
259-
speriod_size == period_size &&
260-
sbuffer_size == buffer_size;
263+
automatic = strcmp(test->format, snd_pcm_format_name(format)) == 0 &&
264+
test->rate == rate &&
265+
test->channels == channels &&
266+
test->period_size == period_size &&
267+
test->buffer_size == buffer_size;
261268

262269
samples = malloc((rate * channels * snd_pcm_format_physical_width(format)) / 8);
263270
if (!samples)
@@ -293,7 +300,7 @@ static void test_pcm_time1(struct pcm_data *data,
293300
if (automatic && format == SND_PCM_FORMAT_S16_LE) {
294301
format = SND_PCM_FORMAT_S32_LE;
295302
ksft_print_msg("%s.%d.%d.%d.%s.%s format S16_LE -> S32_LE\n",
296-
cfg_prefix,
303+
test->cfg_prefix,
297304
data->card, data->device, data->subdevice,
298305
snd_pcm_stream_name(data->stream),
299306
snd_pcm_access_name(access));
@@ -362,7 +369,7 @@ static void test_pcm_time1(struct pcm_data *data,
362369
}
363370

364371
ksft_print_msg("%s.%d.%d.%d.%s hw_params.%s.%s.%ld.%ld.%ld.%ld sw_params.%ld\n",
365-
cfg_prefix,
372+
test->cfg_prefix,
366373
data->card, data->device, data->subdevice,
367374
snd_pcm_stream_name(data->stream),
368375
snd_pcm_access_name(access),
@@ -411,7 +418,7 @@ static void test_pcm_time1(struct pcm_data *data,
411418
pass = true;
412419
__close:
413420
ksft_test_result(pass, "%s.%d.%d.%d.%s%s%s\n",
414-
cfg_prefix,
421+
test->cfg_prefix,
415422
data->card, data->device, data->subdevice,
416423
snd_pcm_stream_name(data->stream),
417424
msg[0] ? " " : "", msg);
@@ -420,19 +427,24 @@ static void test_pcm_time1(struct pcm_data *data,
420427
snd_pcm_close(handle);
421428
}
422429

423-
#define TESTS_PER_PCM 2
430+
static const struct time_test_def time_tests[] = {
431+
/* name format rate chan period buffer */
432+
{ "test.time1", "S16_LE", 48000, 2, 512, 4096 },
433+
{ "test.time2", "S16_LE", 48000, 2, 24000, 192000 },
434+
};
424435

425436
int main(void)
426437
{
427438
struct pcm_data *pcm;
439+
int i;
428440

429441
ksft_print_header();
430442

431443
conf_load();
432444

433445
find_pcms();
434446

435-
ksft_set_plan(num_missing + num_pcms * TESTS_PER_PCM);
447+
ksft_set_plan(num_missing + num_pcms * ARRAY_SIZE(time_tests));
436448

437449
for (pcm = pcm_missing; pcm != NULL; pcm = pcm->next) {
438450
ksft_test_result(false, "test.missing.%d.%d.%d.%s\n",
@@ -441,8 +453,9 @@ int main(void)
441453
}
442454

443455
for (pcm = pcm_list; pcm != NULL; pcm = pcm->next) {
444-
test_pcm_time1(pcm, "test.time1", "S16_LE", 48000, 2, 512, 4096);
445-
test_pcm_time1(pcm, "test.time2", "S16_LE", 48000, 2, 24000, 192000);
456+
for (i = 0; i < ARRAY_SIZE(time_tests); i++) {
457+
test_pcm_time1(pcm, &time_tests[i]);
458+
}
446459
}
447460

448461
conf_free();

0 commit comments

Comments
 (0)