blob: 6dd40d7f2db47ec4b9e1f796cb12f2d43b09a37a [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/**
88 * STRUCT TEST
89 */
90print_r("testStruct({\"Zero\", 1, -3, -5})");
91$out = new Xtruct();
92$out->string_thing = "Zero";
93$out->byte_thing = 1;
94$out->i32_thing = -3;
95$out->i64_thing = -5;
96$in = $testClient->testStruct($out);
97print_r(" = {\"".$in->string_thing."\", ".
98 $in->byte_thing.", ".
99 $in->i32_thing.", ".
100 $in->i64_thing."}\n");
101
102/**
103 * NESTED STRUCT TEST
104 */
105print_r("testNest({1, {\"Zero\", 1, -3, -5}), 5}");
106$out2 = new Xtruct2();
107$out2->byte_thing = 1;
108$out2->struct_thing = $out;
109$out2->i32_thing = 5;
110$in2 = $testClient->testNest($out2);
111$in = $in2->struct_thing;
112print_r(" = {".$in2->byte_thing.", {\"".
113 $in->string_thing."\", ".
114 $in->byte_thing.", ".
115 $in->i32_thing.", ".
116 $in->i64_thing."}, ".
117 $in2->i32_thing."}\n");
118
119/**
120 * MAP TEST
121 */
122$mapout = array();
123for ($i = 0; $i < 5; ++$i) {
124 $mapout[$i] = $i-10;
125}
126print_r("testMap({");
127$first = true;
128foreach ($mapout as $key => $val) {
129 if ($first) {
130 $first = false;
131 } else {
132 print_r(", ");
133 }
134 print_r("$key => $val");
135}
136print_r("})");
137
138$mapin = $testClient->testMap($mapout);
139print_r(" = {");
140$first = true;
141foreach ($mapin as $key => $val) {
142 if ($first) {
143 $first = false;
144 } else {
145 print_r(", ");
146 }
147 print_r("$key => $val");
148}
149print_r("}\n");
150
151/**
152 * SET TEST
153 */
154$setout = array();;
155for ($i = -2; $i < 3; ++$i) {
156 $setout []= $i;
157}
158print_r("testSet({");
159$first = true;
160foreach ($setout as $val) {
161 if ($first) {
162 $first = false;
163 } else {
164 print_r(", ");
165 }
166 print_r($val);
167}
168print_r("})");
169$setin = $testClient->testSet($setout);
170print_r(" = {");
171$first = true;
172foreach ($setin as $val) {
173 if ($first) {
174 $first = false;
175 } else {
176 print_r(", ");
177 }
178 print_r($val);
179}
180print_r("}\n");
181
182/**
183 * LIST TEST
184 */
185$listout = array();
186for ($i = -2; $i < 3; ++$i) {
187 $listout []= $i;
188}
189print_r("testList({");
190$first = true;
191foreach ($listout as $val) {
192 if ($first) {
193 $first = false;
194 } else {
195 print_r(", ");
196 }
197 print_r($val);
198}
199print_r("})");
200$listin = $testClient->testList($listout);
201print_r(" = {");
202$first = true;
203foreach ($listin as $val) {
204 if ($first) {
205 $first = false;
206 } else {
207 print_r(", ");
208 }
209 print_r($val);
210}
211print_r("}\n");
212
213/**
214 * ENUM TEST
215 */
216print_r("testEnum(ONE)");
217$ret = $testClient->testEnum(Numberz::ONE);
218print_r(" = $ret\n");
219
220print_r("testEnum(TWO)");
221$ret = $testClient->testEnum(Numberz::TWO);
222print_r(" = $ret\n");
223
224print_r("testEnum(THREE)");
225$ret = $testClient->testEnum(Numberz::THREE);
226print_r(" = $ret\n");
227
228print_r("testEnum(FIVE)");
229$ret = $testClient->testEnum(Numberz::FIVE);
230print_r(" = $ret\n");
231
232print_r("testEnum(EIGHT)");
233$ret = $testClient->testEnum(Numberz::EIGHT);
234print_r(" = $ret\n");
235
236/**
237 * TYPEDEF TEST
238 */
239print_r("testTypedef(309858235082523)");
240$uid = $testClient->testTypedef(309858235082523);
241print_r(" = $uid\n");
242
243/**
244 * NESTED MAP TEST
245 */
246print_r("testMapMap(1)");
247$mm = $testClient->testMapMap(1);
248print_r(" = {");
249foreach ($mm as $key => $val) {
250 print_r("$key => {");
251 foreach ($val as $k2 => $v2) {
252 print_r("$k2 => $v2, ");
253 }
254 print_r("}, ");
255}
256print_r("}\n");
257
258/**
259 * INSANITY TEST
260 */
261$insane = new Insanity();
262$insane->userMap[Numberz::FIVE] = 5000;
263$truck = new Xtruct();
264$truck->string_thing = "Truck";
265$truck->byte_thing = 8;
266$truck->i32_thing = 8;
267$truck->i64_thing = 8;
268$insane->xtructs []= $truck;
269print_r("testInsanity()");
270$whoa = $testClient->testInsanity($insane);
271print_r(" = {");
272foreach ($whoa as $key => $val) {
273 print_r("$key => {");
274 foreach ($val as $k2 => $v2) {
275 print_r("$k2 => {");
276 $userMap = $v2->userMap;
277 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000278 if (is_array($usermap)) {
279 foreach ($userMap as $k3 => $v3) {
280 print_r("$k3 => $v3, ");
281 }
Mark Slee6e536442006-06-30 18:28:50 +0000282 }
283 print_r("}, ");
284
285 $xtructs = $v2->xtructs;
286 print_r("{");
Mark Sleed3d733a2006-09-01 22:19:06 +0000287 if (is_array($xtructs)) {
288 foreach ($xtructs as $x) {
289 print_r("{\"".$x->string_thing."\", ".
290 $x->byte_thing.", ".$x->i32_thing.", ".$x->i64_thing."}, ");
291 }
Mark Slee6e536442006-06-30 18:28:50 +0000292 }
293 print_r("}");
294
295 print_r("}, ");
296 }
297 print_r("}, ");
298}
299print_r("}\n");
300
Mark Sleed3d733a2006-09-01 22:19:06 +0000301/**
302 * EXCEPTION TEST
303 */
304print_r("testException('Xception')");
305try {
306 $testClient->testException('Xception');
307 print_r(" void\nFAILURE\n");
308} catch (Xception $x) {
309 print_r(' caught xception '.$x->errorCode.': '.$x->message."\n");
310}
311
Mark Slee6e536442006-06-30 18:28:50 +0000312
313/**
314 * Normal tests done.
315 */
316
317$stop = microtime(true);
318$elp = round(1000*($stop - $start), 0);
319print_r("Total time: $elp ms\n");
320
321/**
322 * Extraneous "I don't trust PHP to pack/unpack integer" tests
323 */
324
325// Max I32
326$num = pow(2, 30) + (pow(2, 30) - 1);
327$num2 = $testClient->testI32($num);
328if ($num != $num2) {
329 print "Missed $num = $num2\n";
330}
331
332// Min I32
333$num = 0 - pow(2, 31);
334$num2 = $testClient->testI32($num);
335if ($num != $num2) {
336 print "Missed $num = $num2\n";
337}
338
339// Max I64
340$num = pow(2, 62) + (pow(2, 62) - 1);
341$num2 = $testClient->testI64($num);
342if ($num != $num2) {
343 print "Missed $num = $num2\n";
344}
345
346// Min I64
347$num = 0 - pow(2, 63);
348$num2 = $testClient->testI64($num);
349if ($num != $num2) {
350 print "Missed $num = $num2\n";
351}
352
Mark Sleed3d733a2006-09-01 22:19:06 +0000353$transport->close();
Mark Slee6e536442006-06-30 18:28:50 +0000354return;
355
356?>