OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2011 Apple Inc. All rights reserved. | 2 * Copyright (C) 2011 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' | 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS'' |
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, | 14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | 17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | 18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | 19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | 20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | 21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | 22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 * THE POSSIBILITY OF SUCH DAMAGE. | 23 * THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "wtf/Deque.h" | 26 #include "wtf/Deque.h" |
27 | 27 |
28 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
29 #include "wtf/HashSet.h" | 29 #include "wtf/HashSet.h" |
30 #include "wtf/PtrUtil.h" | 30 #include "wtf/OwnPtr.h" |
| 31 #include "wtf/PassOwnPtr.h" |
31 #include <memory> | 32 #include <memory> |
32 | 33 |
33 namespace WTF { | 34 namespace WTF { |
34 | 35 |
35 namespace { | 36 namespace { |
36 | 37 |
37 TEST(DequeTest, Basic) | 38 TEST(DequeTest, Basic) |
38 { | 39 { |
39 Deque<int> intDeque; | 40 Deque<int> intDeque; |
40 EXPECT_TRUE(intDeque.isEmpty()); | 41 EXPECT_TRUE(intDeque.isEmpty()); |
(...skipping 129 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 private: | 171 private: |
171 int m_i; | 172 int m_i; |
172 int* m_destructNumber; | 173 int* m_destructNumber; |
173 }; | 174 }; |
174 | 175 |
175 template <typename OwnPtrDeque> | 176 template <typename OwnPtrDeque> |
176 void ownPtrTest() | 177 void ownPtrTest() |
177 { | 178 { |
178 int destructNumber = 0; | 179 int destructNumber = 0; |
179 OwnPtrDeque deque; | 180 OwnPtrDeque deque; |
180 deque.append(wrapUnique(new DestructCounter(0, &destructNumber))); | 181 deque.append(adoptPtr(new DestructCounter(0, &destructNumber))); |
181 deque.append(wrapUnique(new DestructCounter(1, &destructNumber))); | 182 deque.append(adoptPtr(new DestructCounter(1, &destructNumber))); |
182 EXPECT_EQ(2u, deque.size()); | 183 EXPECT_EQ(2u, deque.size()); |
183 | 184 |
184 std::unique_ptr<DestructCounter>& counter0 = deque.first(); | 185 OwnPtr<DestructCounter>& counter0 = deque.first(); |
185 EXPECT_EQ(0, counter0->get()); | 186 EXPECT_EQ(0, counter0->get()); |
186 int counter1 = deque.last()->get(); | 187 int counter1 = deque.last()->get(); |
187 EXPECT_EQ(1, counter1); | 188 EXPECT_EQ(1, counter1); |
188 EXPECT_EQ(0, destructNumber); | 189 EXPECT_EQ(0, destructNumber); |
189 | 190 |
190 size_t index = 0; | 191 size_t index = 0; |
191 for (auto iter = deque.begin(); iter != deque.end(); ++iter) { | 192 for (auto iter = deque.begin(); iter != deque.end(); ++iter) { |
192 std::unique_ptr<DestructCounter>& refCounter = *iter; | 193 OwnPtr<DestructCounter>& refCounter = *iter; |
193 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); | 194 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); |
194 EXPECT_EQ(index, static_cast<size_t>((*refCounter).get())); | 195 EXPECT_EQ(index, static_cast<size_t>((*refCounter).get())); |
195 index++; | 196 index++; |
196 } | 197 } |
197 EXPECT_EQ(0, destructNumber); | 198 EXPECT_EQ(0, destructNumber); |
198 | 199 |
199 auto it = deque.begin(); | 200 auto it = deque.begin(); |
200 for (index = 0; index < deque.size(); ++index) { | 201 for (index = 0; index < deque.size(); ++index) { |
201 std::unique_ptr<DestructCounter>& refCounter = *it; | 202 OwnPtr<DestructCounter>& refCounter = *it; |
202 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); | 203 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); |
203 index++; | 204 index++; |
204 ++it; | 205 ++it; |
205 } | 206 } |
206 EXPECT_EQ(0, destructNumber); | 207 EXPECT_EQ(0, destructNumber); |
207 | 208 |
208 EXPECT_EQ(0, deque.first()->get()); | 209 EXPECT_EQ(0, deque.first()->get()); |
209 deque.removeFirst(); | 210 deque.removeFirst(); |
210 EXPECT_EQ(1, deque.first()->get()); | 211 EXPECT_EQ(1, deque.first()->get()); |
211 EXPECT_EQ(1u, deque.size()); | 212 EXPECT_EQ(1u, deque.size()); |
212 EXPECT_EQ(1, destructNumber); | 213 EXPECT_EQ(1, destructNumber); |
213 | 214 |
214 std::unique_ptr<DestructCounter> ownCounter1 = std::move(deque.first()); | 215 OwnPtr<DestructCounter> ownCounter1 = std::move(deque.first()); |
215 deque.removeFirst(); | 216 deque.removeFirst(); |
216 EXPECT_EQ(counter1, ownCounter1->get()); | 217 EXPECT_EQ(counter1, ownCounter1->get()); |
217 EXPECT_EQ(0u, deque.size()); | 218 EXPECT_EQ(0u, deque.size()); |
218 EXPECT_EQ(1, destructNumber); | 219 EXPECT_EQ(1, destructNumber); |
219 | 220 |
220 ownCounter1.reset(); | 221 ownCounter1.reset(); |
221 EXPECT_EQ(2, destructNumber); | 222 EXPECT_EQ(2, destructNumber); |
222 | 223 |
223 size_t count = 1025; | 224 size_t count = 1025; |
224 destructNumber = 0; | 225 destructNumber = 0; |
225 for (size_t i = 0; i < count; ++i) | 226 for (size_t i = 0; i < count; ++i) |
226 deque.prepend(wrapUnique(new DestructCounter(i, &destructNumber))); | 227 deque.prepend(adoptPtr(new DestructCounter(i, &destructNumber))); |
227 | 228 |
228 // Deque relocation must not destruct std::unique_ptr element. | 229 // Deque relocation must not destruct OwnPtr element. |
229 EXPECT_EQ(0, destructNumber); | 230 EXPECT_EQ(0, destructNumber); |
230 EXPECT_EQ(count, deque.size()); | 231 EXPECT_EQ(count, deque.size()); |
231 | 232 |
232 OwnPtrDeque copyDeque; | 233 OwnPtrDeque copyDeque; |
233 deque.swap(copyDeque); | 234 deque.swap(copyDeque); |
234 EXPECT_EQ(0, destructNumber); | 235 EXPECT_EQ(0, destructNumber); |
235 EXPECT_EQ(count, copyDeque.size()); | 236 EXPECT_EQ(count, copyDeque.size()); |
236 EXPECT_EQ(0u, deque.size()); | 237 EXPECT_EQ(0u, deque.size()); |
237 | 238 |
238 copyDeque.clear(); | 239 copyDeque.clear(); |
239 EXPECT_EQ(count, static_cast<size_t>(destructNumber)); | 240 EXPECT_EQ(count, static_cast<size_t>(destructNumber)); |
240 } | 241 } |
241 | 242 |
242 TEST(DequeTest, OwnPtr) | 243 TEST(DequeTest, OwnPtr) |
243 { | 244 { |
244 ownPtrTest<Deque<std::unique_ptr<DestructCounter>>>(); | 245 ownPtrTest<Deque<OwnPtr<DestructCounter>>>(); |
245 ownPtrTest<Deque<std::unique_ptr<DestructCounter>, 2>>(); | 246 ownPtrTest<Deque<OwnPtr<DestructCounter>, 2>>(); |
246 } | 247 } |
247 | 248 |
248 class MoveOnly { | 249 class MoveOnly { |
249 public: | 250 public: |
250 explicit MoveOnly(int i = 0) | 251 explicit MoveOnly(int i = 0) |
251 : m_i(i) | 252 : m_i(i) |
252 { } | 253 { } |
253 | 254 |
254 MoveOnly(MoveOnly&& other) | 255 MoveOnly(MoveOnly&& other) |
255 : m_i(other.m_i) | 256 : m_i(other.m_i) |
(...skipping 345 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
601 EXPECT_EQ(0, counter); | 602 EXPECT_EQ(0, counter); |
602 | 603 |
603 counter = 0; | 604 counter = 0; |
604 Deque<CountCopy, 1> yetAnother(std::move(deque)); // Move construction. | 605 Deque<CountCopy, 1> yetAnother(std::move(deque)); // Move construction. |
605 EXPECT_EQ(0, counter); | 606 EXPECT_EQ(0, counter); |
606 } | 607 } |
607 | 608 |
608 } // anonymous namespace | 609 } // anonymous namespace |
609 | 610 |
610 } // namespace WTF | 611 } // namespace WTF |
OLD | NEW |