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

Side by Side Diff: components/copresence/common/timed_map_unittest.cc

Issue 419073002: Add the copresence DirectiveHandler. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: review comments 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
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 "components/copresence/common/timed_map.h"
9 #include "testing/gtest/include/gtest/gtest.h"
10
11 namespace {
12
13 struct Value {
14 Value() : value(0) {}
15 explicit Value(int new_value) : value(new_value) {}
16
17 int value;
18 };
19
20 } // namespace
21
22 class TimedMapTest : public testing::Test {
23 public:
24 TimedMapTest() {}
25
26 private:
27 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
28
29 DISALLOW_COPY_AND_ASSIGN(TimedMapTest);
30 };
31
32 TEST_F(TimedMapTest, Basic) {
33 typedef copresence::TimedMap<int, Value> Map;
34 Map map(base::TimeDelta::FromSeconds(0x7331), 3);
35
36 EXPECT_FALSE(map.HasKey(0));
37 EXPECT_EQ(0, map.GetValue(0).value);
38
39 map.Add(0x1337, Value(0x7331));
40 EXPECT_TRUE(map.HasKey(0x1337));
41 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
42
43 map.Add(0xbaad, Value(0xf00d));
44 EXPECT_TRUE(map.HasKey(0xbaad));
45 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
46 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
47
48 map.Add(0x1234, Value(0x5678));
49 EXPECT_TRUE(map.HasKey(0x1234));
50 EXPECT_TRUE(map.HasKey(0xbaad));
51 EXPECT_TRUE(map.HasKey(0x1337));
52
53 EXPECT_EQ(0x5678, map.GetValue(0x1234).value);
54 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
55 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
56 }
57
58 TEST_F(TimedMapTest, ValueReplacement) {
59 typedef copresence::TimedMap<int, Value> Map;
60 Map map(base::TimeDelta::FromSeconds(0x7331), 10);
61
62 map.Add(0x1337, Value(0x7331));
63 EXPECT_TRUE(map.HasKey(0x1337));
64 EXPECT_EQ(0x7331, map.GetValue(0x1337).value);
65
66 map.Add(0xbaad, Value(0xf00d));
67 EXPECT_TRUE(map.HasKey(0xbaad));
68 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value);
69
70 map.Add(0x1337, Value(0xd00d));
71 EXPECT_TRUE(map.HasKey(0x1337));
72 EXPECT_EQ(0xd00d, map.GetValue(0x1337).value);
73 }
74
75 TEST_F(TimedMapTest, SizeEvict) {
76 typedef copresence::TimedMap<int, Value> Map;
77 Map two_element_map(base::TimeDelta::FromSeconds(0x7331), 2);
78
79 two_element_map.Add(0x1337, Value(0x7331));
80 EXPECT_TRUE(two_element_map.HasKey(0x1337));
81 EXPECT_EQ(0x7331, two_element_map.GetValue(0x1337).value);
82
83 two_element_map.Add(0xbaad, Value(0xf00d));
84 EXPECT_TRUE(two_element_map.HasKey(0xbaad));
85 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value);
86
87 two_element_map.Add(0x1234, Value(0x5678));
88 EXPECT_TRUE(two_element_map.HasKey(0x1234));
89 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value);
90
91 EXPECT_FALSE(two_element_map.HasKey(0x1337));
92 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value);
93 }
94
95 // TODO(rkc): Figure out how to test timed eviction and add a TimedEvict test.
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698