Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(692)

Side by Side Diff: media/blink/interval_map_unittest.cc

Issue 1422523007: RangeMap: A int->int mapping with fast range operations (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lru
Patch Set: rename RangeMap to IntervalMap & address comments Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include <stdint.h>
6
7 #include <string>
8
9 #include "base/strings/stringprintf.h"
10 #include "media/blink/interval_map.h"
11 #include "media/blink/test_random.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 // Our tests only modifiy the range map entries in [0..kTestSize).
17 // We need this to be big enough to hit tricky corner cases, but small
18 // enough that we get lots of entry duplication to clean up.
19 // Also, SimpleIntervalMap uses a vector of size kTestSize to emulate
20 // a rangemap, so making this too big will the test down a lot.
21 const int kTestSize = 16;
22
23 class SimpleIntervalMap {
24 public:
25 SimpleIntervalMap() : data_(kTestSize) {}
26
27 void IncrementRange(int32_t from, int32_t to, int32_t how_much) {
28 for (int32_t i = from; i < to; i++) {
29 data_[i] += how_much;
30 }
31 }
32
33 void SetRange(int32_t from, int32_t to, int32_t how_much) {
34 for (int32_t i = from; i < to; i++) {
35 data_[i] = how_much;
36 }
37 }
38
39 int32_t operator[](int32_t index) const { return data_[index]; }
40
41 private:
42 std::vector<int32_t> data_;
43 };
44
45 class IntervalMapTest : public testing::Test {
46 public:
47 IntervalMapTest() : rnd_(42) {}
48 void IncrementRange(int32_t from, int32_t to, int32_t how_much) {
49 truth_.IncrementRange(from, to, how_much);
50 testee_.IncrementRange(from, to, how_much);
51 std::string message =
52 base::StringPrintf("After [%d - %d) += %d", from, to, how_much);
53 Compare(message);
54 }
55
56 void SetRange(int32_t from, int32_t to, int32_t how_much) {
57 truth_.SetRange(from, to, how_much);
58 testee_.SetRange(from, to, how_much);
59 std::string message =
60 base::StringPrintf("After [%d - %d) += %d", from, to, how_much);
61 Compare(message);
62 }
63
64 // Will exercise operator[] and IntervalMap::const_iterator.
65 void Compare(const std::string& message) {
66 bool had_fail = HasFailure();
67 for (int i = 0; i < kTestSize; i++) {
68 EXPECT_EQ(truth_[i], testee_[i]) << " i = " << i << " " << message;
69 }
70 EXPECT_EQ(testee_[-1], 0) << message;
71 EXPECT_EQ(testee_[kTestSize], 0) << message;
72 int32_t prev_ = 0;
73 int32_t end_of_last_range = 0;
74 int32_t num_ranges = 0;
75 for (const auto& r : testee_) {
76 num_ranges++;
77 EXPECT_LT(r.first.begin, r.first.end);
78 if (r.first.begin == std::numeric_limits<int32_t>::min()) {
79 EXPECT_EQ(0, r.second);
80 } else {
81 EXPECT_EQ(end_of_last_range, r.first.begin);
82 EXPECT_GE(r.first.begin, 0) << message;
83 EXPECT_LE(r.first.begin, kTestSize) << message;
84 EXPECT_NE(r.second, prev_) << message;
85 }
86 end_of_last_range = r.first.end;
87 prev_ = r.second;
88 }
89 EXPECT_EQ(prev_, 0) << message;
90
91 if (HasFailure() && !had_fail) {
92 for (int i = 0; i < kTestSize; i++) {
93 LOG(ERROR) << i << ": Truth =" << truth_[i]
94 << " Testee = " << testee_[i];
95 }
96 for (const auto& r : testee_) {
97 LOG(ERROR) << "Range: " << r.first.begin << " - " << r.first.end
98 << " = " << r.second;
99 }
100 }
101 }
102
103 void Clear() {
104 for (int j = 0; j < kTestSize; j++) {
105 IncrementRange(j, j + 1, -truth_[j]);
106 }
107 }
108
109 protected:
110 media::TestRandom rnd_;
111 SimpleIntervalMap truth_;
112 media::IntervalMap<int32_t, int32_t> testee_;
113 };
114 }
115
116 TEST_F(IntervalMapTest, SimpleTest) {
117 IncrementRange(3, 7, 4);
118 EXPECT_EQ(0, testee_[0]);
119 EXPECT_EQ(0, testee_[2]);
120 EXPECT_EQ(4, testee_[3]);
121 EXPECT_EQ(4, testee_[5]);
122 EXPECT_EQ(4, testee_[6]);
123 EXPECT_EQ(0, testee_[7]);
124 IncrementRange(3, 7, -4);
125 EXPECT_TRUE(testee_.empty());
126 }
127
128 TEST_F(IntervalMapTest, SimpleIncrementTest) {
129 IncrementRange(3, 7, 1);
130 IncrementRange(6, 10, 2);
131 EXPECT_EQ(0, testee_[2]);
132 EXPECT_EQ(1, testee_[3]);
133 EXPECT_EQ(1, testee_[5]);
134 EXPECT_EQ(3, testee_[6]);
135 EXPECT_EQ(2, testee_[7]);
136 EXPECT_EQ(2, testee_[9]);
137 EXPECT_EQ(0, testee_[10]);
138 SetRange(3, 12, 0);
139 EXPECT_TRUE(testee_.empty());
140 }
141
142 TEST_F(IntervalMapTest, IncrementJoinRangesTest) {
143 IncrementRange(3, 5, 1);
144 IncrementRange(7, 8, 1);
145 IncrementRange(9, 11, 1);
146 IncrementRange(5, 7, 1);
147 IncrementRange(8, 9, 1);
148 auto i = testee_.find(5);
149 EXPECT_EQ(3, i.range_begin());
150 EXPECT_EQ(11, i.range_end());
151 EXPECT_EQ(1, i.value());
152 }
153
154 TEST_F(IntervalMapTest, SetJoinRangesTest) {
155 SetRange(3, 5, 1);
156 SetRange(7, 8, 1);
157 SetRange(9, 11, 1);
158 SetRange(5, 9, 1); // overwrites one range
159 auto i = testee_.find(5);
160 EXPECT_EQ(3, i.range_begin());
161 EXPECT_EQ(11, i.range_end());
162 EXPECT_EQ(1, i.value());
163 }
164
165 TEST_F(IntervalMapTest, FindTest) {
166 IncrementRange(5, 6, 1);
167 IncrementRange(1, 10, 2);
168 int32_t min_value = std::numeric_limits<int32_t>::min();
169 int32_t max_value = std::numeric_limits<int32_t>::max();
170 auto i = testee_.find(0);
171 EXPECT_EQ(min_value, i.range_begin());
172 EXPECT_EQ(1, i.range_end());
173 EXPECT_EQ(0, i.value());
174 i = testee_.find(4);
175 EXPECT_EQ(1, i.range_begin());
176 EXPECT_EQ(5, i.range_end());
177 EXPECT_EQ(2, i.value());
178 i = testee_.find(5);
179 EXPECT_EQ(5, i.range_begin());
180 EXPECT_EQ(6, i.range_end());
181 EXPECT_EQ(3, i.value());
182 i = testee_.find(6);
183 EXPECT_EQ(6, i.range_begin());
184 EXPECT_EQ(10, i.range_end());
185 EXPECT_EQ(2, i.value());
186 i = testee_.find(9);
187 EXPECT_EQ(6, i.range_begin());
188 EXPECT_EQ(10, i.range_end());
189 EXPECT_EQ(2, i.value());
190 i = testee_.find(10);
191 EXPECT_EQ(10, i.range_begin());
192 EXPECT_EQ(max_value, i.range_end());
193 EXPECT_EQ(0, i.value());
194 }
195
xhwang 2015/11/12 18:24:58 Can you add a test to cover MIN_INT, MAX_INT etc?
hubbe 2015/11/12 19:03:13 Done.
196 TEST_F(IntervalMapTest, RandomIncrementTest) {
197 for (int j = 0; j < 200; j++) {
198 Clear();
199 for (int i = 0; i < 200; i++) {
200 int32_t begin = rnd_.Rand() % (kTestSize - 1);
201 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1);
202 IncrementRange(begin, end, (rnd_.Rand() & 32) ? 1 : -1);
203 if (HasFailure()) {
204 return;
205 }
206 }
207 }
208 }
209
210 TEST_F(IntervalMapTest, RandomSetTest) {
211 for (int j = 0; j < 200; j++) {
212 Clear();
213 for (int i = 0; i < 200; i++) {
214 int32_t begin = rnd_.Rand() % (kTestSize - 1);
215 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1);
216 SetRange(begin, end, rnd_.Rand() & 3);
217 if (HasFailure()) {
218 return;
219 }
220 }
221 }
222 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698