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

Unified Diff: mojo/public/c/bindings/tests/union_unittest.cc

Issue 2234063002: Move the C bindings tests to the new location. (Closed) Base URL: https://github.com/domokit/mojo.git@work791_mojo_bindings
Patch Set: Created 4 years, 4 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 | « mojo/public/c/bindings/tests/testing_util.h ('k') | mojo/public/c/bindings/tests/validation_unittest.cc » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: mojo/public/c/bindings/tests/union_unittest.cc
diff --git a/mojo/public/c/bindings/tests/union_unittest.cc b/mojo/public/c/bindings/tests/union_unittest.cc
deleted file mode 100644
index b2d790fdcded8e2f3494ffa69bf720ef2c3830b1..0000000000000000000000000000000000000000
--- a/mojo/public/c/bindings/tests/union_unittest.cc
+++ /dev/null
@@ -1,203 +0,0 @@
-// Copyright 2016 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-// TODO(vardhan): Needs a lot more testing.
-
-#include <mojo/bindings/union.h>
-
-#include <mojo/bindings/array.h>
-
-#include "mojo/public/c/bindings/tests/testing_util.h"
-#include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h"
-#include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h"
-#include "testing/gtest/include/gtest/gtest.h"
-
-namespace {
-
-struct mojo_test_StructOfUnionOfReferences* MakeStructOfUnionReference(
- struct MojomBuffer* buf) {
- auto* struct_with_union =
- static_cast<struct mojo_test_StructOfUnionOfReferences*>(
- MojomBuffer_Allocate(
- buf, sizeof(struct mojo_test_StructOfUnionOfReferences)));
-
- *struct_with_union = mojo_test_StructOfUnionOfReferences{
- // header
- {
- sizeof(struct mojo_test_StructOfUnionOfReferences), 0,
- },
- {
- 0u, mojo_test_UnionOfReferences_Tag__UNKNOWN__, {},
- } // null
- };
- return struct_with_union;
-}
-
-// Test serialization when a union points to a union.
-TEST(UnionSerializationTest, UnionOfUnion) {
- char buffer_bytes[1000] = {0};
- struct MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0};
- struct mojo_test_StructOfUnionOfReferences* struct_with_union =
- MakeStructOfUnionReference(&buf);
-
- EXPECT_EQ(8u + 1 * 16u, // 1 union type (set to null)
- mojo_test_StructOfUnionOfReferences_ComputeSerializedSize(
- struct_with_union));
-
- struct_with_union->u.size = 16u;
- EXPECT_EQ(8u + 1 * 16u, // 1 union type (not null, but unknown)
- mojo_test_StructOfUnionOfReferences_ComputeSerializedSize(
- struct_with_union));
-
- // Test when a union points to a union.
- struct mojo_test_PodUnion* pod_union =
- static_cast<struct mojo_test_PodUnion*>(
- MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_PodUnion)));
- *pod_union = mojo_test_PodUnion{
- 16u, mojo_test_PodUnion_Tag_f_int8, {13},
- };
- struct_with_union->u.tag = mojo_test_UnionOfReferences_Tag_pod_union;
- struct_with_union->u.data.f_pod_union.ptr = pod_union;
- EXPECT_EQ(8u + 1 * 16u // 1 union type (set to PodUnion)
- + 16u, // PodUnion is out of line.
- mojo_test_StructOfUnionOfReferences_ComputeSerializedSize(
- struct_with_union));
-
- // We save the underlying (unencoded) buffer. We can compare the two after
- // deserialization to make sure deserialization is correct.
- char buffer_bytes_copy[sizeof(buffer_bytes)];
- memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy));
-
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, NULL);
-
- EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) -
- offsetof(struct mojo_test_StructOfUnionOfReferences, u.data),
- struct_with_union->u.data.f_pod_union.offset);
-
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, NULL, 0);
- EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used));
-
- {
- char buffer_bytes2[sizeof(buffer_bytes)] = {0};
- struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0};
- CopyAndCompare(
- &buf2, struct_with_union, buf.num_bytes_used,
- mojo_test_StructOfUnionOfReferences_DeepCopy,
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles,
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles);
- }
-}
-
-// Test when a union points to a struct.
-TEST(UnionSerializationTest, UnionOfStruct) {
- char buffer_bytes[1000];
- MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0};
-
- struct mojo_test_StructOfUnionOfReferences* struct_with_union =
- MakeStructOfUnionReference(&buf);
-
- struct mojo_test_DummyStruct* dummy_struct =
- static_cast<struct mojo_test_DummyStruct*>(
- MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_DummyStruct)));
- *dummy_struct = mojo_test_DummyStruct{
- // header
- {
- sizeof(struct mojo_test_DummyStruct),
- 0u // version
- },
- 13, // int8
- {}, // padding
- };
- struct_with_union->u.size = 16u;
- struct_with_union->u.tag = mojo_test_UnionOfReferences_Tag_dummy_struct;
- struct_with_union->u.data.f_dummy_struct.ptr = dummy_struct;
-
- EXPECT_EQ(8u + 1 * 16u // 1 union type (set to DummyStruct)
- + 16u, // DummyStruct is out of line.
- mojo_test_StructOfUnionOfReferences_ComputeSerializedSize(
- struct_with_union));
-
- // We save the underlying (unencoded) buffer. We can compare the two after
- // deserialization to make sure deserialization is correct.
- char buffer_bytes_copy[sizeof(buffer_bytes)];
- memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy));
-
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, NULL);
-
- EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) -
- offsetof(struct mojo_test_StructOfUnionOfReferences, u.data),
- struct_with_union->u.data.f_dummy_struct.offset);
-
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, NULL, 0);
- EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used));
-
- {
- char buffer_bytes2[sizeof(buffer_bytes)] = {0};
- struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0};
- CopyAndCompare(
- &buf2, struct_with_union, buf.num_bytes_used,
- mojo_test_StructOfUnionOfReferences_DeepCopy,
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles,
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles);
- }
-}
-
-// Test when a union points to an array of int32
-TEST(UnionSerializationTest, UnionOfArray) {
- char buffer_bytes[1000];
- MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0};
- struct mojo_test_StructOfUnionOfReferences* struct_with_union =
- MakeStructOfUnionReference(&buf);
-
- struct MojomArrayHeader* array_of_int32 =
- MojomArray_New(&buf, 5, sizeof(int32_t));
- *MOJOM_ARRAY_INDEX(array_of_int32, int32_t, 0) = 13;
- *MOJOM_ARRAY_INDEX(array_of_int32, int32_t, 1) = 13;
- *MOJOM_ARRAY_INDEX(array_of_int32, int32_t, 2) = 13;
- *MOJOM_ARRAY_INDEX(array_of_int32, int32_t, 3) = 13;
- *MOJOM_ARRAY_INDEX(array_of_int32, int32_t, 4) = 13;
- struct_with_union->u.size = 16u;
- struct_with_union->u.tag = mojo_test_UnionOfReferences_Tag_int_array;
- struct_with_union->u.data.f_int_array.ptr = array_of_int32;
-
- EXPECT_EQ(8u + 1 * 16u // 1 union type (set to int array)
- + (8 + 4u * 5) // mojom array of 5 int32s
- + 4u, // padding for the mojom array
- mojo_test_StructOfUnionOfReferences_ComputeSerializedSize(
- struct_with_union));
-
- // We save the underlying (unencoded) buffer. We can compare the two after
- // deserialization to make sure deserialization is correct.
- char buffer_bytes_copy[sizeof(buffer_bytes)];
- memcpy(buffer_bytes_copy, buffer_bytes, sizeof(buffer_bytes_copy));
-
- struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u};
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, &handle_buf);
- EXPECT_EQ(0u, handle_buf.num_handles_used);
-
- EXPECT_EQ(sizeof(struct mojo_test_StructOfUnionOfReferences) -
- offsetof(struct mojo_test_StructOfUnionOfReferences, u.data),
- struct_with_union->u.data.f_int_array.offset);
-
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles(
- struct_with_union, buf.num_bytes_used, NULL, 0);
- EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used));
-
- {
- char buffer_bytes2[sizeof(buffer_bytes)] = {0};
- struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0};
- CopyAndCompare(
- &buf2, struct_with_union, buf.num_bytes_used,
- mojo_test_StructOfUnionOfReferences_DeepCopy,
- mojo_test_StructOfUnionOfReferences_EncodePointersAndHandles,
- mojo_test_StructOfUnionOfReferences_DecodePointersAndHandles);
- }
-}
-
-} // namespace
« no previous file with comments | « mojo/public/c/bindings/tests/testing_util.h ('k') | mojo/public/c/bindings/tests/validation_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698