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

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

Issue 2080623002: Revert "Remove OwnPtr from Blink." (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: 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/PtrUtil.h" 30 #include "wtf/OwnPtr.h"
31 #include "wtf/PassOwnPtr.h"
31 #include "wtf/text/WTFString.h" 32 #include "wtf/text/WTFString.h"
32 #include <memory> 33 #include <memory>
33 34
34 namespace WTF { 35 namespace WTF {
35 36
36 namespace { 37 namespace {
37 38
38 TEST(VectorTest, Basic) 39 TEST(VectorTest, Basic)
39 { 40 {
40 Vector<int> intVector; 41 Vector<int> intVector;
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after
154 { } 155 { }
155 156
156 ~DestructCounter() { ++(*m_destructNumber); } 157 ~DestructCounter() { ++(*m_destructNumber); }
157 int get() const { return m_i; } 158 int get() const { return m_i; }
158 159
159 private: 160 private:
160 int m_i; 161 int m_i;
161 int* m_destructNumber; 162 int* m_destructNumber;
162 }; 163 };
163 164
164 typedef WTF::Vector<std::unique_ptr<DestructCounter>> OwnPtrVector; 165 typedef WTF::Vector<OwnPtr<DestructCounter>> OwnPtrVector;
165 166
166 TEST(VectorTest, OwnPtr) 167 TEST(VectorTest, OwnPtr)
167 { 168 {
168 int destructNumber = 0; 169 int destructNumber = 0;
169 OwnPtrVector vector; 170 OwnPtrVector vector;
170 vector.append(wrapUnique(new DestructCounter(0, &destructNumber))); 171 vector.append(adoptPtr(new DestructCounter(0, &destructNumber)));
171 vector.append(wrapUnique(new DestructCounter(1, &destructNumber))); 172 vector.append(adoptPtr(new DestructCounter(1, &destructNumber)));
172 EXPECT_EQ(2u, vector.size()); 173 EXPECT_EQ(2u, vector.size());
173 174
174 std::unique_ptr<DestructCounter>& counter0 = vector.first(); 175 OwnPtr<DestructCounter>& counter0 = vector.first();
175 ASSERT_EQ(0, counter0->get()); 176 ASSERT_EQ(0, counter0->get());
176 int counter1 = vector.last()->get(); 177 int counter1 = vector.last()->get();
177 ASSERT_EQ(1, counter1); 178 ASSERT_EQ(1, counter1);
178 ASSERT_EQ(0, destructNumber); 179 ASSERT_EQ(0, destructNumber);
179 180
180 size_t index = 0; 181 size_t index = 0;
181 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++i ter) { 182 for (OwnPtrVector::iterator iter = vector.begin(); iter != vector.end(); ++i ter) {
182 std::unique_ptr<DestructCounter>* refCounter = iter; 183 OwnPtr<DestructCounter>* refCounter = iter;
183 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get())); 184 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()->get()));
184 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get())); 185 EXPECT_EQ(index, static_cast<size_t>((*refCounter)->get()));
185 index++; 186 index++;
186 } 187 }
187 EXPECT_EQ(0, destructNumber); 188 EXPECT_EQ(0, destructNumber);
188 189
189 for (index = 0; index < vector.size(); index++) { 190 for (index = 0; index < vector.size(); index++) {
190 std::unique_ptr<DestructCounter>& refCounter = vector[index]; 191 OwnPtr<DestructCounter>& refCounter = vector[index];
191 EXPECT_EQ(index, static_cast<size_t>(refCounter->get())); 192 EXPECT_EQ(index, static_cast<size_t>(refCounter->get()));
192 } 193 }
193 EXPECT_EQ(0, destructNumber); 194 EXPECT_EQ(0, destructNumber);
194 195
195 EXPECT_EQ(0, vector[0]->get()); 196 EXPECT_EQ(0, vector[0]->get());
196 EXPECT_EQ(1, vector[1]->get()); 197 EXPECT_EQ(1, vector[1]->get());
197 vector.remove(0); 198 vector.remove(0);
198 EXPECT_EQ(1, vector[0]->get()); 199 EXPECT_EQ(1, vector[0]->get());
199 EXPECT_EQ(1u, vector.size()); 200 EXPECT_EQ(1u, vector.size());
200 EXPECT_EQ(1, destructNumber); 201 EXPECT_EQ(1, destructNumber);
201 202
202 std::unique_ptr<DestructCounter> ownCounter1 = std::move(vector[0]); 203 OwnPtr<DestructCounter> ownCounter1 = std::move(vector[0]);
203 vector.remove(0); 204 vector.remove(0);
204 ASSERT_EQ(counter1, ownCounter1->get()); 205 ASSERT_EQ(counter1, ownCounter1->get());
205 ASSERT_EQ(0u, vector.size()); 206 ASSERT_EQ(0u, vector.size());
206 ASSERT_EQ(1, destructNumber); 207 ASSERT_EQ(1, destructNumber);
207 208
208 ownCounter1.reset(); 209 ownCounter1.reset();
209 EXPECT_EQ(2, destructNumber); 210 EXPECT_EQ(2, destructNumber);
210 211
211 size_t count = 1025; 212 size_t count = 1025;
212 destructNumber = 0; 213 destructNumber = 0;
213 for (size_t i = 0; i < count; i++) 214 for (size_t i = 0; i < count; i++)
214 vector.prepend(wrapUnique(new DestructCounter(i, &destructNumber))); 215 vector.prepend(adoptPtr(new DestructCounter(i, &destructNumber)));
215 216
216 // Vector relocation must not destruct std::unique_ptr element. 217 // Vector relocation must not destruct OwnPtr element.
217 EXPECT_EQ(0, destructNumber); 218 EXPECT_EQ(0, destructNumber);
218 EXPECT_EQ(count, vector.size()); 219 EXPECT_EQ(count, vector.size());
219 220
220 OwnPtrVector copyVector; 221 OwnPtrVector copyVector;
221 vector.swap(copyVector); 222 vector.swap(copyVector);
222 EXPECT_EQ(0, destructNumber); 223 EXPECT_EQ(0, destructNumber);
223 EXPECT_EQ(count, copyVector.size()); 224 EXPECT_EQ(count, copyVector.size());
224 EXPECT_EQ(0u, vector.size()); 225 EXPECT_EQ(0u, vector.size());
225 226
226 copyVector.clear(); 227 copyVector.clear();
(...skipping 457 matching lines...) Expand 10 before | Expand all | Expand 10 after
684 vector3 = {1, 2, 3}; 685 vector3 = {1, 2, 3};
685 ASSERT_EQ(3u, vector3.size()); 686 ASSERT_EQ(3u, vector3.size());
686 EXPECT_EQ(1, vector3[0]); 687 EXPECT_EQ(1, vector3[0]);
687 EXPECT_EQ(2, vector3[1]); 688 EXPECT_EQ(2, vector3[1]);
688 EXPECT_EQ(3, vector3[2]); 689 EXPECT_EQ(3, vector3[2]);
689 } 690 }
690 691
691 } // anonymous namespace 692 } // anonymous namespace
692 693
693 } // namespace WTF 694 } // 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