lib/test_cpumask: follow KUnit style guidelines
authorSander Vanheule <sander@svanheule.net>
Tue, 23 Aug 2022 06:12:21 +0000 (08:12 +0200)
committerYury Norov <yury.norov@gmail.com>
Wed, 24 Aug 2022 15:35:42 +0000 (08:35 -0700)
The cpumask test suite doesn't follow the KUnit style guidelines, as
laid out in Documentation/dev-tools/kunit/style.rst.  The file is
renamed to lib/cpumask_kunit.c to clearly distinguish it from other,
non-KUnit, tests.

Link: https://lore.kernel.org/lkml/346cb279-8e75-24b0-7d12-9803f2b41c73@riseup.net/
Suggested-by: Maíra Canal <mairacanal@riseup.net>
Signed-off-by: Sander Vanheule <sander@svanheule.net>
Reviewed-by: Maíra Canal <mairacanal@riseup.net>
Reviewed-by: David Gow <davidgow@google.com>
Acked-by: Yury Norov <yury.norov@gmail.com>
Signed-off-by: Yury Norov <yury.norov@gmail.com>
lib/Kconfig.debug
lib/Makefile
lib/cpumask_kunit.c [new file with mode: 0644]
lib/test_cpumask.c [deleted file]

index 072e4b289c13e2b312000092b59e12868c7cf301..bcbe60d6c80c1a7cfd3bf4f595b26b14c5579883 100644 (file)
@@ -2029,13 +2029,16 @@ config LKDTM
        Documentation on how to use the module can be found in
        Documentation/fault-injection/provoke-crashes.rst
 
-config TEST_CPUMASK
-       tristate "cpumask tests" if !KUNIT_ALL_TESTS
+config CPUMASK_KUNIT_TEST
+       tristate "KUnit test for cpumask" if !KUNIT_ALL_TESTS
        depends on KUNIT
        default KUNIT_ALL_TESTS
        help
          Enable to turn on cpumask tests, running at boot or module load time.
 
+         For more information on KUnit and unit tests in general, please refer
+         to the KUnit documentation in Documentation/dev-tools/kunit/.
+
          If unsure, say N.
 
 config TEST_LIST_SORT
index 5927d7fa08063dec069e64a7e469e7fc9dcf333c..ffabc30a27d4e3bf9aef1389d576321bec95d743 100644 (file)
@@ -60,6 +60,7 @@ obj-$(CONFIG_TEST_BPF) += test_bpf.o
 obj-$(CONFIG_TEST_FIRMWARE) += test_firmware.o
 obj-$(CONFIG_TEST_BITOPS) += test_bitops.o
 CFLAGS_test_bitops.o += -Werror
+obj-$(CONFIG_CPUMASK_KUNIT_TEST) += cpumask_kunit.o
 obj-$(CONFIG_TEST_SYSCTL) += test_sysctl.o
 obj-$(CONFIG_TEST_SIPHASH) += test_siphash.o
 obj-$(CONFIG_HASH_KUNIT_TEST) += test_hash.o
@@ -100,7 +101,6 @@ obj-$(CONFIG_TEST_HMM) += test_hmm.o
 obj-$(CONFIG_TEST_FREE_PAGES) += test_free_pages.o
 obj-$(CONFIG_KPROBES_SANITY_TEST) += test_kprobes.o
 obj-$(CONFIG_TEST_REF_TRACKER) += test_ref_tracker.o
-obj-$(CONFIG_TEST_CPUMASK) += test_cpumask.o
 CFLAGS_test_fprobe.o += $(CC_FLAGS_FTRACE)
 obj-$(CONFIG_FPROBE_SANITY_TEST) += test_fprobe.o
 #
