[yaml2obj/obj2yaml] - Add support for .stack_sizes sections.
[llvm-complete.git] / unittests / ADT / ArrayRefTest.cpp
blob5424742b6b65a9cd89c4b5e04ac9b28d8e22da6f
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 //===----------------------------------------------------------------------===//
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;
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");
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");
49 namespace {
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());
64 // Check that copy can cope with uninitialized memory.
65 struct NonAssignable {
66 const char *Ptr;
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());
78 TEST(ArrayRefTest, SizeTSizedOperations) {
79 ArrayRef<char> AR(nullptr, std::numeric_limits<ptrdiff_t>::max());
81 // Check that drop_back accepts size_t-sized numbers.
82 EXPECT_EQ(1U, AR.drop_back(AR.size() - 1).size());
84 // Check that drop_front accepts size_t-sized numbers.
85 EXPECT_EQ(1U, AR.drop_front(AR.size() - 1).size());
87 // Check that slice accepts size_t-sized numbers.
88 EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
89 EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
92 TEST(ArrayRefTest, DropBack) {
93 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
94 ArrayRef<int> AR1(TheNumbers);
95 ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
96 EXPECT_TRUE(AR1.drop_back().equals(AR2));
99 TEST(ArrayRefTest, DropFront) {
100 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
101 ArrayRef<int> AR1(TheNumbers);
102 ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
103 EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
106 TEST(ArrayRefTest, DropWhile) {
107 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
108 ArrayRef<int> AR1(TheNumbers);
109 ArrayRef<int> Expected = AR1.drop_front(3);
110 EXPECT_EQ(Expected, AR1.drop_while([](const int &N) { return N % 2 == 1; }));
112 EXPECT_EQ(AR1, AR1.drop_while([](const int &N) { return N < 0; }));
113 EXPECT_EQ(ArrayRef<int>(),
114 AR1.drop_while([](const int &N) { return N > 0; }));
117 TEST(ArrayRefTest, DropUntil) {
118 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
119 ArrayRef<int> AR1(TheNumbers);
120 ArrayRef<int> Expected = AR1.drop_front(3);
121 EXPECT_EQ(Expected, AR1.drop_until([](const int &N) { return N % 2 == 0; }));
123 EXPECT_EQ(ArrayRef<int>(),
124 AR1.drop_until([](const int &N) { return N < 0; }));
125 EXPECT_EQ(AR1, AR1.drop_until([](const int &N) { return N > 0; }));
128 TEST(ArrayRefTest, TakeBack) {
129 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
130 ArrayRef<int> AR1(TheNumbers);
131 ArrayRef<int> AR2(AR1.end() - 1, 1);
132 EXPECT_TRUE(AR1.take_back().equals(AR2));
135 TEST(ArrayRefTest, TakeFront) {
136 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
137 ArrayRef<int> AR1(TheNumbers);
138 ArrayRef<int> AR2(AR1.data(), 2);
139 EXPECT_TRUE(AR1.take_front(2).equals(AR2));
142 TEST(ArrayRefTest, TakeWhile) {
143 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
144 ArrayRef<int> AR1(TheNumbers);
145 ArrayRef<int> Expected = AR1.take_front(3);
146 EXPECT_EQ(Expected, AR1.take_while([](const int &N) { return N % 2 == 1; }));
148 EXPECT_EQ(ArrayRef<int>(),
149 AR1.take_while([](const int &N) { return N < 0; }));
150 EXPECT_EQ(AR1, AR1.take_while([](const int &N) { return N > 0; }));
153 TEST(ArrayRefTest, TakeUntil) {
154 static const int TheNumbers[] = {1, 3, 5, 8, 10, 11};
155 ArrayRef<int> AR1(TheNumbers);
156 ArrayRef<int> Expected = AR1.take_front(3);
157 EXPECT_EQ(Expected, AR1.take_until([](const int &N) { return N % 2 == 0; }));
159 EXPECT_EQ(AR1, AR1.take_until([](const int &N) { return N < 0; }));
160 EXPECT_EQ(ArrayRef<int>(),
161 AR1.take_until([](const int &N) { return N > 0; }));
164 TEST(ArrayRefTest, Equals) {
165 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
166 ArrayRef<int> AR1(A1);
167 EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8}));
168 EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7}));
169 EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1}));
170 EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7}));
171 EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8}));
172 EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8}));
173 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42}));
174 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7}));
175 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9}));
177 ArrayRef<int> AR1a = AR1.drop_back();
178 EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7}));
179 EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8}));
181 ArrayRef<int> AR1b = AR1a.slice(2, 4);
182 EXPECT_TRUE(AR1b.equals({3, 4, 5, 6}));
183 EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6}));
184 EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7}));
187 TEST(ArrayRefTest, EmptyEquals) {
188 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
191 TEST(ArrayRefTest, ConstConvert) {
192 int buf[4];
193 for (int i = 0; i < 4; ++i)
194 buf[i] = i;
196 static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
197 ArrayRef<const int *> a((ArrayRef<int *>(A)));
198 a = ArrayRef<int *>(A);
201 static std::vector<int> ReturnTest12() { return {1, 2}; }
202 static void ArgTest12(ArrayRef<int> A) {
203 EXPECT_EQ(2U, A.size());
204 EXPECT_EQ(1, A[0]);
205 EXPECT_EQ(2, A[1]);
208 TEST(ArrayRefTest, InitializerList) {
209 std::initializer_list<int> init_list = { 0, 1, 2, 3, 4 };
210 ArrayRef<int> A = init_list;
211 for (int i = 0; i < 5; ++i)
212 EXPECT_EQ(i, A[i]);
214 std::vector<int> B = ReturnTest12();
215 A = B;
216 EXPECT_EQ(1, A[0]);
217 EXPECT_EQ(2, A[1]);
219 ArgTest12({1, 2});
222 TEST(ArrayRefTest, EmptyInitializerList) {
223 ArrayRef<int> A = {};
224 EXPECT_TRUE(A.empty());
226 A = {};
227 EXPECT_TRUE(A.empty());
230 // Test that makeArrayRef works on ArrayRef (no-op)
231 TEST(ArrayRefTest, makeArrayRef) {
232 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
234 // No copy expected for non-const ArrayRef (true no-op)
235 ArrayRef<int> AR1(A1);
236 ArrayRef<int> &AR1Ref = makeArrayRef(AR1);
237 EXPECT_EQ(&AR1, &AR1Ref);
239 // A copy is expected for non-const ArrayRef (thin copy)
240 const ArrayRef<int> AR2(A1);
241 const ArrayRef<int> &AR2Ref = makeArrayRef(AR2);
242 EXPECT_NE(&AR2Ref, &AR2);
243 EXPECT_TRUE(AR2.equals(AR2Ref));
246 TEST(ArrayRefTest, OwningArrayRef) {
247 static const int A1[] = {0, 1};
248 OwningArrayRef<int> A(makeArrayRef(A1));
249 OwningArrayRef<int> B(std::move(A));
250 EXPECT_EQ(A.data(), nullptr);
253 TEST(ArrayRefTest, makeArrayRefFromStdArray) {
254 std::array<int, 5> A1{42, -5, 0, 1000000, -1000000};
255 ArrayRef<int> A2 = makeArrayRef(A1);
257 EXPECT_EQ(A1.size(), A2.size());
258 for (std::size_t i = 0; i < A1.size(); ++i) {
259 EXPECT_EQ(A1[i], A2[i]);
263 static_assert(is_trivially_copyable<ArrayRef<int>>::value,
264 "trivially copyable");
266 } // end anonymous namespace