]> git.proxmox.com Git - rustc.git/blame - src/llvm/unittests/ADT/ArrayRefTest.cpp
Imported Upstream version 1.0.0+dfsg1
[rustc.git] / src / llvm / unittests / ADT / ArrayRefTest.cpp
CommitLineData
1a4d82fc
JJ
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"
14using namespace llvm;
15
16// Check that the ArrayRef-of-pointer converting constructor only allows adding
17// cv qualifiers (not removing them, or otherwise changing the type)
18static_assert(
19 std::is_convertible<ArrayRef<int *>, ArrayRef<const int *>>::value,
20 "Adding const");
21static_assert(
22 std::is_convertible<ArrayRef<int *>, ArrayRef<volatile int *>>::value,
23 "Adding volatile");
24static_assert(!std::is_convertible<ArrayRef<int *>, ArrayRef<float *>>::value,
25 "Changing pointer of one type to a pointer of another");
26static_assert(
27 !std::is_convertible<ArrayRef<const int *>, ArrayRef<int *>>::value,
28 "Removing const");
29static_assert(
30 !std::is_convertible<ArrayRef<volatile int *>, ArrayRef<int *>>::value,
31 "Removing volatile");
32
33namespace llvm {
34
35TEST(ArrayRefTest, AllocatorCopy) {
36 BumpPtrAllocator Alloc;
37 static const uint16_t Words1[] = { 1, 4, 200, 37 };
38 ArrayRef<uint16_t> Array1 = makeArrayRef(Words1, 4);
39 static const uint16_t Words2[] = { 11, 4003, 67, 64000, 13 };
40 ArrayRef<uint16_t> Array2 = makeArrayRef(Words2, 5);
41 ArrayRef<uint16_t> Array1c = Array1.copy(Alloc);
42 ArrayRef<uint16_t> Array2c = Array2.copy(Alloc);;
43 EXPECT_TRUE(Array1.equals(Array1c));
44 EXPECT_NE(Array1.data(), Array1c.data());
45 EXPECT_TRUE(Array2.equals(Array2c));
46 EXPECT_NE(Array2.data(), Array2c.data());
47}
48
49TEST(ArrayRefTest, DropBack) {
50 static const int TheNumbers[] = {4, 8, 15, 16, 23, 42};
51 ArrayRef<int> AR1(TheNumbers);
52 ArrayRef<int> AR2(TheNumbers, AR1.size() - 1);
53 EXPECT_TRUE(AR1.drop_back().equals(AR2));
54}
55
56TEST(ArrayRefTest, Equals) {
57 static const int A1[] = {1, 2, 3, 4, 5, 6, 7, 8};
58 ArrayRef<int> AR1(A1);
59 EXPECT_TRUE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8));
60 EXPECT_FALSE(AR1.equals(8, 1, 2, 4, 5, 6, 6, 7));
61 EXPECT_FALSE(AR1.equals(2, 4, 5, 6, 6, 7, 8, 1));
62 EXPECT_FALSE(AR1.equals(0, 1, 2, 4, 5, 6, 6, 7));
63 EXPECT_FALSE(AR1.equals(1, 2, 42, 4, 5, 6, 7, 8));
64 EXPECT_FALSE(AR1.equals(42, 2, 3, 4, 5, 6, 7, 8));
65 EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 42));
66 EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7));
67 EXPECT_FALSE(AR1.equals(1, 2, 3, 4, 5, 6, 7, 8, 9));
68
69 ArrayRef<int> AR1a = AR1.drop_back();
70 EXPECT_TRUE(AR1a.equals(1, 2, 3, 4, 5, 6, 7));
71 EXPECT_FALSE(AR1a.equals(1, 2, 3, 4, 5, 6, 7, 8));
72
73 ArrayRef<int> AR1b = AR1a.slice(2, 4);
74 EXPECT_TRUE(AR1b.equals(3, 4, 5, 6));
75 EXPECT_FALSE(AR1b.equals(2, 3, 4, 5, 6));
76 EXPECT_FALSE(AR1b.equals(3, 4, 5, 6, 7));
77}
78
79TEST(ArrayRefTest, EmptyEquals) {
80 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
81}
82
83TEST(ArrayRefTest, ConstConvert) {
84 int buf[4];
85 for (int i = 0; i < 4; ++i)
86 buf[i] = i;
87
88 static int *A[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
89 ArrayRef<const int *> a((ArrayRef<int *>(A)));
90 a = ArrayRef<int *>(A);
91}
92
93} // end anonymous namespace