1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
BEGIN {
if ($ENV{PERL_CORE}) {
chdir 't' if -d 't';
@INC = ("../lib", "lib/compress");
}
}
use lib qw(t t/compress);
use strict;
use warnings;
use bytes;
use Test::More ;
use CompTestUtils;
BEGIN {
# use Test::NoWarnings, if available
my $extra = 0 ;
$extra = 1
if eval { require Test::NoWarnings ; import Test::NoWarnings; 1 };
plan tests => 163 + $extra ;
use_ok('Scalar::Util');
use_ok('IO::Compress::Base::Common');
}
ok gotScalarUtilXS(), "Got XS Version of Scalar::Util"
or diag <<EOM;
You don't have the XS version of Scalar::Util
EOM
# Compress::Zlib::Common;
sub My::testParseParameters()
{
eval { ParseParameters(1, {}, 1) ; };
like $@, mkErr(': Expected even number of parameters, got 1'),
"Trap odd number of params";
eval { ParseParameters(1, {}, undef) ; };
like $@, mkErr(': Expected even number of parameters, got 1'),
"Trap odd number of params";
eval { ParseParameters(1, {}, []) ; };
like $@, mkErr(': Expected even number of parameters, got 1'),
"Trap odd number of params";
eval { ParseParameters(1, {'fred' => [Parse_boolean, 0]}, fred => 'joe') ; };
like $@, mkErr("Parameter 'fred' must be an int, got 'joe'"),
"wanted unsigned, got undef";
eval { ParseParameters(1, {'fred' => [Parse_unsigned, 0]}, fred => undef) ; };
like $@, mkErr("Parameter 'fred' must be an unsigned int, got 'undef'"),
"wanted unsigned, got undef";
eval { ParseParameters(1, {'fred' => [Parse_signed, 0]}, fred => undef) ; };
like $@, mkErr("Parameter 'fred' must be a signed int, got 'undef'"),
"wanted signed, got undef";
eval { ParseParameters(1, {'fred' => [Parse_signed, 0]}, fred => 'abc') ; };
like $@, mkErr("Parameter 'fred' must be a signed int, got 'abc'"),
"wanted signed, got 'abc'";
eval { ParseParameters(1, {'fred' => [Parse_code, undef]}, fred => 'abc') ; };
like $@, mkErr("Parameter 'fred' must be a code reference, got 'abc'"),
"wanted code, got 'abc'";
SKIP:
{
use Config;
skip 'readonly + threads', 2
if $Config{useithreads};
eval { ParseParameters(1, {'fred' => [Parse_writable_scalar, 0]}, fred => 'abc') ; };
like $@, mkErr("Parameter 'fred' not writable"),
"wanted writable, got readonly";
skip '\\ returns mutable value in 5.19.3', 1
if $] >= 5.019003;
eval { ParseParameters(1, {'fred' => [Parse_writable_scalar, 0]}, fred => \'abc') ; };
like $@, mkErr("Parameter 'fred' not writable"),
"wanted writable, got readonly";
}
my @xx;
eval { ParseParameters(1, {'fred' => [Parse_writable_scalar, 0]}, fred => \@xx) ; };
like $@, mkErr("Parameter 'fred' not a scalar reference"),
"wanted scalar reference";
local *ABC;
eval { ParseParameters(1, {'fred' => [Parse_writable_scalar, 0]}, fred => *ABC) ; };
like $@, mkErr("Parameter 'fred' not a scalar"),
"wanted scalar";
eval { ParseParameters(1, {'fred' => [Parse_any, 0]}, fred => 1, fred => 2) ; };
like $@, mkErr("Muliple instances of 'fred' found"),
"multiple instances";
# my $g = ParseParameters(1, {'fred' => [Parse_unsigned|Parse_multiple, 7]}, fred => 1, fred => 2) ;
# is_deeply $g->value('fred'), [ 1, 2 ] ;
ok 1;
#ok 1;
my $got = ParseParameters(1, {'fred' => [0x1000000, 0]}, fred => 'abc') ;
is $got->getValue('fred'), "abc", "other" ;
$got = ParseParameters(1, {'fred' => [Parse_any, undef]}, fred => undef) ;
ok $got->parsed('fred'), "undef" ;
ok ! defined $got->getValue('fred'), "undef" ;
$got = ParseParameters(1, {'fred' => [Parse_string, undef]}, fred => undef) ;
ok $got->parsed('fred'), "undef" ;
is $got->getValue('fred'), "", "empty string" ;
my $xx;
$got = ParseParameters(1, {'fred' => [Parse_writable_scalar, undef]}, fred => $xx) ;
ok $got->parsed('fred'), "parsed" ;
my $xx_ref = $got->getValue('fred');
$$xx_ref = 77 ;
is $xx, 77;
$got = ParseParameters(1, {'fred' => [Parse_writable_scalar, undef]}, fred => \$xx) ;
ok $got->parsed('fred'), "parsed" ;
$xx_ref = $got->getValue('fred');
$$xx_ref = 666 ;
is $xx, 666;
{
my $got1 = ParseParameters(1, {'fred' => [Parse_writable_scalar, undef]}, $got) ;
is $got1, $got, "Same object";
ok $got1->parsed('fred'), "parsed" ;
$xx_ref = $got1->getValue('fred');
$$xx_ref = 777 ;
is $xx, 777;
}
for my $type (Parse_unsigned, Parse_signed, Parse_any)
{
my $value = 0;
my $got1 ;
eval { $got1 = ParseParameters(1, {'fred' => [$type, 1]}, fred => $value) } ;
ok ! $@;
ok $got1->parsed('fred'), "parsed ok" ;
is $got1->getValue('fred'), 0;
}
{
# setValue/getValue
my $value = 0;
my $got1 ;
eval { $got1 = ParseParameters(1, {'fred' => [Parse_any, 1]}, fred => $value) } ;
ok ! $@;
ok $got1->parsed('fred'), "parsed ok" ;
is $got1->getValue('fred'), 0;
$got1->setValue('fred' => undef);
is $got1->getValue('fred'), undef;
}
{
# twice
my $value = 0;
my $got = IO::Compress::Base::Parameters::new();
ok $got->parse({'fred' => [Parse_any, 1]}, fred => $value) ;
ok $got->parsed('fred'), "parsed ok" ;
is $got->getValue('fred'), 0;
ok $got->parse({'fred' => [Parse_any, 1]}, fred => undef) ;
ok $got->parsed('fred'), "parsed ok" ;
is $got->getValue('fred'), undef;
ok $got->parse({'fred' => [Parse_any, 1]}, fred => 7) ;
ok $got->parsed('fred'), "parsed ok" ;
is $got->getValue('fred'), 7;
}
}
My::testParseParameters();
{
title "isaFilename" ;
ok isaFilename("abc"), "'abc' isaFilename";
ok ! isaFilename(undef), "undef ! isaFilename";
ok ! isaFilename([]), "[] ! isaFilename";
$main::X = 1; $main::X = $main::X ;
ok ! isaFilename(*X), "glob ! isaFilename";
}
{
title "whatIsInput" ;
my $lex = new LexFile my $out_file ;
open FH, ">$out_file" ;
is whatIsInput(*FH), 'handle', "Match filehandle" ;
close FH ;
my $stdin = '-';
is whatIsInput($stdin), 'handle', "Match '-' as stdin";
#is $stdin, \*STDIN, "'-' changed to *STDIN";
#isa_ok $stdin, 'IO::File', "'-' changed to IO::File";
is whatIsInput("abc"), 'filename', "Match filename";
is whatIsInput(\"abc"), 'buffer', "Match buffer";
is whatIsInput(sub { 1 }, 1), 'code', "Match code";
is whatIsInput(sub { 1 }), '' , "Don't match code";
}
{
title "whatIsOutput" ;
my $lex = new LexFile my $out_file ;
open FH, ">$out_file" ;
is whatIsOutput(*FH), 'handle', "Match filehandle" ;
close FH ;
my $stdout = '-';
is whatIsOutput($stdout), 'handle', "Match '-' as stdout";
#is $stdout, \*STDOUT, "'-' changed to *STDOUT";
#isa_ok $stdout, 'IO::File', "'-' changed to IO::File";
is whatIsOutput("abc"), 'filename', "Match filename";
is whatIsOutput(\"abc"), 'buffer', "Match buffer";
is whatIsOutput(sub { 1 }, 1), 'code', "Match code";
is whatIsOutput(sub { 1 }), '' , "Don't match code";
}
# U64
{
title "U64" ;
my $x = new U64();
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0, " getLow is 0";
ok ! $x->is64bit(), " ! is64bit";
$x = new U64(1,2);
is $x->getHigh, 1, " getHigh is 1";
is $x->getLow, 2, " getLow is 2";
ok $x->is64bit(), " is64bit";
$x = new U64(0xFFFFFFFF,2);
is $x->getHigh, 0xFFFFFFFF, " getHigh is 0xFFFFFFFF";
is $x->getLow, 2, " getLow is 2";
ok $x->is64bit(), " is64bit";
$x = new U64(7, 0xFFFFFFFF);
is $x->getHigh, 7, " getHigh is 7";
is $x->getLow, 0xFFFFFFFF, " getLow is 0xFFFFFFFF";
ok $x->is64bit(), " is64bit";
$x = new U64(666);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 666, " getLow is 666";
ok ! $x->is64bit(), " ! is64bit";
title "U64 - add" ;
$x = new U64(0, 1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 1, " getLow is 1";
ok ! $x->is64bit(), " ! is64bit";
$x->add(1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 2, " getLow is 2";
ok ! $x->is64bit(), " ! is64bit";
$x = new U64(0, 0xFFFFFFFE);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0xFFFFFFFE, " getLow is 0xFFFFFFFE";
is $x->get32bit(), 0xFFFFFFFE, " get32bit is 0xFFFFFFFE";
is $x->get64bit(), 0xFFFFFFFE, " get64bit is 0xFFFFFFFE";
ok ! $x->is64bit(), " ! is64bit";
$x->add(1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0xFFFFFFFF, " getLow is 0xFFFFFFFF";
is $x->get32bit(), 0xFFFFFFFF, " get32bit is 0xFFFFFFFF";
is $x->get64bit(), 0xFFFFFFFF, " get64bit is 0xFFFFFFFF";
ok ! $x->is64bit(), " ! is64bit";
$x->add(1);
is $x->getHigh, 1, " getHigh is 1";
is $x->getLow, 0, " getLow is 0";
is $x->get32bit(), 0x0, " get32bit is 0x0";
is $x->get64bit(), 0xFFFFFFFF+1, " get64bit is 0x100000000";
ok $x->is64bit(), " is64bit";
$x->add(1);
is $x->getHigh, 1, " getHigh is 1";
is $x->getLow, 1, " getLow is 1";
is $x->get32bit(), 0x1, " get32bit is 0x1";
is $x->get64bit(), 0xFFFFFFFF+2, " get64bit is 0x100000001";
ok $x->is64bit(), " is64bit";
$x->add(1);
is $x->getHigh, 1, " getHigh is 1";
is $x->getLow, 2, " getLow is 1";
is $x->get32bit(), 0x2, " get32bit is 0x2";
is $x->get64bit(), 0xFFFFFFFF+3, " get64bit is 0x100000002";
ok $x->is64bit(), " is64bit";
$x = new U64(1, 0xFFFFFFFE);
my $y = new U64(2, 3);
$x->add($y);
is $x->getHigh, 4, " getHigh is 4";
is $x->getLow, 1, " getLow is 1";
ok $x->is64bit(), " is64bit";
title "U64 - subtract" ;
$x = new U64(0, 1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 1, " getLow is 1";
ok ! $x->is64bit(), " ! is64bit";
$x->subtract(1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0, " getLow is 0";
ok ! $x->is64bit(), " ! is64bit";
$x = new U64(1, 0);
is $x->getHigh, 1, " getHigh is 1";
is $x->getLow, 0, " getLow is 0";
is $x->get32bit(), 0, " get32bit is 0xFFFFFFFE";
is $x->get64bit(), 0xFFFFFFFF+1, " get64bit is 0x100000000";
ok $x->is64bit(), " is64bit";
$x->subtract(1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0xFFFFFFFF, " getLow is 0xFFFFFFFF";
is $x->get32bit(), 0xFFFFFFFF, " get32bit is 0xFFFFFFFF";
is $x->get64bit(), 0xFFFFFFFF, " get64bit is 0xFFFFFFFF";
ok ! $x->is64bit(), " ! is64bit";
$x = new U64(2, 2);
$y = new U64(1, 3);
$x->subtract($y);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 0xFFFFFFFF, " getLow is 1";
ok ! $x->is64bit(), " ! is64bit";
$x = new U64(0x01CADCE2, 0x4E815983);
$y = new U64(0x19DB1DE, 0xD53E8000); # NTFS to Unix time delta
$x->subtract($y);
is $x->getHigh, 0x2D2B03, " getHigh is 2D2B03";
is $x->getLow, 0x7942D983, " getLow is 7942D983";
ok $x->is64bit(), " is64bit";
title "U64 - equal" ;
$x = new U64(0, 1);
is $x->getHigh, 0, " getHigh is 0";
is $x->getLow, 1, " getLow is 1";
ok ! $x->is64bit(), " ! is64bit";
$y = new U64(0, 1);
is $y->getHigh, 0, " getHigh is 0";
is $y->getLow, 1, " getLow is 1";
ok ! $y->is64bit(), " ! is64bit";
my $z = new U64(0, 2);
is $z->getHigh, 0, " getHigh is 0";
is $z->getLow, 2, " getLow is 2";
ok ! $z->is64bit(), " ! is64bit";
ok $x->equal($y), " equal";
ok !$x->equal($z), " ! equal";
title "U64 - clone" ;
$x = new U64(21, 77);
$z = U64::clone($x);
is $z->getHigh, 21, " getHigh is 21";
is $z->getLow, 77, " getLow is 77";
title "U64 - cmp.gt" ;
$x = new U64 1;
$y = new U64 0;
cmp_ok $x->cmp($y), '>', 0, " cmp > 0";
is $x->gt($y), 1, " gt";
cmp_ok $y->cmp($x), '<', 0, " cmp < 0";
}
|