Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 1 | /* |
| 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 Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 20 | #include <assert.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 21 | #include <math.h> |
| 22 | #include <glib-object.h> |
| 23 | |
| 24 | #ifndef M_PI |
| 25 | #define M_PI 3.1415926535897932385 |
| 26 | #endif |
| 27 | |
Roger Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 28 | #include <thrift/c_glib/protocol/thrift_protocol.h> |
| 29 | #include <thrift/c_glib/protocol/thrift_binary_protocol.h> |
| 30 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 31 | #include "gen-c_glib/t_test_debug_proto_test_types.h" |
Roger Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 32 | #include "gen-c_glib/t_test_srv.h" |
| 33 | #include "gen-c_glib/t_test_inherited.h" |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 34 | |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 35 | static void |
Roger Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 36 | test_structs(void) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 37 | { |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 38 | TTestOneOfEach *ooe = NULL; |
| 39 | TTestNesting *n = NULL; |
| 40 | TTestHolyMoley *hm = NULL; |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 41 | gchar *random = g_strdup("random string"); |
| 42 | gchar *nothing = g_strdup("nothing"); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 43 | |
| 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 Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 52 | ooe->some_characters = g_strdup("Debug THIS!"); |
| 53 | ooe->zomg_unicode = g_strdup("\xd7\n\a\t"); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 54 | |
| 55 | n = g_object_new (T_TEST_TYPE_NESTING, NULL); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 56 | if (n->my_ooe != NULL) |
| 57 | g_object_unref(n->my_ooe); |
| 58 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 59 | 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 Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 64 | 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 Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 66 | n->my_bonk->type = 31337; |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 67 | n->my_bonk->message = g_strdup("I am a bonk... xor!"); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 68 | |
| 69 | hm = g_object_new (T_TEST_TYPE_HOLY_MOLEY, NULL); |
| 70 | g_ptr_array_add (hm->big, ooe); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 71 | g_ptr_array_add (hm->big, g_object_ref(n->my_ooe)); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 72 | ((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 Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 75 | g_hash_table_insert (hm->contain, random, random); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 76 | |
| 77 | TTestBonk *bonk = NULL; |
| 78 | bonk = g_object_new (T_TEST_TYPE_BONK, NULL); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 79 | GPtrArray *bonks = g_ptr_array_new_with_free_func (g_object_unref); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 80 | g_ptr_array_add (bonks, bonk); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 81 | g_hash_table_insert (hm->bonks, nothing, bonks); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 82 | |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 83 | g_object_unref (hm); |
| 84 | |
| 85 | return 0; |
| 86 | } |
| 87 | |
Roger Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 88 | static void |
| 89 | test_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 Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 128 | int |
| 129 | main(int argc, char *argv[]) |
| 130 | { |
| 131 | g_type_init(); |
| 132 | g_test_init (&argc, &argv, NULL); |
| 133 | |
Roger Meier | 1d7e35a | 2014-07-22 21:56:12 +0200 | [diff] [blame^] | 134 | g_test_add_func ("/testdebugproto/DebugProto/Structs", test_structs); |
| 135 | g_test_add_func ("/testdebugproto/DebugProto/ServiceInheritance", |
| 136 | test_service_inheritance); |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 137 | |
| 138 | return g_test_run (); |
| 139 | } |
| 140 | |
| 141 | |