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/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 interval 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 intervalmap, 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 IncrementInterval(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 SetInterval(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 IncrementInterval(int32_t from, int32_t to, int32_t how_much) { |
| 49 truth_.IncrementInterval(from, to, how_much); |
| 50 testee_.IncrementInterval(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 SetInterval(int32_t from, int32_t to, int32_t how_much) { |
| 57 truth_.SetInterval(from, to, how_much); |
| 58 testee_.SetInterval(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_interval = 0; |
| 74 int32_t num_intervals = 0; |
| 75 for (const auto& r : testee_) { |
| 76 num_intervals++; |
| 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_interval, 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_interval = 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) << "Interval: " << 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 IncrementInterval(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 IncrementInterval(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 IncrementInterval(3, 7, -4); |
| 125 EXPECT_TRUE(testee_.empty()); |
| 126 } |
| 127 |
| 128 TEST_F(IntervalMapTest, SimpleIncrementTest) { |
| 129 IncrementInterval(3, 7, 1); |
| 130 IncrementInterval(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 SetInterval(3, 12, 0); |
| 139 EXPECT_TRUE(testee_.empty()); |
| 140 } |
| 141 |
| 142 TEST_F(IntervalMapTest, IncrementJoinIntervalsTest) { |
| 143 IncrementInterval(3, 5, 1); |
| 144 IncrementInterval(7, 8, 1); |
| 145 IncrementInterval(9, 11, 1); |
| 146 IncrementInterval(5, 7, 1); |
| 147 IncrementInterval(8, 9, 1); |
| 148 auto i = testee_.find(5); |
| 149 EXPECT_EQ(3, i.interval_begin()); |
| 150 EXPECT_EQ(11, i.interval_end()); |
| 151 EXPECT_EQ(1, i.value()); |
| 152 } |
| 153 |
| 154 TEST_F(IntervalMapTest, SetJoinIntervalsTest) { |
| 155 SetInterval(3, 5, 1); |
| 156 SetInterval(7, 8, 1); |
| 157 SetInterval(9, 11, 1); |
| 158 SetInterval(5, 9, 1); // overwrites one interval |
| 159 auto i = testee_.find(5); |
| 160 EXPECT_EQ(3, i.interval_begin()); |
| 161 EXPECT_EQ(11, i.interval_end()); |
| 162 EXPECT_EQ(1, i.value()); |
| 163 } |
| 164 |
| 165 TEST_F(IntervalMapTest, FindTest) { |
| 166 IncrementInterval(5, 6, 1); |
| 167 IncrementInterval(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.interval_begin()); |
| 172 EXPECT_EQ(1, i.interval_end()); |
| 173 EXPECT_EQ(0, i.value()); |
| 174 i = testee_.find(4); |
| 175 EXPECT_EQ(1, i.interval_begin()); |
| 176 EXPECT_EQ(5, i.interval_end()); |
| 177 EXPECT_EQ(2, i.value()); |
| 178 i = testee_.find(5); |
| 179 EXPECT_EQ(5, i.interval_begin()); |
| 180 EXPECT_EQ(6, i.interval_end()); |
| 181 EXPECT_EQ(3, i.value()); |
| 182 i = testee_.find(6); |
| 183 EXPECT_EQ(6, i.interval_begin()); |
| 184 EXPECT_EQ(10, i.interval_end()); |
| 185 EXPECT_EQ(2, i.value()); |
| 186 i = testee_.find(9); |
| 187 EXPECT_EQ(6, i.interval_begin()); |
| 188 EXPECT_EQ(10, i.interval_end()); |
| 189 EXPECT_EQ(2, i.value()); |
| 190 i = testee_.find(10); |
| 191 EXPECT_EQ(10, i.interval_begin()); |
| 192 EXPECT_EQ(max_value, i.interval_end()); |
| 193 EXPECT_EQ(0, i.value()); |
| 194 } |
| 195 |
| 196 TEST_F(IntervalMapTest, MinMaxInt) { |
| 197 int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 198 int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 199 |
| 200 // Change a single value at minint |
| 201 testee_.IncrementInterval(min_value, min_value + 1, 7); |
| 202 EXPECT_EQ(7, testee_[min_value]); |
| 203 EXPECT_EQ(0, testee_[min_value + 1]); |
| 204 auto i = testee_.find(0); |
| 205 EXPECT_EQ(min_value + 1, i.interval_begin()); |
| 206 EXPECT_EQ(max_value, i.interval_end()); |
| 207 EXPECT_EQ(0, i.value()); |
| 208 --i; |
| 209 EXPECT_TRUE(i == testee_.find(min_value)); |
| 210 EXPECT_EQ(min_value, i.interval_begin()); |
| 211 EXPECT_EQ(min_value + 1, i.interval_end()); |
| 212 EXPECT_EQ(7, i.value()); |
| 213 testee_.clear(); |
| 214 |
| 215 // Change a single value at maxint |
| 216 // Note that we don't actually have a way to represent a range |
| 217 // that includes maxint as the end of the interval is non-inclusive. |
| 218 testee_.IncrementInterval(max_value - 1, max_value, 7); |
| 219 EXPECT_EQ(7, testee_[max_value - 1]); |
| 220 EXPECT_EQ(0, testee_[max_value - 2]); |
| 221 i = testee_.find(0); |
| 222 EXPECT_EQ(min_value, i.interval_begin()); |
| 223 EXPECT_EQ(max_value - 1, i.interval_end()); |
| 224 EXPECT_EQ(0, i.value()); |
| 225 ++i; |
| 226 EXPECT_TRUE(i == testee_.find(max_value - 1)); |
| 227 EXPECT_EQ(max_value - 1, i.interval_begin()); |
| 228 EXPECT_EQ(max_value, i.interval_end()); |
| 229 EXPECT_EQ(7, i.value()); |
| 230 |
| 231 testee_.clear(); |
| 232 |
| 233 // Change entire range (almost) |
| 234 testee_.IncrementInterval(min_value, max_value, 17); |
| 235 EXPECT_EQ(17, testee_[min_value]); |
| 236 EXPECT_EQ(17, testee_[0]); |
| 237 EXPECT_EQ(17, testee_[max_value - 1]); |
| 238 i = testee_.find(0); |
| 239 EXPECT_EQ(min_value, i.interval_begin()); |
| 240 EXPECT_EQ(max_value, i.interval_end()); |
| 241 EXPECT_EQ(17, i.value()); |
| 242 EXPECT_TRUE(i == testee_.find(max_value - 1)); |
| 243 EXPECT_TRUE(i == testee_.find(min_value)); |
| 244 } |
| 245 |
| 246 TEST_F(IntervalMapTest, RandomIncrementTest) { |
| 247 for (int j = 0; j < 200; j++) { |
| 248 Clear(); |
| 249 for (int i = 0; i < 200; i++) { |
| 250 int32_t begin = rnd_.Rand() % (kTestSize - 1); |
| 251 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1); |
| 252 IncrementInterval(begin, end, (rnd_.Rand() & 32) ? 1 : -1); |
| 253 if (HasFailure()) { |
| 254 return; |
| 255 } |
| 256 } |
| 257 } |
| 258 } |
| 259 |
| 260 TEST_F(IntervalMapTest, RandomSetTest) { |
| 261 for (int j = 0; j < 200; j++) { |
| 262 Clear(); |
| 263 for (int i = 0; i < 200; i++) { |
| 264 int32_t begin = rnd_.Rand() % (kTestSize - 1); |
| 265 int32_t end = begin + 1 + rnd_.Rand() % (kTestSize - begin - 1); |
| 266 SetInterval(begin, end, rnd_.Rand() & 3); |
| 267 if (HasFailure()) { |
| 268 return; |
| 269 } |
| 270 } |
| 271 } |
| 272 } |
OLD | NEW |