//===- llvm/unittest/ADT/EnumeratedArrayTest.cpp ----------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // EnumeratedArray unit tests. // //===----------------------------------------------------------------------===// #include "llvm/ADT/EnumeratedArray.h" #include "llvm/ADT/iterator_range.h" #include "gmock/gmock.h" #include "gtest/gtest.h" #include namespace llvm { //===--------------------------------------------------------------------===// // Test initialization and use of operator[] for both read and write. //===--------------------------------------------------------------------===// TEST(EnumeratedArray, InitAndIndex) { enum class Colors { Red, Blue, Green, Last = Green }; EnumeratedArray Array1; Array1[Colors::Red] = 1.0; Array1[Colors::Blue] = 2.0; Array1[Colors::Green] = 3.0; EXPECT_EQ(Array1[Colors::Red], 1.0); EXPECT_EQ(Array1[Colors::Blue], 2.0); EXPECT_EQ(Array1[Colors::Green], 3.0); EnumeratedArray Array2(true); EXPECT_TRUE(Array2[Colors::Red]); EXPECT_TRUE(Array2[Colors::Blue]); EXPECT_TRUE(Array2[Colors::Green]); Array2[Colors::Red] = true; Array2[Colors::Blue] = false; Array2[Colors::Green] = true; EXPECT_TRUE(Array2[Colors::Red]); EXPECT_FALSE(Array2[Colors::Blue]); EXPECT_TRUE(Array2[Colors::Green]); EnumeratedArray Array3 = {10.0, 11.0, 12.0}; EXPECT_EQ(Array3[Colors::Red], 10.0); EXPECT_EQ(Array3[Colors::Blue], 11.0); EXPECT_EQ(Array3[Colors::Green], 12.0); } //===--------------------------------------------------------------------===// // Test size and empty function //===--------------------------------------------------------------------===// TEST(EnumeratedArray, Size) { enum class Colors { Red, Blue, Green, Last = Green }; EnumeratedArray Array; const auto &ConstArray = Array; EXPECT_EQ(ConstArray.size(), 3u); EXPECT_EQ(ConstArray.empty(), false); } //===--------------------------------------------------------------------===// // Test iterators //===--------------------------------------------------------------------===// TEST(EnumeratedArray, Iterators) { enum class Colors { Red, Blue, Green, Last = Green }; EnumeratedArray Array; const auto &ConstArray = Array; Array[Colors::Red] = 1.0; Array[Colors::Blue] = 2.0; Array[Colors::Green] = 3.0; EXPECT_THAT(Array, testing::ElementsAre(1.0, 2.0, 3.0)); EXPECT_THAT(ConstArray, testing::ElementsAre(1.0, 2.0, 3.0)); EXPECT_THAT(make_range(Array.rbegin(), Array.rend()), testing::ElementsAre(3.0, 2.0, 1.0)); EXPECT_THAT(make_range(ConstArray.rbegin(), ConstArray.rend()), testing::ElementsAre(3.0, 2.0, 1.0)); } //===--------------------------------------------------------------------===// // Test typedefs //===--------------------------------------------------------------------===// namespace { enum class Colors { Red, Blue, Green, Last = Green }; using Array = EnumeratedArray; static_assert(std::is_same_v, "Incorrect value_type type"); static_assert(std::is_same_v, "Incorrect reference type!"); static_assert(std::is_same_v, "Incorrect pointer type!"); static_assert(std::is_same_v, "Incorrect const_reference type!"); static_assert(std::is_same_v, "Incorrect const_pointer type!"); } // namespace } // namespace llvm