Add basic test coverage for files that don't require any config options:
* part of math.h (what seem to be the most commonly used macros)
* gcd.c
* lcm.c
* int_sqrt.c
* reciprocal_div.c
(Ignored int_pow.c since it's a simple textbook algorithm.)
These tests aren't particularly interesting, but they
* provide short and simple examples of parameterized tests
* provide a place to add tests for any new files in this dir
* are written so adding new test cases to cover edge cases should be easy
* looking at code coverage, we hit all the branches in the .c files
Changes since v4:
* add in test cases for some math.h macros (abs, round_up/round_down,
div_round_down/closest)
* use parameterized testing less to keep things terser
Changes since v3:
* fix `
checkpatch.pl --strict` warnings
* add test cases for gcd(0,0) and lcm(0,0)
* minor: don't test both gcd(a,b) and gcd(b,a) when a == b
Changes since v2: mv math_test.c => math_kunit.c
Changes since v1:
* Rebase and rewrite to use the new parameterized testing support.
* misc: fix overflow in literal and inline int_sqrt format string.
* related: commit 1f0e943df68a ("Documentation: kunit: provide guidance
for testing many inputs") was merged explaining the patterns shown here.
* there's an in-flight patch to update it for parameterized testing.
v1:
https://lore.kernel.org/lkml/20201019224556.3...@google.com/
---
lib/math/Kconfig | 5 +
lib/math/Makefile | 2 +
lib/math/math_kunit.c | 264 ++++++++++++++++++++++++++++++++++++++++++
3 files changed, 271 insertions(+)
index 000000000000..80a087a32884
--- /dev/null
+++ b/lib/math/math_kunit.c
@@ -0,0 +1,264 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Simple KUnit suite for math helper funcs that are always enabled.
+ *
+ * Copyright (C) 2020, Google LLC.
+ * Author: Daniel Latypov <
dlat...@google.com>
+ */
+
+#include <kunit/test.h>
+#include <linux/gcd.h>
+#include <linux/kernel.h>
+#include <linux/lcm.h>
+#include <linux/reciprocal_div.h>
+
+static void abs_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, abs('\0'), '\0');
+ KUNIT_EXPECT_EQ(test, abs('a'), 'a');
+ KUNIT_EXPECT_EQ(test, abs(-'a'), 'a');
+
+ /* The expression in the macro is actually promoted to an int. */
+ KUNIT_EXPECT_EQ(test, abs((short)0), 0);
+ KUNIT_EXPECT_EQ(test, abs((short)42), 42);
+ KUNIT_EXPECT_EQ(test, abs((short)-42), 42);
+
+ KUNIT_EXPECT_EQ(test, abs(0), 0);
+ KUNIT_EXPECT_EQ(test, abs(42), 42);
+ KUNIT_EXPECT_EQ(test, abs(-42), 42);
+
+ KUNIT_EXPECT_EQ(test, abs(0L), 0L);
+ KUNIT_EXPECT_EQ(test, abs(42L), 42L);
+ KUNIT_EXPECT_EQ(test, abs(-42L), 42L);
+
+ KUNIT_EXPECT_EQ(test, abs(0LL), 0LL);
+ KUNIT_EXPECT_EQ(test, abs(42LL), 42LL);
+ KUNIT_EXPECT_EQ(test, abs(-42LL), 42LL);
+
+ /* Unsigned types get casted to signed. */
+ KUNIT_EXPECT_EQ(test, abs(0ULL), 0LL);
+ KUNIT_EXPECT_EQ(test, abs(42ULL), 42LL);
+}
+
+static void int_sqrt_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, int_sqrt(0UL), 0UL);
+ KUNIT_EXPECT_EQ(test, int_sqrt(1UL), 1UL);
+ KUNIT_EXPECT_EQ(test, int_sqrt(4UL), 2UL);
+ KUNIT_EXPECT_EQ(test, int_sqrt(5UL), 2UL);
+ KUNIT_EXPECT_EQ(test, int_sqrt(8UL), 2UL);
+ KUNIT_EXPECT_EQ(test, int_sqrt(1UL << 30), 1UL << 15);
+}
+
+static void round_up_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, round_up(0, 1), 0);
+ KUNIT_EXPECT_EQ(test, round_up(1, 2), 2);
+ KUNIT_EXPECT_EQ(test, round_up(3, 2), 4);
+ KUNIT_EXPECT_EQ(test, round_up((1 << 30) - 1, 2), 1 << 30);
+ KUNIT_EXPECT_EQ(test, round_up((1 << 30) - 1, 1 << 29), 1 << 30);
+}
+
+static void round_down_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, round_down(0, 1), 0);
+ KUNIT_EXPECT_EQ(test, round_down(1, 2), 0);
+ KUNIT_EXPECT_EQ(test, round_down(3, 2), 2);
+ KUNIT_EXPECT_EQ(test, round_down((1 << 30) - 1, 2), (1 << 30) - 2);
+ KUNIT_EXPECT_EQ(test, round_down((1 << 30) - 1, 1 << 29), 1 << 29);
+}
+
+static void div_round_up_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(0, 1), 0);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(20, 10), 2);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(21, 10), 3);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(21, 20), 2);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_UP(21, 99), 1);
+}
+
+static void div_round_closest_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_CLOSEST(0, 1), 0);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_CLOSEST(20, 10), 2);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_CLOSEST(21, 10), 2);
+ KUNIT_EXPECT_EQ(test, DIV_ROUND_CLOSEST(25, 10), 3);
+}
+struct u32_test_case {
+ u32 a, b;
+ u32 result;
+};
+
+static struct u32_test_case reciprocal_div_cases[] = {
+ {
+ .a = 0, .b = 1,
+ .result = 0,
+ },
+ {
+ .a = 42, .b = 20,
+ .result = 2,
+ },
+ {
+ .a = 42, .b = 9999,
+ .result = 0,
+ },
+ {
+ .a = (1 << 16), .b = (1 << 14),
+ .result = 1 << 2,
+ },
+};
+
+KUNIT_ARRAY_PARAM(reciprocal_div, reciprocal_div_cases, NULL);
+
+static void reciprocal_div_test(struct kunit *test)
+{
+ const struct u32_test_case *test_param = test->param_value;
+ struct reciprocal_value rv = reciprocal_value(test_param->b);
+
+ KUNIT_EXPECT_EQ_MSG(test, test_param->result,
+ reciprocal_divide(test_param->a, rv),
+ "reciprocal_divide(%u, %u)",
+ test_param->a, test_param->b);
+}
+
+static void reciprocal_scale_test(struct kunit *test)
+{
+ KUNIT_EXPECT_EQ(test, reciprocal_scale(0u, 100), 0u);
+ KUNIT_EXPECT_EQ(test, reciprocal_scale(1u, 100), 0u);
+ KUNIT_EXPECT_EQ(test, reciprocal_scale(1u << 4, 1 << 28), 1u);
+ KUNIT_EXPECT_EQ(test, reciprocal_scale(1u << 16, 1 << 28), 1u << 12);
+ KUNIT_EXPECT_EQ(test, reciprocal_scale(~0u, 1 << 28), (1u << 28) - 1);
+}
+
+static struct kunit_case math_test_cases[] = {
+ KUNIT_CASE(abs_test),
+ KUNIT_CASE(int_sqrt_test),
+ KUNIT_CASE(round_up_test),
+ KUNIT_CASE(round_down_test),
+ KUNIT_CASE(div_round_up_test),
+ KUNIT_CASE(div_round_closest_test),
+ KUNIT_CASE_PARAM(gcd_test, gcd_gen_params),
+ KUNIT_CASE_PARAM(lcm_test, lcm_gen_params),
+ KUNIT_CASE_PARAM(reciprocal_div_test, reciprocal_div_gen_params),
+ KUNIT_CASE(reciprocal_scale_test),