1 // SPDX-License-Identifier: GPL-2.0-only 2 3 #include <kunit/test.h> 4 #include <linux/module.h> 5 #include <linux/prime_numbers.h> 6 7 #include "../prime_numbers_private.h" 8 9 static void dump_primes(void *ctx, const struct primes *p) 10 { 11 static char buf[PAGE_SIZE]; 12 struct kunit_suite *suite = ctx; 13 14 bitmap_print_to_pagebuf(true, buf, p->primes, p->sz); 15 kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s", 16 p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], buf); 17 } 18 19 static void prime_numbers_test(struct kunit *test) 20 { 21 const unsigned long max = 65536; 22 unsigned long x, last, next; 23 24 for (last = 0, x = 2; x < max; x++) { 25 const bool slow = slow_is_prime_number(x); 26 const bool fast = is_prime_number(x); 27 28 KUNIT_ASSERT_EQ_MSG(test, slow, fast, "is-prime(%lu)", x); 29 30 if (!slow) 31 continue; 32 33 next = next_prime_number(last); 34 KUNIT_ASSERT_EQ_MSG(test, next, x, "next-prime(%lu)", last); 35 last = next; 36 } 37 } 38 39 static void kunit_suite_exit(struct kunit_suite *suite) 40 { 41 with_primes(suite, dump_primes); 42 } 43 44 static struct kunit_case prime_numbers_cases[] = { 45 KUNIT_CASE(prime_numbers_test), 46 {}, 47 }; 48 49 static struct kunit_suite prime_numbers_suite = { 50 .name = "math-prime_numbers", 51 .suite_exit = kunit_suite_exit, 52 .test_cases = prime_numbers_cases, 53 }; 54 55 kunit_test_suite(prime_numbers_suite); 56 57 MODULE_AUTHOR("Intel Corporation"); 58 MODULE_DESCRIPTION("Prime number library"); 59 MODULE_LICENSE("GPL"); 60