[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 4 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 5 | #include "base/at_exit.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 6 | #include "base/file_util.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 7 | #include "base/memory/singleton.h" |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 8 | #include "base/path_service.h" |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 9 | #include "testing/gtest/include/gtest/gtest.h" |
| 10 | |
| 11 | namespace { |
| 12 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 13 | COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a); |
| 14 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 15 | typedef void (*CallbackFunc)(); |
| 16 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 17 | class IntSingleton { |
| 18 | public: |
| 19 | static IntSingleton* GetInstance() { |
| 20 | return Singleton<IntSingleton>::get(); |
| 21 | } |
| 22 | |
| 23 | int value_; |
| 24 | }; |
| 25 | |
| 26 | class Init5Singleton { |
| 27 | public: |
| 28 | struct Trait; |
| 29 | |
| 30 | static Init5Singleton* GetInstance() { |
| 31 | return Singleton<Init5Singleton, Trait>::get(); |
| 32 | } |
| 33 | |
| 34 | int value_; |
| 35 | }; |
| 36 | |
| 37 | struct Init5Singleton::Trait : public DefaultSingletonTraits<Init5Singleton> { |
| 38 | static Init5Singleton* New() { |
| 39 | Init5Singleton* instance = new Init5Singleton(); |
| 40 | instance->value_ = 5; |
| 41 | return instance; |
[email protected] | 5820d2f0 | 2010-12-11 10:23:37 | [diff] [blame] | 42 | } |
| 43 | }; |
[email protected] | 49ab556 | 2010-12-11 09:13:54 | [diff] [blame] | 44 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 45 | int* SingletonInt() { |
| 46 | return &IntSingleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | int* SingletonInt5() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 50 | return &Init5Singleton::GetInstance()->value_; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 51 | } |
| 52 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 53 | template <typename Type> |
| 54 | struct CallbackTrait : public DefaultSingletonTraits<Type> { |
| 55 | static void Delete(Type* instance) { |
| 56 | if (instance->callback_) |
| 57 | (instance->callback_)(); |
| 58 | DefaultSingletonTraits<Type>::Delete(instance); |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | class CallbackSingleton { |
| 63 | public: |
| 64 | CallbackSingleton() : callback_(NULL) { } |
| 65 | CallbackFunc callback_; |
| 66 | }; |
| 67 | |
| 68 | class CallbackSingletonWithNoLeakTrait : public CallbackSingleton { |
| 69 | public: |
| 70 | struct Trait : public CallbackTrait<CallbackSingletonWithNoLeakTrait> { }; |
| 71 | |
| 72 | CallbackSingletonWithNoLeakTrait() : CallbackSingleton() { } |
| 73 | |
| 74 | static CallbackSingletonWithNoLeakTrait* GetInstance() { |
| 75 | return Singleton<CallbackSingletonWithNoLeakTrait, Trait>::get(); |
| 76 | } |
| 77 | }; |
| 78 | |
| 79 | class CallbackSingletonWithLeakTrait : public CallbackSingleton { |
| 80 | public: |
| 81 | struct Trait : public CallbackTrait<CallbackSingletonWithLeakTrait> { |
| 82 | static const bool kRegisterAtExit = false; |
| 83 | }; |
| 84 | |
| 85 | CallbackSingletonWithLeakTrait() : CallbackSingleton() { } |
| 86 | |
| 87 | static CallbackSingletonWithLeakTrait* GetInstance() { |
| 88 | return Singleton<CallbackSingletonWithLeakTrait, Trait>::get(); |
| 89 | } |
| 90 | }; |
| 91 | |
| 92 | class CallbackSingletonWithStaticTrait : public CallbackSingleton { |
| 93 | public: |
| 94 | struct Trait; |
| 95 | |
| 96 | CallbackSingletonWithStaticTrait() : CallbackSingleton() { } |
| 97 | |
| 98 | static CallbackSingletonWithStaticTrait* GetInstance() { |
| 99 | return Singleton<CallbackSingletonWithStaticTrait, Trait>::get(); |
| 100 | } |
| 101 | }; |
| 102 | |
| 103 | struct CallbackSingletonWithStaticTrait::Trait |
| 104 | : public StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait> { |
| 105 | static void Delete(CallbackSingletonWithStaticTrait* instance) { |
| 106 | if (instance->callback_) |
| 107 | (instance->callback_)(); |
| 108 | StaticMemorySingletonTraits<CallbackSingletonWithStaticTrait>::Delete( |
| 109 | instance); |
| 110 | } |
| 111 | }; |
| 112 | |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame^] | 113 | template <class Type> |
| 114 | class AlignedTestSingleton { |
| 115 | public: |
| 116 | AlignedTestSingleton() {} |
| 117 | ~AlignedTestSingleton() {} |
| 118 | static AlignedTestSingleton* GetInstance() { |
| 119 | return Singleton<AlignedTestSingleton, |
| 120 | StaticMemorySingletonTraits<AlignedTestSingleton> >::get(); |
| 121 | } |
| 122 | |
| 123 | Type type_; |
| 124 | }; |
| 125 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 126 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 127 | void SingletonNoLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 128 | CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | void SingletonLeak(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 132 | CallbackSingletonWithLeakTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | CallbackFunc* GetLeakySingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 136 | return &CallbackSingletonWithLeakTrait::GetInstance()->callback_; |
| 137 | } |
| 138 | |
| 139 | void DeleteLeakySingleton() { |
| 140 | DefaultSingletonTraits<CallbackSingletonWithLeakTrait>::Delete( |
| 141 | CallbackSingletonWithLeakTrait::GetInstance()); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 142 | } |
| 143 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 144 | void SingletonStatic(CallbackFunc CallOnQuit) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 145 | CallbackSingletonWithStaticTrait::GetInstance()->callback_ = CallOnQuit; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | CallbackFunc* GetStaticSingleton() { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 149 | return &CallbackSingletonWithStaticTrait::GetInstance()->callback_; |
| 150 | } |
| 151 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 152 | } // namespace |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 153 | |
| 154 | class SingletonTest : public testing::Test { |
| 155 | public: |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 156 | SingletonTest() { } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 157 | |
| 158 | virtual void SetUp() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 159 | non_leak_called_ = false; |
| 160 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 161 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 162 | } |
| 163 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 164 | protected: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 165 | void VerifiesCallbacks() { |
| 166 | EXPECT_TRUE(non_leak_called_); |
| 167 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 168 | EXPECT_TRUE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 169 | non_leak_called_ = false; |
| 170 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 171 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | void VerifiesCallbacksNotCalled() { |
| 175 | EXPECT_FALSE(non_leak_called_); |
| 176 | EXPECT_FALSE(leaky_called_); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 177 | EXPECT_FALSE(static_called_); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 178 | non_leak_called_ = false; |
| 179 | leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 180 | static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 181 | } |
| 182 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 183 | static void CallbackNoLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 184 | non_leak_called_ = true; |
| 185 | } |
| 186 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 187 | static void CallbackLeak() { |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 188 | leaky_called_ = true; |
| 189 | } |
| 190 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 191 | static void CallbackStatic() { |
| 192 | static_called_ = true; |
| 193 | } |
| 194 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 195 | private: |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 196 | static bool non_leak_called_; |
| 197 | static bool leaky_called_; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 198 | static bool static_called_; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 199 | }; |
| 200 | |
| 201 | bool SingletonTest::non_leak_called_ = false; |
| 202 | bool SingletonTest::leaky_called_ = false; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 203 | bool SingletonTest::static_called_ = false; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 204 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 205 | TEST_F(SingletonTest, Basic) { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 206 | int* singleton_int; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 207 | int* singleton_int_5; |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 208 | CallbackFunc* leaky_singleton; |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 209 | CallbackFunc* static_singleton; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 210 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 211 | { |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 212 | base::ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 213 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 214 | singleton_int = SingletonInt(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 215 | } |
| 216 | // Ensure POD type initialization. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 217 | EXPECT_EQ(*singleton_int, 0); |
| 218 | *singleton_int = 1; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 219 | |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 220 | EXPECT_EQ(singleton_int, SingletonInt()); |
| 221 | EXPECT_EQ(*singleton_int, 1); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 222 | |
| 223 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 224 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 225 | } |
| 226 | // Is default initialized to 5. |
| 227 | EXPECT_EQ(*singleton_int_5, 5); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 228 | |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 229 | SingletonNoLeak(&CallbackNoLeak); |
| 230 | SingletonLeak(&CallbackLeak); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 231 | SingletonStatic(&CallbackStatic); |
| 232 | static_singleton = GetStaticSingleton(); |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 233 | leaky_singleton = GetLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 234 | EXPECT_TRUE(leaky_singleton); |
| 235 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 236 | |
| 237 | // Verify that only the expected callback has been called. |
| 238 | VerifiesCallbacks(); |
[email protected] | 64e95e1 | 2011-08-17 17:41:02 | [diff] [blame] | 239 | // Delete the leaky singleton. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 240 | DeleteLeakySingleton(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 241 | |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 242 | // The static singleton can't be acquired post-atexit. |
| 243 | EXPECT_EQ(NULL, GetStaticSingleton()); |
| 244 | |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 245 | { |
[email protected] | 4ea927b | 2009-11-19 09:11:39 | [diff] [blame] | 246 | base::ShadowingAtExitManager sem; |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 247 | // Verifiy that the variables were reset. |
| 248 | { |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 249 | singleton_int = SingletonInt(); |
| 250 | EXPECT_EQ(*singleton_int, 0); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 251 | } |
| 252 | { |
[email protected] | 4ac8f67 | 2008-08-11 14:02:06 | [diff] [blame] | 253 | singleton_int_5 = SingletonInt5(); |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 254 | EXPECT_EQ(*singleton_int_5, 5); |
| 255 | } |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 256 | { |
| 257 | // Resurrect the static singleton, and assert that it |
| 258 | // still points to the same (static) memory. |
[email protected] | 625332e0 | 2010-12-14 07:48:49 | [diff] [blame] | 259 | CallbackSingletonWithStaticTrait::Trait::Resurrect(); |
[email protected] | 297d0e5 | 2010-05-07 15:24:49 | [diff] [blame] | 260 | EXPECT_EQ(GetStaticSingleton(), static_singleton); |
| 261 | } |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 262 | } |
| 263 | // The leaky singleton shouldn't leak since SingletonLeak has not been called. |
initial.commit | d7cae12 | 2008-07-26 21:49:38 | [diff] [blame] | 264 | VerifiesCallbacksNotCalled(); |
| 265 | } |
[email protected] | cd924d6 | 2012-02-23 17:52:20 | [diff] [blame^] | 266 | |
| 267 | #define EXPECT_ALIGNED(ptr, align) \ |
| 268 | EXPECT_EQ(0u, reinterpret_cast<uintptr_t>(ptr) & (align - 1)) |
| 269 | |
| 270 | TEST_F(SingletonTest, Alignment) { |
| 271 | using base::AlignedMemory; |
| 272 | |
| 273 | // Create some static singletons with increasing sizes and alignment |
| 274 | // requirements. By ordering this way, the linker will need to do some work to |
| 275 | // ensure proper alignment of the static data. |
| 276 | AlignedTestSingleton<int32>* align4 = |
| 277 | AlignedTestSingleton<int32>::GetInstance(); |
| 278 | AlignedTestSingleton<AlignedMemory<32, 32> >* align32 = |
| 279 | AlignedTestSingleton<AlignedMemory<32, 32> >::GetInstance(); |
| 280 | AlignedTestSingleton<AlignedMemory<128, 128> >* align128 = |
| 281 | AlignedTestSingleton<AlignedMemory<128, 128> >::GetInstance(); |
| 282 | AlignedTestSingleton<AlignedMemory<4096, 4096> >* align4096 = |
| 283 | AlignedTestSingleton<AlignedMemory<4096, 4096> >::GetInstance(); |
| 284 | |
| 285 | EXPECT_ALIGNED(align4, 4); |
| 286 | EXPECT_ALIGNED(align32, 32); |
| 287 | EXPECT_ALIGNED(align128, 128); |
| 288 | EXPECT_ALIGNED(align4096, 4096); |
| 289 | } |