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

Unified Diff: mojo/public/cpp/bindings/tests/union_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, 10 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/union_unittest.cc
diff --git a/mojo/public/cpp/bindings/tests/union_unittest.cc b/mojo/public/cpp/bindings/tests/union_unittest.cc
index f811a22a85cef5d8bc485a75f3497b541318d8f1..2b37ba50cc4875db721ab88a4389f5ef0b132ab4 100644
--- a/mojo/public/cpp/bindings/tests/union_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/union_unittest.cc
@@ -108,7 +108,7 @@ TEST(UnionTest, SerializationPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
+ internal::PodUnion_Data* data = nullptr;
Serialize_(pod1.Pass(), &buf, &data);
PodUnionPtr pod2;
@@ -127,7 +127,7 @@ TEST(UnionTest, ValidationJustWorksPod) {
EXPECT_EQ(16U, size);
mojo::internal::FixedBuffer buf(size);
- internal::PodUnion_Data* data;
+ internal::PodUnion_Data* data = nullptr;
Serialize_(pod.Pass(), &buf, &data);
void* raw_buf = buf.Leak();
mojo::internal::BoundsChecker bounds_checker(data, size, 0);
@@ -135,6 +135,16 @@ TEST(UnionTest, ValidationJustWorksPod) {
free(raw_buf);
}
+TEST(UnionTest, SerializeNotNull) {
+ PodUnionPtr pod(PodUnion::New());
+ pod->set_f_int8(0);
+ size_t size = GetSerializedSize_(pod);
+ mojo::internal::FixedBuffer buf(size);
+ internal::PodUnion_Data* data = nullptr;
+ Serialize_(pod.Pass(), &buf, &data);
+ EXPECT_FALSE(data->is_null());
+}
+
TEST(UnionTest, NullValidation) {
void* buf = nullptr;
mojo::internal::BoundsChecker bounds_checker(buf, 0, 0);
@@ -220,7 +230,7 @@ TEST(UnionTest, StringSerialization) {
size_t size = GetSerializedSize_(pod1);
mojo::internal::FixedBuffer buf(size);
- internal::ObjectUnion_Data* data;
+ internal::ObjectUnion_Data* data = nullptr;
Serialize_(pod1.Pass(), &buf, &data);
ObjectUnionPtr pod2;
@@ -274,5 +284,133 @@ TEST(UnionTest, StringValidationValidateString) {
EXPECT_FALSE(internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker));
free(raw_buf);
}
+
+TEST(UnionTest, PodUnionInStruct) {
+ SmallStructPtr small_struct(SmallStruct::New());
yzshen1 2015/02/17 19:34:35 Why do we need this test and ObjectUnionInStruct?
azani 2015/02/18 00:27:57 Artefacts of TDD. Gone now.
+ small_struct->pod_union = PodUnion::New();
+ small_struct->pod_union->set_f_int8(10);
+ EXPECT_EQ(10, small_struct->pod_union->get_f_int8());
+}
+
+TEST(UnionTest, PodUnionInStructClone) {
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union = PodUnion::New();
+ small_struct->pod_union->set_f_int8(10);
+
+ SmallStructPtr clone = small_struct.Clone();
+ EXPECT_EQ(10, clone->pod_union->get_f_int8());
+}
+
+TEST(UnionTest, ObjectUnionInStruct) {
+ SmallObjStructPtr small_struct(SmallObjStruct::New());
+ String hello("hello world");
+ small_struct->obj_union = ObjectUnion::New();
+ small_struct->obj_union->set_f_string(hello);
+ EXPECT_EQ(hello, small_struct->obj_union->get_f_string());
+}
+
+TEST(UnionTest, PodUnionInStructSerialization) {
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union = PodUnion::New();
+ small_struct->pod_union->set_f_int32(10);
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStruct_Data* data = nullptr;
+ Serialize_(small_struct.Pass(), &buf, &data);
+
+ SmallStructPtr deserialized;
+ Deserialize_(data, &deserialized);
+
+ EXPECT_EQ(10, deserialized->pod_union->get_f_int32());
+}
+
+TEST(UnionTest, ObjectUnionInStructSerialization) {
+ SmallObjStructPtr obj_struct(SmallObjStruct::New());
+ obj_struct->obj_union = ObjectUnion::New();
+ String hello("hello world");
+ obj_struct->obj_union->set_f_string(hello);
+
+ size_t size = GetSerializedSize_(obj_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallObjStruct_Data* data = nullptr;
+ Serialize_(obj_struct.Pass(), &buf, &data);
+
+ SmallObjStructPtr deserialized;
+ Deserialize_(data, &deserialized);
+
+ EXPECT_EQ(hello, deserialized->obj_union->get_f_string());
+}
+
+TEST(UnionTest, ValidationJustWorksForUnionsInStructs) {
yzshen1 2015/02/17 19:34:35 Nit: maybe UnionsInStructsValidation?
azani 2015/02/18 00:27:57 Done.
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union = PodUnion::New();
+ small_struct->pod_union->set_f_int32(10);
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStruct_Data* data = nullptr;
+ Serialize_(small_struct.Pass(), &buf, &data);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data, size, 0);
+ EXPECT_TRUE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
+ free(raw_buf);
+}
+
+TEST(UnionTest, PodUnionInStructValidationFails) {
yzshen1 2015/02/17 19:34:35 Maybe use the same style across multiple test name
azani 2015/02/18 00:27:57 Done.
+ Environment environment;
+ SmallStructPtr small_struct(SmallStruct::New());
+ small_struct->pod_union = PodUnion::New();
+ small_struct->pod_union->set_f_int32(10);
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStruct_Data* data = nullptr;
+ Serialize_(small_struct.Pass(), &buf, &data);
+ data->pod_union.tag = static_cast<internal::PodUnion_Data::PodUnion_Tag>(100);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data, size, 0);
+ EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
+ free(raw_buf);
+}
+
+TEST(UnionTest, ValidationNullUnionFailure) {
+ Environment environment;
+ SmallStructPtr small_struct(SmallStruct::New());
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStruct_Data* data = nullptr;
+ Serialize_(small_struct.Pass(), &buf, &data);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data, size, 0);
+ EXPECT_FALSE(internal::SmallStruct_Data::Validate(raw_buf, &bounds_checker));
+ free(raw_buf);
+}
+
+TEST(UnionTest, ValidationNullbleUnion) {
+ Environment environment;
+ SmallStructNullableUnionPtr small_struct(SmallStructNullableUnion::New());
+
+ size_t size = GetSerializedSize_(small_struct);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::SmallStructNullableUnion_Data* data = nullptr;
+ Serialize_(small_struct.Pass(), &buf, &data);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data, size, 0);
+ EXPECT_TRUE(internal::SmallStructNullableUnion_Data::Validate(
+ raw_buf, &bounds_checker));
+ free(raw_buf);
+}
} // namespace test
} // namespace mojo

Powered by Google App Engine
This is Rietveld 408576698