This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
new perldelta
[perl5.git] / t / op / length.t
CommitLineData
9dc04555 1#!./perl
2814f8a2
KW
2use warnings;
3use strict;
9dc04555
JH
4
5BEGIN {
6 chdir 't' if -d 't';
d0644529 7 require './test.pl';
9ab40e4a 8 require './charset_tools.pl';
43ece5b1 9 set_up_inc('../lib');
9dc04555
JH
10}
11
9ab40e4a 12plan (tests => 46 + 3 * $::IS_ASCII);
9dc04555 13
2814f8a2
KW
14is(length(""), 0);
15is(length("abc"), 3);
9dc04555 16$_ = "foobar";
2814f8a2 17is(length(), 6);
9dc04555
JH
18
19# Okay, so that wasn't very challenging. Let's go Unicode.
20
21{
22 my $a = "\x{41}";
2814f8a2 23 is(length($a), 1);
9dc04555
JH
24
25 use bytes;
2814f8a2
KW
26 ok($a eq "\x41");
27 is(length($a), 1);
9dc04555
JH
28}
29
9ab40e4a 30if ($::IS_ASCII) { # Generally UTF-8 invariant on EBCDIC, so skip there
6c8584ec 31 my $a = pack("U", 0xFF);
c4d5f83a 32
2814f8a2 33 is(length($a), 1);
c4d5f83a 34
9dc04555 35 use bytes;
9ab40e4a
KW
36 ok($a eq byte_utf8a_to_utf8n("\xc3\xbf"));
37 is(length($a), 2);
38}
39
40{
41 my $a = pack("U", 0xB6); # Works on both ASCII and EBCDIC
42
43 is(length($a), 1);
44
45 use bytes;
46 ok($a eq byte_utf8a_to_utf8n("\xc2\xb6"));
47 is(length($a), 2);
9dc04555
JH
48}
49
50{
51 my $a = "\x{100}";
c4d5f83a 52
2814f8a2 53 is(length($a), 1);
c4d5f83a 54
9dc04555 55 use bytes;
9ab40e4a
KW
56 ok($a eq byte_utf8a_to_utf8n("\xc4\x80"));
57 is(length($a), 2);
9dc04555
JH
58}
59
60{
9ab40e4a 61 my $a = "\x{100}\x{B6}";
c4d5f83a 62
2814f8a2 63 is(length($a), 2);
c4d5f83a 64
9dc04555 65 use bytes;
9ab40e4a
KW
66 ok($a eq byte_utf8a_to_utf8n("\xc4\x80\xc2\xb6"));
67 is(length($a), 4);
9dc04555
JH
68}
69
70{
9ab40e4a 71 my $a = "\x{b6}\x{100}";
c4d5f83a 72
2814f8a2 73 is(length($a), 2);
c4d5f83a 74
9dc04555 75 use bytes;
9ab40e4a
KW
76 ok($a eq byte_utf8a_to_utf8n("\xc2\xb6\xc4\x80"));
77 is(length($a), 4);
9dc04555 78}
5636d518
DB
79
80# Now for Unicode with magical vtbls
81
82{
83 require Tie::Scalar;
84 my $a;
85 tie $a, 'Tie::StdScalar'; # makes $a magical
86 $a = "\x{263A}";
2814f8a2
KW
87
88 is(length($a), 1);
5636d518
DB
89
90 use bytes;
2814f8a2 91 is(length($a), 3);
5636d518 92}
54f923ef
JH
93
94{
95 # Play around with Unicode strings,
96 # give a little workout to the UTF-8 length cache.
97 my $a = chr(256) x 100;
2814f8a2 98 is(length $a, 100);
54f923ef 99 chop $a;
2814f8a2 100 is(length $a, 99);
54f923ef 101 $a .= $a;
2814f8a2 102 is(length $a, 198);
54f923ef 103 $a = chr(256) x 999;
2814f8a2 104 is(length $a, 999);
54f923ef 105 substr($a, 0, 1) = '';
2814f8a2 106 is(length $a, 998);
54f923ef 107}
d0644529 108
d0644529
NC
109require Tie::Scalar;
110
2814f8a2 111my $u = "ASCII";
d0644529
NC
112
113tie $u, 'Tie::StdScalar', chr 256;
114
115is(length $u, 1, "Length of a UTF-8 scalar returned from tie");
116is(length $u, 1, "Again! Again!");
117
9f621bb0
NC
118$^W = 1;
119
120my $warnings = 0;
121
122$SIG{__WARN__} = sub {
123 $warnings++;
124 warn @_;
125};
126
127is(length(undef), undef, "Length of literal undef");
128
2814f8a2 129undef $u;
9f621bb0
NC
130
131is(length($u), undef, "Length of regular scalar");
132
133$u = "Gotcha!";
134
135tie $u, 'Tie::StdScalar';
136
137is(length($u), undef, "Length of tied scalar (MAGIC)");
138
139is($u, undef);
140
141{
142 package U;
143 use overload '""' => sub {return undef;};
144}
145
146my $uo = bless [], 'U';
147
0f43fd57
FC
148{
149 my $w;
150 local $SIG{__WARN__} = sub { $w = shift };
151 is(length($uo), 0, "Length of overloaded reference");
152 like $w, qr/uninitialized/, 'uninit warning for stringifying as undef';
153}
9f621bb0 154
d88e091f 155my $ul = 3;
2814f8a2 156is(($ul = length(undef)), undef,
d88e091f
BM
157 "Returned length of undef with result in TARG");
158is($ul, undef, "Assigned length of undef with result in TARG");
159
160$ul = 3;
161is(($ul = length($u)), undef,
162 "Returned length of tied undef with result in TARG");
163is($ul, undef, "Assigned length of tied undef with result in TARG");
164
165$ul = 3;
0f43fd57
FC
166{
167 my $w;
168 local $SIG{__WARN__} = sub { $w = shift };
169 is(($ul = length($uo)), 0,
d88e091f 170 "Returned length of overloaded undef with result in TARG");
0f43fd57 171 like $w, qr/uninitialized/, 'uninit warning for stringifying as undef';
2814f8a2 172}
0f43fd57 173is($ul, 0, "Assigned length of overloaded undef with result in TARG");
9f621bb0 174
6ef2ab89
NC
175{
176 my $y = "\x{100}BC";
177 is(index($y, "B"), 1, 'adds an intermediate position to the offset cache');
178 is(length $y, 3,
179 'Check that sv_len_utf8() can take advantage of the offset cache');
180}
9407f9c1
DL
181
182{
183 local $SIG{__WARN__} = sub {
184 pass("'print length undef' warned");
185 };
186 print length undef;
187}
188
c6fb3f6e
FC
189{
190 local $SIG{__WARN__} = sub {
191 pass '[perl #106726] no crash with length @lexical warning'
192 };
193 eval ' sub { length my @forecasts } ';
194}
195
193059ca
FC
196# length could be fooled by UTF8ness of non-magical variables changing with
197# stringification.
198my $ref = [];
199bless $ref, "\x{100}";
200is length $ref, length "$ref", 'length on reference blessed to utf8 class';
201
9407f9c1 202is($warnings, 0, "There were no other warnings");