[alsa-lib][PATCH 1/6] test: ctl-elem-id: add test program for future APIs relevant to control element ID

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Some APIs are planned to add for equality check and comparison of a pair
of control element IDs. The equality check and comparison are quite
basic methods to operate data, thus it's preferable not to include any
bug.

This commit adds skeleton of test program for the APIs.

Signed-off-by: Takashi Sakamoto <o-takashi@xxxxxxxxxxxxx>
---
 test/lsb/Makefile.am   |  6 +++--
 test/lsb/ctl-elem-id.c | 61 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 65 insertions(+), 2 deletions(-)
 create mode 100644 test/lsb/ctl-elem-id.c

diff --git a/test/lsb/Makefile.am b/test/lsb/Makefile.am
index ceb4d715..7d5f754d 100644
--- a/test/lsb/Makefile.am
+++ b/test/lsb/Makefile.am
@@ -1,5 +1,7 @@
-TESTS  = config
-TESTS += midi_event
+TESTS = \
+	config \
+	midi_event \
+	ctl-elem-id
 check_PROGRAMS = $(TESTS)
 noinst_HEADERS = test.h
 
diff --git a/test/lsb/ctl-elem-id.c b/test/lsb/ctl-elem-id.c
new file mode 100644
index 00000000..ae416698
--- /dev/null
+++ b/test/lsb/ctl-elem-id.c
@@ -0,0 +1,61 @@
+// SPDX-License-Identifier: GPL-3.0-or-later
+//
+// ctl-elem-id.c - a test program for equality and some comparison algorithms
+// for control element ID structure.
+//
+// Copyright (c) 2021 Takashi Sakamoto
+//
+// Licensed under the terms of the GNU General Public License, version 2.
+
+#include <stdlib.h>
+#include <stdint.h>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include <unistd.h>
+
+#include "../include/asoundlib.h"
+
+int main()
+{
+	void (*entries[])(snd_ctl_elem_id_t *l, snd_ctl_elem_id_t *r) = {
+	};
+	int count = sizeof(entries) / sizeof(*entries);
+	int fd;
+	uint8_t *buf;
+	int i;
+
+	fd = open("/dev/urandom", O_RDONLY);
+	if (fd < 0)
+		return EXIT_FAILURE;
+
+	buf = calloc(snd_ctl_elem_id_sizeof(), 2);
+	if (buf == NULL)
+		goto error_urandom;
+
+	for (i = 0; i < count; ++i) {
+		snd_ctl_elem_id_t *l, *r;
+		ssize_t len;
+randomize:
+		len = read(fd, buf, snd_ctl_elem_id_sizeof() * 2);
+		if (len < 0)
+			goto error_memory;
+		l = (snd_ctl_elem_id_t *)buf;
+		r = (snd_ctl_elem_id_t *)(buf + snd_ctl_elem_id_sizeof());
+		if (!memcmp(l, r, snd_ctl_elem_id_sizeof()))
+			goto randomize;
+
+		entries[i](l, r);
+	}
+
+	free(buf);
+
+	return EXIT_SUCCESS;
+error_memory:
+	free(buf);
+error_urandom:
+	close(fd);
+	return EXIT_FAILURE;
+}
-- 
2.27.0




[Index of Archives]     [ALSA User]     [Linux Audio Users]     [Pulse Audio]     [Kernel Archive]     [Asterisk PBX]     [Photo Sharing]     [Linux Sound]     [Video 4 Linux]     [Gimp]     [Yosemite News]

  Powered by Linux