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

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

Issue 923033003: Implement unions as members of structs. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 9 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
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_internal.h" 6 #include "mojo/public/cpp/bindings/lib/array_internal.h"
7 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 7 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 8 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
9 #include "mojo/public/cpp/bindings/tests/container_test_util.h" 9 #include "mojo/public/cpp/bindings/tests/container_test_util.h"
10 #include "mojo/public/cpp/environment/environment.h" 10 #include "mojo/public/cpp/environment/environment.h"
11 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 11 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
12 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace mojo { 15 namespace mojo {
15 namespace test { 16 namespace test {
16 namespace { 17 namespace {
17 18
18 using mojo::internal::Array_Data; 19 using mojo::internal::Array_Data;
19 using mojo::internal::ArrayValidateParams; 20 using mojo::internal::ArrayValidateParams;
20 using mojo::internal::FixedBuffer; 21 using mojo::internal::FixedBuffer;
21 using mojo::internal::NoValidateParams; 22 using mojo::internal::NoValidateParams;
(...skipping 416 matching lines...) Expand 10 before | Expand all | Expand 10 after
438 EXPECT_EQ(array.size(), MoveOnlyType::num_instances()); 439 EXPECT_EQ(array.size(), MoveOnlyType::num_instances());
439 440
440 for (size_t i = 0; i < array.size(); i++) { 441 for (size_t i = 0; i < array.size(); i++) {
441 EXPECT_TRUE(array[i].moved()); 442 EXPECT_TRUE(array[i].moved());
442 EXPECT_EQ(value_ptrs[i], array[i].ptr()); 443 EXPECT_EQ(value_ptrs[i], array[i].ptr());
443 } 444 }
444 array.reset(); 445 array.reset();
445 EXPECT_EQ(0u, MoveOnlyType::num_instances()); 446 EXPECT_EQ(0u, MoveOnlyType::num_instances());
446 } 447 }
447 448
449 TEST_F(ArrayTest, PodUnionInArray) {
450 SmallStructPtr small_struct(SmallStruct::New());
451 small_struct->pod_union_array = Array<PodUnionPtr>(2);
452 small_struct->pod_union_array[0] = PodUnion::New();
453 small_struct->pod_union_array[1] = PodUnion::New();
454
455 small_struct->pod_union_array[0]->set_f_int8(10);
456 small_struct->pod_union_array[1]->set_f_int16(12);
457
458 EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8());
459 EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16());
460 }
461
462 TEST_F(ArrayTest, PodUnionInArraySerialization) {
463 Array<PodUnionPtr> array(2);
464 array[0] = PodUnion::New();
465 array[1] = PodUnion::New();
466
467 array[0]->set_f_int8(10);
468 array[1]->set_f_int16(12);
469 EXPECT_EQ(2U, array.size());
470
471 size_t size = GetSerializedSize_(array);
472 EXPECT_EQ(40U, size);
473
474 mojo::internal::FixedBuffer buf(size);
475 mojo::internal::Array_Data<internal::PodUnion_Data>* data;
476 SerializeArray_<mojo::internal::ArrayValidateParams<
477 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
478
479 Array<PodUnionPtr> array2;
480 Deserialize_(data, &array2);
481
482 EXPECT_EQ(2U, array2.size());
483
484 EXPECT_EQ(10, array2[0]->get_f_int8());
485 EXPECT_EQ(12, array2[1]->get_f_int16());
486 }
487
488 TEST_F(ArrayTest, PodUnionInArraySerializationWithNull) {
489 Array<PodUnionPtr> array(2);
490 array[0] = PodUnion::New();
491
492 array[0]->set_f_int8(10);
493 EXPECT_EQ(2U, array.size());
494
495 size_t size = GetSerializedSize_(array);
496 EXPECT_EQ(40U, size);
497
498 mojo::internal::FixedBuffer buf(size);
499 mojo::internal::Array_Data<internal::PodUnion_Data>* data;
500 SerializeArray_<mojo::internal::ArrayValidateParams<
501 0, true, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
502
503 Array<PodUnionPtr> array2;
504 Deserialize_(data, &array2);
505
506 EXPECT_EQ(2U, array2.size());
507
508 EXPECT_EQ(10, array2[0]->get_f_int8());
509 EXPECT_TRUE(array2[1].is_null());
510 }
511
448 } // namespace 512 } // namespace
449 } // namespace test 513 } // namespace test
450 } // namespace mojo 514 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698