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/rangemap.h" |
| 11 #include "testing/gtest/include/gtest/gtest.h" |
| 12 |
| 13 namespace { |
| 14 |
| 15 const int kTestSize = 16; |
| 16 |
| 17 class SimpleRangeMap { |
| 18 public: |
| 19 SimpleRangeMap() : data_(kTestSize) { |
| 20 } |
| 21 |
| 22 void IncrementRange(int32_t from, int32_t to, int32_t howmuch) { |
| 23 for (int32_t i = from; i < to; i++) { |
| 24 data_[i] += howmuch; |
| 25 } |
| 26 } |
| 27 |
| 28 int32_t operator[](int32_t index) const { |
| 29 return data_[index]; |
| 30 } |
| 31 |
| 32 private: |
| 33 std::vector<int32_t> data_; |
| 34 }; |
| 35 |
| 36 class RangeMapTest : public testing::Test { |
| 37 public: |
| 38 void IncrementRange(int32_t from, int32_t to, int32_t howmuch) { |
| 39 truth_.IncrementRange(from, to, howmuch); |
| 40 testee_.IncrementRange(from, to, howmuch); |
| 41 std::string message = base::StringPrintf( |
| 42 "After [%d - %d) += %d", |
| 43 from, to, howmuch); |
| 44 Compare(message); |
| 45 if (HasFailure()) { |
| 46 for (int i = 0; i < kTestSize; i++) { |
| 47 LOG(ERROR) << "Truth[" << i << "] =" << truth_[i]; |
| 48 } |
| 49 media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
| 50 for (i = testee_.map().begin(); |
| 51 i != testee_.map().end(); |
| 52 ++i) { |
| 53 LOG(ERROR) << "Testee[" << i->first << "..] = " << i->second; |
| 54 } |
| 55 } |
| 56 } |
| 57 |
| 58 void Compare(const std::string& message) { |
| 59 for (int i = 0; i < kTestSize; i++) { |
| 60 EXPECT_EQ(truth_[i], testee_[i]) |
| 61 << " i = " << i << " " << message; |
| 62 } |
| 63 EXPECT_EQ(testee_[-1], 0) << message; |
| 64 EXPECT_EQ(testee_[kTestSize], 0) << message; |
| 65 int32_t prev_ = 0; |
| 66 media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
| 67 for (i = testee_.map().begin(); |
| 68 i != testee_.map().end(); |
| 69 ++i) { |
| 70 EXPECT_GE(i->first, 0) << message; |
| 71 EXPECT_LE(i->first, kTestSize) << message; |
| 72 EXPECT_NE(i->second, prev_) << message; |
| 73 prev_ = i->second; |
| 74 } |
| 75 EXPECT_EQ(prev_, 0) << message; |
| 76 } |
| 77 |
| 78 void Clear() { |
| 79 for (int j = 0; j < kTestSize; j++) { |
| 80 IncrementRange(j, j + 1, -truth_[j]); |
| 81 } |
| 82 } |
| 83 |
| 84 private: |
| 85 SimpleRangeMap truth_; |
| 86 media::RangeMap<int32_t, int32_t> testee_; |
| 87 }; |
| 88 |
| 89 } |
| 90 |
| 91 TEST_F(RangeMapTest, RandomTest) { |
| 92 for (int j = 0; j < 100; j++) { |
| 93 Clear(); |
| 94 for (int i = 0; i < 100; i++) { |
| 95 int32_t begin = rand() % (kTestSize - 1); |
| 96 int32_t end = begin + 1 + rand() % (kTestSize - begin - 1); |
| 97 IncrementRange(begin, end, (rand() & 32) ? 1 : -1); |
| 98 if (HasFailure()) { |
| 99 return; |
| 100 } |
| 101 } |
| 102 } |
| 103 } |
OLD | NEW |