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