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 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 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 70 TEST(ArrayRefTest, Equals) { 71 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; 72 ArrayRef<int> AR1(A1); 73 EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8})); 74 EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7})); 75 EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1})); 76 EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7})); 77 EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8})); 78 EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8})); 79 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42})); 80 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7})); 81 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9})); 82 83 ArrayRef<int> AR1a = AR1.drop_back(); 84 EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7})); 85 EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8})); 86 87 ArrayRef<int> AR1b = AR1a.slice(2, 4); 88 EXPECT_TRUE(AR1b.equals({3, 4, 5, 6})); 89 EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6})); 90 EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7})); 91 } 92 93 TEST(ArrayRefTest, EmptyEquals) { 94 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>()); 95 } 96 97 TEST(ArrayRefTest, ConstConvert) { 98 int buf[4]; 99 for (int i = 0; i < 4; ++i) 100 buf[i] = i; 101 102 static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]}; 103 ArrayRef<const int *> a((ArrayRef<int *>(A))); 104 a = ArrayRef<int *>(A); 105 } 106 107 static std::vector<int> ReturnTest12() { return {1, 2}; } 108 static void ArgTest12(ArrayRef<int> A) { 109 EXPECT_EQ(2U, A.size()); 110 EXPECT_EQ(1, A[0]); 111 EXPECT_EQ(2, A[1]); 112 } 113 114 TEST(ArrayRefTest, InitializerList) { 115 ArrayRef<int> A = { 0, 1, 2, 3, 4 }; 116 for (int i = 0; i < 5; ++i) 117 EXPECT_EQ(i, A[i]); 118 119 std::vector<int> B = ReturnTest12(); 120 A = B; 121 EXPECT_EQ(1, A[0]); 122 EXPECT_EQ(2, A[1]); 123 124 ArgTest12({1, 2}); 125 } 126 127 // Test that makeArrayRef works on ArrayRef (no-op) 128 TEST(ArrayRefTest, makeArrayRef) { 129 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8}; 130 131 // No copy expected for non-const ArrayRef (true no-op) 132 ArrayRef<int> AR1(A1); 133 ArrayRef<int> &AR1Ref = makeArrayRef(AR1); 134 EXPECT_EQ(&AR1, &AR1Ref); 135 136 // A copy is expected for non-const ArrayRef (thin copy) 137 const ArrayRef<int> AR2(A1); 138 const ArrayRef<int> &AR2Ref = makeArrayRef(AR2); 139 EXPECT_NE(&AR2Ref, &AR2); 140 EXPECT_TRUE(AR2.equals(AR2Ref)); 141 } 142 143 } // end anonymous namespace 144