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