diff --git a/lib/cpumask_kunit.c b/lib/cpumask_kunit.c
new file mode 100644 (file)
index 0000000..4d35361
--- /dev/null
@@ -0,0 +1,137 @@
+// SPDX-License-Identifier: GPL-2.0-only
+/*
+ * KUnit tests for cpumask.
+ *
+ * Author: Sander Vanheule <sander@svanheule.net>
+ */
+
+#include <kunit/test.h>
+#include <linux/cpu.h>
+#include <linux/cpumask.h>
+
+#define EXPECT_FOR_EACH_CPU_EQ(test, mask)                     \
+       do {                                                    \
+               const cpumask_t *m = (mask);                    \
+               int mask_weight = cpumask_weight(m);            \
+               int cpu, iter = 0;                              \
+               for_each_cpu(cpu, m)                            \
+                       iter++;                                 \
+               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
+       } while (0)
+
+#define EXPECT_FOR_EACH_CPU_NOT_EQ(test, mask)                                 \
+       do {                                                                    \
+               const cpumask_t *m = (mask);                                    \
+               int mask_weight = cpumask_weight(m);                            \
+               int cpu, iter = 0;                                              \
+               for_each_cpu_not(cpu, m)                                        \
+                       iter++;                                                 \
+               KUNIT_EXPECT_EQ((test), nr_cpu_ids - mask_weight, iter);        \
+       } while (0)
+
+#define EXPECT_FOR_EACH_CPU_WRAP_EQ(test, mask)                        \
+       do {                                                    \
+               const cpumask_t *m = (mask);                    \
+               int mask_weight = cpumask_weight(m);            \
+               int cpu, iter = 0;                              \
+               for_each_cpu_wrap(cpu, m, nr_cpu_ids / 2)       \
+                       iter++;                                 \
+               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
+       } while (0)
+
+#define EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, name)             \
+       do {                                                    \
+               int mask_weight = num_##name##_cpus();          \
+               int cpu, iter = 0;                              \
+               for_each_##name##_cpu(cpu)                      \
+                       iter++;                                 \
+               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
+       } while (0)
+
+static cpumask_t mask_empty;
+static cpumask_t mask_all;
+
+static void test_cpumask_weight(struct kunit *test)
+{
+       KUNIT_EXPECT_TRUE(test, cpumask_empty(&mask_empty));
+       KUNIT_EXPECT_TRUE(test, cpumask_full(&mask_all));
+
+       KUNIT_EXPECT_EQ(test, 0, cpumask_weight(&mask_empty));
+       KUNIT_EXPECT_EQ(test, nr_cpu_ids, cpumask_weight(cpu_possible_mask));
+       KUNIT_EXPECT_EQ(test, nr_cpumask_bits, cpumask_weight(&mask_all));
+}
+
+static void test_cpumask_first(struct kunit *test)
+{
+       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first(&mask_empty));
+       KUNIT_EXPECT_EQ(test, 0, cpumask_first(cpu_possible_mask));
+
+       KUNIT_EXPECT_EQ(test, 0, cpumask_first_zero(&mask_empty));
+       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first_zero(cpu_possible_mask));
+}
+
+static void test_cpumask_last(struct kunit *test)
+{
+       KUNIT_EXPECT_LE(test, nr_cpumask_bits, cpumask_last(&mask_empty));
+       KUNIT_EXPECT_EQ(test, nr_cpu_ids - 1, cpumask_last(cpu_possible_mask));
+}
+
+static void test_cpumask_next(struct kunit *test)
+{
+       KUNIT_EXPECT_EQ(test, 0, cpumask_next_zero(-1, &mask_empty));
+       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next_zero(-1, cpu_possible_mask));
+
+       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next(-1, &mask_empty));
+       KUNIT_EXPECT_EQ(test, 0, cpumask_next(-1, cpu_possible_mask));
+}
+
+static void test_cpumask_iterators(struct kunit *test)
+{
+       EXPECT_FOR_EACH_CPU_EQ(test, &mask_empty);
+       EXPECT_FOR_EACH_CPU_NOT_EQ(test, &mask_empty);
+       EXPECT_FOR_EACH_CPU_WRAP_EQ(test, &mask_empty);
+
+       EXPECT_FOR_EACH_CPU_EQ(test, cpu_possible_mask);
+       EXPECT_FOR_EACH_CPU_NOT_EQ(test, cpu_possible_mask);
+       EXPECT_FOR_EACH_CPU_WRAP_EQ(test, cpu_possible_mask);
+}
+
+static void test_cpumask_iterators_builtin(struct kunit *test)
+{
+       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, possible);
+
+       /* Ensure the dynamic masks are stable while running the tests */
+       cpu_hotplug_disable();
+
+       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, online);
+       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, present);
+
+       cpu_hotplug_enable();
+}
+
+static int test_cpumask_init(struct kunit *test)
+{
+       cpumask_clear(&mask_empty);
+       cpumask_setall(&mask_all);
+
+       return 0;
+}
+
+static struct kunit_case test_cpumask_cases[] = {
+       KUNIT_CASE(test_cpumask_weight),
+       KUNIT_CASE(test_cpumask_first),
+       KUNIT_CASE(test_cpumask_last),
+       KUNIT_CASE(test_cpumask_next),
+       KUNIT_CASE(test_cpumask_iterators),
+       KUNIT_CASE(test_cpumask_iterators_builtin),
+       {}
+};
+
+static struct kunit_suite test_cpumask_suite = {
+       .name = "cpumask",
+       .init = test_cpumask_init,
+       .test_cases = test_cpumask_cases,
+};
+kunit_test_suite(test_cpumask_suite);
+
+MODULE_LICENSE("GPL");
diff --git a/lib/test_cpumask.c b/lib/test_cpumask.c
deleted file mode 100644 (file)
index 4d35361..0000000
+++ /dev/null
@@ -1,137 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0-only
-/*
- * KUnit tests for cpumask.
- *
- * Author: Sander Vanheule <sander@svanheule.net>
- */
-
-#include <kunit/test.h>
-#include <linux/cpu.h>
-#include <linux/cpumask.h>
-
-#define EXPECT_FOR_EACH_CPU_EQ(test, mask)                     \
-       do {                                                    \
-               const cpumask_t *m = (mask);                    \
-               int mask_weight = cpumask_weight(m);            \
-               int cpu, iter = 0;                              \
-               for_each_cpu(cpu, m)                            \
-                       iter++;                                 \
-               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
-       } while (0)
-
-#define EXPECT_FOR_EACH_CPU_NOT_EQ(test, mask)                                 \
-       do {                                                                    \
-               const cpumask_t *m = (mask);                                    \
-               int mask_weight = cpumask_weight(m);                            \
-               int cpu, iter = 0;                                              \
-               for_each_cpu_not(cpu, m)                                        \
-                       iter++;                                                 \
-               KUNIT_EXPECT_EQ((test), nr_cpu_ids - mask_weight, iter);        \
-       } while (0)
-
-#define EXPECT_FOR_EACH_CPU_WRAP_EQ(test, mask)                        \
-       do {                                                    \
-               const cpumask_t *m = (mask);                    \
-               int mask_weight = cpumask_weight(m);            \
-               int cpu, iter = 0;                              \
-               for_each_cpu_wrap(cpu, m, nr_cpu_ids / 2)       \
-                       iter++;                                 \
-               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
-       } while (0)
-
-#define EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, name)             \
-       do {                                                    \
-               int mask_weight = num_##name##_cpus();          \
-               int cpu, iter = 0;                              \
-               for_each_##name##_cpu(cpu)                      \
-                       iter++;                                 \
-               KUNIT_EXPECT_EQ((test), mask_weight, iter);     \
-       } while (0)
-
-static cpumask_t mask_empty;
-static cpumask_t mask_all;
-
-static void test_cpumask_weight(struct kunit *test)
-{
-       KUNIT_EXPECT_TRUE(test, cpumask_empty(&mask_empty));
-       KUNIT_EXPECT_TRUE(test, cpumask_full(&mask_all));
-
-       KUNIT_EXPECT_EQ(test, 0, cpumask_weight(&mask_empty));
-       KUNIT_EXPECT_EQ(test, nr_cpu_ids, cpumask_weight(cpu_possible_mask));
-       KUNIT_EXPECT_EQ(test, nr_cpumask_bits, cpumask_weight(&mask_all));
-}
-
-static void test_cpumask_first(struct kunit *test)
-{
-       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first(&mask_empty));
-       KUNIT_EXPECT_EQ(test, 0, cpumask_first(cpu_possible_mask));
-
-       KUNIT_EXPECT_EQ(test, 0, cpumask_first_zero(&mask_empty));
-       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_first_zero(cpu_possible_mask));
-}
-
-static void test_cpumask_last(struct kunit *test)
-{
-       KUNIT_EXPECT_LE(test, nr_cpumask_bits, cpumask_last(&mask_empty));
-       KUNIT_EXPECT_EQ(test, nr_cpu_ids - 1, cpumask_last(cpu_possible_mask));
-}
-
-static void test_cpumask_next(struct kunit *test)
-{
-       KUNIT_EXPECT_EQ(test, 0, cpumask_next_zero(-1, &mask_empty));
-       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next_zero(-1, cpu_possible_mask));
-
-       KUNIT_EXPECT_LE(test, nr_cpu_ids, cpumask_next(-1, &mask_empty));
-       KUNIT_EXPECT_EQ(test, 0, cpumask_next(-1, cpu_possible_mask));
-}
-
-static void test_cpumask_iterators(struct kunit *test)
-{
-       EXPECT_FOR_EACH_CPU_EQ(test, &mask_empty);
-       EXPECT_FOR_EACH_CPU_NOT_EQ(test, &mask_empty);
-       EXPECT_FOR_EACH_CPU_WRAP_EQ(test, &mask_empty);
-
-       EXPECT_FOR_EACH_CPU_EQ(test, cpu_possible_mask);
-       EXPECT_FOR_EACH_CPU_NOT_EQ(test, cpu_possible_mask);
-       EXPECT_FOR_EACH_CPU_WRAP_EQ(test, cpu_possible_mask);
-}
-
-static void test_cpumask_iterators_builtin(struct kunit *test)
-{
-       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, possible);
-
-       /* Ensure the dynamic masks are stable while running the tests */
-       cpu_hotplug_disable();
-
-       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, online);
-       EXPECT_FOR_EACH_CPU_BUILTIN_EQ(test, present);
-
-       cpu_hotplug_enable();
-}
-
-static int test_cpumask_init(struct kunit *test)
-{
-       cpumask_clear(&mask_empty);
-       cpumask_setall(&mask_all);
-
-       return 0;
-}
-
-static struct kunit_case test_cpumask_cases[] = {
-       KUNIT_CASE(test_cpumask_weight),
-       KUNIT_CASE(test_cpumask_first),
-       KUNIT_CASE(test_cpumask_last),
-       KUNIT_CASE(test_cpumask_next),
-       KUNIT_CASE(test_cpumask_iterators),
-       KUNIT_CASE(test_cpumask_iterators_builtin),
-       {}
-};
-
-static struct kunit_suite test_cpumask_suite = {
-       .name = "cpumask",
-       .init = test_cpumask_init,
-       .test_cases = test_cpumask_cases,
-};
-kunit_test_suite(test_cpumask_suite);
-
-MODULE_LICENSE("GPL");