1//===- sanitizer_array_ref.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//===----------------------------------------------------------------------===//
8
9#include "sanitizer_common/sanitizer_array_ref.h"
10
11#include <vector>
12
13#include "gtest/gtest.h"
14#include "sanitizer_internal_defs.h"
15
16using namespace __sanitizer;
17namespace {
18
19TEST(ArrayRefTest, Constructors) {
20 ArrayRef<int> ar0;
21 EXPECT_TRUE(ar0.empty());
22 EXPECT_EQ(ar0.size(), 0u);
23
24 static const int kTheNumbers[] = {4, 8, 15, 16, 23, 42};
25 ArrayRef<int> ar1(kTheNumbers);
26 EXPECT_FALSE(ar1.empty());
27 EXPECT_EQ(ar1.size(), ARRAY_SIZE(kTheNumbers));
28
29 ArrayRef<int> ar2(&kTheNumbers[0], &kTheNumbers[2]);
30 EXPECT_FALSE(ar2.empty());
31 EXPECT_EQ(ar2.size(), 2u);
32
33 ArrayRef<int> ar3(&kTheNumbers[0], 3);
34 EXPECT_FALSE(ar3.empty());
35 EXPECT_EQ(ar3.size(), 3u);
36
37 std::vector<int> v(4, 1);
38 ArrayRef<int> ar4(v);
39 EXPECT_FALSE(ar4.empty());
40 EXPECT_EQ(ar4.size(), 4u);
41
42 int n;
43 ArrayRef<int> ar5(n);
44 EXPECT_FALSE(ar5.empty());
45 EXPECT_EQ(ar5.size(), 1u);
46}
47
48TEST(ArrayRefTest, DropBack) {
49 static const int kTheNumbers[] = {4, 8, 15, 16, 23, 42};
50 ArrayRef<int> ar1(kTheNumbers);
51 ArrayRef<int> ar2(kTheNumbers, ar1.size() - 1);
52 EXPECT_TRUE(ar1.drop_back().equals(rhs: ar2));
53}
54
55TEST(ArrayRefTest, DropFront) {
56 static const int kTheNumbers[] = {4, 8, 15, 16, 23, 42};
57 ArrayRef<int> ar1(kTheNumbers);
58 ArrayRef<int> ar2(&kTheNumbers[2], ar1.size() - 2);
59 EXPECT_TRUE(ar1.drop_front(N: 2).equals(rhs: ar2));
60}
61
62TEST(ArrayRefTest, TakeBack) {
63 static const int kTheNumbers[] = {4, 8, 15, 16, 23, 42};
64 ArrayRef<int> ar1(kTheNumbers);
65 ArrayRef<int> ar2(ar1.end() - 1, 1);
66 EXPECT_TRUE(ar1.take_back().equals(rhs: ar2));
67}
68
69TEST(ArrayRefTest, TakeFront) {
70 static const int kTheNumbers[] = {4, 8, 15, 16, 23, 42};
71 ArrayRef<int> ar1(kTheNumbers);
72 ArrayRef<int> ar2(ar1.data(), 2);
73 EXPECT_TRUE(ar1.take_front(N: 2).equals(rhs: ar2));
74}
75
76TEST(ArrayRefTest, Equals) {
77 static const int kA1[] = {1, 2, 3, 4, 5, 6, 7, 8};
78 ArrayRef<int> ar1(kA1);
79 EXPECT_TRUE(ar1.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8})));
80 EXPECT_FALSE(ar1.equals(std::vector<int>({8, 1, 2, 4, 5, 6, 6, 7})));
81 EXPECT_FALSE(ar1.equals(std::vector<int>({2, 4, 5, 6, 6, 7, 8, 1})));
82 EXPECT_FALSE(ar1.equals(std::vector<int>({0, 1, 2, 4, 5, 6, 6, 7})));
83 EXPECT_FALSE(ar1.equals(std::vector<int>({1, 2, 42, 4, 5, 6, 7, 8})));
84 EXPECT_FALSE(ar1.equals(std::vector<int>({42, 2, 3, 4, 5, 6, 7, 8})));
85 EXPECT_FALSE(ar1.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7, 42})));
86 EXPECT_FALSE(ar1.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7})));
87 EXPECT_FALSE(ar1.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8, 9})));
88
89 ArrayRef<int> ar1_a = ar1.drop_back();
90 EXPECT_TRUE(ar1_a.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7})));
91 EXPECT_FALSE(ar1_a.equals(std::vector<int>({1, 2, 3, 4, 5, 6, 7, 8})));
92
93 ArrayRef<int> ar1_b = ar1_a.slice(N: 2, M: 4);
94 EXPECT_TRUE(ar1_b.equals(std::vector<int>({3, 4, 5, 6})));
95 EXPECT_FALSE(ar1_b.equals(std::vector<int>({2, 3, 4, 5, 6})));
96 EXPECT_FALSE(ar1_b.equals(std::vector<int>({3, 4, 5, 6, 7})));
97}
98
99TEST(ArrayRefTest, EmptyEquals) {
100 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
101}
102
103TEST(ArrayRefTest, ConstConvert) {
104 int buf[4];
105 for (int i = 0; i < 4; ++i) buf[i] = i;
106
107 static int *ptrs[] = {&buf[0], &buf[1], &buf[2], &buf[3]};
108 ArrayRef<const int *> a((ArrayRef<int *>(ptrs)));
109 a = ArrayRef<int *>(ptrs);
110}
111
112TEST(ArrayRefTest, ArrayRef) {
113 static const int kA1[] = {1, 2, 3, 4, 5, 6, 7, 8};
114
115 // A copy is expected for non-const ArrayRef (thin copy)
116 ArrayRef<int> ar1(kA1);
117 const ArrayRef<int> &ar1_ref = ArrayRef<int>(ar1);
118 EXPECT_NE(&ar1, &ar1_ref);
119 EXPECT_TRUE(ar1.equals(rhs: ar1_ref));
120
121 // A copy is expected for non-const ArrayRef (thin copy)
122 const ArrayRef<int> ar2(kA1);
123 const ArrayRef<int> &ar2_ref = ArrayRef<int>(ar2);
124 EXPECT_NE(&ar2_ref, &ar2);
125 EXPECT_TRUE(ar2.equals(rhs: ar2_ref));
126}
127
128static_assert(std::is_trivially_copyable_v<ArrayRef<int>>,
129 "trivially copyable");
130
131} // namespace
132

source code of compiler-rt/lib/sanitizer_common/tests/sanitizer_array_ref_test.cpp