Roger Meier | 213a664 | 2010-10-27 12:30:11 +0000 | [diff] [blame^] | 1 | #include <assert.h> |
| 2 | #include <glib.h> |
| 3 | |
| 4 | #include "thrift_struct.h" |
| 5 | #include "protocol/thrift_protocol.h" |
| 6 | #include "protocol/thrift_binary_protocol.h" |
| 7 | #include "transport/thrift_memory_buffer.h" |
| 8 | #include "gen-c_glib/t_test_optional_required_test_types.h" |
| 9 | |
| 10 | #include "gen-c_glib/t_test_optional_required_test_types.c" |
| 11 | |
| 12 | static void |
| 13 | write_to_read (ThriftStruct *w, ThriftStruct *r, GError **write_error, |
| 14 | GError **read_error) |
| 15 | { |
| 16 | ThriftMemoryBuffer *tbuffer = NULL; |
| 17 | ThriftProtocol *protocol = NULL; |
| 18 | |
| 19 | tbuffer = g_object_new (THRIFT_TYPE_MEMORY_BUFFER, NULL); |
| 20 | protocol = g_object_new (THRIFT_TYPE_BINARY_PROTOCOL, "transport", |
| 21 | tbuffer, NULL); |
| 22 | |
| 23 | thrift_struct_write (w, protocol, write_error); |
| 24 | thrift_struct_read (r, protocol, read_error); |
| 25 | |
| 26 | g_object_unref (protocol); |
| 27 | g_object_unref (tbuffer); |
| 28 | } |
| 29 | |
| 30 | static void |
| 31 | test_old_school1 (void) |
| 32 | { |
| 33 | TTestOldSchool *o = NULL; |
| 34 | |
| 35 | o = g_object_new (T_TEST_TYPE_OLD_SCHOOL, NULL); |
| 36 | o->im_int = 10; |
| 37 | o->im_str = g_strdup ("test"); |
| 38 | o->im_big = g_ptr_array_new (); |
| 39 | g_ptr_array_free (o->im_big, FALSE); |
| 40 | g_free (o->im_str); |
| 41 | g_object_unref (o); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Write to read with optional fields |
| 46 | */ |
| 47 | static void |
| 48 | test_simple (void) |
| 49 | { |
| 50 | TTestSimple *s1 = NULL, *s2 = NULL, *s3 = NULL; |
| 51 | |
| 52 | s1 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 53 | s2 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 54 | s3 = g_object_new (T_TEST_TYPE_SIMPLE, NULL); |
| 55 | |
| 56 | // write-to-read with optional fields |
| 57 | s1->im_optional = 10; |
| 58 | assert (s1->__isset_im_default == FALSE); |
| 59 | assert (s1->__isset_im_optional == FALSE); |
| 60 | write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s2), NULL, NULL); |
| 61 | assert (s2->__isset_im_default = TRUE); |
| 62 | assert (s2->__isset_im_optional == FALSE); |
| 63 | assert (s2->im_optional == 0); |
| 64 | |
| 65 | s1->__isset_im_optional = TRUE; |
| 66 | write_to_read (THRIFT_STRUCT (s1), THRIFT_STRUCT (s3), NULL, NULL); |
| 67 | assert (s3->__isset_im_default == TRUE); |
| 68 | assert (s3->__isset_im_optional == TRUE); |
| 69 | assert (s3->im_optional == 10); |
| 70 | |
| 71 | g_object_unref (s1); |
| 72 | g_object_unref (s2); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Writing between optional and default |
| 77 | */ |
| 78 | static void |
| 79 | test_tricky1 (void) |
| 80 | { |
| 81 | TTestTricky1 *t1 = NULL; |
| 82 | TTestTricky2 *t2 = NULL; |
| 83 | |
| 84 | t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL); |
| 85 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 86 | |
| 87 | t2->im_optional = 10; |
| 88 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t1), NULL, NULL); |
| 89 | write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t2), NULL, NULL); |
| 90 | |
| 91 | assert (t1->__isset_im_default == FALSE); |
| 92 | assert (t2->__isset_im_optional == TRUE); |
| 93 | assert (t1->im_default == t2->im_optional); |
| 94 | assert (t1->im_default == 0); |
| 95 | |
| 96 | g_object_unref (t1); |
| 97 | g_object_unref (t2); |
| 98 | } |
| 99 | |
| 100 | /** |
| 101 | * Writing between default and required. |
| 102 | */ |
| 103 | static void |
| 104 | test_tricky2 (void) |
| 105 | { |
| 106 | TTestTricky1 *t1 = NULL; |
| 107 | TTestTricky3 *t3 = NULL; |
| 108 | |
| 109 | t1 = g_object_new (T_TEST_TYPE_TRICKY1, NULL); |
| 110 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 111 | |
| 112 | write_to_read (THRIFT_STRUCT (t1), THRIFT_STRUCT (t3), NULL, NULL); |
| 113 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t1), NULL, NULL); |
| 114 | |
| 115 | assert (t1->__isset_im_default == TRUE); |
| 116 | |
| 117 | g_object_unref (t1); |
| 118 | g_object_unref (t3); |
| 119 | } |
| 120 | |
| 121 | /** |
| 122 | * Writing between optional and required. |
| 123 | */ |
| 124 | static void |
| 125 | test_tricky3 (void) |
| 126 | { |
| 127 | TTestTricky2 *t2 = NULL; |
| 128 | TTestTricky3 *t3 = NULL; |
| 129 | |
| 130 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 131 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 132 | |
| 133 | t2->__isset_im_optional = TRUE; |
| 134 | |
| 135 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, NULL); |
| 136 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL); |
| 137 | |
| 138 | g_object_unref (t2); |
| 139 | g_object_unref (t3); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Catch an optional not set exception. To quote the |
| 144 | * C++ test, "Mu-hu-ha-ha-ha!" |
| 145 | */ |
| 146 | static void |
| 147 | test_tricky4 (void) |
| 148 | { |
| 149 | TTestTricky2 *t2 = NULL; |
| 150 | TTestTricky3 *t3 = NULL; |
| 151 | GError *read_error = NULL; |
| 152 | |
| 153 | t2 = g_object_new (T_TEST_TYPE_TRICKY2, NULL); |
| 154 | t3 = g_object_new (T_TEST_TYPE_TRICKY3, NULL); |
| 155 | |
| 156 | // throws protocol exception |
| 157 | write_to_read (THRIFT_STRUCT (t2), THRIFT_STRUCT (t3), NULL, &read_error); |
| 158 | assert (read_error != NULL); |
| 159 | g_error_free (read_error); |
| 160 | |
| 161 | write_to_read (THRIFT_STRUCT (t3), THRIFT_STRUCT (t2), NULL, NULL); |
| 162 | |
| 163 | assert (t2->__isset_im_optional); |
| 164 | |
| 165 | g_object_unref (t2); |
| 166 | g_object_unref (t3); |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | main(void) |
| 171 | { |
| 172 | g_type_init (); |
| 173 | test_old_school1 (); |
| 174 | test_simple (); |
| 175 | test_tricky1 (); |
| 176 | test_tricky2 (); |
| 177 | test_tricky3 (); |
| 178 | test_tricky4 (); |
| 179 | return 0; |
| 180 | } |
| 181 | |
| 182 | |