summaryrefslogtreecommitdiff
path: root/libmisc/tests/test_macro.c
blob: 69655d14544db89877b02273f694131d43c8aaae (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
/* libmisc/tests/test_macro.c - Tests for <libmisc/macro.h>
 *
 * Copyright (C) 2024-2025  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <libmisc/macro.h>

#include "test.h"

int main() {
	printf("== LM_NEXT_POWER_OF_2 =====================================\n");
	/* valid down to 0.  */
	test_assert(LM_NEXT_POWER_OF_2(0) == 1);
	test_assert(LM_NEXT_POWER_OF_2(1) == 2);
	test_assert(LM_NEXT_POWER_OF_2(2) == 4);
	test_assert(LM_NEXT_POWER_OF_2(3) == 4);
	test_assert(LM_NEXT_POWER_OF_2(4) == 8);
	test_assert(LM_NEXT_POWER_OF_2(5) == 8);
	test_assert(LM_NEXT_POWER_OF_2(6) == 8);
	test_assert(LM_NEXT_POWER_OF_2(7) == 8);
	test_assert(LM_NEXT_POWER_OF_2(8) == 16);
	/* ... */
	test_assert(LM_NEXT_POWER_OF_2(16) == 32);
	/* ... */
	test_assert(LM_NEXT_POWER_OF_2(0x7000000000000000) == 0x8000000000000000);
	/* ... */
	test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000-1) == 0x8000000000000000);
	/* Valid up to 0x8000000000000000-1 = (1<<63)-1 */
	test_assert(LM_NEXT_POWER_OF_2(0x8000000000000000) == 0); /* :( */

	printf("== LM_FLOORLOG2 ===========================================\n");
	/* valid down to 1.  */
	test_assert(LM_FLOORLOG2(1) == 0);
	test_assert(LM_FLOORLOG2(2) == 1);
	test_assert(LM_FLOORLOG2(3) == 1);
	test_assert(LM_FLOORLOG2(4) == 2);
	test_assert(LM_FLOORLOG2(5) == 2);
	test_assert(LM_FLOORLOG2(6) == 2);
	test_assert(LM_FLOORLOG2(7) == 2);
	test_assert(LM_FLOORLOG2(8) == 3);
	/* ... */
	test_assert(LM_FLOORLOG2(16) == 4);
	/* ... */
	test_assert(LM_FLOORLOG2(0x80000000) == 31);
	/* ... */
	test_assert(LM_FLOORLOG2(0xFFFFFFFF) == 31);
	test_assert(LM_FLOORLOG2(0x100000000) == 32);
	/* ... */
	test_assert(LM_FLOORLOG2(0x8000000000000000) == 63);
	/* ... */
	test_assert(LM_FLOORLOG2(0xFFFFFFFFFFFFFFFF) == 63);

	return 0;
}