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

Side by Side Diff: chrome/common/instant_restricted_id_cache_unittest.cc

Issue 12498002: InstantExtended: Adding InstantRestrictedIDCache. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Merging conflicts. Created 7 years, 9 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 | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright 2013 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 <string>
6 #include <utility>
7 #include <vector>
8 #include "chrome/common/instant_restricted_id_cache.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 struct TestData {
14 TestData() {}
15 explicit TestData(const std::string& i_value) : value(i_value) {}
16
17 bool operator==(const TestData& rhs) const {
18 return rhs.value == value;
19 }
20
21 std::string value;
22 };
23
24 // For printing failures nicely.
25 void PrintTo(const TestData& data, std::ostream* os) {
26 *os << data.value;
27 }
28
29 } // namespace
30
31 typedef testing::Test InstantRestrictedIDCacheTest;
32 typedef InstantRestrictedIDCache<TestData>::ItemIDPair ItemIDPair;
33
34 TEST_F(InstantRestrictedIDCacheTest, AutoIDGeneration) {
35 InstantRestrictedIDCache<TestData> cache(7);
36 EXPECT_EQ(0u, cache.cache_.size());
37 EXPECT_EQ(0, cache.last_restricted_id_);
38
39 // Check first addition.
40 std::vector<TestData> input1;
41 input1.push_back(TestData("A"));
42 input1.push_back(TestData("B"));
43 input1.push_back(TestData("C"));
44 cache.AddItems(input1);
45 EXPECT_EQ(3u, cache.cache_.size());
46 EXPECT_EQ(3, cache.last_restricted_id_);
47
48 std::vector<ItemIDPair> output;
49 cache.GetCurrentItems(&output);
50 EXPECT_EQ(3u, output.size());
51 for (int64 i = 0; i < 3; ++i) {
52 EXPECT_EQ(i + 1, output[i].first);
53 EXPECT_EQ(input1[i], output[i].second);
54 }
55
56 TestData t;
57 EXPECT_FALSE(cache.GetItemWithRestrictedID(4, &t));
58 EXPECT_TRUE(cache.GetItemWithRestrictedID(3, &t));
59 EXPECT_EQ(input1[2], t);
60
61 // Add more items, no overflow.
62 std::vector<TestData> input2;
63 input2.push_back(TestData("D"));
64 input2.push_back(TestData("E"));
65 cache.AddItems(input2);
66 EXPECT_EQ(5u, cache.cache_.size());
67 EXPECT_EQ(5, cache.last_restricted_id_);
68
69 output.clear();
70 cache.GetCurrentItems(&output);
71 EXPECT_EQ(2u, output.size());
72 for (int64 i = 0; i < 2; ++i) {
73 EXPECT_EQ(i + 4, output[i].first);
74 EXPECT_EQ(input2[i], output[i].second);
75 }
76
77 EXPECT_FALSE(cache.GetItemWithRestrictedID(6, &t));
78 EXPECT_TRUE(cache.GetItemWithRestrictedID(3, &t));
79 EXPECT_EQ(input1[2], t);
80 EXPECT_TRUE(cache.GetItemWithRestrictedID(5, &t));
81 EXPECT_EQ(input2[1], t);
82
83 // Add another set, overflows.
84 std::vector<TestData> input3;
85 input3.push_back(TestData("F"));
86 input3.push_back(TestData("G"));
87 input3.push_back(TestData("H"));
88 input3.push_back(TestData("I"));
89 cache.AddItems(input3);
90 EXPECT_EQ(7u, cache.cache_.size());
91 EXPECT_EQ(9, cache.last_restricted_id_);
92
93 output.clear();
94 cache.GetCurrentItems(&output);
95 EXPECT_EQ(4u, output.size());
96 for (int64 i = 0; i < 3; ++i) {
97 EXPECT_EQ(i + 6, output[i].first);
98 EXPECT_EQ(input3[i], output[i].second);
99 }
100
101 EXPECT_FALSE(cache.GetItemWithRestrictedID(1, &t));
102 EXPECT_FALSE(cache.GetItemWithRestrictedID(2, &t));
103 EXPECT_TRUE(cache.GetItemWithRestrictedID(3, &t));
104 EXPECT_EQ(input1[2], t);
105 EXPECT_TRUE(cache.GetItemWithRestrictedID(5, &t));
106 EXPECT_EQ(input2[1], t);
107 EXPECT_TRUE(cache.GetItemWithRestrictedID(7, &t));
108 EXPECT_EQ(input3[1], t);
109 }
110
111 TEST_F(InstantRestrictedIDCacheTest, ManualIDGeneration) {
112 InstantRestrictedIDCache<TestData> cache(5);
113 EXPECT_EQ(0u, cache.cache_.size());
114 EXPECT_EQ(0, cache.last_restricted_id_);
115
116 // Check first addition.
117 std::vector<ItemIDPair> input1;
118 input1.push_back(std::make_pair(1, TestData("A")));
119 input1.push_back(std::make_pair(2, TestData("B")));
120 input1.push_back(std::make_pair(4, TestData("C")));
121 cache.AddItemsWithRestrictedID(input1);
122 EXPECT_EQ(3u, cache.cache_.size());
123 EXPECT_EQ(4, cache.last_restricted_id_);
124
125 std::vector<ItemIDPair> output;
126 cache.GetCurrentItems(&output);
127 EXPECT_EQ(3u, output.size());
128 for (int i = 0; i < 3; ++i) {
129 EXPECT_EQ(input1[i].first, output[i].first);
130 EXPECT_EQ(input1[i].second, output[i].second);
131 }
132
133 TestData t;
134 EXPECT_FALSE(cache.GetItemWithRestrictedID(3, &t));
135 EXPECT_TRUE(cache.GetItemWithRestrictedID(4, &t));
136 EXPECT_EQ(input1[2].second, t);
137
138
139 // Add more items, one with same rid, no overflow.
140 std::vector<ItemIDPair> input2;
141 input2.push_back(std::make_pair(4, TestData("D")));
142 input2.push_back(std::make_pair(7, TestData("E")));
143 cache.AddItemsWithRestrictedID(input2);
144 EXPECT_EQ(5u, cache.cache_.size());
145 EXPECT_EQ(7, cache.last_restricted_id_);
146
147 output.clear();
148 cache.GetCurrentItems(&output);
149 EXPECT_EQ(2u, output.size());
150 for (int i = 0; i < 2; ++i) {
151 EXPECT_EQ(input2[i].first, output[i].first);
152 EXPECT_EQ(input2[i].second, output[i].second);
153 }
154
155 EXPECT_FALSE(cache.GetItemWithRestrictedID(6, &t));
156 EXPECT_TRUE(cache.GetItemWithRestrictedID(2, &t));
157 EXPECT_EQ(input1[1].second, t);
158 EXPECT_TRUE(cache.GetItemWithRestrictedID(4, &t));
159 EXPECT_EQ(input2[0].second, t);
160 EXPECT_TRUE(cache.GetItemWithRestrictedID(7, &t));
161 EXPECT_EQ(input2[1].second, t);
162
163 // Add another set, duplicate rids, overflows.
164 std::vector<ItemIDPair> input3;
165 input3.push_back(std::make_pair(1, TestData("F")));
166 input3.push_back(std::make_pair(7, TestData("G")));
167 input3.push_back(std::make_pair(9, TestData("H")));
168 cache.AddItemsWithRestrictedID(input3);
169 EXPECT_EQ(5u, cache.cache_.size());
170 EXPECT_EQ(9, cache.last_restricted_id_);
171
172 output.clear();
173 cache.GetCurrentItems(&output);
174 EXPECT_EQ(3u, output.size());
175 for (int64 i = 0; i < 2; ++i) {
176 EXPECT_EQ(input3[i].first, output[i].first);
177 EXPECT_EQ(input3[i].second, output[i].second);
178 }
179
180 EXPECT_TRUE(cache.GetItemWithRestrictedID(1, &t));
181 EXPECT_EQ(input3[0].second, t);
182 EXPECT_FALSE(cache.GetItemWithRestrictedID(2, &t));
183 EXPECT_FALSE(cache.GetItemWithRestrictedID(3, &t));
184 EXPECT_TRUE(cache.GetItemWithRestrictedID(4, &t));
185 EXPECT_EQ(input2[0].second, t);
186 EXPECT_FALSE(cache.GetItemWithRestrictedID(6, &t));
187 EXPECT_TRUE(cache.GetItemWithRestrictedID(7, &t));
188 EXPECT_EQ(input3[1].second, t);
189 EXPECT_FALSE(cache.GetItemWithRestrictedID(8, &t));
190 EXPECT_TRUE(cache.GetItemWithRestrictedID(9, &t));
191 EXPECT_EQ(input3[2].second, t);
192 }
193
194 TEST_F(InstantRestrictedIDCacheTest, MixIDGeneration) {
195 InstantRestrictedIDCache<TestData> cache(5);
196 EXPECT_EQ(0u, cache.cache_.size());
197 EXPECT_EQ(0, cache.last_restricted_id_);
198
199 // Add some items with manually assigned ids.
200 std::vector<ItemIDPair> input1;
201 input1.push_back(std::make_pair(1, TestData("A")));
202 input1.push_back(std::make_pair(2, TestData("B")));
203 input1.push_back(std::make_pair(4, TestData("C")));
204 cache.AddItemsWithRestrictedID(input1);
205 EXPECT_EQ(3u, cache.cache_.size());
206 EXPECT_EQ(4, cache.last_restricted_id_);
207
208 std::vector<ItemIDPair> output;
209 cache.GetCurrentItems(&output);
210 EXPECT_EQ(3u, output.size());
211 for (int i = 0; i < 3; ++i) {
212 EXPECT_EQ(input1[i].first, output[i].first);
213 EXPECT_EQ(input1[i].second, output[i].second);
214 }
215
216 TestData t;
217 EXPECT_FALSE(cache.GetItemWithRestrictedID(3, &t));
218 EXPECT_TRUE(cache.GetItemWithRestrictedID(4, &t));
219 EXPECT_EQ(input1[2].second, t);
220
221 // Add items with auto id generation.
222 std::vector<TestData> input2;
223 input2.push_back(TestData("D"));
224 input2.push_back(TestData("E"));
225 cache.AddItems(input2);
226 EXPECT_EQ(5u, cache.cache_.size());
227 EXPECT_EQ(6, cache.last_restricted_id_);
228
229 output.clear();
230 cache.GetCurrentItems(&output);
231 EXPECT_EQ(2u, output.size());
232 for (int64 i = 0; i < 2; ++i) {
233 EXPECT_EQ(i + 5, output[i].first);
234 EXPECT_EQ(input2[i], output[i].second);
235 }
236
237 EXPECT_FALSE(cache.GetItemWithRestrictedID(3, &t));
238 EXPECT_TRUE(cache.GetItemWithRestrictedID(2, &t));
239 EXPECT_EQ(input1[1].second, t);
240 EXPECT_TRUE(cache.GetItemWithRestrictedID(4, &t));
241 EXPECT_EQ(input1[2].second, t);
242 EXPECT_TRUE(cache.GetItemWithRestrictedID(5, &t));
243 EXPECT_EQ(input2[0], t);
244 EXPECT_TRUE(cache.GetItemWithRestrictedID(6, &t));
245 EXPECT_EQ(input2[1], t);
246 EXPECT_FALSE(cache.GetItemWithRestrictedID(7, &t));
palmer 2013/03/19 21:48:30 Could you please add some tests that get and set v
Shishir 2013/03/19 22:20:20 Done.
247
248 // Add manually assigned ids again.
249 std::vector<ItemIDPair> input3;
250 input3.push_back(std::make_pair(1, TestData("F")));
251 input3.push_back(std::make_pair(5, TestData("G")));
252 input3.push_back(std::make_pair(6, TestData("H")));
253 cache.AddItemsWithRestrictedID(input3);
254 EXPECT_EQ(5u, cache.cache_.size());
255 EXPECT_EQ(6, cache.last_restricted_id_);
256
257 output.clear();
258 cache.GetCurrentItems(&output);
259 EXPECT_EQ(3u, output.size());
260 for (int64 i = 0; i < 2; ++i) {
261 EXPECT_EQ(input3[i].first, output[i].first);
262 EXPECT_EQ(input3[i].second, output[i].second);
263 }
264
265 EXPECT_TRUE(cache.GetItemWithRestrictedID(1, &t));
266 EXPECT_EQ(input3[0].second, t);
267 EXPECT_FALSE(cache.GetItemWithRestrictedID(2, &t));
268 EXPECT_FALSE(cache.GetItemWithRestrictedID(4, &t));
269 EXPECT_TRUE(cache.GetItemWithRestrictedID(5, &t));
270 EXPECT_EQ(input3[1].second, t);
271 EXPECT_TRUE(cache.GetItemWithRestrictedID(6, &t));
272 EXPECT_EQ(input3[2].second, t);
273 EXPECT_FALSE(cache.GetItemWithRestrictedID(7, &t));
274 }
275
276 TEST_F(InstantRestrictedIDCacheTest, AdditionOverflow) {
277 InstantRestrictedIDCache<TestData> cache(2);
278
279 EXPECT_EQ(0u, cache.cache_.size());
280 EXPECT_EQ(0, cache.last_restricted_id_);
281
282 // Add too many.
283 std::vector<TestData> input1;
284 input1.push_back(TestData("A"));
285 input1.push_back(TestData("B"));
286 input1.push_back(TestData("C"));
287 cache.AddItems(input1);
288 EXPECT_EQ(2u, cache.cache_.size());
289 EXPECT_EQ(3, cache.last_restricted_id_);
290
291 std::vector<ItemIDPair> output;
292 cache.GetCurrentItems(&output);
293 EXPECT_EQ(2u, output.size());
294 for (int64 i = 0; i < 2; ++i) {
295 EXPECT_EQ(i + 2, output[i].first);
296 EXPECT_EQ(input1[i + 1], output[i].second);
297 }
298
299 TestData t;
300 EXPECT_FALSE(cache.GetItemWithRestrictedID(1, &t));
301 EXPECT_FALSE(cache.GetItemWithRestrictedID(4, &t));
302 EXPECT_TRUE(cache.GetItemWithRestrictedID(3, &t));
303 EXPECT_EQ(input1[2], t);
304 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698