blob: f2ea3af737cc2baf5e1311c582653db9230296ee [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>
22#include <glib-object.h>
23
24#ifndef M_PI
25#define M_PI 3.1415926535897932385
26#endif
27
Roger Meier1d7e35a2014-07-22 21:56:12 +020028#include <thrift/c_glib/protocol/thrift_protocol.h>
29#include <thrift/c_glib/protocol/thrift_binary_protocol.h>
30
Roger Meier213a6642010-10-27 12:30:11 +000031#include "gen-c_glib/t_test_debug_proto_test_types.h"
Roger Meier1d7e35a2014-07-22 21:56:12 +020032#include "gen-c_glib/t_test_srv.h"
33#include "gen-c_glib/t_test_inherited.h"
Roger Meier213a6642010-10-27 12:30:11 +000034
Roger Meierc1010922010-11-26 10:17:48 +000035static void
Roger Meier1d7e35a2014-07-22 21:56:12 +020036test_structs(void)
Roger Meier213a6642010-10-27 12:30:11 +000037{
Roger Meier213a6642010-10-27 12:30:11 +000038 TTestOneOfEach *ooe = NULL;
39 TTestNesting *n = NULL;
40 TTestHolyMoley *hm = NULL;
Roger Meierc75797d2012-04-28 11:33:58 +000041 gchar *random = g_strdup("random string");
42 gchar *nothing = g_strdup("nothing");
Roger Meier213a6642010-10-27 12:30:11 +000043
44 ooe = g_object_new (T_TEST_TYPE_ONE_OF_EACH, NULL);
45 ooe->im_true = TRUE;
46 ooe->im_false = FALSE;
47 ooe->a_bite = 0xd6;
48 ooe->integer16 = 27000;
49 ooe->integer32 = 1<<24;
50 ooe->integer64 = (guint64) 6000 * 1000 * 1000;
51 ooe->double_precision = M_PI;
Roger Meierc75797d2012-04-28 11:33:58 +000052 ooe->some_characters = g_strdup("Debug THIS!");
53 ooe->zomg_unicode = g_strdup("\xd7\n\a\t");
Roger Meier213a6642010-10-27 12:30:11 +000054
55 n = g_object_new (T_TEST_TYPE_NESTING, NULL);
Roger Meierc75797d2012-04-28 11:33:58 +000056 if (n->my_ooe != NULL)
57 g_object_unref(n->my_ooe);
58
Roger Meier213a6642010-10-27 12:30:11 +000059 n->my_ooe = ooe;
60 n->my_ooe->integer16 = 16;
61 n->my_ooe->integer32 = 32;
62 n->my_ooe->integer64 = 64;
63 n->my_ooe->double_precision = (sqrt(5.0) + 1) / 2;
Roger Meierc75797d2012-04-28 11:33:58 +000064 n->my_ooe->some_characters = g_strdup(":R (me going \"rrrr\")");
65 n->my_ooe->zomg_unicode = g_strdup("\xd3\x80\xe2\x85\xae\xce\x9d\x20");
Roger Meier213a6642010-10-27 12:30:11 +000066 n->my_bonk->type = 31337;
Roger Meierc75797d2012-04-28 11:33:58 +000067 n->my_bonk->message = g_strdup("I am a bonk... xor!");
Roger Meier213a6642010-10-27 12:30:11 +000068
69 hm = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL);
70 g_ptr_array_add (hm->big, ooe);
Roger Meierc75797d2012-04-28 11:33:58 +000071 g_ptr_array_add (hm->big, g_object_ref(n->my_ooe));
Roger Meier213a6642010-10-27 12:30:11 +000072 ((TTestOneOfEach *) g_ptr_array_index (hm->big, 0))->a_bite = 0x22;
73 ((TTestOneOfEach *) g_ptr_array_index (hm->big, 1))->a_bite = 0x33;
74
Roger Meierc75797d2012-04-28 11:33:58 +000075 g_hash_table_insert (hm->contain, random, random);
Roger Meier213a6642010-10-27 12:30:11 +000076
77 TTestBonk *bonk = NULL;
78 bonk = g_object_new (T_TEST_TYPE_BONK, NULL);
Roger Meierc75797d2012-04-28 11:33:58 +000079 GPtrArray *bonks = g_ptr_array_new_with_free_func (g_object_unref);
Roger Meier213a6642010-10-27 12:30:11 +000080 g_ptr_array_add (bonks, bonk);
Roger Meierc75797d2012-04-28 11:33:58 +000081 g_hash_table_insert (hm->bonks, nothing, bonks);
Roger Meier213a6642010-10-27 12:30:11 +000082
Roger Meier213a6642010-10-27 12:30:11 +000083 g_object_unref (hm);
84
85 return 0;
86}
87
Roger Meier1d7e35a2014-07-22 21:56:12 +020088static void
89test_service_inheritance (void)
90{
91 ThriftProtocol *protocol;
92 TTestInheritedClient *inherited_client;
93 GObject *input_protocol, *output_protocol;
94
95 protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, NULL);
96 inherited_client = g_object_new (T_TEST_TYPE_INHERITED_CLIENT,
97 NULL);
98
99 /* TTestInheritedClient inherits from TTestSrvClient */
100 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
101 T_TEST_TYPE_SRV_CLIENT));
102
103 /* TTestInheritedClient implements TTestSrvClient's interface */
104 assert (g_type_is_a (T_TEST_TYPE_INHERITED_CLIENT,
105 T_TEST_TYPE_SRV_IF));
106
107 /* TTestInheritedClient's inherited properties can be set and
108 * retrieved */
109 g_object_set (inherited_client,
110 "input_protocol", protocol,
111 "output_protocol", protocol,
112 NULL);
113
114 g_object_get (inherited_client,
115 "input_protocol", &input_protocol,
116 "output_protocol", &output_protocol,
117 NULL);
118
119 assert (input_protocol == G_OBJECT(protocol));
120 assert (output_protocol == G_OBJECT(protocol));
121
122 g_object_unref (output_protocol);
123 g_object_unref (input_protocol);
124 g_object_unref (inherited_client);
125 g_object_unref (protocol);
126}
127
Roger Meierc1010922010-11-26 10:17:48 +0000128int
129main(int argc, char *argv[])
130{
131 g_type_init();
132 g_test_init (&argc, &argv, NULL);
133
Roger Meier1d7e35a2014-07-22 21:56:12 +0200134 g_test_add_func ("/testdebugproto/DebugProto/Structs", test_structs);
135 g_test_add_func ("/testdebugproto/DebugProto/ServiceInheritance",
136 test_service_inheritance);
Roger Meierc1010922010-11-26 10:17:48 +0000137
138 return g_test_run ();
139}
140
141