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 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 { |
| 35 return data_[index]; |
| 36 } |
| 37 |
| 38 private: |
| 39 std::vector<int32_t> data_; |
| 40 }; |
| 41 |
| 42 class RangeMapTest : public testing::Test { |
| 43 public: |
| 44 void IncrementRange(int32_t from, int32_t to, int32_t howmuch) { |
| 45 truth_.IncrementRange(from, to, howmuch); |
| 46 testee_.IncrementRange(from, to, howmuch); |
| 47 std::string message = base::StringPrintf( |
| 48 "After [%d - %d) += %d", |
| 49 from, to, howmuch); |
| 50 Compare(message); |
| 51 if (HasFailure()) { |
| 52 for (int i = 0; i < kTestSize; i++) { |
| 53 LOG(ERROR) << "Truth[" << i << "] =" << truth_[i]; |
| 54 } |
| 55 media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
| 56 for (i = testee_.map().begin(); |
| 57 i != testee_.map().end(); |
| 58 ++i) { |
| 59 LOG(ERROR) << "Testee[" << i->first << "..] = " << i->second; |
| 60 } |
| 61 } |
| 62 } |
| 63 |
| 64 void SetRange(int32_t from, int32_t to, int32_t howmuch) { |
| 65 truth_.SetRange(from, to, howmuch); |
| 66 testee_.SetRange(from, to, howmuch); |
| 67 std::string message = base::StringPrintf( |
| 68 "After [%d - %d) += %d", |
| 69 from, to, howmuch); |
| 70 Compare(message); |
| 71 if (HasFailure()) { |
| 72 for (int i = 0; i < kTestSize; i++) { |
| 73 LOG(ERROR) << "Truth[" << i << "] =" << truth_[i]; |
| 74 } |
| 75 media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
| 76 for (i = testee_.map().begin(); |
| 77 i != testee_.map().end(); |
| 78 ++i) { |
| 79 LOG(ERROR) << "Testee[" << i->first << "..] = " << i->second; |
| 80 } |
| 81 } |
| 82 } |
| 83 |
| 84 void Compare(const std::string& message) { |
| 85 for (int i = 0; i < kTestSize; i++) { |
| 86 EXPECT_EQ(truth_[i], testee_[i]) |
| 87 << " i = " << i << " " << message; |
| 88 } |
| 89 EXPECT_EQ(testee_[-1], 0) << message; |
| 90 EXPECT_EQ(testee_[kTestSize], 0) << message; |
| 91 int32_t prev_ = 0; |
| 92 media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
| 93 for (i = testee_.map().begin(); |
| 94 i != testee_.map().end(); |
| 95 ++i) { |
| 96 EXPECT_GE(i->first, 0) << message; |
| 97 EXPECT_LE(i->first, kTestSize) << message; |
| 98 EXPECT_NE(i->second, prev_) << message; |
| 99 prev_ = i->second; |
| 100 } |
| 101 EXPECT_EQ(prev_, 0) << message; |
| 102 } |
| 103 |
| 104 void Clear() { |
| 105 for (int j = 0; j < kTestSize; j++) { |
| 106 IncrementRange(j, j + 1, -truth_[j]); |
| 107 } |
| 108 } |
| 109 |
| 110 private: |
| 111 SimpleRangeMap truth_; |
| 112 media::RangeMap<int32_t, int32_t> testee_; |
| 113 }; |
| 114 |
| 115 } |
| 116 |
| 117 TEST_F(RangeMapTest, RandomIncrementTest) { |
| 118 for (int j = 0; j < 100; j++) { |
| 119 Clear(); |
| 120 for (int i = 0; i < 100; i++) { |
| 121 int32_t begin = rand() % (kTestSize - 1); |
| 122 int32_t end = begin + 1 + rand() % (kTestSize - begin - 1); |
| 123 IncrementRange(begin, end, (rand() & 32) ? 1 : -1); |
| 124 if (HasFailure()) { |
| 125 return; |
| 126 } |
| 127 } |
| 128 } |
| 129 } |
| 130 |
| 131 TEST_F(RangeMapTest, RandomSetTest) { |
| 132 for (int j = 0; j < 100; j++) { |
| 133 Clear(); |
| 134 for (int i = 0; i < 100; i++) { |
| 135 int32_t begin = rand() % (kTestSize - 1); |
| 136 int32_t end = begin + 1 + rand() % (kTestSize - begin - 1); |
| 137 SetRange(begin, end, rand() & 3); |
| 138 if (HasFailure()) { |
| 139 return; |
| 140 } |
| 141 } |
| 142 } |
| 143 } |
OLD | NEW |