• 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 <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 namespace {
35 
TEST(ArrayRefTest,AllocatorCopy)36 TEST(ArrayRefTest, AllocatorCopy) {
37   BumpPtrAllocator Alloc;
38   static const uint16_t Words1[] = { 1, 4, 200, 37 };
39   ArrayRef<uint16_t> Array1 = makeArrayRef(Words1, 4);
40   static const uint16_t Words2[] = { 11, 4003, 67, 64000, 13 };
41   ArrayRef<uint16_t> Array2 = makeArrayRef(Words2, 5);
42   ArrayRef<uint16_t> Array1c = Array1.copy(Alloc);
43   ArrayRef<uint16_t> Array2c = Array2.copy(Alloc);
44   EXPECT_TRUE(Array1.equals(Array1c));
45   EXPECT_NE(Array1.data(), Array1c.data());
46   EXPECT_TRUE(Array2.equals(Array2c));
47   EXPECT_NE(Array2.data(), Array2c.data());
48 
49   // Check that copy can cope with uninitialized memory.
50   struct NonAssignable {
51     const char *Ptr;
52 
53     NonAssignable(const char *Ptr) : Ptr(Ptr) {}
54     NonAssignable(const NonAssignable &RHS) = default;
55     void operator=(const NonAssignable &RHS) { assert(RHS.Ptr != nullptr); }
56     bool operator==(const NonAssignable &RHS) const { return Ptr == RHS.Ptr; }
57   } Array3Src[] = {"hello", "world"};
58   ArrayRef<NonAssignable> Array3Copy = makeArrayRef(Array3Src).copy(Alloc);
59   EXPECT_EQ(makeArrayRef(Array3Src), Array3Copy);
60   EXPECT_NE(makeArrayRef(Array3Src).data(), Array3Copy.data());
61 }
62 
TEST(ArrayRefTest,DropBack)63 TEST(ArrayRefTest, DropBack) {
64   static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
65   ArrayRef<int> AR1(TheNumbers);
66   ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
67   EXPECT_TRUE(AR1.drop_back().equals(AR2));
68 
69   // Check that drop_back accepts size_t-sized numbers.
70   ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
71   EXPECT_EQ(1U, AR3.drop_back(AR3.size() - 1).size());
72 }
73 
TEST(ArrayRefTest,DropFront)74 TEST(ArrayRefTest, DropFront) {
75   static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
76   ArrayRef<int> AR1(TheNumbers);
77   ArrayRef<int> AR2(&TheNumbers[2], AR1.size() - 2);
78   EXPECT_TRUE(AR1.drop_front(2).equals(AR2));
79 
80   // Check that drop_front accepts size_t-sized numbers.
81   ArrayRef<char> AR3((const char *)0x10000, SIZE_MAX - 0x10000);
82   EXPECT_EQ(1U, AR3.drop_front(AR3.size() - 1).size());
83 }
84 
TEST(ArrayRefTest,Equals)85 TEST(ArrayRefTest, Equals) {
86   static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
87   ArrayRef<int> AR1(A1);
88   EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8}));
89   EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7}));
90   EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1}));
91   EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7}));
92   EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8}));
93   EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8}));
94   EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42}));
95   EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7}));
96   EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9}));
97 
98   ArrayRef<int> AR1a = AR1.drop_back();
99   EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7}));
100   EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8}));
101 
102   ArrayRef<int> AR1b = AR1a.slice(2, 4);
103   EXPECT_TRUE(AR1b.equals({3, 4, 5, 6}));
104   EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6}));
105   EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7}));
106 }
107 
TEST(ArrayRefTest,EmptyEquals)108 TEST(ArrayRefTest, EmptyEquals) {
109   EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
110 }
111 
TEST(ArrayRefTest,Slice)112 TEST(ArrayRefTest, Slice) {
113   // Check that slice accepts size_t-sized numbers.
114   ArrayRef<char> AR((const char *)0x10000, SIZE_MAX - 0x10000);
115   EXPECT_EQ(1U, AR.slice(AR.size() - 1).size());
116   EXPECT_EQ(AR.size() - 1, AR.slice(1, AR.size() - 1).size());
117 }
118 
TEST(ArrayRefTest,ConstConvert)119 TEST(ArrayRefTest, ConstConvert) {
120   int buf[4];
121   for (int i = 0; i < 4; ++i)
122     buf[i] = i;
123 
124   static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
125   ArrayRef<const int *> a((ArrayRef<int *>(A)));
126   a = ArrayRef<int *>(A);
127 }
128 
ReturnTest12()129 static std::vector<int> ReturnTest12() { return {1, 2}; }
ArgTest12(ArrayRef<int> A)130 static void ArgTest12(ArrayRef<int> A) {
131   EXPECT_EQ(2U, A.size());
132   EXPECT_EQ(1, A[0]);
133   EXPECT_EQ(2, A[1]);
134 }
135 
TEST(ArrayRefTest,InitializerList)136 TEST(ArrayRefTest, InitializerList) {
137   ArrayRef<int> A = { 0, 1, 2, 3, 4 };
138   for (int i = 0; i < 5; ++i)
139     EXPECT_EQ(i, A[i]);
140 
141   std::vector<int> B = ReturnTest12();
142   A = B;
143   EXPECT_EQ(1, A[0]);
144   EXPECT_EQ(2, A[1]);
145 
146   ArgTest12({1, 2});
147 }
148 
149 // Test that makeArrayRef works on ArrayRef (no-op)
TEST(ArrayRefTest,makeArrayRef)150 TEST(ArrayRefTest, makeArrayRef) {
151   static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
152 
153   // No copy expected for non-const ArrayRef (true no-op)
154   ArrayRef<int> AR1(A1);
155   ArrayRef<int> &AR1Ref = makeArrayRef(AR1);
156   EXPECT_EQ(&AR1, &AR1Ref);
157 
158   // A copy is expected for non-const ArrayRef (thin copy)
159   const ArrayRef<int> AR2(A1);
160   const ArrayRef<int> &AR2Ref = makeArrayRef(AR2);
161   EXPECT_NE(&AR2Ref, &AR2);
162   EXPECT_TRUE(AR2.equals(AR2Ref));
163 }
164 
165 } // end anonymous namespace
166