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

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: renamed kTestSize 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
« no previous file with comments | « media/blink/lru.h ('k') | media/blink/media_blink.gyp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 // Range of integer used in tests below.
13 // We keep the integers small to get lots of re-use of integers.
14 const int kTestIntRange = 16;
15
16 namespace media {
17
18 class LRUTest;
19
20 class SimpleLRU {
21 public:
22 void Insert(int x) {
23 DCHECK(!Contains(x));
24 data_.push_back(x);
25 }
26
27 void Remove(int x) {
28 for (std::list<int>::iterator i = data_.begin(); i != data_.end(); ++i) {
29 if (*i == x) {
30 data_.erase(i);
31 DCHECK(!Contains(x));
32 return;
33 }
34 }
35 LOG(FATAL) << "Remove non-existing element " << x;
36 }
37
38 void Use(int x) {
39 if (Contains(x))
40 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 int Peek() {
54 DCHECK(!Empty());
55 return data_.front();
56 }
57
58 bool Contains(int x) const {
59 for (std::list<int>::const_iterator i = data_.begin(); i != data_.end();
60 ++i) {
61 if (*i == x) {
62 return true;
63 }
64 }
65 return false;
66 }
67
68 size_t Size() const { return data_.size(); }
69
70 private:
71 friend class LRUTest;
72 std::list<int> data_;
73 };
74
75 class LRUTest : public testing::Test {
76 public:
77 LRUTest() : rnd_(42) {}
78
79 void Insert(int x) {
80 truth_.Insert(x);
81 testee_.Insert(x);
82 Compare();
83 }
84
85 void Remove(int x) {
86 truth_.Remove(x);
87 testee_.Remove(x);
88 Compare();
89 }
90
91 void Use(int x) {
92 truth_.Use(x);
93 testee_.Use(x);
94 Compare();
95 }
96
97 int Pop() {
98 int truth_value = truth_.Pop();
99 int testee_value = testee_.Pop();
100 EXPECT_EQ(truth_value, testee_value);
101 Compare();
102 return truth_value;
103 }
104
105 void Compare() {
106 EXPECT_EQ(truth_.Size(), testee_.Size());
107 auto testee_iterator = testee_.lru_.rbegin();
108 for (const auto truth : truth_.data_) {
109 EXPECT_TRUE(testee_iterator != testee_.lru_.rend());
110 EXPECT_EQ(truth, *testee_iterator);
111 ++testee_iterator;
112 }
113 EXPECT_TRUE(testee_iterator == testee_.lru_.rend());
114 }
115
116 bool Empty() const {
117 EXPECT_EQ(truth_.Empty(), testee_.Empty());
118 return truth_.Empty();
119 }
120
121 bool Contains(int i) const {
122 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i));
123 return testee_.Contains(i);
124 }
125
126 void Clear() {
127 while (!Empty())
128 Pop();
129 }
130
131 int Peek() {
132 EXPECT_EQ(truth_.Peek(), testee_.Peek());
133 return testee_.Peek();
134 }
135
136 protected:
137 media::TestRandom rnd_;
138 SimpleLRU truth_;
139 media::LRU<int> testee_;
140 };
141
142 TEST_F(LRUTest, SimpleTest) {
143 Insert(1); // 1
144 Insert(2); // 1 2
145 Insert(3); // 1 2 3
146 EXPECT_EQ(1, Peek());
147 EXPECT_EQ(1, Pop()); // 2 3
148 EXPECT_EQ(2, Peek());
149 Use(2); // 3 2
150 EXPECT_EQ(3, Peek());
151 EXPECT_EQ(3, Pop()); // 2
152 EXPECT_EQ(2, Pop());
153 EXPECT_TRUE(Empty());
154 }
155
156 TEST_F(LRUTest, UseTest) {
157 EXPECT_TRUE(Empty());
158 // Using a value that's not on the LRU adds it.
159 Use(3); // 3
160 EXPECT_EQ(3, Peek());
161 Use(5); // 3 5
162 EXPECT_EQ(3, Peek());
163 EXPECT_TRUE(Contains(5));
164 Use(7); // 3 5 7
165 EXPECT_EQ(3, Peek());
166 EXPECT_TRUE(Contains(7));
167 // Using a value that's alraedy on the LRU moves it to the top.
168 Use(3); // 5 7 3
169 EXPECT_EQ(5, Peek());
170 EXPECT_TRUE(Contains(5));
171 EXPECT_EQ(5, Pop()); // 7 3
172 EXPECT_FALSE(Contains(5));
173 EXPECT_EQ(7, Peek());
174 EXPECT_TRUE(Contains(7));
175 EXPECT_TRUE(Contains(3));
176 Use(9); // 7 3 9
177 EXPECT_EQ(7, Peek());
178 // Using the same value again has no effect.
179 Use(9); // 7 3 9
180 EXPECT_EQ(7, Peek());
181 Use(3); // 7 9 3
182 EXPECT_EQ(7, Pop());
183 EXPECT_EQ(9, Pop());
184 EXPECT_EQ(3, Pop());
185 EXPECT_TRUE(Empty());
186 }
187
188 TEST_F(LRUTest, RemoveTest) {
189 Insert(5); // 5
190 Insert(4); // 5 4
191 Insert(3); // 5 4 3
192 Insert(2); // 5 4 3 2
193 Insert(1); // 5 4 3 2 1
194 EXPECT_EQ(5, Peek());
195 Remove(5); // 4 3 2 1
196 EXPECT_EQ(4, Peek());
197 Remove(1); // 4 3 2
198 EXPECT_EQ(4, Peek());
199 Remove(3); // 4 2
200 EXPECT_EQ(4, Pop());
201 EXPECT_EQ(2, Pop());
202 EXPECT_TRUE(Empty());
203 }
204
205 TEST_F(LRUTest, RandomTest) {
206 for (int j = 0; j < 100; j++) {
207 Clear();
208 for (int i = 0; i < 1000; i++) {
209 int value = rnd_.Rand() % kTestIntRange;
210 switch (rnd_.Rand() % 3) {
211 case 0:
212 if (!Empty())
213 Pop();
214 break;
215
216 case 1:
217 Use(value);
218 break;
219
220 case 2:
221 if (Contains(value)) {
222 Remove(value);
223 } else {
224 Insert(value);
225 }
226 break;
227 }
228 if (HasFailure()) {
229 return;
230 }
231 }
232 }
233 }
234
235 } // namespace media
OLDNEW
« no previous file with comments | « media/blink/lru.h ('k') | media/blink/media_blink.gyp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698