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>
20 using namespace testing::ext;
21 using namespace std;
22
23 namespace OHOS {
24 namespace {
25 class DelayedSingletonDeclearTest {
26 DECLARE_DELAYED_SINGLETON(DelayedSingletonDeclearTest);
27 public:
GetObjAddr()28 void* GetObjAddr()
29 {
30 return static_cast<void*>(this);
31 }
32 };
33
~DelayedSingletonDeclearTest()34 DelayedSingletonDeclearTest::~DelayedSingletonDeclearTest() {};
DelayedSingletonDeclearTest()35 DelayedSingletonDeclearTest::DelayedSingletonDeclearTest() {};
36
37 class SingletonDeclearTest {
38 DECLARE_SINGLETON(SingletonDeclearTest);
39 public:
GetObjAddr()40 void* GetObjAddr()
41 {
42 return static_cast<void*>(this);
43 }
44 };
45
~SingletonDeclearTest()46 SingletonDeclearTest::~SingletonDeclearTest() {};
SingletonDeclearTest()47 SingletonDeclearTest::SingletonDeclearTest() {};
48
49 class SingletonTest: public Singleton<SingletonTest> {
50 public:
GetObjAddr()51 void* GetObjAddr()
52 {
53 return static_cast<void*>(this);
54 }
55 };
56
57 class DelayedSingletonTest: public DelayedSingleton<DelayedSingletonTest> {
58 public:
GetObjAddr()59 void* GetObjAddr()
60 {
61 return static_cast<void*>(this);
62 }
63 };
64
65
66 class DelayedRefSingletonDeclearTest {
67 DECLARE_DELAYED_REF_SINGLETON(DelayedRefSingletonDeclearTest);
68 public:
GetObjAddr()69 void* GetObjAddr()
70 {
71 return static_cast<void*>(this);
72 }
73 };
74
DelayedRefSingletonDeclearTest()75 DelayedRefSingletonDeclearTest::DelayedRefSingletonDeclearTest() {};
~DelayedRefSingletonDeclearTest()76 DelayedRefSingletonDeclearTest::~DelayedRefSingletonDeclearTest() {};
77
78 class DelayedRefSingletonTest: public DelayedRefSingleton<DelayedRefSingletonTest> {
79 public:
GetObjAddr()80 void* GetObjAddr()
81 {
82 return static_cast<void*>(this);
83 }
84 };
85
86
87 class UtilsSingletonTest : public testing::Test
88 {
89 public :
90 static void SetUpTestCase(void);
91 static void TearDownTestCase(void);
92 void SetUp();
93 void TearDown();
94 };
95
SetUpTestCase(void)96 void UtilsSingletonTest::SetUpTestCase(void)
97 {
98 // step 2: input testsuit setup step
99 }
100
TearDownTestCase(void)101 void UtilsSingletonTest::TearDownTestCase(void)
102 {
103 // step 2: input testsuit teardown step
104 }
105
SetUp(void)106 void UtilsSingletonTest::SetUp(void)
107 {
108 // step 3: input testcase setup step
109 }
110
TearDown(void)111 void UtilsSingletonTest::TearDown(void)
112 {
113 // step 3: input testcase teardown step
114 }
115
116 HWTEST_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
127 HWTEST_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
135 HWTEST_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
142
143 HWTEST_F(UtilsSingletonTest, test_DelayedSingletonTest, TestSize.Level0)
144 {
145 shared_ptr<DelayedSingletonTest> sp1 = DelayedSingletonTest::GetInstance();
146 EXPECT_EQ(sp1.use_count(), 2);
147 shared_ptr<DelayedSingletonTest> sp2 = DelayedSingletonTest::GetInstance();
148 EXPECT_EQ(sp1->GetObjAddr(), sp2->GetObjAddr());
149 EXPECT_EQ(sp1.get(), sp2.get());
150 EXPECT_EQ(sp2.use_count(), 3);
151 }
152
153 HWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonTest, TestSize.Level0)
154 {
155 DelayedRefSingletonTest& p1 = DelayedRefSingletonTest::GetInstance();
156 DelayedRefSingletonTest& p2 = DelayedRefSingletonTest::GetInstance();
157 EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr());
158 }
159
160 HWTEST_F(UtilsSingletonTest, test_DelayedRefSingletonDeclearTest, TestSize.Level0)
161 {
162 DelayedRefSingletonDeclearTest& p1 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance();
163 DelayedRefSingletonDeclearTest& p2 = DelayedRefSingleton<DelayedRefSingletonDeclearTest>::GetInstance();
164 EXPECT_EQ(p1.GetObjAddr(), p2.GetObjAddr());
165 }
166 } // namespace
167 } // namespace OHOS