Index: components/copresence/common/timed_map_unittest.cc |
diff --git a/components/copresence/common/timed_map_unittest.cc b/components/copresence/common/timed_map_unittest.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..82d250e0de6b268fca86b334835e28bb96b4515f |
--- /dev/null |
+++ b/components/copresence/common/timed_map_unittest.cc |
@@ -0,0 +1,95 @@ |
+// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "base/basictypes.h" |
+#include "base/macros.h" |
+#include "base/message_loop/message_loop.h" |
+#include "components/copresence/common/timed_map.h" |
+#include "testing/gtest/include/gtest/gtest.h" |
+ |
+namespace { |
+ |
+struct Value { |
+ Value() : value(0) {} |
+ explicit Value(int new_value) : value(new_value) {} |
+ |
+ int value; |
+}; |
+ |
+} // namespace |
+ |
+class TimedMapTest : public testing::Test { |
+ public: |
+ TimedMapTest() {} |
+ |
+ private: |
+ base::MessageLoop ml_; |
xiyuan
2014/07/29 00:22:26
nit: Seems not used. If it is used, give it a bett
rkc
2014/07/29 00:33:36
It needs to exist so we have a message loop that t
|
+ |
+ DISALLOW_COPY_AND_ASSIGN(TimedMapTest); |
+}; |
+ |
+TEST_F(TimedMapTest, Basic) { |
+ typedef copresence::TimedMap<int, Value> Map; |
+ Map map(base::TimeDelta::FromSeconds(0x7331), 3); |
+ |
+ EXPECT_FALSE(map.HasKey(0)); |
+ EXPECT_EQ(0, map.GetValue(0).value); |
+ |
+ map.Add(0x1337, Value(0x7331)); |
+ EXPECT_TRUE(map.HasKey(0x1337)); |
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
+ |
+ map.Add(0xbaad, Value(0xf00d)); |
+ EXPECT_TRUE(map.HasKey(0xbaad)); |
+ EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
+ |
+ map.Add(0x1234, Value(0x5678)); |
+ EXPECT_TRUE(map.HasKey(0x1234)); |
+ EXPECT_TRUE(map.HasKey(0xbaad)); |
+ EXPECT_TRUE(map.HasKey(0x1337)); |
+ |
+ EXPECT_EQ(0x5678, map.GetValue(0x1234).value); |
+ EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
+} |
+ |
+TEST_F(TimedMapTest, ValueReplacement) { |
+ typedef copresence::TimedMap<int, Value> Map; |
+ Map map(base::TimeDelta::FromSeconds(0x7331), 10); |
+ |
+ map.Add(0x1337, Value(0x7331)); |
+ EXPECT_TRUE(map.HasKey(0x1337)); |
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
+ |
+ map.Add(0xbaad, Value(0xf00d)); |
+ EXPECT_TRUE(map.HasKey(0xbaad)); |
+ EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
+ |
+ map.Add(0x1337, Value(0xd00d)); |
+ EXPECT_TRUE(map.HasKey(0x1337)); |
+ EXPECT_EQ(0xd00d, map.GetValue(0x1337).value); |
+} |
+ |
+TEST_F(TimedMapTest, SizeEvict) { |
+ typedef copresence::TimedMap<int, Value> Map; |
+ Map two_element_map(base::TimeDelta::FromSeconds(0x7331), 2); |
+ |
+ two_element_map.Add(0x1337, Value(0x7331)); |
+ EXPECT_TRUE(two_element_map.HasKey(0x1337)); |
+ EXPECT_EQ(0x7331, two_element_map.GetValue(0x1337).value); |
+ |
+ two_element_map.Add(0xbaad, Value(0xf00d)); |
+ EXPECT_TRUE(two_element_map.HasKey(0xbaad)); |
+ EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); |
+ |
+ two_element_map.Add(0x1234, Value(0x5678)); |
+ EXPECT_TRUE(two_element_map.HasKey(0x1234)); |
+ EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); |
+ |
+ EXPECT_FALSE(two_element_map.HasKey(0x1337)); |
+ EXPECT_EQ(0, two_element_map.GetValue(0x1337).value); |
+} |
+ |
+// TODO(rkc): Figure out how to test timed eviction and add a TimedEvict test. |