11 print "not " unless length("") == 0;
14 print "not " unless length("abc") == 3;
18 print "not " unless length() == 6;
21 # Okay, so that wasn't very challenging. Let's go Unicode.
26 print "not " unless length($a) == 1;
31 print "not " unless $a eq "\x41" && length($a) == 1;
37 my $a = pack("U", 0xFF);
39 print "not " unless length($a) == 1;
46 printf "#%vx for 0xFF\n",$a;
47 print "not " unless $a eq "\x8b\x73" && length($a) == 2;
51 print "not " unless $a eq "\xc3\xbf" && length($a) == 2;
60 print "not " unless length($a) == 1;
67 printf "#%vx for 0x100\n",$a;
68 print "not " unless $a eq "\x8c\x41" && length($a) == 2;
72 print "not " unless $a eq "\xc4\x80" && length($a) == 2;
79 my $a = "\x{100}\x{80}";
81 print "not " unless length($a) == 2;
88 printf "#%vx for 0x100 0x80\n",$a;
89 print "not " unless $a eq "\x8c\x41\x8a\x67" && length($a) == 4;
93 print "not " unless $a eq "\xc4\x80\xc2\x80" && length($a) == 4;
100 my $a = "\x{80}\x{100}";
102 print "not " unless length($a) == 2;
109 printf "#%vx for 0x80 0x100\n",$a;
110 print "not " unless $a eq "\x8a\x67\x8c\x41" && length($a) == 4;
114 print "not " unless $a eq "\xc2\x80\xc4\x80" && length($a) == 4;
120 # Now for Unicode with magical vtbls
125 tie $a, 'Tie::StdScalar'; # makes $a magical
128 print "not " unless length($a) == 1;
133 print "not " unless length($a) == 3;
139 # Play around with Unicode strings,
140 # give a little workout to the UTF-8 length cache.
141 my $a = chr(256) x 100;
142 print length $a == 100 ? "ok 16\n" : "not ok 16\n";
144 print length $a == 99 ? "ok 17\n" : "not ok 17\n";
146 print length $a == 198 ? "ok 18\n" : "not ok 18\n";
148 print length $a == 999 ? "ok 19\n" : "not ok 19\n";
149 substr($a, 0, 1) = '';
150 print length $a == 998 ? "ok 20\n" : "not ok 20\n";
159 tie $u, 'Tie::StdScalar', chr 256;
161 is(length $u, 1, "Length of a UTF-8 scalar returned from tie");
162 is(length $u, 1, "Again! Again!");
168 $SIG{__WARN__} = sub {
173 is(length(undef), undef, "Length of literal undef");
177 is(length($u), undef, "Length of regular scalar");
181 tie $u, 'Tie::StdScalar';
183 is(length($u), undef, "Length of tied scalar (MAGIC)");
189 use overload '""' => sub {return undef;};
192 my $uo = bless [], 'U';
196 local $SIG{__WARN__} = sub { $w = shift };
197 is(length($uo), 0, "Length of overloaded reference");
198 like $w, qr/uninitialized/, 'uninit warning for stringifying as undef';
202 is(($ul = length(undef)), undef,
203 "Returned length of undef with result in TARG");
204 is($ul, undef, "Assigned length of undef with result in TARG");
207 is(($ul = length($u)), undef,
208 "Returned length of tied undef with result in TARG");
209 is($ul, undef, "Assigned length of tied undef with result in TARG");
214 local $SIG{__WARN__} = sub { $w = shift };
215 is(($ul = length($uo)), 0,
216 "Returned length of overloaded undef with result in TARG");
217 like $w, qr/uninitialized/, 'uninit warning for stringifying as undef';
219 is($ul, 0, "Assigned length of overloaded undef with result in TARG");
223 is(index($y, "B"), 1, 'adds an intermediate position to the offset cache');
225 'Check that sv_len_utf8() can take advantage of the offset cache');
229 local $SIG{__WARN__} = sub {
230 pass("'print length undef' warned");
236 local $SIG{__WARN__} = sub {
237 pass '[perl #106726] no crash with length @lexical warning'
239 eval ' sub { length my @forecasts } ';
242 # length could be fooled by UTF8ness of non-magical variables changing with
245 bless $ref, "\x{100}";
246 is length $ref, length "$ref", 'length on reference blessed to utf8 class';
248 is($warnings, 0, "There were no other warnings");