summaryrefslogtreecommitdiff
path: root/Tests/AK/TestArbitrarySizedEnum.cpp
blob: 1abda962e87aabc20186c6ac564c13d724981fbd (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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibTest/TestCase.h>

#include <AK/ArbitrarySizedEnum.h>
#include <AK/UFixedBigInt.h>

AK_MAKE_ARBITRARY_SIZED_ENUM(TestEnum, u8,
    Foo = TestEnum(1) << 0,
    Bar = TestEnum(1) << 1,
    Baz = TestEnum(1) << 2);

AK_MAKE_ARBITRARY_SIZED_ENUM(BigIntTestEnum, u128,
    Foo = BigIntTestEnum(1u) << 127u);

TEST_CASE(constructor)
{
    {
        constexpr TestEnum::Type test;
        static_assert(test.value() == 0);
    }
    {
        constexpr TestEnum::Type test { TestEnum::Foo | TestEnum::Baz };
        static_assert(test.value() == 0b101);
    }
    {
        constexpr BigIntTestEnum::Type test { BigIntTestEnum::Foo };
        static_assert(test.value() == u128(1u) << 127u);
    }
}

TEST_CASE(bitwise_or)
{
    {
        TestEnum::Type test;
        EXPECT_EQ(test.value(), 0);
        test |= TestEnum::Foo;
        EXPECT_EQ(test.value(), 0b001);
        test |= TestEnum::Bar;
        EXPECT_EQ(test.value(), 0b011);
        test |= TestEnum::Baz;
        EXPECT_EQ(test.value(), 0b111);
    }
    {
        BigIntTestEnum::Type test;
        EXPECT_EQ(test.value(), 0u);
        test |= BigIntTestEnum::Foo;
        EXPECT_EQ(test.value(), u128(1u) << 127u);
    }
}

TEST_CASE(bitwise_and)
{
    {
        TestEnum::Type test { 0b111 };
        EXPECT_EQ(test.value(), 0b111);
        test &= TestEnum::Foo;
        EXPECT_EQ(test.value(), 0b001);
    }
    {
        BigIntTestEnum::Type test { u128(1u) << 127u | u128(1u) << 126u };
        EXPECT_EQ(test.value(), u128(1u) << 127u | u128(1u) << 126u);
        test &= BigIntTestEnum::Foo;
        EXPECT_EQ(test.value(), u128(1u) << 127u);
    }
}

TEST_CASE(bitwise_xor)
{
    {
        TestEnum::Type test { 0b111 };
        EXPECT_EQ(test.value(), 0b111);
        test ^= TestEnum::Foo;
        EXPECT_EQ(test.value(), 0b110);
    }
    {
        BigIntTestEnum::Type test { u128(1u) << 127u | 1u };
        EXPECT_EQ(test.value(), u128(1u) << 127u | 1u);
        test ^= BigIntTestEnum::Foo;
        EXPECT_EQ(test.value(), 1u);
    }
}

TEST_CASE(has_flag)
{
    {
        TestEnum::Type test;
        test |= TestEnum::Foo;
        EXPECT(test.has_flag(TestEnum::Foo));
        EXPECT(!test.has_flag(TestEnum::Bar));
        EXPECT(!test.has_flag(TestEnum::Baz));
        EXPECT(!test.has_flag(TestEnum::Foo | TestEnum::Bar | TestEnum::Baz));
    }
    {
        BigIntTestEnum::Type test;
        test |= BigIntTestEnum::Foo;
        EXPECT(test.has_flag(BigIntTestEnum::Foo));
    }
}

TEST_CASE(has_any_flag)
{
    {
        TestEnum::Type test;
        test |= TestEnum::Foo;
        EXPECT(test.has_any_flag(TestEnum::Foo));
        EXPECT(!test.has_any_flag(TestEnum::Bar));
        EXPECT(!test.has_any_flag(TestEnum::Baz));
        EXPECT(test.has_any_flag(TestEnum::Foo | TestEnum::Bar | TestEnum::Baz));
    }
    {
        BigIntTestEnum::Type test;
        test |= BigIntTestEnum::Foo;
        EXPECT(test.has_any_flag(BigIntTestEnum::Foo));
    }
}