blob: 67d411de10e63caa00873dac11c95a28d0cae199 [file] [log] [blame]
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +09001#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 Sukegawaba3fe862015-12-01 22:42:55 +09006#include "gen-c_glib/t_test_enum_test_types.h"
7
8static 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 Southbf8f7b42015-12-23 20:29:29 -050017 int write_len;
18 int read_len;
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090019
Simon Southbf8f7b42015-12-23 20:29:29 -050020 write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090021 g_assert(!error);
22 g_assert(write_len > 0);
23
Simon Southbf8f7b42015-12-23 20:29:29 -050024 read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090025 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
33static 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 Southbf8f7b42015-12-23 20:29:29 -050042 int write_len;
43 int read_len;
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090044
Simon Southbf8f7b42015-12-23 20:29:29 -050045 write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090046 g_assert(!error);
47 g_assert(write_len > 0);
48
Simon Southbf8f7b42015-12-23 20:29:29 -050049 read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090050 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 Sukegawa362a5ed2015-12-01 22:17:24 +090057
58static 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 Southbf8f7b42015-12-23 20:29:29 -050067 int write_len;
68 int read_len;
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090069
Simon Southbf8f7b42015-12-23 20:29:29 -050070 write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090071 g_assert(!error);
72 g_assert(write_len > 0);
73
Simon Southbf8f7b42015-12-23 20:29:29 -050074 read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090075 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
84int 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 Sukegawaba3fe862015-12-01 22:42:55 +090092 g_test_add_func("/testserialization/StructConstants", struct_constants_read_write);
93 g_test_add_func("/testserialization/EnumConstants", enum_constants_read_write);
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090094 return g_test_run();
95}