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> { |
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; |
26 | |
27 | /// See `llvm::indexed_accessor_range` for details. |
28 | static T &dereference(T *data, ptrdiff_t index) { return data[index]; } |
29 | }; |
30 | } // end anonymous namespace |
31 | |
32 | template <typename T> |
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())); |
37 | } |
38 | |
39 | namespace { |
40 | TEST(AccessorRange, SliceTest) { |
41 | int rawData[] = {0, 1, 2, 3, 4}; |
42 | ArrayRef<int> data = llvm::ArrayRef(rawData); |
43 | |
44 | ArrayIndexedAccessorRange<int> range(rawData, /*start=*/0, /*numElements=*/5); |
45 | compareData(range, referenceData: data); |
46 | compareData(range: range.slice(n: 2, m: 3), referenceData: data.slice(N: 2, M: 3)); |
47 | compareData(range: range.slice(n: 0, m: 5), referenceData: data.slice(N: 0, M: 5)); |
48 | } |
49 | |
50 | TEST(AccessorRange, EqualTest) { |
51 | int32_t rawData1[] = {0, 1, 2, 3, 4}; |
52 | uint64_t rawData2[] = {0, 1, 2, 3, 4}; |
53 | |
54 | ArrayIndexedAccessorRange<int32_t> range1(rawData1, /*start=*/0, |
55 | /*numElements=*/5); |
56 | ArrayIndexedAccessorRange<uint64_t> range2(rawData2, /*start=*/0, |
57 | /*numElements=*/5); |
58 | EXPECT_TRUE(range1 == range2); |
59 | EXPECT_FALSE(range1 != range2); |
60 | EXPECT_TRUE(range2 == range1); |
61 | EXPECT_FALSE(range2 != range1); |
62 | } |
63 | } // end anonymous namespace |
64 | |