blob: 9fc635797d14b18bf3e72f10e9e79e441d42a8e3 [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);
17
18 int write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
19 g_assert(!error);
20 g_assert(write_len > 0);
21
22 int read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
23 g_assert(!error);
24 g_assert_cmpint(write_len, ==, read_len);
25
26 g_object_unref(dst);
27 g_object_unref(protocol);
28 g_object_unref(transport);
29}
30
31static void struct_constants_read_write() {
32 GError* error = NULL;
33 ThriftTransport* transport
34 = THRIFT_TRANSPORT(g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 4096, NULL));
35 ThriftProtocol* protocol
36 = THRIFT_PROTOCOL(g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL));
37 TTestCompactProtoTestStruct* src = T_TEST_COMPACT_TEST;
38 TTestCompactProtoTestStruct* dst = g_object_new(T_TEST_TYPE_COMPACT_PROTO_TEST_STRUCT, NULL);
39 TTestCompactProtoTestStructClass* cls = T_TEST_COMPACT_PROTO_TEST_STRUCT_GET_CLASS(src);
40
41 int write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
42 g_assert(!error);
43 g_assert(write_len > 0);
44
45 int read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
46 g_assert(!error);
47 g_assert_cmpint(write_len, ==, read_len);
48
49 g_object_unref(dst);
50 g_object_unref(protocol);
51 g_object_unref(transport);
52}
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090053
54static void struct_read_write_length_should_equal() {
55 GError* error = NULL;
56 ThriftTransport* transport
57 = THRIFT_TRANSPORT(g_object_new(THRIFT_TYPE_MEMORY_BUFFER, "buf_size", 2048, NULL));
58 ThriftProtocol* protocol
59 = THRIFT_PROTOCOL(g_object_new(THRIFT_TYPE_BINARY_PROTOCOL, "transport", transport, NULL));
60 TTestBonk* src = g_object_new(T_TEST_TYPE_BONK, NULL);
61 TTestBonk* dst = g_object_new(T_TEST_TYPE_BONK, NULL);
62 TTestBonkClass* cls = T_TEST_BONK_GET_CLASS(src);
63
64 int write_len = THRIFT_STRUCT_CLASS(cls)->write(THRIFT_STRUCT(src), protocol, &error);
65 g_assert(!error);
66 g_assert(write_len > 0);
67
68 int read_len = THRIFT_STRUCT_CLASS(cls)->read(THRIFT_STRUCT(dst), protocol, &error);
69 g_assert(!error);
70 g_assert_cmpint(write_len, ==, read_len);
71
72 g_object_unref(dst);
73 g_object_unref(src);
74 g_object_unref(protocol);
75 g_object_unref(transport);
76}
77
78int main(int argc, char* argv[]) {
79#if (!GLIB_CHECK_VERSION(2, 36, 0))
80 g_type_init();
81#endif
82 g_test_init(&argc, &argv, NULL);
83
84 g_test_add_func("/testserialization/StructReadWriteLengthShouldEqual",
85 struct_read_write_length_should_equal);
Nobuaki Sukegawaba3fe862015-12-01 22:42:55 +090086 g_test_add_func("/testserialization/StructConstants", struct_constants_read_write);
87 g_test_add_func("/testserialization/EnumConstants", enum_constants_read_write);
Nobuaki Sukegawa362a5ed2015-12-01 22:17:24 +090088 return g_test_run();
89}