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 Unique pointer.
22 *//*--------------------------------------------------------------------*/
24 #include "deUniquePtr.hpp"
54 CustomDeleter (bool* called) : m_called(called) {}
56 void operator() (Object* ptr)
58 DE_TEST_ASSERT(!*m_called);
66 MovePtr<Object> createObject (bool& exists)
68 UniquePtr<Object> objectPtr(new Object(exists));
69 return objectPtr.move();
74 void UniquePtr_selfTest (void)
80 UniquePtr<Object> ptr(new Object(exists));
81 DE_TEST_ASSERT(exists);
82 DE_TEST_ASSERT(ptr.get() != DE_NULL);
84 DE_TEST_ASSERT(!exists);
92 UniquePtr<Object> ptr(new Object(exists));
93 DE_TEST_ASSERT(exists);
94 DE_TEST_ASSERT(ptr.get() != DE_NULL);
95 throw std::exception();
97 catch (const std::exception&)
99 DE_TEST_ASSERT(!exists);
101 DE_TEST_ASSERT(!exists);
107 bool test = (UniquePtr<Object>(new Object(exists))).get() != DE_NULL && exists;
108 DE_TEST_ASSERT(!exists);
109 DE_TEST_ASSERT(test);
115 bool deleterCalled = false;
117 UniquePtr<Object, CustomDeleter> ptr(new Object(exists), CustomDeleter(&deleterCalled));
118 DE_TEST_ASSERT(exists);
119 DE_TEST_ASSERT(!deleterCalled);
120 DE_TEST_ASSERT(ptr.get() != DE_NULL);
122 DE_TEST_ASSERT(!exists);
123 DE_TEST_ASSERT(deleterCalled);
126 // MovePtr -> MovePtr moving
129 MovePtr<Object> ptr(new Object(exists));
130 DE_TEST_ASSERT(exists);
132 MovePtr<Object> ptr2 = ptr;
133 DE_TEST_ASSERT(exists);
134 // Ownership moved to ptr2, should be deleted when ptr2 goes out of scope.
136 DE_TEST_ASSERT(!exists);
139 // UniquePtr -> MovePtr moving
142 UniquePtr<Object> ptr(new Object(exists));
143 DE_TEST_ASSERT(exists);
145 MovePtr<Object> ptr2 = ptr.move();
146 DE_TEST_ASSERT(exists);
147 // Ownership moved to ptr2, should be deleted when ptr2 goes out of scope.
149 DE_TEST_ASSERT(!exists);
152 // MovePtr -> UniquePtr moving
156 UniquePtr<Object> ptr(createObject(exists));
157 DE_TEST_ASSERT(exists);
159 DE_TEST_ASSERT(!exists);
162 // MovePtr assignment
164 bool exists1 = false;
165 bool exists2 = false;
166 MovePtr<Object> ptr1(new Object(exists1));
167 MovePtr<Object> ptr2(new Object(exists2));
169 DE_TEST_ASSERT(!exists1);
170 DE_TEST_ASSERT(exists2);
176 Object* raw = DE_NULL;
178 MovePtr<Object> ptr1(new Object(exists));
179 raw = ptr1.release();
180 DE_TEST_ASSERT(raw != DE_NULL);
181 DE_TEST_ASSERT(ptr1.get() == DE_NULL);
182 DE_TEST_ASSERT(exists);
184 DE_TEST_ASSERT(exists);
186 DE_TEST_ASSERT(!exists);
189 // Null MovePtr and assigning to it.
193 MovePtr<Object> ptr1;
194 DE_TEST_ASSERT(ptr1.get() == DE_NULL);
195 MovePtr<Object> ptr2(new Object(exists));
197 DE_TEST_ASSERT(exists);
198 DE_TEST_ASSERT(ptr1.get() != DE_NULL);
199 DE_TEST_ASSERT(ptr2.get() == DE_NULL);
201 DE_TEST_ASSERT(!exists);
205 // UniquePtr assignment or copy construction should not compile. This
206 // piece of code is intentionally commented out. To manually test that
207 // copying a UniquePtr is statically forbidden, uncomment and try to
211 UniquePtr<Object> ptr(new Object(exists));
213 UniquePtr<Object> ptr2(ptr);
214 DE_TEST_ASSERT(exists);
216 DE_TEST_ASSERT(!exists);