Linux Kernel Selftest development
 help / color / mirror / Atom feed
* [PATCH] lib/math: Add int_pow test suite
@ 2024-08-15  2:07 Luis Felipe Hernandez
  2024-08-16  6:07 ` David Gow
  0 siblings, 1 reply; 2+ messages in thread
From: Luis Felipe Hernandez @ 2024-08-15  2:07 UTC (permalink / raw)
  To: brendan.higgins, davidgow, rmoar
  Cc: Luis Felipe Hernandez, linux-kselftest, kunit-dev, skhan,
	linux-kernel-mentees

Adds test suite for integer based power function.

Signed-off-by: Luis Felipe Hernandez <luis.hernandez093@gmail.com>
---
 lib/math/Makefile       |  1 +
 lib/math/test_int_pow.c | 70 +++++++++++++++++++++++++++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 lib/math/test_int_pow.c

diff --git a/lib/math/Makefile b/lib/math/Makefile
index 91fcdb0c9efe..ba564bf4fb00 100644
--- a/lib/math/Makefile
+++ b/lib/math/Makefile
@@ -6,4 +6,5 @@ obj-$(CONFIG_PRIME_NUMBERS)	+= prime_numbers.o
 obj-$(CONFIG_RATIONAL)		+= rational.o
 
 obj-$(CONFIG_TEST_DIV64)	+= test_div64.o
+obj-$(CONFIG_TEST_INT_POW)	+= test_int_pow.o
 obj-$(CONFIG_RATIONAL_KUNIT_TEST) += rational-test.o
diff --git a/lib/math/test_int_pow.c b/lib/math/test_int_pow.c
new file mode 100644
index 000000000000..ecabe71d01cc
--- /dev/null
+++ b/lib/math/test_int_pow.c
@@ -0,0 +1,70 @@
+// SPDX-License-Identifier: GPL-2.0
+
+#include <kunit/test.h>
+#include <kunit/static_stub.h>
+
+#include <linux/math.h>
+
+
+static void test_pow_0(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1, int_pow(64, 0));
+}
+
+static void test_pow_1(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 64, int_pow(64, 1));
+}
+
+static void test_base_0(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 0, int_pow(0, 5));
+}
+
+static void test_base_1(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1, int_pow(1, 100));
+	KUNIT_EXPECT_EQ(test, 1, int_pow(1, 0));
+}
+
+static void test_base_0_pow_0(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 1, int_pow(0, 0));
+}
+
+static void test_base_2_pow_2(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, 4, int_pow(2, 2));
+}
+
+static void test_max_base(struct kunit *test)
+{
+	KUNIT_EXPECT_EQ(test, U64_MAX, int_pow(U64_MAX, 1));
+}
+
+static void test_large_result(struct kunit *test)
+{
+	UNIT_EXPECT_EQ(test, 1152921504606846976, int_pow(2, 60));
+}
+
+static struct kunit_case math_int_pow_test_cases[] = {
+	KUNIT_CASE(test_pow_0),
+	KUNIT_CASE(test_pow_1),
+	KUNIT_CASE(test_base_0),
+	KUNIT_CASE(test_base_1),
+	KUNIT_CASE(test_base_0_pow_0),
+	KUNIT_CASE(test_base_2_pow_2),
+	KUNIT_CASE(test_max_base),
+	KUNIT_CASE(test_large_result),
+	{}
+};
+
+static struct kunit_suite int_pow_test_suite = {
+	.name = "lib-math-int_pow",
+	.test_cases = math_int_pow_test_cases,
+};
+
+kunit_test_suites(&int_pow_test_suite);
+
+MODULE_DESCRIPTION("math.int_pow KUnit test suite");
+MODULE_LICENSE("GPL v2");
-- 
2.46.0


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-08-16  6:07 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-15  2:07 [PATCH] lib/math: Add int_pow test suite Luis Felipe Hernandez
2024-08-16  6:07 ` David Gow

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox