OLD | NEW |
1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 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 "net/base/host_cache.h" | 5 #include "net/base/host_cache.h" |
6 | 6 |
7 #include "base/stl_util-inl.h" | 7 #include "base/stl_util-inl.h" |
8 #include "base/string_util.h" | 8 #include "base/string_util.h" |
9 #include "net/base/net_errors.h" | 9 #include "net/base/net_errors.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 238 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
249 base::TimeTicks now; | 249 base::TimeTicks now; |
250 | 250 |
251 // Lookup and Set should have no effect. | 251 // Lookup and Set should have no effect. |
252 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); | 252 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
253 cache.Set(Key("foobar.com"), OK, AddressList(), now); | 253 cache.Set(Key("foobar.com"), OK, AddressList(), now); |
254 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); | 254 EXPECT_TRUE(cache.Lookup(Key("foobar.com"), base::TimeTicks()) == NULL); |
255 | 255 |
256 EXPECT_EQ(0U, cache.size()); | 256 EXPECT_EQ(0U, cache.size()); |
257 } | 257 } |
258 | 258 |
| 259 // Tests the less than and equal operators for HostCache::Key work. |
| 260 TEST(HostCacheTest, KeyComparators) { |
| 261 struct { |
| 262 // Inputs. |
| 263 HostCache::Key key1; |
| 264 HostCache::Key key2; |
| 265 |
| 266 // Expectation. |
| 267 // -1 means key1 is less than key2 |
| 268 // 0 means key1 equals key2 |
| 269 // 1 means key1 is greater than key2 |
| 270 int expected_comparison; |
| 271 } tests[] = { |
| 272 { |
| 273 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 274 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 275 0 |
| 276 }, |
| 277 { |
| 278 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 279 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 280 1 |
| 281 }, |
| 282 { |
| 283 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 284 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 285 -1 |
| 286 }, |
| 287 { |
| 288 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 289 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), |
| 290 -1 |
| 291 }, |
| 292 { |
| 293 HostCache::Key("host1", ADDRESS_FAMILY_IPV4), |
| 294 HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), |
| 295 1 |
| 296 }, |
| 297 { |
| 298 HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), |
| 299 HostCache::Key("host2", ADDRESS_FAMILY_IPV4), |
| 300 -1 |
| 301 }, |
| 302 }; |
| 303 |
| 304 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 305 SCOPED_TRACE(StringPrintf("Test[%d]", i)); |
| 306 |
| 307 const HostCache::Key& key1 = tests[i].key1; |
| 308 const HostCache::Key& key2 = tests[i].key2; |
| 309 |
| 310 switch (tests[i].expected_comparison) { |
| 311 case -1: |
| 312 EXPECT_TRUE(key1 < key2); |
| 313 EXPECT_FALSE(key2 < key1); |
| 314 EXPECT_FALSE(key2 == key1); |
| 315 break; |
| 316 case 0: |
| 317 EXPECT_FALSE(key1 < key2); |
| 318 EXPECT_FALSE(key2 < key1); |
| 319 EXPECT_TRUE(key2 == key1); |
| 320 break; |
| 321 case 1: |
| 322 EXPECT_FALSE(key1 < key2); |
| 323 EXPECT_TRUE(key2 < key1); |
| 324 EXPECT_FALSE(key2 == key1); |
| 325 break; |
| 326 default: |
| 327 FAIL() << "Invalid expectation. Can be only -1, 0, 1"; |
| 328 } |
| 329 } |
| 330 } |
| 331 |
259 } // namespace net | 332 } // namespace net |
OLD | NEW |