1 //===- IndexedAccessorTest.cpp - Indexed Accessor 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 "llvm/ADT/ArrayRef.h"
10 #include "llvm/ADT/STLExtras.h"
11 #include "gmock/gmock.h"
12
13 using namespace llvm;
14 using namespace llvm::detail;
15
16 namespace {
17 /// Simple indexed accessor range that wraps an array.
18 template <typename T>
19 struct ArrayIndexedAccessorRange
20 : public indexed_accessor_range<ArrayIndexedAccessorRange<T>, T *, T> {
ArrayIndexedAccessorRange__anone302421c0111::ArrayIndexedAccessorRange21 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;
26
27 /// See `llvm::indexed_accessor_range` for details.
dereference__anone302421c0111::ArrayIndexedAccessorRange28 static T &dereference(T *data, ptrdiff_t index) { return data[index]; }
29 };
30 } // end anonymous namespace
31
32 template <typename T>
compareData(ArrayIndexedAccessorRange<T> range,ArrayRef<T> referenceData)33 static void compareData(ArrayIndexedAccessorRange<T> range,
34 ArrayRef<T> referenceData) {
35 ASSERT_TRUE(referenceData.size() == range.size());
36 ASSERT_TRUE(std::equal(range.begin(), range.end(), referenceData.begin()));
37 }
38
39 namespace {
TEST(AccessorRange,SliceTest)40 TEST(AccessorRange, SliceTest) {
41 int rawData[] = {0, 1, 2, 3, 4};
42 ArrayRef<int> data = llvm::makeArrayRef(rawData);
43
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));
48 }
49 } // end anonymous namespace
50