Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 1 | #include <thrift/c_glib/protocol/thrift_binary_protocol.h> |
| 2 | #include <thrift/c_glib/protocol/thrift_protocol.h> |
| 3 | #include <thrift/c_glib/transport/thrift_memory_buffer.h> |
| 4 | #include <thrift/c_glib/transport/thrift_transport.h> |
| 5 | #include "gen-c_glib/t_test_debug_proto_test_types.h" |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 6 | #include "gen-c_glib/t_test_enum_test_types.h" |
| 7 | |
| 8 | static void enum_constants_read_write() { |
| 9 | GError* error = NULL; |
| 10 | ThriftTransport* transport |
| 11 | = THRIFT_TRANSPORT(g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 1024, NULL)); |
| 12 | ThriftProtocol* protocol |
| 13 | = THRIFT_PROTOCOL(g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL)); |
| 14 | TTestEnumTestStruct* src = T_TEST_ENUM_TEST; |
| 15 | TTestEnumTestStruct* dst = g_object_new(T_TEST_TYPE_ENUM_TEST_STRUCT, NULL); |
| 16 | TTestEnumTestStructClass* cls = T_TEST_ENUM_TEST_STRUCT_GET_CLASS(src); |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 17 | int write_len; |
| 18 | int read_len; |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 19 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 20 | write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error); |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 21 | g_assert(!error); |
| 22 | g_assert(write_len > 0); |
| 23 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 24 | read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error); |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 25 | g_assert(!error); |
| 26 | g_assert_cmpint(write_len, ==, read_len); |
| 27 | |
| 28 | g_object_unref(dst); |
| 29 | g_object_unref(protocol); |
| 30 | g_object_unref(transport); |
| 31 | } |
| 32 | |
| 33 | static void struct_constants_read_write() { |
| 34 | GError* error = NULL; |
| 35 | ThriftTransport* transport |
| 36 | = THRIFT_TRANSPORT(g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 4096, NULL)); |
| 37 | ThriftProtocol* protocol |
| 38 | = THRIFT_PROTOCOL(g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL)); |
| 39 | TTestCompactProtoTestStruct* src = T_TEST_COMPACT_TEST; |
| 40 | TTestCompactProtoTestStruct* dst = g_object_new(T_TEST_TYPE_COMPACT_PROTO_TEST_STRUCT, NULL); |
| 41 | TTestCompactProtoTestStructClass* cls = T_TEST_COMPACT_PROTO_TEST_STRUCT_GET_CLASS(src); |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 42 | int write_len; |
| 43 | int read_len; |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 44 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 45 | write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error); |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 46 | g_assert(!error); |
| 47 | g_assert(write_len > 0); |
| 48 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 49 | read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error); |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 50 | g_assert(!error); |
| 51 | g_assert_cmpint(write_len, ==, read_len); |
| 52 | |
| 53 | g_object_unref(dst); |
| 54 | g_object_unref(protocol); |
| 55 | g_object_unref(transport); |
| 56 | } |
Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 57 | |
| 58 | static void struct_read_write_length_should_equal() { |
| 59 | GError* error = NULL; |
| 60 | ThriftTransport* transport |
| 61 | = THRIFT_TRANSPORT(g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 2048, NULL)); |
| 62 | ThriftProtocol* protocol |
| 63 | = THRIFT_PROTOCOL(g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL)); |
| 64 | TTestBonk* src = g_object_new(T_TEST_TYPE_BONK, NULL); |
| 65 | TTestBonk* dst = g_object_new(T_TEST_TYPE_BONK, NULL); |
| 66 | TTestBonkClass* cls = T_TEST_BONK_GET_CLASS(src); |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 67 | int write_len; |
| 68 | int read_len; |
Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 69 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 70 | write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error); |
Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 71 | g_assert(!error); |
| 72 | g_assert(write_len > 0); |
| 73 | |
Simon South | bf8f7b4 | 2015-12-23 20:29:29 -0500 | [diff] [blame] | 74 | read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error); |
Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 75 | g_assert(!error); |
| 76 | g_assert_cmpint(write_len, ==, read_len); |
| 77 | |
| 78 | g_object_unref(dst); |
| 79 | g_object_unref(src); |
| 80 | g_object_unref(protocol); |
| 81 | g_object_unref(transport); |
| 82 | } |
| 83 | |
| 84 | int main(int argc, char* argv[]) { |
| 85 | #if (!GLIB_CHECK_VERSION(2, 36, 0)) |
| 86 | g_type_init(); |
| 87 | #endif |
| 88 | g_test_init(&argc, &argv, NULL); |
| 89 | |
| 90 | g_test_add_func("/testserialization/StructReadWriteLengthShouldEqual", |
| 91 | struct_read_write_length_should_equal); |
Nobuaki Sukegawa | ba3fe86 | 2015-12-01 22:42:55 +0900 | [diff] [blame] | 92 | g_test_add_func("/testserialization/StructConstants", struct_constants_read_write); |
| 93 | g_test_add_func("/testserialization/EnumConstants", enum_constants_read_write); |
Nobuaki Sukegawa | 362a5ed | 2015-12-01 22:17:24 +0900 | [diff] [blame] | 94 | return g_test_run(); |
| 95 | } |