1 /*-------------------------------------------------------------------------
2 * drawElements C++ Base Library
3 * -----------------------------
5 * Copyright 2014 The Android Open Source Project
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
21 * \brief Shared pointer.
22 *//*--------------------------------------------------------------------*/
24 #include "deSharedPtr.hpp"
25 #include "deThread.hpp"
38 THREAD_TEST_TIME = 200*1000
50 virtual ~Object (void)
59 class DerivedObject : public Object
62 DerivedObject (bool& exists)
68 class SharedPtrTestThread : public Thread
71 SharedPtrTestThread (const SharedPtr<Object>& ptr, const bool& exists)
79 deUint64 startTime = deGetMicroseconds();
84 if (((cnt&(1<<14)) != 0) && (deGetMicroseconds()-startTime >= THREAD_TEST_TIME))
88 SharedPtr<Object> ptrA(m_ptr);
90 SharedPtr<Object> ptrB;
92 ptrA = SharedPtr<Object>();
95 DE_TEST_ASSERT(m_exists);
100 SharedPtr<Object> m_ptr;
101 const bool& m_exists;
104 class WeakPtrTestThread : public Thread
107 WeakPtrTestThread (const SharedPtr<Object>& ptr, const bool& exists)
115 deUint64 startTime = deGetMicroseconds();
120 if (((cnt&(1<<14)) != 0) && (deGetMicroseconds()-startTime >= THREAD_TEST_TIME))
124 WeakPtr<Object> ptrA(m_ptr);
126 WeakPtr<Object> ptrB;
128 ptrA = SharedPtr<Object>();
131 DE_TEST_ASSERT(m_exists);
136 SharedPtr<Object> m_ptr;
137 const bool& m_exists;
140 SharedPtr<Object> makeObject (bool& exists)
142 return SharedPtr<Object>(new Object(exists));
147 CustomDeleter (bool* called) : m_called(called) {}
149 void operator() (Object* ptr)
151 DE_TEST_ASSERT(!*m_called);
161 void SharedPtr_selfTest (void)
163 // Empty pointer test.
165 SharedPtr<Object> ptr;
166 DE_TEST_ASSERT(ptr.get() == DE_NULL);
167 DE_TEST_ASSERT(!ptr);
170 // Empty pointer copy.
172 SharedPtr<Object> ptrA;
173 SharedPtr<Object> ptrB(ptrA);
174 DE_TEST_ASSERT(ptrB.get() == DE_NULL);
177 // Empty pointer assignment.
179 SharedPtr<Object> ptrA;
180 SharedPtr<Object> ptrB;
189 SharedPtr<Object> ptr(new Object(exists));
190 DE_TEST_ASSERT(exists);
191 DE_TEST_ASSERT(ptr.get() != DE_NULL);
194 DE_TEST_ASSERT(!exists);
202 SharedPtr<Object> ptr(new Object(exists));
203 DE_TEST_ASSERT(exists);
204 DE_TEST_ASSERT(ptr.get() != DE_NULL);
205 throw std::exception();
207 catch (const std::exception&)
209 DE_TEST_ASSERT(!exists);
211 DE_TEST_ASSERT(!exists);
217 bool test = (SharedPtr<Object>(new Object(exists))).get() != DE_NULL && exists;
218 DE_TEST_ASSERT(!exists);
219 DE_TEST_ASSERT(test);
225 SharedPtr<Object> ptr(new Object(exists));
226 DE_TEST_ASSERT(exists);
227 ptr = SharedPtr<Object>();
228 DE_TEST_ASSERT(!exists);
231 // Self-assignment test.
235 SharedPtr<Object> ptr(new Object(exists));
236 DE_TEST_ASSERT(exists);
237 DE_TEST_ASSERT(ptr.get() != DE_NULL);
240 DE_TEST_ASSERT(!exists);
243 // Basic multi-reference via copy ctor.
247 SharedPtr<Object> ptrA(new Object(exists));
248 DE_TEST_ASSERT(exists);
250 SharedPtr<Object> ptrB(ptrA);
251 DE_TEST_ASSERT(exists);
253 DE_TEST_ASSERT(exists);
255 DE_TEST_ASSERT(!exists);
258 // Basic multi-reference via assignment to empty.
262 SharedPtr<Object> ptrA(new Object(exists));
263 DE_TEST_ASSERT(exists);
265 SharedPtr<Object> ptrB;
267 DE_TEST_ASSERT(exists);
269 DE_TEST_ASSERT(exists);
271 DE_TEST_ASSERT(!exists);
274 // Multi-reference via assignment to non-empty.
276 bool existsA = false;
277 bool existsB = false;
279 SharedPtr<Object> ptrA(new Object(existsA));
280 DE_TEST_ASSERT(existsA);
282 SharedPtr<Object> ptrB(new Object(existsB));
283 DE_TEST_ASSERT(existsB);
285 DE_TEST_ASSERT(!existsA);
286 DE_TEST_ASSERT(existsB);
288 DE_TEST_ASSERT(existsB);
290 DE_TEST_ASSERT(!existsB);
293 // Return from function.
297 SharedPtr<Object> ptr;
298 ptr = makeObject(exists);
299 DE_TEST_ASSERT(exists);
301 DE_TEST_ASSERT(!exists);
304 // Equality comparison.
306 bool existsA = false;
307 bool existsB = false;
308 SharedPtr<Object> ptrA(new Object(existsA));
309 SharedPtr<Object> ptrB(new Object(existsB));
310 SharedPtr<Object> ptrC(ptrA);
312 DE_TEST_ASSERT(ptrA == ptrA);
313 DE_TEST_ASSERT(ptrA != ptrB);
314 DE_TEST_ASSERT(ptrA == ptrC);
315 DE_TEST_ASSERT(ptrC != ptrB);
318 // Conversion via assignment.
322 SharedPtr<Object> basePtr;
324 SharedPtr<DerivedObject> derivedPtr(new DerivedObject(exists));
325 DE_TEST_ASSERT(exists);
326 basePtr = derivedPtr;
327 DE_TEST_ASSERT(exists);
329 DE_TEST_ASSERT(exists);
331 DE_TEST_ASSERT(!exists);
334 // Conversion via copy ctor.
338 SharedPtr<DerivedObject> derivedPtr (new DerivedObject(exists));
339 SharedPtr<Object> basePtr (derivedPtr);
340 DE_TEST_ASSERT(exists);
341 derivedPtr = SharedPtr<DerivedObject>();
342 DE_TEST_ASSERT(exists);
344 DE_TEST_ASSERT(!exists);
347 // Explicit conversion operator.
351 SharedPtr<DerivedObject> derivedPtr (new DerivedObject(exists));
352 DE_TEST_ASSERT(exists);
354 SharedPtr<Object> basePtr = (SharedPtr<Object>)(derivedPtr);
355 derivedPtr = SharedPtr<DerivedObject>();
356 DE_TEST_ASSERT(exists);
358 DE_TEST_ASSERT(!exists);
361 // Basic weak reference.
364 SharedPtr<Object> ptr(new Object(exists));
365 DE_TEST_ASSERT(exists);
367 WeakPtr<Object> weakPtr(ptr);
370 SharedPtr<Object> newRef(weakPtr);
371 DE_TEST_ASSERT(exists);
373 catch (const DeadReferenceException&)
375 DE_TEST_ASSERT(false);
378 ptr = SharedPtr<Object>();
379 DE_TEST_ASSERT(!exists);
382 SharedPtr<Object> newRef(weakPtr);
383 DE_TEST_ASSERT(false);
385 catch (const DeadReferenceException&)
390 // Basic SharedPtr threaded test.
394 SharedPtr<Object> ptr(new Object(exists));
396 SharedPtrTestThread threadA(ptr, exists);
397 SharedPtrTestThread threadB(ptr, exists);
404 DE_TEST_ASSERT(exists);
406 DE_TEST_ASSERT(!exists);
409 // Basic WeakPtr threaded test.
413 SharedPtr<Object> ptr(new Object(exists));
414 WeakPtrTestThread threadA(ptr, exists);
415 WeakPtrTestThread threadB(ptr, exists);
422 DE_TEST_ASSERT(exists);
424 DE_TEST_ASSERT(!exists);
427 // Basic custom deleter.
430 bool deleterCalled = false;
432 SharedPtr<Object> ptr(new Object(exists), CustomDeleter(&deleterCalled));
433 DE_TEST_ASSERT(exists);
434 DE_TEST_ASSERT(!deleterCalled);
435 DE_TEST_ASSERT(ptr.get() != DE_NULL);
437 DE_TEST_ASSERT(!exists);
438 DE_TEST_ASSERT(deleterCalled);