Index: media/blink/rangemap_unittest.cc |
diff --git a/media/blink/rangemap_unittest.cc b/media/blink/rangemap_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..da003ac1a41cb349b07990367609127b42ee3078 |
--- /dev/null |
+++ b/media/blink/rangemap_unittest.cc |
@@ -0,0 +1,143 @@ |
+// Copyright 2015 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include <stdint.h> |
+ |
+#include <string> |
+ |
+#include "base/strings/stringprintf.h" |
+#include "media/blink/rangemap.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+const int kTestSize = 16; |
+ |
+class SimpleRangeMap { |
+ public: |
+ SimpleRangeMap() : data_(kTestSize) { |
+ } |
+ |
+ void IncrementRange(int32_t from, int32_t to, int32_t howmuch) { |
+ for (int32_t i = from; i < to; i++) { |
+ data_[i] += howmuch; |
+ } |
+ } |
+ |
+ void SetRange(int32_t from, int32_t to, int32_t howmuch) { |
+ for (int32_t i = from; i < to; i++) { |
+ data_[i] = howmuch; |
+ } |
+ } |
+ |
+ int32_t operator[](int32_t index) const { |
+ return data_[index]; |
+ } |
+ |
+ private: |
+ std::vector<int32_t> data_; |
+}; |
+ |
+class RangeMapTest : public testing::Test { |
+ public: |
+ void IncrementRange(int32_t from, int32_t to, int32_t howmuch) { |
+ truth_.IncrementRange(from, to, howmuch); |
+ testee_.IncrementRange(from, to, howmuch); |
+ std::string message = base::StringPrintf( |
+ "After [%d - %d) += %d", |
+ from, to, howmuch); |
+ Compare(message); |
+ if (HasFailure()) { |
+ for (int i = 0; i < kTestSize; i++) { |
+ LOG(ERROR) << "Truth[" << i << "] =" << truth_[i]; |
+ } |
+ media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
+ for (i = testee_.map().begin(); |
+ i != testee_.map().end(); |
+ ++i) { |
+ LOG(ERROR) << "Testee[" << i->first << "..] = " << i->second; |
+ } |
+ } |
+ } |
+ |
+ void SetRange(int32_t from, int32_t to, int32_t howmuch) { |
+ truth_.SetRange(from, to, howmuch); |
+ testee_.SetRange(from, to, howmuch); |
+ std::string message = base::StringPrintf( |
+ "After [%d - %d) += %d", |
+ from, to, howmuch); |
+ Compare(message); |
+ if (HasFailure()) { |
+ for (int i = 0; i < kTestSize; i++) { |
+ LOG(ERROR) << "Truth[" << i << "] =" << truth_[i]; |
+ } |
+ media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
+ for (i = testee_.map().begin(); |
+ i != testee_.map().end(); |
+ ++i) { |
+ LOG(ERROR) << "Testee[" << i->first << "..] = " << i->second; |
+ } |
+ } |
+ } |
+ |
+ void Compare(const std::string& message) { |
+ for (int i = 0; i < kTestSize; i++) { |
+ EXPECT_EQ(truth_[i], testee_[i]) |
+ << " i = " << i << " " << message; |
+ } |
+ EXPECT_EQ(testee_[-1], 0) << message; |
+ EXPECT_EQ(testee_[kTestSize], 0) << message; |
+ int32_t prev_ = 0; |
+ media::RangeMap<int32_t, int32_t>::MapType::const_iterator i; |
+ for (i = testee_.map().begin(); |
+ i != testee_.map().end(); |
+ ++i) { |
+ EXPECT_GE(i->first, 0) << message; |
+ EXPECT_LE(i->first, kTestSize) << message; |
+ EXPECT_NE(i->second, prev_) << message; |
+ prev_ = i->second; |
+ } |
+ EXPECT_EQ(prev_, 0) << message; |
+ } |
+ |
+ void Clear() { |
+ for (int j = 0; j < kTestSize; j++) { |
+ IncrementRange(j, j + 1, -truth_[j]); |
+ } |
+ } |
+ |
+ private: |
+ SimpleRangeMap truth_; |
+ media::RangeMap<int32_t, int32_t> testee_; |
+}; |
+ |
+} |
+ |
+TEST_F(RangeMapTest, RandomIncrementTest) { |
+ for (int j = 0; j < 100; j++) { |
+ Clear(); |
+ for (int i = 0; i < 100; i++) { |
+ int32_t begin = rand() % (kTestSize - 1); |
+ int32_t end = begin + 1 + rand() % (kTestSize - begin - 1); |
+ IncrementRange(begin, end, (rand() & 32) ? 1 : -1); |
+ if (HasFailure()) { |
+ return; |
+ } |
+ } |
+ } |
+} |
+ |
+TEST_F(RangeMapTest, RandomSetTest) { |
+ for (int j = 0; j < 100; j++) { |
+ Clear(); |
+ for (int i = 0; i < 100; i++) { |
+ int32_t begin = rand() % (kTestSize - 1); |
+ int32_t end = begin + 1 + rand() % (kTestSize - begin - 1); |
+ SetRange(begin, end, rand() & 3); |
+ if (HasFailure()) { |
+ return; |
+ } |
+ } |
+ } |
+} |