Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [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 | |
| 20 | #include <glib-object.h> |
| 21 | #include <inttypes.h> |
| 22 | #include <signal.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | |
| 26 | #include <sys/time.h> |
| 27 | |
| 28 | #include <thrift/c_glib/thrift.h> |
| 29 | #include <thrift/c_glib/protocol/thrift_binary_protocol.h> |
| 30 | #include <thrift/c_glib/transport/thrift_buffered_transport.h> |
| 31 | #include <thrift/c_glib/transport/thrift_framed_transport.h> |
| 32 | #include <thrift/c_glib/transport/thrift_socket.h> |
| 33 | #include <thrift/c_glib/transport/thrift_transport.h> |
| 34 | |
| 35 | #include "../gen-c_glib/t_test_thrift_test.h" |
| 36 | |
| 37 | /* Handle SIGPIPE signals (indicating the server has closed the |
| 38 | connection prematurely) by outputting an error message before |
| 39 | exiting. */ |
| 40 | static void |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 41 | sigpipe_handler (int signal_number) |
| 42 | { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 43 | THRIFT_UNUSED_VAR (signal_number); |
| 44 | |
| 45 | /* Flush standard output to make sure the test results so far are |
| 46 | logged */ |
| 47 | fflush (stdout); |
| 48 | |
| 49 | fputs ("Broken pipe (server closed connection prematurely)\n", stderr); |
| 50 | fflush (stderr); |
| 51 | |
| 52 | /* Re-raise the signal, this time invoking the default signal |
| 53 | handler, to terminate the program */ |
| 54 | raise (SIGPIPE); |
| 55 | } |
| 56 | |
| 57 | /* Compare two gint32 values. Used for sorting and finding integer |
| 58 | values within a GList. */ |
| 59 | static gint |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 60 | gint32_compare (gconstpointer a, gconstpointer b) |
| 61 | { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 62 | gint32 int32_a = *(gint32 *)a; |
| 63 | gint32 int32_b = *(gint32 *)b; |
| 64 | int result = 0; |
| 65 | |
| 66 | if (int32_a < int32_b) |
| 67 | result = -1; |
| 68 | else if (int32_a > int32_b) |
| 69 | result = 1; |
| 70 | |
| 71 | return result; |
| 72 | } |
| 73 | |
| 74 | int |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 75 | main (int argc, char **argv) |
| 76 | { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 77 | static gchar *host = NULL; |
| 78 | static gint port = 9090; |
| 79 | static gchar *transport_option = NULL; |
| 80 | static gchar *protocol_option = NULL; |
| 81 | static gint num_tests = 1; |
| 82 | |
| 83 | static |
| 84 | GOptionEntry option_entries[] ={ |
| 85 | { "host", 0, 0, G_OPTION_ARG_STRING, &host, |
| 86 | "Host to connect (=localhost)", NULL }, |
| 87 | { "port", 0, 0, G_OPTION_ARG_INT, &port, |
| 88 | "Port number to connect (=9090)", NULL }, |
| 89 | { "transport", 0, 0, G_OPTION_ARG_STRING, &transport_option, |
| 90 | "Transport: buffered, framed (=buffered)", NULL }, |
| 91 | { "protocol", 0, 0, G_OPTION_ARG_STRING, &protocol_option, |
| 92 | "Protocol: binary (=binary)", NULL }, |
| 93 | { "testloops", 'n', 0, G_OPTION_ARG_INT, &num_tests, |
| 94 | "Number of tests (=1)", NULL }, |
| 95 | { NULL } |
| 96 | }; |
| 97 | |
| 98 | struct sigaction sigpipe_action; |
| 99 | |
| 100 | GType transport_type = THRIFT_TYPE_BUFFERED_TRANSPORT; |
| 101 | gchar *transport_name = "buffered"; |
| 102 | GType protocol_type = THRIFT_TYPE_BINARY_PROTOCOL; |
| 103 | gchar *protocol_name = "binary"; |
| 104 | |
| 105 | ThriftSocket *socket; |
| 106 | ThriftTransport *transport; |
| 107 | ThriftProtocol *protocol; |
| 108 | |
| 109 | TTestThriftTestIf *test_client; |
| 110 | |
| 111 | struct timeval time_start, time_stop, time_elapsed; |
| 112 | guint64 time_elapsed_usec, time_total_usec = 0; |
| 113 | guint64 time_min_usec = G_MAXUINT64, time_max_usec = 0, time_avg_usec; |
| 114 | |
| 115 | GOptionContext *option_context; |
| 116 | gboolean options_valid = TRUE; |
| 117 | int test_num = 0; |
| 118 | int fail_count = 0; |
| 119 | GError *error = NULL; |
| 120 | |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 121 | #if (!GLIB_CHECK_VERSION (2, 36, 0)) |
| 122 | g_type_init (); |
| 123 | #endif |
| 124 | |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 125 | /* Configure and parse our command-line options */ |
| 126 | option_context = g_option_context_new (NULL); |
| 127 | g_option_context_add_main_entries (option_context, |
| 128 | option_entries, |
| 129 | NULL); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 130 | if (!g_option_context_parse (option_context, |
| 131 | &argc, |
| 132 | &argv, |
| 133 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 134 | fprintf (stderr, "%s\n", error->message); |
| 135 | return 255; |
| 136 | } |
| 137 | g_option_context_free (option_context); |
| 138 | |
| 139 | /* Set remaining default values for unspecified options */ |
| 140 | if (host == NULL) |
| 141 | host = g_strdup ("localhost"); |
| 142 | |
| 143 | /* Validate the parsed options */ |
| 144 | if (protocol_option != NULL && |
| 145 | strncmp (protocol_option, "binary", 7) != 0) { |
| 146 | fprintf (stderr, "Unknown protocol type %s\n", protocol_option); |
| 147 | options_valid = FALSE; |
| 148 | } |
| 149 | |
| 150 | if (transport_option != NULL) { |
| 151 | if (strncmp (transport_option, "framed", 7) == 0) { |
| 152 | transport_type = THRIFT_TYPE_FRAMED_TRANSPORT; |
| 153 | transport_name = "framed"; |
| 154 | } |
| 155 | else if (strncmp (transport_option, "buffered", 9) != 0) { |
| 156 | fprintf (stderr, "Unknown transport type %s\n", transport_option); |
| 157 | options_valid = FALSE; |
| 158 | } |
| 159 | } |
| 160 | |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 161 | if (!options_valid) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 162 | return 254; |
| 163 | |
| 164 | printf ("Connecting (%s/%s) to: %s:%d\n", |
| 165 | transport_name, |
| 166 | protocol_name, |
| 167 | host, |
| 168 | port); |
| 169 | |
| 170 | /* Install our SIGPIPE handler, which outputs an error message to |
| 171 | standard error before exiting so testers can know what |
| 172 | happened */ |
| 173 | memset (&sigpipe_action, 0, sizeof (sigpipe_action)); |
| 174 | sigpipe_action.sa_handler = sigpipe_handler; |
| 175 | sigpipe_action.sa_flags = SA_RESETHAND; |
| 176 | sigaction (SIGPIPE, &sigpipe_action, NULL); |
| 177 | |
| 178 | /* Establish all our connection objects */ |
| 179 | socket = g_object_new (THRIFT_TYPE_SOCKET, |
| 180 | "hostname", host, |
| 181 | "port", port, |
| 182 | NULL); |
| 183 | transport = g_object_new (transport_type, |
| 184 | "transport", socket, |
| 185 | NULL); |
| 186 | protocol = g_object_new (protocol_type, |
| 187 | "transport", transport, |
| 188 | NULL); |
| 189 | test_client = g_object_new (T_TEST_TYPE_THRIFT_TEST_CLIENT, |
| 190 | "input_protocol", protocol, |
| 191 | "output_protocol", protocol, |
| 192 | NULL); |
| 193 | |
| 194 | /* Execute the actual tests */ |
| 195 | for (test_num = 0; test_num < num_tests; ++test_num) { |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 196 | if (thrift_transport_open (transport, &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 197 | gchar *string = NULL; |
| 198 | gint8 byte = 0; |
| 199 | gint32 int32 = 0; |
| 200 | gint64 int64 = 0; |
| 201 | gdouble dub = 0; |
| 202 | |
| 203 | gint byte_thing, i32_thing, inner_byte_thing, inner_i32_thing; |
| 204 | gint64 i64_thing, inner_i64_thing; |
| 205 | |
| 206 | TTestXtruct *xtruct_out, *xtruct_in, *inner_xtruct_in; |
| 207 | TTestXtruct2 *xtruct2_out, *xtruct2_in; |
| 208 | |
| 209 | GHashTable *map_out, *map_in, *inner_map_in; |
| 210 | GHashTable *set_out, *set_in; |
| 211 | gpointer key, value; |
| 212 | gint32 *i32_key_ptr, *i32_value_ptr; |
| 213 | GHashTableIter hash_table_iter, inner_hash_table_iter; |
| 214 | GList *keys_out, *keys_in, *keys_elem; |
| 215 | |
| 216 | GArray *list_out, *list_in; |
| 217 | |
| 218 | TTestNumberz numberz; |
| 219 | |
| 220 | TTestUserId user_id, *user_id_ptr; |
| 221 | |
| 222 | TTestInsanity *insanity_out, *insanity_in; |
| 223 | GHashTable *user_map; |
| 224 | GHashTableIter user_map_iter; |
| 225 | GPtrArray *xtructs; |
| 226 | |
| 227 | TTestXception *xception = NULL; |
| 228 | TTestXception2 *xception2 = NULL; |
| 229 | |
| 230 | gboolean oneway_result; |
| 231 | struct timeval oneway_start, oneway_end, oneway_elapsed; |
| 232 | gint oneway_elapsed_usec; |
| 233 | |
| 234 | gboolean first; |
| 235 | gint32 i, j; |
| 236 | |
| 237 | printf ("Test #%d, connect %s:%d\n", test_num + 1, host, port); |
| 238 | gettimeofday (&time_start, NULL); |
| 239 | |
| 240 | /* These test routines have been ported from the C++ test |
| 241 | client, care being taken to ensure their output remains as |
| 242 | close as possible to the original to facilitate diffs. |
| 243 | |
| 244 | For simplicity comments have been omitted, but every routine |
| 245 | has the same basic structure: |
| 246 | |
| 247 | - Create and populate data structures as necessary. |
| 248 | |
| 249 | - Format and output (to the console) a representation of the |
| 250 | outgoing data. |
| 251 | |
| 252 | - Issue the remote method call to the server. |
| 253 | |
| 254 | - Format and output a representation of the returned data. |
| 255 | |
| 256 | - Verify the returned data matches what was expected. |
| 257 | |
| 258 | - Deallocate any created data structures. |
| 259 | |
| 260 | Note the recognized values and expected behaviour of each |
| 261 | remote method are described in ThriftTest.thrift, which |
| 262 | you'll find in the top-level "test" folder. */ |
| 263 | |
| 264 | /** |
| 265 | * VOID TEST |
| 266 | */ |
| 267 | printf ("testVoid()"); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 268 | if (t_test_thrift_test_if_test_void (test_client, &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 269 | printf (" = void\n"); |
| 270 | } |
| 271 | else { |
| 272 | printf ("%s\n", error->message); |
| 273 | g_error_free (error); |
| 274 | error = NULL; |
| 275 | |
| 276 | fail_count++; |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * STRING TEST |
| 281 | */ |
| 282 | printf ("testString(\"Test\")"); |
| 283 | if (t_test_thrift_test_if_test_string (test_client, |
| 284 | &string, |
| 285 | "Test", |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 286 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 287 | printf (" = \"%s\"\n", string); |
| 288 | if (strncmp (string, "Test", 5) != 0) |
| 289 | fail_count++; |
| 290 | |
| 291 | g_free (string); |
| 292 | string = NULL; |
| 293 | } |
| 294 | else { |
| 295 | printf ("%s\n", error->message); |
| 296 | g_error_free (error); |
| 297 | error = NULL; |
| 298 | |
| 299 | fail_count++; |
| 300 | } |
| 301 | |
| 302 | /** |
| 303 | * BYTE TEST |
| 304 | */ |
| 305 | printf ("testByte(1)"); |
| 306 | if (t_test_thrift_test_if_test_byte (test_client, |
| 307 | &byte, |
| 308 | 1, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 309 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 310 | printf (" = %d\n", byte); |
| 311 | if (byte != 1) |
| 312 | fail_count++; |
| 313 | } |
| 314 | else { |
| 315 | printf ("%s\n", error->message); |
| 316 | g_error_free (error); |
| 317 | error = NULL; |
| 318 | |
| 319 | fail_count++; |
| 320 | } |
| 321 | |
| 322 | /** |
| 323 | * I32 TEST |
| 324 | */ |
| 325 | printf ("testI32(-1)"); |
| 326 | if (t_test_thrift_test_if_test_i32 (test_client, |
| 327 | &int32, |
| 328 | -1, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 329 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 330 | printf (" = %d\n", int32); |
| 331 | if (int32 != -1) |
| 332 | fail_count++; |
| 333 | } |
| 334 | else { |
| 335 | printf ("%s\n", error->message); |
| 336 | g_error_free (error); |
| 337 | error = NULL; |
| 338 | |
| 339 | fail_count++; |
| 340 | } |
| 341 | |
| 342 | /** |
| 343 | * I64 TEST |
| 344 | */ |
| 345 | printf ("testI64(-34359738368)"); |
| 346 | if (t_test_thrift_test_if_test_i64 (test_client, |
| 347 | &int64, |
| 348 | (gint64)-34359738368, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 349 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 350 | printf (" = %" PRId64 "\n", int64); |
| 351 | if (int64 != (gint64)-34359738368) |
| 352 | fail_count++; |
| 353 | } |
| 354 | else { |
| 355 | printf ("%s\n", error->message); |
| 356 | g_error_free (error); |
| 357 | error = NULL; |
| 358 | |
| 359 | fail_count++; |
| 360 | } |
| 361 | |
| 362 | /** |
| 363 | * DOUBLE TEST |
| 364 | */ |
| 365 | printf("testDouble(-5.2098523)"); |
| 366 | if (t_test_thrift_test_if_test_double (test_client, |
| 367 | &dub, |
| 368 | -5.2098523, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 369 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 370 | printf (" = %f\n", dub); |
| 371 | if ((dub - (-5.2098523)) > 0.001) |
| 372 | fail_count++; |
| 373 | } |
| 374 | else { |
| 375 | printf ("%s\n", error->message); |
| 376 | g_error_free (error); |
| 377 | error = NULL; |
| 378 | |
| 379 | fail_count++; |
| 380 | } |
| 381 | |
| 382 | /** |
| 383 | * STRUCT TEST |
| 384 | */ |
| 385 | printf ("testStruct({\"Zero\", 1, -3, -5})"); |
| 386 | xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT, |
| 387 | "string_thing", "Zero", |
| 388 | "byte_thing", 1, |
| 389 | "i32_thing", -3, |
| 390 | "i64_thing", -5LL, |
| 391 | NULL); |
| 392 | xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL); |
| 393 | |
| 394 | if (t_test_thrift_test_if_test_struct (test_client, |
| 395 | &xtruct_in, |
| 396 | xtruct_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 397 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 398 | g_object_get (xtruct_in, |
| 399 | "string_thing", &string, |
| 400 | "byte_thing", &byte_thing, |
| 401 | "i32_thing", &i32_thing, |
| 402 | "i64_thing", &i64_thing, |
| 403 | NULL); |
| 404 | |
| 405 | printf (" = {\"%s\", %d, %d, %" PRId64 "}\n", |
| 406 | string, |
| 407 | byte_thing, |
| 408 | i32_thing, |
| 409 | i64_thing); |
| 410 | if ((string == NULL || strncmp (string, "Zero", 5) != 0) || |
| 411 | byte_thing != 1 || |
| 412 | i32_thing != -3 || |
| 413 | i64_thing != (gint64)-5) |
| 414 | fail_count++; |
| 415 | } |
| 416 | else { |
| 417 | printf ("%s\n", error->message); |
| 418 | g_error_free (error); |
| 419 | error = NULL; |
| 420 | |
| 421 | fail_count++; |
| 422 | } |
| 423 | g_object_unref (xtruct_in); |
| 424 | |
| 425 | /** |
| 426 | * NESTED STRUCT TEST |
| 427 | */ |
| 428 | printf ("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
| 429 | xtruct2_out = g_object_new (T_TEST_TYPE_XTRUCT2, |
| 430 | "byte_thing", 1, |
| 431 | "struct_thing", xtruct_out, |
| 432 | "i32_thing", 5, |
| 433 | NULL); |
| 434 | xtruct2_in = g_object_new (T_TEST_TYPE_XTRUCT2, NULL); |
| 435 | |
| 436 | if (t_test_thrift_test_if_test_nest (test_client, |
| 437 | &xtruct2_in, |
| 438 | xtruct2_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 439 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 440 | g_object_get (xtruct2_in, |
| 441 | "byte_thing", &byte_thing, |
| 442 | "struct_thing", &xtruct_in, |
| 443 | "i32_thing", &i32_thing, |
| 444 | NULL); |
| 445 | g_object_get (xtruct_in, |
| 446 | "string_thing", &string, |
| 447 | "byte_thing", &inner_byte_thing, |
| 448 | "i32_thing", &inner_i32_thing, |
| 449 | "i64_thing", &inner_i64_thing, |
| 450 | NULL); |
| 451 | |
| 452 | printf (" = {%d, {\"%s\", %d, %d, %" PRId64 "}, %d}\n", |
| 453 | byte_thing, |
| 454 | string, |
| 455 | inner_byte_thing, |
| 456 | inner_i32_thing, |
| 457 | inner_i64_thing, |
| 458 | i32_thing); |
| 459 | if (byte_thing != 1 || |
| 460 | (string == NULL || strncmp (string, "Zero", 5) != 0) || |
| 461 | inner_byte_thing != 1 || |
| 462 | inner_i32_thing != -3 || |
| 463 | inner_i64_thing != (gint64)-5 || |
| 464 | i32_thing != 5) |
| 465 | fail_count++; |
| 466 | } |
| 467 | else { |
| 468 | printf ("%s\n", error->message); |
| 469 | g_error_free (error); |
| 470 | error = NULL; |
| 471 | |
| 472 | fail_count++; |
| 473 | } |
| 474 | |
| 475 | g_object_unref (xtruct_in); |
| 476 | g_object_unref (xtruct2_in); |
| 477 | g_object_unref (xtruct2_out); |
| 478 | g_object_unref (xtruct_out); |
| 479 | |
| 480 | /** |
| 481 | * MAP TEST |
| 482 | */ |
| 483 | map_out = g_hash_table_new_full (g_int_hash, |
| 484 | g_int_equal, |
| 485 | g_free, |
| 486 | g_free); |
| 487 | for (i = 0; i < 5; ++i) { |
| 488 | i32_key_ptr = g_malloc (sizeof *i32_key_ptr); |
| 489 | i32_value_ptr = g_malloc (sizeof *i32_value_ptr); |
| 490 | |
| 491 | *i32_key_ptr = i; |
| 492 | *i32_value_ptr = i - 10; |
| 493 | |
| 494 | g_hash_table_insert (map_out, i32_key_ptr, i32_value_ptr); |
| 495 | } |
| 496 | printf ("testMap({"); |
| 497 | first = TRUE; |
| 498 | g_hash_table_iter_init (&hash_table_iter, map_out); |
| 499 | while (g_hash_table_iter_next (&hash_table_iter, |
| 500 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 501 | &value)) { |
| 502 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 503 | first = FALSE; |
| 504 | else |
| 505 | printf (", "); |
| 506 | |
| 507 | printf ("%d => %d", *(gint32 *)key, *(gint32 *)value); |
| 508 | } |
| 509 | printf ("})"); |
| 510 | |
| 511 | map_in = g_hash_table_new_full (g_int_hash, |
| 512 | g_int_equal, |
| 513 | g_free, |
| 514 | g_free); |
| 515 | |
| 516 | if (t_test_thrift_test_if_test_map (test_client, |
| 517 | &map_in, |
| 518 | map_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 519 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 520 | printf (" = {"); |
| 521 | first = TRUE; |
| 522 | g_hash_table_iter_init (&hash_table_iter, map_in); |
| 523 | while (g_hash_table_iter_next (&hash_table_iter, |
| 524 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 525 | &value)) { |
| 526 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 527 | first = FALSE; |
| 528 | else |
| 529 | printf (", "); |
| 530 | |
| 531 | printf ("%d => %d", *(gint32 *)key, *(gint32 *)value); |
| 532 | } |
| 533 | printf ("}\n"); |
| 534 | |
| 535 | if (g_hash_table_size (map_in) != g_hash_table_size (map_out)) |
| 536 | fail_count++; |
| 537 | else { |
| 538 | g_hash_table_iter_init (&hash_table_iter, map_out); |
| 539 | while (g_hash_table_iter_next (&hash_table_iter, |
| 540 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 541 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 542 | gpointer in_value = g_hash_table_lookup (map_in, key); |
| 543 | if (in_value == NULL || |
| 544 | *(gint32 *)in_value != *(gint32 *)value) { |
| 545 | fail_count++; |
| 546 | break; |
| 547 | } |
| 548 | } |
| 549 | } |
| 550 | } |
| 551 | else { |
| 552 | printf ("%s\n", error->message); |
| 553 | g_error_free (error); |
| 554 | error = NULL; |
| 555 | |
| 556 | fail_count++; |
| 557 | } |
| 558 | |
| 559 | g_hash_table_unref (map_in); |
| 560 | g_hash_table_unref (map_out); |
| 561 | |
| 562 | /** |
| 563 | * STRING MAP TEST |
| 564 | */ |
| 565 | map_out = g_hash_table_new_full (g_str_hash, |
| 566 | g_str_equal, |
| 567 | NULL, |
| 568 | NULL); |
| 569 | g_hash_table_insert (map_out, "a", "2"); |
| 570 | g_hash_table_insert (map_out, "b", "blah"); |
| 571 | g_hash_table_insert (map_out, "some", "thing"); |
| 572 | printf ("testStringMap({"); |
| 573 | first = TRUE; |
| 574 | g_hash_table_iter_init (&hash_table_iter, map_out); |
| 575 | while (g_hash_table_iter_next (&hash_table_iter, |
| 576 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 577 | &value)) { |
| 578 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 579 | first = FALSE; |
| 580 | else |
| 581 | printf (", "); |
| 582 | |
| 583 | printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value); |
| 584 | } |
| 585 | printf (")}"); |
| 586 | |
| 587 | map_in = g_hash_table_new_full (g_str_hash, |
| 588 | g_str_equal, |
| 589 | g_free, |
| 590 | g_free); |
| 591 | |
| 592 | if (t_test_thrift_test_if_test_string_map (test_client, |
| 593 | &map_in, |
| 594 | map_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 595 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 596 | printf (" = {"); |
| 597 | first = TRUE; |
| 598 | g_hash_table_iter_init (&hash_table_iter, map_in); |
| 599 | while (g_hash_table_iter_next (&hash_table_iter, |
| 600 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 601 | &value)) { |
| 602 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 603 | first = FALSE; |
| 604 | else |
| 605 | printf (", "); |
| 606 | |
| 607 | printf ("\"%s\" => \"%s\"", (gchar *)key, (gchar *)value); |
| 608 | } |
| 609 | printf ("}\n"); |
| 610 | |
| 611 | if (g_hash_table_size (map_in) != g_hash_table_size (map_out)) |
| 612 | fail_count++; |
| 613 | else { |
| 614 | g_hash_table_iter_init (&hash_table_iter, map_out); |
| 615 | while (g_hash_table_iter_next (&hash_table_iter, |
| 616 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 617 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 618 | gpointer in_value = g_hash_table_lookup (map_in, key); |
| 619 | if (in_value == NULL || |
| 620 | strcmp ((gchar *)in_value, (gchar *)value) != 0) { |
| 621 | fail_count++; |
| 622 | break; |
| 623 | } |
| 624 | } |
| 625 | } |
| 626 | } |
| 627 | else { |
| 628 | printf ("%s\n", error->message); |
| 629 | g_error_free (error); |
| 630 | error = NULL; |
| 631 | |
| 632 | fail_count++; |
| 633 | } |
| 634 | |
| 635 | g_hash_table_unref (map_in); |
| 636 | g_hash_table_unref (map_out); |
| 637 | |
| 638 | /** |
| 639 | * SET TEST |
| 640 | */ |
| 641 | set_out = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL); |
| 642 | for (i = -2; i < 3; ++i) { |
| 643 | i32_key_ptr = g_malloc (sizeof *i32_key_ptr); |
| 644 | *i32_key_ptr = i; |
| 645 | |
| 646 | g_hash_table_insert (set_out, i32_key_ptr, NULL); |
| 647 | } |
| 648 | printf ("testSet({"); |
| 649 | first = TRUE; |
| 650 | keys_out = g_hash_table_get_keys (set_out); |
| 651 | keys_elem = keys_out; |
| 652 | while (keys_elem != NULL) { |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 653 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 654 | first = FALSE; |
| 655 | else |
| 656 | printf (", "); |
| 657 | |
| 658 | printf ("%d", *(gint32 *)keys_elem->data); |
| 659 | |
| 660 | keys_elem = keys_elem->next; |
| 661 | } |
| 662 | printf ("})"); |
| 663 | |
| 664 | set_in = g_hash_table_new_full (g_int_hash, g_int_equal, g_free, NULL); |
| 665 | |
| 666 | if (t_test_thrift_test_if_test_set (test_client, |
| 667 | &set_in, |
| 668 | set_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 669 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 670 | printf(" = {"); |
| 671 | first = TRUE; |
| 672 | keys_in = g_hash_table_get_keys (set_in); |
| 673 | keys_elem = keys_in; |
| 674 | while (keys_elem != NULL) { |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 675 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 676 | first = FALSE; |
| 677 | else |
| 678 | printf (", "); |
| 679 | |
| 680 | printf ("%d", *(gint32 *)keys_elem->data); |
| 681 | |
| 682 | keys_elem = keys_elem->next; |
| 683 | } |
| 684 | printf ("}\n"); |
| 685 | |
| 686 | if (g_list_length (keys_in) != g_list_length (keys_out)) |
| 687 | fail_count++; |
| 688 | else { |
| 689 | keys_elem = keys_out; |
| 690 | while (keys_elem != NULL) { |
| 691 | if (g_list_find_custom (keys_in, |
| 692 | keys_elem->data, |
| 693 | gint32_compare) == NULL) { |
| 694 | fail_count++; |
| 695 | break; |
| 696 | } |
| 697 | |
| 698 | keys_elem = keys_elem->next; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | g_list_free (keys_in); |
| 703 | } |
| 704 | else { |
| 705 | printf ("%s\n", error->message); |
| 706 | g_error_free (error); |
| 707 | error = NULL; |
| 708 | |
| 709 | fail_count++; |
| 710 | } |
| 711 | |
| 712 | g_hash_table_unref (set_in); |
| 713 | g_list_free (keys_out); |
| 714 | g_hash_table_unref (set_out); |
| 715 | |
| 716 | /** |
| 717 | * LIST TEST |
| 718 | */ |
| 719 | list_out = g_array_new (FALSE, TRUE, sizeof (gint32)); |
| 720 | for (i = -2; i < 3; ++i) { |
| 721 | g_array_append_val (list_out, i); |
| 722 | } |
| 723 | printf ("testList({"); |
| 724 | first = TRUE; |
| 725 | for (i = 0; i < (gint32)list_out->len; ++i) { |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 726 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 727 | first = FALSE; |
| 728 | else |
| 729 | printf (", "); |
| 730 | |
| 731 | printf ("%d", g_array_index (list_out, gint32, i)); |
| 732 | } |
| 733 | printf ("})"); |
| 734 | |
| 735 | list_in = g_array_new (FALSE, TRUE, sizeof (gint32)); |
| 736 | |
| 737 | if (t_test_thrift_test_if_test_list (test_client, |
| 738 | &list_in, |
| 739 | list_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 740 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 741 | printf (" = {"); |
| 742 | first = TRUE; |
| 743 | for (i = 0; i < (gint32)list_in->len; ++i) { |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 744 | if (first) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 745 | first = FALSE; |
| 746 | else |
| 747 | printf (", "); |
| 748 | |
| 749 | printf ("%d", g_array_index (list_in, gint32, i)); |
| 750 | } |
| 751 | printf ("}\n"); |
| 752 | |
| 753 | if (list_in->len != list_out->len || |
| 754 | memcmp (list_in->data, |
| 755 | list_out->data, |
| 756 | list_in->len * sizeof (gint32)) != 0) |
| 757 | fail_count++; |
| 758 | } |
| 759 | else { |
| 760 | printf ("%s\n", error->message); |
| 761 | g_error_free (error); |
| 762 | error = NULL; |
| 763 | |
| 764 | fail_count++; |
| 765 | } |
| 766 | |
| 767 | g_array_unref (list_in); |
| 768 | g_array_unref (list_out); |
| 769 | |
| 770 | /** |
| 771 | * ENUM TEST |
| 772 | */ |
| 773 | printf("testEnum(ONE)"); |
| 774 | if (t_test_thrift_test_if_test_enum (test_client, |
| 775 | &numberz, |
| 776 | T_TEST_NUMBERZ_ONE, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 777 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 778 | printf(" = %d\n", numberz); |
| 779 | if (numberz != T_TEST_NUMBERZ_ONE) |
| 780 | fail_count++; |
| 781 | } |
| 782 | else { |
| 783 | printf ("%s\n", error->message); |
| 784 | g_error_free (error); |
| 785 | error = NULL; |
| 786 | |
| 787 | fail_count++; |
| 788 | } |
| 789 | |
| 790 | printf("testEnum(TWO)"); |
| 791 | if (t_test_thrift_test_if_test_enum (test_client, |
| 792 | &numberz, |
| 793 | T_TEST_NUMBERZ_TWO, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 794 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 795 | printf(" = %d\n", numberz); |
| 796 | if (numberz != T_TEST_NUMBERZ_TWO) |
| 797 | fail_count++; |
| 798 | } |
| 799 | else { |
| 800 | printf ("%s\n", error->message); |
| 801 | g_error_free (error); |
| 802 | error = NULL; |
| 803 | |
| 804 | fail_count++; |
| 805 | } |
| 806 | |
| 807 | printf("testEnum(THREE)"); |
| 808 | if (t_test_thrift_test_if_test_enum (test_client, |
| 809 | &numberz, |
| 810 | T_TEST_NUMBERZ_THREE, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 811 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 812 | printf(" = %d\n", numberz); |
| 813 | if (numberz != T_TEST_NUMBERZ_THREE) |
| 814 | fail_count++; |
| 815 | } |
| 816 | else { |
| 817 | printf ("%s\n", error->message); |
| 818 | g_error_free (error); |
| 819 | error = NULL; |
| 820 | |
| 821 | fail_count++; |
| 822 | } |
| 823 | |
| 824 | printf("testEnum(FIVE)"); |
| 825 | if (t_test_thrift_test_if_test_enum (test_client, |
| 826 | &numberz, |
| 827 | T_TEST_NUMBERZ_FIVE, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 828 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 829 | printf(" = %d\n", numberz); |
| 830 | if (numberz != T_TEST_NUMBERZ_FIVE) |
| 831 | fail_count++; |
| 832 | } |
| 833 | else { |
| 834 | printf ("%s\n", error->message); |
| 835 | g_error_free (error); |
| 836 | error = NULL; |
| 837 | |
| 838 | fail_count++; |
| 839 | } |
| 840 | |
| 841 | printf("testEnum(EIGHT)"); |
| 842 | if (t_test_thrift_test_if_test_enum (test_client, |
| 843 | &numberz, |
| 844 | T_TEST_NUMBERZ_EIGHT, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 845 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 846 | printf(" = %d\n", numberz); |
| 847 | if (numberz != T_TEST_NUMBERZ_EIGHT) |
| 848 | fail_count++; |
| 849 | } |
| 850 | else { |
| 851 | printf ("%s\n", error->message); |
| 852 | g_error_free (error); |
| 853 | error = NULL; |
| 854 | |
| 855 | fail_count++; |
| 856 | } |
| 857 | |
| 858 | /** |
| 859 | * TYPEDEF TEST |
| 860 | */ |
| 861 | printf ("testTypedef(309858235082523)"); |
| 862 | if (t_test_thrift_test_if_test_typedef (test_client, |
| 863 | &user_id, |
| 864 | 309858235082523LL, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 865 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 866 | printf(" = %" PRId64 "\n", user_id); |
| 867 | if (user_id != 309858235082523LL) |
| 868 | fail_count++; |
| 869 | } |
| 870 | else { |
| 871 | printf ("%s\n", error->message); |
| 872 | g_error_free (error); |
| 873 | error = NULL; |
| 874 | |
| 875 | fail_count++; |
| 876 | } |
| 877 | |
| 878 | /** |
| 879 | * NESTED MAP TEST |
| 880 | */ |
| 881 | printf ("testMapMap(1)"); |
| 882 | map_in = g_hash_table_new_full (g_int_hash, |
| 883 | g_int_equal, |
| 884 | g_free, |
| 885 | (GDestroyNotify)g_hash_table_unref); |
| 886 | if (t_test_thrift_test_if_test_map_map (test_client, |
| 887 | &map_in, |
| 888 | 1, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 889 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 890 | g_hash_table_iter_init (&hash_table_iter, map_in); |
| 891 | |
| 892 | printf (" = {"); |
| 893 | while (g_hash_table_iter_next (&hash_table_iter, |
| 894 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 895 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 896 | printf ("%d => {", *(gint32 *)key); |
| 897 | |
| 898 | g_hash_table_iter_init (&inner_hash_table_iter, |
| 899 | (GHashTable *)value); |
| 900 | while (g_hash_table_iter_next (&inner_hash_table_iter, |
| 901 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 902 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 903 | printf ("%d => %d, ", *(gint32 *)key, *(gint32 *)value); |
| 904 | } |
| 905 | |
| 906 | printf ("}, "); |
| 907 | } |
| 908 | printf ("}\n"); |
| 909 | |
| 910 | if (g_hash_table_size (map_in) != 2) |
| 911 | fail_count++; |
| 912 | else { |
| 913 | gint32 inner_keys[] = {1, 2, 3, 4}; |
| 914 | gint32 i32_key; |
| 915 | |
| 916 | i32_key = -4; |
| 917 | inner_map_in = g_hash_table_lookup (map_in, &i32_key); |
| 918 | if (inner_map_in == NULL || |
| 919 | g_hash_table_size (inner_map_in) != 4) |
| 920 | fail_count++; |
| 921 | else { |
| 922 | keys_in = g_hash_table_get_keys (inner_map_in); |
| 923 | keys_in = g_list_sort (keys_in, gint32_compare); |
| 924 | |
| 925 | for (i = 0; i < 4; i++) { |
| 926 | keys_elem = g_list_nth (keys_in, 3 - i); |
| 927 | |
| 928 | if (*(gint32 *)keys_elem->data != (-1 * inner_keys[i]) || |
| 929 | *(gint32 *)g_hash_table_lookup (inner_map_in, |
| 930 | keys_elem->data) != |
| 931 | (-1 * inner_keys[i])) { |
| 932 | fail_count++; |
| 933 | break; |
| 934 | } |
| 935 | } |
| 936 | |
| 937 | g_list_free (keys_in); |
| 938 | } |
| 939 | |
| 940 | i32_key = 4; |
| 941 | inner_map_in = g_hash_table_lookup (map_in, &i32_key); |
| 942 | if (inner_map_in == NULL || |
| 943 | g_hash_table_size (inner_map_in) != 4) |
| 944 | fail_count++; |
| 945 | else { |
| 946 | keys_in = g_hash_table_get_keys (inner_map_in); |
| 947 | keys_in = g_list_sort (keys_in, gint32_compare); |
| 948 | |
| 949 | for (i = 0; i < 4; i++) { |
| 950 | keys_elem = g_list_nth (keys_in, i); |
| 951 | |
| 952 | if (*(gint32 *)keys_elem->data != inner_keys[i] || |
| 953 | *(gint32 *)g_hash_table_lookup (inner_map_in, |
| 954 | keys_elem->data) != |
| 955 | inner_keys[i]) { |
| 956 | fail_count++; |
| 957 | break; |
| 958 | } |
| 959 | } |
| 960 | |
| 961 | g_list_free (keys_in); |
| 962 | } |
| 963 | } |
| 964 | } |
| 965 | else { |
| 966 | printf ("%s\n", error->message); |
| 967 | g_error_free (error); |
| 968 | error = NULL; |
| 969 | |
| 970 | fail_count++; |
| 971 | } |
| 972 | |
| 973 | g_hash_table_unref (map_in); |
| 974 | |
| 975 | /** |
| 976 | * INSANITY TEST |
| 977 | */ |
| 978 | insanity_out = g_object_new (T_TEST_TYPE_INSANITY, NULL); |
| 979 | g_object_get (insanity_out, |
| 980 | "userMap", &user_map, |
| 981 | "xtructs", &xtructs, |
| 982 | NULL); |
| 983 | |
| 984 | numberz = T_TEST_NUMBERZ_FIVE; |
| 985 | user_id_ptr = g_malloc (sizeof *user_id_ptr); |
| 986 | *user_id_ptr = 5000; |
| 987 | g_hash_table_insert (user_map, (gpointer)numberz, user_id_ptr); |
| 988 | g_hash_table_unref (user_map); |
| 989 | |
| 990 | xtruct_out = g_object_new (T_TEST_TYPE_XTRUCT, |
| 991 | "string_thing", "Truck", |
| 992 | "byte_thing", 8, |
| 993 | "i32_thing", 8, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 994 | "i64_thing", 8LL, |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 995 | NULL); |
| 996 | g_ptr_array_add (xtructs, xtruct_out); |
| 997 | g_ptr_array_unref (xtructs); |
| 998 | |
| 999 | map_in = g_hash_table_new_full (g_int64_hash, |
| 1000 | g_int64_equal, |
| 1001 | g_free, |
| 1002 | (GDestroyNotify)g_hash_table_unref); |
| 1003 | |
| 1004 | printf("testInsanity()"); |
| 1005 | if (t_test_thrift_test_if_test_insanity (test_client, |
| 1006 | &map_in, |
| 1007 | insanity_out, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1008 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1009 | printf (" = {"); |
| 1010 | g_hash_table_iter_init (&hash_table_iter, map_in); |
| 1011 | while (g_hash_table_iter_next (&hash_table_iter, |
| 1012 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1013 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1014 | printf ("%" PRId64 " => {", *(TTestUserId *)key); |
| 1015 | |
| 1016 | g_hash_table_iter_init (&inner_hash_table_iter, |
| 1017 | (GHashTable *)value); |
| 1018 | while (g_hash_table_iter_next (&inner_hash_table_iter, |
| 1019 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1020 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1021 | printf ("%d => {", (TTestNumberz)key); |
| 1022 | |
| 1023 | g_object_get ((TTestInsanity *)value, |
| 1024 | "userMap", &user_map, |
| 1025 | "xtructs", &xtructs, |
| 1026 | NULL); |
| 1027 | |
| 1028 | printf ("{"); |
| 1029 | g_hash_table_iter_init (&user_map_iter, user_map); |
| 1030 | while (g_hash_table_iter_next (&user_map_iter, |
| 1031 | &key, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1032 | &value)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1033 | printf ("%d => %" PRId64 ", ", |
| 1034 | (TTestNumberz)key, |
| 1035 | *(TTestUserId *)value); |
| 1036 | } |
| 1037 | printf ("}, "); |
| 1038 | g_hash_table_unref (user_map); |
| 1039 | |
| 1040 | printf("{"); |
| 1041 | for (i = 0; i < (gint32)xtructs->len; ++i) { |
| 1042 | xtruct_in = g_ptr_array_index (xtructs, i); |
| 1043 | g_object_get (xtruct_in, |
| 1044 | "string_thing", &string, |
| 1045 | "byte_thing", &byte_thing, |
| 1046 | "i32_thing", &i32_thing, |
| 1047 | "i64_thing", &i64_thing, |
| 1048 | NULL); |
| 1049 | |
| 1050 | printf ("{\"%s\", %d, %d, %" PRId64 "}, ", |
| 1051 | string, |
| 1052 | byte_thing, |
| 1053 | i32_thing, |
| 1054 | i64_thing); |
| 1055 | } |
| 1056 | printf ("}"); |
| 1057 | g_ptr_array_unref (xtructs); |
| 1058 | |
| 1059 | printf ("}, "); |
| 1060 | } |
| 1061 | printf("}, "); |
| 1062 | } |
| 1063 | printf("}\n"); |
| 1064 | |
| 1065 | if (g_hash_table_size (map_in) != 2) |
| 1066 | fail_count++; |
| 1067 | else { |
| 1068 | TTestNumberz numberz_key_values[] = { |
| 1069 | T_TEST_NUMBERZ_TWO, T_TEST_NUMBERZ_THREE |
| 1070 | }; |
| 1071 | gint user_map_values[] = { 5, 8 }; |
| 1072 | TTestUserId user_id_key; |
| 1073 | |
| 1074 | user_id_key = 1; |
| 1075 | inner_map_in = g_hash_table_lookup (map_in, &user_id_key); |
| 1076 | if (inner_map_in == NULL || |
| 1077 | g_hash_table_size (inner_map_in) != 2) |
| 1078 | fail_count++; |
| 1079 | else { |
| 1080 | TTestNumberz numberz_key; |
| 1081 | |
| 1082 | for (i = 0; i < 2; ++i) { |
| 1083 | numberz_key = numberz_key_values[i]; |
| 1084 | insanity_in = |
| 1085 | g_hash_table_lookup (inner_map_in, |
| 1086 | (gconstpointer)numberz_key); |
| 1087 | if (insanity_in == NULL) |
| 1088 | fail_count++; |
| 1089 | else { |
| 1090 | g_object_get (insanity_in, |
| 1091 | "userMap", &user_map, |
| 1092 | "xtructs", &xtructs, |
| 1093 | NULL); |
| 1094 | |
| 1095 | if (user_map == NULL) |
| 1096 | fail_count++; |
| 1097 | else { |
| 1098 | if (g_hash_table_size (user_map) != 2) |
| 1099 | fail_count++; |
| 1100 | else { |
| 1101 | for (j = 0; j < 2; ++j) { |
| 1102 | numberz_key = (TTestNumberz)user_map_values[j]; |
| 1103 | |
| 1104 | value = |
| 1105 | g_hash_table_lookup (user_map, |
| 1106 | (gconstpointer)numberz_key); |
| 1107 | if (value == NULL || |
| 1108 | *(TTestUserId *)value != (TTestUserId)user_map_values[j]) |
| 1109 | fail_count++; |
| 1110 | } |
| 1111 | } |
| 1112 | |
| 1113 | g_hash_table_unref (user_map); |
| 1114 | } |
| 1115 | |
| 1116 | if (xtructs == NULL) |
| 1117 | fail_count++; |
| 1118 | else { |
| 1119 | if (xtructs->len != 2) |
| 1120 | fail_count++; |
| 1121 | else { |
| 1122 | xtruct_in = g_ptr_array_index (xtructs, 0); |
| 1123 | g_object_get (xtruct_in, |
| 1124 | "string_thing", &string, |
| 1125 | "byte_thing", &byte_thing, |
| 1126 | "i32_thing", &i32_thing, |
| 1127 | "i64_thing", &i64_thing, |
| 1128 | NULL); |
| 1129 | if ((string == NULL || |
| 1130 | strncmp (string, "Goodbye4", 9) != 0) || |
| 1131 | byte_thing != 4 || |
| 1132 | i32_thing != 4 || |
| 1133 | i64_thing != 4) |
| 1134 | fail_count++; |
| 1135 | |
| 1136 | if (string != NULL) |
| 1137 | g_free (string); |
| 1138 | |
| 1139 | xtruct_in = g_ptr_array_index (xtructs, 1); |
| 1140 | g_object_get (xtruct_in, |
| 1141 | "string_thing", &string, |
| 1142 | "byte_thing", &byte_thing, |
| 1143 | "i32_thing", &i32_thing, |
| 1144 | "i64_thing", &i64_thing, |
| 1145 | NULL); |
| 1146 | if ((string == NULL || |
| 1147 | strncmp (string, "Hello2", 7) != 0) || |
| 1148 | byte_thing != 2 || |
| 1149 | i32_thing != 2 || |
| 1150 | i64_thing != 2) |
| 1151 | fail_count++; |
| 1152 | |
| 1153 | if (string != NULL) |
| 1154 | g_free (string); |
| 1155 | } |
| 1156 | |
| 1157 | g_ptr_array_unref (xtructs); |
| 1158 | } |
| 1159 | } |
| 1160 | } |
| 1161 | } |
| 1162 | |
| 1163 | user_id_key = 2; |
| 1164 | inner_map_in = g_hash_table_lookup (map_in, &user_id_key); |
| 1165 | if (inner_map_in == NULL || |
| 1166 | g_hash_table_size (inner_map_in) != 1) |
| 1167 | fail_count++; |
| 1168 | else { |
| 1169 | insanity_in = |
| 1170 | g_hash_table_lookup (inner_map_in, |
| 1171 | (gconstpointer)T_TEST_NUMBERZ_SIX); |
| 1172 | if (insanity_in == NULL) |
| 1173 | fail_count++; |
| 1174 | else { |
| 1175 | g_object_get (insanity_in, |
| 1176 | "userMap", &user_map, |
| 1177 | "xtructs", &xtructs, |
| 1178 | NULL); |
| 1179 | |
| 1180 | if (user_map == NULL) |
| 1181 | fail_count++; |
| 1182 | else { |
| 1183 | if (g_hash_table_size (user_map) != 0) |
| 1184 | fail_count++; |
| 1185 | |
| 1186 | g_hash_table_unref (user_map); |
| 1187 | } |
| 1188 | |
| 1189 | if (xtructs == NULL) |
| 1190 | fail_count++; |
| 1191 | else { |
| 1192 | if (xtructs->len != 0) |
| 1193 | fail_count++; |
| 1194 | |
| 1195 | g_ptr_array_unref (xtructs); |
| 1196 | } |
| 1197 | } |
| 1198 | } |
| 1199 | } |
| 1200 | } |
| 1201 | else { |
| 1202 | printf ("%s\n", error->message); |
| 1203 | g_error_free (error); |
| 1204 | error = NULL; |
| 1205 | |
| 1206 | fail_count++; |
| 1207 | } |
| 1208 | |
| 1209 | g_hash_table_unref (map_in); |
| 1210 | g_object_unref (insanity_out); |
| 1211 | |
| 1212 | /* test exception */ |
| 1213 | printf ("testClient.testException(\"Xception\") =>"); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1214 | if (!t_test_thrift_test_if_test_exception (test_client, |
| 1215 | "Xception", |
| 1216 | &xception, |
| 1217 | &error) && |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1218 | xception != NULL) { |
| 1219 | g_object_get (xception, |
| 1220 | "errorCode", &int32, |
| 1221 | "message", &string, |
| 1222 | NULL); |
| 1223 | printf (" {%u, \"%s\"}\n", int32, string); |
| 1224 | g_free (string); |
| 1225 | |
| 1226 | g_object_unref (xception); |
| 1227 | xception = NULL; |
| 1228 | |
| 1229 | g_error_free (error); |
| 1230 | error = NULL; |
| 1231 | } |
| 1232 | else { |
| 1233 | printf (" void\nFAILURE\n"); |
| 1234 | fail_count++; |
| 1235 | |
| 1236 | if (xception != NULL) { |
| 1237 | g_object_unref (xception); |
| 1238 | xception = NULL; |
| 1239 | } |
| 1240 | |
| 1241 | if (error != NULL) { |
| 1242 | g_error_free (error); |
| 1243 | error = NULL; |
| 1244 | } |
| 1245 | } |
| 1246 | |
| 1247 | printf ("testClient.testException(\"TException\") =>"); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1248 | if (!t_test_thrift_test_if_test_exception (test_client, |
| 1249 | "TException", |
| 1250 | &xception, |
| 1251 | &error) && |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1252 | xception == NULL && |
| 1253 | error != NULL) { |
| 1254 | printf (" Caught TException\n"); |
| 1255 | |
| 1256 | g_error_free (error); |
| 1257 | error = NULL; |
| 1258 | } |
| 1259 | else { |
| 1260 | printf (" void\nFAILURE\n"); |
| 1261 | fail_count++; |
| 1262 | |
| 1263 | if (xception != NULL) { |
| 1264 | g_object_unref (xception); |
| 1265 | xception = NULL; |
| 1266 | } |
| 1267 | |
| 1268 | if (error != NULL) { |
| 1269 | g_error_free (error); |
| 1270 | error = NULL; |
| 1271 | } |
| 1272 | } |
| 1273 | |
| 1274 | printf ("testClient.testException(\"success\") =>"); |
| 1275 | if (t_test_thrift_test_if_test_exception (test_client, |
| 1276 | "success", |
| 1277 | &xception, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1278 | &error)) |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1279 | printf (" void\n"); |
| 1280 | else { |
| 1281 | printf (" void\nFAILURE\n"); |
| 1282 | fail_count++; |
| 1283 | |
| 1284 | if (xception != NULL) { |
| 1285 | g_object_unref (xception); |
| 1286 | xception = NULL; |
| 1287 | } |
| 1288 | |
| 1289 | g_error_free (error); |
| 1290 | error = NULL; |
| 1291 | } |
| 1292 | |
| 1293 | g_assert (error == NULL); |
| 1294 | |
| 1295 | /* test multi exception */ |
| 1296 | printf ("testClient.testMultiException(\"Xception\", \"test 1\") =>"); |
| 1297 | xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1298 | if (!t_test_thrift_test_if_test_multi_exception (test_client, |
| 1299 | &xtruct_in, |
| 1300 | "Xception", |
| 1301 | "test 1", |
| 1302 | &xception, |
| 1303 | &xception2, |
| 1304 | &error) && |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1305 | xception != NULL && |
| 1306 | xception2 == NULL) { |
| 1307 | g_object_get (xception, |
| 1308 | "errorCode", &int32, |
| 1309 | "message", &string, |
| 1310 | NULL); |
| 1311 | printf (" {%u, \"%s\"}\n", int32, string); |
| 1312 | g_free (string); |
| 1313 | |
| 1314 | g_object_unref (xception); |
| 1315 | xception = NULL; |
| 1316 | |
| 1317 | g_error_free (error); |
| 1318 | error = NULL; |
| 1319 | } |
| 1320 | else { |
| 1321 | printf (" result\nFAILURE\n"); |
| 1322 | fail_count++; |
| 1323 | |
| 1324 | if (xception != NULL) { |
| 1325 | g_object_unref (xception); |
| 1326 | xception = NULL; |
| 1327 | } |
| 1328 | |
| 1329 | if (xception2 != NULL) { |
| 1330 | g_object_unref (xception2); |
| 1331 | xception = NULL; |
| 1332 | } |
| 1333 | |
| 1334 | if (error != NULL) { |
| 1335 | g_error_free (error); |
| 1336 | error = NULL; |
| 1337 | } |
| 1338 | } |
| 1339 | g_object_unref (xtruct_in); |
| 1340 | |
| 1341 | printf ("testClient.testMultiException(\"Xception2\", \"test 2\") =>"); |
| 1342 | xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL); |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1343 | if (!t_test_thrift_test_if_test_multi_exception (test_client, |
| 1344 | &xtruct_in, |
| 1345 | "Xception2", |
| 1346 | "test 2", |
| 1347 | &xception, |
| 1348 | &xception2, |
| 1349 | &error) && |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1350 | xception == NULL && |
| 1351 | xception2 != NULL) { |
| 1352 | g_object_get (xception2, |
| 1353 | "errorCode", &int32, |
| 1354 | "struct_thing", &inner_xtruct_in, |
| 1355 | NULL); |
| 1356 | g_object_get (inner_xtruct_in, |
| 1357 | "string_thing", &string, |
| 1358 | NULL); |
| 1359 | printf (" {%u, {\"%s\"}}\n", int32, string); |
| 1360 | g_free (string); |
| 1361 | |
| 1362 | g_object_unref (inner_xtruct_in); |
| 1363 | inner_xtruct_in = NULL; |
| 1364 | |
| 1365 | g_object_unref (xception2); |
| 1366 | xception2 = NULL; |
| 1367 | |
| 1368 | g_error_free (error); |
| 1369 | error = NULL; |
| 1370 | } |
| 1371 | else { |
| 1372 | printf (" result\nFAILURE\n"); |
| 1373 | fail_count++; |
| 1374 | |
| 1375 | if (xception != NULL) { |
| 1376 | g_object_unref (xception); |
| 1377 | xception = NULL; |
| 1378 | } |
| 1379 | |
| 1380 | if (xception2 != NULL) { |
| 1381 | g_object_unref (xception2); |
| 1382 | xception = NULL; |
| 1383 | } |
| 1384 | |
| 1385 | if (error != NULL) { |
| 1386 | g_error_free (error); |
| 1387 | error = NULL; |
| 1388 | } |
| 1389 | } |
| 1390 | g_object_unref (xtruct_in); |
| 1391 | |
| 1392 | printf ("testClient.testMultiException(\"success\", \"test 3\") =>"); |
| 1393 | xtruct_in = g_object_new (T_TEST_TYPE_XTRUCT, NULL); |
| 1394 | if (t_test_thrift_test_if_test_multi_exception (test_client, |
| 1395 | &xtruct_in, |
| 1396 | "success", |
| 1397 | "test 3", |
| 1398 | &xception, |
| 1399 | &xception2, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1400 | &error) && |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1401 | xception == NULL && |
| 1402 | xception2 == NULL) { |
| 1403 | g_object_get (xtruct_in, |
| 1404 | "string_thing", &string, |
| 1405 | NULL); |
| 1406 | printf (" {{\"%s\"}}\n", string); |
| 1407 | g_free (string); |
| 1408 | } |
| 1409 | else { |
| 1410 | printf (" result\nFAILURE\n"); |
| 1411 | fail_count++; |
| 1412 | |
| 1413 | if (xception != NULL) { |
| 1414 | g_object_unref (xception); |
| 1415 | xception = NULL; |
| 1416 | } |
| 1417 | |
| 1418 | if (xception2 != NULL) { |
| 1419 | g_object_unref (xception2); |
| 1420 | xception = NULL; |
| 1421 | } |
| 1422 | |
| 1423 | if (error != NULL) { |
| 1424 | g_error_free (error); |
| 1425 | error = NULL; |
| 1426 | } |
| 1427 | } |
| 1428 | g_object_unref (xtruct_in); |
| 1429 | |
| 1430 | /* test oneway void */ |
| 1431 | printf ("testClient.testOneway(1) =>"); |
| 1432 | gettimeofday (&oneway_start, NULL); |
| 1433 | oneway_result = t_test_thrift_test_if_test_oneway (test_client, |
| 1434 | 1, |
| 1435 | &error); |
| 1436 | gettimeofday (&oneway_end, NULL); |
| 1437 | timersub (&oneway_end, &oneway_start, &oneway_elapsed); |
| 1438 | oneway_elapsed_usec = |
| 1439 | oneway_elapsed.tv_sec * 1000 * 1000 + oneway_elapsed.tv_usec; |
| 1440 | |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1441 | if (oneway_result) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1442 | if (oneway_elapsed_usec > 200 * 1000) { |
| 1443 | printf (" FAILURE - took %.2f ms\n", |
| 1444 | (double)oneway_elapsed_usec / 1000.0); |
| 1445 | fail_count++; |
| 1446 | } |
| 1447 | else |
| 1448 | printf (" success - took %.2f ms\n", |
| 1449 | (double)oneway_elapsed_usec / 1000.0); |
| 1450 | } |
| 1451 | else { |
| 1452 | printf ("%s\n", error->message); |
| 1453 | g_error_free (error); |
| 1454 | error = NULL; |
| 1455 | |
| 1456 | fail_count++; |
| 1457 | } |
| 1458 | |
| 1459 | /** |
| 1460 | * redo a simple test after the oneway to make sure we aren't "off by |
| 1461 | * one" -- if the server treated oneway void like normal void, this next |
| 1462 | * test will fail since it will get the void confirmation rather than |
| 1463 | * the correct result. In this circumstance, the client will receive the |
| 1464 | * error: |
| 1465 | * |
| 1466 | * application error: Wrong method name |
| 1467 | */ |
| 1468 | /** |
| 1469 | * I32 TEST |
| 1470 | */ |
| 1471 | printf ("re-test testI32(-1)"); |
| 1472 | if (t_test_thrift_test_if_test_i32 (test_client, |
| 1473 | &int32, |
| 1474 | -1, |
Roger Meier | 15df076 | 2014-09-29 20:50:56 +0200 | [diff] [blame] | 1475 | &error)) { |
Roger Meier | b3c8409 | 2014-09-01 21:53:40 +0200 | [diff] [blame] | 1476 | printf (" = %d\n", int32); |
| 1477 | if (int32 != -1) |
| 1478 | fail_count++; |
| 1479 | } |
| 1480 | else { |
| 1481 | printf ("%s\n", error->message); |
| 1482 | g_error_free (error); |
| 1483 | error = NULL; |
| 1484 | |
| 1485 | fail_count++; |
| 1486 | } |
| 1487 | |
| 1488 | gettimeofday (&time_stop, NULL); |
| 1489 | timersub (&time_stop, &time_start, &time_elapsed); |
| 1490 | time_elapsed_usec = |
| 1491 | time_elapsed.tv_sec * 1000 * 1000 + time_elapsed.tv_usec; |
| 1492 | |
| 1493 | printf("Total time: %" PRIu64 " us\n", time_elapsed_usec); |
| 1494 | |
| 1495 | time_total_usec += time_elapsed_usec; |
| 1496 | if (time_elapsed_usec < time_min_usec) |
| 1497 | time_min_usec = time_elapsed_usec; |
| 1498 | if (time_elapsed_usec > time_max_usec) |
| 1499 | time_max_usec = time_elapsed_usec; |
| 1500 | |
| 1501 | thrift_transport_close (transport, &error); |
| 1502 | } |
| 1503 | else { |
| 1504 | printf ("Connect failed: %s\n", error->message); |
| 1505 | g_error_free (error); |
| 1506 | error = NULL; |
| 1507 | |
| 1508 | return 1; |
| 1509 | } |
| 1510 | } |
| 1511 | |
| 1512 | /* All done---output statistics */ |
| 1513 | puts ("\nAll tests done."); |
| 1514 | |
| 1515 | time_avg_usec = time_total_usec / num_tests; |
| 1516 | |
| 1517 | printf ("Min time: %" PRIu64 " us\n", time_min_usec); |
| 1518 | printf ("Max time: %" PRIu64 " us\n", time_max_usec); |
| 1519 | printf ("Avg time: %" PRIu64 " us\n", time_avg_usec); |
| 1520 | |
| 1521 | g_object_unref (test_client); |
| 1522 | g_object_unref (protocol); |
| 1523 | g_object_unref (transport); |
| 1524 | g_free (host); |
| 1525 | |
| 1526 | return fail_count; |
| 1527 | } |