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 llvm {
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
TEST(ArrayRefTest,DropBack)50 TEST(ArrayRefTest, DropBack) {
51 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
52 ArrayRef<int> AR1(TheNumbers);
53 ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
54 EXPECT_TRUE(AR1.drop_back().equals(AR2));
55 }
56
TEST(ArrayRefTest,Equals)57 TEST(ArrayRefTest, Equals) {
58 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
59 ArrayRef<int> AR1(A1);
60 EXPECT_TRUE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8}));
61 EXPECT_FALSE(AR1.equals({8, 1, 2, 4, 5, 6, 6, 7}));
62 EXPECT_FALSE(AR1.equals({2, 4, 5, 6, 6, 7, 8, 1}));
63 EXPECT_FALSE(AR1.equals({0, 1, 2, 4, 5, 6, 6, 7}));
64 EXPECT_FALSE(AR1.equals({1, 2, 42, 4, 5, 6, 7, 8}));
65 EXPECT_FALSE(AR1.equals({42, 2, 3, 4, 5, 6, 7, 8}));
66 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 42}));
67 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7}));
68 EXPECT_FALSE(AR1.equals({1, 2, 3, 4, 5, 6, 7, 8, 9}));
69
70 ArrayRef<int> AR1a = AR1.drop_back();
71 EXPECT_TRUE(AR1a.equals({1, 2, 3, 4, 5, 6, 7}));
72 EXPECT_FALSE(AR1a.equals({1, 2, 3, 4, 5, 6, 7, 8}));
73
74 ArrayRef<int> AR1b = AR1a.slice(2, 4);
75 EXPECT_TRUE(AR1b.equals({3, 4, 5, 6}));
76 EXPECT_FALSE(AR1b.equals({2, 3, 4, 5, 6}));
77 EXPECT_FALSE(AR1b.equals({3, 4, 5, 6, 7}));
78 }
79
TEST(ArrayRefTest,EmptyEquals)80 TEST(ArrayRefTest, EmptyEquals) {
81 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
82 }
83
TEST(ArrayRefTest,ConstConvert)84 TEST(ArrayRefTest, ConstConvert) {
85 int buf[4];
86 for (int i = 0; i < 4; ++i)
87 buf[i] = i;
88
89 static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
90 ArrayRef<const int *> a((ArrayRef<int *>(A)));
91 a = ArrayRef<int *>(A);
92 }
93
ReturnTest12()94 static std::vector<int> ReturnTest12() { return {1, 2}; }
ArgTest12(ArrayRef<int> A)95 static void ArgTest12(ArrayRef<int> A) {
96 EXPECT_EQ(2U, A.size());
97 EXPECT_EQ(1, A[0]);
98 EXPECT_EQ(2, A[1]);
99 }
100
TEST(ArrayRefTest,InitializerList)101 TEST(ArrayRefTest, InitializerList) {
102 ArrayRef<int> A = { 0, 1, 2, 3, 4 };
103 for (int i = 0; i < 5; ++i)
104 EXPECT_EQ(i, A[i]);
105
106 std::vector<int> B = ReturnTest12();
107 A = B;
108 EXPECT_EQ(1, A[0]);
109 EXPECT_EQ(2, A[1]);
110
111 ArgTest12({1, 2});
112 }
113
114 } // end anonymous namespace
115