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

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

Issue 2607063002: Remove mojo::Array. (Closed)
Patch Set: rebase Created 3 years, 11 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 <stddef.h> 5 #include <stddef.h>
6 #include <stdint.h> 6 #include <stdint.h>
7 #include <utility> 7 #include <utility>
8 #include <vector> 8 #include <vector>
9 9
10 #include "base/message_loop/message_loop.h" 10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h" 11 #include "base/run_loop.h"
12 #include "mojo/public/cpp/bindings/array.h"
13 #include "mojo/public/cpp/bindings/binding.h" 12 #include "mojo/public/cpp/bindings/binding.h"
14 #include "mojo/public/cpp/bindings/lib/array_internal.h" 13 #include "mojo/public/cpp/bindings/lib/array_internal.h"
15 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 14 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
16 #include "mojo/public/cpp/bindings/lib/serialization.h" 15 #include "mojo/public/cpp/bindings/lib/serialization.h"
17 #include "mojo/public/cpp/bindings/lib/validation_context.h" 16 #include "mojo/public/cpp/bindings/lib/validation_context.h"
18 #include "mojo/public/cpp/bindings/lib/validation_errors.h" 17 #include "mojo/public/cpp/bindings/lib/validation_errors.h"
19 #include "mojo/public/cpp/test_support/test_utils.h" 18 #include "mojo/public/cpp/test_support/test_utils.h"
20 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 19 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
21 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" 20 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
22 #include "testing/gtest/include/gtest/gtest.h" 21 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 397 matching lines...) Expand 10 before | Expand all | Expand 10 after
420 small_struct->pod_union_array.value()[1] = PodUnion::New(); 419 small_struct->pod_union_array.value()[1] = PodUnion::New();
421 420
422 small_struct->pod_union_array.value()[0]->set_f_int8(10); 421 small_struct->pod_union_array.value()[0]->set_f_int8(10);
423 small_struct->pod_union_array.value()[1]->set_f_int16(12); 422 small_struct->pod_union_array.value()[1]->set_f_int16(12);
424 423
425 EXPECT_EQ(10, small_struct->pod_union_array.value()[0]->get_f_int8()); 424 EXPECT_EQ(10, small_struct->pod_union_array.value()[0]->get_f_int8());
426 EXPECT_EQ(12, small_struct->pod_union_array.value()[1]->get_f_int16()); 425 EXPECT_EQ(12, small_struct->pod_union_array.value()[1]->get_f_int16());
427 } 426 }
428 427
429 TEST(UnionTest, PodUnionInArraySerialization) { 428 TEST(UnionTest, PodUnionInArraySerialization) {
430 Array<PodUnionPtr> array(2); 429 std::vector<PodUnionPtr> array(2);
431 array[0] = PodUnion::New(); 430 array[0] = PodUnion::New();
432 array[1] = PodUnion::New(); 431 array[1] = PodUnion::New();
433 432
434 array[0]->set_f_int8(10); 433 array[0]->set_f_int8(10);
435 array[1]->set_f_int16(12); 434 array[1]->set_f_int16(12);
436 EXPECT_EQ(2U, array.size()); 435 EXPECT_EQ(2U, array.size());
437 436
438 size_t size = 437 size_t size =
439 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>( 438 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>(
440 array, nullptr); 439 array, nullptr);
441 EXPECT_EQ(40U, size); 440 EXPECT_EQ(40U, size);
442 441
443 mojo::internal::FixedBufferForTesting buf(size); 442 mojo::internal::FixedBufferForTesting buf(size);
444 mojo::internal::Array_Data<internal::PodUnion_Data>* data; 443 mojo::internal::Array_Data<internal::PodUnion_Data>* data;
445 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr); 444 mojo::internal::ContainerValidateParams validate_params(0, false, nullptr);
446 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>( 445 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>(
447 array, &buf, &data, &validate_params, nullptr); 446 array, &buf, &data, &validate_params, nullptr);
448 447
449 Array<PodUnionPtr> array2; 448 std::vector<PodUnionPtr> array2;
450 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2, 449 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2,
451 nullptr); 450 nullptr);
452 451
453 EXPECT_EQ(2U, array2.size()); 452 EXPECT_EQ(2U, array2.size());
454 453
455 EXPECT_EQ(10, array2[0]->get_f_int8()); 454 EXPECT_EQ(10, array2[0]->get_f_int8());
456 EXPECT_EQ(12, array2[1]->get_f_int16()); 455 EXPECT_EQ(12, array2[1]->get_f_int16());
457 } 456 }
458 457
459 TEST(UnionTest, PodUnionInArraySerializationWithNull) { 458 TEST(UnionTest, PodUnionInArraySerializationWithNull) {
460 Array<PodUnionPtr> array(2); 459 std::vector<PodUnionPtr> array(2);
461 array[0] = PodUnion::New(); 460 array[0] = PodUnion::New();
462 461
463 array[0]->set_f_int8(10); 462 array[0]->set_f_int8(10);
464 EXPECT_EQ(2U, array.size()); 463 EXPECT_EQ(2U, array.size());
465 464
466 size_t size = 465 size_t size =
467 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>( 466 mojo::internal::PrepareToSerialize<ArrayDataView<PodUnionDataView>>(
468 array, nullptr); 467 array, nullptr);
469 EXPECT_EQ(40U, size); 468 EXPECT_EQ(40U, size);
470 469
471 mojo::internal::FixedBufferForTesting buf(size); 470 mojo::internal::FixedBufferForTesting buf(size);
472 mojo::internal::Array_Data<internal::PodUnion_Data>* data; 471 mojo::internal::Array_Data<internal::PodUnion_Data>* data;
473 mojo::internal::ContainerValidateParams validate_params(0, true, nullptr); 472 mojo::internal::ContainerValidateParams validate_params(0, true, nullptr);
474 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>( 473 mojo::internal::Serialize<ArrayDataView<PodUnionDataView>>(
475 array, &buf, &data, &validate_params, nullptr); 474 array, &buf, &data, &validate_params, nullptr);
476 475
477 Array<PodUnionPtr> array2; 476 std::vector<PodUnionPtr> array2;
478 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2, 477 mojo::internal::Deserialize<ArrayDataView<PodUnionDataView>>(data, &array2,
479 nullptr); 478 nullptr);
480 479
481 EXPECT_EQ(2U, array2.size()); 480 EXPECT_EQ(2U, array2.size());
482 481
483 EXPECT_EQ(10, array2[0]->get_f_int8()); 482 EXPECT_EQ(10, array2[0]->get_f_int8());
484 EXPECT_TRUE(array2[1].is_null()); 483 EXPECT_TRUE(array2[1].is_null());
485 } 484 }
486 485
487 TEST(UnionTest, ObjectUnionInArraySerialization) { 486 TEST(UnionTest, ObjectUnionInArraySerialization) {
488 Array<ObjectUnionPtr> array(2); 487 std::vector<ObjectUnionPtr> array(2);
489 array[0] = ObjectUnion::New(); 488 array[0] = ObjectUnion::New();
490 array[1] = ObjectUnion::New(); 489 array[1] = ObjectUnion::New();
491 490
492 array[0]->set_f_string("hello"); 491 array[0]->set_f_string("hello");
493 array[1]->set_f_string("world"); 492 array[1]->set_f_string("world");
494 EXPECT_EQ(2U, array.size()); 493 EXPECT_EQ(2U, array.size());
495 494
496 size_t size = 495 size_t size =
497 mojo::internal::PrepareToSerialize<ArrayDataView<ObjectUnionDataView>>( 496 mojo::internal::PrepareToSerialize<ArrayDataView<ObjectUnionDataView>>(
498 array, nullptr); 497 array, nullptr);
(...skipping 14 matching lines...) Expand all
513 free(raw_buf); 512 free(raw_buf);
514 513
515 data = 514 data =
516 reinterpret_cast<mojo::internal::Array_Data<internal::ObjectUnion_Data>*>( 515 reinterpret_cast<mojo::internal::Array_Data<internal::ObjectUnion_Data>*>(
517 new_buf.data()); 516 new_buf.data());
518 mojo::internal::ValidationContext validation_context( 517 mojo::internal::ValidationContext validation_context(
519 data, static_cast<uint32_t>(size), 0); 518 data, static_cast<uint32_t>(size), 0);
520 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate( 519 ASSERT_TRUE(mojo::internal::Array_Data<internal::ObjectUnion_Data>::Validate(
521 data, &validation_context, &validate_params)); 520 data, &validation_context, &validate_params));
522 521
523 Array<ObjectUnionPtr> array2; 522 std::vector<ObjectUnionPtr> array2;
524 mojo::internal::Deserialize<ArrayDataView<ObjectUnionDataView>>(data, &array2, 523 mojo::internal::Deserialize<ArrayDataView<ObjectUnionDataView>>(data, &array2,
525 nullptr); 524 nullptr);
526 525
527 EXPECT_EQ(2U, array2.size()); 526 EXPECT_EQ(2U, array2.size());
528 527
529 EXPECT_EQ("hello", array2[0]->get_f_string()); 528 EXPECT_EQ("hello", array2[0]->get_f_string());
530 EXPECT_EQ("world", array2[1]->get_f_string()); 529 EXPECT_EQ("world", array2[1]->get_f_string());
531 } 530 }
532 531
533 // TODO(azani): Move back in struct_unittest.cc when possible. 532 // TODO(azani): Move back in struct_unittest.cc when possible.
(...skipping 307 matching lines...) Expand 10 before | Expand all | Expand 10 after
841 840
842 void* raw_buf = buf.Leak(); 841 void* raw_buf = buf.Leak();
843 mojo::internal::ValidationContext validation_context( 842 mojo::internal::ValidationContext validation_context(
844 data, static_cast<uint32_t>(size), 0); 843 data, static_cast<uint32_t>(size), 0);
845 EXPECT_TRUE(internal::ObjectUnion_Data::Validate( 844 EXPECT_TRUE(internal::ObjectUnion_Data::Validate(
846 raw_buf, &validation_context, false)); 845 raw_buf, &validation_context, false));
847 free(raw_buf); 846 free(raw_buf);
848 } 847 }
849 848
850 TEST(UnionTest, ArrayInUnionGetterSetter) { 849 TEST(UnionTest, ArrayInUnionGetterSetter) {
851 Array<int8_t> array(2); 850 std::vector<int8_t> array(2);
852 array[0] = 8; 851 array[0] = 8;
853 array[1] = 9; 852 array[1] = 9;
854 853
855 ObjectUnionPtr obj(ObjectUnion::New()); 854 ObjectUnionPtr obj(ObjectUnion::New());
856 obj->set_f_array_int8(std::move(array)); 855 obj->set_f_array_int8(std::move(array));
857 856
858 EXPECT_EQ(8, obj->get_f_array_int8()[0]); 857 EXPECT_EQ(8, obj->get_f_array_int8()[0]);
859 EXPECT_EQ(9, obj->get_f_array_int8()[1]); 858 EXPECT_EQ(9, obj->get_f_array_int8()[1]);
860 } 859 }
861 860
862 TEST(UnionTest, ArrayInUnionSerialization) { 861 TEST(UnionTest, ArrayInUnionSerialization) {
863 Array<int8_t> array(2); 862 std::vector<int8_t> array(2);
864 array[0] = 8; 863 array[0] = 8;
865 array[1] = 9; 864 array[1] = 9;
866 865
867 ObjectUnionPtr obj(ObjectUnion::New()); 866 ObjectUnionPtr obj(ObjectUnion::New());
868 obj->set_f_array_int8(std::move(array)); 867 obj->set_f_array_int8(std::move(array));
869 868
870 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( 869 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>(
871 obj, false, nullptr); 870 obj, false, nullptr);
872 EXPECT_EQ(32U, size); 871 EXPECT_EQ(32U, size);
873 872
874 mojo::internal::FixedBufferForTesting buf(size); 873 mojo::internal::FixedBufferForTesting buf(size);
875 internal::ObjectUnion_Data* data = nullptr; 874 internal::ObjectUnion_Data* data = nullptr;
876 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false, 875 mojo::internal::Serialize<ObjectUnionDataView>(obj, &buf, &data, false,
877 nullptr); 876 nullptr);
878 877
879 ObjectUnionPtr obj2; 878 ObjectUnionPtr obj2;
880 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, nullptr); 879 mojo::internal::Deserialize<ObjectUnionDataView>(data, &obj2, nullptr);
881 880
882 EXPECT_EQ(8, obj2->get_f_array_int8()[0]); 881 EXPECT_EQ(8, obj2->get_f_array_int8()[0]);
883 EXPECT_EQ(9, obj2->get_f_array_int8()[1]); 882 EXPECT_EQ(9, obj2->get_f_array_int8()[1]);
884 } 883 }
885 884
886 TEST(UnionTest, ArrayInUnionValidation) { 885 TEST(UnionTest, ArrayInUnionValidation) {
887 Array<int8_t> array(2); 886 std::vector<int8_t> array(2);
888 array[0] = 8; 887 array[0] = 8;
889 array[1] = 9; 888 array[1] = 9;
890 889
891 ObjectUnionPtr obj(ObjectUnion::New()); 890 ObjectUnionPtr obj(ObjectUnion::New());
892 obj->set_f_array_int8(std::move(array)); 891 obj->set_f_array_int8(std::move(array));
893 892
894 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>( 893 size_t size = mojo::internal::PrepareToSerialize<ObjectUnionDataView>(
895 obj, false, nullptr); 894 obj, false, nullptr);
896 mojo::internal::FixedBufferForTesting buf(size); 895 mojo::internal::FixedBufferForTesting buf(size);
897 internal::ObjectUnion_Data* data = nullptr; 896 internal::ObjectUnion_Data* data = nullptr;
(...skipping 340 matching lines...) Expand 10 before | Expand all | Expand 10 after
1238 1237
1239 PodUnionPtr pod(PodUnion::New()); 1238 PodUnionPtr pod(PodUnion::New());
1240 pod->set_f_int16(16); 1239 pod->set_f_int16(16);
1241 1240
1242 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16)); 1241 ptr->Echo(std::move(pod), base::Bind(&ExpectInt16, 16));
1243 base::RunLoop().RunUntilIdle(); 1242 base::RunLoop().RunUntilIdle();
1244 } 1243 }
1245 1244
1246 } // namespace test 1245 } // namespace test
1247 } // namespace mojo 1246 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698