diff mbox series

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

Message ID 20210318103013.265264-2-o-takashi@sakamocchi.jp
State New
Headers show
Series add API of equality and comparison for a pair of control element IDs | expand

Commit Message

Takashi Sakamoto March 18, 2021, 10:30 a.m. UTC
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@sakamocchi.jp>
---
 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 mbox series

Patch

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;
+}