1 //===- IndexedAccessorTest.cpp - Indexed Accessor Tests -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "gmock/gmock.h"
14 using namespace llvm::detail
;
17 /// Simple indexed accessor range that wraps an array.
19 struct ArrayIndexedAccessorRange
20 : public indexed_accessor_range
<ArrayIndexedAccessorRange
<T
>, T
*, T
> {
21 ArrayIndexedAccessorRange(T
*data
, ptrdiff_t start
, ptrdiff_t numElements
)
22 : indexed_accessor_range
<ArrayIndexedAccessorRange
<T
>, T
*, T
>(
23 data
, start
, numElements
) {}
24 using indexed_accessor_range
<ArrayIndexedAccessorRange
<T
>, T
*,
25 T
>::indexed_accessor_range
;
27 /// See `llvm::indexed_accessor_range` for details.
28 static T
&dereference(T
*data
, ptrdiff_t index
) { return data
[index
]; }
30 } // end anonymous namespace
33 static void compareData(ArrayIndexedAccessorRange
<T
> range
,
34 ArrayRef
<T
> referenceData
) {
35 ASSERT_EQ(referenceData
.size(), range
.size());
36 ASSERT_TRUE(std::equal(range
.begin(), range
.end(), referenceData
.begin()));
40 TEST(AccessorRange
, SliceTest
) {
41 int rawData
[] = {0, 1, 2, 3, 4};
42 ArrayRef
<int> data
= llvm::ArrayRef(rawData
);
44 ArrayIndexedAccessorRange
<int> range(rawData
, /*start=*/0, /*numElements=*/5);
45 compareData(range
, data
);
46 compareData(range
.slice(2, 3), data
.slice(2, 3));
47 compareData(range
.slice(0, 5), data
.slice(0, 5));
50 TEST(AccessorRange
, EqualTest
) {
51 int32_t rawData1
[] = {0, 1, 2, 3, 4};
52 uint64_t rawData2
[] = {0, 1, 2, 3, 4};
54 ArrayIndexedAccessorRange
<int32_t> range1(rawData1
, /*start=*/0,
56 ArrayIndexedAccessorRange
<uint64_t> range2(rawData2
, /*start=*/0,
58 EXPECT_TRUE(range1
== range2
);
59 EXPECT_FALSE(range1
!= range2
);
60 EXPECT_TRUE(range2
== range1
);
61 EXPECT_FALSE(range2
!= range1
);
63 } // end anonymous namespace