linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
@ 2024-01-25 22:35 Ivan Orlov
  2024-01-25 22:35 ` [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function Ivan Orlov
                   ` (3 more replies)
  0 siblings, 4 replies; 8+ messages in thread
From: Ivan Orlov @ 2024-01-25 22:35 UTC (permalink / raw)
  To: perex, tiwai; +Cc: Ivan Orlov, linux-sound, linux-kernel

Add 4 missing formats to 'snd_pcm_format_names' array in order to be
able to get their names with 'snd_pcm_format_name' function.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 sound/core/pcm.c | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index a09f0154e6a7..d0788126cbab 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
 	FORMAT(DSD_U32_LE),
 	FORMAT(DSD_U16_BE),
 	FORMAT(DSD_U32_BE),
+	FORMAT(S20_LE),
+	FORMAT(S20_BE),
+	FORMAT(U20_LE),
+	FORMAT(U20_BE),
 };
 
 /**
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function
  2024-01-25 22:35 [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Ivan Orlov
@ 2024-01-25 22:35 ` Ivan Orlov
  2024-01-25 22:35 ` [PATCH 3/3] ALSA: core: Add sound core KUnit test Ivan Orlov
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 8+ messages in thread
From: Ivan Orlov @ 2024-01-25 22:35 UTC (permalink / raw)
  To: perex, tiwai; +Cc: Ivan Orlov, linux-sound, linux-kernel

Fix snd_pcm_format_name so it won't return NULL-pointer in case if it
can't find the format in the 'snd_pcm_format_names' list. Return
"Unknown" instead, as it is done if the number passed to the function
is larger than a list size.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 sound/core/pcm.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sound/core/pcm.c b/sound/core/pcm.c
index d0788126cbab..d9b338088d10 100644
--- a/sound/core/pcm.c
+++ b/sound/core/pcm.c
@@ -225,9 +225,11 @@ static const char * const snd_pcm_format_names[] = {
  */
 const char *snd_pcm_format_name(snd_pcm_format_t format)
 {
-	if ((__force unsigned int)format >= ARRAY_SIZE(snd_pcm_format_names))
+	unsigned int format_num = (__force unsigned int)format;
+
+	if (format_num >= ARRAY_SIZE(snd_pcm_format_names) || !snd_pcm_format_names[format_num])
 		return "Unknown";
-	return snd_pcm_format_names[(__force unsigned int)format];
+	return snd_pcm_format_names[format_num];
 }
 EXPORT_SYMBOL_GPL(snd_pcm_format_name);
 
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* [PATCH 3/3] ALSA: core: Add sound core KUnit test
  2024-01-25 22:35 [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Ivan Orlov
  2024-01-25 22:35 ` [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function Ivan Orlov
@ 2024-01-25 22:35 ` Ivan Orlov
  2024-01-26  9:01 ` [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Amadeusz Sławiński
  2024-01-30 13:15 ` Takashi Iwai
  3 siblings, 0 replies; 8+ messages in thread
From: Ivan Orlov @ 2024-01-25 22:35 UTC (permalink / raw)
  To: perex, tiwai; +Cc: Ivan Orlov, linux-sound, linux-kernel

At the moment, we have a decent amount of integration tests (selftests)
covering different aspects of the sound subsystem. However, a lot of
of sound-related in-kernel functions remains uncovered. This patch
introduces the KUnit test for the core part of the sound subsystem.
It includes 10 test cases:

- Coverage of the format-related inline functions from 'pcm.h' header
file: snd_pcm_format_physical_width, snd_pcm_format_width,
snd_pcm_format_signed, test_format_endianness

- Coverage of the available bytes counting functions from 'pcm.h'
header: snd_pcm_capture_avail, snd_pcm_playback_avail

- Coverage of functions from pcm_misc: snd_pcm_format_set_silence,
snd_pcm_format_name

- Coverage of card-related functions from init.c: snd_card_set_id,
snd_component_add

This patch depends on the previous patches in this patch series as they
contain fix for the bug, which was found during the test development.
Without them, the test doesn't pass.

Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
---
 MAINTAINERS              |   6 +
 sound/core/Kconfig       |  16 ++
 sound/core/Makefile      |   2 +
 sound/core/sound_kunit.c | 310 +++++++++++++++++++++++++++++++++++++++
 4 files changed, 334 insertions(+)
 create mode 100644 sound/core/sound_kunit.c

diff --git a/MAINTAINERS b/MAINTAINERS
index 39219b144c23..40b79daad32b 100644
--- a/MAINTAINERS
+++ b/MAINTAINERS
@@ -20485,6 +20485,12 @@ F:	include/uapi/sound/compress_*
 F:	sound/core/compress_offload.c
 F:	sound/soc/soc-compress.c
 
+SOUND - CORE KUNIT TEST
+M:	Ivan Orlov <ivan.orlov0322@gmail.com>
+L:	linux-sound@vger.kernel.org
+S:	Supported
+F:	sound/core/sound_kunit.c
+
 SOUND - DMAENGINE HELPERS
 M:	Lars-Peter Clausen <lars@metafoo.de>
 S:	Supported
diff --git a/sound/core/Kconfig b/sound/core/Kconfig
index e41818e59a15..664c6ee2b5a1 100644
--- a/sound/core/Kconfig
+++ b/sound/core/Kconfig
@@ -39,6 +39,22 @@ config SND_UMP_LEGACY_RAWMIDI
 	  legacy MIDI 1.0 byte streams is created for each UMP Endpoint.
 	  The device contains 16 substreams corresponding to UMP groups.
 
+config SND_CORE_TEST
+	tristate "Sound core KUnit test"
+	depends on KUNIT
+	default KUNIT_ALL_TESTS
+	help
+	  This options enables the sound core functions KUnit test.
+
+	  KUnit tests run during boot and output the results to the debug
+	  log in TAP format (https://testanything.org/). Only useful for
+	  kernel devs running KUnit test harness and are not for inclusion
+	  into a production build.
+
+	  For more information on KUnit and unit tests in general, refer
+	  to the KUnit documentation in Documentation/dev-tools/kunit/.
+
+
 config SND_COMPRESS_OFFLOAD
 	tristate
 
diff --git a/sound/core/Makefile b/sound/core/Makefile
index a6b444ee2832..1d34e6950317 100644
--- a/sound/core/Makefile
+++ b/sound/core/Makefile
@@ -49,6 +49,8 @@ obj-$(CONFIG_SND_SEQ_DEVICE)	+= snd-seq-device.o
 obj-$(CONFIG_SND_RAWMIDI)	+= snd-rawmidi.o
 obj-$(CONFIG_SND_UMP)		+= snd-ump.o
 
+obj-$(CONFIG_SND_CORE_TEST)	+= sound_kunit.o
+
 obj-$(CONFIG_SND_OSSEMUL)	+= oss/
 obj-$(CONFIG_SND_SEQUENCER)	+= seq/
 
diff --git a/sound/core/sound_kunit.c b/sound/core/sound_kunit.c
new file mode 100644
index 000000000000..5d5a7bf88de4
--- /dev/null
+++ b/sound/core/sound_kunit.c
@@ -0,0 +1,310 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Sound core KUnit test
+ * Author: Ivan Orlov <ivan.orlov0322@gmail.com>
+ */
+
+#include <kunit/test.h>
+#include <sound/core.h>
+#include <sound/pcm.h>
+
+#define SILENCE_BUFFER_SIZE 2048
+#define SILENCE(...) { __VA_ARGS__ }
+#define DEFINE_FORMAT(fmt, pbits, wd, endianness, signd, silence_arr) {		\
+	.format = SNDRV_PCM_FORMAT_##fmt, .physical_bits = pbits,		\
+	.width = wd, .le = endianness, .sd = signd, .silence = silence_arr,	\
+	.name = #fmt,								\
+}
+
+#define WRONG_FORMAT (SNDRV_PCM_FORMAT_LAST + 1)
+
+#define VALID_NAME "ValidName"
+#define NAME_W_SPEC_CHARS "In%v@1id name"
+#define NAME_W_SPACE "Test name"
+#define NAME_W_SPACE_REMOVED "Testname"
+
+#define TEST_FIRST_COMPONENT "Component1"
+#define TEST_SECOND_COMPONENT "Component2"
+
+struct snd_format_test_data {
+	snd_pcm_format_t format;
+	int physical_bits;
+	int width;
+	int le;
+	int sd;
+	unsigned char silence[8];
+	unsigned char *name;
+};
+
+struct avail_test_data {
+	snd_pcm_uframes_t buffer_size;
+	snd_pcm_uframes_t hw_ptr;
+	snd_pcm_uframes_t appl_ptr;
+	snd_pcm_uframes_t expected_avail;
+};
+
+static struct snd_format_test_data valid_fmt[] = {
+	DEFINE_FORMAT(S8, 8, 8, -1, 1, SILENCE()),
+	DEFINE_FORMAT(U8, 8, 8, -1, 0, SILENCE(0x80)),
+	DEFINE_FORMAT(S16_LE, 16, 16, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S16_BE, 16, 16, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U16_LE, 16, 16, 1, 0, SILENCE(0x00, 0x80)),
+	DEFINE_FORMAT(U16_BE, 16, 16, 0, 0, SILENCE(0x80, 0x00)),
+	DEFINE_FORMAT(S24_LE, 32, 24, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S24_BE, 32, 24, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U24_LE, 32, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
+	DEFINE_FORMAT(U24_BE, 32, 24, 0, 0, SILENCE(0x00, 0x80, 0x00, 0x00)),
+	DEFINE_FORMAT(S32_LE, 32, 32, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S32_BE, 32, 32, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U32_LE, 32, 32, 1, 0, SILENCE(0x00, 0x00, 0x00, 0x80)),
+	DEFINE_FORMAT(U32_BE, 32, 32, 0, 0, SILENCE(0x80, 0x00, 0x00, 0x00)),
+	DEFINE_FORMAT(FLOAT_LE, 32, 32, 1, -1, SILENCE()),
+	DEFINE_FORMAT(FLOAT_BE, 32, 32, 0, -1, SILENCE()),
+	DEFINE_FORMAT(FLOAT64_LE, 64, 64, 1, -1, SILENCE()),
+	DEFINE_FORMAT(FLOAT64_BE, 64, 64, 0, -1, SILENCE()),
+	DEFINE_FORMAT(IEC958_SUBFRAME_LE, 32, 32, 1, -1, SILENCE()),
+	DEFINE_FORMAT(IEC958_SUBFRAME_BE, 32, 32, 0, -1, SILENCE()),
+	DEFINE_FORMAT(MU_LAW, 8, 8, -1, -1, SILENCE(0x7f)),
+	DEFINE_FORMAT(A_LAW, 8, 8, -1, -1, SILENCE(0x55)),
+	DEFINE_FORMAT(IMA_ADPCM, 4, 4, -1, -1, SILENCE()),
+	DEFINE_FORMAT(G723_24, 3, 3, -1, -1, SILENCE()),
+	DEFINE_FORMAT(G723_40, 5, 5, -1, -1, SILENCE()),
+	DEFINE_FORMAT(DSD_U8, 8, 8, 1, 0, SILENCE(0x69)),
+	DEFINE_FORMAT(DSD_U16_LE, 16, 16, 1, 0, SILENCE(0x69, 0x69)),
+	DEFINE_FORMAT(DSD_U32_LE, 32, 32, 1, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
+	DEFINE_FORMAT(DSD_U16_BE, 16, 16, 0, 0, SILENCE(0x69, 0x69)),
+	DEFINE_FORMAT(DSD_U32_BE, 32, 32, 0, 0, SILENCE(0x69, 0x69, 0x69, 0x69)),
+	DEFINE_FORMAT(S20_LE, 32, 20, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S20_BE, 32, 20, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U20_LE, 32, 20, 1, 0, SILENCE(0x00, 0x00, 0x08, 0x00)),
+	DEFINE_FORMAT(U20_BE, 32, 20, 0, 0, SILENCE(0x00, 0x08, 0x00, 0x00)),
+	DEFINE_FORMAT(S24_3LE, 24, 24, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S24_3BE, 24, 24, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U24_3LE, 24, 24, 1, 0, SILENCE(0x00, 0x00, 0x80)),
+	DEFINE_FORMAT(U24_3BE, 24, 24, 0, 0, SILENCE(0x80, 0x00, 0x00)),
+	DEFINE_FORMAT(S20_3LE, 24, 20, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S20_3BE, 24, 20, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U20_3LE, 24, 20, 1, 0, SILENCE(0x00, 0x00, 0x08)),
+	DEFINE_FORMAT(U20_3BE, 24, 20, 0, 0, SILENCE(0x08, 0x00, 0x00)),
+	DEFINE_FORMAT(S18_3LE, 24, 18, 1, 1, SILENCE()),
+	DEFINE_FORMAT(S18_3BE, 24, 18, 0, 1, SILENCE()),
+	DEFINE_FORMAT(U18_3LE, 24, 18, 1, 0, SILENCE(0x00, 0x00, 0x02)),
+	DEFINE_FORMAT(U18_3BE, 24, 18, 0, 0, SILENCE(0x02, 0x00, 0x00)),
+	DEFINE_FORMAT(G723_24_1B, 8, 3, -1, -1, SILENCE()),
+	DEFINE_FORMAT(G723_40_1B, 8, 5, -1, -1, SILENCE()),
+};
+
+static void test_phys_format_size(struct kunit *test)
+{
+	u32 i;
+
+	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(valid_fmt[i].format),
+				valid_fmt[i].physical_bits);
+	}
+
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(WRONG_FORMAT), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_physical_width(-1), -EINVAL);
+}
+
+static void test_format_width(struct kunit *test)
+{
+	u32 i;
+
+	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_width(valid_fmt[i].format),
+				valid_fmt[i].width);
+	}
+
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+}
+
+static void test_format_signed(struct kunit *test)
+{
+	u32 i;
+
+	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_signed(valid_fmt[i].format),
+				valid_fmt[i].sd < 0 ? -EINVAL : valid_fmt[i].sd);
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_unsigned(valid_fmt[i].format),
+				valid_fmt[i].sd < 0 ? -EINVAL : 1 - valid_fmt[i].sd);
+	}
+
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(WRONG_FORMAT), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_width(-1), -EINVAL);
+}
+
+static void test_format_endianness(struct kunit *test)
+{
+	u32 i;
+
+	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(valid_fmt[i].format),
+				valid_fmt[i].le < 0 ? -EINVAL : valid_fmt[i].le);
+		KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(valid_fmt[i].format),
+				valid_fmt[i].le < 0 ? -EINVAL : 1 - valid_fmt[i].le);
+	}
+
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(WRONG_FORMAT), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_little_endian(-1), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(WRONG_FORMAT), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_big_endian(-1), -EINVAL);
+}
+
+static void _test_fill_silence(struct kunit *test, struct snd_format_test_data *data,
+			       u8 *buffer, size_t samples_count)
+{
+	size_t sample_bytes = data->physical_bits >> 3;
+	u32 i;
+
+	KUNIT_ASSERT_EQ(test, snd_pcm_format_set_silence(data->format, buffer, samples_count), 0);
+	for (i = 0; i < samples_count * sample_bytes; i++)
+		KUNIT_EXPECT_EQ(test, buffer[i], data->silence[i % sample_bytes]);
+}
+
+static void test_format_fill_silence(struct kunit *test)
+{
+	u32 buf_samples[] = { 10, 20, 32, 64, 129, 260 };
+	u8 *buffer;
+	u32 i, j;
+
+	buffer = kunit_kzalloc(test, SILENCE_BUFFER_SIZE, GFP_KERNEL);
+
+	for (i = 0; i < ARRAY_SIZE(buf_samples); i++) {
+		for (j = 0; j < ARRAY_SIZE(valid_fmt); j++)
+			_test_fill_silence(test, &valid_fmt[j], buffer, buf_samples[i]);
+	}
+
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(WRONG_FORMAT, buffer, 20), -EINVAL);
+	KUNIT_EXPECT_EQ(test, snd_pcm_format_set_silence(SNDRV_PCM_FORMAT_LAST, buffer, 0), 0);
+}
+
+static snd_pcm_uframes_t calculate_boundary(snd_pcm_uframes_t buffer_size)
+{
+	snd_pcm_uframes_t boundary = buffer_size;
+
+	while (boundary * 2 <= 0x7fffffffUL - buffer_size)
+		boundary *= 2;
+	return boundary;
+}
+
+static struct avail_test_data p_avail_data[] = {
+	/* buf_size + hw_ptr < appl_ptr => avail = buf_size + hw_ptr - appl_ptr + boundary */
+	{ 128, 1000, 1129, 1073741824UL - 1 },
+	/*
+	 * buf_size + hw_ptr - appl_ptr >= boundary =>
+	 * => avail = buf_size + hw_ptr - appl_ptr - boundary
+	 */
+	{ 128, 1073741824UL, 10, 118 },
+	/* standard case: avail = buf_size + hw_ptr - appl_ptr */
+	{ 128, 1000, 1001, 127 },
+};
+
+static void test_playback_avail(struct kunit *test)
+{
+	struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
+	u32 i;
+
+	r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
+	r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
+
+	for (i = 0; i < ARRAY_SIZE(p_avail_data); i++) {
+		r->buffer_size = p_avail_data[i].buffer_size;
+		r->boundary = calculate_boundary(r->buffer_size);
+		r->status->hw_ptr = p_avail_data[i].hw_ptr;
+		r->control->appl_ptr = p_avail_data[i].appl_ptr;
+		KUNIT_EXPECT_EQ(test, snd_pcm_playback_avail(r), p_avail_data[i].expected_avail);
+	}
+}
+
+static struct avail_test_data c_avail_data[] = {
+	/* hw_ptr - appl_ptr < 0 => avail = hw_ptr - appl_ptr + boundary */
+	{ 128, 1000, 1001, 1073741824UL - 1 },
+	/* standard case: avail = hw_ptr - appl_ptr */
+	{ 128, 1001, 1000, 1 },
+};
+
+static void test_capture_avail(struct kunit *test)
+{
+	struct snd_pcm_runtime *r = kunit_kzalloc(test, sizeof(*r), GFP_KERNEL);
+	u32 i;
+
+	r->status = kunit_kzalloc(test, sizeof(*r->status), GFP_KERNEL);
+	r->control = kunit_kzalloc(test, sizeof(*r->control), GFP_KERNEL);
+
+	for (i = 0; i < ARRAY_SIZE(c_avail_data); i++) {
+		r->buffer_size = c_avail_data[i].buffer_size;
+		r->boundary = calculate_boundary(r->buffer_size);
+		r->status->hw_ptr = c_avail_data[i].hw_ptr;
+		r->control->appl_ptr = c_avail_data[i].appl_ptr;
+		KUNIT_EXPECT_EQ(test, snd_pcm_capture_avail(r), c_avail_data[i].expected_avail);
+	}
+}
+
+static void test_card_set_id(struct kunit *test)
+{
+	struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
+
+	snd_card_set_id(card, VALID_NAME);
+	KUNIT_EXPECT_STREQ(test, card->id, VALID_NAME);
+
+	/* clear the first id character so we can set it again */
+	card->id[0] = '\0';
+	snd_card_set_id(card, NAME_W_SPEC_CHARS);
+	KUNIT_EXPECT_STRNEQ(test, card->id, NAME_W_SPEC_CHARS);
+
+	card->id[0] = '\0';
+	snd_card_set_id(card, NAME_W_SPACE);
+	kunit_info(test, "%s", card->id);
+	KUNIT_EXPECT_STREQ(test, card->id, NAME_W_SPACE_REMOVED);
+}
+
+static void test_pcm_format_name(struct kunit *test)
+{
+	u32 i;
+	const char *name;
+
+	for (i = 0; i < ARRAY_SIZE(valid_fmt); i++) {
+		name = snd_pcm_format_name(valid_fmt[i].format);
+		KUNIT_ASSERT_NOT_NULL_MSG(test, name, "Don't have name for %s", valid_fmt[i].name);
+		KUNIT_EXPECT_STREQ(test, name, valid_fmt[i].name);
+	}
+
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(WRONG_FORMAT), "Unknown");
+	KUNIT_ASSERT_STREQ(test, snd_pcm_format_name(-1), "Unknown");
+}
+
+static void test_card_add_component(struct kunit *test)
+{
+	struct snd_card *card = kunit_kzalloc(test, sizeof(*card), GFP_KERNEL);
+
+	snd_component_add(card, TEST_FIRST_COMPONENT);
+	KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT);
+
+	snd_component_add(card, TEST_SECOND_COMPONENT);
+	KUNIT_ASSERT_STREQ(test, card->components, TEST_FIRST_COMPONENT " " TEST_SECOND_COMPONENT);
+}
+
+static struct kunit_case sound_utils_cases[] = {
+	KUNIT_CASE(test_phys_format_size),
+	KUNIT_CASE(test_format_width),
+	KUNIT_CASE(test_format_endianness),
+	KUNIT_CASE(test_format_signed),
+	KUNIT_CASE(test_format_fill_silence),
+	KUNIT_CASE(test_playback_avail),
+	KUNIT_CASE(test_capture_avail),
+	KUNIT_CASE(test_card_set_id),
+	KUNIT_CASE(test_pcm_format_name),
+	KUNIT_CASE(test_card_add_component),
+	{},
+};
+
+static struct kunit_suite sound_utils_suite = {
+	.name = "sound-core-test",
+	.test_cases = sound_utils_cases,
+};
+
+kunit_test_suite(sound_utils_suite);
+MODULE_AUTHOR("Ivan Orlov");
+MODULE_LICENSE("GPL");
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
  2024-01-25 22:35 [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Ivan Orlov
  2024-01-25 22:35 ` [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function Ivan Orlov
  2024-01-25 22:35 ` [PATCH 3/3] ALSA: core: Add sound core KUnit test Ivan Orlov
@ 2024-01-26  9:01 ` Amadeusz Sławiński
  2024-01-31 17:41   ` Ivan Orlov
  2024-01-30 13:15 ` Takashi Iwai
  3 siblings, 1 reply; 8+ messages in thread
From: Amadeusz Sławiński @ 2024-01-26  9:01 UTC (permalink / raw)
  To: Ivan Orlov, perex, tiwai; +Cc: linux-sound, linux-kernel

On 1/25/2024 11:35 PM, Ivan Orlov wrote:
> Add 4 missing formats to 'snd_pcm_format_names' array in order to be
> able to get their names with 'snd_pcm_format_name' function.
> 
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
> ---
>   sound/core/pcm.c | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
> index a09f0154e6a7..d0788126cbab 100644
> --- a/sound/core/pcm.c
> +++ b/sound/core/pcm.c
> @@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
>   	FORMAT(DSD_U32_LE),
>   	FORMAT(DSD_U16_BE),
>   	FORMAT(DSD_U32_BE),
> +	FORMAT(S20_LE),
> +	FORMAT(S20_BE),
> +	FORMAT(U20_LE),
> +	FORMAT(U20_BE),
>   };
>   
>   /**

Maybe we can also add some kind of static_assert to check at compile 
time that all formats are handled, something like:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST 
+ 1);

Although looking at definitions there is a hole between 
SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause 
this idea to fail.

Perhaps with comment:
static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST 
+ 1 - 2); /* -2 for formats reserved for future use */
?

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
  2024-01-25 22:35 [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Ivan Orlov
                   ` (2 preceding siblings ...)
  2024-01-26  9:01 ` [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Amadeusz Sławiński
@ 2024-01-30 13:15 ` Takashi Iwai
  2024-01-31 17:42   ` Ivan Orlov
  3 siblings, 1 reply; 8+ messages in thread
From: Takashi Iwai @ 2024-01-30 13:15 UTC (permalink / raw)
  To: Ivan Orlov; +Cc: perex, tiwai, linux-sound, linux-kernel

On Thu, 25 Jan 2024 23:35:19 +0100,
Ivan Orlov wrote:
> 
> Add 4 missing formats to 'snd_pcm_format_names' array in order to be
> able to get their names with 'snd_pcm_format_name' function.
> 
> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>

Now applied all three patches.  This first one is to for-linus, while
the rest two are to for-next for 6.9 kernel.

For further improvements (e.g. Amadeusz suggested), let's apply on top
of that.


thanks,

Takashi

^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
  2024-01-26  9:01 ` [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Amadeusz Sławiński
@ 2024-01-31 17:41   ` Ivan Orlov
  2024-02-01  8:54     ` Amadeusz Sławiński
  0 siblings, 1 reply; 8+ messages in thread
From: Ivan Orlov @ 2024-01-31 17:41 UTC (permalink / raw)
  To: Amadeusz Sławiński, perex, tiwai; +Cc: linux-sound, linux-kernel

On 1/26/24 09:01, Amadeusz Sławiński wrote:
> On 1/25/2024 11:35 PM, Ivan Orlov wrote:
>> Add 4 missing formats to 'snd_pcm_format_names' array in order to be
>> able to get their names with 'snd_pcm_format_name' function.
>>
>> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
>> ---
>>   sound/core/pcm.c | 4 ++++
>>   1 file changed, 4 insertions(+)
>>
>> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
>> index a09f0154e6a7..d0788126cbab 100644
>> --- a/sound/core/pcm.c
>> +++ b/sound/core/pcm.c
>> @@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] = {
>>       FORMAT(DSD_U32_LE),
>>       FORMAT(DSD_U16_BE),
>>       FORMAT(DSD_U32_BE),
>> +    FORMAT(S20_LE),
>> +    FORMAT(S20_BE),
>> +    FORMAT(U20_LE),
>> +    FORMAT(U20_BE),
>>   };
>>   /**
> 
> Maybe we can also add some kind of static_assert to check at compile 
> time that all formats are handled, something like:
> static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST 
> + 1);
> 
> Although looking at definitions there is a hole between 
> SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause 
> this idea to fail.
> 
> Perhaps with comment:
> static_assert(ARRAY_SIZE(snd_pcm_format_names) == SNDRV_PCM_FORMAT_LAST 
> + 1 - 2); /* -2 for formats reserved for future use */
> ?

Hi Amadeusz,

Thank you for the review and sorry for the late answer. Yes, I believe 
it could be a nice improvement! Could I send a patch and specify you in 
Suggested-by tag?

-- 
Kind regards,
Ivan Orlov


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
  2024-01-30 13:15 ` Takashi Iwai
@ 2024-01-31 17:42   ` Ivan Orlov
  0 siblings, 0 replies; 8+ messages in thread
From: Ivan Orlov @ 2024-01-31 17:42 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: perex, tiwai, linux-sound, linux-kernel

On 1/30/24 13:15, Takashi Iwai wrote:
> On Thu, 25 Jan 2024 23:35:19 +0100,
> Ivan Orlov wrote:
>>
>> Add 4 missing formats to 'snd_pcm_format_names' array in order to be
>> able to get their names with 'snd_pcm_format_name' function.
>>
>> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
> 
> Now applied all three patches.  This first one is to for-linus, while
> the rest two are to for-next for 6.9 kernel.
> 
> For further improvements (e.g. Amadeusz suggested), let's apply on top
> of that.
> 
> 
> thanks,
> 
> Takashi

Hi Takashi,

Thank you for applying this changes! I'll send the patch with the 
improvements suggested by Amadeusz as soon as possible.

-- 
Kind regards,
Ivan Orlov


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH 1/3] ALSA: pcm: Add missing formats to formats list
  2024-01-31 17:41   ` Ivan Orlov
@ 2024-02-01  8:54     ` Amadeusz Sławiński
  0 siblings, 0 replies; 8+ messages in thread
