TestArbitrarySizedEnum.cpp 3.0 KB

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