File: prime_numbers_kunit.c

package info (click to toggle)
linux 6.17.10-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,734,900 kB
  • sloc: ansic: 26,684,436; asm: 271,195; sh: 147,406; python: 75,980; makefile: 57,306; perl: 36,943; xml: 19,562; cpp: 5,899; yacc: 4,909; lex: 2,943; awk: 1,556; sed: 29; ruby: 25
file content (59 lines) | stat: -rw-r--r-- 1,420 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
// SPDX-License-Identifier: GPL-2.0-only

#include <kunit/test.h>
#include <linux/module.h>
#include <linux/prime_numbers.h>

#include "../prime_numbers_private.h"

static void dump_primes(void *ctx, const struct primes *p)
{
	static char buf[PAGE_SIZE];
	struct kunit_suite *suite = ctx;

	bitmap_print_to_pagebuf(true, buf, p->primes, p->sz);
	kunit_info(suite, "primes.{last=%lu, .sz=%lu, .primes[]=...x%lx} = %s",
		   p->last, p->sz, p->primes[BITS_TO_LONGS(p->sz) - 1], buf);
}

static void prime_numbers_test(struct kunit *test)
{
	const unsigned long max = 65536;
	unsigned long x, last, next;

	for (last = 0, x = 2; x < max; x++) {
		const bool slow = slow_is_prime_number(x);
		const bool fast = is_prime_number(x);

		KUNIT_ASSERT_EQ_MSG(test, slow, fast, "is-prime(%lu)", x);

		if (!slow)
			continue;

		next = next_prime_number(last);
		KUNIT_ASSERT_EQ_MSG(test, next, x, "next-prime(%lu)", last);
		last = next;
	}
}

static void kunit_suite_exit(struct kunit_suite *suite)
{
	with_primes(suite, dump_primes);
}

static struct kunit_case prime_numbers_cases[] = {
	KUNIT_CASE(prime_numbers_test),
	{},
};

static struct kunit_suite prime_numbers_suite = {
	.name = "math-prime_numbers",
	.suite_exit = kunit_suite_exit,
	.test_cases = prime_numbers_cases,
};

kunit_test_suite(prime_numbers_suite);

MODULE_AUTHOR("Intel Corporation");
MODULE_DESCRIPTION("Prime number library");
MODULE_LICENSE("GPL");