summaryrefslogtreecommitdiffstats
path: root/tests/lib/lib-test.c
blob: 826c6c563a94481104234a7104e381067d7d3506 (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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
/* SPDX-License-Identifier: GPL-2.0-only */

#include <lib.h>
#include <tests/test.h>

void test_popcnt(void **state)
{
	assert_int_equal(popcnt(0x0), 0);
	assert_int_equal(popcnt(0x10), 1);
	assert_int_equal(popcnt(0x10010010), 3);
	assert_int_equal(popcnt(0xffffffff), 32);
}

void test_clz(void **state)
{
	assert_int_equal(clz(0x0), 32);
	assert_int_equal(clz(0xf), 28);
	assert_int_equal(clz(0x80000000), 0);
	assert_int_equal(clz(0xffffffff), 0);
}

void test_log2(void **state)
{
	assert_int_equal(log2(0x0), -1);
	assert_int_equal(log2(0x1), 0);
	assert_int_equal(log2(0x5), 2);
	assert_int_equal(log2(0x80000000), 31);
	assert_int_equal(log2(0xffffffff), 31);
}

void test_ffs(void **state)
{
	assert_int_equal(__ffs(0x0), -1);
	assert_int_equal(__ffs(0x1), 0);
	assert_int_equal(__ffs(0x1010), 4);
	assert_int_equal(__ffs(0x10000000), 28);
	assert_int_equal(__ffs(0xffffffff), 0);
}

void test_fls(void **state)
{
	assert_int_equal(__fls(0x0), -1);
	assert_int_equal(__fls(0x1), 0);
	assert_int_equal(__fls(0x5), 2);
	assert_int_equal(__fls(0x80000000), 31);
	assert_int_equal(__fls(0xffffffff), 31);
}

void test_log2_ceil(void **state)
{
	assert_int_equal(log2_ceil(0x0), -1);
	assert_int_equal(log2_ceil(0x1), 0);
	assert_int_equal(log2_ceil(0x5), 3);
	assert_int_equal(log2_ceil(0x80000000), 31);
	assert_int_equal(log2_ceil(0xffffffff), 32);
}

int main(void)
{
	const struct CMUnitTest tests[] = {
		cmocka_unit_test(test_popcnt),
		cmocka_unit_test(test_clz),
		cmocka_unit_test(test_log2),
		cmocka_unit_test(test_ffs),
		cmocka_unit_test(test_fls),
		cmocka_unit_test(test_log2_ceil),
	};

	return cb_run_group_tests(tests, NULL, NULL);
}