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