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

Side by Side 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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « components/copresence/timed_map.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/basictypes.h"
6 #include "base/macros.h"
7 #include "base/message_loop/message_loop.h"
8 #include "base/test/simple_test_tick_clock.h"
9 #include "components/copresence/timed_map.h"
10 #include "testing/gtest/include/gtest/gtest.h"
11
12 namespace {
13
14 struct Value {
15 Value() : value(0) {}
16 explicit Value(int new_value) : value(new_value) {}
17
18 int value;
19 };
20
21 } // namespace
22
23 class TimedMapTest : public testing::Test {
24 public:
25 TimedMapTest() {}
26
27 private:
28 // Exists since the timer needs a message loop.
29 base::MessageLoop message_loop_;
30
31 DISALLOW_COPY_AND_ASSIGN(TimedMapTest);
32 };
33
34 TEST_F(TimedMapTest, Basic) {
35 typedef copresence::TimedMap<int, Value> Map;
36 Map map(base::TimeDelta::FromSeconds(9999), 3);
37
38 EXPECT_FALSE(map.HasKey(0));
39 EXPECT_EQ(0, map.GetValue(0).value);
40
41 map.Add(0x1337, Value(0x7331));
42 EXPECT_TRUE(map.HasKey(0x1337));
43 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
44
45 map.Add(0xbaad, Value(0xf00d));
46 EXPECT_TRUE(map.HasKey(0xbaad));
47 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
48 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
49
50 map.Add(0x1234, Value(0x5678));
51 EXPECT_TRUE(map.HasKey(0x1234));
52 EXPECT_TRUE(map.HasKey(0xbaad));
53 EXPECT_TRUE(map.HasKey(0x1337));
54
55 EXPECT_EQ(0x5678, map.GetValue(0x1234).value);
56 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
57 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
58 }
59
60 TEST_F(TimedMapTest, ValueReplacement) {
61 typedef copresence::TimedMap<int, Value> Map;
62 Map map(base::TimeDelta::FromSeconds(9999), 10);
63
64 map.Add(0x1337, Value(0x7331));
65 EXPECT_TRUE(map.HasKey(0x1337));
66 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
67
68 map.Add(0xbaad, Value(0xf00d));
69 EXPECT_TRUE(map.HasKey(0xbaad));
70 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
71
72 map.Add(0x1337, Value(0xd00d));
73 EXPECT_TRUE(map.HasKey(0x1337));
74 EXPECT_EQ(0xd00d, map.GetValue(0x1337).value);
75 }
76
77 TEST_F(TimedMapTest, SizeEvict) {
78 typedef copresence::TimedMap<int, Value> Map;
79 Map two_element_map(base::TimeDelta::FromSeconds(9999), 2);
80
81 two_element_map.Add(0x1337, Value(0x7331));
82 EXPECT_TRUE(two_element_map.HasKey(0x1337));
83 EXPECT_EQ(0x7331, two_element_map.GetValue(0x1337).value);
84
85 two_element_map.Add(0xbaad, Value(0xf00d));
86 EXPECT_TRUE(two_element_map.HasKey(0xbaad));
87 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value);
88
89 two_element_map.Add(0x1234, Value(0x5678));
90 EXPECT_TRUE(two_element_map.HasKey(0x1234));
91 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value);
92
93 EXPECT_FALSE(two_element_map.HasKey(0x1337));
94 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value);
95 }
96
97 TEST_F(TimedMapTest, TimedEvict) {
98 const int kLargeTimeValueSeconds = 9999;
99 base::SimpleTestTickClock clock;
100 typedef copresence::TimedMap<int, Value> Map;
101 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2);
102 map.set_clock_for_testing(&clock);
103
104 // Add value at T=0.
105 map.Add(0x1337, Value(0x7331));
106 EXPECT_TRUE(map.HasKey(0x1337));
107 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
108
109 // Add value at T=kLargeTimeValueSeconds-1.
110 clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1));
111 map.Add(0xbaad, Value(0xf00d));
112
113 // Check values at T=kLargeTimeValueSeconds-1.
114 EXPECT_TRUE(map.HasKey(0xbaad));
115 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
116 EXPECT_TRUE(map.HasKey(0x1337));
117 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
118
119 // Check values at T=kLargeTimeValueSeconds.
120 clock.Advance(base::TimeDelta::FromSeconds(1));
121 EXPECT_FALSE(map.HasKey(0x1337));
122 EXPECT_EQ(0, map.GetValue(0x1337).value);
123 EXPECT_TRUE(map.HasKey(0xbaad));
124 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
125
126 // Check values at T=2*kLargeTimeValueSeconds
127 clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds));
128 EXPECT_FALSE(map.HasKey(0xbaad));
129 EXPECT_EQ(0, map.GetValue(0xbaad).value);
130 }
OLDNEW
« 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