1N/A#!./perl -w
1N/A#
1N/A# Copyright 2002, Larry Wall.
1N/A#
1N/A# You may redistribute only under the same terms as Perl 5, as specified
1N/A# in the README file that comes with the distribution.
1N/A#
1N/A
1N/A# I ought to keep this test easily backwards compatible to 5.004, so no
1N/A# qr//;
1N/A
1N/A# This test checks downgrade behaviour on pre-5.8 perls when new 5.8 features
1N/A# are encountered.
1N/A
1N/Asub BEGIN {
1N/A if ($ENV{PERL_CORE}){
1N/A chdir('t') if -d 't';
1N/A @INC = ('.', '../lib');
1N/A } else {
1N/A unshift @INC, 't';
1N/A }
1N/A require Config; import Config;
1N/A if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
1N/A print "1..0 # Skip: Storable was not built\n";
1N/A exit 0;
1N/A }
1N/A}
1N/A
1N/Ause Test::More;
1N/Ause Storable qw (dclone store retrieve freeze thaw nstore nfreeze);
1N/Ause strict;
1N/A
1N/Amy $max_uv = ~0;
1N/Amy $max_uv_m1 = ~0 ^ 1;
1N/A# Express it in this way so as not to use any addition, as 5.6 maths would
1N/A# do this in NVs on 64 bit machines, and we're overflowing IVs so can't use
1N/A# use integer.
1N/Amy $max_iv_p1 = $max_uv ^ ($max_uv >> 1);
1N/Amy $lots_of_9C = do {
1N/A my $temp = sprintf "%#x", ~0;
1N/A $temp =~ s/ff/9c/g;
1N/A local $^W;
1N/A eval $temp;
1N/A};
1N/A
1N/Amy $max_iv = ~0 >> 1;
1N/Amy $min_iv = do {use integer; -$max_iv-1}; # 2s complement assumption
1N/A
1N/Amy @processes = (["dclone", \&do_clone],
1N/A ["freeze/thaw", \&freeze_and_thaw],
1N/A ["nfreeze/thaw", \&nfreeze_and_thaw],
1N/A ["store/retrieve", \&store_and_retrieve],
1N/A ["nstore/retrieve", \&nstore_and_retrieve],
1N/A );
1N/Amy @numbers =
1N/A (# IV bounds of 8 bits
1N/A -1, 0, 1, -127, -128, -129, 42, 126, 127, 128, 129, 254, 255, 256, 257,
1N/A # IV bounds of 32 bits
1N/A -2147483647, -2147483648, -2147483649, 2147483646, 2147483647, 2147483648,
1N/A # IV bounds
1N/A $min_iv, do {use integer; $min_iv + 1}, do {use integer; $max_iv - 1},
1N/A $max_iv,
1N/A # UV bounds at 32 bits
1N/A 0x7FFFFFFF, 0x80000000, 0x80000001, 0xFFFFFFFF, 0xDEADBEEF,
1N/A # UV bounds
1N/A $max_iv_p1, $max_uv_m1, $max_uv, $lots_of_9C,
1N/A # NV-UV conversion
1N/A 2559831922.0,
1N/A );
1N/A
1N/Aplan tests => @processes * @numbers * 5;
1N/A
1N/Amy $file = "integer.$$";
1N/Adie "Temporary file '$file' already exists" if -e $file;
1N/A
1N/AEND { while (-f $file) {unlink $file or die "Can't unlink '$file': $!" }}
1N/A
1N/Asub do_clone {
1N/A my $data = shift;
1N/A my $copy = eval {dclone $data};
1N/A is ($@, '', 'Should be no error dcloning');
1N/A ok (1, "dlcone is only 1 process, not 2");
1N/A return $copy;
1N/A}
1N/A
1N/Asub freeze_and_thaw {
1N/A my $data = shift;
1N/A my $frozen = eval {freeze $data};
1N/A is ($@, '', 'Should be no error freezing');
1N/A my $copy = eval {thaw $frozen};
1N/A is ($@, '', 'Should be no error thawing');
1N/A return $copy;
1N/A}
1N/A
1N/Asub nfreeze_and_thaw {
1N/A my $data = shift;
1N/A my $frozen = eval {nfreeze $data};
1N/A is ($@, '', 'Should be no error nfreezing');
1N/A my $copy = eval {thaw $frozen};
1N/A is ($@, '', 'Should be no error thawing');
1N/A return $copy;
1N/A}
1N/A
1N/Asub store_and_retrieve {
1N/A my $data = shift;
1N/A my $frozen = eval {store $data, $file};
1N/A is ($@, '', 'Should be no error storing');
1N/A my $copy = eval {retrieve $file};
1N/A is ($@, '', 'Should be no error retrieving');
1N/A return $copy;
1N/A}
1N/A
1N/Asub nstore_and_retrieve {
1N/A my $data = shift;
1N/A my $frozen = eval {nstore $data, $file};
1N/A is ($@, '', 'Should be no error storing');
1N/A my $copy = eval {retrieve $file};
1N/A is ($@, '', 'Should be no error retrieving');
1N/A return $copy;
1N/A}
1N/A
1N/Aforeach (@processes) {
1N/A my ($process, $sub) = @$_;
1N/A foreach my $number (@numbers) {
1N/A # as $number is an alias into @numbers, we don't want any side effects of
1N/A # conversion macros affecting later runs, so pass a copy to Storable:
1N/A my $copy1 = my $copy2 = my $copy0 = $number;
1N/A my $copy_s = &$sub (\$copy0);
1N/A if (is (ref $copy_s, "SCALAR", "got back a scalar ref?")) {
1N/A # Test inside use integer to see if the bit pattern is identical
1N/A # and outside to see if the sign is right.
1N/A # On 5.8 we don't need this trickery anymore.
1N/A # We really do need 2 copies here, as conversion may have side effect
1N/A # bugs. In particular, I know that this happens:
1N/A # perl5.00503 -le '$a = "-2147483649"; $a & 0; print $a; print $a+1'
1N/A # -2147483649
1N/A # 2147483648
1N/A
1N/A my $copy_s1 = my $copy_s2 = $$copy_s;
1N/A # On 5.8 can do this with a straight ==, due to the integer/float maths
1N/A # on 5.6 can't do this with
1N/A # my $eq = do {use integer; $copy_s1 == $copy1} && $copy_s1 == $copy1;
1N/A # because on builds with IV as long long it tickles bugs.
1N/A # (Uncomment it and the Devel::Peek line below to see the messed up
1N/A # state of the scalar, with PV showing the correct string for the
1N/A # number, and IV holding a bogus value which has been truncated to 32 bits
1N/A
1N/A # So, check the bit patterns are identical, and check that the sign is the
1N/A # same. This works on all the versions in all the sizes.
1N/A # $eq = && (($copy_s1 <=> 0) == ($copy1 <=> 0));
1N/A # Split this into 2 tests, to cater for 5.005_03
1N/A
1N/A # Aargh. Even this doesn't work because 5.6.x sends values with (same
1N/A # number of decimal digits as ~0 + 1) via atof. So ^ is getting strings
1N/A # cast to doubles cast to integers. And that truncates low order bits.
1N/A # my $bit = ok (($copy_s1 ^ $copy1) == 0, "$process $copy1 (bitpattern)");
1N/A
1N/A # Oh well; at least the parser gets it right. :-)
1N/A my $copy_s3 = eval $copy_s1;
1N/A die "Was supposed to have number $copy_s3, got error $@"
1N/A unless defined $copy_s3;
1N/A my $bit = ok (($copy_s3 ^ $copy1) == 0, "$process $copy1 (bitpattern)");
1N/A # This is sick. 5.005_03 survives without the IV/UV flag, and somehow
1N/A # gets it right, providing you don't have side effects of conversion.
1N/A# local $TODO;
1N/A# $TODO = "pre 5.6 doesn't have flag to distinguish IV/UV"
1N/A# if $[ < 5.005_56 and $copy1 > $max_iv;
1N/A my $sign = ok (($copy_s2 <=> 0) == ($copy2 <=> 0),
1N/A "$process $copy1 (sign)");
1N/A
1N/A unless ($bit and $sign) {
1N/A printf "# Passed in %s (%#x, %i)\n# got back '%s' (%#x, %i)\n",
1N/A $copy1, $copy1, $copy1, $copy_s1, $copy_s1, $copy_s1;
1N/A # use Devel::Peek; Dump $number; Dump $copy1; Dump $copy_s1;
1N/A }
1N/A # unless ($bit) { use Devel::Peek; Dump $copy_s1; Dump $$copy_s; }
1N/A } else {
1N/A fail ("$process $copy1");
1N/A fail ("$process $copy1");
1N/A }
1N/A }
1N/A}