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