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

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

Powered by Google App Engine
This is Rietveld 408576698