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 { | |
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(); | |
29 i != data_.end(); | |
30 ++i) { | |
31 if (*i == x) { | |
32 data_.erase(i); | |
33 DCHECK(!Contains(x)); | |
34 return; | |
35 } | |
36 } | |
37 LOG(FATAL) << "Remove non-existing element " << x; | |
38 } | |
39 | |
40 void Use(int x) { | |
41 if (Contains(x)) Remove(x); | |
42 Insert(x); | |
43 } | |
44 | |
45 bool Empty() const { return data_.empty(); } | |
46 | |
47 int Pop() { | |
48 DCHECK(!Empty()); | |
49 int ret = data_.front(); | |
50 data_.pop_front(); | |
51 return ret; | |
52 } | |
53 | |
54 int Peek() { | |
55 DCHECK(!Empty()); | |
56 return data_.front(); | |
57 } | |
58 | |
59 bool Contains(int x) const { | |
60 for (std::list<int>::const_iterator i = data_.begin(); | |
61 i != data_.end(); | |
62 ++i) { | |
63 if (*i == x) { | |
64 return true; | |
65 } | |
66 } | |
67 return false; | |
68 } | |
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) {} | |
DaleCurtis
2015/10/28 23:06:13
Hmm, I don't understand the point of the random te
hubbe
2015/10/29 17:52:44
I don't think so. The point is not make each test
DaleCurtis
2015/10/29 17:59:05
Seems like varying the test would do that better t
hubbe
2015/10/29 21:45:05
I think the usefulness would be cancelled out by c
DaleCurtis
2015/10/30 00:24:24
That's why I was expecting failures to print the s
hubbe
2015/11/02 22:49:53
The difference is that a hand written tests might
| |
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 for (const auto truth : truth_.data_) { | |
108 EXPECT_EQ(truth, testee.Pop()); | |
109 } | |
110 EXPECT_TRUE(testee.Empty()); | |
111 } | |
112 | |
113 bool Empty() const { | |
114 EXPECT_EQ(truth_.Empty(), testee_.Empty()); | |
115 return truth_.Empty(); | |
116 } | |
117 | |
118 bool Contains(int i) const { | |
119 EXPECT_EQ(truth_.Contains(i), testee_.Contains(i)); | |
120 return testee_.Contains(i); | |
121 } | |
122 | |
123 void Clear() { | |
124 while (!Empty()) 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_; | |
134 SimpleLRU truth_; | |
135 media::LRU<int> testee_; | |
136 }; | |
137 | |
138 } | |
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()) Pop(); | |
211 break; | |
212 | |
213 case 1: | |
214 Use(value); | |
215 break; | |
216 | |
217 case 2: | |
218 if (Contains(value)) { | |
219 Remove(value); | |
220 } else { | |
221 Insert(value); | |
222 } | |
223 break; | |
224 } | |
225 if (HasFailure()) { | |
226 return; | |
227 } | |
228 } | |
229 } | |
230 } | |
OLD | NEW |