blob: e343c1eb85cbdd5e9c3a9e6515a7451ffbc35b43 [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);
149 g_assert_cmpint (integer64, ==, 10000000000);
150
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 {
182 1100087778366101931, 1779979416004714189, 2880067194370816120,
183 4660046610375530309, 7540113804746346429
184 };
185 static const guint8 initial_base64[8] =
186 {
187 0x56, 0x47, 0x68, 0x79, 0x61, 0x57, 0x5a, 0x30
188 };
189
190 TTestOneOfEach *one_of_each;
191 gboolean im_true;
192 gboolean im_false;
193 gint a_bite;
194 gint integer16;
195 gint integer32;
196 gint64 integer64;
197 double double_precision;
198 gchar *some_characters;
199 gchar *zomg_unicode;
200 gboolean what_who;
201 GByteArray *base64;
202 GArray *byte_list;
203 GArray *i16_list;
204 GArray *i64_list;
205
206 base64 = g_byte_array_new ();
207 g_byte_array_append (base64, initial_base64, 8);
208
209 byte_list = g_array_new (FALSE, FALSE, sizeof (gint8));
210 g_array_append_vals (byte_list, initial_byte_list, 5);
211
212 i16_list = g_array_new (FALSE, FALSE, sizeof (gint16));
213 g_array_append_vals (i16_list, initial_i16_list, 5);
214
215 i64_list = g_array_new (FALSE, FALSE, sizeof (gint64));
216 g_array_append_vals (i64_list, initial_i64_list, 5);
217
218 /* All of OneOfEach's properties can be set at construction... */
219 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH,
220 "im_true", TRUE,
221 "im_false", FALSE,
222 "a_bite", 0x50,
223 "integer16", 0x7e57,
224 "integer32", 0xdeadbeef,
225 "integer64", 0xfa15efacade15bad,
226 "double_precision", M_PI,
227 "some_characters", "Debug THIS!",
228 "zomg_unicode", "\xd7\n\a\t",
229 "what_who", TRUE,
230 "base64", base64,
231 "byte_list", byte_list,
232 "i16_list", i16_list,
233 "i64_list", i64_list,
234 NULL);
235 g_assert (one_of_each != NULL);
236
237 g_array_unref (i64_list);
238 i64_list = NULL;
239 g_array_unref (i16_list);
240 i16_list = NULL;
241 g_array_unref (byte_list);
242 byte_list = NULL;
243 g_byte_array_unref (base64);
244 base64 = NULL;
245
246 /* ...and later retrieved */
247 g_object_get (one_of_each,
248 "im_true", &im_true,
249 "im_false", &im_false,
250 "a_bite", &a_bite,
251 "integer16", &integer16,
252 "integer32", &integer32,
253 "integer64", &integer64,
254 "double_precision", &double_precision,
255 "some_characters", &some_characters,
256 "zomg_unicode", &zomg_unicode,
257 "what_who", &what_who,
258 "base64", &base64,
259 "byte_list", &byte_list,
260 "i16_list", &i16_list,
261 "i64_list", &i64_list,
262 NULL);
263
264 g_assert (im_true == TRUE);
265 g_assert (im_false == FALSE);
266
267 g_assert_cmphex (a_bite, ==, 0x50);
268 g_assert_cmphex (integer16, ==, 0x7e57);
269 g_assert_cmphex (integer32, ==, (gint32)0xdeadbeef);
270 g_assert_cmphex (integer64, ==, 0xfa15efacade15bad);
271
272 g_assert_cmpfloat (double_precision, ==, M_PI);
273
274 g_assert_cmpstr (some_characters, ==, "Debug THIS!");
275 g_assert_cmpstr (zomg_unicode, ==, "\xd7\n\a\t");
276
277 g_assert (what_who == TRUE);
278
279 g_assert_cmpint (base64->len, ==, 8);
280 g_assert_cmpint (memcmp (base64->data,
281 initial_base64,
282 8 * sizeof (guint8)), ==, 0);
283
284 g_assert_cmpint (byte_list->len, ==, 5);
285 g_assert_cmpint (memcmp (byte_list->data,
286 initial_byte_list,
287 5 * sizeof (gint8)), ==, 0);
288
289 g_assert_cmpint (i16_list->len, ==, 5);
290 g_assert_cmpint (memcmp (i16_list->data,
291 initial_i16_list,
292 5 * sizeof (gint16)), ==, 0);
293
294 g_assert_cmpint (i64_list->len, ==, 5);
295 g_assert_cmpint (memcmp (i64_list->data,
296 initial_i64_list,
297 5 * sizeof (gint64)), ==, 0);
298
299 g_array_unref (i64_list);
300 g_array_unref (i16_list);
301 g_array_unref (byte_list);
302 g_byte_array_unref (base64);
303
304 g_object_unref (one_of_each);
305}
306
307static void
308test_structs_one_of_each_properties_byte_list (void)
309{
310 TTestOneOfEach *one_of_each;
311 GArray *byte_list = NULL;
312
313 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
314
315 /* OneOfEach's "byte_list" member is a list that holds eight-bit-wide integer
316 values */
317 g_object_get (one_of_each, "byte_list", &byte_list, NULL);
318
319 g_assert (byte_list != NULL);
320 g_assert_cmpint (g_array_get_element_size (byte_list), ==, sizeof (gint8));
321
322 g_array_unref (byte_list);
323 g_object_unref (one_of_each);
324}
325
326static void
327test_structs_one_of_each_properties_i16_list (void)
328{
329 TTestOneOfEach *one_of_each;
330 GArray *i16_list = NULL;
331
332 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
333
334 /* OneOfEach's "i16_list" member is a list that holds sixteen-bit-wide integer
335 values */
336 g_object_get (one_of_each, "i16_list", &i16_list, NULL);
337
338 g_assert (i16_list != NULL);
339 g_assert_cmpint (g_array_get_element_size (i16_list), ==, sizeof (gint16));
340
341 g_array_unref (i16_list);
342 g_object_unref (one_of_each);
343}
344
345static void
346test_structs_one_of_each_properties_i64_list (void)
347{
348 TTestOneOfEach *one_of_each;
349 GArray *i64_list = NULL;
350
351 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
352
353 /* OneOfEach's "i64_list" member is a list that holds sixty-four-bit-wide
354 integer values */
355 g_object_get (one_of_each, "i64_list", &i64_list, NULL);
356
357 g_assert (i64_list != NULL);
358 g_assert_cmpint (g_array_get_element_size (i64_list), ==, sizeof (gint64));
359
360 g_array_unref (i64_list);
361 g_object_unref (one_of_each);
362}
363
364static void
365test_structs_nesting_create_and_destroy (void)
366{
367 GObject *object = NULL;
368
369 /* A Nesting structure can be created... */
370 object = g_object_new (T_TEST_TYPE_NESTING, NULL);
371
372 g_assert (object != NULL);
373 g_assert (T_TEST_IS_NESTING (object));
374
375 /* ...and destroyed */
376 g_object_unref (object);
377}
378
379static void
380test_structs_nesting_properties_my_bonk (void)
381{
382 TTestNesting *nesting;
383 TTestBonk *bonk = NULL;
384 gint type;
385 gchar *message;
386
387 nesting = g_object_new (T_TEST_TYPE_NESTING, NULL);
388
389 /* Nesting's "my_bonk" member is initialized with a new, default Bonk object
390 during construction */
391 g_object_get (nesting, "my_bonk", &bonk, NULL);
392
393 g_assert (bonk != NULL);
394 g_assert (T_TEST_IS_BONK (bonk));
395
396 g_object_get (bonk,
397 "type", &type,
398 "message", &message,
399 NULL);
400
401 g_assert_cmpint (type, ==, 0);
402 g_assert (message == NULL);
403
404 g_object_unref (bonk);
405 bonk = NULL;
406
407 /* It can be replaced... */
408 bonk = g_object_new (T_TEST_TYPE_BONK,
409 "type", 100,
410 "message", "Replacement Bonk",
411 NULL);
412 g_object_set (nesting, "my_bonk", bonk, NULL);
413 g_object_unref (bonk);
414 bonk = NULL;
415
416 g_object_get (nesting, "my_bonk", &bonk, NULL);
417
418 g_assert (bonk != NULL);
419 g_assert (T_TEST_IS_BONK (bonk));
420
421 g_object_get (bonk,
422 "type", &type,
423 "message", &message,
424 NULL);
425
426 g_assert_cmpint (type, ==, 100);
427 g_assert_cmpstr (message, ==, "Replacement Bonk");
428
429 g_free (message);
430 g_object_unref (bonk);
431 bonk = NULL;
432
433 /* ...or set to null */
434 g_object_set (nesting, "my_bonk", NULL, NULL);
435 g_object_get (nesting, "my_bonk", &bonk, NULL);
436
437 g_assert (bonk == NULL);
438
439 g_object_unref (nesting);
440}
441
442static void
443test_structs_nesting_properties_my_ooe (void)
444{
445 TTestNesting *nesting;
446 TTestOneOfEach *one_of_each = NULL;
447 gint a_bite;
448 gint integer16;
449
450 nesting = g_object_new (T_TEST_TYPE_NESTING, NULL);
451
452 /* Nesting's "my_ooe" member is initialized with a new, default OneOfEach
453 object during construction */
454 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
455
456 g_assert (one_of_each != NULL);
457 g_assert (T_TEST_IS_ONE_OF_EACH (one_of_each));
458
459 g_object_get (one_of_each,
460 "a_bite", &a_bite,
461 "integer16", &integer16,
462 NULL);
463
464 g_assert_cmphex (a_bite, ==, 0x7f);
465 g_assert_cmphex (integer16, ==, 0x7fff);
466
467 g_object_unref (one_of_each);
468 one_of_each = NULL;
469
470 /* It can be replaced... */
471 one_of_each = g_object_new (T_TEST_TYPE_ONE_OF_EACH,
472 "a_bite", 0x50,
473 "integer16", 0x5050,
474 NULL);
475 g_object_set (nesting, "my_ooe", one_of_each, NULL);
476 g_object_unref (one_of_each);
477 one_of_each = NULL;
478
479 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
480
481 g_assert (one_of_each != NULL);
482 g_assert (T_TEST_IS_ONE_OF_EACH (one_of_each));
483
484 g_object_get (one_of_each,
485 "a_bite", &a_bite,
486 "integer16", &integer16,
487 NULL);
488
489 g_assert_cmphex (a_bite, ==, 0x50);
490 g_assert_cmphex (integer16, ==, 0x5050);
491
492 g_object_unref (one_of_each);
493 one_of_each = NULL;
494
495 /* ...or set to null */
496 g_object_set (nesting, "my_ooe", NULL, NULL);
497 g_object_get (nesting, "my_ooe", &one_of_each, NULL);
498
499 g_assert (one_of_each == NULL);
500
501 g_object_unref (nesting);
502}
503
504static void
505test_structs_holy_moley_create_and_destroy (void)
506{
507 GObject *object = NULL;
508
509 /* A HolyMoley structure can be created... */
510 object = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
511
512 g_assert (object != NULL);
513 g_assert (T_TEST_IS_HOLY_MOLEY (object));
514
515 /* ...and destroyed */
516 g_object_unref (object);
517}
518
519static void
520test_structs_holy_moley_properties_big (void)
521{
522 TTestHolyMoley *holy_moley;
523 GPtrArray *big = NULL;
524 gint a_bite = 0;
525 gint integer16 = 0;
526
527 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
528
529 /* A HolyMoley's "big" member is is initialized on construction */
530 g_object_get (holy_moley, "big", &big, NULL);
531
532 g_assert (big != NULL);
533 g_assert_cmpint (big->len, ==, 0);
534
535 /* It can be modified... */
536 g_ptr_array_add (big,
537 g_object_new (T_TEST_TYPE_ONE_OF_EACH,
538 "a_bite", 0x50,
539 "integer16", 0x5050,
540 NULL));
541
542 g_ptr_array_unref (big);
543 big = NULL;
544
545 g_object_get (holy_moley, "big", &big, NULL);
546
547 g_assert_cmpint (big->len, ==, 1);
548 g_object_get (g_ptr_array_index (big, 0),
549 "a_bite", &a_bite,
550 "integer16", &integer16,
551 NULL);
552
553 g_assert_cmphex (a_bite, ==, 0x50);
554 g_assert_cmphex (integer16, ==, 0x5050);
555
556 g_ptr_array_unref (big);
557 big = NULL;
558
559 /* ...replaced... */
560 big = g_ptr_array_new_with_free_func (g_object_unref);
561 g_ptr_array_add (big,
562 g_object_new (T_TEST_TYPE_ONE_OF_EACH,
563 "a_bite", 0x64,
564 "integer16", 0x1541,
565 NULL));
566
567 g_object_set (holy_moley, "big", big, NULL);
568
569 g_ptr_array_unref (big);
570 big = NULL;
571
572 g_object_get (holy_moley, "big", &big, NULL);
573
574 g_assert_cmpint (big->len, ==, 1);
575 g_object_get (g_ptr_array_index (big, 0),
576 "a_bite", &a_bite,
577 "integer16", &integer16,
578 NULL);
579
580 g_assert_cmphex (a_bite, ==, 0x64);
581 g_assert_cmphex (integer16, ==, 0x1541);
582
583 g_ptr_array_unref (big);
584 big = NULL;
585
586 /* ...or set to NULL */
587 g_object_set (holy_moley, "big", NULL, NULL);
588 g_object_get (holy_moley, "big", &big, NULL);
589
590 g_assert (big == NULL);
591
592 g_object_unref (holy_moley);
593}
594
595static void
596test_structs_holy_moley_properties_contain (void)
597{
598 static gchar *strings[2] = { "Apache", "Thrift" };
599
600 TTestHolyMoley *holy_moley;
601 GHashTable *contain = NULL;
602 GPtrArray *string_list;
603 GList *key_list;
604
605 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
606
607 /* A HolyMoley's "contain" member is initialized on construction */
608 g_object_get (holy_moley, "contain", &contain, NULL);
609
610 g_assert (contain != NULL);
611 g_assert_cmpint (g_hash_table_size (contain), ==, 0);
612
613 /* It can be modified... */
614 string_list = g_ptr_array_new ();
615 g_ptr_array_add (string_list, strings[0]);
616 g_ptr_array_add (string_list, strings[1]);
617
618 g_hash_table_insert (contain, string_list, NULL);
619 string_list = NULL;
620
621 g_hash_table_unref (contain);
622 contain = NULL;
623
624 g_object_get (holy_moley, "contain", &contain, NULL);
625
626 g_assert_cmpint (g_hash_table_size (contain), ==, 1);
627
628 key_list = g_hash_table_get_keys (contain);
629 string_list = g_list_nth_data (key_list, 0);
630
631 g_assert_cmpint (string_list->len, ==, 2);
632 g_assert_cmpstr (g_ptr_array_index (string_list, 0), ==, "Apache");
633 g_assert_cmpstr (g_ptr_array_index (string_list, 1), ==, "Thrift");
634
635 g_list_free (key_list);
636 g_hash_table_unref (contain);
637 contain = NULL;
638
639 /* ...replaced... */
640 contain = g_hash_table_new_full (g_direct_hash,
641 g_direct_equal,
642 (GDestroyNotify) g_ptr_array_unref,
643 NULL);
644 g_object_set (holy_moley, "contain", contain, NULL);
645 g_hash_table_unref (contain);
646 contain = NULL;
647
648 g_object_get (holy_moley, "contain", &contain, NULL);
649
650 g_assert_cmpint (g_hash_table_size (contain), ==, 0);
651
652 g_hash_table_unref (contain);
653 contain = NULL;
654
655 /* ...or set to NULL */
656 g_object_set (holy_moley, "contain", NULL, NULL);
657 g_object_get (holy_moley, "contain", &contain, NULL);
658
659 g_assert (contain == NULL);
660
661 g_object_unref (holy_moley);
662}
663
664static void
665test_structs_holy_moley_properties_bonks (void)
666{
667 TTestHolyMoley *holy_moley;
668 GHashTable *bonks = NULL;
669 GPtrArray *bonk_list = NULL;
670 TTestBonk *bonk = NULL;
671 gint type;
672 gchar *message;
673 GList *key_list;
674
675 holy_moley = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
676
677 /* A HolyMoley's "bonks" member is initialized on construction */
678 g_object_get (holy_moley, "bonks", &bonks, NULL);
679
680 g_assert (bonks != NULL);
681 g_assert_cmpint (g_hash_table_size (bonks), ==, 0);
682
683 /* It can be modified... */
684 bonk = g_object_new (T_TEST_TYPE_BONK,
685 "type", 100,
686 "message", "Sample Bonk",
687 NULL);
688 bonk_list = g_ptr_array_new_with_free_func (g_object_unref);
689 g_ptr_array_add (bonk_list, bonk);
690 bonk = NULL;
691
692 g_hash_table_insert (bonks, g_strdup ("Sample Bonks"), bonk_list);
693 bonk_list = NULL;
694
695 g_hash_table_unref (bonks);
696 bonks = NULL;
697
698 g_object_get (holy_moley, "bonks", &bonks, NULL);
699
700 g_assert_cmpint (g_hash_table_size (bonks), ==, 1);
701
702 key_list = g_hash_table_get_keys (bonks);
703 bonk_list = g_hash_table_lookup (bonks, g_list_nth_data (key_list, 0));
704
705 g_assert_cmpint (bonk_list->len, ==, 1);
706
707 bonk = (g_ptr_array_index (bonk_list, 0));
708 g_object_get (bonk,
709 "type", &type,
710 "message", &message,
711 NULL);
712
713 g_assert_cmpint (type, ==, 100);
714 g_assert_cmpstr (message, ==, "Sample Bonk");
715
716 bonk = NULL;
717 g_free (message);
718 g_list_free (key_list);
719 g_hash_table_unref (bonks);
720 bonks = NULL;
721
722 /* ...replaced... */
723 bonks = g_hash_table_new_full (g_str_hash,
724 g_str_equal,
725 g_free,
726 (GDestroyNotify) g_ptr_array_unref);
727 g_object_set (holy_moley, "bonks", bonks, NULL);
728 g_hash_table_unref (bonks);
729 bonks = NULL;
730
731 g_object_get (holy_moley, "bonks", &bonks, NULL);
732
733 g_assert_cmpint (g_hash_table_size (bonks), ==, 0);
734
735 g_hash_table_unref (bonks);
736 bonks = NULL;
737
738 /* ...or set to NULL */
739 g_object_set (holy_moley, "bonks", NULL, NULL);
740 g_object_get (holy_moley, "bonks", &bonks, NULL);
741
742 g_assert (bonks == NULL);
743
744 g_object_unref (holy_moley);
745}
746
747static void
748test_structs_empty (void)
749{
750 GObject *object = NULL;
751 GParamSpec **properties;
752 guint property_count;
753
754 /* An Empty structure can be created */
755 object = g_object_new (T_TEST_TYPE_EMPTY, NULL);
756
757 g_assert (object != NULL);
758 g_assert (T_TEST_IS_EMPTY (object));
759
760 /* An Empty structure has no members and thus no properties */
761 properties = g_object_class_list_properties (G_OBJECT_GET_CLASS (object),
762 &property_count);
763 g_assert_cmpint (property_count, ==, 0);
764 g_free (properties);
765
766 /* An Empty structure can be destroyed */
767 g_object_unref (object);
768}
769
770static void
771test_structs_wrapper_create_and_destroy (void)
772{
773 GObject *object = NULL;
774
775 /* A Wrapper structure can be created... */
776 object = g_object_new (T_TEST_TYPE_EMPTY, NULL);
777
778 g_assert (object != NULL);
779 g_assert (T_TEST_IS_EMPTY (object));
780
781 /* ...and destroyed */
782 g_object_unref (object);
783}
784
785static void
786test_structs_wrapper_properties_foo (void) {
787 TTestWrapper *wrapper;
788 TTestEmpty *foo;
789
790 wrapper = g_object_new (T_TEST_TYPE_WRAPPER, NULL);
791
792 /* A Wrapper structure has one member, "foo", which is an Empty
793 structure initialized during construction */
794 g_object_get (wrapper, "foo", &foo, NULL);
795
796 g_assert (foo != NULL);
797 g_assert (T_TEST_IS_EMPTY (foo));
798
799 g_object_unref (foo);
800 foo = NULL;
801
802 /* A Wrapper's foo property can be replaced... */
803 foo = g_object_new (T_TEST_TYPE_EMPTY, NULL);
804 g_object_set (wrapper, "foo", foo, NULL);
805
806 g_object_unref (foo);
807 foo = NULL;
808
809 g_object_get (wrapper, "foo", &foo, NULL);
810 g_assert (foo != NULL);
811 g_assert (T_TEST_IS_EMPTY (foo));
812
813 g_object_unref (foo);
814 foo = NULL;
815
816 /* ...or set to NULL */
817 g_object_set (wrapper, "foo", NULL, NULL);
818 g_object_get (wrapper, "foo", &foo, NULL);
819
820 g_assert (foo == NULL);
821
822 g_object_unref (wrapper);
823}
824
825static void
826test_services_inherited (void)
Roger Meier1d7e35a2014-07-22 21:56:12 +0200827{
828 ThriftProtocol *protocol;
829 TTestInheritedClient *inherited_client;
830 GObject *input_protocol, *output_protocol;
831
832 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
833 inherited_client = g_object_new (T_TEST_TYPE_INHERITED_CLIENT,
834 NULL);
835
836 /* TTestInheritedClient inherits from TTestSrvClient */
837 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
838 T_TEST_TYPE_SRV_CLIENT));
839
840 /* TTestInheritedClient implements TTestSrvClient's interface */
841 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
842 T_TEST_TYPE_SRV_IF));
843
Roger Meier60b7ad62014-07-29 23:23:36 +0200844 /* TTestInheritedClient's inherited properties can be set and retrieved */
Roger Meier1d7e35a2014-07-22 21:56:12 +0200845 g_object_set (inherited_client,
846 "input_protocol", protocol,
847 "output_protocol", protocol,
848 NULL);
849
850 g_object_get (inherited_client,
851 "input_protocol", &input_protocol,
852 "output_protocol", &output_protocol,
853 NULL);
854
855 assert (input_protocol == G_OBJECT(protocol));
856 assert (output_protocol == G_OBJECT(protocol));
857
858 g_object_unref (output_protocol);
859 g_object_unref (input_protocol);
860 g_object_unref (inherited_client);
861 g_object_unref (protocol);
862}
863
Roger Meierc1010922010-11-26 10:17:48 +0000864int
865main(int argc, char *argv[])
866{
Roger Meier60b7ad62014-07-29 23:23:36 +0200867#if (GLIB_MAJOR_VERSION == 2) && (GLIB_MINOR_VERSION < 36)
868 g_type_init ();
869#endif
870
Roger Meierc1010922010-11-26 10:17:48 +0000871 g_test_init (&argc, &argv, NULL);
872
Roger Meier60b7ad62014-07-29 23:23:36 +0200873 g_test_add_func
874 ("/testdebugproto/DebugProto/Structs/Doubles/CreateAndDestroy",
875 test_structs_doubles_create_and_destroy);
876 g_test_add_func
877 ("/testdebugproto/DebugProto/Structs/Doubles/Initialize",
878 test_structs_doubles_initialize);
879
880 g_test_add_func
881 ("/testdebugproto/DebugProto/Structs/OneOfEach/CreateAndDestroy",
882 test_structs_one_of_each_create_and_destroy);
883 g_test_add_func
884 ("/testdebugproto/DebugProto/Structs/OneOfEach/Initialize/DefaultValues",
885 test_structs_one_of_each_initialize_default_values);
886 g_test_add_func
887 ("/testdebugproto/DebugProto/Structs/OneOfEach/Initialize/SpecifiedValues",
888 test_structs_one_of_each_initialize_specified_values);
889 g_test_add_func
890 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/byte_list",
891 test_structs_one_of_each_properties_byte_list);
892 g_test_add_func
893 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/i16_list",
894 test_structs_one_of_each_properties_i16_list);
895 g_test_add_func
896 ("/testdebugproto/DebugProto/Structs/OneOfEach/Properties/i64_list",
897 test_structs_one_of_each_properties_i64_list);
898
899 g_test_add_func
900 ("/testdebugproto/DebugProto/Structs/Nesting/CreateAndDestroy",
901 test_structs_nesting_create_and_destroy);
902 g_test_add_func
903 ("/testdebugproto/DebugProto/Structs/Nesting/Properties/my_bonk",
904 test_structs_nesting_properties_my_bonk);
905 g_test_add_func
906 ("/testdebugproto/DebugProto/Structs/Nesting/Properties/my_ooe",
907 test_structs_nesting_properties_my_ooe);
908
909 g_test_add_func
910 ("/testdebugproto/DebugProto/Structs/HolyMoley/CreateAndDestroy",
911 test_structs_holy_moley_create_and_destroy);
912 g_test_add_func
913 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/big",
914 test_structs_holy_moley_properties_big);
915 g_test_add_func
916 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/contain",
917 test_structs_holy_moley_properties_contain);
918 g_test_add_func
919 ("/testdebugproto/DebugProto/Structs/HolyMoley/Properties/bonks",
920 test_structs_holy_moley_properties_bonks);
921
922 g_test_add_func
923 ("/testdebugproto/DebugProto/Structs/Empty",
924 test_structs_empty);
925
926 g_test_add_func
927 ("/testdebugproto/DebugProto/Structs/Wrapper/CreateAndDestroy",
928 test_structs_wrapper_create_and_destroy);
929 g_test_add_func
930 ("/testdebugproto/DebugProto/Structs/Wrapper/Properties/foo",
931 test_structs_wrapper_properties_foo);
932
933 g_test_add_func
934 ("/testdebugproto/DebugProto/Services/Inherited",
935 test_services_inherited);
Roger Meierc1010922010-11-26 10:17:48 +0000936
937 return g_test_run ();
938}