[alsa-utils][PATCH 08/14] axfer: test: minor code arrangement to use the same file descriptor for container-test

Takashi Sakamoto o-takashi at sakamocchi.jp
Thu Mar 11 06:21:39 CET 2021


In container test program, two file descriptors open to the same file
for builder and parser contexts, however the same file descriptor is
available for the case.

This commit arranges to use the same file descriptor for the contexts.

Signed-off-by: Takashi Sakamoto <o-takashi at sakamocchi.jp>
---
 axfer/test/container-test.c | 40 +++++++++++++++++++++----------------
 1 file changed, 23 insertions(+), 17 deletions(-)

diff --git a/axfer/test/container-test.c b/axfer/test/container-test.c
index d89852a..4d825b6 100644
--- a/axfer/test/container-test.c
+++ b/axfer/test/container-test.c
@@ -24,8 +24,8 @@ struct container_trial {
 	bool verbose;
 };
 
-static void test_builder(struct container_context *cntr,
-			 enum container_format format, const char *const name,
+static void test_builder(struct container_context *cntr, int fd,
+			 enum container_format format,
 			 snd_pcm_access_t access,
 			 snd_pcm_format_t sample_format,
 			 unsigned int samples_per_frame,
@@ -33,7 +33,6 @@ static void test_builder(struct container_context *cntr,
 			 void *frame_buffer, unsigned int frame_count,
 			 bool verbose)
 {
-	int fd;
 	snd_pcm_format_t sample;
 	unsigned int channels;
 	unsigned int rate;
@@ -42,9 +41,6 @@ static void test_builder(struct container_context *cntr,
 	uint64_t total_frame_count;
 	int err;
 
-	fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
-	assert(fd >= 0);
-
 	err = container_builder_init(cntr, fd, format, verbose);
 	assert(err == 0);
 
@@ -73,18 +69,16 @@ static void test_builder(struct container_context *cntr,
 	assert(total_frame_count == frame_count);
 
 	container_context_destroy(cntr);
-	close(fd);
 }
 
-static void test_parser(struct container_context *cntr,
-		        enum container_format format, const char *const name,
+static void test_parser(struct container_context *cntr, int fd,
+		        enum container_format format,
 		        snd_pcm_access_t access, snd_pcm_format_t sample_format,
 		        unsigned int samples_per_frame,
 		        unsigned int frames_per_second,
 		        void *frame_buffer, unsigned int frame_count,
 			bool verbose)
 {
-	int fd;
 	snd_pcm_format_t sample;
 	unsigned int channels;
 	unsigned int rate;
@@ -92,9 +86,6 @@ static void test_parser(struct container_context *cntr,
 	unsigned int handled_frame_count;
 	int err;
 
-	fd = open(name, O_RDONLY);
-	assert(fd >= 0);
-
 	err = container_parser_init(cntr, fd, verbose);
 	assert(err == 0);
 
@@ -122,7 +113,6 @@ static void test_parser(struct container_context *cntr,
 	assert(total_frame_count == handled_frame_count);
 
 	container_context_destroy(cntr);
-	close(fd);
 }
 
 static int callback(struct test_generator *gen, snd_pcm_access_t access,
@@ -156,26 +146,42 @@ static int callback(struct test_generator *gen, snd_pcm_access_t access,
 	unlink(name);
 
 	for (i = 0; i < ARRAY_SIZE(entries); ++i) {
+		int fd;
+		off64_t pos;
+
 		frames_per_second = entries[i];
 
-		test_builder(&trial->cntr, trial->format, name, access,
+		fd = open(name, O_RDWR | O_CREAT | O_TRUNC, 0644);
+		if (fd < 0) {
+			err = -errno;
+			break;
+		}
+
+		test_builder(&trial->cntr, fd, trial->format, access,
 			     sample_format, samples_per_frame,
 			     frames_per_second, frame_buffer, frame_count,
 			     trial->verbose);
 
-		test_parser(&trial->cntr, trial->format, name, access,
+		pos = lseek64(fd, 0, SEEK_SET);
+		if (pos < 0) {
+			err = -errno;
+			break;
+		}
+
+		test_parser(&trial->cntr, fd, trial->format, access,
 			    sample_format, samples_per_frame, frames_per_second,
 			    buf, frame_count, trial->verbose);
 
 		err = memcmp(buf, frame_buffer, size);
 		assert(err == 0);
 
+		close(fd);
 		unlink(name);
 	}
 
 	free(buf);
 
-	return 0;
+	return err;
 }
 
 int main(int argc, const char *argv[])
-- 
2.27.0



More information about the Alsa-devel mailing list