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