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

Side by Side Diff: media/blink/lru_unittest.cc

Issue 1427433012: Simple LRU class (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: comments addressed Created 5 years, 1 month 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 <list>
6
7 #include "base/logging.h"
8 #include "media/blink/lru.h"
9 #include "media/blink/test_random.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 const int kTestSize = 16;
xhwang 2015/11/10 19:55:13 nit: TestSize is confusing. This is really the ran
hubbe 2015/11/10 21:50:01 Done.
15
16 class LRUTest;
17
18 class SimpleLRU {
19 public:
20 void Insert(int x) {
21 DCHECK(!Contains(x));
22 data_.push_back(x);
23 }
24
25 void Remove(int x) {
26 for (std::list<int>::iterator i = data_.begin(); i != data_.end(); ++i) {
27 if (*i == x) {
28 data_.erase(i);
29 DCHECK(!Contains(x));
30 return;
31 }
32 }
33 LOG(FATAL) << "Remove non-existing element " << x;
34 }
35
36 void Use(int x) {
37 if (Contains(x))
38 Remove(x);
39 Insert(x);
40 }
41
42 bool Empty() const { return data_.empty(); }
43
44 int Pop() {
45 DCHECK(!Empty());
46 int ret = data_.front();
47 data_.pop_front();
48 return ret;
49 }
50
51 int Peek() {
52 DCHECK(!Empty());
53 return data_.front();
54 }
55
56 bool Contains(int x) const {
57 for (std::list<int>::const_iterator i = data_.begin(); i != data_.end();
58 ++i) {
59 if (*i == x) {
60 return true;
61 }
62 }
63 return false;
64 }
65
66 size_t Size() const { return data_.size(); }
67
68 private:
69 friend class LRUTest;
70 std::list<int> data_;
71 };
72
73 class LRUTest : public testing::Test {
74 public:
75 LRUTest() : rnd_(42) {}
76
77 void Insert(int x) {
78 truth_.Insert(x);
79 testee_.Insert(x);
80 Compare();
81 }
82
83 void Remove(int x) {
84 truth_.Remove(x);
85 testee_.Remove(x);
86 Compare();
87 }
88
89 void Use(int x) {
90 truth_.Use(x);
91 testee_.Use(x);
92 Compare();
93 }
94
95 int Pop() {
96 int truth_value = truth_.Pop();
97 int testee_value = testee_.Pop();
98 EXPECT_EQ(truth_value, testee_value);
99 Compare();
100 return truth_value;
101 }
102
103 void Compare() {
104 media::LRU<int> testee = testee_;
105 EXPECT_EQ(truth_.Size(), testee.Size());
106 for (const auto truth : truth_.data_) {
107 EXPECT_EQ(truth, testee.Pop());
108 }
109 EXPECT_TRUE(testee.Empty());
110 }
111
112 bool Empty() const {
113 EXPECT_EQ(truth_.Empty(), testee_.Empty());
114 return truth_.Empty();
115 }
116
117 bool Contains(int i) const {
118 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i));
119 return testee_.Contains(i);
120 }
121
122 void Clear() {
123 while (!Empty())
124 Pop();
125 }
126
127 int Peek() {
128 EXPECT_EQ(truth_.Peek(), testee_.Peek());
129 return testee_.Peek();
130 }
131
132 protected:
133 media::TestRandom rnd_;
xhwang 2015/11/10 19:55:13 As discussed offline, personally I'd prefer pure r
hubbe 2015/11/10 21:50:01 Acknowledged.
134 SimpleLRU truth_;
135 media::LRU<int> testee_;
136 };
137
138 } // namespace
139
140 TEST_F(LRUTest, SimpleTest) {
141 Insert(1); // 1
142 Insert(2); // 1 2
143 Insert(3); // 1 2 3
144 EXPECT_EQ(1, Peek());
145 EXPECT_EQ(1, Pop()); // 2 3
146 EXPECT_EQ(2, Peek());
147 Use(2); // 3 2
148 EXPECT_EQ(3, Peek());
149 EXPECT_EQ(3, Pop()); // 2
150 EXPECT_EQ(2, Pop());
151 EXPECT_TRUE(Empty());
152 }
153
154 TEST_F(LRUTest, UseTest) {
155 EXPECT_TRUE(Empty());
156 // Using a value that's not on the LRU adds it.
157 Use(3); // 3
158 EXPECT_EQ(3, Peek());
159 Use(5); // 3 5
160 EXPECT_EQ(3, Peek());
161 EXPECT_TRUE(Contains(5));
162 Use(7); // 3 5 7
163 EXPECT_EQ(3, Peek());
164 EXPECT_TRUE(Contains(7));
165 // Using a value that's alraedy on the LRU moves it to the top.
166 Use(3); // 5 7 3
167 EXPECT_EQ(5, Peek());
168 EXPECT_TRUE(Contains(5));
169 EXPECT_EQ(5, Pop()); // 7 3
170 EXPECT_FALSE(Contains(5));
171 EXPECT_EQ(7, Peek());
172 EXPECT_TRUE(Contains(7));
173 EXPECT_TRUE(Contains(3));
174 Use(9); // 7 3 9
175 EXPECT_EQ(7, Peek());
176 // Using the same value again has no effect.
177 Use(9); // 7 3 9
178 EXPECT_EQ(7, Peek());
179 Use(3); // 7 9 3
180 EXPECT_EQ(7, Pop());
181 EXPECT_EQ(9, Pop());
182 EXPECT_EQ(3, Pop());
183 EXPECT_TRUE(Empty());
184 }
185
186 TEST_F(LRUTest, RemoveTest) {
187 Insert(5); // 5
188 Insert(4); // 5 4
189 Insert(3); // 5 4 3
190 Insert(2); // 5 4 3 2
191 Insert(1); // 5 4 3 2 1
192 EXPECT_EQ(5, Peek());
193 Remove(5); // 4 3 2 1
194 EXPECT_EQ(4, Peek());
195 Remove(1); // 4 3 2
196 EXPECT_EQ(4, Peek());
197 Remove(3); // 4 2
198 EXPECT_EQ(4, Pop());
199 EXPECT_EQ(2, Pop());
200 EXPECT_TRUE(Empty());
201 }
202
203 TEST_F(LRUTest, RandomTest) {
204 for (int j = 0; j < 100; j++) {
205 Clear();
206 for (int i = 0; i < 1000; i++) {
207 int value = rnd_.Rand() % kTestSize;
208 switch (rnd_.Rand() % 3) {
209 case 0:
210 if (!Empty())
211 Pop();
212 break;
213
214 case 1:
215 Use(value);
216 break;
217
218 case 2:
219 if (Contains(value)) {
220 Remove(value);
221 } else {
222 Insert(value);
223 }
224 break;
225 }
226 if (HasFailure()) {
227 return;
228 }
229 }
230 }
231 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698