13c827367444ee418f129b2c238299f49d3264554Jarkko Poyry/*-------------------------------------------------------------------------
23c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * drawElements C++ Base Library
33c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * -----------------------------
43c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
53c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Copyright 2014 The Android Open Source Project
63c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
73c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Licensed under the Apache License, Version 2.0 (the "License");
83c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * you may not use this file except in compliance with the License.
93c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * You may obtain a copy of the License at
103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *      http://www.apache.org/licenses/LICENSE-2.0
123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * Unless required by applicable law or agreed to in writing, software
143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * distributed under the License is distributed on an "AS IS" BASIS,
153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * See the License for the specific language governing permissions and
173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * limitations under the License.
183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *
193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*!
203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \file
213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry * \brief Shared pointer.
223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry *//*--------------------------------------------------------------------*/
233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deSharedPtr.hpp"
253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deThread.hpp"
263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include "deClock.h"
273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry#include <exception>
293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
303c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace de
313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
333c827367444ee418f129b2c238299f49d3264554Jarkko Poyrynamespace
343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
363c827367444ee418f129b2c238299f49d3264554Jarkko Poyryenum
373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	THREAD_TEST_TIME = 200*1000
393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
413c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass Object
423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
433c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	Object (bool& exists)
453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_exists(exists)
463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_exists = true;
483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	virtual ~Object (void)
513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		m_exists = false;
533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
553c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool& m_exists;
573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
593c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass DerivedObject : public Object
603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
613c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	DerivedObject (bool& exists)
633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: Object(exists)
643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
683c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass SharedPtrTestThread : public Thread
693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
703c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SharedPtrTestThread (const SharedPtr<Object>& ptr, const bool& exists)
723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_ptr		(ptr)
733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_exists	(exists)
743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void run (void)
783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint64 startTime	= deGetMicroseconds();
803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint64 cnt		= 0;
813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (;; cnt++)
833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (((cnt&(1<<14)) != 0) && (deGetMicroseconds()-startTime >= THREAD_TEST_TIME))
853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				break;
863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				SharedPtr<Object> ptrA(m_ptr);
893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				{
903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					SharedPtr<Object> ptrB;
913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					ptrB = ptrA;
923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					ptrA = SharedPtr<Object>();
933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				}
943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(m_exists);
963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
993c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SharedPtr<Object>	m_ptr;
1013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const bool&			m_exists;
1023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1043c827367444ee418f129b2c238299f49d3264554Jarkko Poyryclass WeakPtrTestThread : public Thread
1053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1063c827367444ee418f129b2c238299f49d3264554Jarkko Poyrypublic:
1073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	WeakPtrTestThread (const SharedPtr<Object>& ptr, const bool& exists)
1083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		: m_ptr		(ptr)
1093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		, m_exists	(exists)
1103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void run (void)
1143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint64 startTime	= deGetMicroseconds();
1163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		deUint64 cnt		= 0;
1173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		for (;; cnt++)
1193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			if (((cnt&(1<<14)) != 0) && (deGetMicroseconds()-startTime >= THREAD_TEST_TIME))
1213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				break;
1223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
1243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				WeakPtr<Object> ptrA(m_ptr);
1253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				{
1263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					WeakPtr<Object> ptrB;
1273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					ptrB = ptrA;
1283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry					ptrA = SharedPtr<Object>();
1293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				}
1303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
1313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(m_exists);
1323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1353c827367444ee418f129b2c238299f49d3264554Jarkko Poyryprivate:
1363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	SharedPtr<Object>	m_ptr;
1373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	const bool&			m_exists;
1383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1403c827367444ee418f129b2c238299f49d3264554Jarkko PoyrySharedPtr<Object> makeObject (bool& exists)
1413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	return SharedPtr<Object>(new Object(exists));
1433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
1443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1453c827367444ee418f129b2c238299f49d3264554Jarkko Poyrystruct CustomDeleter
1463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	CustomDeleter (bool* called) : m_called(called) {}
1483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	void operator() (Object* ptr)
1503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!*m_called);
1523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		delete ptr;
1533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		*m_called = true;
1543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	bool* m_called;
1573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry};
1583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // anonymous
1603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1613c827367444ee418f129b2c238299f49d3264554Jarkko Poyryvoid SharedPtr_selfTest (void)
1623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry{
1633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Empty pointer test.
1643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptr;
1663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptr.get() == DE_NULL);
1673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!ptr);
1683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Empty pointer copy.
1713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrA;
1733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrB(ptrA);
1743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptrB.get() == DE_NULL);
1753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Empty pointer assignment.
1783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrA;
1803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrB;
1813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ptrB = ptrA;
1823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ptrB = ptrB;
1833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic test.
1863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
1883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
1893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr(new Object(exists));
1903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
1913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
1923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr);
1933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
1943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
1953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
1963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
1973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Exception test.
1983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
1993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		try
2013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr(new Object(exists));
2033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
2053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			throw std::exception();
2063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		catch (const std::exception&)
2083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(!exists);
2103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Expression test.
2153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool test	= (SharedPtr<Object>(new Object(exists))).get() != DE_NULL && exists;
2183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(test);
2203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Assignment test.
2233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptr(new Object(exists));
2263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists);
2273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ptr = SharedPtr<Object>();
2283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Self-assignment test.
2323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr(new Object(exists));
2363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
2383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ptr = ptr;
2393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic multi-reference via copy ctor.
2443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptrA(new Object(exists));
2483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				SharedPtr<Object> ptrB(ptrA);
2513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(exists);
2523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic multi-reference via assignment to empty.
2593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptrA(new Object(exists));
2633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				SharedPtr<Object> ptrB;
2663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				ptrB = ptrA;
2673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(exists);
2683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
2703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
2723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Multi-reference via assignment to non-empty.
2753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool existsA = false;
2773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool existsB = false;
2783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptrA(new Object(existsA));
2803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(existsA);
2813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
2823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				SharedPtr<Object> ptrB(new Object(existsB));
2833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(existsB);
2843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				ptrA = ptrB;
2853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(!existsA);
2863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(existsB);
2873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
2883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(existsB);
2893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
2903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!existsB);
2913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
2923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
2933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Return from function.
2943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
2953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
2963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
2973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr;
2983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			ptr = makeObject(exists);
2993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
3023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Equality comparison.
3053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool existsA = false;
3073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool existsB = false;
3083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrA(new Object(existsA));
3093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrB(new Object(existsB));
3103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptrC(ptrA);
3113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptrA == ptrA);
3133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptrA != ptrB);
3143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptrA == ptrC);
3153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(ptrC != ptrB);
3163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Conversion via assignment.
3193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
3213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> basePtr;
3233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			{
3243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				SharedPtr<DerivedObject> derivedPtr(new DerivedObject(exists));
3253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(exists);
3263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				basePtr = derivedPtr;
3273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry				DE_TEST_ASSERT(exists);
3283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			}
3293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
3323c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Conversion via copy ctor.
3353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
3373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<DerivedObject>	derivedPtr	(new DerivedObject(exists));
3393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object>			basePtr		(derivedPtr);
3403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			derivedPtr = SharedPtr<DerivedObject>();
3423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3433c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3443c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
3453c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3463c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3473c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Explicit conversion operator.
3483c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3493c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
3503c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3513c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<DerivedObject> derivedPtr (new DerivedObject(exists));
3523c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3533c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3543c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> basePtr = (SharedPtr<Object>)(derivedPtr);
3553c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			derivedPtr = SharedPtr<DerivedObject>();
3563c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3573c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3583c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
3593c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3603c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3613c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic weak reference.
3623c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3633c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
3643c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		SharedPtr<Object> ptr(new Object(exists));
3653c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(exists);
3663c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3673c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		WeakPtr<Object> weakPtr(ptr);
3683c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		try
3693c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3703c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> newRef(weakPtr);
3713c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
3723c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3733c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		catch (const DeadReferenceException&)
3743c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3753c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(false);
3763c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3773c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3783c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		ptr = SharedPtr<Object>();
3793c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
3803c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		try
3813c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3823c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> newRef(weakPtr);
3833c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(false);
3843c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3853c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		catch (const DeadReferenceException&)
3863c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3873c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
3883c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
3893c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3903c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic SharedPtr threaded test.
3913c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
3923c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
3933c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
3943c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr(new Object(exists));
3953c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3963c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtrTestThread threadA(ptr, exists);
3973c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtrTestThread threadB(ptr, exists);
3983c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
3993c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadA.start();
4003c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadB.start();
4013c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4023c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadA.join();
4033c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadB.join();
4043c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
4053c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
4063c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
4073c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4083c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4093c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic WeakPtr threaded test.
4103c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4113c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
4123c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4133c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			SharedPtr<Object> ptr(new Object(exists));
4143c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			WeakPtrTestThread threadA(ptr, exists);
4153c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			WeakPtrTestThread threadB(ptr, exists);
4163c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4173c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadA.start();
4183c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadB.start();
4193c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4203c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadA.join();
4213c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			threadB.join();
4223c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
4233c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
4243c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
4253c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4263c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4273c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	// Basic custom deleter.
4283c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	{
4293c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool exists = false;
4303c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		bool deleterCalled = false;
4313c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		{
4326801c0680107ff001b065db07b125d622926f311Mika Isojärvi			SharedPtr<Object> ptr(new Object(exists), CustomDeleter(&deleterCalled));
4333c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(exists);
4343c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(!deleterCalled);
4353c827367444ee418f129b2c238299f49d3264554Jarkko Poyry			DE_TEST_ASSERT(ptr.get() != DE_NULL);
4363c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		}
4373c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(!exists);
4383c827367444ee418f129b2c238299f49d3264554Jarkko Poyry		DE_TEST_ASSERT(deleterCalled);
4393c827367444ee418f129b2c238299f49d3264554Jarkko Poyry	}
4403c827367444ee418f129b2c238299f49d3264554Jarkko Poyry}
4413c827367444ee418f129b2c238299f49d3264554Jarkko Poyry
4423c827367444ee418f129b2c238299f49d3264554Jarkko Poyry} // de
443