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

Side by Side 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: fix tab indent, update sha1 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 unified diff | Download patch
« no previous file with comments | « mojo/public/c/bindings/tests/BUILD.gn ('k') | mojo/public/c/bindings/tests/struct_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 // TODO(vardhan): Needs a lot more testing. 5 // TODO(vardhan): Needs a lot more testing.
6 6
7 #include "mojo/public/c/bindings/array.h" 7 #include "mojo/public/c/bindings/array.h"
8 8
9 #include <stddef.h> 9 #include <stddef.h>
10 10
11 #include "mojo/public/c/bindings/struct.h" 11 #include "mojo/public/c/bindings/struct.h"
12 #include "mojo/public/c/bindings/tests/testing_util.h"
12 #include "mojo/public/cpp/system/macros.h" 13 #include "mojo/public/cpp/system/macros.h"
13 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h" 14 #include "mojo/public/interfaces/bindings/tests/test_structs.mojom-c.h"
14 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h" 15 #include "mojo/public/interfaces/bindings/tests/test_unions.mojom-c.h"
15 #include "testing/gtest/include/gtest/gtest.h" 16 #include "testing/gtest/include/gtest/gtest.h"
16 17
17 namespace { 18 namespace {
18 19
19 // Tests MojomArray_New(). 20 // Tests MojomArray_New().
20 TEST(ArrayTest, New) { 21 TEST(ArrayTest, New) {
21 char bytes_buffer[1000]; 22 char bytes_buffer[1000];
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
78 small_struct->nullable_pod_union_array.ptr = 79 small_struct->nullable_pod_union_array.ptr =
79 MojomArray_New(&buf, 2, sizeof(struct mojo_test_PodUnion)); 80 MojomArray_New(&buf, 2, sizeof(struct mojo_test_PodUnion));
80 81
81 // 0th element is NULL. 82 // 0th element is NULL.
82 MOJOM_ARRAY_INDEX(small_struct->nullable_pod_union_array.ptr, 83 MOJOM_ARRAY_INDEX(small_struct->nullable_pod_union_array.ptr,
83 struct mojo_test_PodUnion, 0) 84 struct mojo_test_PodUnion, 0)
84 ->size = 0; 85 ->size = 0;
85 // 1st element is not NULL. 86 // 1st element is not NULL.
86 struct mojo_test_PodUnion* e1 = MOJOM_ARRAY_INDEX( 87 struct mojo_test_PodUnion* e1 = MOJOM_ARRAY_INDEX(
87 small_struct->nullable_pod_union_array.ptr, struct mojo_test_PodUnion, 0); 88 small_struct->nullable_pod_union_array.ptr, struct mojo_test_PodUnion, 0);
88 e1->size = 16; 89 e1->size = sizeof(struct mojo_test_PodUnion);
89 e1->tag = mojo_test_PodUnion_Tag_f_int8; 90 e1->tag = mojo_test_PodUnion_Tag_f_int8;
90 e1->data.f_f_int8 = 13; 91 e1->data.f_f_int8 = 13;
91 92
92 EXPECT_EQ(8u + 6 * 8u // 6 references types 93 EXPECT_EQ(8u + 6 * 8u // 6 references types
93 + 1 * 16u // 1 union type 94 + 1 * 16u // 1 union type
94 + (8u + 16u * 2), // array of 2 unions 95 + (8u + 16u * 2), // array of 2 unions
95 mojo_test_SmallStruct_ComputeSerializedSize(small_struct)); 96 mojo_test_SmallStruct_ComputeSerializedSize(small_struct));
96 97
97 // Save the underlying buffer before encoding, so we can decode+compare 98 // Save the underlying buffer before encoding, so we can decode+compare
98 // later. 99 // later.
99 char bytes_buffer_copy[sizeof(bytes_buffer)]; 100 char bytes_buffer_copy[sizeof(bytes_buffer)];
100 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); 101 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer));
101 102
102 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u}; 103 mojo_test_SmallStruct_EncodePointersAndHandles(small_struct,
103 mojo_test_SmallStruct_EncodePointersAndHandles( 104 buf.num_bytes_used, NULL);
104 small_struct, buf.num_bytes_used, &handle_buf);
105 EXPECT_EQ(0u, handle_buf.num_handles_used);
106 105
107 // The null pointers should now be 0-offsets: 106 // The null pointers should now be 0-offsets:
108 EXPECT_EQ(0u, small_struct->dummy_struct.offset); 107 EXPECT_EQ(0u, small_struct->dummy_struct.offset);
109 EXPECT_EQ(0u, small_struct->pod_union_array.offset); 108 EXPECT_EQ(0u, small_struct->pod_union_array.offset);
110 EXPECT_EQ(0u, small_struct->s_array.offset); 109 EXPECT_EQ(0u, small_struct->s_array.offset);
111 EXPECT_EQ(0u, small_struct->pod_union_map.offset); 110 EXPECT_EQ(0u, small_struct->pod_union_map.offset);
112 EXPECT_EQ(0u, small_struct->nullable_pod_union_map.offset); 111 EXPECT_EQ(0u, small_struct->nullable_pod_union_map.offset);
113 112
114 // Test the pod_union_array offset: 113 // Test the pod_union_array offset:
115 EXPECT_EQ( 114 EXPECT_EQ(
116 sizeof(struct mojo_test_SmallStruct) - 115 sizeof(struct mojo_test_SmallStruct) -
117 offsetof(struct mojo_test_SmallStruct, nullable_pod_union_array), 116 offsetof(struct mojo_test_SmallStruct, nullable_pod_union_array),
118 small_struct->nullable_pod_union_array.offset); 117 small_struct->nullable_pod_union_array.offset);
119 118
120 mojo_test_SmallStruct_DecodePointersAndHandles(small_struct, 119 mojo_test_SmallStruct_DecodePointersAndHandles(small_struct,
121 buf.num_bytes_used, NULL, 0); 120 buf.num_bytes_used, NULL, 0);
122 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); 121 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used));
122
123 {
124 char bytes_buffer2[sizeof(bytes_buffer)] = {0};
125 struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0};
126 CopyAndCompare(&buf2, small_struct, buf.num_bytes_used,
127 mojo_test_SmallStruct_DeepCopy,
128 mojo_test_SmallStruct_EncodePointersAndHandles,
129 mojo_test_SmallStruct_DecodePointersAndHandles);
130 }
123 } 131 }
124 132
125 // Tests serialized size of an array of arrays. 133 // Tests serialized size of an array of arrays.
126 TEST(ArraySerializationTest, ArrayOfArrays) { 134 TEST(ArraySerializationTest, ArrayOfArrays) {
127 char bytes_buffer[1000] = {0}; 135 char bytes_buffer[1000] = {0};
128 MojomBuffer buf = {bytes_buffer, sizeof(bytes_buffer), 0}; 136 MojomBuffer buf = {bytes_buffer, sizeof(bytes_buffer), 0};
129 137
130 struct mojo_test_ArrayOfArrays* arr = 138 struct mojo_test_ArrayOfArrays* arr =
131 static_cast<struct mojo_test_ArrayOfArrays*>( 139 static_cast<struct mojo_test_ArrayOfArrays*>(
132 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_ArrayOfArrays))); 140 MojomBuffer_Allocate(&buf, sizeof(struct mojo_test_ArrayOfArrays)));
(...skipping 26 matching lines...) Expand all
159 EXPECT_EQ(24u + (8u + 2 * 8u) // a (with 2 arrays, 1 of them NULL) 167 EXPECT_EQ(24u + (8u + 2 * 8u) // a (with 2 arrays, 1 of them NULL)
160 + 0u // b (null altogether) 168 + 0u // b (null altogether)
161 + (8u + 8u), // first array<int> in a 169 + (8u + 8u), // first array<int> in a
162 mojo_test_ArrayOfArrays_ComputeSerializedSize(arr)); 170 mojo_test_ArrayOfArrays_ComputeSerializedSize(arr));
163 171
164 // Save the underlying buffer before encoding, so we can decode+compare 172 // Save the underlying buffer before encoding, so we can decode+compare
165 // later. 173 // later.
166 char bytes_buffer_copy[sizeof(bytes_buffer)]; 174 char bytes_buffer_copy[sizeof(bytes_buffer)];
167 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer)); 175 memcpy(bytes_buffer_copy, bytes_buffer, sizeof(bytes_buffer));
168 176
169 struct MojomHandleBuffer handle_buf = {NULL, 0u, 0u};
170 mojo_test_ArrayOfArrays_EncodePointersAndHandles(arr, buf.num_bytes_used, 177 mojo_test_ArrayOfArrays_EncodePointersAndHandles(arr, buf.num_bytes_used,
171 &handle_buf); 178 NULL);
172 EXPECT_EQ(0u, handle_buf.num_handles_used);
173 179
174 EXPECT_EQ(sizeof(struct mojo_test_ArrayOfArrays) - 180 EXPECT_EQ(sizeof(struct mojo_test_ArrayOfArrays) -
175 offsetof(struct mojo_test_ArrayOfArrays, a), 181 offsetof(struct mojo_test_ArrayOfArrays, a),
176 arr->a.offset); 182 arr->a.offset);
177 183
178 // Array of int32 should occur at the end of the array of 2 arrays -- so the 184 // Array of int32 should occur at the end of the array of 2 arrays -- so the
179 // offset is 2 pointer-sizes (1st one pointers to the array of int32, second 185 // offset is 2 pointer-sizes (1st one pointers to the array of int32, second
180 // one is NULL). 186 // one is NULL).
181 EXPECT_EQ(2 * sizeof(union MojomArrayHeaderPtr), 187 EXPECT_EQ(2 * sizeof(union MojomArrayHeaderPtr),
182 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 0)->offset); 188 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 0)->offset);
183 EXPECT_EQ(0u, 189 EXPECT_EQ(0u,
184 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 1)->offset); 190 MOJOM_ARRAY_INDEX(a_array, union MojomArrayHeaderPtr, 1)->offset);
185 191
186 mojo_test_ArrayOfArrays_DecodePointersAndHandles(arr, buf.num_bytes_used, 192 mojo_test_ArrayOfArrays_DecodePointersAndHandles(arr, buf.num_bytes_used,
187 NULL, 0); 193 NULL, 0);
188 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used)); 194 EXPECT_EQ(0, memcmp(buf.buf, bytes_buffer_copy, buf.num_bytes_used));
195
196 {
197 char bytes_buffer2[sizeof(bytes_buffer)] = {0};
198 struct MojomBuffer buf2 = {bytes_buffer2, sizeof(bytes_buffer2), 0};
199 CopyAndCompare(&buf2, arr, buf.num_bytes_used,
200 mojo_test_ArrayOfArrays_DeepCopy,
201 mojo_test_ArrayOfArrays_EncodePointersAndHandles,
202 mojo_test_ArrayOfArrays_DecodePointersAndHandles);
203 }
189 } 204 }
190 205
191 // Tests serialization of an array of handles. 206 // Tests serialization of an array of handles.
192 TEST(ArraySerializationTest, ArrayOfHandles) { 207 TEST(ArraySerializationTest, ArrayOfHandles) {
193 char buffer_bytes[1000] = {0}; 208 char buffer_bytes[1000] = {0};
194 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0}; 209 MojomBuffer buf = {buffer_bytes, sizeof(buffer_bytes), 0};
195 210
196 struct mojo_test_StructWithNullableHandles* handle_struct = 211 struct mojo_test_StructWithNullableHandles* handle_struct =
197 static_cast<struct mojo_test_StructWithNullableHandles*>( 212 static_cast<struct mojo_test_StructWithNullableHandles*>(
198 MojomBuffer_Allocate( 213 MojomBuffer_Allocate(
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 handle_struct->array_h.offset); 258 handle_struct->array_h.offset);
244 259
245 mojo_test_StructWithNullableHandles_DecodePointersAndHandles( 260 mojo_test_StructWithNullableHandles_DecodePointersAndHandles(
246 handle_struct, buf.num_bytes_used, handles, MOJO_ARRAYSIZE(handles)); 261 handle_struct, buf.num_bytes_used, handles, MOJO_ARRAYSIZE(handles));
247 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used)); 262 EXPECT_EQ(0, memcmp(buf.buf, buffer_bytes_copy, buf.num_bytes_used));
248 263
249 // Check that the handles in the handles array are invalidated: 264 // Check that the handles in the handles array are invalidated:
250 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]); 265 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[0]);
251 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[1]); 266 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[1]);
252 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[2]); 267 EXPECT_EQ(MOJO_HANDLE_INVALID, handles[2]);
268
269 {
270 char buffer_bytes2[sizeof(buffer_bytes)] = {0};
271 struct MojomBuffer buf2 = {buffer_bytes2, sizeof(buffer_bytes2), 0};
272 auto* copied_struct =
273 mojo_test_StructWithNullableHandles_DeepCopy(&buf2, handle_struct);
274
275 // The copy should still have the handles.
276 EXPECT_EQ(static_cast<MojoHandle>(10u), handle_struct->h);
277 EXPECT_EQ(static_cast<MojoHandle>(20u),
278 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 0));
279 EXPECT_EQ(MOJO_HANDLE_INVALID,
280 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 1));
281 EXPECT_EQ(static_cast<MojoHandle>(30u),
282 *MOJOM_ARRAY_INDEX(handle_struct->array_h.ptr, MojoHandle, 2));
283 // The new struct should have the handles now:
284 EXPECT_EQ(static_cast<MojoHandle>(10u), copied_struct->h);
285 EXPECT_EQ(static_cast<MojoHandle>(20u),
286 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 0));
287 EXPECT_EQ(MOJO_HANDLE_INVALID,
288 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 1));
289 EXPECT_EQ(static_cast<MojoHandle>(30u),
290 *MOJOM_ARRAY_INDEX(copied_struct->array_h.ptr, MojoHandle, 2));
291 }
253 } 292 }
254 293
255 } // namespace 294 } // namespace
OLDNEW
« no previous file with comments | « mojo/public/c/bindings/tests/BUILD.gn ('k') | mojo/public/c/bindings/tests/struct_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698