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

Side by Side Diff: mojo/public/c/bindings/tests/testing_util.h

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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #ifndef MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
6 #define MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
7
8 #include <mojo/bindings/buffer.h>
9 #include <mojo/system/handle.h>
10 #include <stddef.h>
11 #include <stdint.h>
12 #include <string.h>
13
14 #include "testing/gtest/include/gtest/gtest.h"
15
16 // This will copy the supplied |in_struct| and compare it against the new
17 // copy, expecting them to be the same. It compares the encoded version to be
18 // sure that they are the same, since a unencoded version will have pointers
19 // pointing to separate objects in the two copies, whereas encoded versions will
20 // only have relative offsets for pointers. The new copy will remain encoded,
21 // and the original will be decoded.
22 // This function won't work for structs with handles, and will crash.
23 template <typename T>
24 void CopyAndCompare(
25 MojomBuffer* buf,
26 T* in_struct,
27 size_t in_struct_size,
28 T* (*copy_fn)(struct MojomBuffer* in_buffer, T* in_struct),
29 void (*encode_fn)(T* inout_struct,
30 uint32_t size,
31 struct MojomHandleBuffer* inout_handle_buffer),
32 void (*decode_fn)(T* inout_struct,
33 uint32_t size,
34 MojoHandle* handles,
35 uint32_t num_handles)) {
36 T* out_struct = copy_fn(buf, in_struct);
37 ASSERT_TRUE(out_struct);
38 EXPECT_EQ(in_struct_size, buf->num_bytes_used);
39
40 encode_fn(in_struct, in_struct_size, NULL);
41 encode_fn(out_struct, buf->num_bytes_used, NULL);
42 EXPECT_EQ(0, memcmp(in_struct, out_struct, buf->num_bytes_used));
43
44 decode_fn(in_struct, in_struct_size, NULL, 0);
45 }
46
47 #endif // MOJO_PUBLIC_C_BINDINGS_TESTS_TESTING_UTIL_H_
OLDNEW
« 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