OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
| 5 #include <stdint.h> |
| 6 |
| 7 #include <limits> |
5 #include <string> | 8 #include <string> |
6 #include <utility> | 9 #include <utility> |
7 #include <vector> | 10 #include <vector> |
8 | 11 |
9 #include "chrome/renderer/instant_restricted_id_cache.h" | 12 #include "chrome/renderer/instant_restricted_id_cache.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 13 #include "testing/gtest/include/gtest/gtest.h" |
11 | 14 |
12 namespace { | 15 namespace { |
13 | 16 |
14 struct TestData { | 17 struct TestData { |
(...skipping 177 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
192 } | 195 } |
193 | 196 |
194 TEST_F(InstantRestrictedIDCacheTest, CrazyIDGeneration) { | 197 TEST_F(InstantRestrictedIDCacheTest, CrazyIDGeneration) { |
195 InstantRestrictedIDCache<TestData> cache(4); | 198 InstantRestrictedIDCache<TestData> cache(4); |
196 EXPECT_EQ(0u, cache.cache_.size()); | 199 EXPECT_EQ(0u, cache.cache_.size()); |
197 EXPECT_EQ(0, cache.last_restricted_id_); | 200 EXPECT_EQ(0, cache.last_restricted_id_); |
198 | 201 |
199 // Check first addition. | 202 // Check first addition. |
200 std::vector<ItemIDPair> input1; | 203 std::vector<ItemIDPair> input1; |
201 input1.push_back(std::make_pair(0, TestData("A"))); | 204 input1.push_back(std::make_pair(0, TestData("A"))); |
202 input1.push_back(std::make_pair(kint32max, TestData("B"))); | 205 input1.push_back( |
| 206 std::make_pair(std::numeric_limits<int32_t>::max(), TestData("B"))); |
203 input1.push_back(std::make_pair(-100, TestData("C"))); | 207 input1.push_back(std::make_pair(-100, TestData("C"))); |
204 cache.AddItemsWithRestrictedID(input1); | 208 cache.AddItemsWithRestrictedID(input1); |
205 EXPECT_EQ(3u, cache.cache_.size()); | 209 EXPECT_EQ(3u, cache.cache_.size()); |
206 EXPECT_EQ(kint32max, cache.last_restricted_id_); | 210 EXPECT_EQ(std::numeric_limits<int32_t>::max(), cache.last_restricted_id_); |
207 | 211 |
208 std::vector<ItemIDPair> output; | 212 std::vector<ItemIDPair> output; |
209 cache.GetCurrentItems(&output); | 213 cache.GetCurrentItems(&output); |
210 EXPECT_EQ(3u, output.size()); | 214 EXPECT_EQ(3u, output.size()); |
211 for (int i = 0; i < 3; ++i) { | 215 for (int i = 0; i < 3; ++i) { |
212 EXPECT_EQ(input1[i].first, output[i].first); | 216 EXPECT_EQ(input1[i].first, output[i].first); |
213 EXPECT_EQ(input1[i].second, output[i].second); | 217 EXPECT_EQ(input1[i].second, output[i].second); |
214 } | 218 } |
215 | 219 |
216 TestData t; | 220 TestData t; |
217 EXPECT_FALSE(cache.GetItemWithRestrictedID(1, &t)); | 221 EXPECT_FALSE(cache.GetItemWithRestrictedID(1, &t)); |
218 EXPECT_TRUE(cache.GetItemWithRestrictedID(kint32max, &t)); | 222 EXPECT_TRUE( |
| 223 cache.GetItemWithRestrictedID(std::numeric_limits<int32_t>::max(), &t)); |
219 EXPECT_EQ(input1[1].second, t); | 224 EXPECT_EQ(input1[1].second, t); |
220 EXPECT_TRUE(cache.GetItemWithRestrictedID(-100, &t)); | 225 EXPECT_TRUE(cache.GetItemWithRestrictedID(-100, &t)); |
221 EXPECT_EQ(input1[2].second, t); | 226 EXPECT_EQ(input1[2].second, t); |
222 | 227 |
223 // Add more items, one with same rid, no overflow. | 228 // Add more items, one with same rid, no overflow. |
224 std::vector<ItemIDPair> input2; | 229 std::vector<ItemIDPair> input2; |
225 input2.push_back(std::make_pair(kint32min, TestData("D"))); | 230 input2.push_back( |
| 231 std::make_pair(std::numeric_limits<int32_t>::min(), TestData("D"))); |
226 input2.push_back(std::make_pair(7, TestData("E"))); | 232 input2.push_back(std::make_pair(7, TestData("E"))); |
227 cache.AddItemsWithRestrictedID(input2); | 233 cache.AddItemsWithRestrictedID(input2); |
228 EXPECT_EQ(4u, cache.cache_.size()); | 234 EXPECT_EQ(4u, cache.cache_.size()); |
229 EXPECT_EQ(kint32max, cache.last_restricted_id_); | 235 EXPECT_EQ(std::numeric_limits<int32_t>::max(), cache.last_restricted_id_); |
230 | 236 |
231 output.clear(); | 237 output.clear(); |
232 cache.GetCurrentItems(&output); | 238 cache.GetCurrentItems(&output); |
233 EXPECT_EQ(2u, output.size()); | 239 EXPECT_EQ(2u, output.size()); |
234 for (int i = 0; i < 2; ++i) { | 240 for (int i = 0; i < 2; ++i) { |
235 EXPECT_EQ(input2[i].first, output[i].first); | 241 EXPECT_EQ(input2[i].first, output[i].first); |
236 EXPECT_EQ(input2[i].second, output[i].second); | 242 EXPECT_EQ(input2[i].second, output[i].second); |
237 } | 243 } |
238 | 244 |
239 EXPECT_FALSE(cache.GetItemWithRestrictedID(0, &t)); | 245 EXPECT_FALSE(cache.GetItemWithRestrictedID(0, &t)); |
240 EXPECT_TRUE(cache.GetItemWithRestrictedID(kint32max, &t)); | 246 EXPECT_TRUE( |
| 247 cache.GetItemWithRestrictedID(std::numeric_limits<int32_t>::max(), &t)); |
241 EXPECT_EQ(input1[1].second, t); | 248 EXPECT_EQ(input1[1].second, t); |
242 EXPECT_TRUE(cache.GetItemWithRestrictedID(kint32min, &t)); | 249 EXPECT_TRUE( |
| 250 cache.GetItemWithRestrictedID(std::numeric_limits<int32_t>::min(), &t)); |
243 EXPECT_EQ(input2[0].second, t); | 251 EXPECT_EQ(input2[0].second, t); |
244 EXPECT_TRUE(cache.GetItemWithRestrictedID(7, &t)); | 252 EXPECT_TRUE(cache.GetItemWithRestrictedID(7, &t)); |
245 EXPECT_EQ(input2[1].second, t); | 253 EXPECT_EQ(input2[1].second, t); |
246 | 254 |
247 // Add an item without RID. last_restricted_id_ will overflow. | 255 // Add an item without RID. last_restricted_id_ will overflow. |
248 std::vector<TestData> input3; | 256 std::vector<TestData> input3; |
249 input3.push_back(TestData("F")); | 257 input3.push_back(TestData("F")); |
250 input3.push_back(TestData("G")); | 258 input3.push_back(TestData("G")); |
251 cache.AddItems(input3); | 259 cache.AddItems(input3); |
252 EXPECT_EQ(4u, cache.cache_.size()); | 260 EXPECT_EQ(4u, cache.cache_.size()); |
253 EXPECT_EQ(kint32min + 1, cache.last_restricted_id_); | 261 EXPECT_EQ(std::numeric_limits<int32_t>::min() + 1, cache.last_restricted_id_); |
254 | 262 |
255 output.clear(); | 263 output.clear(); |
256 cache.GetCurrentItems(&output); | 264 cache.GetCurrentItems(&output); |
257 EXPECT_EQ(2u, output.size()); | 265 EXPECT_EQ(2u, output.size()); |
258 for (int i = 0; i < 2; ++i) { | 266 for (int i = 0; i < 2; ++i) { |
259 EXPECT_EQ(kint32min + i, output[i].first); | 267 EXPECT_EQ(std::numeric_limits<int32_t>::min() + i, output[i].first); |
260 EXPECT_EQ(input3[i], output[i].second); | 268 EXPECT_EQ(input3[i], output[i].second); |
261 } | 269 } |
262 | 270 |
263 EXPECT_TRUE(cache.GetItemWithRestrictedID(kint32min, &t)); | 271 EXPECT_TRUE( |
| 272 cache.GetItemWithRestrictedID(std::numeric_limits<int32_t>::min(), &t)); |
264 EXPECT_EQ(input3[0], t); | 273 EXPECT_EQ(input3[0], t); |
265 } | 274 } |
266 | 275 |
267 TEST_F(InstantRestrictedIDCacheTest, MixIDGeneration) { | 276 TEST_F(InstantRestrictedIDCacheTest, MixIDGeneration) { |
268 InstantRestrictedIDCache<TestData> cache(5); | 277 InstantRestrictedIDCache<TestData> cache(5); |
269 EXPECT_EQ(0u, cache.cache_.size()); | 278 EXPECT_EQ(0u, cache.cache_.size()); |
270 EXPECT_EQ(0, cache.last_restricted_id_); | 279 EXPECT_EQ(0, cache.last_restricted_id_); |
271 | 280 |
272 // Add some items with manually assigned ids. | 281 // Add some items with manually assigned ids. |
273 std::vector<ItemIDPair> input1; | 282 std::vector<ItemIDPair> input1; |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
415 // Add the same items again. | 424 // Add the same items again. |
416 cache.AddItemsWithRestrictedID(input1); | 425 cache.AddItemsWithRestrictedID(input1); |
417 | 426 |
418 // Make sure |cache.last_add_start_| is still valid. | 427 // Make sure |cache.last_add_start_| is still valid. |
419 cache.GetCurrentItems(&output); | 428 cache.GetCurrentItems(&output); |
420 EXPECT_EQ(3u, output.size()); | 429 EXPECT_EQ(3u, output.size()); |
421 EXPECT_EQ(3u, cache.cache_.size()); | 430 EXPECT_EQ(3u, cache.cache_.size()); |
422 EXPECT_EQ(12, cache.last_restricted_id_); | 431 EXPECT_EQ(12, cache.last_restricted_id_); |
423 EXPECT_EQ(10, cache.last_add_start_->first); | 432 EXPECT_EQ(10, cache.last_add_start_->first); |
424 } | 433 } |
OLD | NEW |