Blame t/interwork56.t

Packit 14c646
#!./perl -w
Packit 14c646
#
Packit 14c646
#  Copyright 2002, Larry Wall.
Packit 14c646
#
Packit 14c646
#  You may redistribute only under the same terms as Perl 5, as specified
Packit 14c646
#  in the README file that comes with the distribution.
Packit 14c646
#
Packit 14c646
Packit 14c646
# I ought to keep this test easily backwards compatible to 5.004, so no
Packit 14c646
# qr//;
Packit 14c646
Packit 14c646
# This test checks whether the kludge to interwork with 5.6 Storables compiled
Packit 14c646
# on Unix systems with IV as long long works.
Packit 14c646
Packit 14c646
sub BEGIN {
Packit 14c646
    unshift @INC, 't';
Packit 14c646
    unshift @INC, 't/compat' if $] < 5.006002;
Packit 14c646
    require Config; import Config;
Packit 14c646
    if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
Packit 14c646
        print "1..0 # Skip: Storable was not built\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
    unless ($Config{ivsize} and $Config{ivsize} > $Config{longsize}) {
Packit 14c646
        print "1..0 # Skip: Your IVs are no larger than your longs\n";
Packit 14c646
        exit 0;
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
use Storable qw(freeze thaw);
Packit 14c646
use strict;
Packit 14c646
use Test::More tests=>30;
Packit 14c646
Packit 14c646
our (%tests);
Packit 14c646
Packit 14c646
{
Packit 14c646
    local $/ = "\n\nend\n";
Packit 14c646
    while (<DATA>) {
Packit 14c646
        next unless /\S/s;
Packit 14c646
        unless (/begin ([0-7]{3}) ([^\n]*)\n(.*)$/s) {
Packit 14c646
            s/\n.*//s;
Packit 14c646
            warn "Dodgy data in section starting '$_'";
Packit 14c646
            next;
Packit 14c646
        }
Packit 14c646
        next unless oct $1 == ord 'A'; # Skip ASCII on EBCDIC, and vice versa
Packit 14c646
        my $data = unpack 'u', $3;
Packit 14c646
        $tests{$2} = $data;
Packit 14c646
    }
Packit 14c646
}
Packit 14c646
Packit 14c646
# perl makes easy things easy, and hard things possible:
Packit 14c646
my $test = freeze \'Hell';
Packit 14c646
Packit 14c646
my $header = Storable::read_magic ($test);
Packit 14c646
Packit 14c646
is ($header->{byteorder}, $Config{byteorder},
Packit 14c646
    "header's byteorder and Config.pm's should agree");
Packit 14c646
Packit 14c646
my $result = eval {thaw $test};
Packit 14c646
isa_ok ($result, 'SCALAR', "Check thawing test data");
Packit 14c646
is ($@, '', "causes no errors");
Packit 14c646
is ($$result, 'Hell', 'and gives the expected data');
Packit 14c646
Packit 14c646
my $kingdom = $Config{byteorder} =~ /23/ ? "Lillput" : "Belfuscu";
Packit 14c646
Packit 14c646
my $name = join ',', $kingdom, @$header{qw(intsize longsize ptrsize nvsize)};
Packit 14c646
Packit 14c646
SKIP: {
Packit 14c646
    my $real_thing = $tests{$name};
Packit 14c646
    if (!defined $real_thing) {
Packit 14c646
        print << "EOM";
Packit 14c646
# No test data for Storable 1.x for:
Packit 14c646
#
Packit 14c646
# byteorder	 '$Config{byteorder}'
Packit 14c646
# sizeof(int)	 $$header{intsize}
Packit 14c646
# sizeof(long)	 $$header{longsize}
Packit 14c646
# sizeof(char *) $$header{ptrsize}
Packit 14c646
# sizeof(NV)	 $$header{nvsize}
Packit 14c646
Packit 14c646
# If you have Storable 1.x built with perl 5.6.x on this platform, please
Packit 14c646
# make_56_interwork.pl to generate test data, and append the test data to
Packit 14c646
# this test. 
Packit 14c646
# You may find that make_56_interwork.pl reports that your platform has no
Packit 14c646
# interworking problems, in which case you need do nothing.
Packit 14c646
EOM
Packit 14c646
        skip "# No 1.x test file", 9;
Packit 14c646
    }
Packit 14c646
    my $result = eval {thaw $real_thing};
Packit 14c646
    is ($result, undef, "By default should not be able to thaw");
Packit 14c646
    like ($@, qr/Byte order is not compatible/,
Packit 14c646
          "because the header byte order strings differ");
Packit 14c646
    local $Storable::interwork_56_64bit = 1;
Packit 14c646
    $result = eval {thaw $real_thing};
Packit 14c646
    isa_ok ($result, 'ARRAY', "With flag should now thaw");
Packit 14c646
    is ($@, '', "with no errors");
Packit 14c646
Packit 14c646
    # However, as the file is written with Storable pre 2.01, it's a known
Packit 14c646
    # bug that large (positive) UVs become IVs
Packit 14c646
    my $value = (~0 ^ (~0 >> 1) ^ 2);
Packit 14c646
Packit 14c646
    is (@$result, 4, "4 elements in array");
Packit 14c646
    like ($$result[0],
Packit 14c646
          qr/^This file was written with [0-9.]+ on perl [0-9.]+\z/,
Packit 14c646
         "1st element");
Packit 14c646
    is ($$result[1], "$kingdom was correct", "2nd element");
Packit 14c646
    cmp_ok ($$result[2] ^ $value, '==', 0, "3rd element") or
Packit 14c646
        printf "# expected %#X, got %#X\n", $value, $$result[2];
Packit 14c646
    is ($$result[3], "The End", "4th element");
Packit 14c646
}
Packit 14c646
Packit 14c646
$result = eval {thaw $test};
Packit 14c646
isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
Packit 14c646
is ($@, '', "        causes no errors");
Packit 14c646
is ($$result, 'Hell', "        and gives the expected data");
Packit 14c646
Packit 14c646
my $test_kludge;
Packit 14c646
{
Packit 14c646
    local $Storable::interwork_56_64bit = 1;
Packit 14c646
    $test_kludge = freeze \'Heck';
Packit 14c646
}
Packit 14c646
Packit 14c646
my $header_kludge = Storable::read_magic ($test_kludge);
Packit 14c646
Packit 14c646
cmp_ok (length ($header_kludge->{byteorder}), '==', $Config{longsize},
Packit 14c646
        "With 5.6 interwork kludge byteorder string should be same size as long"
Packit 14c646
       );
Packit 14c646
$result = eval {thaw $test_kludge};
Packit 14c646
is ($result, undef, "By default should not be able to thaw");
Packit 14c646
like ($@, qr/Byte order is not compatible/,
Packit 14c646
      "because the header byte order strings differ");
Packit 14c646
Packit 14c646
$result = eval {thaw $test};
Packit 14c646
isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
Packit 14c646
is ($@, '', "        causes no errors");
Packit 14c646
is ($$result, 'Hell', "        and gives the expected data");
Packit 14c646
Packit 14c646
{
Packit 14c646
    local $Storable::interwork_56_64bit = 1;
Packit 14c646
Packit 14c646
    $result = eval {thaw $test_kludge};
Packit 14c646
    isa_ok ($result, 'SCALAR', "should be able to thaw kludge data");
Packit 14c646
    is ($@, '', "with no errors");
Packit 14c646
    is ($$result, 'Heck', "and gives expected data");
Packit 14c646
Packit 14c646
    $result = eval {thaw $test};
Packit 14c646
    is ($result, undef, "But now can't thaw real data");
Packit 14c646
    like ($@, qr/Byte order is not compatible/,
Packit 14c646
          "because the header byte order strings differ");
Packit 14c646
}
Packit 14c646
Packit 14c646
#  All together now:
Packit 14c646
$result = eval {thaw $test};
Packit 14c646
isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
Packit 14c646
is ($@, '', "        causes no errors");
Packit 14c646
is ($$result, 'Hell', "        and gives the expected data");
Packit 14c646
Packit 14c646
__END__
Packit 14c646
# A whole run of 1.1.14 freeze data, uuencoded. The "mode bits" are the octal
Packit 14c646
# value of 'A', the "file name" is the test name. Use make_56_interwork.pl
Packit 14c646
# with a copy of Storable 1.X generate these.
Packit 14c646
Packit 14c646
# byteorder      '1234'
Packit 14c646
# sizeof(int)    4
Packit 14c646
# sizeof(long)   4
Packit 14c646
# sizeof(char *) 4
Packit 14c646
# sizeof(NV)     8
Packit 14c646
begin 101 Lillput,4,4,4,8
Packit 14c646
M!`0$,3(S-`0$!`@"!`````HQ5&AI
Packit 14c646
M,2XP,30@;VX@<&5;;"`U+C`P-C`P,0H33&EL;'!U="!W87,@8V]R
Packit 14c646
0````````@`H'5&AE($5N9```
Packit 14c646
Packit 14c646
end
Packit 14c646
Packit 14c646
# byteorder      '4321'
Packit 14c646
# sizeof(int)    4
Packit 14c646
# sizeof(long)   4
Packit 14c646
# sizeof(char *) 4
Packit 14c646
# sizeof(NV)     8
Packit 14c646
begin 101 Belfuscu,4,4,4,8
Packit 14c646
M!`0$-#,R,00$!`@"````!`HQ5&AI
Packit 14c646
M,2XP,30@;VX@<&5;;"`U+C`P-C`P,0H40F5L9G5S8W4@=V%S(&-O
Packit 14c646
1@`````````(*!U1H92!%;F0`
Packit 14c646
Packit 14c646
end
Packit 14c646
Packit 14c646
# byteorder      '1234'
Packit 14c646
# sizeof(int)    4
Packit 14c646
# sizeof(long)   4
Packit 14c646
# sizeof(char *) 4
Packit 14c646
# sizeof(NV)     12
Packit 14c646
begin 101 Lillput,4,4,4,12
Packit 14c646
M!`0$,3(S-`0$!`P"!`````HQ5&AI
Packit 14c646
M,2XP,30@;VX@<&5;;"`U+C`P-C`P,0H33&EL;'!U="!W87,@8V]R
Packit 14c646
0````````@`H'5&AE($5N9```
Packit 14c646
Packit 14c646
end
Packit 14c646