diff options
author | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-26 20:35:02 +0000 |
---|---|---|
committer | eroman@chromium.org <eroman@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-10-26 20:35:02 +0000 |
commit | 60cf7c9ce8cc0b34e9a7768db0c2550eb7cf1e78 (patch) | |
tree | c9ce9e57f2966c0d063119dc2c0c98bf060f1cfb /net | |
parent | 72732449c037ad35ea43b228105b7ff387d5cb31 (diff) | |
download | chromium_src-60cf7c9ce8cc0b34e9a7768db0c2550eb7cf1e78.zip chromium_src-60cf7c9ce8cc0b34e9a7768db0c2550eb7cf1e78.tar.gz chromium_src-60cf7c9ce8cc0b34e9a7768db0c2550eb7cf1e78.tar.bz2 |
Fix a bad comparator. This caused lookups in std::map to be wrong.
BUG=25823
TEST=HostCacheTest.KeyComparator
Review URL: http://codereview.chromium.org/338023
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@30084 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'net')
-rw-r--r-- | net/base/host_cache.h | 6 | ||||
-rw-r--r-- | net/base/host_cache_unittest.cc | 73 |
2 files changed, 76 insertions, 3 deletions
diff --git a/net/base/host_cache.h b/net/base/host_cache.h index 538f7ef..b36af35 100644 --- a/net/base/host_cache.h +++ b/net/base/host_cache.h @@ -42,9 +42,9 @@ class HostCache { } bool operator<(const Key& other) const { - if (address_family < other.address_family) - return true; - return hostname < other.hostname; + if (address_family == other.address_family) + return hostname < other.hostname; + return address_family < other.address_family; } std::string hostname; diff --git a/net/base/host_cache_unittest.cc b/net/base/host_cache_unittest.cc index c5a0f21..a879bed 100644 --- a/net/base/host_cache_unittest.cc +++ b/net/base/host_cache_unittest.cc @@ -256,4 +256,77 @@ TEST(HostCacheTest, NoCache) { EXPECT_EQ(0U, cache.size()); } +// Tests the less than and equal operators for HostCache::Key work. +TEST(HostCacheTest, KeyComparators) { + struct { + // Inputs. + HostCache::Key key1; + HostCache::Key key2; + + // Expectation. + // -1 means key1 is less than key2 + // 0 means key1 equals key2 + // 1 means key1 is greater than key2 + int expected_comparison; + } tests[] = { + { + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + 0 + }, + { + HostCache::Key("host1", ADDRESS_FAMILY_IPV4), + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + 1 + }, + { + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + HostCache::Key("host1", ADDRESS_FAMILY_IPV4), + -1 + }, + { + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), + -1 + }, + { + HostCache::Key("host1", ADDRESS_FAMILY_IPV4), + HostCache::Key("host2", ADDRESS_FAMILY_UNSPECIFIED), + 1 + }, + { + HostCache::Key("host1", ADDRESS_FAMILY_UNSPECIFIED), + HostCache::Key("host2", ADDRESS_FAMILY_IPV4), + -1 + }, + }; + + for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { + SCOPED_TRACE(StringPrintf("Test[%d]", i)); + + const HostCache::Key& key1 = tests[i].key1; + const HostCache::Key& key2 = tests[i].key2; + + switch (tests[i].expected_comparison) { + case -1: + EXPECT_TRUE(key1 < key2); + EXPECT_FALSE(key2 < key1); + EXPECT_FALSE(key2 == key1); + break; + case 0: + EXPECT_FALSE(key1 < key2); + EXPECT_FALSE(key2 < key1); + EXPECT_TRUE(key2 == key1); + break; + case 1: + EXPECT_FALSE(key1 < key2); + EXPECT_TRUE(key2 < key1); + EXPECT_FALSE(key2 == key1); + break; + default: + FAIL() << "Invalid expectation. Can be only -1, 0, 1"; + } + } +} + } // namespace net |