1 //===- llvm/unittest/ADT/BitmaskEnumTest.cpp - BitmaskEnum unit tests -----===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9
10 #include "llvm/ADT/BitmaskEnum.h"
11 #include "gtest/gtest.h"
12
13 using namespace llvm;
14
15 namespace {
16 enum Flags {
17 F0 = 0,
18 F1 = 1,
19 F2 = 2,
20 F3 = 4,
21 F4 = 8,
22 LLVM_MARK_AS_BITMASK_ENUM(F4)
23 };
24
TEST(BitmaskEnumTest,BitwiseOr)25 TEST(BitmaskEnumTest, BitwiseOr) {
26 Flags f = F1 | F2;
27 EXPECT_EQ(3, f);
28
29 f = f | F3;
30 EXPECT_EQ(7, f);
31 }
32
TEST(BitmaskEnumTest,BitwiseOrEquals)33 TEST(BitmaskEnumTest, BitwiseOrEquals) {
34 Flags f = F1;
35 f |= F3;
36 EXPECT_EQ(5, f);
37
38 // |= should return a reference to the LHS.
39 f = F2;
40 (f |= F3) = F1;
41 EXPECT_EQ(F1, f);
42 }
43
TEST(BitmaskEnumTest,BitwiseAnd)44 TEST(BitmaskEnumTest, BitwiseAnd) {
45 Flags f = static_cast<Flags>(3) & F2;
46 EXPECT_EQ(F2, f);
47
48 f = (f | F3) & (F1 | F2 | F3);
49 EXPECT_EQ(6, f);
50 }
51
TEST(BitmaskEnumTest,BitwiseAndEquals)52 TEST(BitmaskEnumTest, BitwiseAndEquals) {
53 Flags f = F1 | F2 | F3;
54 f &= F1 | F2;
55 EXPECT_EQ(3, f);
56
57 // &= should return a reference to the LHS.
58 (f &= F1) = F3;
59 EXPECT_EQ(F3, f);
60 }
61
TEST(BitmaskEnumTest,BitwiseXor)62 TEST(BitmaskEnumTest, BitwiseXor) {
63 Flags f = (F1 | F2) ^ (F2 | F3);
64 EXPECT_EQ(5, f);
65
66 f = f ^ F1;
67 EXPECT_EQ(4, f);
68 }
69
TEST(BitmaskEnumTest,BitwiseXorEquals)70 TEST(BitmaskEnumTest, BitwiseXorEquals) {
71 Flags f = (F1 | F2);
72 f ^= (F2 | F4);
73 EXPECT_EQ(9, f);
74
75 // ^= should return a reference to the LHS.
76 (f ^= F4) = F3;
77 EXPECT_EQ(F3, f);
78 }
79
TEST(BitmaskEnumTest,BitwiseNot)80 TEST(BitmaskEnumTest, BitwiseNot) {
81 Flags f = ~F1;
82 EXPECT_EQ(14, f); // Largest value for f is 15.
83 EXPECT_EQ(15, ~F0);
84 }
85
86 enum class FlagsClass {
87 F0 = 0,
88 F1 = 1,
89 F2 = 2,
90 F3 = 4,
91 LLVM_MARK_AS_BITMASK_ENUM(F3)
92 };
93
TEST(BitmaskEnumTest,ScopedEnum)94 TEST(BitmaskEnumTest, ScopedEnum) {
95 FlagsClass f = (FlagsClass::F1 & ~FlagsClass::F0) | FlagsClass::F2;
96 f |= FlagsClass::F3;
97 EXPECT_EQ(7, static_cast<int>(f));
98 }
99
100 struct Container {
101 enum Flags { F0 = 0, F1 = 1, F2 = 2, F3 = 4, LLVM_MARK_AS_BITMASK_ENUM(F3) };
102
getFlags__anon262e3ed60111::Container103 static Flags getFlags() {
104 Flags f = F0 | F1;
105 f |= F2;
106 return f;
107 }
108 };
109
TEST(BitmaskEnumTest,EnumInStruct)110 TEST(BitmaskEnumTest, EnumInStruct) { EXPECT_EQ(3, Container::getFlags()); }
111
112 } // namespace
113
114 namespace foo {
115 namespace bar {
116 namespace {
117 enum FlagsInNamespace {
118 F0 = 0,
119 F1 = 1,
120 F2 = 2,
121 F3 = 4,
122 LLVM_MARK_AS_BITMASK_ENUM(F3)
123 };
124 } // namespace
125 } // namespace foo
126 } // namespace bar
127
128 namespace {
TEST(BitmaskEnumTest,EnumInNamespace)129 TEST(BitmaskEnumTest, EnumInNamespace) {
130 foo::bar::FlagsInNamespace f = ~foo::bar::F0 & (foo::bar::F1 | foo::bar::F2);
131 f |= foo::bar::F3;
132 EXPECT_EQ(7, f);
133 }
134 } // namespace
135