| Index: base/set_template_unittest.cc
|
| diff --git a/base/set_template_unittest.cc b/base/set_template_unittest.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..6003c319441b2d340b7e4d431016260730efb103
|
| --- /dev/null
|
| +++ b/base/set_template_unittest.cc
|
| @@ -0,0 +1,161 @@
|
| +// Copyright (c) 2016 The Chromium Authors. All rights reserved.
|
| +// Use of this source code is governed by a BSD-style license that can be
|
| +// found in the LICENSE file.
|
| +
|
| +#include "base/set_template.h"
|
| +
|
| +#include "build/build_config.h"
|
| +#include "testing/gtest/include/gtest/gtest.h"
|
| +#include "testing/platform_test.h"
|
| +
|
| +typedef PlatformTest SetTemplateTest;
|
| +
|
| +namespace base {
|
| +
|
| +enum class CharElements {
|
| + One,
|
| + Two,
|
| + Three,
|
| + Four,
|
| + Five,
|
| + Six,
|
| +};
|
| +
|
| +enum class ShortElements {
|
| + One,
|
| + Two,
|
| + Three,
|
| + Four,
|
| + Five,
|
| + Six,
|
| + Seven,
|
| + Eight,
|
| + Nine,
|
| + Ten,
|
| + Eleven,
|
| + Twelve,
|
| +};
|
| +
|
| +enum class IntElements {
|
| + One,
|
| + Two,
|
| + Three,
|
| + Four,
|
| + Five,
|
| + Six,
|
| + Seven,
|
| + Eight,
|
| + Nine,
|
| + Ten,
|
| + Eleven,
|
| + Twelve,
|
| + Thirteen,
|
| + Fourteen,
|
| + Fifteen,
|
| + Sixteen,
|
| + Seventeen,
|
| + Eighteen,
|
| + Nineteen,
|
| + Twenty,
|
| + TwentyOne,
|
| + TwentyTwo,
|
| + TwentyThree,
|
| + TwentyFour,
|
| + TwentyFive,
|
| + TwentySix,
|
| + TwentySeven,
|
| + TwentyEight,
|
| + TwentyNine,
|
| + Thirty,
|
| + ThirtyOne,
|
| + ThirtyTwo,
|
| +};
|
| +
|
| +using CharSet = SetOf<CharElements, CharElements::Six>;
|
| +using ShortSet = SetOf<ShortElements, ShortElements::Twelve>;
|
| +using IntSet = SetOf<IntElements, IntElements::ThirtyTwo>;
|
| +
|
| +TEST_F(SetTemplateTest, TestCharSet) {
|
| + CharSet charSet1({CharElements::Two, CharElements::Five});
|
| + CharSet charSet2;
|
| +
|
| + EXPECT_TRUE(sizeof(charSet1) == sizeof(unsigned char));
|
| + EXPECT_EQ(charSet1.data, 0x12);
|
| + EXPECT_EQ(charSet2.data, 0);
|
| + charSet1.Include(CharElements::One);
|
| + EXPECT_EQ(charSet1.data, 0x13);
|
| + charSet1.Exclude(CharElements::Five);
|
| + EXPECT_EQ(charSet1.data, 0x03);
|
| + charSet2 *= charSet1;
|
| + EXPECT_EQ(charSet2.data, 0);
|
| + charSet2 += charSet1;
|
| + EXPECT_TRUE(charSet1 == charSet2);
|
| + charSet2.Include({CharElements::Six, CharElements::Four});
|
| + EXPECT_EQ(charSet2.data, 0x2B);
|
| + charSet2 -= charSet1;
|
| + EXPECT_EQ(charSet2.data, 0x28);
|
| + charSet2.data |= 0x80;
|
| + // Test to ensure operator== only checks bits corresponding to the enum
|
| + EXPECT_TRUE(charSet2 == CharSet({CharElements::Four, CharElements::Six}));
|
| + EXPECT_FALSE(charSet2.Contains(CharElements::One));
|
| + EXPECT_TRUE(charSet2.ContainsAll({CharElements::Four, CharElements::Six}));
|
| + EXPECT_TRUE(charSet2.ContainsSome({CharElements::Four,
|
| + CharElements::Six,
|
| + CharElements::One}));
|
| + EXPECT_FALSE(charSet2.ContainsSome({CharElements::Two}));
|
| + EXPECT_FALSE(charSet1[CharElements::Six]);
|
| + charSet1[CharElements::Six] = true;
|
| + EXPECT_TRUE(charSet1[CharElements::Six]);
|
| +}
|
| +
|
| +TEST_F(SetTemplateTest, TestShortSet) {
|
| + ShortSet shortSet1;
|
| + shortSet1.Include({ShortElements::Five, ShortElements::Seven,
|
| + ShortElements::Nine, ShortElements::Eleven});
|
| + ShortSet shortSet2;
|
| +
|
| + EXPECT_TRUE(sizeof(shortSet1) == sizeof(unsigned short));
|
| + EXPECT_EQ(shortSet1.data, 0x0550);
|
| + EXPECT_EQ(shortSet2.data, 0);
|
| + shortSet1.Include(ShortElements::One).Include(ShortElements::Three);
|
| + EXPECT_EQ(shortSet1.data, 0x0555);
|
| + shortSet2 = shortSet1;
|
| + EXPECT_EQ(shortSet2.data, shortSet1.data);
|
| + shortSet2.Exclude(ShortElements::Eleven).Exclude(ShortElements::Nine);
|
| + EXPECT_EQ(shortSet2.data, 0x0055);
|
| + EXPECT_FALSE(shortSet1[ShortElements::Eight]);
|
| + shortSet1[ShortElements::Eight] = true;
|
| + EXPECT_TRUE(shortSet1[ShortElements::Eight]);
|
| + shortSet1[ShortElements::Eight] = false;
|
| + EXPECT_FALSE(shortSet1[ShortElements::Eight]);
|
| +}
|
| +
|
| +TEST_F(SetTemplateTest, TestIntSet) {
|
| + IntSet intSet1 = {IntElements::Two, IntElements::Four, IntElements::Six,
|
| + IntElements::Eight, IntElements::TwentyEight, IntElements::Thirty,
|
| + IntElements::ThirtyTwo};
|
| + IntSet intSet2;
|
| +
|
| + EXPECT_EQ(sizeof(intSet1), sizeof(unsigned int));
|
| + EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xA80000AA));
|
| + EXPECT_EQ(intSet2.data, static_cast<unsigned int>(0));
|
| + intSet1.Include(IntElements::TwentySix)
|
| + .Include(IntElements::TwentyFour)
|
| + .Include(IntElements::TwentyTwo)
|
| + .Include(IntElements::Twenty);
|
| + EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xAAA800AA));
|
| + intSet2 = intSet1;
|
| + EXPECT_EQ(intSet1.data, intSet2.data);
|
| + intSet2 -= intSet1;
|
| + EXPECT_EQ(intSet2.data, static_cast<unsigned int>(0));
|
| + intSet1 -= intSet2;
|
| + EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0xAAA800AA));
|
| + intSet1.Exclude(IntElements::ThirtyTwo)
|
| + .Exclude(IntElements::Thirty);
|
| + EXPECT_EQ(intSet1.data, static_cast<unsigned int>(0x0AA800AA));
|
| + EXPECT_TRUE(intSet1[IntElements::TwentySix]);
|
| + intSet1[IntElements::TwentySeven] = true;
|
| + EXPECT_TRUE(intSet1.Contains(IntElements::TwentySeven));
|
| +}
|
| +
|
| +} // namespace base
|
|
|