Linux kernel mirror (for testing)
git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel
os
linux
fork
Configure Feed
Select the types of activity you want to include in your feed.
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 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
19static 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
39static void kunit_suite_exit(struct kunit_suite *suite)
40{
41 with_primes(suite, dump_primes);
42}
43
44static struct kunit_case prime_numbers_cases[] = {
45 KUNIT_CASE(prime_numbers_test),
46 {},
47};
48
49static 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
55kunit_test_suite(prime_numbers_suite);
56
57MODULE_AUTHOR("Intel Corporation");
58MODULE_DESCRIPTION("Prime number library");
59MODULE_LICENSE("GPL");