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