From: Michael Jeanson Date: Mon, 1 Aug 2022 15:51:03 +0000 (-0400) Subject: Add unit tests for possible_cpus_array_len X-Git-Tag: v0.14.0~22 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=4de89c1129d52f0f13b63ec037802e095c506875;p=userspace-rcu.git Add unit tests for possible_cpus_array_len Change-Id: Ida3affbc7021c96bbc8941a96455ef93030c5c96 Signed-off-by: Michael Jeanson Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 0ef3e67..cbb0332 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,14 @@ tests/unit/test_urcu_multiflavor_single_unit tests/unit/test_urcu_multiflavor_single_unit_cxx tests/unit/test_urcu_multiflavor_single_unit_dynlink tests/unit/test_urcu_multiflavor_single_unit_dynlink_cxx +tests/unit/get_cpu_mask_from_sysfs +tests/unit/get_cpu_mask_from_sysfs_cxx +tests/unit/get_max_cpuid_from_sysfs +tests/unit/get_max_cpuid_from_sysfs_cxx +tests/unit/test_get_max_cpuid_from_mask +tests/unit/test_get_max_cpuid_from_mask_cxx +tests/unit/test_get_possible_cpus_array_len +tests/unit/test_get_possible_cpus_array_len_cxx tests/utils/libtap.a tests/utils/env.sh diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index ecb9fc6..233fdbd 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -7,8 +7,16 @@ LOG_DRIVER = env AM_TAP_AWK='$(AWK)' \ $(SHELL) $(top_srcdir)/tests/utils/tap-driver.sh noinst_PROGRAMS = \ + get_cpu_mask_from_sysfs \ + get_cpu_mask_from_sysfs_cxx \ + get_max_cpuid_from_sysfs \ + get_max_cpuid_from_sysfs_cxx \ test_arch \ test_arch_cxx \ + test_get_max_cpuid_from_mask \ + test_get_max_cpuid_from_mask_cxx \ + test_get_possible_cpus_array_len \ + test_get_possible_cpus_array_len_cxx \ test_uatomic \ test_uatomic_cxx \ test_urcu_multiflavor \ @@ -24,7 +32,37 @@ noinst_PROGRAMS = \ test_build_dynlink \ test_build_dynlink_cxx -TESTS = $(noinst_PROGRAMS) +dist_noinst_SCRIPTS = \ + test_get_cpu_mask_from_sysfs \ + test_get_cpu_mask_from_sysfs_cxx \ + test_get_max_cpuid_from_sysfs \ + test_get_max_cpuid_from_sysfs_cxx + +TESTS = \ + test_arch \ + test_arch_cxx \ + test_get_cpu_mask_from_sysfs \ + test_get_cpu_mask_from_sysfs_cxx \ + test_get_max_cpuid_from_mask \ + test_get_max_cpuid_from_mask_cxx \ + test_get_max_cpuid_from_sysfs \ + test_get_max_cpuid_from_sysfs_cxx \ + test_get_possible_cpus_array_len \ + test_get_possible_cpus_array_len_cxx \ + test_uatomic \ + test_uatomic_cxx \ + test_urcu_multiflavor \ + test_urcu_multiflavor_cxx \ + test_urcu_multiflavor_dynlink \ + test_urcu_multiflavor_dynlink_cxx \ + test_urcu_multiflavor_single_unit \ + test_urcu_multiflavor_single_unit_cxx \ + test_urcu_multiflavor_single_unit_dynlink \ + test_urcu_multiflavor_single_unit_dynlink_cxx \ + test_build \ + test_build_cxx \ + test_build_dynlink \ + test_build_dynlink_cxx noinst_HEADERS = test_urcu_multiflavor.h @@ -37,12 +75,29 @@ URCU_BP_LIB=$(top_builddir)/src/liburcu-bp.la URCU_CDS_LIB=$(top_builddir)/src/liburcu-cds.la TAP_LIB=$(top_builddir)/tests/utils/libtap.a +get_cpu_mask_from_sysfs_cxx_SOURCES = get_cpu_mask_from_sysfs_cxx.cpp +get_cpu_mask_from_sysfs_SOURCES = get_cpu_mask_from_sysfs.c +get_max_cpuid_from_sysfs_cxx_SOURCES = get_max_cpuid_from_sysfs_cxx.cpp +get_max_cpuid_from_sysfs_SOURCES = get_max_cpuid_from_sysfs.c + test_arch_SOURCES = test_arch.c test_arch_LDADD = $(URCU_COMMON_LIB) $(TAP_LIB) test_arch_cxx_SOURCES = test_arch_cxx.cpp test_arch_cxx_LDADD = $(URCU_COMMO_LIB) $(TAP_LIB) +test_get_max_cpuid_from_mask_SOURCES = test_get_max_cpuid_from_mask.c +test_get_max_cpuid_from_mask_LDADD = $(TAP_LIB) + +test_get_max_cpuid_from_mask_cxx_SOURCES = test_get_max_cpuid_from_mask_cxx.cpp +test_get_max_cpuid_from_mask_cxx_LDADD = $(TAP_LIB) + +test_get_possible_cpus_array_len_SOURCES = test_get_possible_cpus_array_len.c +test_get_possible_cpus_array_len_LDADD = $(TAP_LIB) + +test_get_possible_cpus_array_len_cxx_SOURCES = test_get_possible_cpus_array_len_cxx.cpp +test_get_possible_cpus_array_len_cxx_LDADD = $(TAP_LIB) + test_uatomic_SOURCES = test_uatomic.c test_uatomic_LDADD = $(URCU_COMMON_LIB) $(TAP_LIB) diff --git a/tests/unit/get_cpu_mask_from_sysfs.c b/tests/unit/get_cpu_mask_from_sysfs.c new file mode 100644 index 0000000..91f2ebf --- /dev/null +++ b/tests/unit/get_cpu_mask_from_sysfs.c @@ -0,0 +1,30 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Copyright (C) 2022 Michael Jeanson + */ + +#include +#include + +#include "compat-smp.h" + +int main(int argc, char *argv[]) +{ + int ret; + char buf[URCU_CPUMASK_SIZE]; + + if( argc < 2 ) { + fprintf(stderr, "Missing argument.\n"); + return EXIT_FAILURE; + } + + ret = get_cpu_mask_from_sysfs((char *) &buf, URCU_CPUMASK_SIZE, argv[1]); + + printf("%s", buf); + + if (ret >= 0) + return EXIT_SUCCESS; + else + return EXIT_FAILURE; +} diff --git a/tests/unit/get_cpu_mask_from_sysfs_cxx.cpp b/tests/unit/get_cpu_mask_from_sysfs_cxx.cpp new file mode 100644 index 0000000..1e120d4 --- /dev/null +++ b/tests/unit/get_cpu_mask_from_sysfs_cxx.cpp @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "get_cpu_mask_from_sysfs.c" diff --git a/tests/unit/get_max_cpuid_from_sysfs.c b/tests/unit/get_max_cpuid_from_sysfs.c new file mode 100644 index 0000000..a948a22 --- /dev/null +++ b/tests/unit/get_max_cpuid_from_sysfs.c @@ -0,0 +1,29 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Copyright (C) 2022 Michael Jeanson + */ + +#include +#include + +#include "compat-smp.h" + +int main(int argc, char *argv[]) +{ + int ret; + + if( argc < 2 ) { + fprintf(stderr, "Missing argument.\n"); + return EXIT_FAILURE; + } + + ret = _get_max_cpuid_from_sysfs(argv[1]); + + printf("%d\n", ret); + + if (ret >= 0) + return EXIT_SUCCESS; + else + return EXIT_FAILURE; +} diff --git a/tests/unit/get_max_cpuid_from_sysfs_cxx.cpp b/tests/unit/get_max_cpuid_from_sysfs_cxx.cpp new file mode 100644 index 0000000..951e9f7 --- /dev/null +++ b/tests/unit/get_max_cpuid_from_sysfs_cxx.cpp @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "get_max_cpuid_from_sysfs.c" diff --git a/tests/unit/test_get_cpu_mask_from_sysfs b/tests/unit/test_get_cpu_mask_from_sysfs new file mode 100755 index 0000000..fff561f --- /dev/null +++ b/tests/unit/test_get_cpu_mask_from_sysfs @@ -0,0 +1,51 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-or-later + +if [ "x${URCU_TESTS_SRCDIR:-}" != "x" ]; then + UTILSSH="$URCU_TESTS_SRCDIR/utils/utils.sh" +else + UTILSSH="$(dirname "$0")/../utils/utils.sh" +fi + +# shellcheck source=../utils/utils.sh +source "$UTILSSH" + +# shellcheck source=../../utils/tap.sh +source "$URCU_TESTS_SRCDIR/utils/tap.sh" + +CURDIR="${URCU_TESTS_BUILDDIR}/unit" + +NUM_TESTS=8 + +TESTFILE=$(mktemp) + +populate_testfile() { + local cpumask="$1" + + # shellcheck disable=SC2059 + printf "$cpumask" > "$TESTFILE" +} + +test_test_get_cpu_mask_from_sysfs() { + local cpumask="$1" + local result + + # Without '\n' + populate_testfile "$cpumask" + result=$("${CURDIR}/get_cpu_mask_from_sysfs" "$TESTFILE") + test "$cpumask" == "$result" + ok $? "test_get_cpu_mask_from_sysfs - without '\n' expected: '$cpumask', result: '$result'" + + # With '\n' + populate_testfile "$cpumask\n" + result=$("${CURDIR}/get_cpu_mask_from_sysfs" "$TESTFILE") + test "$cpumask" == "$result" + ok $? "test_get_cpu_mask_from_sysfs - with '\n' expected: '$cpumask', result: '$result'" +} + +plan_tests $NUM_TESTS + +test_test_get_cpu_mask_from_sysfs "" +test_test_get_cpu_mask_from_sysfs "0" +test_test_get_cpu_mask_from_sysfs "0-3" +test_test_get_cpu_mask_from_sysfs "0,3-7,9" diff --git a/tests/unit/test_get_cpu_mask_from_sysfs_cxx b/tests/unit/test_get_cpu_mask_from_sysfs_cxx new file mode 100755 index 0000000..6b80e53 --- /dev/null +++ b/tests/unit/test_get_cpu_mask_from_sysfs_cxx @@ -0,0 +1,51 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-or-later + +if [ "x${URCU_TESTS_SRCDIR:-}" != "x" ]; then + UTILSSH="$URCU_TESTS_SRCDIR/utils/utils.sh" +else + UTILSSH="$(dirname "$0")/../utils/utils.sh" +fi + +# shellcheck source=../utils/utils.sh +source "$UTILSSH" + +# shellcheck source=../../utils/tap.sh +source "$URCU_TESTS_SRCDIR/utils/tap.sh" + +CURDIR="${URCU_TESTS_BUILDDIR}/unit" + +NUM_TESTS=8 + +TESTFILE=$(mktemp) + +populate_testfile() { + local cpumask="$1" + + # shellcheck disable=SC2059 + printf "$cpumask" > "$TESTFILE" +} + +test_test_get_cpu_mask_from_sysfs_cxx() { + local cpumask="$1" + local result + + # Without '\n' + populate_testfile "$cpumask" + result=$("${CURDIR}/get_cpu_mask_from_sysfs_cxx" "$TESTFILE") + test "$cpumask" == "$result" + ok $? "test_get_cpu_mask_from_sysfs_cxx - without '\n' expected: '$cpumask', result: '$result'" + + # With '\n' + populate_testfile "$cpumask\n" + result=$("${CURDIR}/get_cpu_mask_from_sysfs_cxx" "$TESTFILE") + test "$cpumask" == "$result" + ok $? "test_get_cpu_mask_from_sysfs_cxx - with '\n' expected: '$cpumask', result: '$result'" +} + +plan_tests $NUM_TESTS + +test_test_get_cpu_mask_from_sysfs_cxx "" +test_test_get_cpu_mask_from_sysfs_cxx "0" +test_test_get_cpu_mask_from_sysfs_cxx "0-3" +test_test_get_cpu_mask_from_sysfs_cxx "0,3-7,9" diff --git a/tests/unit/test_get_max_cpuid_from_mask.c b/tests/unit/test_get_max_cpuid_from_mask.c new file mode 100644 index 0000000..435bb5c --- /dev/null +++ b/tests/unit/test_get_max_cpuid_from_mask.c @@ -0,0 +1,77 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Copyright (C) 2022 Michael Jeanson + */ + +#include + +#include "tap.h" + +#include "compat-smp.h" + +struct parse_test_data { + const char *buf; + int expected; +}; + +static struct parse_test_data parse_test_data[] = { + { "", -1 }, + { "abc", -1 }, + { ",,,", -1 }, + { "--", -1 }, + { ",", -1 }, + { "-", -1 }, + { "2147483647", -1 }, + { "18446744073709551615", -1 }, + { "0-2147483647", -1 }, + { "0-18446744073709551615", -1 }, + { "0", 0 }, + { "1", 1 }, + { "0-1", 1 }, + { "1-3", 3 }, + { "0,2", 2 }, + { "1,2", 2 }, + { "0,4-6,127", 127 }, + { "0-4095", 4095 }, + + { "\n", -1 }, + { "abc\n", -1 }, + { ",,,\n", -1 }, + { "--\n", -1 }, + { ",\n", -1 }, + { "-\n", -1 }, + { "2147483647\n", -1 }, + { "18446744073709551615\n", -1 }, + { "0-2147483647\n", -1 }, + { "0-18446744073709551615\n", -1 }, + { "0\n", 0 }, + { "1\n", 1 }, + { "0-1\n", 1 }, + { "1-3\n", 3 }, + { "0,2\n", 2 }, + { "1,2\n", 2 }, + { "0,4-6,127\n", 127 }, + { "0-4095\n", 4095 }, +}; + +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); + + diag("Testing smp helpers"); + + for (i = 0; i < parse_test_data_len; i++) { + ret = get_max_cpuid_from_mask(parse_test_data[i].buf, + strlen(parse_test_data[i].buf)); + ok(ret == parse_test_data[i].expected, + "get_max_cpuid_from_mask '%s', expected: '%d', result: '%d'", + parse_test_data[i].buf, parse_test_data[i].expected, ret); + } + + return exit_status(); +} diff --git a/tests/unit/test_get_max_cpuid_from_mask_cxx.cpp b/tests/unit/test_get_max_cpuid_from_mask_cxx.cpp new file mode 100644 index 0000000..5c58108 --- /dev/null +++ b/tests/unit/test_get_max_cpuid_from_mask_cxx.cpp @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "test_get_max_cpuid_from_mask.c" diff --git a/tests/unit/test_get_max_cpuid_from_sysfs b/tests/unit/test_get_max_cpuid_from_sysfs new file mode 100755 index 0000000..230a51c --- /dev/null +++ b/tests/unit/test_get_max_cpuid_from_sysfs @@ -0,0 +1,89 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-or-later + +if [ "x${URCU_TESTS_SRCDIR:-}" != "x" ]; then + UTILSSH="$URCU_TESTS_SRCDIR/utils/utils.sh" +else + UTILSSH="$(dirname "$0")/../utils/utils.sh" +fi + +# shellcheck source=../utils/utils.sh +source "$UTILSSH" + +# shellcheck source=../../utils/tap.sh +source "$URCU_TESTS_SRCDIR/utils/tap.sh" + +CURDIR="${URCU_TESTS_BUILDDIR}/unit" + +STD_OUTPUT="/dev/null" +STD_ERROR="/dev/null" + +NUM_TESTS=13 + +TESTDIR=$(mktemp -d) + +populate_testdir() { + local cpus=("$@") + + mkdir "$TESTDIR" + + for i in "${cpus[@]}"; do + mkdir "$TESTDIR/$i" + done +} + +test_get_max_cpuid_from_sysfs() { + local num_cpus=$1 + shift + local current_cpus=("$@") + local result + + populate_testdir "${current_cpus[@]}" >"$STD_OUTPUT" 2>"$STD_ERROR" + result=$("${CURDIR}/get_max_cpuid_from_sysfs" "$TESTDIR") + is "$result" "$num_cpus" "get_max_cpuid_from_sysfs - cpu set: '${current_cpus[*]}', expected: '$num_cpus', result: '$result'" + rm -rf "$TESTDIR" +} + +plan_tests $NUM_TESTS + +diag "get_max_cpuid_from_sysfs" + +test_data=(0 "cpu0") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(1 "cpu0" "cpu1") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(1 "cpu1" "cpu0") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(3 "cpu3") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(99 "cpu99") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(3 "cpu0" "cpu3") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(0 "cpu" "cpu0") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(5 "cpu" "cpu5") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + + +test_data=(-1 "toto") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(-1 "cpu") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(-1 "cpua" "cpud") +test_get_max_cpuid_from_sysfs "${test_data[@]}" + +test_data=(-1 "cpufreq" "cpuidle") +test_get_max_cpuid_from_sysfs "${test_data[@]}" diff --git a/tests/unit/test_get_max_cpuid_from_sysfs_cxx b/tests/unit/test_get_max_cpuid_from_sysfs_cxx new file mode 100755 index 0000000..886c065 --- /dev/null +++ b/tests/unit/test_get_max_cpuid_from_sysfs_cxx @@ -0,0 +1,89 @@ +#!/bin/bash +# SPDX-License-Identifier: GPL-2.0-or-later + +if [ "x${URCU_TESTS_SRCDIR:-}" != "x" ]; then + UTILSSH="$URCU_TESTS_SRCDIR/utils/utils.sh" +else + UTILSSH="$(dirname "$0")/../utils/utils.sh" +fi + +# shellcheck source=../utils/utils.sh +source "$UTILSSH" + +# shellcheck source=../../utils/tap.sh +source "$URCU_TESTS_SRCDIR/utils/tap.sh" + +CURDIR="${URCU_TESTS_BUILDDIR}/unit" + +STD_OUTPUT="/dev/null" +STD_ERROR="/dev/null" + +NUM_TESTS=13 + +TESTDIR=$(mktemp -d) + +populate_testdir() { + local cpus=("$@") + + mkdir "$TESTDIR" + + for i in "${cpus[@]}"; do + mkdir "$TESTDIR/$i" + done +} + +test_get_max_cpuid_from_sysfs_cxx() { + local num_cpus=$1 + shift + local current_cpus=("$@") + local result + + populate_testdir "${current_cpus[@]}" >"$STD_OUTPUT" 2>"$STD_ERROR" + result=$("${CURDIR}/get_max_cpuid_from_sysfs_cxx" "$TESTDIR") + is "$result" "$num_cpus" "get_max_cpuid_from_sysfs_cxx - cpu set: '${current_cpus[*]}', expected: '$num_cpus', result: '$result'" + rm -rf "$TESTDIR" +} + +plan_tests $NUM_TESTS + +diag "get_max_cpuid_from_sysfs_cxx" + +test_data=(0 "cpu0") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(1 "cpu0" "cpu1") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(1 "cpu1" "cpu0") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(3 "cpu3") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(99 "cpu99") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(3 "cpu0" "cpu3") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(3 "cpufreq" "cpuidle" "cpu0" "cpu1" "cpu2" "cpu3") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(0 "cpu" "cpu0") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(5 "cpu" "cpu5") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + + +test_data=(-1 "toto") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(-1 "cpu") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(-1 "cpua" "cpud") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" + +test_data=(-1 "cpufreq" "cpuidle") +test_get_max_cpuid_from_sysfs_cxx "${test_data[@]}" diff --git a/tests/unit/test_get_possible_cpus_array_len.c b/tests/unit/test_get_possible_cpus_array_len.c new file mode 100644 index 0000000..ab64d9d --- /dev/null +++ b/tests/unit/test_get_possible_cpus_array_len.c @@ -0,0 +1,26 @@ +/* + * SPDX-License-Identifier: GPL-2.0-or-later + * + * Copyright (C) 2022 Michael Jeanson + */ + +#include + +#include "tap.h" + +#include "compat-smp.h" + +int main(void) +{ + int ret; + + plan_tests(2); + + ret = get_possible_cpus_array_len(); + ok(ret > 0, "get_possible_cpus_array_len (%d > 0)", ret); + + ret = get_num_possible_cpus_fallback(); + ok(ret > 0, "get_num_possible_cpus_fallback (%d > 0)", ret); + + return exit_status(); +} diff --git a/tests/unit/test_get_possible_cpus_array_len_cxx.cpp b/tests/unit/test_get_possible_cpus_array_len_cxx.cpp new file mode 100644 index 0000000..e081cd4 --- /dev/null +++ b/tests/unit/test_get_possible_cpus_array_len_cxx.cpp @@ -0,0 +1,3 @@ +/* SPDX-License-Identifier: GPL-2.0-or-later */ + +#include "test_get_possible_cpus_array_len.c"