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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | mojo/public/interfaces/bindings/tests/test_unions.mojom » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 6124d275f2c422c96eb52183ac1cb8acfaca96dd..5017d979d2ddc907b530066b7225ba4843bdd940 100644
--- a/mojo/public/cpp/bindings/tests/union_unittest.cc
+++ b/mojo/public/cpp/bindings/tests/union_unittest.cc
@@ -2,6 +2,7 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
+#include <vector>
#include "mojo/public/cpp/bindings/array.h"
#include "mojo/public/cpp/bindings/lib/array_internal.h"
#include "mojo/public/cpp/bindings/lib/array_serialization.h"
@@ -637,6 +638,7 @@ TEST(UnionTest, StructInUnionValidation) {
static_cast<uint32_t>(size), 0);
EXPECT_TRUE(
internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
+ free(raw_buf);
}
TEST(UnionTest, StructInUnionValidationNonNullable) {
@@ -657,6 +659,7 @@ TEST(UnionTest, StructInUnionValidationNonNullable) {
static_cast<uint32_t>(size), 0);
EXPECT_FALSE(
internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
+ free(raw_buf);
}
TEST(UnionTest, StructInUnionValidationNullable) {
@@ -677,6 +680,144 @@ TEST(UnionTest, StructInUnionValidationNullable) {
static_cast<uint32_t>(size), 0);
EXPECT_TRUE(
internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
+ free(raw_buf);
+}
+
+TEST(UnionTest, ArrayInUnionGetterSetter) {
+ Environment environment;
+
+ Array<int8_t> array(2);
+ array[0] = 8;
+ array[1] = 9;
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_array_int8(array.Pass());
+
+ EXPECT_EQ(8, obj->get_f_array_int8()[0]);
+ EXPECT_EQ(9, obj->get_f_array_int8()[1]);
+}
+
+TEST(UnionTest, ArrayInUnionSerialization) {
+ Environment environment;
+
+ Array<int8_t> array(2);
+ array[0] = 8;
+ array[1] = 9;
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_array_int8(array.Pass());
+
+ size_t size = GetSerializedSize_(obj, false);
+ EXPECT_EQ(32U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::ObjectUnion_Data* data = nullptr;
+ SerializeUnion_(obj.Pass(), &buf, &data, false);
+
+ std::vector<Handle> handles;
+ data->EncodePointersAndHandles(&handles);
+ data->DecodePointersAndHandles(&handles);
+
+ ObjectUnionPtr obj2;
+ Deserialize_(data, &obj2);
+
+ EXPECT_EQ(8, obj2->get_f_array_int8()[0]);
+ EXPECT_EQ(9, obj2->get_f_array_int8()[1]);
+}
+
+TEST(UnionTest, ArrayInUnionValidation) {
+ Environment environment;
+
+ Array<int8_t> array(2);
+ array[0] = 8;
+ array[1] = 9;
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_array_int8(array.Pass());
+
+ size_t size = GetSerializedSize_(obj, false);
+ mojo::internal::FixedBuffer buf(size);
+ internal::ObjectUnion_Data* data = nullptr;
+ SerializeUnion_(obj.Pass(), &buf, &data, false);
+
+ std::vector<Handle> handles;
+ data->EncodePointersAndHandles(&handles);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data,
+ static_cast<uint32_t>(size), 0);
+
+ EXPECT_TRUE(
+ internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
+ free(raw_buf);
+}
+
+TEST(UnionTest, MapInUnionGetterSetter) {
+ Environment environment;
+ Map<String, int8_t> map;
+ map.insert("one", 1);
+ map.insert("two", 2);
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_map_int8(map.Pass());
+
+ EXPECT_EQ(1, obj->get_f_map_int8()["one"]);
+ EXPECT_EQ(2, obj->get_f_map_int8()["two"]);
+}
+
+TEST(UnionTest, MapInUnionSerialization) {
+ Environment environment;
+ Map<String, int8_t> map;
+ map.insert("one", 1);
+ map.insert("two", 2);
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_map_int8(map.Pass());
+
+ size_t size = GetSerializedSize_(obj, false);
+ EXPECT_EQ(112U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::ObjectUnion_Data* data = nullptr;
+ SerializeUnion_(obj.Pass(), &buf, &data, false);
+
+ std::vector<Handle> handles;
+ data->EncodePointersAndHandles(&handles);
+ data->DecodePointersAndHandles(&handles);
+
+ ObjectUnionPtr obj2;
+ Deserialize_(data, &obj2);
+
+ EXPECT_EQ(1, obj2->get_f_map_int8()["one"]);
+ EXPECT_EQ(2, obj2->get_f_map_int8()["two"]);
+}
+
+TEST(UnionTest, MapInUnionValidation) {
+ Environment environment;
+ Map<String, int8_t> map;
+ map.insert("one", 1);
+ map.insert("two", 2);
+
+ ObjectUnionPtr obj(ObjectUnion::New());
+ obj->set_f_map_int8(map.Pass());
+
+ size_t size = GetSerializedSize_(obj, false);
+ EXPECT_EQ(112U, size);
+
+ mojo::internal::FixedBuffer buf(size);
+ internal::ObjectUnion_Data* data = nullptr;
+ SerializeUnion_(obj.Pass(), &buf, &data, false);
+
+ std::vector<Handle> handles;
+ data->EncodePointersAndHandles(&handles);
+
+ void* raw_buf = buf.Leak();
+ mojo::internal::BoundsChecker bounds_checker(data,
+ static_cast<uint32_t>(size), 0);
+
+ EXPECT_TRUE(
+ internal::ObjectUnion_Data::Validate(raw_buf, &bounds_checker, false));
+ free(raw_buf);
}
} // namespace test
« no previous file with comments | « no previous file | mojo/public/interfaces/bindings/tests/test_unions.mojom » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698