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

Side by Side 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 unified diff | Download patch
OLDNEW
(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 <list>
8 #include <string>
9
10 #include "base/logging.h"
11 #include "base/strings/stringprintf.h"
12 #include "media/blink/lru.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 const int kTestSize = 16;
18
19 class SimpleLRU {
20 public:
21 void Insert(int x) {
22 DCHECK(!Contains(x));
23 data_.push_back(x);
24 }
25
26 void Remove(int x) {
27 for (std::list<int>::iterator i = data_.begin();
28 i != data_.end();
29 ++i) {
30 if (*i == x) {
31 data_.erase(i);
32 DCHECK(!Contains(x));
33 return;
34 }
35 }
36 LOG(FATAL) << "Remove non-existing element " << x;
37 }
38
39 void Use(int x) {
40 if (Contains(x)) Remove(x);
41 Insert(x);
42 }
43
44 bool Empty() const { return data_.empty(); }
45
46 int Pop() {
47 DCHECK(!Empty());
48 int ret = data_.front();
49 data_.pop_front();
50 return ret;
51 }
52
53 bool Contains(int x) const {
54 for (std::list<int>::const_iterator i = data_.begin();
55 i != data_.end();
56 ++i) {
57 if (*i == x) {
58 return true;
59 }
60 }
61 return false;
62 }
63
64 private:
65 std::list<int> data_;
66 };
67
68 class LRUTest : public testing::Test {
69 public:
70 void Insert(int x) {
71 truth_.Insert(x);
72 testee_.Insert(x);
73 Compare();
74 }
75
76 void Remove(int x) {
77 truth_.Remove(x);
78 testee_.Remove(x);
79 Compare();
80 }
81
82 void Use(int x) {
83 truth_.Use(x);
84 testee_.Use(x);
85 Compare();
86 }
87
88 int Pop() {
89 int truth_value = truth_.Pop();
90 int testee_value = testee_.Pop();
91 EXPECT_EQ(truth_value, testee_value);
92 Compare();
93 return truth_value;
94 }
95
96 void Compare() {
97 for (int i = 0; i < kTestSize; i++) {
98 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i)) << "i =" << i;
99 }
100 EXPECT_EQ(truth_.Empty(), testee_.Empty());
101 }
102
103 bool Empty() const {
104 EXPECT_EQ(truth_.Empty(), testee_.Empty());
105 return truth_.Empty();
106 }
107
108 bool Contains(int i) const {
109 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i));
110 return testee_.Contains(i);
111 }
112
113 void Clear() {
114 while (!Empty()) Pop();
115 }
116
117
118 private:
119 SimpleLRU truth_;
120 media::LRU<int> testee_;
121 };
122
123 }
124
125 TEST_F(LRUTest, RandomTest) {
126 for (int j = 0; j < 100; j++) {
127 Clear();
128 for (int i = 0; i < 1000; i++) {
129 int value = rand() % kTestSize;
DaleCurtis 2015/10/19 21:45:25 Rand tests are generally discouraged: https://code
130 switch (rand() % 3) {
131 case 0:
132 if (!Empty()) Pop();
133 break;
134
135 case 1:
136 Use(value);
137 break;
138
139 case 2:
140 if (Contains(value)) {
141 Remove(value);
142 } else {
143 Insert(value);
144 }
145 break;
146 }
147 if (HasFailure()) {
148 return;
149 }
150 }
151 }
152 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698