lib: add basic KUnit test for lib/math
PATCH v4 1/1 lib: add basic KUnit test for lib/math
Add basic test coverage for files that don't require any config options:
* 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
---
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/[email protected]/
--- lib/math/Kconfig | 5 + lib/math/Makefile | 2 + lib/math/math_kunit.c | 214 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 221 insertions(+) create mode 100644 lib/math/math_kunit.c
lib/math/Kconfig +5 -0
@@ -15,3 +15,8 @@ config PRIME_NUMBERS config RATIONAL bool + +config MATH_KUNIT_TEST + tristate "KUnit test for lib/math" if !KUNIT_ALL_TESTS + default KUNIT_ALL_TESTS + depends on KUNIT
lib/math/Makefile +2 -0
@@ -4,3 +4,5 @@ obj-y += div64.o gcd.o lcm.o int_pow.o int_sqrt.o reciprocal_div.o obj-$(CONFIG_CORDIC) += cordic.o obj-$(CONFIG_PRIME_NUMBERS) += prime_numbers.o obj-$(CONFIG_RATIONAL) += rational.o + +obj-$(CONFIG_MATH_KUNIT_TEST) += math_kunit.o
lib/math/math_kunit.c new file +214 -0
214 lines not on this page · raw message
base-commit: 4fa56ad0d12e24df768c98bffe9039f915d1bc02
Signature · 1 line
2.31.1.295.g9ea45b61b8-goog
Quoted 6 lines · Daniel Latypov
Add basic test coverage for files that don't require any config options:
*gcd.c
*lcm.c
*int_sqrt.c
*reciprocal_div.c
(Ignoredint_pow.csince it's a simple textbook algorithm.)
What about adding math.h test cases?
We have some macros there and it might be a good idea to test them, for example that round_up() and roundup() produces the same output for the same (power of two divisor) input.
Quoted 4 lines
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
Yes, that's why I think macros also can be a good example how to test *macro*.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Quoted 9 lines · Andy Shevchenko
Quoted 6 lines · Daniel Latypov
Add basic test coverage for files that don't require any config options:
*gcd.c
*lcm.c
*int_sqrt.c
*reciprocal_div.c
(Ignoredint_pow.csince it's a simple textbook algorithm.)
Quoted 5 lines
What about adding
math.htest cases?We have some macros there and it might be a good idea to test them, for example that
round_up()androundup()produces the same output for the same (power of two divisor) input.
I completely overlooked the macros in math.h, sounds like a good idea.
Grepping around, seems like abs() and DIV_ROUND_UP/CLOSEST() are among the more popular macros:
$ ag -s '\bDIV_ROUND_UP\(' | wc -l
2946
$ ag -s '\babs\(' | wc -l
923
$ ag -s '\bDIV_ROUND_CLOSEST\(' | wc -l
864
$ ag -s '\bround_up\(' | wc -l
727
$ ag -s '\broundup\(' | wc -l
620
$ ag -s '\bround_down\(' | wc -l
371
$ ag -s 'rounddown\(' | wc -l
131Quoted 7 lines
Quoted 5 lines
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 easyYes, that's why I think macros also can be a good example how to test *macro*.
Yeah, there's more to cover there since they have a range of types they can work on.
On another note, the parameterized test arrays all use unsigned long, so abs() sticks out even more.
I'm thinking of something like
static void test_abs(struct kunit *test)
{
KUNIT_EXPECT_EQ(test, abs('a'), 'a');
KUNIT_EXPECT_EQ(test, abs(-'a'), 'a');
...
}
and then maybe use parameters for the other macros but also throw in an additional test case like
static void test_div_round_up_diff_types(struct kunit *test) {
KUNIT_EXPECT_EQ(test, DIV_ROUND_UP((char) 42, (char) 10), (char) 4); KUNIT_EXPECT_EQ(test, DIV_ROUND_UP((int) 42, (int) 10), (int) 4); KUNIT_EXPECT_EQ(test, DIV_ROUND_UP((long) 42, (long) 10), (long) 4);
...
}
Quoted 10 lines
--
With Best Regards,
Andy Shevchenko--
You received this message because you are subscribed to the Google Groups "KUnit Development" group.
To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/kunit-dev/YHBzA7SwH194ywRv%40smile.fi.intel.com.