blob: 7165d931d26bb77a18e60c61570497e249f94833 [file] [log] [blame]
Roger Meierc1010922010-11-26 10:17:48 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Roger Meier213a6642010-10-27 12:30:11 +000020#include <assert.h>
21#include <glib.h>
22
23#include "thrift_struct.h"
24#include "protocol/thrift_protocol.h"
25#include "protocol/thrift_binary_protocol.h"
26#include "transport/thrift_memory_buffer.h"
27#include "gen-c_glib/t_test_optional_required_test_types.h"
28
29#include "gen-c_glib/t_test_optional_required_test_types.c"
30
31static void
32write_to_read (ThriftStruct *w, ThriftStruct *r, GError **write_error,
33 GError **read_error)
34{
35 ThriftMemoryBuffer *tbuffer = NULL;
36 ThriftProtocol *protocol = NULL;
37
38 tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL);
39 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport",
40 tbuffer, NULL);
41
42 thrift_struct_write (w, protocol, write_error);
43 thrift_struct_read (r, protocol, read_error);
44
45 g_object_unref (protocol);
46 g_object_unref (tbuffer);
47}
48
49static void
50test_old_school1 (void)
51{
52 TTestOldSchool *o = NULL;
53
54 o = g_object_new (T_TEST_TYPE_OLD_SCHOOL, NULL);
55 o->im_int = 10;
56 o->im_str = g_strdup ("test");
57 o->im_big = g_ptr_array_new ();
58 g_ptr_array_free (o->im_big, FALSE);
59 g_free (o->im_str);
60 g_object_unref (o);
61}
62
63/**
64 * Write to read with optional fields
65 */
66static void
67test_simple (void)
68{
69 TTestSimple *s1 = NULL, *s2 = NULL, *s3 = NULL;
70
71 s1 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
72 s2 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
73 s3 = g_object_new (T_TEST_TYPE_SIMPLE, NULL);
74
75 // write-to-read with optional fields
76 s1->im_optional = 10;
77 assert (s1->__isset_im_default == FALSE);
78 assert (s1->__isset_im_optional == FALSE);
79 write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL);
80 assert (s2->__isset_im_default = TRUE);
81 assert (s2->__isset_im_optional == FALSE);
82 assert (s2->im_optional == 0);
83
84 s1->__isset_im_optional = TRUE;
85 write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL);
86 assert (s3->__isset_im_default == TRUE);
87 assert (s3->__isset_im_optional == TRUE);
88 assert (s3->im_optional == 10);
89
90 g_object_unref (s1);
91 g_object_unref (s2);
92}
93
94/**
95 * Writing between optional and default
96 */
97static void
98test_tricky1 (void)
99{
100 TTestTricky1 *t1 = NULL;
101 TTestTricky2 *t2 = NULL;
102
103 t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
104 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
105
106 t2->im_optional = 10;
107 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL);
108 write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL);
109
110 assert (t1->__isset_im_default == FALSE);
111 assert (t2->__isset_im_optional == TRUE);
112 assert (t1->im_default == t2->im_optional);
113 assert (t1->im_default == 0);
114
115 g_object_unref (t1);
116 g_object_unref (t2);
117}
118
119/**
120 * Writing between default and required.
121 */
122static void
123test_tricky2 (void)
124{
125 TTestTricky1 *t1 = NULL;
126 TTestTricky3 *t3 = NULL;
127
128 t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL);
129 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
130
131 write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL);
132 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL);
133
134 assert (t1->__isset_im_default == TRUE);
135
136 g_object_unref (t1);
137 g_object_unref (t3);
138}
139
140/**
141 * Writing between optional and required.
142 */
143static void
144test_tricky3 (void)
145{
146 TTestTricky2 *t2 = NULL;
147 TTestTricky3 *t3 = NULL;
148
149 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
150 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
151
152 t2->__isset_im_optional = TRUE;
153
154 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, NULL);
155 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
156
157 g_object_unref (t2);
158 g_object_unref (t3);
159}
160
161/**
162 * Catch an optional not set exception. To quote the
163 * C++ test, "Mu-hu-ha-ha-ha!"
164 */
165static void
166test_tricky4 (void)
167{
168 TTestTricky2 *t2 = NULL;
169 TTestTricky3 *t3 = NULL;
170 GError *read_error = NULL;
171
172 t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL);
173 t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL);
174
175 // throws protocol exception
176 write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error);
177 assert (read_error != NULL);
178 g_error_free (read_error);
179
180 write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL);
181
182 assert (t2->__isset_im_optional);
183
184 g_object_unref (t2);
185 g_object_unref (t3);
186}
187
188int
Roger Meierc1010922010-11-26 10:17:48 +0000189main(int argc, char *argv[])
Roger Meier213a6642010-10-27 12:30:11 +0000190{
Roger Meierc1010922010-11-26 10:17:48 +0000191 g_type_init();
192 g_test_init (&argc, &argv, NULL);
193
194 g_test_add_func ("/testoptionalrequired/OldSchool", test_old_school1);
195 g_test_add_func ("/testoptionalrequired/Simple", test_simple);
196 g_test_add_func ("/testoptionalrequired/Tricky1", test_tricky1);
197 g_test_add_func ("/testoptionalrequired/Tricky2", test_tricky2);
198 g_test_add_func ("/testoptionalrequired/Tricky3", test_tricky3);
199 g_test_add_func ("/testoptionalrequired/Tricky4", test_tricky4);
200
201 return g_test_run ();
Roger Meier213a6642010-10-27 12:30:11 +0000202}