Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(203)

Side by Side Diff: base/memory/singleton_unittest.cc

Issue 1308823002: Move Singleton and related structs to namespace base (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: ToT Created 5 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/memory/singleton.h ('k') | base/test/test_support_android.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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/at_exit.h" 5 #include "base/at_exit.h"
6 #include "base/memory/singleton.h" 6 #include "base/memory/singleton.h"
7 #include "testing/gtest/include/gtest/gtest.h" 7 #include "testing/gtest/include/gtest/gtest.h"
8 8
9 namespace base {
9 namespace { 10 namespace {
10 11
11 COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a); 12 COMPILE_ASSERT(DefaultSingletonTraits<int>::kRegisterAtExit == true, a);
12 13
13 typedef void (*CallbackFunc)(); 14 typedef void (*CallbackFunc)();
14 15
15 class IntSingleton { 16 class IntSingleton {
16 public: 17 public:
17 static IntSingleton* GetInstance() { 18 static IntSingleton* GetInstance() {
18 return Singleton<IntSingleton>::get(); 19 return Singleton<IntSingleton>::get();
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 } 109 }
109 }; 110 };
110 111
111 template <class Type> 112 template <class Type>
112 class AlignedTestSingleton { 113 class AlignedTestSingleton {
113 public: 114 public:
114 AlignedTestSingleton() {} 115 AlignedTestSingleton() {}
115 ~AlignedTestSingleton() {} 116 ~AlignedTestSingleton() {}
116 static AlignedTestSingleton* GetInstance() { 117 static AlignedTestSingleton* GetInstance() {
117 return Singleton<AlignedTestSingleton, 118 return Singleton<AlignedTestSingleton,
118 StaticMemorySingletonTraits<AlignedTestSingleton> >::get(); 119 StaticMemorySingletonTraits<AlignedTestSingleton>>::get();
119 } 120 }
120 121
121 Type type_; 122 Type type_;
122 }; 123 };
123 124
124 125
125 void SingletonNoLeak(CallbackFunc CallOnQuit) { 126 void SingletonNoLeak(CallbackFunc CallOnQuit) {
126 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit; 127 CallbackSingletonWithNoLeakTrait::GetInstance()->callback_ = CallOnQuit;
127 } 128 }
128 129
(...skipping 11 matching lines...) Expand all
140 } 141 }
141 142
142 void SingletonStatic(CallbackFunc CallOnQuit) { 143 void SingletonStatic(CallbackFunc CallOnQuit) {
143 CallbackSingletonWithStaticTrait::GetInstance()->callback_ = CallOnQuit; 144 CallbackSingletonWithStaticTrait::GetInstance()->callback_ = CallOnQuit;
144 } 145 }
145 146
146 CallbackFunc* GetStaticSingleton() { 147 CallbackFunc* GetStaticSingleton() {
147 return &CallbackSingletonWithStaticTrait::GetInstance()->callback_; 148 return &CallbackSingletonWithStaticTrait::GetInstance()->callback_;
148 } 149 }
149 150
150 } // namespace
151 151
152 class SingletonTest : public testing::Test { 152 class SingletonTest : public testing::Test {
153 public: 153 public:
154 SingletonTest() {} 154 SingletonTest() {}
155 155
156 void SetUp() override { 156 void SetUp() override {
157 non_leak_called_ = false; 157 non_leak_called_ = false;
158 leaky_called_ = false; 158 leaky_called_ = false;
159 static_called_ = false; 159 static_called_ = false;
160 } 160 }
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after
200 bool SingletonTest::leaky_called_ = false; 200 bool SingletonTest::leaky_called_ = false;
201 bool SingletonTest::static_called_ = false; 201 bool SingletonTest::static_called_ = false;
202 202
203 TEST_F(SingletonTest, Basic) { 203 TEST_F(SingletonTest, Basic) {
204 int* singleton_int; 204 int* singleton_int;
205 int* singleton_int_5; 205 int* singleton_int_5;
206 CallbackFunc* leaky_singleton; 206 CallbackFunc* leaky_singleton;
207 CallbackFunc* static_singleton; 207 CallbackFunc* static_singleton;
208 208
209 { 209 {
210 base::ShadowingAtExitManager sem; 210 ShadowingAtExitManager sem;
211 { 211 {
212 singleton_int = SingletonInt(); 212 singleton_int = SingletonInt();
213 } 213 }
214 // Ensure POD type initialization. 214 // Ensure POD type initialization.
215 EXPECT_EQ(*singleton_int, 0); 215 EXPECT_EQ(*singleton_int, 0);
216 *singleton_int = 1; 216 *singleton_int = 1;
217 217
218 EXPECT_EQ(singleton_int, SingletonInt()); 218 EXPECT_EQ(singleton_int, SingletonInt());
219 EXPECT_EQ(*singleton_int, 1); 219 EXPECT_EQ(*singleton_int, 1);
220 220
(...skipping 13 matching lines...) Expand all
234 234
235 // Verify that only the expected callback has been called. 235 // Verify that only the expected callback has been called.
236 VerifiesCallbacks(); 236 VerifiesCallbacks();
237 // Delete the leaky singleton. 237 // Delete the leaky singleton.
238 DeleteLeakySingleton(); 238 DeleteLeakySingleton();
239 239
240 // The static singleton can't be acquired post-atexit. 240 // The static singleton can't be acquired post-atexit.
241 EXPECT_EQ(NULL, GetStaticSingleton()); 241 EXPECT_EQ(NULL, GetStaticSingleton());
242 242
243 { 243 {
244 base::ShadowingAtExitManager sem; 244 ShadowingAtExitManager sem;
245 // Verifiy that the variables were reset. 245 // Verifiy that the variables were reset.
246 { 246 {
247 singleton_int = SingletonInt(); 247 singleton_int = SingletonInt();
248 EXPECT_EQ(*singleton_int, 0); 248 EXPECT_EQ(*singleton_int, 0);
249 } 249 }
250 { 250 {
251 singleton_int_5 = SingletonInt5(); 251 singleton_int_5 = SingletonInt5();
252 EXPECT_EQ(*singleton_int_5, 5); 252 EXPECT_EQ(*singleton_int_5, 5);
253 } 253 }
254 { 254 {
(...skipping 23 matching lines...) Expand all
278 AlignedTestSingleton<AlignedMemory<128, 128> >* align128 = 278 AlignedTestSingleton<AlignedMemory<128, 128> >* align128 =
279 AlignedTestSingleton<AlignedMemory<128, 128> >::GetInstance(); 279 AlignedTestSingleton<AlignedMemory<128, 128> >::GetInstance();
280 AlignedTestSingleton<AlignedMemory<4096, 4096> >* align4096 = 280 AlignedTestSingleton<AlignedMemory<4096, 4096> >* align4096 =
281 AlignedTestSingleton<AlignedMemory<4096, 4096> >::GetInstance(); 281 AlignedTestSingleton<AlignedMemory<4096, 4096> >::GetInstance();
282 282
283 EXPECT_ALIGNED(align4, 4); 283 EXPECT_ALIGNED(align4, 4);
284 EXPECT_ALIGNED(align32, 32); 284 EXPECT_ALIGNED(align32, 32);
285 EXPECT_ALIGNED(align128, 128); 285 EXPECT_ALIGNED(align128, 128);
286 EXPECT_ALIGNED(align4096, 4096); 286 EXPECT_ALIGNED(align4096, 4096);
287 } 287 }
288
289 } // namespace
290 } // namespace base
OLDNEW
« no previous file with comments | « base/memory/singleton.h ('k') | base/test/test_support_android.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698