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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: mojo/public/cpp/bindings/tests/array_unittest.cc
diff --git a/mojo/public/cpp/bindings/tests/array_unittest.cc b/mojo/public/cpp/bindings/tests/array_unittest.cc
index ed9a64709c576ee5bd94fedc2977b96049b284b6..90c8a321748ebd3bc38825c615e69f379ad95a1a 100644
--- a/mojo/public/cpp/bindings/tests/array_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/array_unittest.cc
@@ -9,6 +9,7 @@
#include "mojo/public/cpp/bindings/tests/container_test_util.h"
#include "mojo/public/cpp/environment/environment.h"
#include "mojo/public/interfaces/bindings/tests/test_structs.mojom.h"
+#include "mojo/public/interfaces/bindings/tests/test_unions.mojom.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace mojo {
@@ -445,6 +446,69 @@ TEST_F(ArrayTest, PushBack_MoveOnly) {
EXPECT_EQ(0u, MoveOnlyType::num_instances());
}
+TEST_F(ArrayTest, PodUnionInArray) {
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union_array = Array<PodUnionPtr>(2);
+ small_struct->pod_union_array[0] = PodUnion::New();
+ small_struct->pod_union_array[1] = PodUnion::New();
+
+ small_struct->pod_union_array[0]->set_f_int8(10);
+ small_struct->pod_union_array[1]->set_f_int16(12);
+
+ EXPECT_EQ(10, small_struct->pod_union_array[0]->get_f_int8());
+ EXPECT_EQ(12, small_struct->pod_union_array[1]->get_f_int16());
+}
+
+TEST_F(ArrayTest, PodUnionInArraySerialization) {
+ Array<PodUnionPtr> array(2);
+ array[0] = PodUnion::New();
+ array[1] = PodUnion::New();
+
+ array[0]->set_f_int8(10);
+ array[1]->set_f_int16(12);
+ EXPECT_EQ(2U, array.size());
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(40U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ mojo::internal::Array_Data<internal::PodUnion_Data>* data;
+ SerializeArray_<mojo::internal::ArrayValidateParams<
+ 0, false, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
+
+ Array<PodUnionPtr> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(2U, array2.size());
+
+ EXPECT_EQ(10, array2[0]->get_f_int8());
+ EXPECT_EQ(12, array2[1]->get_f_int16());
+}
+
+TEST_F(ArrayTest, PodUnionInArraySerializationWithNull) {
+ Array<PodUnionPtr> array(2);
+ array[0] = PodUnion::New();
+
+ array[0]->set_f_int8(10);
+ EXPECT_EQ(2U, array.size());
+
+ size_t size = GetSerializedSize_(array);
+ EXPECT_EQ(40U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ mojo::internal::Array_Data<internal::PodUnion_Data>* data;
+ SerializeArray_<mojo::internal::ArrayValidateParams<
+ 0, true, mojo::internal::NoValidateParams>>(array.Pass(), &buf, &data);
+
+ Array<PodUnionPtr> array2;
+ Deserialize_(data, &array2);
+
+ EXPECT_EQ(2U, array2.size());
+
+ EXPECT_EQ(10, array2[0]->get_f_int8());
+ EXPECT_TRUE(array2[1].is_null());
+}
+
} // namespace
} // namespace test
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698