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