1 // Copyright 2018 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either expresso or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #include "android/base/ContiguousRangeMapper.h"
16
17 #include <gtest/gtest.h>
18
19 #include <vector>
20
21 using android::base::ContiguousRangeMapper;
22
23 namespace android {
24 namespace base {
25
TEST(ContiguousRangeMapper,Basic)26 TEST(ContiguousRangeMapper, Basic) {
27 std::vector<uintptr_t> elements = {
28 1, 2, 3, 5, 6, 7,
29 };
30
31 int numTotalRanges = 0;
32
33 ContiguousRangeMapper rm(
34 [&numTotalRanges](uintptr_t start, uintptr_t size) {
35 ++numTotalRanges;
36 });
37
38 EXPECT_EQ(0, numTotalRanges);
39
40 rm.finish();
41 EXPECT_EQ(0, numTotalRanges);
42
43 for (auto elt : elements) {
44 rm.add(elt, 1);
45 }
46
47 EXPECT_EQ(1, numTotalRanges);
48
49 rm.finish();
50
51 EXPECT_EQ(2, numTotalRanges);
52
53 rm.finish();
54
55 EXPECT_EQ(2, numTotalRanges);
56 }
57
TEST(ContiguousRangeMapper,Pages)58 TEST(ContiguousRangeMapper, Pages) {
59 std::vector<uintptr_t> elements = {
60 0x1000,
61 0x2000,
62 0x3000,
63 0x5000,
64 0x6000,
65 0x7000,
66 0xa000,
67 0xc000,
68 };
69
70 int numTotalRanges = 0;
71
72 ContiguousRangeMapper rm(
73 [&numTotalRanges](uintptr_t start, uintptr_t size) {
74 ++numTotalRanges;
75 });
76
77 for (auto elt : elements) {
78 rm.add(elt, 0x1000);
79 }
80
81 rm.finish();
82
83 EXPECT_EQ(4, numTotalRanges);
84 }
85
TEST(ContiguousRangeMapper,PagesBatched)86 TEST(ContiguousRangeMapper, PagesBatched) {
87 std::vector<uintptr_t> elements = {
88 0x1000,
89 0x2000,
90
91 0x3000,
92
93 0x5000,
94 0x6000,
95
96 0x7000,
97
98 0xa000,
99
100 0xc000,
101 };
102
103 int numTotalRanges = 0;
104
105 ContiguousRangeMapper rm(
106 [&numTotalRanges](uintptr_t start, uintptr_t size) {
107 ++numTotalRanges;
108 }, 0x2000); // 2 page batch
109
110 for (auto elt : elements) {
111 rm.add(elt, 0x1000);
112 }
113
114 rm.finish();
115
116 EXPECT_EQ(6, numTotalRanges);
117 }
118
119 } // namespace base
120 } // namespace android
121