Commit | Line | Data |
a687059c |
1 | #!./perl |
2 | |
79072805 |
3 | # $RCSfile: pack.t,v $$Revision: 4.1 $$Date: 92/08/07 18:28:11 $ |
a687059c |
4 | |
a1a0e61e |
5 | BEGIN { |
6 | chdir 't' if -d 't'; |
93430cb4 |
7 | unshift @INC, '../lib' if -d '../lib'; |
a1a0e61e |
8 | require Config; import Config; |
9 | } |
10 | |
5a929a98 |
11 | print "1..72\n"; |
a687059c |
12 | |
bbdab043 |
13 | $format = "c2 x5 C C x s d i l a6"; |
450a55e4 |
14 | # Need the expression in here to force ary[5] to be numeric. This avoids |
15 | # test2 failing because ary2 goes str->numeric->str and ary doesn't. |
16 | @ary = (1,-100,127,128,32767,987.654321098 / 100.0,12345,123456,"abcdef"); |
a687059c |
17 | $foo = pack($format,@ary); |
18 | @ary2 = unpack($format,$foo); |
19 | |
20 | print ($#ary == $#ary2 ? "ok 1\n" : "not ok 1\n"); |
21 | |
22 | $out1=join(':',@ary); |
23 | $out2=join(':',@ary2); |
24 | print ($out1 eq $out2 ? "ok 2\n" : "not ok 2\n"); |
25 | |
26 | print ($foo =~ /def/ ? "ok 3\n" : "not ok 3\n"); |
79072805 |
27 | |
28 | # How about counting bits? |
29 | |
30 | print +($x = unpack("%32B*", "\001\002\004\010\020\040\100\200\377")) == 16 |
31 | ? "ok 4\n" : "not ok 4 $x\n"; |
32 | |
33 | print +($x = unpack("%32b69", "\001\002\004\010\020\040\100\200\017")) == 12 |
34 | ? "ok 5\n" : "not ok 5 $x\n"; |
35 | |
36 | print +($x = unpack("%32B69", "\001\002\004\010\020\040\100\200\017")) == 9 |
37 | ? "ok 6\n" : "not ok 6 $x\n"; |
38 | |
9d116dd7 |
39 | my $sum = 129; # ASCII |
a1a0e61e |
40 | $sum = 103 if ($Config{ebcdic} eq 'define'); |
9d116dd7 |
41 | |
42 | print +($x = unpack("%32B*", "Now is the time for all good blurfl")) == $sum |
79072805 |
43 | ? "ok 7\n" : "not ok 7 $x\n"; |
44 | |
b8440792 |
45 | open(BIN, "./perl") || open(BIN, "./perl.exe") |
46 | || die "Can't open ../perl or ../perl.exe: $!\n"; |
79072805 |
47 | sysread BIN, $foo, 8192; |
48 | close BIN; |
49 | |
50 | $sum = unpack("%32b*", $foo); |
51 | $longway = unpack("b*", $foo); |
52 | print $sum == $longway =~ tr/1/1/ ? "ok 8\n" : "not ok 8\n"; |
73a1c01a |
53 | |
54 | print +($x = unpack("I",pack("I", 0xFFFFFFFF))) == 0xFFFFFFFF |
55 | ? "ok 9\n" : "not ok 9 $x\n"; |
def98dd4 |
56 | |
57 | # check 'w' |
58 | my $test=10; |
55497cff |
59 | my @x = (5,130,256,560,32000,3097152,268435455,1073741844, |
60 | '4503599627365785','23728385234614992549757750638446'); |
def98dd4 |
61 | my $x = pack('w*', @x); |
55497cff |
62 | my $y = pack 'H*', '0581028200843081fa0081bd8440ffffff7f848080801487ffffffffffdb19caefe8e1eeeea0c2e1e3e8ede1ee6e'; |
def98dd4 |
63 | |
64 | print $x eq $y ? "ok $test\n" : "not ok $test\n"; $test++; |
65 | |
66 | @y = unpack('w*', $y); |
55497cff |
67 | my $a; |
68 | while ($a = pop @x) { |
69 | my $b = pop @y; |
70 | print $a eq $b ? "ok $test\n" : "not ok $test\n$a\n$b\n"; $test++; |
71 | } |
def98dd4 |
72 | |
73 | @y = unpack('w2', $x); |
74 | |
75 | print scalar(@y) == 2 ? "ok $test\n" : "not ok $test\n"; $test++; |
76 | print $y[1] == 130 ? "ok $test\n" : "not ok $test\n"; $test++; |
77 | |
55497cff |
78 | # test exeptions |
def98dd4 |
79 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff}; |
80 | print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++; |
81 | |
82 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff}; |
83 | print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++; |
84 | |
85 | eval { $x = unpack 'w', pack 'C*', 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}; |
86 | print $@ ne '' ? "ok $test\n" : "not ok $test\n"; $test++; |
87 | |
84902520 |
88 | # |
89 | # test the "p" template |
90 | |
91 | # literals |
92 | print((unpack("p",pack("p","foo")) eq "foo" ? "ok " : "not ok "),$test++,"\n"); |
93 | |
94 | # scalars |
95 | print((unpack("p",pack("p",$test)) == $test ? "ok " : "not ok "),$test++,"\n"); |
96 | |
97 | # temps |
98 | sub foo { my $a = "a"; return $a . $a++ . $a++ } |
99 | { |
100 | local $^W = 1; |
101 | my $last = $test; |
102 | local $SIG{__WARN__} = sub { |
103 | print "ok ",$test++,"\n" if $_[0] =~ /temporary val/ |
104 | }; |
105 | my $junk = pack("p", &foo); |
106 | print "not ok ", $test++, "\n" if $last == $test; |
107 | } |
108 | |
109 | # undef should give null pointer |
110 | print((pack("p", undef) =~ /^\0+/ ? "ok " : "not ok "),$test++,"\n"); |
111 | |
20408e3c |
112 | # Check for optimizer bug (e.g. Digital Unix GEM cc with -O4 on DU V4.0B gives |
113 | # 4294967295 instead of -1) |
114 | # see #ifdef __osf__ in pp.c pp_unpack |
115 | # Test 30: |
116 | print( ((unpack("i",pack("i",-1))) == -1 ? "ok " : "not ok "),$test++,"\n"); |
117 | |
d4217c7e |
118 | # 31..36: test the pack lengths of s S i I l L |
119 | print "not " unless length(pack("s", 0)) == 2; |
120 | print "ok ", $test++, "\n"; |
121 | |
122 | print "not " unless length(pack("S", 0)) == 2; |
123 | print "ok ", $test++, "\n"; |
124 | |
125 | print "not " unless length(pack("i", 0)) >= 4; |
126 | print "ok ", $test++, "\n"; |
127 | |
128 | print "not " unless length(pack("I", 0)) >= 4; |
129 | print "ok ", $test++, "\n"; |
130 | |
131 | print "not " unless length(pack("l", 0)) == 4; |
132 | print "ok ", $test++, "\n"; |
133 | |
134 | print "not " unless length(pack("L", 0)) == 4; |
135 | print "ok ", $test++, "\n"; |
136 | |
137 | # 37..40: test the pack lengths of n N v V |
138 | |
139 | print "not " unless length(pack("n", 0)) == 2; |
140 | print "ok ", $test++, "\n"; |
141 | |
142 | print "not " unless length(pack("N", 0)) == 4; |
143 | print "ok ", $test++, "\n"; |
144 | |
145 | print "not " unless length(pack("v", 0)) == 2; |
146 | print "ok ", $test++, "\n"; |
147 | |
148 | print "not " unless length(pack("V", 0)) == 4; |
149 | print "ok ", $test++, "\n"; |
150 | |
151 | # 41..56: test unpack-pack lengths |
152 | |
153 | my @templates = qw(c C i I s S l L n N v V f d); |
154 | |
155 | # quads not supported everywhere: if not, retest floats/doubles |
156 | # to preserve the test count... |
157 | eval { my $q = pack("q",0) }; |
158 | push @templates, $@ !~ /Invalid type in pack/ ? qw(q Q) : qw(f d); |
159 | |
160 | foreach my $t (@templates) { |
161 | my @t = unpack("$t*", pack("$t*", 12, 34)); |
162 | print "not " |
163 | unless @t == 2 and (($t[0] == 12 and $t[1] == 34) or ($t =~ /[nv]/i)); |
164 | print "ok ", $test++, "\n"; |
165 | } |
9d116dd7 |
166 | |
eddc390b |
167 | # 57..60: uuencode/decode |
9d116dd7 |
168 | |
342930fb |
169 | $in = pack 'C*', 0 .. 255; |
ba1ac976 |
170 | |
171 | # just to be anal, we do some random tr/`/ / |
9d116dd7 |
172 | $uu = <<'EOUU'; |
ba1ac976 |
173 | M` $"`P0%!@<("0H+# T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL |
9d116dd7 |
174 | M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9 |
175 | M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6& |
176 | MAXB)BHN,C8Z/D)&2DY25EI>8F9J;G)V>GZ"AHJ.DI::GJ*FJJZRMKJ^PL;*S |
177 | MM+6VM[BYNKN\O;Z_P,'"P\3%QL?(R<K+S,W.S]#1TM/4U=;7V-G:V]S=WM_@ |
ba1ac976 |
178 | ?X>+CY.7FY^CIZNOL[>[O\/'R\_3U]O?X^?K[_/W^_P ` |
9d116dd7 |
179 | EOUU |
180 | |
ba1ac976 |
181 | $_ = $uu; |
182 | tr/ /`/; |
183 | print "not " unless pack('u', $in) eq $_; |
9d116dd7 |
184 | print "ok ", $test++, "\n"; |
185 | |
186 | print "not " unless unpack('u', $uu) eq $in; |
187 | print "ok ", $test++, "\n"; |
188 | |
eddc390b |
189 | $in = "\x1f\x8b\x08\x08\x58\xdc\xc4\x35\x02\x03\x4a\x41\x50\x55\x00\xf3\x2a\x2d\x2e\x51\x48\xcc\xcb\x2f\xc9\x48\x2d\x52\x08\x48\x2d\xca\x51\x28\x2d\x4d\xce\x4f\x49\x2d\xe2\x02\x00\x64\x66\x60\x5c\x1a\x00\x00\x00"; |
190 | $uu = <<'EOUU'; |
191 | M'XL("%C<Q#4"`TI!4%4`\RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>("`&1F |
192 | &8%P:```` |
193 | EOUU |
194 | |
195 | print "not " unless unpack('u', $uu) eq $in; |
196 | print "ok ", $test++, "\n"; |
197 | |
198 | # 60 identical to 59 except that backquotes have been changed to spaces |
199 | |
200 | $uu = <<'EOUU'; |
201 | M'XL("%C<Q#4" TI!4%4 \RHM+E%(S,LOR4@M4@A(+<I1*"U-SD])+>(" &1F |
202 | &8%P: |
203 | EOUU |
204 | |
205 | print "not " unless unpack('u', $uu) eq $in; |
206 | print "ok ", $test++, "\n"; |
207 | |
208 | # Note that first uuencoding known 'text' data and then checking the |
209 | # binary values of the uuencoded version would not be portable between |
210 | # character sets. Uuencoding is meant for encoding binary data, not |
211 | # text data. |
5a929a98 |
212 | |
213 | # test the ascii template types (A, a, Z) |
214 | |
215 | print "not " unless pack('A*', "foo\0bar\0 ") eq "foo\0bar\0 "; |
216 | print "ok ", $test++, "\n"; |
217 | |
218 | print "not " unless pack('A11', "foo\0bar\0 ") eq "foo\0bar\0 "; |
219 | print "ok ", $test++, "\n"; |
220 | |
221 | print "not " unless unpack('A*', "foo\0bar \0") eq "foo\0bar"; |
222 | print "ok ", $test++, "\n"; |
223 | |
224 | print "not " unless unpack('A8', "foo\0bar \0") eq "foo\0bar"; |
225 | print "ok ", $test++, "\n"; |
226 | |
227 | print "not " unless pack('a*', "foo\0bar\0 ") eq "foo\0bar\0 "; |
228 | print "ok ", $test++, "\n"; |
229 | |
230 | print "not " unless pack('a11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0"; |
231 | print "ok ", $test++, "\n"; |
232 | |
233 | print "not " unless unpack('a*', "foo\0bar \0") eq "foo\0bar \0"; |
234 | print "ok ", $test++, "\n"; |
235 | |
236 | print "not " unless unpack('a8', "foo\0bar \0") eq "foo\0bar "; |
237 | print "ok ", $test++, "\n"; |
238 | |
239 | print "not " unless pack('Z*', "foo\0bar\0 ") eq "foo\0bar\0 "; |
240 | print "ok ", $test++, "\n"; |
241 | |
242 | print "not " unless pack('Z11', "foo\0bar\0 ") eq "foo\0bar\0 \0\0"; |
243 | print "ok ", $test++, "\n"; |
244 | |
245 | print "not " unless unpack('Z*', "foo\0bar \0") eq "foo"; |
246 | print "ok ", $test++, "\n"; |
247 | |
248 | print "not " unless unpack('Z8', "foo\0bar \0") eq "foo"; |
249 | print "ok ", $test++, "\n"; |
250 | |