blob: 27a66eac86c042f7458adf919b069e3c33f87c9f [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 Sleed3d733a2006-09-01 22:19:06 +000020require_once $GLOBALS['THRIFT_ROOT'].'/transport/TSocket.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 Sleed3d733a2006-09-01 22:19:06 +000026require_once '/home/mcslee/code/projects/thrift/test/php/'.$GEN_DIR.'/ThriftTest.php';
Mark Slee6e536442006-06-30 18:28:50 +000027
28$host = 'localhost';
29$port = 9090;
30
31if ($argc > 1) {
32 $host = $argv[0];
33}
34
35if ($argc > 2) {
36 $host = $argv[1];
37}
38
39$socket = new TSocket($host, $port);
Mark Slee6e536442006-06-30 18:28:50 +000040
Mark Sleed3d733a2006-09-01 22:19:06 +000041if ($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 Slee6e536442006-06-30 18:28:50 +000052
53$start = microtime(true);
54
55/**
56 * VOID TEST
57 */
58print_r("testVoid()");
59$testClient->testVoid();
60print_r(" = void\n");
61
62/**
63 * STRING TEST
64 */
65print_r("testString(\"Test\")");
66$s = $testClient->testString("Test");
67print_r(" = \"$s\"\n");
68
69/**
70 * BYTE TEST
71 */
72print_r("testByte(1)");
73$u8 = $testClient->testByte(1);
74print_r(" = $u8\n");
75
76/**
77 * I32 TEST
78 */
79print_r("testI32(-1)");
80$i32 = $testClient->testI32(-1);
81print_r(" = $i32\n");
82
83/**
84 * I64 TEST
85 */
86print_r("testI64(-34359738368)");
87$i64 = $testClient->testI64(-34359738368);
88print_r(" = $i64\n");
89
90/**
Mark Sleec98d0502006-09-06 02:42:25 +000091 * DOUBLE TEST
92 */
93print_r("testDouble(-852.234234234)");
94$dub = $testClient->testDouble(-852.234234234);
95print_r(" = $dub\n");
96
97/**
Mark Slee6e536442006-06-30 18:28:50 +000098 * STRUCT TEST
99 */
100print_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);
107print_r(" = {\"".$in->string_thing."\", ".
108 $in->byte_thing.", ".
109 $in->i32_thing.", ".
110 $in->i64_thing."}\n");
111
112/**
113 * NESTED STRUCT TEST
114 */
115print_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;
122print_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();
133for ($i = 0; $i < 5; ++$i) {
134 $mapout[$i] = $i-10;
135}
136print_r("testMap({");
137$first = true;
138foreach ($mapout as $key => $val) {
139 if ($first) {
140 $first = false;
141 } else {
142 print_r(", ");
143 }
144 print_r("$key => $val");
145}
146print_r("})");
147
148$mapin = $testClient->testMap($mapout);
149print_r(" = {");
150$first = true;
151foreach ($mapin as $key => $val) {
152 if ($first) {
153 $first = false;
154 } else {
155 print_r(", ");
156 }
157 print_r("$key => $val");
158}
159print_r("}\n");
160
161/**
162 * SET TEST
163 */
164$setout = array();;
165for ($i = -2; $i < 3; ++$i) {
166 $setout []= $i;
167}
168print_r("testSet({");
169$first = true;
170foreach ($setout as $val) {
171 if ($first) {
172 $first = false;
173 } else {
174 print_r(", ");
175 }
176 print_r($val);
177}
178print_r("})");
179$setin = $testClient->testSet($setout);
180print_r(" = {");
181$first = true;
182foreach ($setin as $val) {
183 if ($first) {
184 $first = false;
185 } else {
186 print_r(", ");
187 }
188 print_r($val);
189}
190print_r("}\n");
191
192/**
193 * LIST TEST
194 */
195$listout = array();
196for ($i = -2; $i < 3; ++$i) {
197 $listout []= $i;
198}
199print_r("testList({");
200$first = true;
201foreach ($listout as $val) {
202 if ($first) {
203 $first = false;
204 } else {
205 print_r(", ");
206 }
207 print_r($val);
208}
209print_r("})");
210$listin = $testClient->testList($listout);
211print_r(" = {");
212$first = true;
213foreach ($listin as $val) {
214 if ($first) {
215 $first = false;
216 } else {
217 print_r(", ");
218 }
219 print_r($val);
220}
221print_r("}\n");
222
223/**
224 * ENUM TEST
225 */
226print_r("testEnum(ONE)");
227$ret = $testClient->testEnum(Numberz::ONE);
228print_r(" = $ret\n");
229
230print_r("testEnum(TWO)");
231$ret = $testClient->testEnum(Numberz::TWO);
232print_r(" = $ret\n");
233
234print_r("testEnum(THREE)");
235$ret = $testClient->testEnum(Numberz::THREE);
236print_r(" = $ret\n");
237
238print_r("testEnum(FIVE)");
239$ret = $testClient->testEnum(Numberz::FIVE);
240print_r(" = $ret\n");
241
242print_r("testEnum(EIGHT)");
243$ret = $testClient->testEnum(Numberz::EIGHT);
244print_r(" = $ret\n");
245
246/**
247 * TYPEDEF TEST
248 */
249print_r("testTypedef(309858235082523)");
250$uid = $testClient->testTypedef(309858235082523);
251print_r(" = $uid\n");
252
253/**
254 * NESTED MAP TEST
255 */
256print_r("testMapMap(1)");
257$mm = $testClient->testMapMap(1);
258print_r(" = {");
259foreach ($mm as $key => $val) {
260 print_r("$key => {");
261 foreach ($val as $k2 => $v2) {
262 print_r("$k2 => $v2, ");
263 }
264 print_r("}, ");
265}
266print_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;
279print_r("testInsanity()");
280$whoa = $testClient->testInsanity($insane);
281print_r(" = {");
282foreach ($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 Sleed3d733a2006-09-01 22:19:06 +0000288 if (is_array($usermap)) {
289 foreach ($userMap as $k3 => $v3) {
290 print_r("$k3 => $v3, ");
291 }
Mark Slee6e536442006-06-30 18:28:50 +0000292 }
293 print_r("}, ");
294
295 $xtructs = $v2->xtructs;
296 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000297 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 Slee6e536442006-06-30 18:28:50 +0000302 }
303 print_r("}");
304
305 print_r("}, ");
306 }
307 print_r("}, ");
308}
309print_r("}\n");
310
Mark Sleed3d733a2006-09-01 22:19:06 +0000311/**
312 * EXCEPTION TEST
313 */
314print_r("testException('Xception')");
315try {
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 Slee6e536442006-06-30 18:28:50 +0000322
323/**
324 * Normal tests done.
325 */
326
327$stop = microtime(true);
328$elp = round(1000*($stop - $start), 0);
329print_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);
338if ($num != $num2) {
339 print "Missed $num = $num2\n";
340}
341
342// Min I32
343$num = 0 - pow(2, 31);
344$num2 = $testClient->testI32($num);
345if ($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);
352if ($num != $num2) {
353 print "Missed $num = $num2\n";
354}
355
356// Min I64
357$num = 0 - pow(2, 63);
358$num2 = $testClient->testI64($num);
359if ($num != $num2) {
360 print "Missed $num = $num2\n";
361}
362
Mark Sleed3d733a2006-09-01 22:19:06 +0000363$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000364return;
365
366?>