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"
6 #include "gen-c_glib/t_test_enum_test_types.h"
7 
enum_constants_read_write()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);
17   int write_len;
18   int read_len;
19 
20   write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
21   g_assert(!error);
22   g_assert(write_len > 0);
23 
24   read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
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 
struct_constants_read_write()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);
42   int write_len;
43   int read_len;
44 
45   write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
46   g_assert(!error);
47   g_assert(write_len > 0);
48 
49   read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
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 }
57 
struct_read_write_length_should_equal()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);
67   int write_len;
68   int read_len;
69 
70   write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
71   g_assert(!error);
72   g_assert(write_len > 0);
73 
74   read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
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 
main(int argc,char * argv[])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);
92   g_test_add_func("/testserialization/StructConstants", struct_constants_read_write);
93   g_test_add_func("/testserialization/EnumConstants", enum_constants_read_write);
94   return g_test_run();
95 }
96