Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(146)

Unified Diff: components/copresence/timed_map_unittest.cc

Issue 419073002: Add the copresence DirectiveHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/copresence/timed_map.h ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/copresence/timed_map_unittest.cc
diff --git a/components/copresence/timed_map_unittest.cc b/components/copresence/timed_map_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..10cfdb8c89e6e6fee91cf90da4d55fc28e2c7a1a
--- /dev/null
+++ b/components/copresence/timed_map_unittest.cc
@@ -0,0 +1,130 @@
+// 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 "base/test/simple_test_tick_clock.h"
+#include "components/copresence/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:
+ // Exists since the timer needs a message loop.
+ base::MessageLoop message_loop_;
+
+ DISALLOW_COPY_AND_ASSIGN(TimedMapTest);
+};
+
+TEST_F(TimedMapTest, Basic) {
+ typedef copresence::TimedMap<int, Value> Map;
+ Map map(base::TimeDelta::FromSeconds(9999), 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(9999), 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(9999), 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);
+}
+
+TEST_F(TimedMapTest, TimedEvict) {
+ const int kLargeTimeValueSeconds = 9999;
+ base::SimpleTestTickClock clock;
+ typedef copresence::TimedMap<int, Value> Map;
+ Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2);
+ map.set_clock_for_testing(&clock);
+
+ // Add value at T=0.
+ map.Add(0x1337, Value(0x7331));
+ EXPECT_TRUE(map.HasKey(0x1337));
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
+
+ // Add value at T=kLargeTimeValueSeconds-1.
+ clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1));
+ map.Add(0xbaad, Value(0xf00d));
+
+ // Check values at T=kLargeTimeValueSeconds-1.
+ EXPECT_TRUE(map.HasKey(0xbaad));
+ EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
+ EXPECT_TRUE(map.HasKey(0x1337));
+ EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
+
+ // Check values at T=kLargeTimeValueSeconds.
+ clock.Advance(base::TimeDelta::FromSeconds(1));
+ EXPECT_FALSE(map.HasKey(0x1337));
+ EXPECT_EQ(0, map.GetValue(0x1337).value);
+ EXPECT_TRUE(map.HasKey(0xbaad));
+ EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
+
+ // Check values at T=2*kLargeTimeValueSeconds
+ clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds));
+ EXPECT_FALSE(map.HasKey(0xbaad));
+ EXPECT_EQ(0, map.GetValue(0xbaad).value);
+}
« no previous file with comments | « components/copresence/timed_map.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698