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