Mark Slee | e854063 | 2006-05-30 09:24:40 +0000 | [diff] [blame^] | 1 | #include <stdio.h> |
| 2 | #include <unistd.h> |
| 3 | #include "protocol/TBinaryProtocol.h" |
| 4 | #include "client/TSimpleClient.h" |
| 5 | #include "transport/TSocket.h" |
| 6 | #include "gen-cpp/ThriftTest.h" |
| 7 | using namespace std; |
| 8 | |
| 9 | int main(int argc, char** argv) { |
| 10 | string host = "localhost"; |
| 11 | int port = 9090; |
| 12 | int numTests = 1; |
| 13 | |
| 14 | if (argc > 1) { |
| 15 | host = argv[1]; |
| 16 | } |
| 17 | if (argc > 2) { |
| 18 | port = atoi(argv[2]); |
| 19 | } |
| 20 | if (argc > 3) { |
| 21 | numTests = atoi(argv[3]); |
| 22 | } |
| 23 | |
| 24 | TSocket socket(host, port); |
| 25 | TSimpleClient simpleClient(&socket); |
| 26 | TBinaryProtocol binaryProtocol; |
| 27 | ThriftTestClient testClient(&simpleClient, &binaryProtocol); |
| 28 | |
| 29 | int test = 0; |
| 30 | for (test = 0; test < numTests; ++test) { |
| 31 | printf("Test #%d, connect %s:%d\n", test+1, host.c_str(), port); |
| 32 | bool success = simpleClient.open(); |
| 33 | if (!success) { |
| 34 | printf("Connect failed, server down?\n"); |
| 35 | continue; |
| 36 | } |
| 37 | |
| 38 | /** |
| 39 | * VOID TEST |
| 40 | */ |
| 41 | printf("testVoid()"); |
| 42 | testClient.testVoid(); |
| 43 | printf(" = void\n"); |
| 44 | |
| 45 | /** |
| 46 | * STRING TEST |
| 47 | */ |
| 48 | printf("testString(\"Test\")"); |
| 49 | string s = testClient.testString("Test"); |
| 50 | printf(" = \"%s\"\n", s.c_str()); |
| 51 | |
| 52 | /** |
| 53 | * BYTE TEST |
| 54 | */ |
| 55 | printf("testByte(1)"); |
| 56 | uint8_t u8 = testClient.testByte(1); |
| 57 | printf(" = %d\n", (int)u8); |
| 58 | |
| 59 | /** |
| 60 | * U32 TEST |
| 61 | */ |
| 62 | printf("testU32(1)"); |
| 63 | uint32_t u32 = testClient.testU32(1); |
| 64 | printf(" = %u\n", u32); |
| 65 | |
| 66 | /** |
| 67 | * I32 TEST |
| 68 | */ |
| 69 | printf("testI32(-1)"); |
| 70 | int32_t i32 = testClient.testI32(-1); |
| 71 | printf(" = %d\n", i32); |
| 72 | |
| 73 | /** |
| 74 | * U64 TEST |
| 75 | */ |
| 76 | printf("testU64(34359738368)"); |
| 77 | uint64_t u64 = testClient.testU64(34359738368); |
| 78 | printf(" = %lu\n", u64); |
| 79 | |
| 80 | /** |
| 81 | * I64 TEST |
| 82 | */ |
| 83 | printf("testI64(-34359738368)"); |
| 84 | int64_t i64 = testClient.testI64(-34359738368); |
| 85 | printf(" = %ld\n", i64); |
| 86 | |
| 87 | /** |
| 88 | * STRUCT TEST |
| 89 | */ |
| 90 | printf("testStruct({\"Zero\", 1, 2, -3, 4, -5})"); |
| 91 | Xtruct out; |
| 92 | out.string_thing = "Zero"; |
| 93 | out.byte_thing = 1; |
| 94 | out.u32_thing = 2; |
| 95 | out.i32_thing = -3; |
| 96 | out.u64_thing = 4; |
| 97 | out.i64_thing = -5; |
| 98 | Xtruct in = testClient.testStruct(out); |
| 99 | printf(" = {\"%s\", %d, %u, %d, %lu, %ld}\n", |
| 100 | in.string_thing.c_str(), |
| 101 | (int)in.byte_thing, |
| 102 | in.u32_thing, |
| 103 | in.i32_thing, |
| 104 | in.u64_thing, |
| 105 | in.i64_thing); |
| 106 | |
| 107 | /** |
| 108 | * NESTED STRUCT TEST |
| 109 | */ |
| 110 | printf("testNest({1, {\"Zero\", 1, 2, -3, 4, -5}), 5}"); |
| 111 | Xtruct2 out2; |
| 112 | out2.byte_thing = 1; |
| 113 | out2.struct_thing = out; |
| 114 | out2.i32_thing = 5; |
| 115 | Xtruct2 in2 = testClient.testNest(out2); |
| 116 | in = in2.struct_thing; |
| 117 | printf(" = {%d, {\"%s\", %d, %u, %d, %lu, %ld}, %d}\n", |
| 118 | in2.byte_thing, |
| 119 | in.string_thing.c_str(), |
| 120 | (int)in.byte_thing, |
| 121 | in.u32_thing, |
| 122 | in.i32_thing, |
| 123 | in.u64_thing, |
| 124 | in.i64_thing, |
| 125 | in2.i32_thing); |
| 126 | |
| 127 | /** |
| 128 | * MAP TEST |
| 129 | */ |
| 130 | map<int32_t,int32_t> mapout; |
| 131 | for (int32_t i = 0; i < 5; ++i) { |
| 132 | mapout.insert(make_pair(i, i-10)); |
| 133 | } |
| 134 | printf("testMap({"); |
| 135 | map<int32_t, int32_t>::const_iterator m_iter; |
| 136 | bool first = true; |
| 137 | for (m_iter = mapout.begin(); m_iter != mapout.end(); ++m_iter) { |
| 138 | if (first) { |
| 139 | first = false; |
| 140 | } else { |
| 141 | printf(", "); |
| 142 | } |
| 143 | printf("%d => %d", m_iter->first, m_iter->second); |
| 144 | } |
| 145 | printf("})"); |
| 146 | map<int32_t,int32_t> mapin = testClient.testMap(mapout); |
| 147 | printf(" = {"); |
| 148 | first = true; |
| 149 | for (m_iter = mapin.begin(); m_iter != mapin.end(); ++m_iter) { |
| 150 | if (first) { |
| 151 | first = false; |
| 152 | } else { |
| 153 | printf(", "); |
| 154 | } |
| 155 | printf("%d => %d", m_iter->first, m_iter->second); |
| 156 | } |
| 157 | printf("}\n"); |
| 158 | |
| 159 | /** |
| 160 | * SET TEST |
| 161 | */ |
| 162 | set<int32_t> setout; |
| 163 | for (int32_t i = -2; i < 3; ++i) { |
| 164 | setout.insert(i); |
| 165 | } |
| 166 | printf("testSet({"); |
| 167 | set<int32_t>::const_iterator s_iter; |
| 168 | first = true; |
| 169 | for (s_iter = setout.begin(); s_iter != setout.end(); ++s_iter) { |
| 170 | if (first) { |
| 171 | first = false; |
| 172 | } else { |
| 173 | printf(", "); |
| 174 | } |
| 175 | printf("%d", *s_iter); |
| 176 | } |
| 177 | printf("})"); |
| 178 | set<int32_t> setin = testClient.testSet(setout); |
| 179 | printf(" = {"); |
| 180 | first = true; |
| 181 | for (s_iter = setin.begin(); s_iter != setin.end(); ++s_iter) { |
| 182 | if (first) { |
| 183 | first = false; |
| 184 | } else { |
| 185 | printf(", "); |
| 186 | } |
| 187 | printf("%d", *s_iter); |
| 188 | } |
| 189 | printf("}\n"); |
| 190 | |
| 191 | /** |
| 192 | * LIST TEST |
| 193 | */ |
| 194 | list<int32_t> listout; |
| 195 | for (int32_t i = -2; i < 3; ++i) { |
| 196 | listout.push_back(i); |
| 197 | } |
| 198 | printf("testList({"); |
| 199 | list<int32_t>::const_iterator l_iter; |
| 200 | first = true; |
| 201 | for (l_iter = listout.begin(); l_iter != listout.end(); ++l_iter) { |
| 202 | if (first) { |
| 203 | first = false; |
| 204 | } else { |
| 205 | printf(", "); |
| 206 | } |
| 207 | printf("%d", *l_iter); |
| 208 | } |
| 209 | printf("})"); |
| 210 | list<int32_t> listin = testClient.testList(listout); |
| 211 | printf(" = {"); |
| 212 | first = true; |
| 213 | for (l_iter = listin.begin(); l_iter != listin.end(); ++l_iter) { |
| 214 | if (first) { |
| 215 | first = false; |
| 216 | } else { |
| 217 | printf(", "); |
| 218 | } |
| 219 | printf("%d", *l_iter); |
| 220 | } |
| 221 | printf("}\n"); |
| 222 | |
| 223 | /** |
| 224 | * ENUM TEST |
| 225 | */ |
| 226 | printf("testEnum(ONE)"); |
| 227 | Numberz ret = testClient.testEnum(ONE); |
| 228 | printf(" = %d\n", ret); |
| 229 | |
| 230 | printf("testEnum(TWO)"); |
| 231 | ret = testClient.testEnum(TWO); |
| 232 | printf(" = %d\n", ret); |
| 233 | |
| 234 | printf("testEnum(THREE)"); |
| 235 | ret = testClient.testEnum(THREE); |
| 236 | printf(" = %d\n", ret); |
| 237 | |
| 238 | printf("testEnum(FIVE)"); |
| 239 | ret = testClient.testEnum(FIVE); |
| 240 | printf(" = %d\n", ret); |
| 241 | |
| 242 | printf("testEnum(EIGHT)"); |
| 243 | ret = testClient.testEnum(EIGHT); |
| 244 | printf(" = %d\n", ret); |
| 245 | |
| 246 | /** |
| 247 | * TYPEDEF TEST |
| 248 | */ |
| 249 | printf("testTypedef(309858235082523)"); |
| 250 | UserId uid = testClient.testTypedef(309858235082523); |
| 251 | printf(" = %lu\n", uid); |
| 252 | |
| 253 | /** |
| 254 | * NESTED MAP TEST |
| 255 | */ |
| 256 | printf("testMapMap(1)"); |
| 257 | map<int32_t, map<int32_t, int32_t> > mm = testClient.testMapMap(1); |
| 258 | printf(" = {"); |
| 259 | map<int32_t, map<int32_t, int32_t> >::const_iterator mi; |
| 260 | for (mi = mm.begin(); mi != mm.end(); ++mi) { |
| 261 | printf("%d => {", mi->first); |
| 262 | map<int32_t, int32_t>::const_iterator mi2; |
| 263 | for (mi2 = mi->second.begin(); mi2 != mi->second.end(); ++mi2) { |
| 264 | printf("%d => %d, ", mi2->first, mi2->second); |
| 265 | } |
| 266 | printf("}, "); |
| 267 | } |
| 268 | printf("}\n"); |
| 269 | |
| 270 | /** |
| 271 | * INSANITY TEST |
| 272 | */ |
| 273 | Insanity insane; |
| 274 | insane.userMap.insert(make_pair(FIVE, 5000)); |
| 275 | Xtruct truck; |
| 276 | truck.string_thing = "Truck"; |
| 277 | truck.byte_thing = 8; |
| 278 | truck.u32_thing = 8; |
| 279 | truck.i32_thing = 8; |
| 280 | truck.u64_thing = 8; |
| 281 | truck.i64_thing = 8; |
| 282 | insane.xtructs.push_back(truck); |
| 283 | printf("testInsanity()"); |
| 284 | map<UserId, map<Numberz,Insanity> > whoa = testClient.testInsanity(insane); |
| 285 | printf(" = {"); |
| 286 | map<UserId, map<Numberz,Insanity> >::const_iterator i_iter; |
| 287 | for (i_iter = whoa.begin(); i_iter != whoa.end(); ++i_iter) { |
| 288 | printf("%lu => {", i_iter->first); |
| 289 | map<Numberz,Insanity>::const_iterator i2_iter; |
| 290 | for (i2_iter = i_iter->second.begin(); |
| 291 | i2_iter != i_iter->second.end(); |
| 292 | ++i2_iter) { |
| 293 | printf("%d => {", i2_iter->first); |
| 294 | map<Numberz, UserId> userMap = i2_iter->second.userMap; |
| 295 | map<Numberz, UserId>::const_iterator um; |
| 296 | printf("{"); |
| 297 | for (um = userMap.begin(); um != userMap.end(); ++um) { |
| 298 | printf("%d => %lu, ", um->first, um->second); |
| 299 | } |
| 300 | printf("}, "); |
| 301 | |
| 302 | list<Xtruct> xtructs = i2_iter->second.xtructs; |
| 303 | list<Xtruct>::const_iterator x; |
| 304 | printf("{"); |
| 305 | for (x = xtructs.begin(); x != xtructs.end(); ++x) { |
| 306 | printf("{\"%s\", %d, %u, %d, %lu, %ld}, ", |
| 307 | x->string_thing.c_str(), |
| 308 | (int)x->byte_thing, |
| 309 | x->u32_thing, |
| 310 | x->i32_thing, |
| 311 | x->u64_thing, |
| 312 | x->i64_thing); |
| 313 | } |
| 314 | printf("}"); |
| 315 | |
| 316 | printf("}, "); |
| 317 | } |
| 318 | printf("}, "); |
| 319 | } |
| 320 | printf("}\n"); |
| 321 | |
| 322 | simpleClient.close(); |
| 323 | } |
| 324 | |
| 325 | printf("\nAll tests done.\n"); |
| 326 | return 0; |
| 327 | } |