OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "base/basictypes.h" | 5 #include "base/basictypes.h" |
6 #include "base/macros.h" | 6 #include "base/macros.h" |
7 #include "base/message_loop/message_loop.h" | 7 #include "base/message_loop/message_loop.h" |
8 #include "base/test/simple_test_tick_clock.h" | 8 #include "base/test/simple_test_tick_clock.h" |
9 #include "components/copresence/timed_map.h" | 9 #include "components/copresence/timed_map.h" |
10 #include "testing/gtest/include/gtest/gtest.h" | 10 #include "testing/gtest/include/gtest/gtest.h" |
(...skipping 79 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
90 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); | 90 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); |
91 | 91 |
92 EXPECT_FALSE(two_element_map.HasKey(0x1337)); | 92 EXPECT_FALSE(two_element_map.HasKey(0x1337)); |
93 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value); | 93 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value); |
94 } | 94 } |
95 | 95 |
96 TEST_F(TimedMapTest, TimedEvict) { | 96 TEST_F(TimedMapTest, TimedEvict) { |
97 const int kLargeTimeValueSeconds = 9999; | 97 const int kLargeTimeValueSeconds = 9999; |
98 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2); | 98 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2); |
99 | 99 |
100 // The map takes ownership of the clock, but we retain a pointer. | 100 scoped_refptr<base::SimpleTestTickClock> clock = |
101 base::SimpleTestTickClock* clock = new base::SimpleTestTickClock; | 101 new base::SimpleTestTickClock; |
102 map.set_clock_for_testing(make_scoped_ptr<base::TickClock>(clock)); | 102 map.set_clock_for_testing(clock); |
103 | 103 |
104 // Add value at T=0. | 104 // Add value at T=0. |
105 map.Add(0x1337, Value(0x7331)); | 105 map.Add(0x1337, Value(0x7331)); |
106 EXPECT_TRUE(map.HasKey(0x1337)); | 106 EXPECT_TRUE(map.HasKey(0x1337)); |
107 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 107 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
108 | 108 |
109 // Add value at T=kLargeTimeValueSeconds-1. | 109 // Add value at T=kLargeTimeValueSeconds-1. |
110 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1)); | 110 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1)); |
111 map.Add(0xbaad, Value(0xf00d)); | 111 map.Add(0xbaad, Value(0xf00d)); |
112 | 112 |
113 // Check values at T=kLargeTimeValueSeconds-1. | 113 // Check values at T=kLargeTimeValueSeconds-1. |
114 EXPECT_TRUE(map.HasKey(0xbaad)); | 114 EXPECT_TRUE(map.HasKey(0xbaad)); |
115 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 115 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
116 EXPECT_TRUE(map.HasKey(0x1337)); | 116 EXPECT_TRUE(map.HasKey(0x1337)); |
117 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 117 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
118 | 118 |
119 // Check values at T=kLargeTimeValueSeconds. | 119 // Check values at T=kLargeTimeValueSeconds. |
120 clock->Advance(base::TimeDelta::FromSeconds(1)); | 120 clock->Advance(base::TimeDelta::FromSeconds(1)); |
121 EXPECT_FALSE(map.HasKey(0x1337)); | 121 EXPECT_FALSE(map.HasKey(0x1337)); |
122 EXPECT_EQ(0, map.GetValue(0x1337).value); | 122 EXPECT_EQ(0, map.GetValue(0x1337).value); |
123 EXPECT_TRUE(map.HasKey(0xbaad)); | 123 EXPECT_TRUE(map.HasKey(0xbaad)); |
124 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 124 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
125 | 125 |
126 // Check values at T=2*kLargeTimeValueSeconds | 126 // Check values at T=2*kLargeTimeValueSeconds |
127 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds)); | 127 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds)); |
128 EXPECT_FALSE(map.HasKey(0xbaad)); | 128 EXPECT_FALSE(map.HasKey(0xbaad)); |
129 EXPECT_EQ(0, map.GetValue(0xbaad).value); | 129 EXPECT_EQ(0, map.GetValue(0xbaad).value); |
130 } | 130 } |
OLD | NEW |