Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(238)

Unified Diff: media/blink/lru_unittest.cc

Issue 1165903002: Multi reader/writer cache/buffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: one more compile fix Created 5 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: media/blink/lru_unittest.cc
diff --git a/media/blink/lru_unittest.cc b/media/blink/lru_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..bc78d11fc82f38e271541a01a8f7c9e902c4a743
--- /dev/null
+++ b/media/blink/lru_unittest.cc
@@ -0,0 +1,152 @@
+// 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 <list>
+#include <string>
+
+#include "base/logging.h"
+#include "base/strings/stringprintf.h"
+#include "media/blink/lru.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace {
+
+const int kTestSize = 16;
+
+class SimpleLRU {
+ public:
+ void Insert(int x) {
+ DCHECK(!Contains(x));
+ data_.push_back(x);
+ }
+
+ void Remove(int x) {
+ for (std::list<int>::iterator i = data_.begin();
+ i != data_.end();
+ ++i) {
+ if (*i == x) {
+ data_.erase(i);
+ DCHECK(!Contains(x));
+ return;
+ }
+ }
+ LOG(FATAL) << "Remove non-existing element " << x;
+ }
+
+ void Use(int x) {
+ if (Contains(x)) Remove(x);
+ Insert(x);
+ }
+
+ bool Empty() const { return data_.empty(); }
+
+ int Pop() {
+ DCHECK(!Empty());
+ int ret = data_.front();
+ data_.pop_front();
+ return ret;
+ }
+
+ bool Contains(int x) const {
+ for (std::list<int>::const_iterator i = data_.begin();
+ i != data_.end();
+ ++i) {
+ if (*i == x) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ private:
+ std::list<int> data_;
+};
+
+class LRUTest : public testing::Test {
+ public:
+ void Insert(int x) {
+ truth_.Insert(x);
+ testee_.Insert(x);
+ Compare();
+ }
+
+ void Remove(int x) {
+ truth_.Remove(x);
+ testee_.Remove(x);
+ Compare();
+ }
+
+ void Use(int x) {
+ truth_.Use(x);
+ testee_.Use(x);
+ Compare();
+ }
+
+ int Pop() {
+ int truth_value = truth_.Pop();
+ int testee_value = testee_.Pop();
+ EXPECT_EQ(truth_value, testee_value);
+ Compare();
+ return truth_value;
+ }
+
+ void Compare() {
+ for (int i = 0; i < kTestSize; i++) {
+ EXPECT_EQ(truth_.Contains(i), testee_.Contains(i)) << "i =" << i;
+ }
+ EXPECT_EQ(truth_.Empty(), testee_.Empty());
+ }
+
+ bool Empty() const {
+ EXPECT_EQ(truth_.Empty(), testee_.Empty());
+ return truth_.Empty();
+ }
+
+ bool Contains(int i) const {
+ EXPECT_EQ(truth_.Contains(i), testee_.Contains(i));
+ return testee_.Contains(i);
+ }
+
+ void Clear() {
+ while (!Empty()) Pop();
+ }
+
+
+ private:
+ SimpleLRU truth_;
+ media::LRU<int> testee_;
+};
+
+}
+
+TEST_F(LRUTest, RandomTest) {
+ for (int j = 0; j < 100; j++) {
+ Clear();
+ for (int i = 0; i < 1000; i++) {
+ int value = rand() % kTestSize;
DaleCurtis 2015/10/19 21:45:25 Rand tests are generally discouraged: https://code
+ switch (rand() % 3) {
+ case 0:
+ if (!Empty()) Pop();
+ break;
+
+ case 1:
+ Use(value);
+ break;
+
+ case 2:
+ if (Contains(value)) {
+ Remove(value);
+ } else {
+ Insert(value);
+ }
+ break;
+ }
+ if (HasFailure()) {
+ return;
+ }
+ }
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698