| 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" |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 struct Value { | 14 struct Value { |
| 15 Value() : value(0) {} | 15 Value() : value(0) {} |
| 16 explicit Value(int new_value) : value(new_value) {} | 16 explicit Value(int new_value) : value(new_value) {} |
| 17 | 17 |
| 18 int value; | 18 int value; |
| 19 }; | 19 }; |
| 20 | 20 |
| 21 } // namespace | 21 } // namespace |
| 22 | 22 |
| 23 class TimedMapTest : public testing::Test { | 23 class TimedMapTest : public testing::Test { |
| 24 public: | 24 public: |
| 25 typedef copresence::TimedMap<int, Value> Map; |
| 26 |
| 25 TimedMapTest() {} | 27 TimedMapTest() {} |
| 26 | 28 |
| 27 private: | 29 private: |
| 28 // Exists since the timer needs a message loop. | 30 // Exists since the timer needs a message loop. |
| 29 base::MessageLoop message_loop_; | 31 base::MessageLoop message_loop_; |
| 30 | 32 |
| 31 DISALLOW_COPY_AND_ASSIGN(TimedMapTest); | 33 DISALLOW_COPY_AND_ASSIGN(TimedMapTest); |
| 32 }; | 34 }; |
| 33 | 35 |
| 34 // TODO(rkc): Find and fix the memory leak here. | 36 TEST_F(TimedMapTest, Basic) { |
| 35 #define MAYBE_Basic DISABLED_Basic | |
| 36 | |
| 37 TEST_F(TimedMapTest, MAYBE_Basic) { | |
| 38 typedef copresence::TimedMap<int, Value> Map; | |
| 39 Map map(base::TimeDelta::FromSeconds(9999), 3); | 37 Map map(base::TimeDelta::FromSeconds(9999), 3); |
| 40 | 38 |
| 41 EXPECT_FALSE(map.HasKey(0)); | 39 EXPECT_FALSE(map.HasKey(0)); |
| 42 EXPECT_EQ(0, map.GetValue(0).value); | 40 EXPECT_EQ(0, map.GetValue(0).value); |
| 43 | 41 |
| 44 map.Add(0x1337, Value(0x7331)); | 42 map.Add(0x1337, Value(0x7331)); |
| 45 EXPECT_TRUE(map.HasKey(0x1337)); | 43 EXPECT_TRUE(map.HasKey(0x1337)); |
| 46 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 44 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 47 | 45 |
| 48 map.Add(0xbaad, Value(0xf00d)); | 46 map.Add(0xbaad, Value(0xf00d)); |
| 49 EXPECT_TRUE(map.HasKey(0xbaad)); | 47 EXPECT_TRUE(map.HasKey(0xbaad)); |
| 50 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 48 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
| 51 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 49 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 52 | 50 |
| 53 map.Add(0x1234, Value(0x5678)); | 51 map.Add(0x1234, Value(0x5678)); |
| 54 EXPECT_TRUE(map.HasKey(0x1234)); | 52 EXPECT_TRUE(map.HasKey(0x1234)); |
| 55 EXPECT_TRUE(map.HasKey(0xbaad)); | 53 EXPECT_TRUE(map.HasKey(0xbaad)); |
| 56 EXPECT_TRUE(map.HasKey(0x1337)); | 54 EXPECT_TRUE(map.HasKey(0x1337)); |
| 57 | 55 |
| 58 EXPECT_EQ(0x5678, map.GetValue(0x1234).value); | 56 EXPECT_EQ(0x5678, map.GetValue(0x1234).value); |
| 59 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 57 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
| 60 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 58 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 61 } | 59 } |
| 62 | 60 |
| 63 // TODO(rkc): Find and fix the memory leak here. | 61 TEST_F(TimedMapTest, ValueReplacement) { |
| 64 #define MAYBE_ValueReplacement DISABLED_ValueReplacement | |
| 65 | |
| 66 TEST_F(TimedMapTest, MAYBE_ValueReplacement) { | |
| 67 typedef copresence::TimedMap<int, Value> Map; | |
| 68 Map map(base::TimeDelta::FromSeconds(9999), 10); | 62 Map map(base::TimeDelta::FromSeconds(9999), 10); |
| 69 | 63 |
| 70 map.Add(0x1337, Value(0x7331)); | 64 map.Add(0x1337, Value(0x7331)); |
| 71 EXPECT_TRUE(map.HasKey(0x1337)); | 65 EXPECT_TRUE(map.HasKey(0x1337)); |
| 72 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 66 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 73 | 67 |
| 74 map.Add(0xbaad, Value(0xf00d)); | 68 map.Add(0xbaad, Value(0xf00d)); |
| 75 EXPECT_TRUE(map.HasKey(0xbaad)); | 69 EXPECT_TRUE(map.HasKey(0xbaad)); |
| 76 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 70 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
| 77 | 71 |
| 78 map.Add(0x1337, Value(0xd00d)); | 72 map.Add(0x1337, Value(0xd00d)); |
| 79 EXPECT_TRUE(map.HasKey(0x1337)); | 73 EXPECT_TRUE(map.HasKey(0x1337)); |
| 80 EXPECT_EQ(0xd00d, map.GetValue(0x1337).value); | 74 EXPECT_EQ(0xd00d, map.GetValue(0x1337).value); |
| 81 } | 75 } |
| 82 | 76 |
| 83 // TODO(rkc): Find and fix the memory leak here. | 77 TEST_F(TimedMapTest, SizeEvict) { |
| 84 #define MAYBE_SizeEvict DISABLED_SizeEvict | |
| 85 | |
| 86 TEST_F(TimedMapTest, MAYBE_SizeEvict) { | |
| 87 typedef copresence::TimedMap<int, Value> Map; | |
| 88 Map two_element_map(base::TimeDelta::FromSeconds(9999), 2); | 78 Map two_element_map(base::TimeDelta::FromSeconds(9999), 2); |
| 89 | 79 |
| 90 two_element_map.Add(0x1337, Value(0x7331)); | 80 two_element_map.Add(0x1337, Value(0x7331)); |
| 91 EXPECT_TRUE(two_element_map.HasKey(0x1337)); | 81 EXPECT_TRUE(two_element_map.HasKey(0x1337)); |
| 92 EXPECT_EQ(0x7331, two_element_map.GetValue(0x1337).value); | 82 EXPECT_EQ(0x7331, two_element_map.GetValue(0x1337).value); |
| 93 | 83 |
| 94 two_element_map.Add(0xbaad, Value(0xf00d)); | 84 two_element_map.Add(0xbaad, Value(0xf00d)); |
| 95 EXPECT_TRUE(two_element_map.HasKey(0xbaad)); | 85 EXPECT_TRUE(two_element_map.HasKey(0xbaad)); |
| 96 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); | 86 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); |
| 97 | 87 |
| 98 two_element_map.Add(0x1234, Value(0x5678)); | 88 two_element_map.Add(0x1234, Value(0x5678)); |
| 99 EXPECT_TRUE(two_element_map.HasKey(0x1234)); | 89 EXPECT_TRUE(two_element_map.HasKey(0x1234)); |
| 100 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); | 90 EXPECT_EQ(0xf00d, two_element_map.GetValue(0xbaad).value); |
| 101 | 91 |
| 102 EXPECT_FALSE(two_element_map.HasKey(0x1337)); | 92 EXPECT_FALSE(two_element_map.HasKey(0x1337)); |
| 103 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value); | 93 EXPECT_EQ(0, two_element_map.GetValue(0x1337).value); |
| 104 } | 94 } |
| 105 | 95 |
| 106 // TODO(rkc): Find and fix the memory leak here. | 96 TEST_F(TimedMapTest, TimedEvict) { |
| 107 #define MAYBE_TimedEvict DISABLED_TimedEvict | 97 const int kLargeTimeValueSeconds = 9999; |
| 98 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2); |
| 108 | 99 |
| 109 TEST_F(TimedMapTest, MAYBE_TimedEvict) { | 100 // The map takes ownership of the clock, but we retain a pointer. |
| 110 const int kLargeTimeValueSeconds = 9999; | 101 base::SimpleTestTickClock* clock = new base::SimpleTestTickClock; |
| 111 base::SimpleTestTickClock clock; | 102 map.set_clock_for_testing(clock); |
| 112 typedef copresence::TimedMap<int, Value> Map; | |
| 113 Map map(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds), 2); | |
| 114 map.set_clock_for_testing(&clock); | |
| 115 | 103 |
| 116 // Add value at T=0. | 104 // Add value at T=0. |
| 117 map.Add(0x1337, Value(0x7331)); | 105 map.Add(0x1337, Value(0x7331)); |
| 118 EXPECT_TRUE(map.HasKey(0x1337)); | 106 EXPECT_TRUE(map.HasKey(0x1337)); |
| 119 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 107 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 120 | 108 |
| 121 // Add value at T=kLargeTimeValueSeconds-1. | 109 // Add value at T=kLargeTimeValueSeconds-1. |
| 122 clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1)); | 110 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds - 1)); |
| 123 map.Add(0xbaad, Value(0xf00d)); | 111 map.Add(0xbaad, Value(0xf00d)); |
| 124 | 112 |
| 125 // Check values at T=kLargeTimeValueSeconds-1. | 113 // Check values at T=kLargeTimeValueSeconds-1. |
| 126 EXPECT_TRUE(map.HasKey(0xbaad)); | 114 EXPECT_TRUE(map.HasKey(0xbaad)); |
| 127 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 115 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
| 128 EXPECT_TRUE(map.HasKey(0x1337)); | 116 EXPECT_TRUE(map.HasKey(0x1337)); |
| 129 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); | 117 EXPECT_EQ(0x7331, map.GetValue(0x1337).value); |
| 130 | 118 |
| 131 // Check values at T=kLargeTimeValueSeconds. | 119 // Check values at T=kLargeTimeValueSeconds. |
| 132 clock.Advance(base::TimeDelta::FromSeconds(1)); | 120 clock->Advance(base::TimeDelta::FromSeconds(1)); |
| 133 EXPECT_FALSE(map.HasKey(0x1337)); | 121 EXPECT_FALSE(map.HasKey(0x1337)); |
| 134 EXPECT_EQ(0, map.GetValue(0x1337).value); | 122 EXPECT_EQ(0, map.GetValue(0x1337).value); |
| 135 EXPECT_TRUE(map.HasKey(0xbaad)); | 123 EXPECT_TRUE(map.HasKey(0xbaad)); |
| 136 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); | 124 EXPECT_EQ(0xf00d, map.GetValue(0xbaad).value); |
| 137 | 125 |
| 138 // Check values at T=2*kLargeTimeValueSeconds | 126 // Check values at T=2*kLargeTimeValueSeconds |
| 139 clock.Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds)); | 127 clock->Advance(base::TimeDelta::FromSeconds(kLargeTimeValueSeconds)); |
| 140 EXPECT_FALSE(map.HasKey(0xbaad)); | 128 EXPECT_FALSE(map.HasKey(0xbaad)); |
| 141 EXPECT_EQ(0, map.GetValue(0xbaad).value); | 129 EXPECT_EQ(0, map.GetValue(0xbaad).value); |
| 142 } | 130 } |
| OLD | NEW |