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

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

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: refactor 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/rangemap.h"
11 #include "media/blink/test_random.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
16 const int kTestSize = 16;
17
18 class SimpleRangeMap {
19 public:
20 SimpleRangeMap() : data_(kTestSize) {
21 }
22
23 void IncrementRange(int32_t from, int32_t to, int32_t howmuch) {
24 for (int32_t i = from; i < to; i++) {
25 data_[i] += howmuch;
26 }
27 }
28
29 void SetRange(int32_t from, int32_t to, int32_t howmuch) {
30 for (int32_t i = from; i < to; i++) {
31 data_[i] = howmuch;
32 }
33 }
34
35 int32_t operator[](int32_t index) const {
36 return data_[index];
37 }
38
39 private:
40 std::vector<int32_t> data_;
41 };
42
43 class RangeMapTest : public testing::Test {
44 public:
45 RangeMapTest() : rnd_(42) {}
46 void IncrementRange(int32_t from, int32_t to, int32_t howmuch) {
47 truth_.IncrementRange(from, to, howmuch);
48 testee_.IncrementRange(from, to, howmuch);
49 std::string message = base::StringPrintf(
50 "After [%d - %d) += %d",
51 from, to, howmuch);
52 Compare(message);
53 }
54
55 void SetRange(int32_t from, int32_t to, int32_t howmuch) {
56 truth_.SetRange(from, to, howmuch);
57 testee_.SetRange(from, to, howmuch);
58 std::string message = base::StringPrintf(
59 "After [%d - %d) += %d",
60 from, to, howmuch);
61 Compare(message);
62 }
63
64 // Will exercise operator[] and RangeMap::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])
69 << " i = " << i << " " << message;
70 }
71 EXPECT_EQ(testee_[-1], 0) << message;
72 EXPECT_EQ(testee_[kTestSize], 0) << message;
73 int32_t prev_ = 0;
74 int32_t end_of_last_range = 0;
75 int32_t num_ranges = 0;
76 for (const auto& r : testee_) {
77 num_ranges++;
78 EXPECT_LT(r.first.begin, r.first.end);
79 if (r.first.begin == std::numeric_limits<int32_t>::min()) {
80 EXPECT_EQ(0, r.second);
81 } else {
82 EXPECT_EQ(end_of_last_range, r.first.begin);
83 EXPECT_GE(r.first.begin, 0) << message;
84 EXPECT_LE(r.first.begin, kTestSize) << message;
85 EXPECT_NE(r.second, prev_) << message;
86 }
87 end_of_last_range = r.first.end;
88 prev_ = r.second;
89 }
90 if (num_ranges > 1) {
91 EXPECT_NE(prev_, 0) << message;
92 }
93 if (HasFailure() && !had_fail) {
94 for (int i = 0; i < kTestSize; i++) {
95 LOG(ERROR) << i << ": Truth =" << truth_[i]
96 << " Testee = " << testee_[i];
97 }
98 for (const auto& r : testee_) {
99 LOG(ERROR) << "Range: " << r.first.begin << " - " << r.first.end
100 << " = " << r.second;
101 }
102 }
103 }
104
105 void Clear() {
106 for (int j = 0; j < kTestSize; j++) {
107 IncrementRange(j, j + 1, -truth_[j]);
108 }
109 }
110
111 protected:
112 media::TestRandom rnd_;
113 SimpleRangeMap truth_;
114 media::RangeMap<int32_t, int32_t> testee_;
115 };
116
117 }
118
119 TEST_F(RangeMapTest, SimpleTest) {
120 IncrementRange(3, 7, 4);
121 EXPECT_EQ(0, testee_[0]);
122 EXPECT_EQ(0, testee_[2]);
123 EXPECT_EQ(4, testee_[3]);
124 EXPECT_EQ(4, testee_[5]);
125 EXPECT_EQ(4, testee_[6]);
126 EXPECT_EQ(0, testee_[7]);
127 IncrementRange(3, 7, -4);
128 EXPECT_TRUE(testee_.begin() == testee_.end());
129 }
130
131 TEST_F(RangeMapTest, SimpleIncrementTest) {
132 IncrementRange(3, 7, 1);
133 IncrementRange(6, 10, 2);
134 EXPECT_EQ(0, testee_[2]);
135 EXPECT_EQ(1, testee_[3]);
136 EXPECT_EQ(1, testee_[5]);
137 EXPECT_EQ(3, testee_[6]);
138 EXPECT_EQ(2, testee_[7]);
139 EXPECT_EQ(2, testee_[9]);
140 EXPECT_EQ(0, testee_[10]);
141 SetRange(3, 12, 0);
142 EXPECT_TRUE(testee_.begin() == testee_.end());
143 }
144
145 TEST_F(RangeMapTest, IncrementJoinRangesTest) {
146 IncrementRange(3, 5, 1);
147 IncrementRange(7, 8, 1);
148 IncrementRange(9, 11, 1);
149 IncrementRange(5, 7, 1);
150 IncrementRange(8, 9, 1);
151 auto i = testee_.find(5);
152 EXPECT_EQ(3, i.range_begin());
153 EXPECT_EQ(11, i.range_end());
154 EXPECT_EQ(1, i.value());
155 }
156
157 TEST_F(RangeMapTest, SetJoinRangesTest) {
158 SetRange(3, 5, 1);
159 SetRange(7, 8, 1);
160 SetRange(9, 11, 1);
161 SetRange(5, 9, 1); // overwrites one range
162 auto i = testee_.find(5);
163 EXPECT_EQ(3, i.range_begin());
164 EXPECT_EQ(11, i.range_end());
165 EXPECT_EQ(1, i.value());
166 }
167
168 TEST_F(RangeMapTest, FindTest) {
169 IncrementRange(5, 6, 1);
170 IncrementRange(1, 10, 2);
171 uint32_t min_value = std::numeric_limits<int32_t>::min();
172 uint32_t max_value = std::numeric_limits<int32_t>::max();
173 auto i = testee_.find(0);
174 EXPECT_EQ(min_value, i.range_begin());
175 EXPECT_EQ(1, i.range_end());
176 EXPECT_EQ(0, i.value());
177 i = testee_.find(4);
178 EXPECT_EQ(1, i.range_begin());
179 EXPECT_EQ(5, i.range_end());
180 EXPECT_EQ(2, i.value());
181 i = testee_.find(5);
182 EXPECT_EQ(5, i.range_begin());
183 EXPECT_EQ(6, i.range_end());
184 EXPECT_EQ(3, i.value());
185 i = testee_.find(6);
186 EXPECT_EQ(6, i.range_begin());
187 EXPECT_EQ(10, i.range_end());
188 EXPECT_EQ(2, i.value());
189 i = testee_.find(9);
190 EXPECT_EQ(6, i.range_begin());
191 EXPECT_EQ(10, i.range_end());
192 EXPECT_EQ(2, i.value());
193 i = testee_.find(10);
194 EXPECT_EQ(10, i.range_begin());
195 EXPECT_EQ(max_value, i.range_end());
196 EXPECT_EQ(0, i.value());
197 }
198
199 TEST_F(RangeMapTest, RandomIncrementTest) {
200 for (int j = 0; j < 200; j++) {
201 Clear();
202 for (int i = 0; i < 200; i++) {
203 int32_t begin = rnd_.Rand() % (kTestSize - 1);
204 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1);
205 IncrementRange(begin, end, (rnd_.Rand() & 32) ? 1 : -1);
206 if (HasFailure()) {
207 return;
208 }
209 }
210 }
211 }
212
213 TEST_F(RangeMapTest, RandomSetTest) {
214 for (int j = 0; j < 200; j++) {
215 Clear();
216 for (int i = 0; i < 200; i++) {
217 int32_t begin = rnd_.Rand() % (kTestSize - 1);
218 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1);
219 SetRange(begin, end, rnd_.Rand() & 3);
220 if (HasFailure()) {
221 return;
222 }
223 }
224 }
225 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698