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 "gtest/gtest.h"
15 
16 namespace llvm {
17 
18 //===--------------------------------------------------------------------===//
19 // Test initialization and use of operator[] for both read and write.
20 //===--------------------------------------------------------------------===//
21 
TEST(EnumeratedArray,InitAndIndex)22 TEST(EnumeratedArray, InitAndIndex) {
23 
24   enum class Colors { Red, Blue, Green, Last = Green };
25 
26   EnumeratedArray<float, Colors, Colors::Last, size_t> Array1;
27 
28   Array1[Colors::Red] = 1.0;
29   Array1[Colors::Blue] = 2.0;
30   Array1[Colors::Green] = 3.0;
31 
32   EXPECT_EQ(Array1[Colors::Red], 1.0);
33   EXPECT_EQ(Array1[Colors::Blue], 2.0);
34   EXPECT_EQ(Array1[Colors::Green], 3.0);
35 
36   EnumeratedArray<bool, Colors> Array2(true);
37 
38   EXPECT_TRUE(Array2[Colors::Red]);
39   EXPECT_TRUE(Array2[Colors::Blue]);
40   EXPECT_TRUE(Array2[Colors::Green]);
41 
42   Array2[Colors::Red] = true;
43   Array2[Colors::Blue] = false;
44   Array2[Colors::Green] = true;
45 
46   EXPECT_TRUE(Array2[Colors::Red]);
47   EXPECT_FALSE(Array2[Colors::Blue]);
48   EXPECT_TRUE(Array2[Colors::Green]);
49 }
50 
51 } // namespace llvm
52