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