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

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

Issue 2200843002: C bindings: Implement _DeepCopy() & some unittests. (Closed) Base URL: git@github.com:domokit/mojo.git@cgen_validate
Patch Set: Address comments: no longer move, but copy instead. Return NULL if insufficient space. 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
Index: mojo/public/c/bindings/tests/array_unittest.cc
diff --git a/mojo/public/c/bindings/tests/array_unittest.cc b/mojo/public/c/bindings/tests/array_unittest.cc
index a5d00bf55cc974ea4087455a44d3acbf9f5cd66f..54a238934d3f82b4c623f82bd97de3dbfd5b50b5 100644
--- a/mojo/public/c/bindings/tests/array_unittest.cc
+++ b/mojo/public/c/bindings/tests/array_unittest.cc
@@ -9,6 +9,7 @@
#include <stddef.h>
#include "mojo/public/c/bindings/struct.h"
+#include "mojo/public/c/bindings/tests/testing_util.h"
#include "mojo/public/cpp/system/macros.h"
#include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h"
#include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h"
@@ -99,10 +100,8 @@ TEST(ArraySerializationTest, ArrayOfUnions) {
char bytes_buffer_copy[sizeof(bytes_buffer)];
memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer));
- struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u};
- mojo_test_SmallStruct_EncodePointersAndHandles(
- small_struct, buf.num_bytes_used, &handle_buf);
- EXPECT_EQ(0u, handle_buf.num_handles_used);
+ mojo_test_SmallStruct_EncodePointersAndHandles(small_struct,
+ buf.num_bytes_used, NULL);
// The null pointers should now be 0-offsets:
EXPECT_EQ(0u, small_struct->dummy_struct.offset);
@@ -120,6 +119,15 @@ TEST(ArraySerializationTest, ArrayOfUnions) {
mojo_test_SmallStruct_DecodePointersAndHandles(small_struct,
buf.num_bytes_used, NULL, 0);
EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used));
+
+ {
+ char bytes_buffer2[sizeof(bytes_buffer)] = {0};
+ struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0};
+ CopyAndCompare(&buf2, small_struct, buf.num_bytes_used,
+ mojo_test_SmallStruct_DeepCopy,
+ mojo_test_SmallStruct_EncodePointersAndHandles,
+ mojo_test_SmallStruct_DecodePointersAndHandles);
+ }
}
// Tests serialized size of an array of arrays.
@@ -166,10 +174,8 @@ TEST(ArraySerializationTest, ArrayOfArrays) {
char bytes_buffer_copy[sizeof(bytes_buffer)];
memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer));
- struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u};
mojo_test_ArrayOfArrays_EncodePointersAndHandles(arr, buf.num_bytes_used,
- &handle_buf);
- EXPECT_EQ(0u, handle_buf.num_handles_used);
+ NULL);
EXPECT_EQ(sizeof(struct mojo_test_ArrayOfArrays) -
offsetof(struct mojo_test_ArrayOfArrays, a),
@@ -186,6 +192,15 @@ TEST(ArraySerializationTest, ArrayOfArrays) {
mojo_test_ArrayOfArrays_DecodePointersAndHandles(arr, buf.num_bytes_used,
NULL, 0);
EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used));
+
+ {
+ char bytes_buffer2[sizeof(bytes_buffer)] = {0};
+ struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0};
+ CopyAndCompare(&buf2, arr, buf.num_bytes_used,
+ mojo_test_ArrayOfArrays_DeepCopy,
+ mojo_test_ArrayOfArrays_EncodePointersAndHandles,
+ mojo_test_ArrayOfArrays_DecodePointersAndHandles);
+ }
}
// Tests serialization of an array of handles.
@@ -247,6 +262,30 @@ TEST(ArraySerializationTest, ArrayOfHandles) {
EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]);
EXPECT_EQ(MOJO_HANDLE_INVALID, handles[1]);
EXPECT_EQ(MOJO_HANDLE_INVALID, handles[2]);
+
+ {
+ char buffer_bytes2[sizeof(buffer_bytes)] = {0};
+ struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0};
+ auto* copied_struct =
+ mojo_test_StructWithNullableHandles_DeepCopy(&buf2, handle_struct);
+
+ // The copy should still have the handles.
+ EXPECT_EQ(static_cast<MojoHandle>(10u), handle_struct->h);
+ EXPECT_EQ(static_cast<MojoHandle>(20u),
+ *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 0));
+ EXPECT_EQ(MOJO_HANDLE_INVALID,
+ *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 1));
+ EXPECT_EQ(static_cast<MojoHandle>(30u),
+ *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 2));
+ // The new struct should have the handles now:
+ EXPECT_EQ(static_cast<MojoHandle>(10u), copied_struct->h);
+ EXPECT_EQ(static_cast<MojoHandle>(20u),
+ *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 0));
+ EXPECT_EQ(MOJO_HANDLE_INVALID,
+ *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 1));
+ EXPECT_EQ(static_cast<MojoHandle>(30u),
+ *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 2));
+ }
}
} // namespace

Powered by Google App Engine
This is Rietveld 408576698