aboutsummaryrefslogtreecommitdiff
path: root/test/016_popcount_u32/popcount_u32_tests.c
blob: ff60c68c02f39d348009c80a08a158dd008e4c00 (plain)
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
#include "../test.h"

int popcount_u32(uint32_t x);

int
popcount_u32_simple(uint32_t x)
{
	int ret;

	for (ret = 0; x != 0; x >>= 1)
		ret += x & 1;

	return ret;
}

bool
correct(uint32_t x)
{
	int expected = popcount_u32_simple(x);
	int actual = popcount_u32(x);
	if (actual != expected) {
		printf("Error at %" PRIu32 ": expected %d bits, found %d\n",
		    x, expected, actual);
		return false;
	}
	return true;
}

void run(void) {
	uint32_t i;

	/* Test all numbers up to 2^16, and other ranges of 2^16 numbers */
	for (i = 0; i < 0xFFFF; i++) {
		if (!correct(i) ||
		    !correct(i + UINT32_C(0xFFFF0000)) ||
		    !correct(i + UINT32_C(1000000)) ||
		    !correct(i + UINT32_C(1)) ||
		    !correct(i + UINT32_C(1234567)))
		    return;
	}

	printf("Ok\n");
}

Generated with cgit - Back to sebastiano.tronto.net