| 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 | /** | 
| Mark Slee | c98d050 | 2006-09-06 02:42:25 +0000 | [diff] [blame] | 88 | * DOUBLE TEST | 
|  | 89 | */ | 
|  | 90 | print_r("testDouble(-852.234234234)"); | 
|  | 91 | $dub = $testClient->testDouble(-852.234234234); | 
|  | 92 | print_r(" = $dub\n"); | 
|  | 93 |  | 
|  | 94 | /** | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 95 | * STRUCT TEST | 
|  | 96 | */ | 
|  | 97 | print_r("testStruct({\"Zero\", 1, -3, -5})"); | 
|  | 98 | $out = new Xtruct(); | 
|  | 99 | $out->string_thing = "Zero"; | 
|  | 100 | $out->byte_thing = 1; | 
|  | 101 | $out->i32_thing = -3; | 
|  | 102 | $out->i64_thing = -5; | 
|  | 103 | $in = $testClient->testStruct($out); | 
|  | 104 | print_r(" = {\"".$in->string_thing."\", ". | 
|  | 105 | $in->byte_thing.", ". | 
|  | 106 | $in->i32_thing.", ". | 
|  | 107 | $in->i64_thing."}\n"); | 
|  | 108 |  | 
|  | 109 | /** | 
|  | 110 | * NESTED STRUCT TEST | 
|  | 111 | */ | 
|  | 112 | print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}"); | 
|  | 113 | $out2 = new Xtruct2(); | 
|  | 114 | $out2->byte_thing = 1; | 
|  | 115 | $out2->struct_thing = $out; | 
|  | 116 | $out2->i32_thing = 5; | 
|  | 117 | $in2 = $testClient->testNest($out2); | 
|  | 118 | $in = $in2->struct_thing; | 
|  | 119 | print_r(" = {".$in2->byte_thing.", {\"". | 
|  | 120 | $in->string_thing."\", ". | 
|  | 121 | $in->byte_thing.", ". | 
|  | 122 | $in->i32_thing.", ". | 
|  | 123 | $in->i64_thing."}, ". | 
|  | 124 | $in2->i32_thing."}\n"); | 
|  | 125 |  | 
|  | 126 | /** | 
|  | 127 | * MAP TEST | 
|  | 128 | */ | 
|  | 129 | $mapout = array(); | 
|  | 130 | for ($i = 0; $i < 5; ++$i) { | 
|  | 131 | $mapout[$i] = $i-10; | 
|  | 132 | } | 
|  | 133 | print_r("testMap({"); | 
|  | 134 | $first = true; | 
|  | 135 | foreach ($mapout as $key => $val) { | 
|  | 136 | if ($first) { | 
|  | 137 | $first = false; | 
|  | 138 | } else { | 
|  | 139 | print_r(", "); | 
|  | 140 | } | 
|  | 141 | print_r("$key => $val"); | 
|  | 142 | } | 
|  | 143 | print_r("})"); | 
|  | 144 |  | 
|  | 145 | $mapin = $testClient->testMap($mapout); | 
|  | 146 | print_r(" = {"); | 
|  | 147 | $first = true; | 
|  | 148 | foreach ($mapin as $key => $val) { | 
|  | 149 | if ($first) { | 
|  | 150 | $first = false; | 
|  | 151 | } else { | 
|  | 152 | print_r(", "); | 
|  | 153 | } | 
|  | 154 | print_r("$key => $val"); | 
|  | 155 | } | 
|  | 156 | print_r("}\n"); | 
|  | 157 |  | 
|  | 158 | /** | 
|  | 159 | * SET TEST | 
|  | 160 | */ | 
|  | 161 | $setout = array();; | 
|  | 162 | for ($i = -2; $i < 3; ++$i) { | 
|  | 163 | $setout []= $i; | 
|  | 164 | } | 
|  | 165 | print_r("testSet({"); | 
|  | 166 | $first = true; | 
|  | 167 | foreach ($setout as $val) { | 
|  | 168 | if ($first) { | 
|  | 169 | $first = false; | 
|  | 170 | } else { | 
|  | 171 | print_r(", "); | 
|  | 172 | } | 
|  | 173 | print_r($val); | 
|  | 174 | } | 
|  | 175 | print_r("})"); | 
|  | 176 | $setin = $testClient->testSet($setout); | 
|  | 177 | print_r(" = {"); | 
|  | 178 | $first = true; | 
|  | 179 | foreach ($setin as $val) { | 
|  | 180 | if ($first) { | 
|  | 181 | $first = false; | 
|  | 182 | } else { | 
|  | 183 | print_r(", "); | 
|  | 184 | } | 
|  | 185 | print_r($val); | 
|  | 186 | } | 
|  | 187 | print_r("}\n"); | 
|  | 188 |  | 
|  | 189 | /** | 
|  | 190 | * LIST TEST | 
|  | 191 | */ | 
|  | 192 | $listout = array(); | 
|  | 193 | for ($i = -2; $i < 3; ++$i) { | 
|  | 194 | $listout []= $i; | 
|  | 195 | } | 
|  | 196 | print_r("testList({"); | 
|  | 197 | $first = true; | 
|  | 198 | foreach ($listout as $val) { | 
|  | 199 | if ($first) { | 
|  | 200 | $first = false; | 
|  | 201 | } else { | 
|  | 202 | print_r(", "); | 
|  | 203 | } | 
|  | 204 | print_r($val); | 
|  | 205 | } | 
|  | 206 | print_r("})"); | 
|  | 207 | $listin = $testClient->testList($listout); | 
|  | 208 | print_r(" = {"); | 
|  | 209 | $first = true; | 
|  | 210 | foreach ($listin as $val) { | 
|  | 211 | if ($first) { | 
|  | 212 | $first = false; | 
|  | 213 | } else { | 
|  | 214 | print_r(", "); | 
|  | 215 | } | 
|  | 216 | print_r($val); | 
|  | 217 | } | 
|  | 218 | print_r("}\n"); | 
|  | 219 |  | 
|  | 220 | /** | 
|  | 221 | * ENUM TEST | 
|  | 222 | */ | 
|  | 223 | print_r("testEnum(ONE)"); | 
|  | 224 | $ret = $testClient->testEnum(Numberz::ONE); | 
|  | 225 | print_r(" = $ret\n"); | 
|  | 226 |  | 
|  | 227 | print_r("testEnum(TWO)"); | 
|  | 228 | $ret = $testClient->testEnum(Numberz::TWO); | 
|  | 229 | print_r(" = $ret\n"); | 
|  | 230 |  | 
|  | 231 | print_r("testEnum(THREE)"); | 
|  | 232 | $ret = $testClient->testEnum(Numberz::THREE); | 
|  | 233 | print_r(" = $ret\n"); | 
|  | 234 |  | 
|  | 235 | print_r("testEnum(FIVE)"); | 
|  | 236 | $ret = $testClient->testEnum(Numberz::FIVE); | 
|  | 237 | print_r(" = $ret\n"); | 
|  | 238 |  | 
|  | 239 | print_r("testEnum(EIGHT)"); | 
|  | 240 | $ret = $testClient->testEnum(Numberz::EIGHT); | 
|  | 241 | print_r(" = $ret\n"); | 
|  | 242 |  | 
|  | 243 | /** | 
|  | 244 | * TYPEDEF TEST | 
|  | 245 | */ | 
|  | 246 | print_r("testTypedef(309858235082523)"); | 
|  | 247 | $uid = $testClient->testTypedef(309858235082523); | 
|  | 248 | print_r(" = $uid\n"); | 
|  | 249 |  | 
|  | 250 | /** | 
|  | 251 | * NESTED MAP TEST | 
|  | 252 | */ | 
|  | 253 | print_r("testMapMap(1)"); | 
|  | 254 | $mm = $testClient->testMapMap(1); | 
|  | 255 | print_r(" = {"); | 
|  | 256 | foreach ($mm as $key => $val) { | 
|  | 257 | print_r("$key => {"); | 
|  | 258 | foreach ($val as $k2 => $v2) { | 
|  | 259 | print_r("$k2 => $v2, "); | 
|  | 260 | } | 
|  | 261 | print_r("}, "); | 
|  | 262 | } | 
|  | 263 | print_r("}\n"); | 
|  | 264 |  | 
|  | 265 | /** | 
|  | 266 | * INSANITY TEST | 
|  | 267 | */ | 
|  | 268 | $insane = new Insanity(); | 
|  | 269 | $insane->userMap[Numberz::FIVE] = 5000; | 
|  | 270 | $truck = new Xtruct(); | 
|  | 271 | $truck->string_thing = "Truck"; | 
|  | 272 | $truck->byte_thing = 8; | 
|  | 273 | $truck->i32_thing = 8; | 
|  | 274 | $truck->i64_thing = 8; | 
|  | 275 | $insane->xtructs []= $truck; | 
|  | 276 | print_r("testInsanity()"); | 
|  | 277 | $whoa = $testClient->testInsanity($insane); | 
|  | 278 | print_r(" = {"); | 
|  | 279 | foreach ($whoa as $key => $val) { | 
|  | 280 | print_r("$key => {"); | 
|  | 281 | foreach ($val as $k2 => $v2) { | 
|  | 282 | print_r("$k2 => {"); | 
|  | 283 | $userMap = $v2->userMap; | 
|  | 284 | print_r("{"); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 285 | if (is_array($usermap)) { | 
|  | 286 | foreach ($userMap as $k3 => $v3) { | 
|  | 287 | print_r("$k3 => $v3, "); | 
|  | 288 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 289 | } | 
|  | 290 | print_r("}, "); | 
|  | 291 |  | 
|  | 292 | $xtructs = $v2->xtructs; | 
|  | 293 | print_r("{"); | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 294 | if (is_array($xtructs)) { | 
|  | 295 | foreach ($xtructs as $x) { | 
|  | 296 | print_r("{\"".$x->string_thing."\", ". | 
|  | 297 | $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, "); | 
|  | 298 | } | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 299 | } | 
|  | 300 | print_r("}"); | 
|  | 301 |  | 
|  | 302 | print_r("}, "); | 
|  | 303 | } | 
|  | 304 | print_r("}, "); | 
|  | 305 | } | 
|  | 306 | print_r("}\n"); | 
|  | 307 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 308 | /** | 
|  | 309 | * EXCEPTION TEST | 
|  | 310 | */ | 
|  | 311 | print_r("testException('Xception')"); | 
|  | 312 | try { | 
|  | 313 | $testClient->testException('Xception'); | 
|  | 314 | print_r("  void\nFAILURE\n"); | 
|  | 315 | } catch (Xception $x) { | 
|  | 316 | print_r(' caught xception '.$x->errorCode.': '.$x->message."\n"); | 
|  | 317 | } | 
|  | 318 |  | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 319 |  | 
|  | 320 | /** | 
|  | 321 | * Normal tests done. | 
|  | 322 | */ | 
|  | 323 |  | 
|  | 324 | $stop = microtime(true); | 
|  | 325 | $elp = round(1000*($stop - $start), 0); | 
|  | 326 | print_r("Total time: $elp ms\n"); | 
|  | 327 |  | 
|  | 328 | /** | 
|  | 329 | * Extraneous "I don't trust PHP to pack/unpack integer" tests | 
|  | 330 | */ | 
|  | 331 |  | 
|  | 332 | // Max I32 | 
|  | 333 | $num = pow(2, 30) + (pow(2, 30) - 1); | 
|  | 334 | $num2 = $testClient->testI32($num); | 
|  | 335 | if ($num != $num2) { | 
|  | 336 | print "Missed $num = $num2\n"; | 
|  | 337 | } | 
|  | 338 |  | 
|  | 339 | // Min I32 | 
|  | 340 | $num = 0 - pow(2, 31); | 
|  | 341 | $num2 = $testClient->testI32($num); | 
|  | 342 | if ($num != $num2) { | 
|  | 343 | print "Missed $num = $num2\n"; | 
|  | 344 | } | 
|  | 345 |  | 
|  | 346 | // Max I64 | 
|  | 347 | $num = pow(2, 62) + (pow(2, 62) - 1); | 
|  | 348 | $num2 = $testClient->testI64($num); | 
|  | 349 | if ($num != $num2) { | 
|  | 350 | print "Missed $num = $num2\n"; | 
|  | 351 | } | 
|  | 352 |  | 
|  | 353 | // Min I64 | 
|  | 354 | $num = 0 - pow(2, 63); | 
|  | 355 | $num2 = $testClient->testI64($num); | 
|  | 356 | if ($num != $num2) { | 
|  | 357 | print "Missed $num = $num2\n"; | 
|  | 358 | } | 
|  | 359 |  | 
| Mark Slee | d3d733a | 2006-09-01 22:19:06 +0000 | [diff] [blame] | 360 | $transport->close(); | 
| Mark Slee | 6e53644 | 2006-06-30 18:28:50 +0000 | [diff] [blame] | 361 | return; | 
|  | 362 |  | 
|  | 363 | ?> |