blob: 6d640dac3a20b2c8cda7ffd66e2ca187b3a3dd56 [file] [log] [blame]
David Reissea2cba82009-03-30 21:35:00 +00001/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
Mark Slee6e536442006-06-30 18:28:50 +000020<?php
21
Mark Sleed3d733a2006-09-01 22:19:06 +000022if (!isset($GEN_DIR)) {
23 $GEN_DIR = 'gen-php';
24}
25if (!isset($MODE)) {
26 $MODE = 'normal';
27}
28
Mark Slee018b6992006-09-07 21:31:12 +000029/** Set the Thrift root */
30$GLOBALS['THRIFT_ROOT'] = '../../lib/php/src';
31
Mark Slee6e536442006-06-30 18:28:50 +000032/** Include the Thrift base */
Mark Slee018b6992006-09-07 21:31:12 +000033require_once $GLOBALS['THRIFT_ROOT'].'/Thrift.php';
Mark Slee6e536442006-06-30 18:28:50 +000034
35/** Include the binary protocol */
Mark Sleed3d733a2006-09-01 22:19:06 +000036require_once $GLOBALS['THRIFT_ROOT'].'/protocol/TBinaryProtocol.php';
Mark Slee6e536442006-06-30 18:28:50 +000037
38/** Include the socket layer */
Mark Sleeade2c832006-09-08 03:41:50 +000039require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocketPool.php';
Mark Slee6e536442006-06-30 18:28:50 +000040
41/** Include the socket layer */
Mark Sleed3d733a2006-09-01 22:19:06 +000042require_once $GLOBALS['THRIFT_ROOT'].'/transport/TBufferedTransport.php';
Mark Slee6e536442006-06-30 18:28:50 +000043
Mark Slee5b743072007-11-13 04:00:29 +000044echo '==============================='."\n";
45echo ' SAFE TO IGNORE THESE IN TEST'."\n";
46echo '==============================='."\n";
47
Mark Slee6e536442006-06-30 18:28:50 +000048/** Include the generated code */
Mark Sleee129a2d2007-02-21 05:17:48 +000049require_once $GEN_DIR.'/ThriftTest.php';
50require_once $GEN_DIR.'/ThriftTest_types.php';
Mark Slee6e536442006-06-30 18:28:50 +000051
Mark Slee5b743072007-11-13 04:00:29 +000052echo '==============================='."\n";
53echo ' END OF SAFE ERRORS SECTION'."\n";
54echo '==============================='."\n\n";
55
Mark Slee6e536442006-06-30 18:28:50 +000056$host = 'localhost';
57$port = 9090;
58
59if ($argc > 1) {
60 $host = $argv[0];
61}
62
63if ($argc > 2) {
64 $host = $argv[1];
65}
66
Mark Slee1dd819c2006-10-26 04:56:18 +000067$hosts = array('localhost');
Mark Sleeade2c832006-09-08 03:41:50 +000068
Mark Slee6e536442006-06-30 18:28:50 +000069$socket = new TSocket($host, $port);
Mark Sleeade2c832006-09-08 03:41:50 +000070$socket = new TSocketPool($hosts, $port);
71$socket->setDebug(TRUE);
Mark Slee6e536442006-06-30 18:28:50 +000072
Mark Sleed3d733a2006-09-01 22:19:06 +000073if ($MODE == 'inline') {
74 $transport = $socket;
75 $testClient = new ThriftTestClient($transport);
76} else {
77 $bufferedSocket = new TBufferedTransport($socket, 1024, 1024);
78 $transport = $bufferedSocket;
Mark Slee1dd819c2006-10-26 04:56:18 +000079 $protocol = new TBinaryProtocol($transport);
80 $testClient = new ThriftTestClient($protocol);
Mark Sleed3d733a2006-09-01 22:19:06 +000081}
82
83$transport->open();
Mark Slee6e536442006-06-30 18:28:50 +000084
85$start = microtime(true);
86
87/**
88 * VOID TEST
89 */
90print_r("testVoid()");
91$testClient->testVoid();
92print_r(" = void\n");
93
94/**
95 * STRING TEST
96 */
97print_r("testString(\"Test\")");
98$s = $testClient->testString("Test");
99print_r(" = \"$s\"\n");
Mark Slee5b743072007-11-13 04:00:29 +0000100
Mark Slee6e536442006-06-30 18:28:50 +0000101/**
102 * BYTE TEST
103 */
104print_r("testByte(1)");
105$u8 = $testClient->testByte(1);
106print_r(" = $u8\n");
107
108/**
109 * I32 TEST
110 */
111print_r("testI32(-1)");
112$i32 = $testClient->testI32(-1);
113print_r(" = $i32\n");
114
115/**
116 * I64 TEST
117 */
118print_r("testI64(-34359738368)");
119$i64 = $testClient->testI64(-34359738368);
120print_r(" = $i64\n");
121
122/**
Mark Sleec98d0502006-09-06 02:42:25 +0000123 * DOUBLE TEST
124 */
125print_r("testDouble(-852.234234234)");
126$dub = $testClient->testDouble(-852.234234234);
127print_r(" = $dub\n");
128
129/**
Mark Slee6e536442006-06-30 18:28:50 +0000130 * STRUCT TEST
131 */
132print_r("testStruct({\"Zero\", 1, -3, -5})");
133$out = new Xtruct();
134$out->string_thing = "Zero";
135$out->byte_thing = 1;
136$out->i32_thing = -3;
137$out->i64_thing = -5;
138$in = $testClient->testStruct($out);
139print_r(" = {\"".$in->string_thing."\", ".
140 $in->byte_thing.", ".
141 $in->i32_thing.", ".
142 $in->i64_thing."}\n");
143
144/**
145 * NESTED STRUCT TEST
146 */
147print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
148$out2 = new Xtruct2();
149$out2->byte_thing = 1;
150$out2->struct_thing = $out;
151$out2->i32_thing = 5;
152$in2 = $testClient->testNest($out2);
153$in = $in2->struct_thing;
154print_r(" = {".$in2->byte_thing.", {\"".
155 $in->string_thing."\", ".
156 $in->byte_thing.", ".
157 $in->i32_thing.", ".
158 $in->i64_thing."}, ".
159 $in2->i32_thing."}\n");
160
161/**
162 * MAP TEST
163 */
164$mapout = array();
165for ($i = 0; $i < 5; ++$i) {
166 $mapout[$i] = $i-10;
167}
168print_r("testMap({");
169$first = true;
170foreach ($mapout as $key => $val) {
171 if ($first) {
172 $first = false;
173 } else {
174 print_r(", ");
175 }
176 print_r("$key => $val");
177}
178print_r("})");
179
180$mapin = $testClient->testMap($mapout);
181print_r(" = {");
182$first = true;
183foreach ($mapin as $key => $val) {
184 if ($first) {
185 $first = false;
186 } else {
187 print_r(", ");
188 }
189 print_r("$key => $val");
190}
191print_r("}\n");
192
193/**
194 * SET TEST
195 */
196$setout = array();;
197for ($i = -2; $i < 3; ++$i) {
198 $setout []= $i;
199}
200print_r("testSet({");
201$first = true;
202foreach ($setout as $val) {
203 if ($first) {
204 $first = false;
205 } else {
206 print_r(", ");
207 }
208 print_r($val);
209}
210print_r("})");
211$setin = $testClient->testSet($setout);
212print_r(" = {");
213$first = true;
214foreach ($setin as $val) {
215 if ($first) {
216 $first = false;
217 } else {
218 print_r(", ");
219 }
220 print_r($val);
221}
222print_r("}\n");
223
224/**
225 * LIST TEST
226 */
227$listout = array();
228for ($i = -2; $i < 3; ++$i) {
229 $listout []= $i;
230}
231print_r("testList({");
232$first = true;
233foreach ($listout as $val) {
234 if ($first) {
235 $first = false;
236 } else {
237 print_r(", ");
238 }
239 print_r($val);
240}
241print_r("})");
242$listin = $testClient->testList($listout);
243print_r(" = {");
244$first = true;
245foreach ($listin as $val) {
246 if ($first) {
247 $first = false;
248 } else {
249 print_r(", ");
250 }
251 print_r($val);
252}
253print_r("}\n");
254
255/**
256 * ENUM TEST
257 */
258print_r("testEnum(ONE)");
259$ret = $testClient->testEnum(Numberz::ONE);
260print_r(" = $ret\n");
261
262print_r("testEnum(TWO)");
263$ret = $testClient->testEnum(Numberz::TWO);
264print_r(" = $ret\n");
265
266print_r("testEnum(THREE)");
267$ret = $testClient->testEnum(Numberz::THREE);
268print_r(" = $ret\n");
269
270print_r("testEnum(FIVE)");
271$ret = $testClient->testEnum(Numberz::FIVE);
272print_r(" = $ret\n");
273
274print_r("testEnum(EIGHT)");
275$ret = $testClient->testEnum(Numberz::EIGHT);
276print_r(" = $ret\n");
277
278/**
279 * TYPEDEF TEST
280 */
281print_r("testTypedef(309858235082523)");
282$uid = $testClient->testTypedef(309858235082523);
283print_r(" = $uid\n");
284
285/**
286 * NESTED MAP TEST
287 */
288print_r("testMapMap(1)");
289$mm = $testClient->testMapMap(1);
290print_r(" = {");
291foreach ($mm as $key => $val) {
292 print_r("$key => {");
293 foreach ($val as $k2 => $v2) {
294 print_r("$k2 => $v2, ");
295 }
296 print_r("}, ");
297}
298print_r("}\n");
299
300/**
301 * INSANITY TEST
302 */
303$insane = new Insanity();
304$insane->userMap[Numberz::FIVE] = 5000;
305$truck = new Xtruct();
306$truck->string_thing = "Truck";
307$truck->byte_thing = 8;
308$truck->i32_thing = 8;
309$truck->i64_thing = 8;
310$insane->xtructs []= $truck;
311print_r("testInsanity()");
312$whoa = $testClient->testInsanity($insane);
313print_r(" = {");
314foreach ($whoa as $key => $val) {
315 print_r("$key => {");
316 foreach ($val as $k2 => $v2) {
317 print_r("$k2 => {");
318 $userMap = $v2->userMap;
319 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000320 if (is_array($usermap)) {
321 foreach ($userMap as $k3 => $v3) {
322 print_r("$k3 => $v3, ");
323 }
Mark Slee6e536442006-06-30 18:28:50 +0000324 }
325 print_r("}, ");
Mark Slee5b743072007-11-13 04:00:29 +0000326
Mark Slee6e536442006-06-30 18:28:50 +0000327 $xtructs = $v2->xtructs;
328 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000329 if (is_array($xtructs)) {
330 foreach ($xtructs as $x) {
331 print_r("{\"".$x->string_thing."\", ".
332 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
333 }
Mark Slee6e536442006-06-30 18:28:50 +0000334 }
335 print_r("}");
Mark Slee5b743072007-11-13 04:00:29 +0000336
Mark Slee6e536442006-06-30 18:28:50 +0000337 print_r("}, ");
338 }
339 print_r("}, ");
340}
341print_r("}\n");
342
Mark Sleed3d733a2006-09-01 22:19:06 +0000343/**
344 * EXCEPTION TEST
345 */
346print_r("testException('Xception')");
347try {
348 $testClient->testException('Xception');
349 print_r(" void\nFAILURE\n");
350} catch (Xception $x) {
351 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
352}
353
Mark Slee6e536442006-06-30 18:28:50 +0000354
355/**
356 * Normal tests done.
357 */
358
359$stop = microtime(true);
360$elp = round(1000*($stop - $start), 0);
361print_r("Total time: $elp ms\n");
362
363/**
364 * Extraneous "I don't trust PHP to pack/unpack integer" tests
365 */
366
367// Max I32
368$num = pow(2, 30) + (pow(2, 30) - 1);
369$num2 = $testClient->testI32($num);
370if ($num != $num2) {
371 print "Missed $num = $num2\n";
372}
373
374// Min I32
375$num = 0 - pow(2, 31);
376$num2 = $testClient->testI32($num);
377if ($num != $num2) {
378 print "Missed $num = $num2\n";
379}
380
381// Max I64
382$num = pow(2, 62) + (pow(2, 62) - 1);
383$num2 = $testClient->testI64($num);
384if ($num != $num2) {
385 print "Missed $num = $num2\n";
386}
387
388// Min I64
389$num = 0 - pow(2, 63);
390$num2 = $testClient->testI64($num);
391if ($num != $num2) {
392 print "Missed $num = $num2\n";
393}
394
Mark Sleed3d733a2006-09-01 22:19:06 +0000395$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000396return;
397
398?>