13f4cbf05Sopenharmony_ci/* 23f4cbf05Sopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd. 33f4cbf05Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43f4cbf05Sopenharmony_ci * you may not use this file except in compliance with the License. 53f4cbf05Sopenharmony_ci * You may obtain a copy of the License at 63f4cbf05Sopenharmony_ci * 73f4cbf05Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83f4cbf05Sopenharmony_ci * 93f4cbf05Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103f4cbf05Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113f4cbf05Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123f4cbf05Sopenharmony_ci * See the License for the specific language governing permissions and 133f4cbf05Sopenharmony_ci * limitations under the License. 143f4cbf05Sopenharmony_ci */ 153f4cbf05Sopenharmony_ci#include <gtest/gtest.h> 163f4cbf05Sopenharmony_ci#include "singleton.h" 173f4cbf05Sopenharmony_ci#include <algorithm> 183f4cbf05Sopenharmony_ci#include <iostream> 193f4cbf05Sopenharmony_ci#include <fstream> 203f4cbf05Sopenharmony_ciusing namespace testing::ext; 213f4cbf05Sopenharmony_ciusing namespace std; 223f4cbf05Sopenharmony_ci 233f4cbf05Sopenharmony_cinamespace OHOS { 243f4cbf05Sopenharmony_cinamespace { 253f4cbf05Sopenharmony_ciclass DelayedSingletonDeclearTest { 263f4cbf05Sopenharmony_ci DECLARE_DELAYED_SINGLETON(DelayedSingletonDeclearTest); 273f4cbf05Sopenharmony_cipublic: 283f4cbf05Sopenharmony_ci void* GetObjAddr() 293f4cbf05Sopenharmony_ci { 303f4cbf05Sopenharmony_ci return static_cast<void*>(this); 313f4cbf05Sopenharmony_ci } 323f4cbf05Sopenharmony_ci}; 333f4cbf05Sopenharmony_ci 343f4cbf05Sopenharmony_ciDelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {}; 353f4cbf05Sopenharmony_ciDelayedSingletonDeclearTest::DelayedSingletonDeclearTest() {}; 363f4cbf05Sopenharmony_ci 373f4cbf05Sopenharmony_ciclass SingletonDeclearTest { 383f4cbf05Sopenharmony_ci DECLARE_SINGLETON(SingletonDeclearTest); 393f4cbf05Sopenharmony_cipublic: 403f4cbf05Sopenharmony_ci void* GetObjAddr() 413f4cbf05Sopenharmony_ci { 423f4cbf05Sopenharmony_ci return static_cast<void*>(this); 433f4cbf05Sopenharmony_ci } 443f4cbf05Sopenharmony_ci}; 453f4cbf05Sopenharmony_ci 463f4cbf05Sopenharmony_ciSingletonDeclearTest::~SingletonDeclearTest() {}; 473f4cbf05Sopenharmony_ciSingletonDeclearTest::SingletonDeclearTest() {}; 483f4cbf05Sopenharmony_ci 493f4cbf05Sopenharmony_ciclass SingletonTest: public Singleton<SingletonTest> { 503f4cbf05Sopenharmony_cipublic: 513f4cbf05Sopenharmony_ci void* GetObjAddr() 523f4cbf05Sopenharmony_ci { 533f4cbf05Sopenharmony_ci return static_cast<void*>(this); 543f4cbf05Sopenharmony_ci } 553f4cbf05Sopenharmony_ci}; 563f4cbf05Sopenharmony_ci 573f4cbf05Sopenharmony_ciclass DelayedSingletonTest: public DelayedSingleton<DelayedSingletonTest> { 583f4cbf05Sopenharmony_cipublic: 593f4cbf05Sopenharmony_ci void* GetObjAddr() 603f4cbf05Sopenharmony_ci { 613f4cbf05Sopenharmony_ci return static_cast<void*>(this); 623f4cbf05Sopenharmony_ci } 633f4cbf05Sopenharmony_ci}; 643f4cbf05Sopenharmony_ci 653f4cbf05Sopenharmony_ci 663f4cbf05Sopenharmony_ciclass DelayedRefSingletonDeclearTest { 673f4cbf05Sopenharmony_ci DECLARE_DELAYED_REF_SINGLETON(DelayedRefSingletonDeclearTest); 683f4cbf05Sopenharmony_cipublic: 693f4cbf05Sopenharmony_ci void* GetObjAddr() 703f4cbf05Sopenharmony_ci { 713f4cbf05Sopenharmony_ci return static_cast<void*>(this); 723f4cbf05Sopenharmony_ci } 733f4cbf05Sopenharmony_ci}; 743f4cbf05Sopenharmony_ci 753f4cbf05Sopenharmony_ciDelayedRefSingletonDeclearTest::DelayedRefSingletonDeclearTest() {}; 763f4cbf05Sopenharmony_ciDelayedRefSingletonDeclearTest::~DelayedRefSingletonDeclearTest() {}; 773f4cbf05Sopenharmony_ci 783f4cbf05Sopenharmony_ciclass DelayedRefSingletonTest: public DelayedRefSingleton<DelayedRefSingletonTest> { 793f4cbf05Sopenharmony_cipublic: 803f4cbf05Sopenharmony_ci void* GetObjAddr() 813f4cbf05Sopenharmony_ci { 823f4cbf05Sopenharmony_ci return static_cast<void*>(this); 833f4cbf05Sopenharmony_ci } 843f4cbf05Sopenharmony_ci}; 853f4cbf05Sopenharmony_ci 863f4cbf05Sopenharmony_ci 873f4cbf05Sopenharmony_ciclass UtilsSingletonTest : public testing::Test 883f4cbf05Sopenharmony_ci{ 893f4cbf05Sopenharmony_cipublic : 903f4cbf05Sopenharmony_ci static void SetUpTestCase(void); 913f4cbf05Sopenharmony_ci static void TearDownTestCase(void); 923f4cbf05Sopenharmony_ci void SetUp(); 933f4cbf05Sopenharmony_ci void TearDown(); 943f4cbf05Sopenharmony_ci}; 953f4cbf05Sopenharmony_ci 963f4cbf05Sopenharmony_civoid UtilsSingletonTest::SetUpTestCase(void) 973f4cbf05Sopenharmony_ci{ 983f4cbf05Sopenharmony_ci // step 2: input testsuit setup step 993f4cbf05Sopenharmony_ci} 1003f4cbf05Sopenharmony_ci 1013f4cbf05Sopenharmony_civoid UtilsSingletonTest::TearDownTestCase(void) 1023f4cbf05Sopenharmony_ci{ 1033f4cbf05Sopenharmony_ci // step 2: input testsuit teardown step 1043f4cbf05Sopenharmony_ci} 1053f4cbf05Sopenharmony_ci 1063f4cbf05Sopenharmony_civoid UtilsSingletonTest::SetUp(void) 1073f4cbf05Sopenharmony_ci{ 1083f4cbf05Sopenharmony_ci // step 3: input testcase setup step 1093f4cbf05Sopenharmony_ci} 1103f4cbf05Sopenharmony_ci 1113f4cbf05Sopenharmony_civoid UtilsSingletonTest::TearDown(void) 1123f4cbf05Sopenharmony_ci{ 1133f4cbf05Sopenharmony_ci // step 3: input testcase teardown step 1143f4cbf05Sopenharmony_ci} 1153f4cbf05Sopenharmony_ci 1163f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_DelayedSingletonDeclearTest, TestSize.Level0) 1173f4cbf05Sopenharmony_ci{ 1183f4cbf05Sopenharmony_ci shared_ptr<DelayedSingletonDeclearTest> sp1 = DelayedSingleton<DelayedSingletonDeclearTest>::GetInstance(); 1193f4cbf05Sopenharmony_ci EXPECT_EQ(sp1.use_count(), 2); 1203f4cbf05Sopenharmony_ci shared_ptr<DelayedSingletonDeclearTest> sp2 = DelayedSingleton<DelayedSingletonDeclearTest>::GetInstance(); 1213f4cbf05Sopenharmony_ci EXPECT_EQ(sp1->GetObjAddr(), sp2->GetObjAddr()); 1223f4cbf05Sopenharmony_ci EXPECT_EQ(sp1.get(), sp2.get()); 1233f4cbf05Sopenharmony_ci EXPECT_EQ(sp2.use_count(), 3); 1243f4cbf05Sopenharmony_ci} 1253f4cbf05Sopenharmony_ci 1263f4cbf05Sopenharmony_ci 1273f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_SingletonDeclearTest, TestSize.Level0) 1283f4cbf05Sopenharmony_ci{ 1293f4cbf05Sopenharmony_ci SingletonDeclearTest &st1 = Singleton<SingletonDeclearTest>::GetInstance(); 1303f4cbf05Sopenharmony_ci SingletonDeclearTest &st2 = Singleton<SingletonDeclearTest>::GetInstance(); 1313f4cbf05Sopenharmony_ci EXPECT_EQ(st1.GetObjAddr(), st2.GetObjAddr()); 1323f4cbf05Sopenharmony_ci} 1333f4cbf05Sopenharmony_ci 1343f4cbf05Sopenharmony_ci 1353f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_SingletonTest, TestSize.Level0) 1363f4cbf05Sopenharmony_ci{ 1373f4cbf05Sopenharmony_ci SingletonTest &st1 = SingletonTest::GetInstance(); 1383f4cbf05Sopenharmony_ci SingletonTest &st2 = SingletonTest::GetInstance(); 1393f4cbf05Sopenharmony_ci EXPECT_EQ(st1.GetObjAddr(), st2.GetObjAddr()); 1403f4cbf05Sopenharmony_ci} 1413f4cbf05Sopenharmony_ci 1423f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_DestroyInstanceTest, TestSize.Level0) 1433f4cbf05Sopenharmony_ci{ 1443f4cbf05Sopenharmony_ci shared_ptr<DelayedSingletonTest> sp1 = DelayedSingletonTest::GetInstance(); 1453f4cbf05Sopenharmony_ci auto oldInstance = sp1; 1463f4cbf05Sopenharmony_ci sp1->DestroyInstance(); 1473f4cbf05Sopenharmony_ci 1483f4cbf05Sopenharmony_ci auto newInstance = sp1->GetInstance(); 1493f4cbf05Sopenharmony_ci ASSERT_NE(oldInstance->GetObjAddr(), newInstance->GetObjAddr()); 1503f4cbf05Sopenharmony_ci} 1513f4cbf05Sopenharmony_ci 1523f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_DelayedSingletonTest, TestSize.Level0) 1533f4cbf05Sopenharmony_ci{ 1543f4cbf05Sopenharmony_ci shared_ptr<DelayedSingletonTest> sp1 = DelayedSingletonTest::GetInstance(); 1553f4cbf05Sopenharmony_ci EXPECT_EQ(sp1.use_count(), 2); 1563f4cbf05Sopenharmony_ci shared_ptr<DelayedSingletonTest> sp2 = DelayedSingletonTest::GetInstance(); 1573f4cbf05Sopenharmony_ci EXPECT_EQ(sp1->GetObjAddr(), sp2->GetObjAddr()); 1583f4cbf05Sopenharmony_ci EXPECT_EQ(sp1.get(), sp2.get()); 1593f4cbf05Sopenharmony_ci EXPECT_EQ(sp2.use_count(), 3); 1603f4cbf05Sopenharmony_ci} 1613f4cbf05Sopenharmony_ci 1623f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonTest, TestSize.Level0) 1633f4cbf05Sopenharmony_ci{ 1643f4cbf05Sopenharmony_ci DelayedRefSingletonTest& p1 = DelayedRefSingletonTest::GetInstance(); 1653f4cbf05Sopenharmony_ci DelayedRefSingletonTest& p2 = DelayedRefSingletonTest::GetInstance(); 1663f4cbf05Sopenharmony_ci EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr()); 1673f4cbf05Sopenharmony_ci} 1683f4cbf05Sopenharmony_ci 1693f4cbf05Sopenharmony_ciHWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonDeclearTest, TestSize.Level0) 1703f4cbf05Sopenharmony_ci{ 1713f4cbf05Sopenharmony_ci DelayedRefSingletonDeclearTest& p1 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance(); 1723f4cbf05Sopenharmony_ci DelayedRefSingletonDeclearTest& p2 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance(); 1733f4cbf05Sopenharmony_ci EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr()); 1743f4cbf05Sopenharmony_ci} 1753f4cbf05Sopenharmony_ci} // namespace 1763f4cbf05Sopenharmony_ci} // namespace OHOS