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

Side by Side Diff: mojo/public/cpp/bindings/tests/array_unittest.cc

Issue 311273003: Mojo: Add resize and push_back to Array. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: ForwardType Created 6 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 | Annotate | Revision Log
« no previous file with comments | « mojo/public/cpp/bindings/lib/array_internal.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "mojo/public/cpp/bindings/array.h" 5 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 6 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 7 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
8 #include "mojo/public/cpp/environment/environment.h" 8 #include "mojo/public/cpp/environment/environment.h"
9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h" 9 #include "mojo/public/interfaces/bindings/tests/sample_service.mojom.h"
10 #include "testing/gtest/include/gtest/gtest.h" 10 #include "testing/gtest/include/gtest/gtest.h"
11 11
12 namespace mojo { 12 namespace mojo {
13 namespace test { 13 namespace test {
14 namespace { 14 namespace {
15 15
16 class CopyableType {
17 public:
18 CopyableType() : copied_(false), ptr_(this) { num_instances_++; }
19 CopyableType(const CopyableType& other) : copied_(true), ptr_(other.ptr()) {
20 num_instances_++;
21 }
22 CopyableType& operator=(const CopyableType& other) {
23 copied_ = true;
24 ptr_ = other.ptr();
25 return *this;
26 }
27 ~CopyableType() { num_instances_--; }
28
29 bool copied() const { return copied_; }
30 static size_t num_instances() { return num_instances_; }
31 CopyableType* ptr() const { return ptr_; }
32 void ResetCopied() { copied_ = false; }
33
34 private:
35 bool copied_;
36 static size_t num_instances_;
37 CopyableType* ptr_;
38 };
39
40 size_t CopyableType::num_instances_ = 0;
41
42 class MoveOnlyType {
43 MOJO_MOVE_ONLY_TYPE_FOR_CPP_03(MoveOnlyType, RValue)
44 public:
45 typedef MoveOnlyType Data_;
46 MoveOnlyType() : moved_(false), ptr_(this) { num_instances_++; }
47 MoveOnlyType(RValue other) : moved_(true), ptr_(other.object->ptr()) {
48 num_instances_++;
49 }
50 MoveOnlyType& operator=(RValue other) {
51 moved_ = true;
52 ptr_ = other.object->ptr();
53 return *this;
54 }
55 ~MoveOnlyType() { num_instances_--; }
56
57 bool moved() const { return moved_; }
58 static size_t num_instances() { return num_instances_; }
59 MoveOnlyType* ptr() const { return ptr_; }
60 void ResetMoved() { moved_ = false; }
61
62 private:
63 bool moved_;
64 static size_t num_instances_;
65 MoveOnlyType* ptr_;
66 };
67
68 size_t MoveOnlyType::num_instances_ = 0;
69
16 // Tests that basic Array operations work. 70 // Tests that basic Array operations work.
17 TEST(ArrayTest, Basic) { 71 TEST(ArrayTest, Basic) {
18 Array<char> array(8); 72 Array<char> array(8);
19 for (size_t i = 0; i < array.size(); ++i) { 73 for (size_t i = 0; i < array.size(); ++i) {
20 char val = static_cast<char>(i*2); 74 char val = static_cast<char>(i*2);
21 array[i] = val; 75 array[i] = val;
22 EXPECT_EQ(val, array.at(i)); 76 EXPECT_EQ(val, array.at(i));
23 } 77 }
24 } 78 }
25 79
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
162 Array<String> array2; 216 Array<String> array2;
163 Deserialize_(data, &array2); 217 Deserialize_(data, &array2);
164 218
165 EXPECT_EQ(10U, array2.size()); 219 EXPECT_EQ(10U, array2.size());
166 for (size_t i = 0; i < array2.size(); ++i) { 220 for (size_t i = 0; i < array2.size(); ++i) {
167 char c = 'A' + 1; 221 char c = 'A' + 1;
168 EXPECT_EQ(String(&c, 1), array2[i]); 222 EXPECT_EQ(String(&c, 1), array2[i]);
169 } 223 }
170 } 224 }
171 225
226 TEST(ArrayTest, Resize_Copyable) {
227 ASSERT_EQ(0u, CopyableType::num_instances());
228 mojo::Array<CopyableType> array(3);
229 std::vector<CopyableType*> value_ptrs;
230 value_ptrs.push_back(array[0].ptr());
231 value_ptrs.push_back(array[1].ptr());
232
233 for (size_t i = 0; i < array.size(); i++)
234 array[i].ResetCopied();
235
236 array.resize(2);
237 ASSERT_EQ(2u, array.size());
238 EXPECT_EQ(array.size(), CopyableType::num_instances());
239 for (size_t i = 0; i < array.size(); i++) {
240 EXPECT_FALSE(array[i].copied());
241 EXPECT_EQ(value_ptrs[i], array[i].ptr());
242 }
243
244 array.resize(3);
245 array[2].ResetCopied();
246 ASSERT_EQ(3u, array.size());
247 EXPECT_EQ(array.size(), CopyableType::num_instances());
248 for (size_t i = 0; i < array.size(); i++)
249 EXPECT_FALSE(array[i].copied());
250 value_ptrs.push_back(array[2].ptr());
251
252 size_t capacity = array.storage().capacity();
253 array.resize(capacity);
254 ASSERT_EQ(capacity, array.size());
255 EXPECT_EQ(array.size(), CopyableType::num_instances());
256 for (size_t i = 0; i < 3; i++)
257 EXPECT_FALSE(array[i].copied());
258 for (size_t i = 3; i < array.size(); i++) {
259 array[i].ResetCopied();
260 value_ptrs.push_back(array[i].ptr());
261 }
262
263 array.resize(capacity + 2);
264 ASSERT_EQ(capacity + 2, array.size());
265 EXPECT_EQ(array.size(), CopyableType::num_instances());
266 for (size_t i = 0; i < capacity; i++) {
267 EXPECT_TRUE(array[i].copied());
268 EXPECT_EQ(value_ptrs[i], array[i].ptr());
269 }
270 array.reset();
271 EXPECT_EQ(0u, CopyableType::num_instances());
272 EXPECT_FALSE(array);
273 array.resize(0);
274 EXPECT_EQ(0u, CopyableType::num_instances());
275 EXPECT_TRUE(array);
276 }
277
278 TEST(ArrayTest, Resize_MoveOnly) {
279 ASSERT_EQ(0u, MoveOnlyType::num_instances());
280 mojo::Array<MoveOnlyType> array(3);
281 std::vector<MoveOnlyType*> value_ptrs;
282 value_ptrs.push_back(array[0].ptr());
283 value_ptrs.push_back(array[1].ptr());
284
285 for (size_t i = 0; i < array.size(); i++)
286 EXPECT_FALSE(array[i].moved());
287
288 array.resize(2);
289 ASSERT_EQ(2u, array.size());
290 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
291 for (size_t i = 0; i < array.size(); i++) {
292 EXPECT_FALSE(array[i].moved());
293 EXPECT_EQ(value_ptrs[i], array[i].ptr());
294 }
295
296 array.resize(3);
297 ASSERT_EQ(3u, array.size());
298 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
299 for (size_t i = 0; i < array.size(); i++)
300 EXPECT_FALSE(array[i].moved());
301 value_ptrs.push_back(array[2].ptr());
302
303 size_t capacity = array.storage().capacity();
304 array.resize(capacity);
305 ASSERT_EQ(capacity, array.size());
306 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
307 for (size_t i = 0; i < array.size(); i++)
308 EXPECT_FALSE(array[i].moved());
309 for (size_t i = 3; i < array.size(); i++)
310 value_ptrs.push_back(array[i].ptr());
311
312 array.resize(capacity + 2);
313 ASSERT_EQ(capacity + 2, array.size());
314 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
315 for (size_t i = 0; i < capacity; i++) {
316 EXPECT_TRUE(array[i].moved());
317 EXPECT_EQ(value_ptrs[i], array[i].ptr());
318 }
319 for (size_t i = capacity; i < array.size(); i++)
320 EXPECT_FALSE(array[i].moved());
321
322 array.reset();
323 EXPECT_EQ(0u, MoveOnlyType::num_instances());
324 EXPECT_FALSE(array);
325 array.resize(0);
326 EXPECT_EQ(0u, MoveOnlyType::num_instances());
327 EXPECT_TRUE(array);
328 }
329
330 TEST(ArrayTest, PushBack_Copyable) {
331 ASSERT_EQ(0u, CopyableType::num_instances());
332 mojo::Array<CopyableType> array(2);
333 array.reset();
334 std::vector<CopyableType*> value_ptrs;
335 size_t capacity = array.storage().capacity();
336 for (size_t i = 0; i < capacity; i++) {
337 CopyableType value;
338 value_ptrs.push_back(value.ptr());
339 array.push_back(value);
340 ASSERT_EQ(i + 1, array.size());
341 ASSERT_EQ(i + 1, value_ptrs.size());
342 EXPECT_EQ(array.size() + 1, CopyableType::num_instances());
343 EXPECT_TRUE(array[i].copied());
344 EXPECT_EQ(value_ptrs[i], array[i].ptr());
345 array[i].ResetCopied();
346 EXPECT_TRUE(array);
347 }
348 {
349 CopyableType value;
350 value_ptrs.push_back(value.ptr());
351 array.push_back(value);
352 EXPECT_EQ(array.size() + 1, CopyableType::num_instances());
353 }
354 ASSERT_EQ(capacity + 1, array.size());
355 EXPECT_EQ(array.size(), CopyableType::num_instances());
356
357 for (size_t i = 0; i < array.size(); i++) {
358 EXPECT_TRUE(array[i].copied());
359 EXPECT_EQ(value_ptrs[i], array[i].ptr());
360 }
361 array.reset();
362 EXPECT_EQ(0u, CopyableType::num_instances());
363 }
364
365 TEST(ArrayTest, PushBack_MoveOnly) {
366 ASSERT_EQ(0u, MoveOnlyType::num_instances());
367 mojo::Array<MoveOnlyType> array(2);
368 array.reset();
369 std::vector<MoveOnlyType*> value_ptrs;
370 size_t capacity = array.storage().capacity();
371 for (size_t i = 0; i < capacity; i++) {
372 MoveOnlyType value;
373 value_ptrs.push_back(value.ptr());
374 array.push_back(value.Pass());
375 ASSERT_EQ(i + 1, array.size());
376 ASSERT_EQ(i + 1, value_ptrs.size());
377 EXPECT_EQ(array.size() + 1, MoveOnlyType::num_instances());
378 EXPECT_TRUE(array[i].moved());
379 EXPECT_EQ(value_ptrs[i], array[i].ptr());
380 array[i].ResetMoved();
381 EXPECT_TRUE(array);
382 }
383 {
384 MoveOnlyType value;
385 value_ptrs.push_back(value.ptr());
386 array.push_back(value.Pass());
387 EXPECT_EQ(array.size() + 1, MoveOnlyType::num_instances());
388 }
389 ASSERT_EQ(capacity + 1, array.size());
390 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
391
392 for (size_t i = 0; i < array.size(); i++) {
393 EXPECT_TRUE(array[i].moved());
394 EXPECT_EQ(value_ptrs[i], array[i].ptr());
395 }
396 array.reset();
397 EXPECT_EQ(0u, MoveOnlyType::num_instances());
398 }
399
172 } // namespace 400 } // namespace
173 } // namespace test 401 } // namespace test
174 } // namespace mojo 402 } // namespace mojo
OLDNEW
« no previous file with comments | « mojo/public/cpp/bindings/lib/array_internal.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698