OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2016 The Chromium 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 "base/set_template.h" |
| 6 |
| 7 #include "build/build_config.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 #include "testing/platform_test.h" |
| 10 |
| 11 typedef PlatformTest SetTemplateTest; |
| 12 |
| 13 namespace base { |
| 14 |
| 15 enum class CharElements { |
| 16 One, |
| 17 Two, |
| 18 Three, |
| 19 Four, |
| 20 Five, |
| 21 Six, |
| 22 }; |
| 23 |
| 24 enum class ShortElements { |
| 25 One, |
| 26 Two, |
| 27 Three, |
| 28 Four, |
| 29 Five, |
| 30 Six, |
| 31 Seven, |
| 32 Eight, |
| 33 Nine, |
| 34 Ten, |
| 35 Eleven, |
| 36 Twelve, |
| 37 }; |
| 38 |
| 39 enum class IntElements { |
| 40 One, |
| 41 Two, |
| 42 Three, |
| 43 Four, |
| 44 Five, |
| 45 Six, |
| 46 Seven, |
| 47 Eight, |
| 48 Nine, |
| 49 Ten, |
| 50 Eleven, |
| 51 Twelve, |
| 52 Thirteen, |
| 53 Fourteen, |
| 54 Fifteen, |
| 55 Sixteen, |
| 56 Seventeen, |
| 57 Eighteen, |
| 58 Nineteen, |
| 59 Twenty, |
| 60 TwentyOne, |
| 61 TwentyTwo, |
| 62 TwentyThree, |
| 63 TwentyFour, |
| 64 TwentyFive, |
| 65 TwentySix, |
| 66 TwentySeven, |
| 67 TwentyEight, |
| 68 TwentyNine, |
| 69 Thirty, |
| 70 ThirtyOne, |
| 71 ThirtyTwo, |
| 72 }; |
| 73 |
| 74 using CharSet = SetOf<CharElements, CharElements::Six>; |
| 75 using ShortSet = SetOf<ShortElements, ShortElements::Twelve>; |
| 76 using IntSet = SetOf<IntElements, IntElements::ThirtyTwo>; |
| 77 |
| 78 TEST_F(SetTemplateTest, TestCharSet) { |
| 79 CharSet charSet1({CharElements::Two, CharElements::Five}); |
| 80 CharSet charSet2; |
| 81 |
| 82 EXPECT_TRUE(sizeof(charSet1) == sizeof(unsigned char)); |
| 83 EXPECT_EQ(charSet1.data, 0x12); |
| 84 EXPECT_EQ(charSet2.data, 0); |
| 85 charSet1.Include(CharElements::One); |
| 86 EXPECT_EQ(charSet1.data, 0x13); |
| 87 charSet1.Exclude(CharElements::Five); |
| 88 EXPECT_EQ(charSet1.data, 0x03); |
| 89 charSet2 *= charSet1; |
| 90 EXPECT_EQ(charSet2.data, 0); |
| 91 charSet2 += charSet1; |
| 92 EXPECT_TRUE(charSet1 == charSet2); |
| 93 charSet2.Include({CharElements::Six, CharElements::Four}); |
| 94 EXPECT_EQ(charSet2.data, 0x2B); |
| 95 charSet2 -= charSet1; |
| 96 EXPECT_EQ(charSet2.data, 0x28); |
| 97 charSet2.data |= 0x80; |
| 98 // Test to ensure operator== only checks bits corresponding to the enum |
| 99 EXPECT_TRUE(charSet2 == CharSet({CharElements::Four, CharElements::Six})); |
| 100 EXPECT_FALSE(charSet2.Contains(CharElements::One)); |
| 101 EXPECT_TRUE(charSet2.ContainsAll({CharElements::Four, CharElements::Six})); |
| 102 EXPECT_TRUE(charSet2.ContainsSome({CharElements::Four, |
| 103 CharElements::Six, |
| 104 CharElements::One})); |
| 105 EXPECT_FALSE(charSet2.ContainsSome({CharElements::Two})); |
| 106 EXPECT_FALSE(charSet1[CharElements::Six]); |
| 107 charSet1[CharElements::Six] = true; |
| 108 EXPECT_TRUE(charSet1[CharElements::Six]); |
| 109 } |
| 110 |
| 111 TEST_F(SetTemplateTest, TestShortSet) { |
| 112 ShortSet shortSet1; |
| 113 shortSet1.Include({ShortElements::Five, ShortElements::Seven, |
| 114 ShortElements::Nine, ShortElements::Eleven}); |
| 115 ShortSet shortSet2; |
| 116 |
| 117 EXPECT_TRUE(sizeof(shortSet1) == sizeof(unsigned short)); |
| 118 EXPECT_EQ(shortSet1.data, 0x0550); |
| 119 EXPECT_EQ(shortSet2.data, 0); |
| 120 shortSet1.Include(ShortElements::One).Include(ShortElements::Three); |
| 121 EXPECT_EQ(shortSet1.data, 0x0555); |
| 122 shortSet2 = shortSet1; |
| 123 EXPECT_EQ(shortSet2.data, shortSet1.data); |
| 124 shortSet2.Exclude(ShortElements::Eleven).Exclude(ShortElements::Nine); |
| 125 EXPECT_EQ(shortSet2.data, 0x0055); |
| 126 EXPECT_FALSE(shortSet1[ShortElements::Eight]); |
| 127 shortSet1[ShortElements::Eight] = true; |
| 128 EXPECT_TRUE(shortSet1[ShortElements::Eight]); |
| 129 shortSet1[ShortElements::Eight] = false; |
| 130 EXPECT_FALSE(shortSet1[ShortElements::Eight]); |
| 131 } |
| 132 |
| 133 TEST_F(SetTemplateTest, TestIntSet) { |
| 134 IntSet intSet1 = {IntElements::Two, IntElements::Four, IntElements::Six, |
| 135 IntElements::Eight, IntElements::TwentyEight, IntElements::Thirty, |
| 136 IntElements::ThirtyTwo}; |
| 137 IntSet intSet2; |
| 138 |
| 139 EXPECT_EQ(sizeof(intSet1), sizeof(unsigned int)); |
| 140 EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xA80000AA)); |
| 141 EXPECT_EQ(intSet2.data, static_cast<unsigned int>(0)); |
| 142 intSet1.Include(IntElements::TwentySix) |
| 143 .Include(IntElements::TwentyFour) |
| 144 .Include(IntElements::TwentyTwo) |
| 145 .Include(IntElements::Twenty); |
| 146 EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xAAA800AA)); |
| 147 intSet2 = intSet1; |
| 148 EXPECT_EQ(intSet1.data, intSet2.data); |
| 149 intSet2 -= intSet1; |
| 150 EXPECT_EQ(intSet2.data, static_cast<unsigned int>(0)); |
| 151 intSet1 -= intSet2; |
| 152 EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xAAA800AA)); |
| 153 intSet1.Exclude(IntElements::ThirtyTwo) |
| 154 .Exclude(IntElements::Thirty); |
| 155 EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0x0AA800AA)); |
| 156 EXPECT_TRUE(intSet1[IntElements::TwentySix]); |
| 157 intSet1[IntElements::TwentySeven] = true; |
| 158 EXPECT_TRUE(intSet1.Contains(IntElements::TwentySeven)); |
| 159 } |
| 160 |
| 161 } // namespace base |
OLD | NEW |