diff mbox series

[2/3] lib: add tst_get_median helper binary for use in script tests

Message ID 20210202133454.59224-3-alexey.kodanev@oracle.com
State Accepted
Headers show
Series lib/tst_net.sh: calc median instead of mean in tst_netload() | expand

Commit Message

Alexey Kodanev Feb. 2, 2021, 1:34 p.m. UTC
/tst_get_median 10 11 300 8 9 14
output: 10

Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>
---
 testcases/lib/.gitignore       |  1 +
 testcases/lib/Makefile         |  3 ++-
 testcases/lib/tst_get_median.c | 37 ++++++++++++++++++++++++++++++++++
 3 files changed, 40 insertions(+), 1 deletion(-)
 create mode 100644 testcases/lib/tst_get_median.c

Comments

Petr Vorel Feb. 5, 2021, 10:15 a.m. UTC | #1
Hi Alexey,

> /tst_get_median 10 11 300 8 9 14
> output: 10

> Signed-off-by: Alexey Kodanev <alexey.kodanev@oracle.com>

Reviewed-by: Petr Vorel <pvorel@suse.cz>
Thanks, very good idea!

Kind regards,
Petr
diff mbox series

Patch

diff --git a/testcases/lib/.gitignore b/testcases/lib/.gitignore
index 52f99dc45..bc299b6ee 100644
--- a/testcases/lib/.gitignore
+++ b/testcases/lib/.gitignore
@@ -2,6 +2,7 @@ 
 /tst_checkpoint
 /tst_device
 /tst_getconf
+/tst_get_median
 /tst_get_unused_port
 /tst_kvcmp
 /tst_net_iface_prefix
diff --git a/testcases/lib/Makefile b/testcases/lib/Makefile
index 4616e24c0..f77da0d56 100644
--- a/testcases/lib/Makefile
+++ b/testcases/lib/Makefile
@@ -28,6 +28,7 @@  INSTALL_TARGETS		:= *.sh
 
 MAKE_TARGETS		:= tst_sleep tst_random tst_checkpoint tst_rod tst_kvcmp\
 			   tst_device tst_net_iface_prefix tst_net_ip_prefix tst_net_vars\
-			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port
+			   tst_getconf tst_supported_fs tst_check_drivers tst_get_unused_port\
+			   tst_get_median
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
diff --git a/testcases/lib/tst_get_median.c b/testcases/lib/tst_get_median.c
new file mode 100644
index 000000000..5246f12e0
--- /dev/null
+++ b/testcases/lib/tst_get_median.c
@@ -0,0 +1,37 @@ 
+// SPDX-License-Identifier: GPL-2.0-or-later
+/* Copyright (c) 2021 Oracle and/or its affiliates. All Rights Reserved. */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+static int cmp(const void *a, const void *b)
+{
+   return (*(int *)a - *(int *)b);
+}
+
+int main(int argc, const char *argv[])
+{
+	const size_t size = argc - 1;
+
+	if (!size) {
+		fprintf(stderr, "Please provide a numeric list\n");
+		return 1;
+	}
+	if (size == 1) {
+		printf("%d", atoi(argv[1]));
+		return 0;
+	}
+
+	int arr[size];
+	size_t i;
+
+	for (i = 0; i < size; ++i)
+		arr[i] = atoi(argv[i + 1]);
+
+	qsort(arr, size, sizeof(arr[0]), cmp);
+
+	const size_t size2 = size / 2;
+	printf("%d", (size & 1) ? arr[size2] : ((arr[size2 - 1] + arr[size2]) / 2));
+
+	return 0;
+}