1 | // |
2 | // SharedPtrTest.cpp |
3 | // |
4 | // Copyright (c) 2004-2006, Applied Informatics Software Engineering GmbH. |
5 | // and Contributors. |
6 | // |
7 | // SPDX-License-Identifier: BSL-1.0 |
8 | // |
9 | |
10 | |
11 | #include "SharedPtrTest.h" |
12 | #include "Poco/CppUnit/TestCaller.h" |
13 | #include "Poco/CppUnit/TestSuite.h" |
14 | #include "Poco/SharedPtr.h" |
15 | #include "Poco/Exception.h" |
16 | |
17 | |
18 | using Poco::SharedPtr; |
19 | using Poco::NullPointerException; |
20 | |
21 | |
22 | namespace |
23 | { |
24 | class TestObject |
25 | { |
26 | public: |
27 | TestObject(const std::string& rData): _data(rData) |
28 | { |
29 | ++_count; |
30 | } |
31 | |
32 | virtual ~TestObject() |
33 | { |
34 | --_count; |
35 | } |
36 | |
37 | const std::string& data() |
38 | { |
39 | return _data; |
40 | } |
41 | |
42 | static int count() |
43 | { |
44 | return _count; |
45 | } |
46 | |
47 | private: |
48 | std::string _data; |
49 | static int _count; |
50 | }; |
51 | |
52 | int TestObject::_count = 0; |
53 | |
54 | class DerivedObject: public TestObject |
55 | { |
56 | public: |
57 | DerivedObject(const std::string& s, int i): TestObject(s), _number(i) |
58 | { |
59 | } |
60 | |
61 | int number() const |
62 | { |
63 | return _number; |
64 | } |
65 | |
66 | private: |
67 | int _number; |
68 | }; |
69 | } |
70 | |
71 | |
72 | SharedPtrTest::SharedPtrTest(const std::string& rName): CppUnit::TestCase(rName) |
73 | { |
74 | } |
75 | |
76 | |
77 | SharedPtrTest::~SharedPtrTest() |
78 | { |
79 | } |
80 | |
81 | |
82 | void SharedPtrTest::testSharedPtr() |
83 | { |
84 | SharedPtr<TestObject> ptr1; |
85 | assertNull(ptr1.get()); |
86 | TestObject* pTO1 = new TestObject("one" ); |
87 | TestObject* pTO2 = new TestObject("two" ); |
88 | if (pTO2 < pTO1) |
89 | { |
90 | TestObject* pTmp = pTO1; |
91 | pTO1 = pTO2; |
92 | pTO2 = pTmp; |
93 | } |
94 | assertTrue (pTO1 < pTO2); |
95 | ptr1 = pTO1; |
96 | assertTrue (ptr1.referenceCount() == 1); |
97 | SharedPtr<TestObject> ptr2 = pTO2; |
98 | SharedPtr<TestObject> ptr3 = ptr1; |
99 | assertTrue (ptr1.referenceCount() == 2); |
100 | SharedPtr<TestObject> ptr4; |
101 | assertTrue (ptr1.get() == pTO1); |
102 | assertTrue (ptr1 == pTO1); |
103 | assertTrue (ptr2.get() == pTO2); |
104 | assertTrue (ptr2 == pTO2); |
105 | assertTrue (ptr3.get() == pTO1); |
106 | assertTrue (ptr3 == pTO1); |
107 | |
108 | assertTrue (ptr1 == pTO1); |
109 | assertTrue (ptr1 != pTO2); |
110 | assertTrue (ptr1 < pTO2); |
111 | assertTrue (ptr1 <= pTO2); |
112 | assertTrue (ptr2 > pTO1); |
113 | assertTrue (ptr2 >= pTO1); |
114 | |
115 | assertTrue (ptr1 == ptr3); |
116 | assertTrue (ptr1 != ptr2); |
117 | assertTrue (ptr1 < ptr2); |
118 | assertTrue (ptr1 <= ptr2); |
119 | assertTrue (ptr2 > ptr1); |
120 | assertTrue (ptr2 >= ptr1); |
121 | |
122 | ptr1.swap(ptr2); |
123 | assertTrue (ptr2 < ptr1); |
124 | ptr2.swap(ptr1); |
125 | |
126 | assertTrue ((ptr1->data() == "one" && ptr2->data() == "two" ) || (ptr1->data() == "two" && ptr2->data() == "one" )); |
127 | |
128 | try |
129 | { |
130 | assertTrue (ptr4->data() == "four" ); |
131 | fail ("must throw NullPointerException" ); |
132 | } |
133 | catch (NullPointerException&) |
134 | { |
135 | } |
136 | |
137 | assertTrue (!(ptr4 == ptr1)); |
138 | assertTrue (!(ptr4 == ptr2)); |
139 | assertTrue (ptr4 != ptr1); |
140 | assertTrue (ptr4 != ptr2); |
141 | |
142 | ptr4 = ptr2; |
143 | assertTrue (ptr4 == ptr2); |
144 | assertTrue (!(ptr4 != ptr2)); |
145 | |
146 | assertTrue (TestObject::count() == 2); |
147 | ptr1 = 0; |
148 | ptr2 = 0; |
149 | ptr3 = 0; |
150 | ptr4 = 0; |
151 | assertTrue (TestObject::count() == 0); |
152 | |
153 | { |
154 | SharedPtr<TestObject> ptr = new TestObject("" ); |
155 | assertTrue (TestObject::count() == 1); |
156 | } |
157 | assertTrue (TestObject::count() == 0); |
158 | |
159 | std::shared_ptr<TestObject> stdp(std::make_shared<TestObject>("" )); |
160 | Poco::SharedPtr<TestObject> ptr5(stdp); |
161 | std::shared_ptr<TestObject> stdp2 = ptr5; |
162 | |
163 | assertTrue (stdp == stdp2); |
164 | |
165 | std::shared_ptr<TestObject> stdp3(std::make_shared<TestObject>("" )); |
166 | TestObject* rptr = stdp3.get(); |
167 | Poco::SharedPtr<TestObject> ptr6(std::move(stdp3)); |
168 | |
169 | assertTrue (!stdp3); |
170 | assertTrue (ptr6.get() == rptr); |
171 | |
172 | std::shared_ptr<TestObject> stdp4(std::make_shared<TestObject>("" )); |
173 | rptr = stdp4.get(); |
174 | ptr6 = std::move(stdp4); |
175 | |
176 | assertTrue (!stdp4); |
177 | assertTrue (ptr6.get() == rptr); |
178 | } |
179 | |
180 | |
181 | void SharedPtrTest::testImplicitCast() |
182 | { |
183 | { |
184 | // null assign test |
185 | SharedPtr<DerivedObject> ptr2; |
186 | assertNull(ptr2.get()); |
187 | SharedPtr<TestObject> ptr1 = ptr2; |
188 | } |
189 | { |
190 | SharedPtr<DerivedObject> ptr2(new DerivedObject("test" , 666)); |
191 | assertTrue (TestObject::count() == 1); |
192 | SharedPtr<TestObject> ptr1 = ptr2; |
193 | assertTrue (TestObject::count() == 1); |
194 | } |
195 | assertTrue (TestObject::count() == 0); |
196 | SharedPtr<TestObject> ptr1 = new DerivedObject("test" , 666); |
197 | assertTrue (TestObject::count() == 1); |
198 | ptr1 = 0; |
199 | assertTrue (TestObject::count() == 0); |
200 | } |
201 | |
202 | |
203 | void SharedPtrTest::testExplicitCast() |
204 | { |
205 | SharedPtr<TestObject> ptr1 = new DerivedObject("test" , 666); |
206 | SharedPtr<DerivedObject> ptr2 = ptr1.cast<DerivedObject>(); |
207 | assertTrue (ptr2.get() != 0); |
208 | |
209 | // cast the other way round must fail |
210 | ptr1 = new TestObject("test" ); |
211 | assertTrue (TestObject::count() == 2); |
212 | ptr2 = ptr1.cast<DerivedObject>(); |
213 | assertTrue (TestObject::count() == 1); |
214 | assertTrue (ptr2.get() == 0); |
215 | } |
216 | |
217 | |
218 | void SharedPtrTest::setUp() |
219 | { |
220 | } |
221 | |
222 | |
223 | void SharedPtrTest::tearDown() |
224 | { |
225 | } |
226 | |
227 | |
228 | CppUnit::Test* SharedPtrTest::suite() |
229 | { |
230 | CppUnit::TestSuite* pSuite = new CppUnit::TestSuite("SharedPtrTest" ); |
231 | |
232 | CppUnit_addTest(pSuite, SharedPtrTest, testSharedPtr); |
233 | CppUnit_addTest(pSuite, SharedPtrTest, testImplicitCast); |
234 | CppUnit_addTest(pSuite, SharedPtrTest, testExplicitCast); |
235 | |
236 | return pSuite; |
237 | } |
238 | |