Serenity Operating System
at master 120 lines 3.1 kB view raw
1/* 2 * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> 3 * 4 * SPDX-License-Identifier: BSD-2-Clause 5 */ 6 7#include <LibTest/TestCase.h> 8 9#include <AK/ArbitrarySizedEnum.h> 10#include <AK/UFixedBigInt.h> 11 12AK_MAKE_ARBITRARY_SIZED_ENUM(TestEnum, u8, 13 Foo = TestEnum(1) << 0, 14 Bar = TestEnum(1) << 1, 15 Baz = TestEnum(1) << 2); 16 17AK_MAKE_ARBITRARY_SIZED_ENUM(BigIntTestEnum, u128, 18 Foo = BigIntTestEnum(1u) << 127u); 19 20TEST_CASE(constructor) 21{ 22 { 23 constexpr TestEnum::Type test; 24 static_assert(test.value() == 0); 25 } 26 { 27 constexpr TestEnum::Type test { TestEnum::Foo | TestEnum::Baz }; 28 static_assert(test.value() == 0b101); 29 } 30 { 31 constexpr BigIntTestEnum::Type test { BigIntTestEnum::Foo }; 32 static_assert(test.value() == u128(1u) << 127u); 33 } 34} 35 36TEST_CASE(bitwise_or) 37{ 38 { 39 TestEnum::Type test; 40 EXPECT_EQ(test.value(), 0); 41 test |= TestEnum::Foo; 42 EXPECT_EQ(test.value(), 0b001); 43 test |= TestEnum::Bar; 44 EXPECT_EQ(test.value(), 0b011); 45 test |= TestEnum::Baz; 46 EXPECT_EQ(test.value(), 0b111); 47 } 48 { 49 BigIntTestEnum::Type test; 50 EXPECT_EQ(test.value(), 0u); 51 test |= BigIntTestEnum::Foo; 52 EXPECT_EQ(test.value(), u128(1u) << 127u); 53 } 54} 55 56TEST_CASE(bitwise_and) 57{ 58 { 59 TestEnum::Type test { 0b111 }; 60 EXPECT_EQ(test.value(), 0b111); 61 test &= TestEnum::Foo; 62 EXPECT_EQ(test.value(), 0b001); 63 } 64 { 65 BigIntTestEnum::Type test { u128(1u) << 127u | u128(1u) << 126u }; 66 EXPECT_EQ(test.value(), u128(1u) << 127u | u128(1u) << 126u); 67 test &= BigIntTestEnum::Foo; 68 EXPECT_EQ(test.value(), u128(1u) << 127u); 69 } 70} 71 72TEST_CASE(bitwise_xor) 73{ 74 { 75 TestEnum::Type test { 0b111 }; 76 EXPECT_EQ(test.value(), 0b111); 77 test ^= TestEnum::Foo; 78 EXPECT_EQ(test.value(), 0b110); 79 } 80 { 81 BigIntTestEnum::Type test { u128(1u) << 127u | 1u }; 82 EXPECT_EQ(test.value(), u128(1u) << 127u | 1u); 83 test ^= BigIntTestEnum::Foo; 84 EXPECT_EQ(test.value(), 1u); 85 } 86} 87 88TEST_CASE(has_flag) 89{ 90 { 91 TestEnum::Type test; 92 test |= TestEnum::Foo; 93 EXPECT(test.has_flag(TestEnum::Foo)); 94 EXPECT(!test.has_flag(TestEnum::Bar)); 95 EXPECT(!test.has_flag(TestEnum::Baz)); 96 EXPECT(!test.has_flag(TestEnum::Foo | TestEnum::Bar | TestEnum::Baz)); 97 } 98 { 99 BigIntTestEnum::Type test; 100 test |= BigIntTestEnum::Foo; 101 EXPECT(test.has_flag(BigIntTestEnum::Foo)); 102 } 103} 104 105TEST_CASE(has_any_flag) 106{ 107 { 108 TestEnum::Type test; 109 test |= TestEnum::Foo; 110 EXPECT(test.has_any_flag(TestEnum::Foo)); 111 EXPECT(!test.has_any_flag(TestEnum::Bar)); 112 EXPECT(!test.has_any_flag(TestEnum::Baz)); 113 EXPECT(test.has_any_flag(TestEnum::Foo | TestEnum::Bar | TestEnum::Baz)); 114 } 115 { 116 BigIntTestEnum::Type test; 117 test |= BigIntTestEnum::Foo; 118 EXPECT(test.has_any_flag(BigIntTestEnum::Foo)); 119 } 120}