1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
|
//===- 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 <type_traits>
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<float, Colors, Colors::Last, size_t> 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<bool, Colors> 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<float, Colors, Colors::Last, size_t> 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<float, Colors, Colors::Last, size_t> 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<float, Colors, Colors::Last, size_t> 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<float, Colors, Colors::Last, size_t>;
static_assert(std::is_same_v<Array::value_type, float>,
"Incorrect value_type type");
static_assert(std::is_same_v<Array::reference, float &>,
"Incorrect reference type!");
static_assert(std::is_same_v<Array::pointer, float *>,
"Incorrect pointer type!");
static_assert(std::is_same_v<Array::const_reference, const float &>,
"Incorrect const_reference type!");
static_assert(std::is_same_v<Array::const_pointer, const float *>,
"Incorrect const_pointer type!");
} // namespace
} // namespace llvm
|