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