xref: /llvm-project/llvm/unittests/ADT/EnumeratedArrayTest.cpp (revision 6aa050a69041e610587c51032fa477dd3d6da787)
1 //===- llvm/unittest/ADT/EnumeratedArrayTest.cpp ----------------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // EnumeratedArray unit tests.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "llvm/ADT/EnumeratedArray.h"
14 #include "llvm/ADT/iterator_range.h"
15 #include "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 #include <type_traits>
18 
19 namespace llvm {
20 
21 //===--------------------------------------------------------------------===//
22 // Test initialization and use of operator[] for both read and write.
23 //===--------------------------------------------------------------------===//
24 
TEST(EnumeratedArray,InitAndIndex)25 TEST(EnumeratedArray, InitAndIndex) {
26 
27   enum class Colors { Red, Blue, Green, Last = Green };
28 
29   EnumeratedArray<float, Colors, Colors::Last, size_t> Array1;
30 
31   Array1[Colors::Red] = 1.0;
32   Array1[Colors::Blue] = 2.0;
33   Array1[Colors::Green] = 3.0;
34 
35   EXPECT_EQ(Array1[Colors::Red], 1.0);
36   EXPECT_EQ(Array1[Colors::Blue], 2.0);
37   EXPECT_EQ(Array1[Colors::Green], 3.0);
38 
39   EnumeratedArray<bool, Colors> Array2(true);
40 
41   EXPECT_TRUE(Array2[Colors::Red]);
42   EXPECT_TRUE(Array2[Colors::Blue]);
43   EXPECT_TRUE(Array2[Colors::Green]);
44 
45   Array2[Colors::Red] = true;
46   Array2[Colors::Blue] = false;
47   Array2[Colors::Green] = true;
48 
49   EXPECT_TRUE(Array2[Colors::Red]);
50   EXPECT_FALSE(Array2[Colors::Blue]);
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 
TEST(EnumeratedArray,Size)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 
TEST(EnumeratedArray,Iterators)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));
97 }
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_v<Array::value_type, float>,
110               "Incorrect value_type type");
111 static_assert(std::is_same_v<Array::reference, float &>,
112               "Incorrect reference type!");
113 static_assert(std::is_same_v<Array::pointer, float *>,
114               "Incorrect pointer type!");
115 static_assert(std::is_same_v<Array::const_reference, const float &>,
116               "Incorrect const_reference type!");
117 static_assert(std::is_same_v<Array::const_pointer, const float *>,
118               "Incorrect const_pointer type!");
119 } // namespace
120 
121 } // namespace llvm
122