OLD | NEW |
---|---|
(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 #include <string> | |
7 | |
8 #include "base/logging.h" | |
9 #include "base/strings/stringprintf.h" | |
10 #include "media/blink/lru.h" | |
11 #include "media/blink/test_random.h" | |
12 #include "testing/gtest/include/gtest/gtest.h" | |
13 | |
14 namespace { | |
15 | |
16 const int kTestSize = 16; | |
17 | |
18 class LRUTest; | |
19 | |
20 class SimpleLRU { | |
DaleCurtis
2015/10/30 00:24:24
Typically reimplementation type tests like this ar
hubbe
2015/11/02 22:49:53
I do tend to go a bit overboard sometimes. :)
This
| |
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 media::LRU<int> testee = testee_; | |
107 EXPECT_EQ(truth_.Size(), testee.Size()); | |
108 for (const auto truth : truth_.data_) { | |
109 EXPECT_EQ(truth, testee.Pop()); | |
110 } | |
111 EXPECT_TRUE(testee.Empty()); | |
112 } | |
113 | |
114 bool Empty() const { | |
115 EXPECT_EQ(truth_.Empty(), testee_.Empty()); | |
116 return truth_.Empty(); | |
117 } | |
118 | |
119 bool Contains(int i) const { | |
120 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i)); | |
121 return testee_.Contains(i); | |
122 } | |
123 | |
124 void Clear() { | |
125 while (!Empty()) | |
126 Pop(); | |
127 } | |
128 | |
129 int Peek() { | |
130 EXPECT_EQ(truth_.Peek(), testee_.Peek()); | |
131 return testee_.Peek(); | |
132 } | |
133 | |
134 protected: | |
135 media::TestRandom rnd_; | |
136 SimpleLRU truth_; | |
137 media::LRU<int> testee_; | |
138 }; | |
139 } | |
140 | |
141 TEST_F(LRUTest, SimpleTest) { | |
142 Insert(1); // 1 | |
143 Insert(2); // 1 2 | |
144 Insert(3); // 1 2 3 | |
145 EXPECT_EQ(1, Peek()); | |
146 EXPECT_EQ(1, Pop()); // 2 3 | |
147 EXPECT_EQ(2, Peek()); | |
148 Use(2); // 3 2 | |
149 EXPECT_EQ(3, Peek()); | |
150 EXPECT_EQ(3, Pop()); // 2 | |
151 EXPECT_EQ(2, Pop()); | |
152 EXPECT_TRUE(Empty()); | |
153 } | |
154 | |
155 TEST_F(LRUTest, UseTest) { | |
156 EXPECT_TRUE(Empty()); | |
157 // Using a value that's not on the LRU adds it. | |
158 Use(3); // 3 | |
159 EXPECT_EQ(3, Peek()); | |
160 Use(5); // 3 5 | |
161 EXPECT_EQ(3, Peek()); | |
162 EXPECT_TRUE(Contains(5)); | |
163 Use(7); // 3 5 7 | |
164 EXPECT_EQ(3, Peek()); | |
165 EXPECT_TRUE(Contains(7)); | |
166 // Using a value that's alraedy on the LRU moves it to the top. | |
167 Use(3); // 5 7 3 | |
168 EXPECT_EQ(5, Peek()); | |
169 EXPECT_TRUE(Contains(5)); | |
170 EXPECT_EQ(5, Pop()); // 7 3 | |
171 EXPECT_FALSE(Contains(5)); | |
172 EXPECT_EQ(7, Peek()); | |
173 EXPECT_TRUE(Contains(7)); | |
174 EXPECT_TRUE(Contains(3)); | |
175 Use(9); // 7 3 9 | |
176 EXPECT_EQ(7, Peek()); | |
177 // Using the same value again has no effect. | |
178 Use(9); // 7 3 9 | |
179 EXPECT_EQ(7, Peek()); | |
180 Use(3); // 7 9 3 | |
181 EXPECT_EQ(7, Pop()); | |
182 EXPECT_EQ(9, Pop()); | |
183 EXPECT_EQ(3, Pop()); | |
184 EXPECT_TRUE(Empty()); | |
185 } | |
186 | |
187 TEST_F(LRUTest, RemoveTest) { | |
188 Insert(5); // 5 | |
189 Insert(4); // 5 4 | |
190 Insert(3); // 5 4 3 | |
191 Insert(2); // 5 4 3 2 | |
192 Insert(1); // 5 4 3 2 1 | |
193 EXPECT_EQ(5, Peek()); | |
194 Remove(5); // 4 3 2 1 | |
195 EXPECT_EQ(4, Peek()); | |
196 Remove(1); // 4 3 2 | |
197 EXPECT_EQ(4, Peek()); | |
198 Remove(3); // 4 2 | |
199 EXPECT_EQ(4, Pop()); | |
200 EXPECT_EQ(2, Pop()); | |
201 EXPECT_TRUE(Empty()); | |
202 } | |
203 | |
204 TEST_F(LRUTest, RandomTest) { | |
205 for (int j = 0; j < 100; j++) { | |
206 Clear(); | |
207 for (int i = 0; i < 1000; i++) { | |
208 int value = rnd_.Rand() % kTestSize; | |
209 switch (rnd_.Rand() % 3) { | |
210 case 0: | |
211 if (!Empty()) | |
212 Pop(); | |
213 break; | |
214 | |
215 case 1: | |
216 Use(value); | |
217 break; | |
218 | |
219 case 2: | |
220 if (Contains(value)) { | |
221 Remove(value); | |
222 } else { | |
223 Insert(value); | |
224 } | |
225 break; | |
226 } | |
227 if (HasFailure()) { | |
228 return; | |
229 } | |
230 } | |
231 } | |
232 } | |
OLD | NEW |