projects/linux·blob master · refs

57 lines · 1.3 KiB

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
9static void dump_primes(void *ctx, const struct primes *p)
10{
11 struct kunit_suite *suite = ctx;
12
13 kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %*pbl",
14 p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], (int)p->sz, p->primes);
15}
16
17static void prime_numbers_test(struct kunit *test)
18{
19 const unsigned long max = 65536;
20 unsigned long x, last, next;
21
22 for (last = 0, x = 2; x < max; x++) {
23 const bool slow = slow_is_prime_number(x);
24 const bool fast = is_prime_number(x);
25
26 KUNIT_ASSERT_EQ_MSG(test, slow, fast, "is-prime(%lu)", x);
27
28 if (!slow)
29 continue;
30
31 next = next_prime_number(last);
32 KUNIT_ASSERT_EQ_MSG(test, next, x, "next-prime(%lu)", last);
33 last = next;
34 }
35}
36
37static void kunit_suite_exit(struct kunit_suite *suite)
38{
39 with_primes(suite, dump_primes);
40}
41
42static struct kunit_case prime_numbers_cases[] = {
43 KUNIT_CASE(prime_numbers_test),
44 {},
45};
46
47static struct kunit_suite prime_numbers_suite = {
48 .name = "math-prime_numbers",
49 .suite_exit = kunit_suite_exit,
50 .test_cases = prime_numbers_cases,
51};
52
53kunit_test_suite(prime_numbers_suite);
54
55MODULE_AUTHOR("Intel Corporation");
56MODULE_DESCRIPTION("Prime number library");
57MODULE_LICENSE("GPL");