1/* 2 * Copyright (c) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15#include <gtest/gtest.h> 16#include "singleton.h" 17#include <algorithm> 18#include <iostream> 19#include <fstream> 20using namespace testing::ext; 21using namespace std; 22 23namespace OHOS { 24namespace { 25class DelayedSingletonDeclearTest { 26 DECLARE_DELAYED_SINGLETON(DelayedSingletonDeclearTest); 27public: 28 void* GetObjAddr() 29 { 30 return static_cast<void*>(this); 31 } 32}; 33 34DelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {}; 35DelayedSingletonDeclearTest::DelayedSingletonDeclearTest() {}; 36 37class SingletonDeclearTest { 38 DECLARE_SINGLETON(SingletonDeclearTest); 39public: 40 void* GetObjAddr() 41 { 42 return static_cast<void*>(this); 43 } 44}; 45 46SingletonDeclearTest::~SingletonDeclearTest() {}; 47SingletonDeclearTest::SingletonDeclearTest() {}; 48 49class SingletonTest: public Singleton<SingletonTest> { 50public: 51 void* GetObjAddr() 52 { 53 return static_cast<void*>(this); 54 } 55}; 56 57class DelayedSingletonTest: public DelayedSingleton<DelayedSingletonTest> { 58public: 59 void* GetObjAddr() 60 { 61 return static_cast<void*>(this); 62 } 63}; 64 65 66class DelayedRefSingletonDeclearTest { 67 DECLARE_DELAYED_REF_SINGLETON(DelayedRefSingletonDeclearTest); 68public: 69 void* GetObjAddr() 70 { 71 return static_cast<void*>(this); 72 } 73}; 74 75DelayedRefSingletonDeclearTest::DelayedRefSingletonDeclearTest() {}; 76DelayedRefSingletonDeclearTest::~DelayedRefSingletonDeclearTest() {}; 77 78class DelayedRefSingletonTest: public DelayedRefSingleton<DelayedRefSingletonTest> { 79public: 80 void* GetObjAddr() 81 { 82 return static_cast<void*>(this); 83 } 84}; 85 86 87class UtilsSingletonTest : public testing::Test 88{ 89public : 90 static void SetUpTestCase(void); 91 static void TearDownTestCase(void); 92 void SetUp(); 93 void TearDown(); 94}; 95 96void UtilsSingletonTest::SetUpTestCase(void) 97{ 98 // step 2: input testsuit setup step 99} 100 101void UtilsSingletonTest::TearDownTestCase(void) 102{ 103 // step 2: input testsuit teardown step 104} 105 106void UtilsSingletonTest::SetUp(void) 107{ 108 // step 3: input testcase setup step 109} 110 111void UtilsSingletonTest::TearDown(void) 112{ 113 // step 3: input testcase teardown step 114} 115 116HWTEST_F(UtilsSingletonTest, test_DelayedSingletonDeclearTest, TestSize.Level0) 117{ 118 shared_ptr<DelayedSingletonDeclearTest> sp1 = DelayedSingleton<DelayedSingletonDeclearTest>::GetInstance(); 119 EXPECT_EQ(sp1.use_count(), 2); 120 shared_ptr<DelayedSingletonDeclearTest> sp2 = DelayedSingleton<DelayedSingletonDeclearTest>::GetInstance(); 121 EXPECT_EQ(sp1->GetObjAddr(), sp2->GetObjAddr()); 122 EXPECT_EQ(sp1.get(), sp2.get()); 123 EXPECT_EQ(sp2.use_count(), 3); 124} 125 126 127HWTEST_F(UtilsSingletonTest, test_SingletonDeclearTest, TestSize.Level0) 128{ 129 SingletonDeclearTest &st1 = Singleton<SingletonDeclearTest>::GetInstance(); 130 SingletonDeclearTest &st2 = Singleton<SingletonDeclearTest>::GetInstance(); 131 EXPECT_EQ(st1.GetObjAddr(), st2.GetObjAddr()); 132} 133 134 135HWTEST_F(UtilsSingletonTest, test_SingletonTest, TestSize.Level0) 136{ 137 SingletonTest &st1 = SingletonTest::GetInstance(); 138 SingletonTest &st2 = SingletonTest::GetInstance(); 139 EXPECT_EQ(st1.GetObjAddr(), st2.GetObjAddr()); 140} 141 142HWTEST_F(UtilsSingletonTest, test_DestroyInstanceTest, TestSize.Level0) 143{ 144 shared_ptr<DelayedSingletonTest> sp1 = DelayedSingletonTest::GetInstance(); 145 auto oldInstance = sp1; 146 sp1->DestroyInstance(); 147 148 auto newInstance = sp1->GetInstance(); 149 ASSERT_NE(oldInstance->GetObjAddr(), newInstance->GetObjAddr()); 150} 151 152HWTEST_F(UtilsSingletonTest, test_DelayedSingletonTest, TestSize.Level0) 153{ 154 shared_ptr<DelayedSingletonTest> sp1 = DelayedSingletonTest::GetInstance(); 155 EXPECT_EQ(sp1.use_count(), 2); 156 shared_ptr<DelayedSingletonTest> sp2 = DelayedSingletonTest::GetInstance(); 157 EXPECT_EQ(sp1->GetObjAddr(), sp2->GetObjAddr()); 158 EXPECT_EQ(sp1.get(), sp2.get()); 159 EXPECT_EQ(sp2.use_count(), 3); 160} 161 162HWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonTest, TestSize.Level0) 163{ 164 DelayedRefSingletonTest& p1 = DelayedRefSingletonTest::GetInstance(); 165 DelayedRefSingletonTest& p2 = DelayedRefSingletonTest::GetInstance(); 166 EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr()); 167} 168 169HWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonDeclearTest, TestSize.Level0) 170{ 171 DelayedRefSingletonDeclearTest& p1 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance(); 172 DelayedRefSingletonDeclearTest& p2 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance(); 173 EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr()); 174} 175} // namespace 176} // namespace OHOS