From: Amadeusz Sławiński @ 2024-02-01  8:54 UTC (permalink / raw)
  To: Ivan Orlov, perex, tiwai; +Cc: linux-sound, linux-kernel

On 1/31/2024 6:41 PM, Ivan Orlov wrote:
> On 1/26/24 09:01, Amadeusz Sławiński wrote:
>> On 1/25/2024 11:35 PM, Ivan Orlov wrote:
>>> Add 4 missing formats to 'snd_pcm_format_names' array in order to be
>>> able to get their names with 'snd_pcm_format_name' function.
>>>
>>> Signed-off-by: Ivan Orlov <ivan.orlov0322@gmail.com>
>>> ---
>>>   sound/core/pcm.c | 4 ++++
>>>   1 file changed, 4 insertions(+)
>>>
>>> diff --git a/sound/core/pcm.c b/sound/core/pcm.c
>>> index a09f0154e6a7..d0788126cbab 100644
>>> --- a/sound/core/pcm.c
>>> +++ b/sound/core/pcm.c
>>> @@ -211,6 +211,10 @@ static const char * const snd_pcm_format_names[] 
>>> = {
>>>       FORMAT(DSD_U32_LE),
>>>       FORMAT(DSD_U16_BE),
>>>       FORMAT(DSD_U32_BE),
>>> +    FORMAT(S20_LE),
>>> +    FORMAT(S20_BE),
>>> +    FORMAT(U20_LE),
>>> +    FORMAT(U20_BE),
>>>   };
>>>   /**
>>
>> Maybe we can also add some kind of static_assert to check at compile 
>> time that all formats are handled, something like:
>> static_assert(ARRAY_SIZE(snd_pcm_format_names) == 
>> SNDRV_PCM_FORMAT_LAST + 1);
>>
>> Although looking at definitions there is a hole between 
>> SNDRV_PCM_FORMAT_U20_BE & SNDRV_PCM_FORMAT_SPECIAL, which will cause 
>> this idea to fail.
>>
>> Perhaps with comment:
>> static_assert(ARRAY_SIZE(snd_pcm_format_names) == 
>> SNDRV_PCM_FORMAT_LAST + 1 - 2); /* -2 for formats reserved for future 
>> use */
>> ?
> 
> Hi Amadeusz,
> 
> Thank you for the review and sorry for the late answer. Yes, I believe 
> it could be a nice improvement! Could I send a patch and specify you in 
> Suggested-by tag?
> 

Hi,

yes, go for it!

Amadeusz

^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-02-01  8:54 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-25 22:35 [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Ivan Orlov
2024-01-25 22:35 ` [PATCH 2/3] ALSA: pcm: Fix snd_pcm_format_name function Ivan Orlov
2024-01-25 22:35 ` [PATCH 3/3] ALSA: core: Add sound core KUnit test Ivan Orlov
2024-01-26  9:01 ` [PATCH 1/3] ALSA: pcm: Add missing formats to formats list Amadeusz Sławiński
2024-01-31 17:41   ` Ivan Orlov
2024-02-01  8:54     ` Amadeusz Sławiński
2024-01-30 13:15 ` Takashi Iwai
2024-01-31 17:42   ` Ivan Orlov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).