| Index: media/blink/rangemap_unittests.cc
|
| diff --git a/media/blink/rangemap_unittests.cc b/media/blink/rangemap_unittests.cc
|
| new file mode 100644
|
| index 0000000000000000000000000000000000000000..bef21be1360316479394a31d62c8d9101408d1a6
|
| --- /dev/null
|
| +++ b/media/blink/rangemap_unittests.cc
|
| @@ -0,0 +1,103 @@
|
| +// 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;
|
| + }
|
| + }
|
| +
|
| + 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 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, RandomTest) {
|
| + 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;
|
| + }
|
| + }
|
| + }
|
| +}
|
|
|