|
11 | 11 | //===----------------------------------------------------------------------===//
|
12 | 12 |
|
13 | 13 | #include "llvm/ADT/EnumeratedArray.h"
|
| 14 | +#include "llvm/ADT/iterator_range.h" |
| 15 | +#include "gmock/gmock.h" |
14 | 16 | #include "gtest/gtest.h"
|
| 17 | +#include <type_traits> |
15 | 18 |
|
16 | 19 | namespace llvm {
|
17 | 20 |
|
@@ -46,6 +49,73 @@ TEST(EnumeratedArray, InitAndIndex) {
|
46 | 49 | EXPECT_TRUE(Array2[Colors::Red]);
|
47 | 50 | EXPECT_FALSE(Array2[Colors::Blue]);
|
48 | 51 | EXPECT_TRUE(Array2[Colors::Green]);
|
| 52 | + |
| 53 | + EnumeratedArray<float, Colors, Colors::Last, size_t> Array3 = {10.0, 11.0, |
| 54 | + 12.0}; |
| 55 | + EXPECT_EQ(Array3[Colors::Red], 10.0); |
| 56 | + EXPECT_EQ(Array3[Colors::Blue], 11.0); |
| 57 | + EXPECT_EQ(Array3[Colors::Green], 12.0); |
| 58 | +} |
| 59 | + |
| 60 | +//===--------------------------------------------------------------------===// |
| 61 | +// Test size and empty function |
| 62 | +//===--------------------------------------------------------------------===// |
| 63 | + |
| 64 | +TEST(EnumeratedArray, Size) { |
| 65 | + |
| 66 | + enum class Colors { Red, Blue, Green, Last = Green }; |
| 67 | + |
| 68 | + EnumeratedArray<float, Colors, Colors::Last, size_t> Array; |
| 69 | + const auto &ConstArray = Array; |
| 70 | + |
| 71 | + EXPECT_EQ(ConstArray.size(), 3u); |
| 72 | + EXPECT_EQ(ConstArray.empty(), false); |
| 73 | +} |
| 74 | + |
| 75 | +//===--------------------------------------------------------------------===// |
| 76 | +// Test iterators |
| 77 | +//===--------------------------------------------------------------------===// |
| 78 | + |
| 79 | +TEST(EnumeratedArray, Iterators) { |
| 80 | + |
| 81 | + enum class Colors { Red, Blue, Green, Last = Green }; |
| 82 | + |
| 83 | + EnumeratedArray<float, Colors, Colors::Last, size_t> Array; |
| 84 | + const auto &ConstArray = Array; |
| 85 | + |
| 86 | + Array[Colors::Red] = 1.0; |
| 87 | + Array[Colors::Blue] = 2.0; |
| 88 | + Array[Colors::Green] = 3.0; |
| 89 | + |
| 90 | + EXPECT_THAT(Array, testing::ElementsAre(1.0, 2.0, 3.0)); |
| 91 | + EXPECT_THAT(ConstArray, testing::ElementsAre(1.0, 2.0, 3.0)); |
| 92 | + |
| 93 | + EXPECT_THAT(make_range(Array.rbegin(), Array.rend()), |
| 94 | + testing::ElementsAre(3.0, 2.0, 1.0)); |
| 95 | + EXPECT_THAT(make_range(ConstArray.rbegin(), ConstArray.rend()), |
| 96 | + testing::ElementsAre(3.0, 2.0, 1.0)); |
49 | 97 | }
|
50 | 98 |
|
| 99 | +//===--------------------------------------------------------------------===// |
| 100 | +// Test typedefs |
| 101 | +//===--------------------------------------------------------------------===// |
| 102 | + |
| 103 | +namespace { |
| 104 | + |
| 105 | +enum class Colors { Red, Blue, Green, Last = Green }; |
| 106 | + |
| 107 | +using Array = EnumeratedArray<float, Colors, Colors::Last, size_t>; |
| 108 | + |
| 109 | +static_assert(std::is_same<Array::value_type, float>::value, |
| 110 | + "Incorrect value_type type"); |
| 111 | +static_assert(std::is_same<Array::reference, float &>::value, |
| 112 | + "Incorrect reference type!"); |
| 113 | +static_assert(std::is_same<Array::pointer, float *>::value, |
| 114 | + "Incorrect pointer type!"); |
| 115 | +static_assert(std::is_same<Array::const_reference, const float &>::value, |
| 116 | + "Incorrect const_reference type!"); |
| 117 | +static_assert(std::is_same<Array::const_pointer, const float *>::value, |
| 118 | + "Incorrect const_pointer type!"); |
| 119 | +} // namespace |
| 120 | + |
51 | 121 | } // namespace llvm
|
0 commit comments