blob: 47ac588fafdb73479cb4ed9cd5437878a3d7545d [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 Meier1d7e35a2014-07-22 21:56:12 +020020#include <assert.h>
Roger Meier213a6642010-10-27 12:30:11 +000021#include <math.h>
Roger Meier60b7ad62014-07-29 23:23:36 +020022#include <string.h>
Roger Meier213a6642010-10-27 12:30:11 +000023#include <glib-object.h>
24
25#ifndef M_PI
26#define M_PI 3.1415926535897932385
27#endif
28
Roger Meier1d7e35a2014-07-22 21:56:12 +020029#include <thrift/c_glib/protocol/thrift_protocol.h>
30#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
31
Roger Meier213a6642010-10-27 12:30:11 +000032#include "gen-c_glib/t_test_debug_proto_test_types.h"
Roger Meier1d7e35a2014-07-22 21:56:12 +020033#include "gen-c_glib/t_test_srv.h"
34#include "gen-c_glib/t_test_inherited.h"
Roger Meier213a6642010-10-27 12:30:11 +000035
Roger Meierc1010922010-11-26 10:17:48 +000036static void
Roger Meier60b7ad62014-07-29 23:23:36 +020037test_structs_doubles_create_and_destroy (void)
Roger Meier213a6642010-10-27 12:30:11 +000038{
Roger Meier60b7ad62014-07-29 23:23:36 +020039 GObject *object = NULL;
Roger Meier213a6642010-10-27 12:30:11 +000040
Roger Meier60b7ad62014-07-29 23:23:36 +020041 /* A Doubles structure can be created... */
42 object = g_object_new (T_TEST_TYPE_DOUBLES, NULL);
Roger Meier213a6642010-10-27 12:30:11 +000043
Roger Meier60b7ad62014-07-29 23:23:36 +020044 g_assert (object != NULL);
45 g_assert (T_TEST_IS_DOUBLES (object));
Roger Meierc75797d2012-04-28 11:33:58 +000046
Roger Meier60b7ad62014-07-29 23:23:36 +020047 /* ...and destroyed */
48 g_object_unref (object);
Roger Meier213a6642010-10-27 12:30:11 +000049}
50
Roger Meier1d7e35a2014-07-22 21:56:12 +020051static void
Roger Meier60b7ad62014-07-29 23:23:36 +020052test_structs_doubles_initialize (void)
53{
54 TTestDoubles *doubles = NULL;
55 gdouble nan;
56 gdouble inf;
57 gdouble neginf;
58 gdouble repeating;
59 gdouble big;
60 gdouble tiny;
61 gdouble zero;
62 gdouble negzero;
63
64 /* Note there seems to be no way to get not-a-number ("NAN") values past
65 GObject's range-checking, so that portion of the test has been commented
66 out below. */
67
68 /* A Doubles structure's members are available as GObject properties
69 that can be initialized at construction... */
70 doubles = g_object_new (T_TEST_TYPE_DOUBLES,
71 /* "nan", 0 * INFINITY, */
72 "inf", INFINITY,
73 "neginf", -INFINITY,
74 "repeating", 1.0 / 3,
75 "big", G_MAXDOUBLE,
76 "tiny", 10E-101,
77 "zero", 1.0 * 0,
78 "negzero", -1.0 * 0,
79 NULL);
80
81 g_assert (doubles != NULL);
82
83 /* ...and later retrieved */
84 g_object_get (doubles,
85 "nan", &nan,
86 "inf", &inf,
87 "neginf", &neginf,
88 "repeating", &repeating,
89 "big", &big,
90 "tiny", &tiny,
91 "zero", &zero,
92 "negzero", &negzero,
93 NULL);
94
95 /* g_assert_cmpint (isnan (nan), !=, 0); */
96 g_assert_cmpint (isinf (inf), ==, 1);
97 g_assert_cmpint (isinf (neginf), ==, -1);
98
99 g_assert_cmpfloat (repeating, ==, 1.0 / 3);
100 g_assert_cmpfloat (big, ==, G_MAXDOUBLE);
101 g_assert_cmpfloat (tiny, ==, 10E-101);
102 g_assert_cmpfloat (zero, ==, 1.0 * 0);
103 g_assert_cmpfloat (negzero, ==, -1.0 * 0);
104
105 g_object_unref (doubles);
106}
107
108static void
109test_structs_one_of_each_create_and_destroy (void)
110{
111 GObject *object = NULL;
112
113 /* A OneOfEach structure can be created... */
114 object = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
115
116 g_assert (object != NULL);
117 g_assert (T_TEST_IS_ONE_OF_EACH (object));
118
119 /* ...and destroyed */
120 g_object_unref (object);
121}
122
123static void
124test_structs_one_of_each_initialize_default_values (void)
125{
126 TTestOneOfEach *one_of_each = NULL;
127 gint a_bite;
128 gint integer16;
129 gint64 integer64;
130 GArray *byte_list;
131 GArray *i16_list;
132 GArray *i64_list;
133
134 /* A OneOfEach structure created with no explicit property values
135 will hold the default values specified in the .thrift file */
136 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
137
138 g_object_get (one_of_each,
139 "a_bite", &a_bite,
140 "integer16", &integer16,
141 "integer64", &integer64,
142 "byte_list", &byte_list,
143 "i16_list", &i16_list,
144 "i64_list", &i64_list,
145 NULL);
146
147 g_assert_cmpint (a_bite, ==, 0x7f);
148 g_assert_cmpint (integer16, ==, 0x7fff);
Simon Southbf8f7b42015-12-23 20:29:29 -0500149 g_assert_cmpint (integer64, ==, G_GINT64_CONSTANT (10000000000));
Roger Meier60b7ad62014-07-29 23:23:36 +0200150
151 g_assert (byte_list != NULL);
152 g_assert_cmpint (byte_list->len, ==, 3);
153 g_assert_cmpint (g_array_index (byte_list, gint8, 0), ==, 1);
154 g_assert_cmpint (g_array_index (byte_list, gint8, 1), ==, 2);
155 g_assert_cmpint (g_array_index (byte_list, gint8, 2), ==, 3);
156
157 g_assert (i16_list != NULL);
158 g_assert_cmpint (i16_list->len, ==, 3);
159 g_assert_cmpint (g_array_index (i16_list, gint16, 0), ==, 1);
160 g_assert_cmpint (g_array_index (i16_list, gint16, 1), ==, 2);
161 g_assert_cmpint (g_array_index (i16_list, gint16, 2), ==, 3);
162
163 g_assert (i64_list != NULL);
164 g_assert_cmpint (i64_list->len, ==, 3);
165 g_assert_cmpint (g_array_index (i64_list, gint64, 0), ==, 1);
166 g_assert_cmpint (g_array_index (i64_list, gint64, 1), ==, 2);
167 g_assert_cmpint (g_array_index (i64_list, gint64, 2), ==, 3);
168
169 g_array_unref (i64_list);
170 g_array_unref (i16_list);
171 g_array_unref (byte_list);
172 g_object_unref (one_of_each);
173}
174
175static void
176test_structs_one_of_each_initialize_specified_values (void)
177{
178 static const gint8 initial_byte_list[5] = { 13, 21, 34, 55, 89 };
179 static const gint16 initial_i16_list[5] = { 4181, 6765, 10946, 17711, 28657 };
180 static const gint64 initial_i64_list[5] =
181 {
Simon Southbf8f7b42015-12-23 20:29:29 -0500182 G_GINT64_CONSTANT (1100087778366101931),
183 G_GINT64_CONSTANT (1779979416004714189),
184 G_GINT64_CONSTANT (2880067194370816120),
185 G_GINT64_CONSTANT (4660046610375530309),
186 G_GINT64_CONSTANT (7540113804746346429)
Roger Meier60b7ad62014-07-29 23:23:36 +0200187 };
188 static const guint8 initial_base64[8] =
189 {
190 0x56, 0x47, 0x68, 0x79, 0x61, 0x57, 0x5a, 0x30
191 };
192
193 TTestOneOfEach *one_of_each;
194 gboolean im_true;
195 gboolean im_false;
196 gint a_bite;
197 gint integer16;
198 gint integer32;
199 gint64 integer64;
200 double double_precision;
201 gchar *some_characters;
202 gchar *zomg_unicode;
203 gboolean what_who;
204 GByteArray *base64;
205 GArray *byte_list;
206 GArray *i16_list;
207 GArray *i64_list;
208
209 base64 = g_byte_array_new ();
210 g_byte_array_append (base64, initial_base64, 8);
211
212 byte_list = g_array_new (FALSE, FALSE, sizeof (gint8));
213 g_array_append_vals (byte_list, initial_byte_list, 5);
214
215 i16_list = g_array_new (FALSE, FALSE, sizeof (gint16));
216 g_array_append_vals (i16_list, initial_i16_list, 5);
217
218 i64_list = g_array_new (FALSE, FALSE, sizeof (gint64));
219 g_array_append_vals (i64_list, initial_i64_list, 5);
220
221 /* All of OneOfEach's properties can be set at construction... */
Simon Southbf8f7b42015-12-23 20:29:29 -0500222 one_of_each =
223 g_object_new (T_TEST_TYPE_ONE_OF_EACH,
224 "im_true", TRUE,
225 "im_false", FALSE,
226 "a_bite", 0x50,
227 "integer16", 0x7e57,
228 "integer32", 0xdeadbeef,
229 "integer64", G_GINT64_CONSTANT (0xfa15efacade15bad),
230 "double_precision", M_PI,
231 "some_characters", "Debug THIS!",
232 "zomg_unicode", "\xd7\n\a\t",
233 "what_who", TRUE,
234 "base64", base64,
235 "byte_list", byte_list,
236 "i16_list", i16_list,
237 "i64_list", i64_list,
238 NULL);
Roger Meier60b7ad62014-07-29 23:23:36 +0200239 g_assert (one_of_each != NULL);
240
241 g_array_unref (i64_list);
242 i64_list = NULL;
243 g_array_unref (i16_list);
244 i16_list = NULL;
245 g_array_unref (byte_list);
246 byte_list = NULL;
247 g_byte_array_unref (base64);
248 base64 = NULL;
249
250 /* ...and later retrieved */
251 g_object_get (one_of_each,
252 "im_true", &im_true,
253 "im_false", &im_false,
254 "a_bite", &a_bite,
255 "integer16", &integer16,
256 "integer32", &integer32,
257 "integer64", &integer64,
258 "double_precision", &double_precision,
259 "some_characters", &some_characters,
260 "zomg_unicode", &zomg_unicode,
261 "what_who", &what_who,
262 "base64", &base64,
263 "byte_list", &byte_list,
264 "i16_list", &i16_list,
265 "i64_list", &i64_list,
266 NULL);
267
268 g_assert (im_true == TRUE);
269 g_assert (im_false == FALSE);
270
271 g_assert_cmphex (a_bite, ==, 0x50);
272 g_assert_cmphex (integer16, ==, 0x7e57);
273 g_assert_cmphex (integer32, ==, (gint32)0xdeadbeef);
Simon Southbf8f7b42015-12-23 20:29:29 -0500274 g_assert_cmphex (integer64, ==, G_GINT64_CONSTANT (0xfa15efacade15bad));
Roger Meier60b7ad62014-07-29 23:23:36 +0200275
276 g_assert_cmpfloat (double_precision, ==, M_PI);
277
278 g_assert_cmpstr (some_characters, ==, "Debug THIS!");
279 g_assert_cmpstr (zomg_unicode, ==, "\xd7\n\a\t");
280
281 g_assert (what_who == TRUE);
282
283 g_assert_cmpint (base64->len, ==, 8);
284 g_assert_cmpint (memcmp (base64->data,
285 initial_base64,
286 8 * sizeof (guint8)), ==, 0);
287
288 g_assert_cmpint (byte_list->len, ==, 5);
289 g_assert_cmpint (memcmp (byte_list->data,
290 initial_byte_list,
291 5 * sizeof (gint8)), ==, 0);
292
293 g_assert_cmpint (i16_list->len, ==, 5);
294 g_assert_cmpint (memcmp (i16_list->data,
295 initial_i16_list,
296 5 * sizeof (gint16)), ==, 0);
297
298 g_assert_cmpint (i64_list->len, ==, 5);
299 g_assert_cmpint (memcmp (i64_list->data,
300 initial_i64_list,
301 5 * sizeof (gint64)), ==, 0);
302
303 g_array_unref (i64_list);
304 g_array_unref (i16_list);
305 g_array_unref (byte_list);
306 g_byte_array_unref (base64);
307
308 g_object_unref (one_of_each);
309}
310
311static void
312test_structs_one_of_each_properties_byte_list (void)
313{
314 TTestOneOfEach *one_of_each;
315 GArray *byte_list = NULL;
316
317 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
318
319 /* OneOfEach's "byte_list" member is a list that holds eight-bit-wide integer
320 values */
321 g_object_get (one_of_each, "byte_list", &byte_list, NULL);
322
323 g_assert (byte_list != NULL);
324 g_assert_cmpint (g_array_get_element_size (byte_list), ==, sizeof (gint8));
325
326 g_array_unref (byte_list);
327 g_object_unref (one_of_each);
328}
329
330static void
331test_structs_one_of_each_properties_i16_list (void)
332{
333 TTestOneOfEach *one_of_each;
334 GArray *i16_list = NULL;
335
336 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
337
338 /* OneOfEach's "i16_list" member is a list that holds sixteen-bit-wide integer
339 values */
340 g_object_get (one_of_each, "i16_list", &i16_list, NULL);
341
342 g_assert (i16_list != NULL);
343 g_assert_cmpint (g_array_get_element_size (i16_list), ==, sizeof (gint16));
344
345 g_array_unref (i16_list);
346 g_object_unref (one_of_each);
347}
348
349static void
350test_structs_one_of_each_properties_i64_list (void)
351{
352 TTestOneOfEach *one_of_each;
353 GArray *i64_list = NULL;
354
355 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
356
357 /* OneOfEach's "i64_list" member is a list that holds sixty-four-bit-wide
358 integer values */
359 g_object_get (one_of_each, "i64_list", &i64_list, NULL);
360
361 g_assert (i64_list != NULL);
362 g_assert_cmpint (g_array_get_element_size (i64_list), ==, sizeof (gint64));
363
364 g_array_unref (i64_list);
365 g_object_unref (one_of_each);
366}
367
368static void
369test_structs_nesting_create_and_destroy (void)
370{
371 GObject *object = NULL;
372
373 /* A Nesting structure can be created... */
374 object = g_object_new (T_TEST_TYPE_NESTING, NULL);
375
376 g_assert (object != NULL);
377 g_assert (T_TEST_IS_NESTING (object));
378
379 /* ...and destroyed */
380 g_object_unref (object);
381}
382
383static void
384test_structs_nesting_properties_my_bonk (void)
385{
386 TTestNesting *nesting;
387 TTestBonk *bonk = NULL;
388 gint type;
389 gchar *message;
390
391 nesting = g_object_new (T_TEST_TYPE_NESTING, NULL);
392
393 /* Nesting's "my_bonk" member is initialized with a new, default Bonk object
394 during construction */
395 g_object_get (nesting, "my_bonk", &bonk, NULL);
396
397 g_assert (bonk != NULL);
398 g_assert (T_TEST_IS_BONK (bonk));
399
400 g_object_get (bonk,
401 "type", &type,
402 "message", &message,
403 NULL);
404
405 g_assert_cmpint (type, ==, 0);
406 g_assert (message == NULL);
407
408 g_object_unref (bonk);
409 bonk = NULL;
410
411 /* It can be replaced... */
412 bonk = g_object_new (T_TEST_TYPE_BONK,
413 "type", 100,
414 "message", "Replacement Bonk",
415 NULL);
416 g_object_set (nesting, "my_bonk", bonk, NULL);
417 g_object_unref (bonk);
418 bonk = NULL;
419
420 g_object_get (nesting, "my_bonk", &bonk, NULL);
421
422 g_assert (bonk != NULL);
423 g_assert (T_TEST_IS_BONK (bonk));
424
425 g_object_get (bonk,
426 "type", &type,
427 "message", &message,
428 NULL);
429
430 g_assert_cmpint (type, ==, 100);
431 g_assert_cmpstr (message, ==, "Replacement Bonk");
432
433 g_free (message);
434 g_object_unref (bonk);
435 bonk = NULL;
436
437 /* ...or set to null */
438 g_object_set (nesting, "my_bonk", NULL, NULL);
439 g_object_get (nesting, "my_bonk", &bonk, NULL);
440
441 g_assert (bonk == NULL);
442
443 g_object_unref (nesting);
444}
445
446static void
447test_structs_nesting_properties_my_ooe (void)
448{
449 TTestNesting *nesting;
450 TTestOneOfEach *one_of_each = NULL;
451 gint a_bite;
452 gint integer16;
453
454 nesting = g_object_new (T_TEST_TYPE_NESTING, NULL);
455
456 /* Nesting's "my_ooe" member is initialized with a new, default OneOfEach
457 object during construction */
458 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
459
460 g_assert (one_of_each != NULL);
461 g_assert (T_TEST_IS_ONE_OF_EACH (one_of_each));
462
463 g_object_get (one_of_each,
464 "a_bite", &a_bite,
465 "integer16", &integer16,
466 NULL);
467
468 g_assert_cmphex (a_bite, ==, 0x7f);
469 g_assert_cmphex (integer16, ==, 0x7fff);
470
471 g_object_unref (one_of_each);
472 one_of_each = NULL;
473
474 /* It can be replaced... */
475 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH,
476 "a_bite", 0x50,
477 "integer16", 0x5050,
478 NULL);
479 g_object_set (nesting, "my_ooe", one_of_each, NULL);
480 g_object_unref (one_of_each);
481 one_of_each = NULL;
482
483 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
484
485 g_assert (one_of_each != NULL);
486 g_assert (T_TEST_IS_ONE_OF_EACH (one_of_each));
487
488 g_object_get (one_of_each,
489 "a_bite", &a_bite,
490 "integer16", &integer16,
491 NULL);
492
493 g_assert_cmphex (a_bite, ==, 0x50);
494 g_assert_cmphex (integer16, ==, 0x5050);
495
496 g_object_unref (one_of_each);
497 one_of_each = NULL;
498
499 /* ...or set to null */
500 g_object_set (nesting, "my_ooe", NULL, NULL);
501 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
502
503 g_assert (one_of_each == NULL);
504
505 g_object_unref (nesting);
506}
507
508static void
509test_structs_holy_moley_create_and_destroy (void)
510{
511 GObject *object = NULL;
512
513 /* A HolyMoley structure can be created... */
514 object = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
515
516 g_assert (object != NULL);
517 g_assert (T_TEST_IS_HOLY_MOLEY (object));
518
519 /* ...and destroyed */
520 g_object_unref (object);
521}
522
523static void
524test_structs_holy_moley_properties_big (void)
525{
526 TTestHolyMoley *holy_moley;
527 GPtrArray *big = NULL;
528 gint a_bite = 0;
529 gint integer16 = 0;
530
531 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
532
533 /* A HolyMoley's "big" member is is initialized on construction */
534 g_object_get (holy_moley, "big", &big, NULL);
535
536 g_assert (big != NULL);
537 g_assert_cmpint (big->len, ==, 0);
538
539 /* It can be modified... */
540 g_ptr_array_add (big,
541 g_object_new (T_TEST_TYPE_ONE_OF_EACH,
542 "a_bite", 0x50,
543 "integer16", 0x5050,
544 NULL));
545
546 g_ptr_array_unref (big);
547 big = NULL;
548
549 g_object_get (holy_moley, "big", &big, NULL);
550
551 g_assert_cmpint (big->len, ==, 1);
552 g_object_get (g_ptr_array_index (big, 0),
553 "a_bite", &a_bite,
554 "integer16", &integer16,
555 NULL);
556
557 g_assert_cmphex (a_bite, ==, 0x50);
558 g_assert_cmphex (integer16, ==, 0x5050);
559
560 g_ptr_array_unref (big);
561 big = NULL;
562
563 /* ...replaced... */
564 big = g_ptr_array_new_with_free_func (g_object_unref);
565 g_ptr_array_add (big,
566 g_object_new (T_TEST_TYPE_ONE_OF_EACH,
567 "a_bite", 0x64,
568 "integer16", 0x1541,
569 NULL));
570
571 g_object_set (holy_moley, "big", big, NULL);
572
573 g_ptr_array_unref (big);
574 big = NULL;
575
576 g_object_get (holy_moley, "big", &big, NULL);
577
578 g_assert_cmpint (big->len, ==, 1);
579 g_object_get (g_ptr_array_index (big, 0),
580 "a_bite", &a_bite,
581 "integer16", &integer16,
582 NULL);
583
584 g_assert_cmphex (a_bite, ==, 0x64);
585 g_assert_cmphex (integer16, ==, 0x1541);
586
587 g_ptr_array_unref (big);
588 big = NULL;
589
590 /* ...or set to NULL */
591 g_object_set (holy_moley, "big", NULL, NULL);
592 g_object_get (holy_moley, "big", &big, NULL);
593
594 g_assert (big == NULL);
595
596 g_object_unref (holy_moley);
597}
598
599static void
600test_structs_holy_moley_properties_contain (void)
601{
602 static gchar *strings[2] = { "Apache", "Thrift" };
603
604 TTestHolyMoley *holy_moley;
605 GHashTable *contain = NULL;
606 GPtrArray *string_list;
607 GList *key_list;
608
609 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
610
611 /* A HolyMoley's "contain" member is initialized on construction */
612 g_object_get (holy_moley, "contain", &contain, NULL);
613
614 g_assert (contain != NULL);
615 g_assert_cmpint (g_hash_table_size (contain), ==, 0);
616
617 /* It can be modified... */
618 string_list = g_ptr_array_new ();
619 g_ptr_array_add (string_list, strings[0]);
620 g_ptr_array_add (string_list, strings[1]);
621
622 g_hash_table_insert (contain, string_list, NULL);
623 string_list = NULL;
624
625 g_hash_table_unref (contain);
626 contain = NULL;
627
628 g_object_get (holy_moley, "contain", &contain, NULL);
629
630 g_assert_cmpint (g_hash_table_size (contain), ==, 1);
631
632 key_list = g_hash_table_get_keys (contain);
633 string_list = g_list_nth_data (key_list, 0);
634
635 g_assert_cmpint (string_list->len, ==, 2);
636 g_assert_cmpstr (g_ptr_array_index (string_list, 0), ==, "Apache");
637 g_assert_cmpstr (g_ptr_array_index (string_list, 1), ==, "Thrift");
638
639 g_list_free (key_list);
640 g_hash_table_unref (contain);
641 contain = NULL;
642
643 /* ...replaced... */
644 contain = g_hash_table_new_full (g_direct_hash,
645 g_direct_equal,
646 (GDestroyNotify) g_ptr_array_unref,
647 NULL);
648 g_object_set (holy_moley, "contain", contain, NULL);
649 g_hash_table_unref (contain);
650 contain = NULL;
651
652 g_object_get (holy_moley, "contain", &contain, NULL);
653
654 g_assert_cmpint (g_hash_table_size (contain), ==, 0);
655
656 g_hash_table_unref (contain);
657 contain = NULL;
658
659 /* ...or set to NULL */
660 g_object_set (holy_moley, "contain", NULL, NULL);
661 g_object_get (holy_moley, "contain", &contain, NULL);
662
663 g_assert (contain == NULL);
664
665 g_object_unref (holy_moley);
666}
667
668static void
669test_structs_holy_moley_properties_bonks (void)
670{
671 TTestHolyMoley *holy_moley;
672 GHashTable *bonks = NULL;
673 GPtrArray *bonk_list = NULL;
674 TTestBonk *bonk = NULL;
675 gint type;
676 gchar *message;
677 GList *key_list;
678
679 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
680
681 /* A HolyMoley's "bonks" member is initialized on construction */
682 g_object_get (holy_moley, "bonks", &bonks, NULL);
683
684 g_assert (bonks != NULL);
685 g_assert_cmpint (g_hash_table_size (bonks), ==, 0);
686
687 /* It can be modified... */
688 bonk = g_object_new (T_TEST_TYPE_BONK,
689 "type", 100,
690 "message", "Sample Bonk",
691 NULL);
692 bonk_list = g_ptr_array_new_with_free_func (g_object_unref);
693 g_ptr_array_add (bonk_list, bonk);
694 bonk = NULL;
695
696 g_hash_table_insert (bonks, g_strdup ("Sample Bonks"), bonk_list);
697 bonk_list = NULL;
698
699 g_hash_table_unref (bonks);
700 bonks = NULL;
701
702 g_object_get (holy_moley, "bonks", &bonks, NULL);
703
704 g_assert_cmpint (g_hash_table_size (bonks), ==, 1);
705
706 key_list = g_hash_table_get_keys (bonks);
707 bonk_list = g_hash_table_lookup (bonks, g_list_nth_data (key_list, 0));
708
709 g_assert_cmpint (bonk_list->len, ==, 1);
710
711 bonk = (g_ptr_array_index (bonk_list, 0));
712 g_object_get (bonk,
713 "type", &type,
714 "message", &message,
715 NULL);
716
717 g_assert_cmpint (type, ==, 100);
718 g_assert_cmpstr (message, ==, "Sample Bonk");
719
720 bonk = NULL;
721 g_free (message);
722 g_list_free (key_list);
723 g_hash_table_unref (bonks);
724 bonks = NULL;
725
726 /* ...replaced... */
727 bonks = g_hash_table_new_full (g_str_hash,
728 g_str_equal,
729 g_free,
730 (GDestroyNotify) g_ptr_array_unref);
731 g_object_set (holy_moley, "bonks", bonks, NULL);
732 g_hash_table_unref (bonks);
733 bonks = NULL;
734
735 g_object_get (holy_moley, "bonks", &bonks, NULL);
736
737 g_assert_cmpint (g_hash_table_size (bonks), ==, 0);
738
739 g_hash_table_unref (bonks);
740 bonks = NULL;
741
742 /* ...or set to NULL */
743 g_object_set (holy_moley, "bonks", NULL, NULL);
744 g_object_get (holy_moley, "bonks", &bonks, NULL);
745
746 g_assert (bonks == NULL);
747
748 g_object_unref (holy_moley);
749}
750
751static void
752test_structs_empty (void)
753{
754 GObject *object = NULL;
755 GParamSpec **properties;
756 guint property_count;
757
758 /* An Empty structure can be created */
759 object = g_object_new (T_TEST_TYPE_EMPTY, NULL);
760
761 g_assert (object != NULL);
762 g_assert (T_TEST_IS_EMPTY (object));
763
764 /* An Empty structure has no members and thus no properties */
765 properties = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
766 &property_count);
767 g_assert_cmpint (property_count, ==, 0);
768 g_free (properties);
769
770 /* An Empty structure can be destroyed */
771 g_object_unref (object);
772}
773
774static void
775test_structs_wrapper_create_and_destroy (void)
776{
777 GObject *object = NULL;
778
779 /* A Wrapper structure can be created... */
780 object = g_object_new (T_TEST_TYPE_EMPTY, NULL);
781
782 g_assert (object != NULL);
783 g_assert (T_TEST_IS_EMPTY (object));
784
785 /* ...and destroyed */
786 g_object_unref (object);
787}
788
789static void
790test_structs_wrapper_properties_foo (void) {
791 TTestWrapper *wrapper;
792 TTestEmpty *foo;
793
794 wrapper = g_object_new (T_TEST_TYPE_WRAPPER, NULL);
795
796 /* A Wrapper structure has one member, "foo", which is an Empty
797 structure initialized during construction */
798 g_object_get (wrapper, "foo", &foo, NULL);
799
800 g_assert (foo != NULL);
801 g_assert (T_TEST_IS_EMPTY (foo));
802
803 g_object_unref (foo);
804 foo = NULL;
805
806 /* A Wrapper's foo property can be replaced... */
807 foo = g_object_new (T_TEST_TYPE_EMPTY, NULL);
808 g_object_set (wrapper, "foo", foo, NULL);
809
810 g_object_unref (foo);
811 foo = NULL;
812
813 g_object_get (wrapper, "foo", &foo, NULL);
814 g_assert (foo != NULL);
815 g_assert (T_TEST_IS_EMPTY (foo));
816
817 g_object_unref (foo);
818 foo = NULL;
819
820 /* ...or set to NULL */
821 g_object_set (wrapper, "foo", NULL, NULL);
822 g_object_get (wrapper, "foo", &foo, NULL);
823
824 g_assert (foo == NULL);
825
826 g_object_unref (wrapper);
827}
828
829static void
830test_services_inherited (void)
Roger Meier1d7e35a2014-07-22 21:56:12 +0200831{
832 ThriftProtocol *protocol;
833 TTestInheritedClient *inherited_client;
834 GObject *input_protocol, *output_protocol;
835
836 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
837 inherited_client = g_object_new (T_TEST_TYPE_INHERITED_CLIENT,
838 NULL);
839
840 /* TTestInheritedClient inherits from TTestSrvClient */
841 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
842 T_TEST_TYPE_SRV_CLIENT));
843
844 /* TTestInheritedClient implements TTestSrvClient's interface */
845 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
846 T_TEST_TYPE_SRV_IF));
847
Roger Meier60b7ad62014-07-29 23:23:36 +0200848 /* TTestInheritedClient's inherited properties can be set and retrieved */
Roger Meier1d7e35a2014-07-22 21:56:12 +0200849 g_object_set (inherited_client,
850 "input_protocol", protocol,
851 "output_protocol", protocol,
852 NULL);
853
854 g_object_get (inherited_client,
855 "input_protocol", &input_protocol,
856 "output_protocol", &output_protocol,
857 NULL);
858
859 assert (input_protocol == G_OBJECT(protocol));
860 assert (output_protocol == G_OBJECT(protocol));
861
862 g_object_unref (output_protocol);
863 g_object_unref (input_protocol);
864 g_object_unref (inherited_client);
865 g_object_unref (protocol);
866}
867
Roger Meierc1010922010-11-26 10:17:48 +0000868int
869main(int argc, char *argv[])
870{
Jens Geyer1c190272015-07-28 23:15:18 +0200871#if (!GLIB_CHECK_VERSION (2, 36, 0))
Roger Meier60b7ad62014-07-29 23:23:36 +0200872 g_type_init ();
873#endif
874
Roger Meierc1010922010-11-26 10:17:48 +0000875 g_test_init (&argc, &argv, NULL);
876
Roger Meier60b7ad62014-07-29 23:23:36 +0200877 g_test_add_func
878 ("/testdebugproto/DebugProto/Structs/Doubles/CreateAndDestroy",
879 test_structs_doubles_create_and_destroy);
880 g_test_add_func
881 ("/testdebugproto/DebugProto/Structs/Doubles/Initialize",
882 test_structs_doubles_initialize);
883
884 g_test_add_func
885 ("/testdebugproto/DebugProto/Structs/OneOfEach/CreateAndDestroy",
886 test_structs_one_of_each_create_and_destroy);
887 g_test_add_func
888 ("/testdebugproto/DebugProto/Structs/OneOfEach/Initialize/DefaultValues",
889 test_structs_one_of_each_initialize_default_values);
890 g_test_add_func
891 ("/testdebugproto/DebugProto/Structs/OneOfEach/Initialize/SpecifiedValues",
892 test_structs_one_of_each_initialize_specified_values);
893 g_test_add_func
894 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/byte_list",
895 test_structs_one_of_each_properties_byte_list);
896 g_test_add_func
897 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/i16_list",
898 test_structs_one_of_each_properties_i16_list);
899 g_test_add_func
900 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/i64_list",
901 test_structs_one_of_each_properties_i64_list);
902
903 g_test_add_func
904 ("/testdebugproto/DebugProto/Structs/Nesting/CreateAndDestroy",
905 test_structs_nesting_create_and_destroy);
906 g_test_add_func
907 ("/testdebugproto/DebugProto/Structs/Nesting/Properties/my_bonk",
908 test_structs_nesting_properties_my_bonk);
909 g_test_add_func
910 ("/testdebugproto/DebugProto/Structs/Nesting/Properties/my_ooe",
911 test_structs_nesting_properties_my_ooe);
912
913 g_test_add_func
914 ("/testdebugproto/DebugProto/Structs/HolyMoley/CreateAndDestroy",
915 test_structs_holy_moley_create_and_destroy);
916 g_test_add_func
917 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/big",
918 test_structs_holy_moley_properties_big);
919 g_test_add_func
920 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/contain",
921 test_structs_holy_moley_properties_contain);
922 g_test_add_func
923 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/bonks",
924 test_structs_holy_moley_properties_bonks);
925
926 g_test_add_func
927 ("/testdebugproto/DebugProto/Structs/Empty",
928 test_structs_empty);
929
930 g_test_add_func
931 ("/testdebugproto/DebugProto/Structs/Wrapper/CreateAndDestroy",
932 test_structs_wrapper_create_and_destroy);
933 g_test_add_func
934 ("/testdebugproto/DebugProto/Structs/Wrapper/Properties/foo",
935 test_structs_wrapper_properties_foo);
936
937 g_test_add_func
938 ("/testdebugproto/DebugProto/Services/Inherited",
939 test_services_inherited);
Roger Meierc1010922010-11-26 10:17:48 +0000940
941 return g_test_run ();
942}