Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(250)

Side by Side Diff: third_party/WebKit/Source/wtf/VectorTest.cpp

Issue 2050123002: Remove OwnPtr from Blink. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: First attempt to land. Created 4 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | third_party/WebKit/Source/wtf/VectorTraits.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/Vector.h" 26 #include "wtf/Vector.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/OwnPtr.h" 30 #include "wtf/PtrUtil.h"
31 #include "wtf/PassOwnPtr.h"
32 #include "wtf/text/WTFString.h" 31 #include "wtf/text/WTFString.h"
33 #include <memory> 32 #include <memory>
34 33
35 namespace WTF { 34 namespace WTF {
36 35
37 namespace { 36 namespace {
38 37
39 TEST(VectorTest, Basic) 38 TEST(VectorTest, Basic)
40 { 39 {
41 Vector<int> intVector; 40 Vector<int> intVector;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 { } 154 { }
156 155
157 ~DestructCounter() { ++(*m_destructNumber); } 156 ~DestructCounter() { ++(*m_destructNumber); }
158 int get() const { return m_i; } 157 int get() const { return m_i; }
159 158
160 private: 159 private:
161 int m_i; 160 int m_i;
162 int* m_destructNumber; 161 int* m_destructNumber;
163 }; 162 };
164 163
165 typedef WTF::Vector<OwnPtr<DestructCounter>> OwnPtrVector; 164 typedef WTF::Vector<std::unique_ptr<DestructCounter>> OwnPtrVector;
166 165
167 TEST(VectorTest, OwnPtr) 166 TEST(VectorTest, OwnPtr)
168 { 167 {
169 int destructNumber = 0; 168 int destructNumber = 0;
170 OwnPtrVector vector; 169 OwnPtrVector vector;
171 vector.append(adoptPtr(new DestructCounter(0, &destructNumber))); 170 vector.append(wrapUnique(new DestructCounter(0, &destructNumber)));
172 vector.append(adoptPtr(new DestructCounter(1, &destructNumber))); 171 vector.append(wrapUnique(new DestructCounter(1, &destructNumber)));
173 EXPECT_EQ(2u, vector.size()); 172 EXPECT_EQ(2u, vector.size());
174 173
175 OwnPtr<DestructCounter>& counter0 = vector.first(); 174 std::unique_ptr<DestructCounter>& counter0 = vector.first();
176 ASSERT_EQ(0, counter0->get()); 175 ASSERT_EQ(0, counter0->get());
177 int counter1 = vector.last()->get(); 176 int counter1 = vector.last()->get();
178 ASSERT_EQ(1, counter1); 177 ASSERT_EQ(1, counter1);
179 ASSERT_EQ(0, destructNumber); 178 ASSERT_EQ(0, destructNumber);
180 179
181 size_t index = 0; 180 size_t index = 0;
182 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++i ter) { 181 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++i ter) {
183 OwnPtr<DestructCounter>* refCounter = iter; 182 std::unique_ptr<DestructCounter>* refCounter = iter;
184 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get())); 183 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get()));
185 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get())); 184 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get()));
186 index++; 185 index++;
187 } 186 }
188 EXPECT_EQ(0, destructNumber); 187 EXPECT_EQ(0, destructNumber);
189 188
190 for (index = 0; index < vector.size(); index++) { 189 for (index = 0; index < vector.size(); index++) {
191 OwnPtr<DestructCounter>& refCounter = vector[index]; 190 std::unique_ptr<DestructCounter>& refCounter = vector[index];
192 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); 191 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()));
193 } 192 }
194 EXPECT_EQ(0, destructNumber); 193 EXPECT_EQ(0, destructNumber);
195 194
196 EXPECT_EQ(0, vector[0]->get()); 195 EXPECT_EQ(0, vector[0]->get());
197 EXPECT_EQ(1, vector[1]->get()); 196 EXPECT_EQ(1, vector[1]->get());
198 vector.remove(0); 197 vector.remove(0);
199 EXPECT_EQ(1, vector[0]->get()); 198 EXPECT_EQ(1, vector[0]->get());
200 EXPECT_EQ(1u, vector.size()); 199 EXPECT_EQ(1u, vector.size());
201 EXPECT_EQ(1, destructNumber); 200 EXPECT_EQ(1, destructNumber);
202 201
203 OwnPtr<DestructCounter> ownCounter1 = std::move(vector[0]); 202 std::unique_ptr<DestructCounter> ownCounter1 = std::move(vector[0]);
204 vector.remove(0); 203 vector.remove(0);
205 ASSERT_EQ(counter1, ownCounter1->get()); 204 ASSERT_EQ(counter1, ownCounter1->get());
206 ASSERT_EQ(0u, vector.size()); 205 ASSERT_EQ(0u, vector.size());
207 ASSERT_EQ(1, destructNumber); 206 ASSERT_EQ(1, destructNumber);
208 207
209 ownCounter1.reset(); 208 ownCounter1.reset();
210 EXPECT_EQ(2, destructNumber); 209 EXPECT_EQ(2, destructNumber);
211 210
212 size_t count = 1025; 211 size_t count = 1025;
213 destructNumber = 0; 212 destructNumber = 0;
214 for (size_t i = 0; i < count; i++) 213 for (size_t i = 0; i < count; i++)
215 vector.prepend(adoptPtr(new DestructCounter(i, &destructNumber))); 214 vector.prepend(wrapUnique(new DestructCounter(i, &destructNumber)));
216 215
217 // Vector relocation must not destruct OwnPtr element. 216 // Vector relocation must not destruct std::unique_ptr element.
218 EXPECT_EQ(0, destructNumber); 217 EXPECT_EQ(0, destructNumber);
219 EXPECT_EQ(count, vector.size()); 218 EXPECT_EQ(count, vector.size());
220 219
221 OwnPtrVector copyVector; 220 OwnPtrVector copyVector;
222 vector.swap(copyVector); 221 vector.swap(copyVector);
223 EXPECT_EQ(0, destructNumber); 222 EXPECT_EQ(0, destructNumber);
224 EXPECT_EQ(count, copyVector.size()); 223 EXPECT_EQ(count, copyVector.size());
225 EXPECT_EQ(0u, vector.size()); 224 EXPECT_EQ(0u, vector.size());
226 225
227 copyVector.clear(); 226 copyVector.clear();
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
685 vector3 = {1, 2, 3}; 684 vector3 = {1, 2, 3};
686 ASSERT_EQ(3u, vector3.size()); 685 ASSERT_EQ(3u, vector3.size());
687 EXPECT_EQ(1, vector3[0]); 686 EXPECT_EQ(1, vector3[0]);
688 EXPECT_EQ(2, vector3[1]); 687 EXPECT_EQ(2, vector3[1]);
689 EXPECT_EQ(3, vector3[2]); 688 EXPECT_EQ(3, vector3[2]);
690 } 689 }
691 690
692 } // anonymous namespace 691 } // anonymous namespace
693 692
694 } // namespace WTF 693 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/Vector.h ('k') | third_party/WebKit/Source/wtf/VectorTraits.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698