From: Luis Felipe Hernandez <luis.hernandez093@gmail.com>
To: brendan.higgins@linux.dev, davidgow@google.com, rmoar@google.com
Cc: Luis Felipe Hernandez <luis.hernandez093@gmail.com>,
linux-kselftest@vger.kernel.org, kunit-dev@googlegroups.com,
skhan@linuxfoundation.org,
linux-kernel-mentees@lists.linuxfoundation.org
Subject: [PATCH] lib/math: Add int_pow test suite
Date: Wed, 14 Aug 2024 22:07:09 -0400 [thread overview]
Message-ID: <20240815020711.110640-1-luis.hernandez093@gmail.com> (raw)
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
next reply other threads:[~2024-08-15 2:07 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-08-15 2:07 Luis Felipe Hernandez [this message]
2024-08-16 6:07 ` [PATCH] lib/math: Add int_pow test suite David Gow
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240815020711.110640-1-luis.hernandez093@gmail.com \
--to=luis.hernandez093@gmail.com \
--cc=brendan.higgins@linux.dev \
--cc=davidgow@google.com \
--cc=kunit-dev@googlegroups.com \
--cc=linux-kernel-mentees@lists.linuxfoundation.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=rmoar@google.com \
--cc=skhan@linuxfoundation.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox