1 // Copyright 2018 The Android Open Source Project
2 //
3 // This software is licensed under the terms of the GNU General Public
4 // License version 2, as published by the Free Software Foundation, and
5 // may be copied, distributed, and modified under those terms.
6 //
7 // This program is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 // GNU General Public License for more details.
11
12 #include "android/base/ContiguousRangeMapper.h"
13
14 #include <gtest/gtest.h>
15
16 #include <vector>
17
18 using android::base::ContiguousRangeMapper;
19
20 namespace android {
21 namespace base {
22
TEST(ContiguousRangeMapper,Basic)23 TEST(ContiguousRangeMapper, Basic) {
24 std::vector<uintptr_t> elements = {
25 1, 2, 3, 5, 6, 7,
26 };
27
28 int numTotalRanges = 0;
29
30 ContiguousRangeMapper rm(
31 [&numTotalRanges](uintptr_t start, uintptr_t size) {
32 ++numTotalRanges;
33 });
34
35 EXPECT_EQ(0, numTotalRanges);
36
37 rm.finish();
38 EXPECT_EQ(0, numTotalRanges);
39
40 for (auto elt : elements) {
41 rm.add(elt, 1);
42 }
43
44 EXPECT_EQ(1, numTotalRanges);
45
46 rm.finish();
47
48 EXPECT_EQ(2, numTotalRanges);
49
50 rm.finish();
51
52 EXPECT_EQ(2, numTotalRanges);
53 }
54
TEST(ContiguousRangeMapper,Pages)55 TEST(ContiguousRangeMapper, Pages) {
56 std::vector<uintptr_t> elements = {
57 0x1000,
58 0x2000,
59 0x3000,
60 0x5000,
61 0x6000,
62 0x7000,
63 0xa000,
64 0xc000,
65 };
66
67 int numTotalRanges = 0;
68
69 ContiguousRangeMapper rm(
70 [&numTotalRanges](uintptr_t start, uintptr_t size) {
71 ++numTotalRanges;
72 });
73
74 for (auto elt : elements) {
75 rm.add(elt, 0x1000);
76 }
77
78 rm.finish();
79
80 EXPECT_EQ(4, numTotalRanges);
81 }
82
TEST(ContiguousRangeMapper,PagesBatched)83 TEST(ContiguousRangeMapper, PagesBatched) {
84 std::vector<uintptr_t> elements = {
85 0x1000,
86 0x2000,
87
88 0x3000,
89
90 0x5000,
91 0x6000,
92
93 0x7000,
94
95 0xa000,
96
97 0xc000,
98 };
99
100 int numTotalRanges = 0;
101
102 ContiguousRangeMapper rm(
103 [&numTotalRanges](uintptr_t start, uintptr_t size) {
104 ++numTotalRanges;
105 }, 0x2000); // 2 page batch
106
107 for (auto elt : elements) {
108 rm.add(elt, 0x1000);
109 }
110
111 rm.finish();
112
113 EXPECT_EQ(6, numTotalRanges);
114 }
115
116 } // namespace base
117 } // namespace android
118