Add unit tests for num possible cpus
authorMichael Jeanson <mjeanson@efficios.com>
Wed, 20 Jul 2022 19:02:41 +0000 (15:02 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Mon, 25 Jul 2022 17:51:58 +0000 (13:51 -0400)
Change-Id: I90eff0090b28cef64a8f4a5bd9745971ed89c711
Signed-off-by: Michael Jeanson <mjeanson@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
.gitignore
configure.ac
tests/Makefile.am
tests/unit/Makefile.am
tests/unit/libcommon/Makefile.am [new file with mode: 0644]
tests/unit/libcommon/test_smp.c [new file with mode: 0644]

index fb0f0bdb747c3f0f82d8a4f3699fdcd28e0dc06e..d47486317cda4ee61b0c1ae9375b920c09003afe 100644 (file)
@@ -113,6 +113,7 @@ cscope.*
 /tests/regression/abi0-conflict/app_ust_indirect_abi0_abi1
 /tests/regression/abi0-conflict/app_ust_indirect_abi1
 /tests/unit/gcc-weak-hidden/test_gcc_weak_hidden
+/tests/unit/libcommon/test_smp
 /tests/unit/libmsgpack/test_msgpack
 /tests/unit/libringbuffer/test_shm
 /tests/unit/pthread_name/test_pthread_name
@@ -208,6 +209,7 @@ cscope.*
 /tests/regression/Makefile
 /tests/unit/Makefile
 /tests/unit/gcc-weak-hidden/Makefile
+/tests/unit/libcommon/Makefile
 /tests/unit/libmsgpack/Makefile
 /tests/unit/libringbuffer/Makefile
 /tests/unit/pthread_name/Makefile
index 27f86dde7c6a367fec9f5beebba696cdcb021784..4cc65e1d70fb4f0a7f75adbd2030fa4f2e68e428 100644 (file)
@@ -657,6 +657,7 @@ AC_CONFIG_FILES([
   tests/regression/abi0-conflict/Makefile
   tests/regression/Makefile
   tests/unit/gcc-weak-hidden/Makefile
+  tests/unit/libcommon/Makefile
   tests/unit/libmsgpack/Makefile
   tests/unit/libringbuffer/Makefile
   tests/unit/Makefile
index d750ae391ac7309034ae631fa5391100b11b0375..29cfa814263f65593d5228e6b797a0379ead43de 100644 (file)
@@ -13,6 +13,7 @@ LOG_DRIVER = env AM_TAP_AWK='$(AWK)' \
 TESTS = \
        unit/libringbuffer/test_shm \
        unit/gcc-weak-hidden/test_gcc_weak_hidden \
+       unit/libcommon/test_smp \
        unit/libmsgpack/test_msgpack \
        unit/pthread_name/test_pthread_name \
        unit/snprintf/test_snprintf \
index 47cec612ef2708d5823189b82e7d1749ed2b59f3..75701efcbcdaa126118a9e3d1da5c5c1a19aa592 100644 (file)
@@ -2,6 +2,7 @@
 
 SUBDIRS = \
        gcc-weak-hidden \
+       libcommon \
        libmsgpack \
        libringbuffer \
        pthread_name \
diff --git a/tests/unit/libcommon/Makefile.am b/tests/unit/libcommon/Makefile.am
new file mode 100644 (file)
index 0000000..05b894e
--- /dev/null
@@ -0,0 +1,9 @@
+# SPDX-License-Identifier: LGPL-2.1-only
+
+AM_CPPFLAGS += -I$(top_srcdir)/tests/utils
+
+noinst_PROGRAMS = test_smp
+test_smp_SOURCES = test_smp.c
+test_smp_LDADD = \
+       $(top_builddir)/src/common/libcommon.la \
+       $(top_builddir)/tests/utils/libtap.a
diff --git a/tests/unit/libcommon/test_smp.c b/tests/unit/libcommon/test_smp.c
new file mode 100644 (file)
index 0000000..8f9a404
--- /dev/null
@@ -0,0 +1,84 @@
+/*
+ * SPDX-License-Identifier: LGPL-2.1-only
+ *
+ * Copyright (C) 2020 Francis Deslauriers <francis.deslauriers@efficios.com>
+ */
+
+#include <assert.h>
+#include <limits.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "tap.h"
+
+#include "common/smp.h"
+
+struct parse_test_data {
+       const char *buf;
+       int expected;
+};
+
+static struct parse_test_data parse_test_data[] = {
+       { "", 0 },
+       { "abc", 0 },
+       { ",,,", 0 },
+       { "--", 0 },
+       { ",", 0 },
+       { "-", 0 },
+       { "2147483647", 0 },
+       { "18446744073709551615", 0 },
+       { "0-2147483647", 0 },
+       { "0-18446744073709551615", 0 },
+       { "0", 1 },
+       { "1", 2 },
+       { "0-1", 2 },
+       { "1-3", 4 },
+       { "0,2", 3 },
+       { "1,2", 3 },
+       { "0,4-6,127", 128 },
+       { "0-4095", 4096 },
+
+       { "\n", 0 },
+       { "abc\n", 0 },
+       { ",,,\n", 0 },
+       { "--\n", 0 },
+       { ",\n", 0 },
+       { "-\n", 0 },
+       { "2147483647\n", 0 },
+       { "18446744073709551615\n", 0 },
+       { "0-2147483647\n", 0 },
+       { "0-18446744073709551615\n", 0 },
+       { "0\n", 1 },
+       { "1\n", 2 },
+       { "0-1\n", 2 },
+       { "1-3\n", 4 },
+       { "0,2\n", 3 },
+       { "1,2\n", 3 },
+       { "0,4-6,127\n", 128 },
+       { "0-4095\n", 4096 },
+};
+
+static int parse_test_data_len = sizeof(parse_test_data) / sizeof(parse_test_data[0]);
+
+int main(void)
+{
+       int ret, i;
+
+       plan_tests(parse_test_data_len + 1);
+
+       diag("Testing smp helpers");
+
+       for (i = 0; i < parse_test_data_len; i++) {
+               ret = get_num_possible_cpus_from_mask(parse_test_data[i].buf,
+                               strlen(parse_test_data[i].buf));
+               ok(ret == parse_test_data[i].expected,
+                       "get_num_possible_cpus_from_mask '%s', expected: '%d', result: '%d'",
+                       parse_test_data[i].buf, parse_test_data[i].expected, ret);
+       }
+
+       ok(num_possible_cpus() > 0, "num_possible_cpus (%d > 0)", num_possible_cpus());
+
+       return exit_status();
+}
This page took 0.028141 seconds and 4 git commands to generate.