Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 1 | <?php |
| 2 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 3 | if (!isset($GEN_DIR)) { |
| 4 | $GEN_DIR = 'gen-php'; |
| 5 | } |
| 6 | if (!isset($MODE)) { |
| 7 | $MODE = 'normal'; |
| 8 | } |
| 9 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 10 | /** Include the Thrift base */ |
| 11 | require_once '/home/mcslee/code/projects/thrift/lib/php/src/Thrift.php'; |
| 12 | |
| 13 | /** Include the binary protocol */ |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 14 | require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 15 | |
| 16 | /** Include the socket layer */ |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 17 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.php'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 18 | |
| 19 | /** Include the socket layer */ |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 20 | require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 21 | |
| 22 | /** Include the generated code */ |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 23 | require_once '/home/mcslee/code/projects/thrift/test/php/'.$GEN_DIR.'/ThriftTest.php'; |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 24 | |
| 25 | $host = 'localhost'; |
| 26 | $port = 9090; |
| 27 | |
| 28 | if ($argc > 1) { |
| 29 | $host = $argv[0]; |
| 30 | } |
| 31 | |
| 32 | if ($argc > 2) { |
| 33 | $host = $argv[1]; |
| 34 | } |
| 35 | |
| 36 | $socket = new TSocket($host, $port); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 37 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 38 | if ($MODE == 'inline') { |
| 39 | $transport = $socket; |
| 40 | $testClient = new ThriftTestClient($transport); |
| 41 | } else { |
| 42 | $bufferedSocket = new TBufferedTransport($socket, 1024, 1024); |
| 43 | $transport = $bufferedSocket; |
| 44 | $protocol = new TBinaryProtocol(); |
| 45 | $testClient = new ThriftTestClient($transport, $protocol); |
| 46 | } |
| 47 | |
| 48 | $transport->open(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 49 | |
| 50 | $start = microtime(true); |
| 51 | |
| 52 | /** |
| 53 | * VOID TEST |
| 54 | */ |
| 55 | print_r("testVoid()"); |
| 56 | $testClient->testVoid(); |
| 57 | print_r(" = void\n"); |
| 58 | |
| 59 | /** |
| 60 | * STRING TEST |
| 61 | */ |
| 62 | print_r("testString(\"Test\")"); |
| 63 | $s = $testClient->testString("Test"); |
| 64 | print_r(" = \"$s\"\n"); |
| 65 | |
| 66 | /** |
| 67 | * BYTE TEST |
| 68 | */ |
| 69 | print_r("testByte(1)"); |
| 70 | $u8 = $testClient->testByte(1); |
| 71 | print_r(" = $u8\n"); |
| 72 | |
| 73 | /** |
| 74 | * I32 TEST |
| 75 | */ |
| 76 | print_r("testI32(-1)"); |
| 77 | $i32 = $testClient->testI32(-1); |
| 78 | print_r(" = $i32\n"); |
| 79 | |
| 80 | /** |
| 81 | * I64 TEST |
| 82 | */ |
| 83 | print_r("testI64(-34359738368)"); |
| 84 | $i64 = $testClient->testI64(-34359738368); |
| 85 | print_r(" = $i64\n"); |
| 86 | |
| 87 | /** |
| 88 | * STRUCT TEST |
| 89 | */ |
| 90 | print_r("testStruct({\"Zero\", 1, -3, -5})"); |
| 91 | $out = new Xtruct(); |
| 92 | $out->string_thing = "Zero"; |
| 93 | $out->byte_thing = 1; |
| 94 | $out->i32_thing = -3; |
| 95 | $out->i64_thing = -5; |
| 96 | $in = $testClient->testStruct($out); |
| 97 | print_r(" = {\"".$in->string_thing."\", ". |
| 98 | $in->byte_thing.", ". |
| 99 | $in->i32_thing.", ". |
| 100 | $in->i64_thing."}\n"); |
| 101 | |
| 102 | /** |
| 103 | * NESTED STRUCT TEST |
| 104 | */ |
| 105 | print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); |
| 106 | $out2 = new Xtruct2(); |
| 107 | $out2->byte_thing = 1; |
| 108 | $out2->struct_thing = $out; |
| 109 | $out2->i32_thing = 5; |
| 110 | $in2 = $testClient->testNest($out2); |
| 111 | $in = $in2->struct_thing; |
| 112 | print_r(" = {".$in2->byte_thing.", {\"". |
| 113 | $in->string_thing."\", ". |
| 114 | $in->byte_thing.", ". |
| 115 | $in->i32_thing.", ". |
| 116 | $in->i64_thing."}, ". |
| 117 | $in2->i32_thing."}\n"); |
| 118 | |
| 119 | /** |
| 120 | * MAP TEST |
| 121 | */ |
| 122 | $mapout = array(); |
| 123 | for ($i = 0; $i < 5; ++$i) { |
| 124 | $mapout[$i] = $i-10; |
| 125 | } |
| 126 | print_r("testMap({"); |
| 127 | $first = true; |
| 128 | foreach ($mapout as $key => $val) { |
| 129 | if ($first) { |
| 130 | $first = false; |
| 131 | } else { |
| 132 | print_r(", "); |
| 133 | } |
| 134 | print_r("$key => $val"); |
| 135 | } |
| 136 | print_r("})"); |
| 137 | |
| 138 | $mapin = $testClient->testMap($mapout); |
| 139 | print_r(" = {"); |
| 140 | $first = true; |
| 141 | foreach ($mapin as $key => $val) { |
| 142 | if ($first) { |
| 143 | $first = false; |
| 144 | } else { |
| 145 | print_r(", "); |
| 146 | } |
| 147 | print_r("$key => $val"); |
| 148 | } |
| 149 | print_r("}\n"); |
| 150 | |
| 151 | /** |
| 152 | * SET TEST |
| 153 | */ |
| 154 | $setout = array();; |
| 155 | for ($i = -2; $i < 3; ++$i) { |
| 156 | $setout []= $i; |
| 157 | } |
| 158 | print_r("testSet({"); |
| 159 | $first = true; |
| 160 | foreach ($setout as $val) { |
| 161 | if ($first) { |
| 162 | $first = false; |
| 163 | } else { |
| 164 | print_r(", "); |
| 165 | } |
| 166 | print_r($val); |
| 167 | } |
| 168 | print_r("})"); |
| 169 | $setin = $testClient->testSet($setout); |
| 170 | print_r(" = {"); |
| 171 | $first = true; |
| 172 | foreach ($setin as $val) { |
| 173 | if ($first) { |
| 174 | $first = false; |
| 175 | } else { |
| 176 | print_r(", "); |
| 177 | } |
| 178 | print_r($val); |
| 179 | } |
| 180 | print_r("}\n"); |
| 181 | |
| 182 | /** |
| 183 | * LIST TEST |
| 184 | */ |
| 185 | $listout = array(); |
| 186 | for ($i = -2; $i < 3; ++$i) { |
| 187 | $listout []= $i; |
| 188 | } |
| 189 | print_r("testList({"); |
| 190 | $first = true; |
| 191 | foreach ($listout as $val) { |
| 192 | if ($first) { |
| 193 | $first = false; |
| 194 | } else { |
| 195 | print_r(", "); |
| 196 | } |
| 197 | print_r($val); |
| 198 | } |
| 199 | print_r("})"); |
| 200 | $listin = $testClient->testList($listout); |
| 201 | print_r(" = {"); |
| 202 | $first = true; |
| 203 | foreach ($listin as $val) { |
| 204 | if ($first) { |
| 205 | $first = false; |
| 206 | } else { |
| 207 | print_r(", "); |
| 208 | } |
| 209 | print_r($val); |
| 210 | } |
| 211 | print_r("}\n"); |
| 212 | |
| 213 | /** |
| 214 | * ENUM TEST |
| 215 | */ |
| 216 | print_r("testEnum(ONE)"); |
| 217 | $ret = $testClient->testEnum(Numberz::ONE); |
| 218 | print_r(" = $ret\n"); |
| 219 | |
| 220 | print_r("testEnum(TWO)"); |
| 221 | $ret = $testClient->testEnum(Numberz::TWO); |
| 222 | print_r(" = $ret\n"); |
| 223 | |
| 224 | print_r("testEnum(THREE)"); |
| 225 | $ret = $testClient->testEnum(Numberz::THREE); |
| 226 | print_r(" = $ret\n"); |
| 227 | |
| 228 | print_r("testEnum(FIVE)"); |
| 229 | $ret = $testClient->testEnum(Numberz::FIVE); |
| 230 | print_r(" = $ret\n"); |
| 231 | |
| 232 | print_r("testEnum(EIGHT)"); |
| 233 | $ret = $testClient->testEnum(Numberz::EIGHT); |
| 234 | print_r(" = $ret\n"); |
| 235 | |
| 236 | /** |
| 237 | * TYPEDEF TEST |
| 238 | */ |
| 239 | print_r("testTypedef(309858235082523)"); |
| 240 | $uid = $testClient->testTypedef(309858235082523); |
| 241 | print_r(" = $uid\n"); |
| 242 | |
| 243 | /** |
| 244 | * NESTED MAP TEST |
| 245 | */ |
| 246 | print_r("testMapMap(1)"); |
| 247 | $mm = $testClient->testMapMap(1); |
| 248 | print_r(" = {"); |
| 249 | foreach ($mm as $key => $val) { |
| 250 | print_r("$key => {"); |
| 251 | foreach ($val as $k2 => $v2) { |
| 252 | print_r("$k2 => $v2, "); |
| 253 | } |
| 254 | print_r("}, "); |
| 255 | } |
| 256 | print_r("}\n"); |
| 257 | |
| 258 | /** |
| 259 | * INSANITY TEST |
| 260 | */ |
| 261 | $insane = new Insanity(); |
| 262 | $insane->userMap[Numberz::FIVE] = 5000; |
| 263 | $truck = new Xtruct(); |
| 264 | $truck->string_thing = "Truck"; |
| 265 | $truck->byte_thing = 8; |
| 266 | $truck->i32_thing = 8; |
| 267 | $truck->i64_thing = 8; |
| 268 | $insane->xtructs []= $truck; |
| 269 | print_r("testInsanity()"); |
| 270 | $whoa = $testClient->testInsanity($insane); |
| 271 | print_r(" = {"); |
| 272 | foreach ($whoa as $key => $val) { |
| 273 | print_r("$key => {"); |
| 274 | foreach ($val as $k2 => $v2) { |
| 275 | print_r("$k2 => {"); |
| 276 | $userMap = $v2->userMap; |
| 277 | print_r("{"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 278 | if (is_array($usermap)) { |
| 279 | foreach ($userMap as $k3 => $v3) { |
| 280 | print_r("$k3 => $v3, "); |
| 281 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 282 | } |
| 283 | print_r("}, "); |
| 284 | |
| 285 | $xtructs = $v2->xtructs; |
| 286 | print_r("{"); |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 287 | if (is_array($xtructs)) { |
| 288 | foreach ($xtructs as $x) { |
| 289 | print_r("{\"".$x->string_thing."\", ". |
| 290 | $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, "); |
| 291 | } |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 292 | } |
| 293 | print_r("}"); |
| 294 | |
| 295 | print_r("}, "); |
| 296 | } |
| 297 | print_r("}, "); |
| 298 | } |
| 299 | print_r("}\n"); |
| 300 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 301 | /** |
| 302 | * EXCEPTION TEST |
| 303 | */ |
| 304 | print_r("testException('Xception')"); |
| 305 | try { |
| 306 | $testClient->testException('Xception'); |
| 307 | print_r(" void\nFAILURE\n"); |
| 308 | } catch (Xception $x) { |
| 309 | print_r(' caught xception '.$x->errorCode.': '.$x->message."\n"); |
| 310 | } |
| 311 | |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 312 | |
| 313 | /** |
| 314 | * Normal tests done. |
| 315 | */ |
| 316 | |
| 317 | $stop = microtime(true); |
| 318 | $elp = round(1000*($stop - $start), 0); |
| 319 | print_r("Total time: $elp ms\n"); |
| 320 | |
| 321 | /** |
| 322 | * Extraneous "I don't trust PHP to pack/unpack integer" tests |
| 323 | */ |
| 324 | |
| 325 | // Max I32 |
| 326 | $num = pow(2, 30) + (pow(2, 30) - 1); |
| 327 | $num2 = $testClient->testI32($num); |
| 328 | if ($num != $num2) { |
| 329 | print "Missed $num = $num2\n"; |
| 330 | } |
| 331 | |
| 332 | // Min I32 |
| 333 | $num = 0 - pow(2, 31); |
| 334 | $num2 = $testClient->testI32($num); |
| 335 | if ($num != $num2) { |
| 336 | print "Missed $num = $num2\n"; |
| 337 | } |
| 338 | |
| 339 | // Max I64 |
| 340 | $num = pow(2, 62) + (pow(2, 62) - 1); |
| 341 | $num2 = $testClient->testI64($num); |
| 342 | if ($num != $num2) { |
| 343 | print "Missed $num = $num2\n"; |
| 344 | } |
| 345 | |
| 346 | // Min I64 |
| 347 | $num = 0 - pow(2, 63); |
| 348 | $num2 = $testClient->testI64($num); |
| 349 | if ($num != $num2) { |
| 350 | print "Missed $num = $num2\n"; |
| 351 | } |
| 352 | |
Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 353 | $transport->close(); |
Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 354 | return; |
| 355 | |
| 356 | ?> |