Storable compatibility with 64 bit 5.6.x
[p5sagit/p5-mst-13.2.git] / ext / Storable / t / interwork56.t
1 #!./perl -w
2
3 #
4 #  Copyright 2002, Larry Wall.
5 #
6 #  You may redistribute only under the same terms as Perl 5, as specified
7 #  in the README file that comes with the distribution.
8 #
9
10 # I ought to keep this test easily backwards compatible to 5.004, so no
11 # qr//;
12
13 # This test checks whether the kludge to interwork with 5.6 Storables compiled
14 # on Unix systems with IV as long long works.
15
16 sub BEGIN {
17     if ($ENV{PERL_CORE}){
18         chdir('t') if -d 't';
19         @INC = ('.', '../lib');
20     } else {
21         unshift @INC, 't';
22     }
23     require Config; import Config;
24     if ($ENV{PERL_CORE} and $Config{'extensions'} !~ /\bStorable\b/) {
25         print "1..0 # Skip: Storable was not built\n";
26         exit 0;
27     }
28     unless ($Config{ivsize} and $Config{ivsize} > $Config{longsize}) {
29         print "1..0 # Skip: Your IVs are no larger than your longs\n";
30         exit 0;
31     }
32 }
33
34 use Storable qw(freeze thaw);
35 use strict;
36 use Test::More tests=>30;
37
38 use vars qw(%tests);
39
40 {
41     local $/ = "\n\nend\n";
42     while (<DATA>) {
43         next unless /\S/s;
44         unless (/begin ([0-7]{3}) ([^\n]*)\n(.*)$/s) {
45             s/\n.*//s;
46             warn "Dodgy data in section starting '$_'";
47             next;
48         }
49         next unless oct $1 == ord 'A'; # Skip ASCII on EBCDIC, and vice versa
50         my $data = unpack 'u', $3;
51         $tests{$2} = $data;
52     }
53 }
54
55 # perl makes easy things easy, and hard things possible:
56 my $test = freeze \'Hell';
57
58 my $header = Storable::read_magic ($test);
59
60 is ($header->{byteorder}, $Config{byteorder},
61     "header's byteorder and Config.pm's should agree");
62
63 my $result = eval {thaw $test};
64 isa_ok ($result, 'SCALAR', "Check thawing test data");
65 is ($@, '', "causes no errors");
66 is ($$result, 'Hell', 'and gives the expected data');
67
68 my $kingdom = $Config{byteorder} =~ /23/ ? "Lillput" : "Belfuscu";
69
70 my $name = join ',', $kingdom, @$header{qw(intsize longsize ptrsize nvsize)};
71
72 SKIP: {
73     my $real_thing = $tests{$name};
74     if (!defined $real_thing) {
75         print << "EOM";
76 # No test data for Storable 1.x for:
77 #
78 # byteorder      '$Config{byteorder}'
79 # sizeof(int)    $$header{intsize}
80 # sizeof(long)   $$header{longsize}
81 # sizeof(char *) $$header{ptrsize}
82 # sizeof(NV)     $$header{nvsize}
83
84 # If you have Storable 1.x built with perl 5.6.x on this platform, please
85 # make_56_interwork.pl to generate test data, and append the test data to
86 # this test. 
87 # You may find that make_56_interwork.pl reports that your platform has no
88 # interworking problems, in which case you need do nothing.
89 EOM
90         skip "# No 1.x test file", 9;
91     }
92     my $result = eval {thaw $real_thing};
93     is ($result, undef, "By default should not be able to thaw");
94     like ($@, qr/Byte order is not compatible/,
95           "because the header byte order strings differ");
96     local $Storable::interwork_56_64bit = 1;
97     $result = eval {thaw $real_thing};
98     isa_ok ($result, 'ARRAY', "With flag should now thaw");
99     is ($@, '', "with no errors");
100
101     # However, as the file is written with Storable pre 2.01, it's a known
102     # bug that large (positive) UVs become IVs
103     my $value = (~0 ^ (~0 >> 1) ^ 2);
104
105     is (@$result, 4, "4 elements in array");
106     like ($$result[0],
107           qr/^This file was written with [0-9.]+ on perl [0-9.]+\z/,
108          "1st element");
109     is ($$result[1], "$kingdom was correct", "2nd element");
110     cmp_ok ($$result[2] ^ $value, '==', 0, "3rd element") or
111         printf "# expected %#X, got %#X\n", $value, $$result[2];
112     is ($$result[3], "The End", "4th element");
113 }
114
115 $result = eval {thaw $test};
116 isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
117 is ($@, '', "        causes no errors");
118 is ($$result, 'Hell', "        and gives the expected data");
119
120 my $test_kludge;
121 {
122     local $Storable::interwork_56_64bit = 1;
123     $test_kludge = freeze \'Heck';
124 }
125
126 my $header_kludge = Storable::read_magic ($test_kludge);
127
128 cmp_ok (length ($header_kludge->{byteorder}), '==', $Config{longsize},
129         "With 5.6 interwork kludge byteorder string should be same size as long"
130        );
131 $result = eval {thaw $test_kludge};
132 is ($result, undef, "By default should not be able to thaw");
133 like ($@, qr/Byte order is not compatible/,
134       "because the header byte order strings differ");
135
136 $result = eval {thaw $test};
137 isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
138 is ($@, '', "        causes no errors");
139 is ($$result, 'Hell', "        and gives the expected data");
140
141 {
142     local $Storable::interwork_56_64bit = 1;
143
144     $result = eval {thaw $test_kludge};
145     isa_ok ($result, 'SCALAR', "should be able to thaw kludge data");
146     is ($@, '', "with no errors");
147     is ($$result, 'Heck', "and gives expected data");
148
149     $result = eval {thaw $test};
150     is ($result, undef, "But now can't thaw real data");
151     like ($@, qr/Byte order is not compatible/,
152           "because the header byte order strings differ");
153 }
154
155 #  All together now:
156 $result = eval {thaw $test};
157 isa_ok ($result, 'SCALAR', "CHORUS: check thawing test data");
158 is ($@, '', "        causes no errors");
159 is ($$result, 'Hell', "        and gives the expected data");
160
161 __END__
162 # A whole run of 1.1.14 freeze data, uuencoded. The "mode bits" are the octal
163 # value of 'A', the "file name" is the test name. Use make_56_interwork.pl
164 # with a copy of Storable 1.X generate these.
165
166 # byteorder      '1234'
167 # sizeof(int)    4
168 # sizeof(long)   4
169 # sizeof(char *) 4
170 # sizeof(NV)     8
171 begin 101 Lillput,4,4,4,8
172 M!`0$,3(S-`0$!`@"!`````HQ5&AI<R!F:6QE('=A<R!W<FET=&5N('=I=&@@
173 M,2XP,30@;VX@<&5R;"`U+C`P-C`P,0H33&EL;'!U="!W87,@8V]R<F5C=`8"
174 0````````@`H'5&AE($5N9```
175
176 end
177
178 # byteorder      '4321'
179 # sizeof(int)    4
180 # sizeof(long)   4
181 # sizeof(char *) 4
182 # sizeof(NV)     8
183 begin 101 Belfuscu,4,4,4,8
184 M!`0$-#,R,00$!`@"````!`HQ5&AI<R!F:6QE('=A<R!W<FET=&5N('=I=&@@
185 M,2XP,30@;VX@<&5R;"`U+C`P-C`P,0H40F5L9G5S8W4@=V%S(&-O<G)E8W0&
186 1@`````````(*!U1H92!%;F0`
187
188 end
189