summaryrefslogtreecommitdiff
path: root/asl/base/numeric_tests.cpp
blob: afcc12a6dd89ddfa3ede3cfcf0a1203279f6b80c (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
// Copyright 2025 Steven Le Rouzic
//
// SPDX-License-Identifier: BSD-3-Clause

#include "asl/base/numeric.hpp"

#include "asl/testing/testing.hpp"

ASL_TEST(is_pow2)
{
    ASL_TEST_EXPECT(asl::is_pow2(4));
    ASL_TEST_EXPECT(asl::is_pow2(65536));
    ASL_TEST_EXPECT(!asl::is_pow2(6));
    ASL_TEST_EXPECT(!asl::is_pow2(1978));
    ASL_TEST_EXPECT(!asl::is_pow2(0));
    ASL_TEST_EXPECT(asl::is_pow2(4U));
    ASL_TEST_EXPECT(asl::is_pow2(uint64_t{65536}));
}

ASL_TEST(round_down_pow2) // NOLINT
{
    ASL_TEST_EXPECT(asl::round_down_pow2(0, 1) == 0);
    ASL_TEST_EXPECT(asl::round_down_pow2(1, 1) == 1);
    ASL_TEST_EXPECT(asl::round_down_pow2(2, 1) == 2);
    ASL_TEST_EXPECT(asl::round_down_pow2(3, 1) == 3);
    ASL_TEST_EXPECT(asl::round_down_pow2(-1, 1) == -1);
    ASL_TEST_EXPECT(asl::round_down_pow2(-2, 1) == -2);
    ASL_TEST_EXPECT(asl::round_down_pow2(-3, 1) == -3);

    ASL_TEST_EXPECT(asl::round_down_pow2(0U, 1U) == 0U);
    ASL_TEST_EXPECT(asl::round_down_pow2(1U, 1U) == 1U);
    ASL_TEST_EXPECT(asl::round_down_pow2(2U, 1U) == 2U);
    ASL_TEST_EXPECT(asl::round_down_pow2(3U, 1U) == 3U);

    ASL_TEST_EXPECT(asl::round_down_pow2(0, 16) == 0);
    ASL_TEST_EXPECT(asl::round_down_pow2(1, 16) == 0);
    ASL_TEST_EXPECT(asl::round_down_pow2(8, 16) == 0);
    ASL_TEST_EXPECT(asl::round_down_pow2(15, 16) == 0);
    ASL_TEST_EXPECT(asl::round_down_pow2(16, 16) == 16);
    ASL_TEST_EXPECT(asl::round_down_pow2(17, 16) == 16);
    ASL_TEST_EXPECT(asl::round_down_pow2(255, 16) == 240);
    ASL_TEST_EXPECT(asl::round_down_pow2(-255, 16) == -256);
}

ASL_TEST(round_up_pow2) // NOLINT
{
    ASL_TEST_EXPECT(asl::round_up_pow2(0, 1) == 0);
    ASL_TEST_EXPECT(asl::round_up_pow2(1, 1) == 1);
    ASL_TEST_EXPECT(asl::round_up_pow2(2, 1) == 2);
    ASL_TEST_EXPECT(asl::round_up_pow2(3, 1) == 3);
    ASL_TEST_EXPECT(asl::round_up_pow2(-1, 1) == -1);
    ASL_TEST_EXPECT(asl::round_up_pow2(-2, 1) == -2);
    ASL_TEST_EXPECT(asl::round_up_pow2(-3, 1) == -3);

    ASL_TEST_EXPECT(asl::round_up_pow2(0U, 1U) == 0U);
    ASL_TEST_EXPECT(asl::round_up_pow2(1U, 1U) == 1U);
    ASL_TEST_EXPECT(asl::round_up_pow2(2U, 1U) == 2U);
    ASL_TEST_EXPECT(asl::round_up_pow2(3U, 1U) == 3U);

    ASL_TEST_EXPECT(asl::round_up_pow2(0, 16) == 0);
    ASL_TEST_EXPECT(asl::round_up_pow2(1, 16) == 16);
    ASL_TEST_EXPECT(asl::round_up_pow2(8, 16) == 16);
    ASL_TEST_EXPECT(asl::round_up_pow2(15, 16) == 16);
    ASL_TEST_EXPECT(asl::round_up_pow2(16, 16) == 16);
    ASL_TEST_EXPECT(asl::round_up_pow2(17, 16) == 32);
    ASL_TEST_EXPECT(asl::round_up_pow2(255, 16) == 256);
    ASL_TEST_EXPECT(asl::round_up_pow2(-255, 16) == -240);
}