From 8a0edac9abfec72e0a86aebd6a0a38761c7c8962 Mon Sep 17 00:00:00 2001 From: Robert Sesek Date: Tue, 7 Nov 2017 18:24:26 -0500 Subject: Add index-based set functionality to NonAllocatingMap. This enables repeatedly setting a value based on index, which avoids a linear scan of the entry table after the first SetKeyValue(). Bug: chromium:598854 Change-Id: I9964670a09dcd8ff76180d031a373f20990bf4d8 Reviewed-on: https://chromium-review.googlesource.com/757579 Reviewed-by: Mark Mentovai --- src/common/simple_string_dictionary_unittest.cc | 31 +++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'src/common/simple_string_dictionary_unittest.cc') diff --git a/src/common/simple_string_dictionary_unittest.cc b/src/common/simple_string_dictionary_unittest.cc index 34f4b9ef..e7b8fd76 100644 --- a/src/common/simple_string_dictionary_unittest.cc +++ b/src/common/simple_string_dictionary_unittest.cc @@ -288,6 +288,37 @@ TEST(NonAllocatingMapTest, OutOfSpace) { EXPECT_FALSE(map.GetValueForKey("c")); } +TEST(NonAllocatingMapTest, ByIndex) { + NonAllocatingMap<10, 10, 3> map; + + size_t index1 = map.SetKeyValue("test", "one"); + EXPECT_TRUE(index1 >= 0 && index1 <= map.num_entries); + + size_t index2 = map.SetKeyValue("moo", "foo"); + EXPECT_TRUE(index2 >= 0 && index2 <= map.num_entries); + EXPECT_NE(index1, index2); + + size_t index3 = map.SetKeyValue("blob", "kebab"); + EXPECT_TRUE(index3 >= 0 && index3 <= map.num_entries); + EXPECT_NE(index2, index3); + + size_t index4 = map.SetKeyValue("nogo", "full"); + EXPECT_TRUE(index4 == map.num_entries); + + EXPECT_STREQ("one", map.GetValueForKey("test")); + EXPECT_STREQ("foo", map.GetValueForKey("moo")); + EXPECT_STREQ("kebab", map.GetValueForKey("blob")); + + map.SetValueAtIndex(index2, "booo"); + EXPECT_STREQ("booo", map.GetValueForKey("moo")); + + EXPECT_TRUE(map.RemoveAtIndex(index1)); + EXPECT_FALSE(map.GetValueForKey("test")); + + EXPECT_FALSE(map.RemoveAtIndex(map.num_entries)); + EXPECT_FALSE(map.RemoveAtIndex(9999)); +} + #ifndef NDEBUG TEST(NonAllocatingMapTest, NullKey) { -- cgit v1.2.1