mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 09:38:11 +00:00
AK: Add an ArbitrarySizedEnum template
This is an enum-like type that works with arbitrary sized storage > u64, which is the limit for a regular enum class - which limits it to 64 members when needing bit field behavior. Co-authored-by: Ali Mohammad Pur <mpfard@serenityos.org>
This commit is contained in:
parent
8b2361e362
commit
22308e52cf
3 changed files with 248 additions and 0 deletions
120
Tests/AK/TestArbitrarySizedEnum.cpp
Normal file
120
Tests/AK/TestArbitrarySizedEnum.cpp
Normal file
|
@ -0,0 +1,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));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue