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

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

Issue 1065653006: Array and map members for unions. (Closed) Base URL: https://github.com/domokit/mojo.git@master
Patch Set: Created 5 years, 8 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 <vector>
5 #include "mojo/public/cpp/bindings/array.h" 6 #include "mojo/public/cpp/bindings/array.h"
6 #include "mojo/public/cpp/bindings/lib/array_internal.h" 7 #include "mojo/public/cpp/bindings/lib/array_internal.h"
7 #include "mojo/public/cpp/bindings/lib/array_serialization.h" 8 #include "mojo/public/cpp/bindings/lib/array_serialization.h"
8 #include "mojo/public/cpp/bindings/lib/bounds_checker.h" 9 #include "mojo/public/cpp/bindings/lib/bounds_checker.h"
9 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h" 10 #include "mojo/public/cpp/bindings/lib/fixed_buffer.h"
10 #include "mojo/public/cpp/bindings/string.h" 11 #include "mojo/public/cpp/bindings/string.h"
11 #include "mojo/public/cpp/environment/environment.h" 12 #include "mojo/public/cpp/environment/environment.h"
12 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h" 13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
13 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h" 14 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
14 #include "testing/gtest/include/gtest/gtest.h" 15 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 657 matching lines...) Expand 10 before | Expand all | Expand 10 after
672 internal::ObjectUnion_Data* data = nullptr; 673 internal::ObjectUnion_Data* data = nullptr;
673 SerializeUnion_(obj.Pass(), &buf, &data, false); 674 SerializeUnion_(obj.Pass(), &buf, &data, false);
674 675
675 void* raw_buf = buf.Leak(); 676 void* raw_buf = buf.Leak();
676 mojo::internal::BoundsChecker bounds_checker(data, 677 mojo::internal::BoundsChecker bounds_checker(data,
677 static_cast<uint32_t>(size), 0); 678 static_cast<uint32_t>(size), 0);
678 EXPECT_TRUE( 679 EXPECT_TRUE(
679 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false)); 680 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
680 } 681 }
681 682
683 TEST(UnionTest, ArrayInUnionGetterSetter) {
684 Environment environment;
685
686 Array<int8_t> array(2);
687 array[0] = 8;
688 array[1] = 9;
689
690 ObjectUnionPtr obj(ObjectUnion::New());
691 obj->set_f_array_int8(array.Pass());
692
693 EXPECT_EQ(8, obj->get_f_array_int8()[0]);
694 EXPECT_EQ(9, obj->get_f_array_int8()[1]);
695 }
696
697 TEST(UnionTest, ArrayInUnionSerialization) {
698 Environment environment;
699
700 Array<int8_t> array(2);
701 array[0] = 8;
702 array[1] = 9;
703
704 ObjectUnionPtr obj(ObjectUnion::New());
705 obj->set_f_array_int8(array.Pass());
706
707 size_t size = GetSerializedSize_(obj, false);
708 EXPECT_EQ(32U, size);
709
710 mojo::internal::FixedBuffer buf(size);
711 internal::ObjectUnion_Data* data = nullptr;
712 SerializeUnion_(obj.Pass(), &buf, &data, false);
713
714 std::vector<Handle> handles;
715 data->EncodePointersAndHandles(&handles);
716 data->DecodePointersAndHandles(&handles);
717
718 ObjectUnionPtr obj2;
719 Deserialize_(data, &obj2);
720
721 EXPECT_EQ(8, obj2->get_f_array_int8()[0]);
722 EXPECT_EQ(9, obj2->get_f_array_int8()[1]);
723 }
724
725 TEST(UnionTest, ArrayInUnionValidation) {
726 Environment environment;
727
728 Array<int8_t> array(2);
729 array[0] = 8;
730 array[1] = 9;
731
732 ObjectUnionPtr obj(ObjectUnion::New());
733 obj->set_f_array_int8(array.Pass());
734
735 size_t size = GetSerializedSize_(obj, false);
736 mojo::internal::FixedBuffer buf(size);
737 internal::ObjectUnion_Data* data = nullptr;
738 SerializeUnion_(obj.Pass(), &buf, &data, false);
739
740 std::vector<Handle> handles;
741 data->EncodePointersAndHandles(&handles);
742
743 void* raw_buf = buf.Leak();
yzshen1 2015/04/21 16:39:14 Please dispose it properly.
azani 2015/04/21 17:43:33 Done.
744 mojo::internal::BoundsChecker bounds_checker(data,
745 static_cast<uint32_t>(size), 0);
746
747 EXPECT_TRUE(
748 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
749 }
750
751 TEST(UnionTest, MapInUnionGetterSetter) {
752 Environment environment;
753 Map<String, int8_t> map;
754 map.insert("one", 1);
755 map.insert("two", 2);
756
757 ObjectUnionPtr obj(ObjectUnion::New());
758 obj->set_f_map_int8(map.Pass());
759
760 EXPECT_EQ(1, obj->get_f_map_int8()["one"]);
761 EXPECT_EQ(2, obj->get_f_map_int8()["two"]);
762 }
763
764 TEST(UnionTest, MapInUnionSerialization) {
765 Environment environment;
766 Map<String, int8_t> map;
767 map.insert("one", 1);
768 map.insert("two", 2);
769
770 ObjectUnionPtr obj(ObjectUnion::New());
771 obj->set_f_map_int8(map.Pass());
772
773 size_t size = GetSerializedSize_(obj, false);
774 EXPECT_EQ(112U, size);
775
776 mojo::internal::FixedBuffer buf(size);
777 internal::ObjectUnion_Data* data = nullptr;
778 SerializeUnion_(obj.Pass(), &buf, &data, false);
779
780 std::vector<Handle> handles;
781 data->EncodePointersAndHandles(&handles);
782 data->DecodePointersAndHandles(&handles);
783
784 ObjectUnionPtr obj2;
785 Deserialize_(data, &obj2);
786
787 EXPECT_EQ(1, obj2->get_f_map_int8()["one"]);
788 EXPECT_EQ(2, obj2->get_f_map_int8()["two"]);
789 }
790
791 TEST(UnionTest, MapInUnionValidation) {
792 Environment environment;
793 Map<String, int8_t> map;
794 map.insert("one", 1);
795 map.insert("two", 2);
796
797 ObjectUnionPtr obj(ObjectUnion::New());
798 obj->set_f_map_int8(map.Pass());
799
800 size_t size = GetSerializedSize_(obj, false);
801 EXPECT_EQ(112U, size);
802
803 mojo::internal::FixedBuffer buf(size);
804 internal::ObjectUnion_Data* data = nullptr;
805 SerializeUnion_(obj.Pass(), &buf, &data, false);
806
807 std::vector<Handle> handles;
808 data->EncodePointersAndHandles(&handles);
809
810 void* raw_buf = buf.Leak();
yzshen1 2015/04/21 16:39:14 ditto.
azani 2015/04/21 17:43:33 Done.
811 mojo::internal::BoundsChecker bounds_checker(data,
812 static_cast<uint32_t>(size), 0);
813
814 EXPECT_TRUE(
815 internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
816 }
817
682 } // namespace test 818 } // namespace test
683 } // namespace mojo 819 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698