Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / sanitizer_common / tests / sanitizer_array_ref_test.cpp
blob12a83df455d39e3cde4dd0042038e5fa90cc1f24
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 //===----------------------------------------------------------------------===//
9 #include "sanitizer_common/sanitizer_array_ref.h"
11 #include <vector>
13 #include "gtest/gtest.h"
14 #include "sanitizer_internal_defs.h"
16 using namespace __sanitizer;
17 namespace {
19 TEST(ArrayRefTest, Constructors) {
20 ArrayRef<int> ar0;
21 EXPECT_TRUE(ar0.empty());
22 EXPECT_EQ(ar0.size(), 0u);
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));
29 ArrayRef<int> ar2(&kTheNumbers[0], &kTheNumbers[2]);
30 EXPECT_FALSE(ar2.empty());
31 EXPECT_EQ(ar2.size(), 2u);
33 ArrayRef<int> ar3(&kTheNumbers[0], 3);
34 EXPECT_FALSE(ar3.empty());
35 EXPECT_EQ(ar3.size(), 3u);
37 std::vector<int> v(4, 1);
38 ArrayRef<int> ar4(v);
39 EXPECT_FALSE(ar4.empty());
40 EXPECT_EQ(ar4.size(), 4u);
42 int n;
43 ArrayRef<int> ar5(n);
44 EXPECT_FALSE(ar5.empty());
45 EXPECT_EQ(ar5.size(), 1u);
48 TEST(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(ar2));
55 TEST(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(2).equals(ar2));
62 TEST(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(ar2));
69 TEST(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(2).equals(ar2));
76 TEST(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})));
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})));
93 ArrayRef<int> ar1_b = ar1_a.slice(2, 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})));
99 TEST(ArrayRefTest, EmptyEquals) {
100 EXPECT_TRUE(ArrayRef<unsigned>() == ArrayRef<unsigned>());
103 TEST(ArrayRefTest, ConstConvert) {
104 int buf[4];
105 for (int i = 0; i < 4; ++i) buf[i] = i;
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);
112 TEST(ArrayRefTest, ArrayRef) {
113 static const int kA1[] = {1, 2, 3, 4, 5, 6, 7, 8};
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(ar1_ref));
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(ar2_ref));
128 static_assert(std::is_trivially_copyable_v<ArrayRef<int>>,
129 "trivially copyable");
131 } // namespace