OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "src/base/flags.h" |
| 6 #include "testing/gtest/include/gtest/gtest.h" |
| 7 |
| 8 namespace v8 { |
| 9 namespace base { |
| 10 |
| 11 namespace { |
| 12 |
| 13 enum Flag1 { |
| 14 kFlag1None = 0, |
| 15 kFlag1First = 1u << 1, |
| 16 kFlag1Second = 1u << 2, |
| 17 kFlag1All = kFlag1None | kFlag1First | kFlag1Second |
| 18 }; |
| 19 DEFINE_FLAGS(Flags1, Flag1); |
| 20 |
| 21 |
| 22 DEFINE_OPERATORS_FOR_FLAGS(Flags1) |
| 23 |
| 24 |
| 25 Flags1 bar(Flags1 flags1) { return flags1; } |
| 26 |
| 27 } // namespace |
| 28 |
| 29 |
| 30 TEST(FlagsTest, BasicOperations) { |
| 31 Flags1 a; |
| 32 EXPECT_EQ(kFlag1None, static_cast<int>(a)); |
| 33 a |= kFlag1First; |
| 34 EXPECT_EQ(kFlag1First, static_cast<int>(a)); |
| 35 a = a | kFlag1Second; |
| 36 EXPECT_EQ(kFlag1All, static_cast<int>(a)); |
| 37 a &= kFlag1Second; |
| 38 EXPECT_EQ(kFlag1Second, static_cast<int>(a)); |
| 39 a = kFlag1None & a; |
| 40 EXPECT_EQ(kFlag1None, static_cast<int>(a)); |
| 41 a ^= (kFlag1All | kFlag1None); |
| 42 EXPECT_EQ(kFlag1All, static_cast<int>(a)); |
| 43 Flags1 b = ~a; |
| 44 EXPECT_EQ(kFlag1All, static_cast<int>(a)); |
| 45 EXPECT_EQ(~static_cast<int>(a), static_cast<int>(b)); |
| 46 Flags1 c = a; |
| 47 EXPECT_EQ(a, c); |
| 48 EXPECT_NE(a, b); |
| 49 EXPECT_EQ(a, bar(a)); |
| 50 EXPECT_EQ(a, bar(kFlag1All)); |
| 51 } |
| 52 |
| 53 |
| 54 namespace { |
| 55 namespace foo { |
| 56 |
| 57 enum Option { |
| 58 kNoOptions = 0, |
| 59 kOption1 = 1, |
| 60 kOption2 = 2, |
| 61 kAllOptions = kNoOptions | kOption1 | kOption2 |
| 62 }; |
| 63 DEFINE_FLAGS(Options, Option); |
| 64 |
| 65 } // namespace foo |
| 66 |
| 67 |
| 68 DEFINE_OPERATORS_FOR_FLAGS(foo::Options) |
| 69 |
| 70 } // namespace |
| 71 |
| 72 |
| 73 TEST(FlagsTest, NamespaceScope) { |
| 74 foo::Options options; |
| 75 options ^= foo::kNoOptions; |
| 76 options |= foo::kOption1 | foo::kOption2; |
| 77 EXPECT_EQ(foo::kAllOptions, static_cast<int>(options)); |
| 78 } |
| 79 |
| 80 |
| 81 namespace { |
| 82 |
| 83 struct Foo { |
| 84 enum Enum { kEnum1 = 1, kEnum2 = 2 }; |
| 85 DEFINE_FLAGS(Enums, Enum); |
| 86 }; |
| 87 |
| 88 |
| 89 DEFINE_OPERATORS_FOR_FLAGS(Foo::Enums) |
| 90 |
| 91 } // namespace |
| 92 |
| 93 |
| 94 TEST(FlagsTest, ClassScope) { |
| 95 Foo::Enums enums; |
| 96 enums |= Foo::kEnum1; |
| 97 enums |= Foo::kEnum2; |
| 98 EXPECT_TRUE(enums & Foo::kEnum1); |
| 99 EXPECT_TRUE(enums & Foo::kEnum2); |
| 100 } |
| 101 |
| 102 } // namespace base |
| 103 } // namespace v8 |
OLD | NEW |