1 //===- llvm/unittest/ADT/ArrayRefTest.cpp - ArrayRef unit tests -----------===// 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 #include "llvm/ADT/ArrayRef.h" 10 #include "llvm/Support/Allocator.h" 11 #include "llvm/Support/raw_ostream.h" 12 #include "gtest/gtest.h" 13 #include <limits> 14 #include <vector> 15 using namespace llvm; 16 17 // Check that the ArrayRef-of-pointer converting constructor only allows adding 18 // cv qualifiers (not removing them, or otherwise changing the type) 19 static_assert( 20 std::is_convertible<ArrayRef<int *>, ArrayRef<const int *>>::value, 21 "Adding const"); 22 static_assert( 23 std::is_convertible<ArrayRef<int *>, ArrayRef<volatile int *>>::value, 24 "Adding volatile"); 25 static_assert(!std::is_convertible<ArrayRef<int *>, ArrayRef<float *>>::value, 26 "Changing pointer of one type to a pointer of another"); 27 static_assert( 28 !std::is_convertible<ArrayRef<const int *>, ArrayRef<int *>>::value, 29 "Removing const"); 30 static_assert( 31 !std::is_convertible<ArrayRef<volatile int *>, ArrayRef<int *>>::value, 32 "Removing volatile"); 33 34 // Check that we can't accidentally assign a temporary location to an ArrayRef. 35 // (Unfortunately we can't make use of the same thing with constructors.) 36 static_assert( 37 !std::is_assignable<ArrayRef<int *>&, int *>::value, 38 "Assigning from single prvalue element"); 39 static_assert( 40 !std::is_assignable<ArrayRef<int *>&, int * &&>::value, 41 "Assigning from single xvalue element"); 42 static_assert( 43 std::is_assignable<ArrayRef<int *>&, int * &>::value, 44 "Assigning from single lvalue element"); 45 static_assert( 46 !std::is_assignable<ArrayRef<int *>&, std::initializer_list<int *>>::value, 47 "Assigning from an initializer list"); 48 49 namespace { 50 51 TEST(ArrayRefTest, AllocatorCopy) { 52 BumpPtrAllocator Alloc; 53 static const uint16_t Words1[] = { 1, 4, 200, 37 }; 54 ArrayRef<uint16_t> Array1 = makeArrayRef(Words1, 4); 55 static const uint16_t Words2[] = { 11, 4003, 67, 64000, 13 }; 56 ArrayRef<uint16_t> Array2 = makeArrayRef(Words2, 5); 57 ArrayRef<uint16_t> Array1c = Array1.copy(Alloc); 58 ArrayRef<uint16_t> Array2c = Array2.copy(Alloc); 59 EXPECT_TRUE(Array1.equals(Array1c)); 60 EXPECT_NE(Array1.data(), Array1c.data()); 61 EXPECT_TRUE(Array2.equals(Array2c)); 62 EXPECT_NE(Array2.data(), Array2c.data()); 63 64 // Check that copy can cope with uninitialized memory. 65 struct NonAssignable { 66 const char *Ptr; 67 68 NonAssignable(const char *Ptr) : Ptr(Ptr) {} 69 NonAssignable(const NonAssignable &RHS) = default; 70 void operator=(const NonAssignable &RHS) { assert(RHS.Ptr != nullptr); } 71 bool operator==(const NonAssignable &RHS) const { return Ptr == RHS.Ptr; } 72 } Array3Src[] = {"hello", "world"}; 73 ArrayRef<NonAssignable> Array3Copy = makeArrayRef(Array3Src).copy(Alloc); 74 EXPECT_EQ(makeArrayRef(Array3Src), Array3Copy); 75 EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data()); 76 } 77 78 // This test is pure UB given the ArrayRef<> implementation. 79 // You are not allowed to produce non-null pointers given null base pointer. 80 TEST(ArrayRefTest, DISABLED_SizeTSizedOperations) { 81 ArrayRef<char> AR(nullptr, std::numeric_limits<ptrdiff_t>::max()); 82 83 // Check that drop_back accepts size_t-sized numbers. 84 EXPECT_EQ(1U, AR.drop_back(AR.size() - 1).size()); 85 86 // Check that drop_front accepts size_t-sized numbers. 87 EXPECT_EQ(1U, AR.drop_front(AR.size() - 1).size()); 88 89 // Check that slice accepts size_t-sized numbers. 90 EXPECT_EQ(1U, AR.slice(AR.size() - 1).size()); 91 EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size()); 92 } 93 94 TEST(ArrayRefTest, DropBack) { 95 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42}; 96 ArrayRef<int> AR1(TheNumbers); 97 ArrayRef<int> AR2(TheNumbers, AR1.size() - 1); 98 EXPECT_TRUE(AR1.drop_back().equals(AR2)); 99 } 100 101 TEST(ArrayRefTest, DropFront) { 102 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42}; 103 ArrayRef<int> AR1(TheNumbers); 104 ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2); 105 EXPECT_TRUE(AR1.drop_front(2).equals(AR2)); 106 } 107 108 TEST(ArrayRefTest, DropWhile) { 109 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; 110 ArrayRef<int> AR1(TheNumbers); 111 ArrayRef<int> Expected = AR1.drop_front(3); 112 EXPECT_EQ(Expected, AR1.drop_while([](const int &N) { return N % 2 == 1; })); 113 114 EXPECT_EQ(AR1, AR1.drop_while([](const int &N) { return N < 0; })); 115 EXPECT_EQ(ArrayRef<int>(), 116 AR1.drop_while([](const int &N) { return N > 0; })); 117 } 118 119 TEST(ArrayRefTest, DropUntil) { 120 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; 121 ArrayRef<int> AR1(TheNumbers); 122 ArrayRef<int> Expected = AR1.drop_front(3); 123 EXPECT_EQ(Expected, AR1.drop_until([](const int &N) { return N % 2 == 0; })); 124 125 EXPECT_EQ(ArrayRef<int>(), 126 AR1.drop_until([](const int &N) { return N < 0; })); 127 EXPECT_EQ(AR1, AR1.drop_until([](const int &N) { return N > 0; })); 128 } 129 130 TEST(ArrayRefTest, TakeBack) { 131 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42}; 132 ArrayRef<int> AR1(TheNumbers); 133 ArrayRef<int> AR2(AR1.end() - 1, 1); 134 EXPECT_TRUE(AR1.take_back().equals(AR2)); 135 } 136 137 TEST(ArrayRefTest, TakeFront) { 138 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42}; 139 ArrayRef<int> AR1(TheNumbers); 140 ArrayRef<int> AR2(AR1.data(), 2); 141 EXPECT_TRUE(AR1.take_front(2).equals(AR2)); 142 } 143 144 TEST(ArrayRefTest, TakeWhile) { 145 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; 146 ArrayRef<int> AR1(TheNumbers); 147 ArrayRef<int> Expected = AR1.take_front(3); 148 EXPECT_EQ(Expected, AR1.take_while([](const int &N) { return N % 2 == 1; })); 149 150 EXPECT_EQ(ArrayRef<int>(), 151 AR1.take_while([](const int &N) { return N < 0; })); 152 EXPECT_EQ(AR1, AR1.take_while([](const int &N) { return N > 0; })); 153 } 154 155 TEST(ArrayRefTest, TakeUntil) { 156 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11}; 157 ArrayRef<int> AR1(TheNumbers); 158 ArrayRef<int> Expected = AR1.take_front(3); 159 EXPECT_EQ(Expected, AR1.take_until([](const int &N) { return N % 2 == 0; })); 160 161 EXPECT_EQ(AR1, AR1.take_until([](const int &N) { return N < 0; })); 162 EXPECT_EQ(ArrayRef<int>(), 163 AR1.take_until([](const int &N) { return N > 0; })); 164 } 165 166 TEST(ArrayRefTest, Equals) { 167 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; 168 ArrayRef<int> AR1(A1); 169 EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8})); 170 EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7})); 171 EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1})); 172 EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7})); 173 EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8})); 174 EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8})); 175 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42})); 176 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7})); 177 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9})); 178 179 ArrayRef<int> AR1a = AR1.drop_back(); 180 EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7})); 181 EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8})); 182 183 ArrayRef<int> AR1b = AR1a.slice(2, 4); 184 EXPECT_TRUE(AR1b.equals({3, 4, 5, 6})); 185 EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6})); 186 EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7})); 187 } 188 189 TEST(ArrayRefTest, EmptyEquals) { 190 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>()); 191 } 192 193 TEST(ArrayRefTest, ConstConvert) { 194 int buf[4]; 195 for (int i = 0; i < 4; ++i) 196 buf[i] = i; 197 198 static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]}; 199 ArrayRef<const int *> a((ArrayRef<int *>(A))); 200 a = ArrayRef<int *>(A); 201 } 202 203 static std::vector<int> ReturnTest12() { return {1, 2}; } 204 static void ArgTest12(ArrayRef<int> A) { 205 EXPECT_EQ(2U, A.size()); 206 EXPECT_EQ(1, A[0]); 207 EXPECT_EQ(2, A[1]); 208 } 209 210 TEST(ArrayRefTest, InitializerList) { 211 std::initializer_list<int> init_list = { 0, 1, 2, 3, 4 }; 212 ArrayRef<int> A = init_list; 213 for (int i = 0; i < 5; ++i) 214 EXPECT_EQ(i, A[i]); 215 216 std::vector<int> B = ReturnTest12(); 217 A = B; 218 EXPECT_EQ(1, A[0]); 219 EXPECT_EQ(2, A[1]); 220 221 ArgTest12({1, 2}); 222 } 223 224 TEST(ArrayRefTest, EmptyInitializerList) { 225 ArrayRef<int> A = {}; 226 EXPECT_TRUE(A.empty()); 227 228 A = {}; 229 EXPECT_TRUE(A.empty()); 230 } 231 232 // Test that makeArrayRef works on ArrayRef (no-op) 233 TEST(ArrayRefTest, makeArrayRef) { 234 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; 235 236 // No copy expected for non-const ArrayRef (true no-op) 237 ArrayRef<int> AR1(A1); 238 ArrayRef<int> &AR1Ref = makeArrayRef(AR1); 239 EXPECT_EQ(&AR1, &AR1Ref); 240 241 // A copy is expected for non-const ArrayRef (thin copy) 242 const ArrayRef<int> AR2(A1); 243 const ArrayRef<int> &AR2Ref = makeArrayRef(AR2); 244 EXPECT_NE(&AR2Ref, &AR2); 245 EXPECT_TRUE(AR2.equals(AR2Ref)); 246 } 247 248 TEST(ArrayRefTest, OwningArrayRef) { 249 static const int A1[] = {0, 1}; 250 OwningArrayRef<int> A(makeArrayRef(A1)); 251 OwningArrayRef<int> B(std::move(A)); 252 EXPECT_EQ(A.data(), nullptr); 253 } 254 255 TEST(ArrayRefTest, makeArrayRefFromStdArray) { 256 std::array<int, 5> A1{{42, -5, 0, 1000000, -1000000}}; 257 ArrayRef<int> A2 = makeArrayRef(A1); 258 259 EXPECT_EQ(A1.size(), A2.size()); 260 for (std::size_t i = 0; i < A1.size(); ++i) { 261 EXPECT_EQ(A1[i], A2[i]); 262 } 263 } 264 265 static_assert(std::is_trivially_copyable<ArrayRef<int>>::value, 266 "trivially copyable"); 267 268 } // end anonymous namespace 269