1 //===- llvm/unittest/ADT/EnumeratedArrayTest.cpp ----------------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // EnumeratedArray unit tests.
11 //===----------------------------------------------------------------------===//
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>
21 //===--------------------------------------------------------------------===//
22 // Test initialization and use of operator[] for both read and write.
23 //===--------------------------------------------------------------------===//
25 TEST(EnumeratedArray
, InitAndIndex
) {
27 enum class Colors
{ Red
, Blue
, Green
, Last
= Green
};
29 EnumeratedArray
<float, Colors
, Colors::Last
, size_t> Array1
;
31 Array1
[Colors::Red
] = 1.0;
32 Array1
[Colors::Blue
] = 2.0;
33 Array1
[Colors::Green
] = 3.0;
35 EXPECT_EQ(Array1
[Colors::Red
], 1.0);
36 EXPECT_EQ(Array1
[Colors::Blue
], 2.0);
37 EXPECT_EQ(Array1
[Colors::Green
], 3.0);
39 EnumeratedArray
<bool, Colors
> Array2(true);
41 EXPECT_TRUE(Array2
[Colors::Red
]);
42 EXPECT_TRUE(Array2
[Colors::Blue
]);
43 EXPECT_TRUE(Array2
[Colors::Green
]);
45 Array2
[Colors::Red
] = true;
46 Array2
[Colors::Blue
] = false;
47 Array2
[Colors::Green
] = true;
49 EXPECT_TRUE(Array2
[Colors::Red
]);
50 EXPECT_FALSE(Array2
[Colors::Blue
]);
51 EXPECT_TRUE(Array2
[Colors::Green
]);
53 EnumeratedArray
<float, Colors
, Colors::Last
, size_t> Array3
= {10.0, 11.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);
60 //===--------------------------------------------------------------------===//
61 // Test size and empty function
62 //===--------------------------------------------------------------------===//
64 TEST(EnumeratedArray
, Size
) {
66 enum class Colors
{ Red
, Blue
, Green
, Last
= Green
};
68 EnumeratedArray
<float, Colors
, Colors::Last
, size_t> Array
;
69 const auto &ConstArray
= Array
;
71 EXPECT_EQ(ConstArray
.size(), 3u);
72 EXPECT_EQ(ConstArray
.empty(), false);
75 //===--------------------------------------------------------------------===//
77 //===--------------------------------------------------------------------===//
79 TEST(EnumeratedArray
, Iterators
) {
81 enum class Colors
{ Red
, Blue
, Green
, Last
= Green
};
83 EnumeratedArray
<float, Colors
, Colors::Last
, size_t> Array
;
84 const auto &ConstArray
= Array
;
86 Array
[Colors::Red
] = 1.0;
87 Array
[Colors::Blue
] = 2.0;
88 Array
[Colors::Green
] = 3.0;
90 EXPECT_THAT(Array
, testing::ElementsAre(1.0, 2.0, 3.0));
91 EXPECT_THAT(ConstArray
, testing::ElementsAre(1.0, 2.0, 3.0));
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));
99 //===--------------------------------------------------------------------===//
101 //===--------------------------------------------------------------------===//
105 enum class Colors
{ Red
, Blue
, Green
, Last
= Green
};
107 using Array
= EnumeratedArray
<float, Colors
, Colors::Last
, size_t>;
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!");