1 //===- llvm/unittest/ADT/BitmaskEnumTest.cpp - BitmaskEnum unit tests -----===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/BitmaskEnum.h"
10 #include "gtest/gtest.h"
21 LLVM_MARK_AS_BITMASK_ENUM(F4
)
24 static_assert(is_bitmask_enum
<Flags
>::value
!= 0);
25 static_assert(largest_bitmask_enum_bit
<Flags
>::value
== Flags::F4
);
27 enum Flags2
{ V0
= 0, V1
= 1, V2
= 2, V3
= 4, V4
= 8 };
31 LLVM_DECLARE_ENUM_AS_BITMASK(Flags2
, V4
);
34 static_assert(is_bitmask_enum
<Flags
>::value
!= 0);
35 static_assert(largest_bitmask_enum_bit
<Flags
>::value
== Flags::F4
);
38 TEST(BitmaskEnumTest
, BitwiseOr
) {
52 TEST(BitmaskEnumTest
, BitwiseOrEquals
) {
57 // |= should return a reference to the LHS.
71 TEST(BitmaskEnumTest
, BitwiseAnd
) {
72 Flags f
= static_cast<Flags
>(3) & F2
;
75 f
= (f
| F3
) & (F1
| F2
| F3
);
78 Flags2 f2
= static_cast<Flags2
>(3) & V2
;
81 f2
= (f2
| V3
) & (V1
| V2
| V3
);
85 TEST(BitmaskEnumTest
, BitwiseAndEquals
) {
86 Flags f
= F1
| F2
| F3
;
90 // &= should return a reference to the LHS.
94 Flags2 f2
= V1
| V2
| V3
;
102 TEST(BitmaskEnumTest
, BitwiseXor
) {
103 Flags f
= (F1
| F2
) ^ (F2
| F3
);
109 Flags2 f2
= (V1
| V2
) ^ (V2
| V3
);
116 TEST(BitmaskEnumTest
, BitwiseXorEquals
) {
121 // ^= should return a reference to the LHS.
125 Flags2 f2
= (V1
| V2
);
133 TEST(BitmaskEnumTest
, ConstantExpression
) {
134 constexpr Flags f1
= ~F1
;
135 constexpr Flags f2
= F1
| F2
;
136 constexpr Flags f3
= F1
& F2
;
137 constexpr Flags f4
= F1
^ F2
;
139 EXPECT_EQ(f2
, F1
| F2
);
140 EXPECT_EQ(f3
, F1
& F2
);
141 EXPECT_EQ(f4
, F1
^ F2
);
143 constexpr Flags2 f21
= ~V1
;
144 constexpr Flags2 f22
= V1
| V2
;
145 constexpr Flags2 f23
= V1
& V2
;
146 constexpr Flags2 f24
= V1
^ V2
;
148 EXPECT_EQ(f22
, V1
| V2
);
149 EXPECT_EQ(f23
, V1
& V2
);
150 EXPECT_EQ(f24
, V1
^ V2
);
153 TEST(BitmaskEnumTest
, BitwiseNot
) {
155 EXPECT_EQ(14, f
); // Largest value for f is 15.
163 enum class FlagsClass
{
168 LLVM_MARK_AS_BITMASK_ENUM(F3
)
171 TEST(BitmaskEnumTest
, ScopedEnum
) {
172 FlagsClass f
= (FlagsClass::F1
& ~FlagsClass::F0
) | FlagsClass::F2
;
174 EXPECT_EQ(7, static_cast<int>(f
));
178 enum Flags
{ F0
= 0, F1
= 1, F2
= 2, F3
= 4, LLVM_MARK_AS_BITMASK_ENUM(F3
) };
180 static Flags
getFlags() {
187 TEST(BitmaskEnumTest
, EnumInStruct
) { EXPECT_EQ(3, Container::getFlags()); }
194 enum FlagsInNamespace
{
199 LLVM_MARK_AS_BITMASK_ENUM(F3
)
206 TEST(BitmaskEnumTest
, EnumInNamespace
) {
207 foo::bar::FlagsInNamespace f
= ~foo::bar::F0
& (foo::bar::F1
| foo::bar::F2
);