1 /*
2 * Copyright 2023 Meta
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <limits.h>
8
9 #include <zephyr/sys/util.h>
10 #include <zephyr/ztest.h>
11
12 static const uint32_t val = NHPOT(42);
13 static const uint64_t val64 = NHPOT(42 + BIT64(32));
14
ZTEST(pot,test_NHPOT)15 ZTEST(pot, test_NHPOT)
16 {
17 zassert_equal(1, NHPOT(LLONG_MIN));
18 zassert_equal(1, NHPOT(LONG_MIN));
19 zassert_equal(1, NHPOT(INT_MIN));
20 zassert_equal(1, NHPOT(-1));
21 zassert_equal(1, NHPOT(0));
22 zassert_equal(1, NHPOT(1));
23 zassert_equal(2, NHPOT(2));
24 zassert_equal(4, NHPOT(3));
25 zassert_equal(4, NHPOT(4));
26 zassert_equal(8, NHPOT(5));
27 zassert_equal(BIT(31), NHPOT(BIT(31)));
28 zassert_equal(BIT64(32), NHPOT(BIT(31) + 1));
29 zassert_equal(BIT64(32), NHPOT(UINT32_MAX));
30 zassert_equal(BIT64(32), NHPOT(BIT64(32)));
31 zassert_equal(0, (uint32_t)NHPOT(BIT64(32)));
32 zassert_equal(BIT64(33), NHPOT(BIT64(32) + 1));
33 zassert_equal(BIT64(63), NHPOT(BIT64(63) - 1));
34 zassert_equal(BIT64(63), NHPOT(BIT64(63)));
35 zassert_equal(0, NHPOT(BIT64(63) + 1));
36 zassert_equal(0, NHPOT(UINT64_MAX));
37
38 zassert_equal(64, val);
39 zassert_equal(BIT64(33), val64);
40 }
41