This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Restore a portion of reverted commits
[perl5.git] / t / op / ver.t
CommitLineData
3cb0bbe5
GS
1#!./perl
2
3BEGIN {
4 chdir 't' if -d 't';
e83d50c9 5 $SIG{'__WARN__'} = sub { warn $_[0] if $DOWARN };
624c42e2
N
6 require "./test.pl";
7 set_up_inc( qw(. ../lib) );
8 require "./charset_tools.pl";
3cb0bbe5
GS
9}
10
e83d50c9 11$DOWARN = 1; # enable run-time warnings now
3cb0bbe5 12
e83d50c9 13use Config;
3cb0bbe5 14
1ddd0324 15plan( tests => 52 );
fd36d4b8 16
84251760 17eval 'use v5.5.640';
e83d50c9 18is( $@, '', "use v5.5.640; $@");
fd36d4b8 19
e83d50c9 20require_ok('v5.5.640');
3cb0bbe5 21
e526c9e6 22# printing characters should work
83bcbc61 23if ($::IS_ASCII) { # ASCII
e83d50c9 24 is('ok ',v111.107.32,'ASCII printing characters');
7bd40e26
PP
25
26 # hash keys too
27 $h{v111.107} = "ok";
e83d50c9 28 is('ok',$h{v111.107},'ASCII hash keys');
7bd40e26
PP
29}
30else { # EBCDIC
e83d50c9 31 is('ok ',v150.146.64,'EBCDIC printing characters');
7bd40e26
PP
32
33 # hash keys too
34 $h{v150.146} = "ok";
9034b73c 35 is('ok',$h{v150.146},'EBCDIC hash keys');
7bd40e26 36}
e526c9e6
GS
37
38# poetry optimization should also
39sub v77 { "ok" }
40$x = v77;
e83d50c9 41is('ok',$x,'poetry optimization');
e526c9e6
GS
42
43# but not when dots are involved
83bcbc61 44if ($::IS_ASCII) { # ASCII
7bd40e26
PP
45 $x = v77.78.79;
46}
47else {
48 $x = v212.213.214;
49}
e83d50c9 50is($x, 'MNO','poetry optimization with dots');
e526c9e6 51
e83d50c9 52is(v1.20.300.4000, "\x{1}\x{14}\x{12c}\x{fa0}",'compare embedded \x{} string');
3cb0bbe5 53
dd629d5b
GS
54#
55# now do the same without the "v"
84251760 56eval 'use 5.5.640';
e83d50c9
JP
57is( $@, '', "use 5.5.640; $@");
58
59require_ok('5.5.640');
dd629d5b
GS
60
61# hash keys too
83bcbc61 62if ($::IS_ASCII) { # ASCII
7bd40e26
PP
63 $h{111.107.32} = "ok";
64}
65else {
66 $h{150.146.64} = "ok";
67}
e83d50c9 68is('ok',$h{ok },'hash keys w/o v');
dd629d5b 69
83bcbc61 70if ($::IS_ASCII) { # ASCII
7bd40e26
PP
71 $x = 77.78.79;
72}
73else {
74 $x = 212.213.214;
75}
e83d50c9 76is($x, 'MNO','poetry optimization with dots w/o v');
dd629d5b 77
e83d50c9 78is(1.20.300.4000, "\x{1}\x{14}\x{12c}\x{fa0}",'compare embedded \x{} string w/o v');
dd629d5b
GS
79
80# test sprintf("%vd"...) etc
83bcbc61 81if ($::IS_ASCII) { # ASCII
e83d50c9 82 is(sprintf("%vd", "Perl"), '80.101.114.108', 'ASCII sprintf("%vd", "Perl")');
7bd40e26
PP
83}
84else {
e83d50c9 85 is(sprintf("%vd", "Perl"), '215.133.153.147', 'EBCDIC sprintf("%vd", "Perl")');
7bd40e26 86}
3cb0bbe5 87
e83d50c9 88is(sprintf("%vd", v1.22.333.4444), '1.22.333.4444', 'sprintf("%vd", v1.22.333.4444)');
b22c7a20 89
83bcbc61 90if ($::IS_ASCII) { # ASCII
e83d50c9 91 is(sprintf("%vx", "Perl"), '50.65.72.6c', 'ASCII sprintf("%vx", "Perl")');
7bd40e26
PP
92}
93else {
e83d50c9 94 is(sprintf("%vx", "Perl"), 'd7.85.99.93', 'EBCDIC sprintf("%vx", "Perl")');
7bd40e26 95}
b22c7a20 96
e83d50c9 97is(sprintf("%vX", 1.22.333.4444), '1.16.14D.115C','ASCII sprintf("%vX", 1.22.333.4444)');
b22c7a20 98
83bcbc61 99if ($::IS_ASCII) { # ASCII
e83d50c9 100 is(sprintf("%#*vo", ":", "Perl"), '0120:0145:0162:0154', 'ASCII sprintf("%vo", "Perl")');
7bd40e26
PP
101}
102else {
e83d50c9 103 is(sprintf("%#*vo", ":", "Perl"), '0327:0205:0231:0223', 'EBCDIC sprintf("%vo", "Perl")');
7bd40e26 104}
b22c7a20 105
e83d50c9
JP
106is(sprintf("%*vb", "##", v1.22.333.4444),
107 '1##10110##101001101##1000101011100', 'sprintf("%vb", 1.22.333.4444)');
3cb0bbe5 108
e83d50c9 109is(sprintf("%vd", join("", map { chr }
fd36d4b8 110 unpack 'U*', pack('U*',2001,2002,2003))),
e83d50c9 111 '2001.2002.2003','unpack/pack U*');
8dbfaa5d 112
3cb0bbe5 113{
8058d7ab 114 use bytes;
fd36d4b8 115
83bcbc61 116 if ($::IS_ASCII) { # ASCII
e83d50c9 117 is(sprintf("%vd", "Perl"), '80.101.114.108', 'ASCII sprintf("%vd", "Perl") w/use bytes');
7bd40e26
PP
118 }
119 else {
e83d50c9 120 is(sprintf("%vd", "Perl"), '215.133.153.147', 'EBCDIC sprintf("%vd", "Perl") w/use bytes');
7bd40e26 121 }
b22c7a20 122
83bcbc61 123 if ($::IS_ASCII) { # ASCII
e83d50c9 124 is(sprintf("%vd", 1.22.333.4444), '1.22.197.141.225.133.156', 'ASCII sprintf("%vd", v1.22.333.4444 w/use bytes');
210db7fc
PP
125 }
126 else {
e83d50c9 127 is(sprintf("%vd", 1.22.333.4444), '1.22.142.84.187.81.112', 'EBCDIC sprintf("%vd", v1.22.333.4444 w/use bytes');
210db7fc 128 }
b22c7a20 129
83bcbc61 130 if ($::IS_ASCII) { # ASCII
e83d50c9 131 is(sprintf("%vx", "Perl"), '50.65.72.6c', 'ASCII sprintf("%vx", "Perl")');
7bd40e26
PP
132 }
133 else {
e83d50c9 134 is(sprintf("%vx", "Perl"), 'd7.85.99.93', 'EBCDIC sprintf("%vx", "Perl")');
7bd40e26 135 }
b22c7a20 136
83bcbc61 137 if ($::IS_ASCII) { # ASCII
e83d50c9 138 is(sprintf("%vX", v1.22.333.4444), '1.16.C5.8D.E1.85.9C', 'ASCII sprintf("%vX", v1.22.333.4444)');
210db7fc
PP
139 }
140 else {
e83d50c9 141 is(sprintf("%vX", v1.22.333.4444), '1.16.8E.54.BB.51.70', 'EBCDIC sprintf("%vX", v1.22.333.4444)');
210db7fc 142 }
b22c7a20 143
83bcbc61 144 if ($::IS_ASCII) { # ASCII
e83d50c9 145 is(sprintf("%#*vo", ":", "Perl"), '0120:0145:0162:0154', 'ASCII sprintf("%#*vo", ":", "Perl")');
7bd40e26
PP
146 }
147 else {
e83d50c9 148 is(sprintf("%#*vo", ":", "Perl"), '0327:0205:0231:0223', 'EBCDIC sprintf("%#*vo", ":", "Perl")');
7bd40e26 149 }
b22c7a20 150
83bcbc61 151 if ($::IS_ASCII) { # ASCII
e83d50c9
JP
152 is(sprintf("%*vb", "##", v1.22.333.4444),
153 '1##10110##11000101##10001101##11100001##10000101##10011100',
154 'ASCII sprintf("%*vb", "##", v1.22.333.4444)');
210db7fc
PP
155 }
156 else {
e83d50c9
JP
157 is(sprintf("%*vb", "##", v1.22.333.4444),
158 '1##10110##10001110##1010100##10111011##1010001##1110000',
159 'EBCDIC sprintf("%*vb", "##", v1.22.333.4444)');
210db7fc 160 }
3cb0bbe5 161}
00387047
JH
162
163{
ee95e30c 164 # bug id 20000323.056 (#2641)
00387047 165
ee95e30c
FC
166 is( "\x{41}", +v65, 'bug id 20000323.056 (#2641)');
167 is( "\x41", +v65, 'bug id 20000323.056 (#2641)');
168 is( "\x{c8}", +v200, 'bug id 20000323.056 (#2641)');
169 is( "\xc8", +v200, 'bug id 20000323.056 (#2641)');
170 is( "\x{221b}", +v8731, 'bug id 20000323.056 (#2641)');
00387047 171}
fd36d4b8
JH
172
173# See if the things Camel-III says are true: 29..33
174
175# Chapter 2 pp67/68
176my $vs = v1.20.300.4000;
e83d50c9
JP
177is($vs,"\x{1}\x{14}\x{12c}\x{fa0}","v-string ne \\x{}");
178is($vs,chr(1).chr(20).chr(300).chr(4000),"v-string ne chr()");
81452d21 179is('foo',($::IS_EBCDIC ? v134.150.150 : v102.111.111),"v-string ne ''");
fd36d4b8
JH
180
181# Chapter 15, pp403
182
183# See if sane addr and gethostbyaddr() work
9eeec659 184eval { require Socket; gethostbyaddr(v127.0.0.1, &Socket::AF_INET) };
7a9ee8c4 185if ($@) {
e83d50c9 186 # No - so do not test insane fails.
7a9ee8c4 187 $@ =~ s/\n/\n# /g;
7a9ee8c4 188}
58d344df 189SKIP: {
190 skip("No Socket::AF_INET # $@") if $@;
7a9ee8c4
JP
191 my $ip = v2004.148.0.1;
192 my $host;
9eeec659 193 eval { $host = gethostbyaddr($ip,&Socket::AF_INET) };
1d662fb6 194 like($@, qr/Wide character/, "Non-bytes leak to gethostbyaddr");
7a9ee8c4 195}
fd36d4b8
JH
196
197# Chapter 28, pp671
e83d50c9
JP
198ok(v5.6.0 lt v5.7.0, "v5.6.0 lt v5.7.0");
199
ee95e30c 200# part of 20000323.059 (#2644)
e83d50c9
JP
201is(v200, chr(200), "v200 eq chr(200)" );
202is(v200, +v200, "v200 eq +v200" );
203is(v200, eval( "v200"), 'v200 eq "v200"' );
204is(v200, eval("+v200"), 'v200 eq eval("+v200")' );
205
206# Tests for string/numeric value of $] itself
8c5f6936 207my ($revision,$version,$subversion) = split /\./, sprintf("%vd",$^V);
e83d50c9 208
9137345a
JP
209# $^V always displays the leading 'v' but we don't want that here
210$revision =~ s/^v//;
211
4282de36
JH
212print "# revision = '$revision'\n";
213print "# version = '$version'\n";
214print "# subversion = '$subversion'\n";
215
e83d50c9
JP
216my $v = sprintf("%d.%.3d%.3d",$revision,$version,$subversion);
217
4282de36
JH
218print "# v = '$v'\n";
219print "# ] = '$]'\n";
220
8c5f6936 221is( $v, "$]", qq{\$^V eq "\$]"});
e83d50c9
JP
222
223$v = $revision + $version/1000 + $subversion/1000000;
224
d7aa5382 225ok( abs($v - $]) < 10**-8 , "\$^V == \$] (numeric)" );
e83d50c9 226
439cb1c4
JP
227# Tests for magic v-strings
228
229$v = 1.2.3;
230is( ref(\$v), 'VSTRING', 'v-string objects' );
231
232$v = v1.2_3;
233is( ref(\$v), 'VSTRING', 'v-string objects with v' );
234is( sprintf("%vd", $v), '1.23', 'v-string ignores underscores' );
3cd5fe90
JH
235
236# [perl #16010]
237%h = (v65 => 42);
238ok( exists $h{v65}, "v-stringness is not engaged for vX" );
239%h = (v65.66 => 42);
240ok( exists $h{chr(65).chr(66)}, "v-stringness is engaged for vX.Y" );
241%h = (65.66.67 => 42);
242ok( exists $h{chr(65).chr(66).chr(67)}, "v-stringness is engaged for X.Y.Z" );
243
190e89f0
FC
244{
245 local $|;
246 $| = v0;
247 $| = 1;
248 --$|; --$|;
249 is $|, 1, 'clobbering vstrings does not clobber all magic';
250}
251
81452d21 252$a = $::IS_EBCDIC ? v134 : v102; $a =~ s/f/f/;
4499db73
FC
253is ref \$a, 'SCALAR',
254 's/// flattens vstrings even when the subst results in the same value';
81452d21 255$a = $::IS_EBCDIC ? v134 : v102; $a =~ y/f/g/;
4499db73 256is ref \$a, 'SCALAR', 'y/// flattens vstrings';
3cd5fe90 257
ff44333e
FC
258sub { $_[0] = v3;
259 is ref \$h{nonexistent}, 'VSTRING', 'defelems can pass vstrings' }
260->($h{nonexistent});
261
6c336d06
RH
262# The following tests whether v-strings are correctly
263# interpreted by the tokeniser when it's in a XTERMORDORDOR
264# state (fittingly, the only tokeniser state to contain the
265# word MORDOR).
266
267*{"\3"} = *DATA;
268is( (readline v3), "This is what we expect to see!\n", "v-strings even work in Mordor" );
269
270__DATA__
271This is what we expect to see!