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

Unified Diff: mojo/public/c/bindings/tests/testing_util.h

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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « mojo/public/c/bindings/tests/struct_unittest.cc ('k') | mojo/public/c/bindings/tests/union_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/testing_util.h
diff --git a/mojo/public/c/bindings/tests/testing_util.h b/mojo/public/c/bindings/tests/testing_util.h
new file mode 100644
index 0000000000000000000000000000000000000000..739175dcde3a08f08fa2814ba6800ac36cda84f9
--- /dev/null
+++ b/mojo/public/c/bindings/tests/testing_util.h
@@ -0,0 +1,47 @@
+// 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.
+
+#ifndef MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
+#define MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
+
+#include <stddef.h>
+#include <stdint.h>
+#include <string.h>
+
+#include "mojo/public/c/bindings/buffer.h"
+#include "mojo/public/c/system/handle.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+// This will copy the supplied |in_struct| and compare it against the new
+// copy, expecting them to be the same. It compares the encoded version to be
+// sure that they are the same, since a unencoded version will have pointers
+// pointing to separate objects in the two copies, whereas encoded versions will
+// only have relative offsets for pointers. The new copy will remain encoded,
+// and the original will be decoded.
+// This function won't work for structs with handles, and will crash.
+template <typename T>
+void CopyAndCompare(
+ MojomBuffer* buf,
+ T* in_struct,
+ size_t in_struct_size,
+ T* (*copy_fn)(struct MojomBuffer* in_buffer, T* in_struct),
+ void (*encode_fn)(T* inout_struct,
+ uint32_t size,
+ struct MojomHandleBuffer* inout_handle_buffer),
+ void (*decode_fn)(T* inout_struct,
+ uint32_t size,
+ MojoHandle* handles,
+ uint32_t num_handles)) {
+ T* out_struct = copy_fn(buf, in_struct);
+ ASSERT_TRUE(out_struct);
+ EXPECT_EQ(in_struct_size, buf->num_bytes_used);
+
+ encode_fn(in_struct, in_struct_size, NULL);
+ encode_fn(out_struct, buf->num_bytes_used, NULL);
+ EXPECT_EQ(0, memcmp(in_struct, out_struct, buf->num_bytes_used));
+
+ decode_fn(in_struct, in_struct_size, NULL, 0);
+}
+
+#endif // MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
« no previous file with comments | « mojo/public/c/bindings/tests/struct_unittest.cc ('k') | mojo/public/c/bindings/tests/union_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698