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 | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 20 | #include <assert.h> |
| 21 | #include <glib.h> |
| 22 | |
Roger Meier | e3da768 | 2013-01-11 11:41:53 +0100 | [diff] [blame^] | 23 | #include <thrift/c_glib/thrift_struct.h> |
| 24 | #include <thrift/c_glib/protocol/thrift_protocol.h> |
| 25 | #include <thrift/c_glib/protocol/thrift_binary_protocol.h> |
| 26 | #include <thrift/c_glib/transport/thrift_memory_buffer.h> |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 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 | |
| 31 | static void |
| 32 | write_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 | |
| 49 | static void |
| 50 | test_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 (); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 58 | g_ptr_array_free (o->im_big, TRUE); |
| 59 | o->im_big = NULL; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 60 | g_free (o->im_str); |
Roger Meier | c75797d | 2012-04-28 11:33:58 +0000 | [diff] [blame] | 61 | o->im_str = NULL; |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 62 | g_object_unref (o); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Write to read with optional fields |
| 67 | */ |
| 68 | static void |
| 69 | test_simple (void) |
| 70 | { |
| 71 | TTestSimple *s1 = NULL, *s2 = NULL, *s3 = NULL; |
| 72 | |
| 73 | s1 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 74 | s2 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 75 | s3 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 76 | |
| 77 | // write-to-read with optional fields |
| 78 | s1->im_optional = 10; |
| 79 | assert (s1->__isset_im_default == FALSE); |
| 80 | assert (s1->__isset_im_optional == FALSE); |
| 81 | write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL); |
| 82 | assert (s2->__isset_im_default = TRUE); |
| 83 | assert (s2->__isset_im_optional == FALSE); |
| 84 | assert (s2->im_optional == 0); |
| 85 | |
| 86 | s1->__isset_im_optional = TRUE; |
| 87 | write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL); |
| 88 | assert (s3->__isset_im_default == TRUE); |
| 89 | assert (s3->__isset_im_optional == TRUE); |
| 90 | assert (s3->im_optional == 10); |
| 91 | |
| 92 | g_object_unref (s1); |
| 93 | g_object_unref (s2); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Writing between optional and default |
| 98 | */ |
| 99 | static void |
| 100 | test_tricky1 (void) |
| 101 | { |
| 102 | TTestTricky1 *t1 = NULL; |
| 103 | TTestTricky2 *t2 = NULL; |
| 104 | |
| 105 | t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL); |
| 106 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 107 | |
| 108 | t2->im_optional = 10; |
| 109 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL); |
| 110 | write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL); |
| 111 | |
| 112 | assert (t1->__isset_im_default == FALSE); |
| 113 | assert (t2->__isset_im_optional == TRUE); |
| 114 | assert (t1->im_default == t2->im_optional); |
| 115 | assert (t1->im_default == 0); |
| 116 | |
| 117 | g_object_unref (t1); |
| 118 | g_object_unref (t2); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Writing between default and required. |
| 123 | */ |
| 124 | static void |
| 125 | test_tricky2 (void) |
| 126 | { |
| 127 | TTestTricky1 *t1 = NULL; |
| 128 | TTestTricky3 *t3 = NULL; |
| 129 | |
| 130 | t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL); |
| 131 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 132 | |
| 133 | write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL); |
| 134 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL); |
| 135 | |
| 136 | assert (t1->__isset_im_default == TRUE); |
| 137 | |
| 138 | g_object_unref (t1); |
| 139 | g_object_unref (t3); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Writing between optional and required. |
| 144 | */ |
| 145 | static void |
| 146 | test_tricky3 (void) |
| 147 | { |
| 148 | TTestTricky2 *t2 = NULL; |
| 149 | TTestTricky3 *t3 = NULL; |
| 150 | |
| 151 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 152 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 153 | |
| 154 | t2->__isset_im_optional = TRUE; |
| 155 | |
| 156 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, NULL); |
| 157 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL); |
| 158 | |
| 159 | g_object_unref (t2); |
| 160 | g_object_unref (t3); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Catch an optional not set exception. To quote the |
| 165 | * C++ test, "Mu-hu-ha-ha-ha!" |
| 166 | */ |
| 167 | static void |
| 168 | test_tricky4 (void) |
| 169 | { |
| 170 | TTestTricky2 *t2 = NULL; |
| 171 | TTestTricky3 *t3 = NULL; |
| 172 | GError *read_error = NULL; |
| 173 | |
| 174 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 175 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 176 | |
| 177 | // throws protocol exception |
| 178 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error); |
| 179 | assert (read_error != NULL); |
| 180 | g_error_free (read_error); |
| 181 | |
| 182 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL); |
| 183 | |
| 184 | assert (t2->__isset_im_optional); |
| 185 | |
| 186 | g_object_unref (t2); |
| 187 | g_object_unref (t3); |
| 188 | } |
| 189 | |
| 190 | int |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 191 | main(int argc, char *argv[]) |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 192 | { |
Roger Meier | c101092 | 2010-11-26 10:17:48 +0000 | [diff] [blame] | 193 | g_type_init(); |
| 194 | g_test_init (&argc, &argv, NULL); |
| 195 | |
| 196 | g_test_add_func ("/testoptionalrequired/OldSchool", test_old_school1); |
| 197 | g_test_add_func ("/testoptionalrequired/Simple", test_simple); |
| 198 | g_test_add_func ("/testoptionalrequired/Tricky1", test_tricky1); |
| 199 | g_test_add_func ("/testoptionalrequired/Tricky2", test_tricky2); |
| 200 | g_test_add_func ("/testoptionalrequired/Tricky3", test_tricky3); |
| 201 | g_test_add_func ("/testoptionalrequired/Tricky4", test_tricky4); |
| 202 | |
| 203 | return g_test_run (); |
Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame] | 204 | } |