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