OLD | NEW |
| (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 "components/copresence/timed_map.h" | |
6 | |
7 #include "base/macros.h" | |
8 #include "base/memory/ptr_util.h" | |
9 #include "base/message_loop/message_loop.h" | |
10 #include "base/test/simple_test_tick_clock.h" | |
11 #include "testing/gtest/include/gtest/gtest.h" | |
12 | |
13 namespace { | |
14 | |
15 struct Value { | |
16 Value() : value(0) {} | |
17 explicit Value(int new_value) : value(new_value) {} | |
18 | |
19 int value; | |
20 }; | |
21 | |
22 } // namespace | |
23 | |
24 class TimedMapTest : public testing::Test { | |
25 public: | |
26 using Map = copresence::TimedMap<int, Value>; | |
27 | |
28 TimedMapTest() {} | |
29 | |
30 private: | |
31 // Exists since the timer needs a message loop. | |
32 base::MessageLoop message_loop_; | |
33 | |
34 DISALLOW_COPY_AND_ASSIGN(TimedMapTest); | |
35 }; | |
36 | |
37 TEST_F(TimedMapTest, Basic) { | |
38 Map map(base::TimeDelta::FromSeconds(9999), 3); | |
39 | |
40 EXPECT_FALSE(map.HasKey(0)); | |
41 EXPECT_EQ(0, map.GetValue(0).value); | |
42 | |
43 map.Add(0x1337, Value(0x7331)); | |
44 EXPECT_TRUE(map.HasKey(0x1337)); | |
45 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
46 | |
47 map.Add(0xbaad, Value(0xf00d)); | |
48 EXPECT_TRUE(map.HasKey(0xbaad)); | |
49 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | |
50 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
51 | |
52 map.Add(0x1234, Value(0x5678)); | |
53 EXPECT_TRUE(map.HasKey(0x1234)); | |
54 EXPECT_TRUE(map.HasKey(0xbaad)); | |
55 EXPECT_TRUE(map.HasKey(0x1337)); | |
56 | |
57 EXPECT_EQ(0x5678, map.GetValue(0x1234).value); | |
58 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | |
59 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
60 } | |
61 | |
62 TEST_F(TimedMapTest, ValueReplacement) { | |
63 Map map(base::TimeDelta::FromSeconds(9999), 10); | |
64 | |
65 map.Add(0x1337, Value(0x7331)); | |
66 EXPECT_TRUE(map.HasKey(0x1337)); | |
67 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
68 | |
69 map.Add(0xbaad, Value(0xf00d)); | |
70 EXPECT_TRUE(map.HasKey(0xbaad)); | |
71 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | |
72 | |
73 map.Add(0x1337, Value(0xd00d)); | |
74 EXPECT_TRUE(map.HasKey(0x1337)); | |
75 EXPECT_EQ(0xd00d, map.GetValue(0x1337).value); | |
76 } | |
77 | |
78 TEST_F(TimedMapTest, SizeEvict) { | |
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 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2); | |
100 | |
101 // The map takes ownership of the clock, but we retain a pointer. | |
102 base::SimpleTestTickClock* clock = new base::SimpleTestTickClock; | |
103 map.set_clock_for_testing(base::WrapUnique<base::TickClock>(clock)); | |
104 | |
105 // Add value at T=0. | |
106 map.Add(0x1337, Value(0x7331)); | |
107 EXPECT_TRUE(map.HasKey(0x1337)); | |
108 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
109 | |
110 // Add value at T=kLargeTimeValueSeconds-1. | |
111 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1)); | |
112 map.Add(0xbaad, Value(0xf00d)); | |
113 | |
114 // Check values at T=kLargeTimeValueSeconds-1. | |
115 EXPECT_TRUE(map.HasKey(0xbaad)); | |
116 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | |
117 EXPECT_TRUE(map.HasKey(0x1337)); | |
118 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | |
119 | |
120 // Check values at T=kLargeTimeValueSeconds. | |
121 clock->Advance(base::TimeDelta::FromSeconds(1)); | |
122 EXPECT_FALSE(map.HasKey(0x1337)); | |
123 EXPECT_EQ(0, map.GetValue(0x1337).value); | |
124 EXPECT_TRUE(map.HasKey(0xbaad)); | |
125 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | |
126 | |
127 // Check values at T=2*kLargeTimeValueSeconds | |
128 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds)); | |
129 EXPECT_FALSE(map.HasKey(0xbaad)); | |
130 EXPECT_EQ(0, map.GetValue(0xbaad).value); | |
131 } | |
OLD | NEW |