This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
improve perldiag implicit close text
[perl5.git] / t / op / lc.t
CommitLineData
b0f2b690
JH
1#!./perl
2
838f2281
BF
3# This file is intentionally encoded in latin-1.
4
3a2263fe 5BEGIN {
a817e89d 6 chdir 't' if -d 't';
3ffecb73 7 require './test.pl';
43ece5b1 8 set_up_inc('../lib');
31f05a37 9 require Config; import Config;
2b08d1e2 10 skip_all_without_unicode_tables();
3ffecb73 11 require './charset_tools.pl';
9b0711ee 12 require './loc_tools.pl'; # Contains find_utf8_ctype_locale()
983ffd37 13}
b0f2b690 14
838f2281
BF
15use feature qw( fc );
16
31f05a37 17plan tests => 134 + 4 * 256;
c6502f5c
RGS
18
19is(lc(undef), "", "lc(undef) is ''");
20is(lcfirst(undef), "", "lcfirst(undef) is ''");
21is(uc(undef), "", "uc(undef) is ''");
22is(ucfirst(undef), "", "ucfirst(undef) is ''");
3a2263fe 23
838f2281
BF
24{
25 no feature 'fc';
26 is(CORE::fc(undef), "", "fc(undef) is ''");
27 is(CORE::fc(''), "", "fc('') is ''");
28
29 local $@;
30 eval { fc("eeyup") };
31 like($@, qr/Undefined subroutine &main::fc/, "fc() throws an exception,");
32
33 {
34 use feature 'fc';
35 local $@;
36 eval { fc("eeyup") };
37 ok(!$@, "...but works after requesting the feature");
38 }
39}
40
b0f2b690
JH
41$a = "HELLO.* world";
42$b = "hello.* WORLD";
43
3a2263fe
RGS
44is("\Q$a\E." , "HELLO\\.\\*\\ world.", '\Q\E HELLO.* world');
45is("\u$a" , "HELLO\.\* world", '\u');
46is("\l$a" , "hELLO\.\* world", '\l');
47is("\U$a" , "HELLO\.\* WORLD", '\U');
48is("\L$a" , "hello\.\* world", '\L');
838f2281 49is("\F$a" , "hello\.\* world", '\F');
3a2263fe
RGS
50
51is(quotemeta($a) , "HELLO\\.\\*\\ world", 'quotemeta');
52is(ucfirst($a) , "HELLO\.\* world", 'ucfirst');
53is(lcfirst($a) , "hELLO\.\* world", 'lcfirst');
54is(uc($a) , "HELLO\.\* WORLD", 'uc');
55is(lc($a) , "hello\.\* world", 'lc');
838f2281 56is(fc($a) , "hello\.\* world", 'fc');
3a2263fe
RGS
57
58is("\Q$b\E." , "hello\\.\\*\\ WORLD.", '\Q\E hello.* WORLD');
59is("\u$b" , "Hello\.\* WORLD", '\u');
60is("\l$b" , "hello\.\* WORLD", '\l');
61is("\U$b" , "HELLO\.\* WORLD", '\U');
62is("\L$b" , "hello\.\* world", '\L');
838f2281 63is("\F$b" , "hello\.\* world", '\F');
3a2263fe
RGS
64
65is(quotemeta($b) , "hello\\.\\*\\ WORLD", 'quotemeta');
66is(ucfirst($b) , "Hello\.\* WORLD", 'ucfirst');
67is(lcfirst($b) , "hello\.\* WORLD", 'lcfirst');
68is(uc($b) , "HELLO\.\* WORLD", 'uc');
69is(lc($b) , "hello\.\* world", 'lc');
838f2281 70is(fc($b) , "hello\.\* world", 'fc');
983ffd37
JH
71
72# \x{100} is LATIN CAPITAL LETTER A WITH MACRON; its bijective lowercase is
7e965bc5 73# \x{101}, LATIN SMALL LETTER A WITH MACRON.
838f2281 74# Which is also its foldcase.
b0f2b690 75
2533d950
JH
76$a = "\x{100}\x{101}Aa";
77$b = "\x{101}\x{100}aA";
b0f2b690 78
3a2263fe
RGS
79is("\Q$a\E." , "\x{100}\x{101}Aa.", '\Q\E \x{100}\x{101}Aa');
80is("\u$a" , "\x{100}\x{101}Aa", '\u');
81is("\l$a" , "\x{101}\x{101}Aa", '\l');
82is("\U$a" , "\x{100}\x{100}AA", '\U');
83is("\L$a" , "\x{101}\x{101}aa", '\L');
838f2281 84is("\F$a" , "\x{101}\x{101}aa", '\F');
3a2263fe
RGS
85
86is(quotemeta($a) , "\x{100}\x{101}Aa", 'quotemeta');
87is(ucfirst($a) , "\x{100}\x{101}Aa", 'ucfirst');
88is(lcfirst($a) , "\x{101}\x{101}Aa", 'lcfirst');
89is(uc($a) , "\x{100}\x{100}AA", 'uc');
90is(lc($a) , "\x{101}\x{101}aa", 'lc');
838f2281 91is(fc($a) , "\x{101}\x{101}aa", 'fc');
3a2263fe
RGS
92
93is("\Q$b\E." , "\x{101}\x{100}aA.", '\Q\E \x{101}\x{100}aA');
94is("\u$b" , "\x{100}\x{100}aA", '\u');
95is("\l$b" , "\x{101}\x{100}aA", '\l');
96is("\U$b" , "\x{100}\x{100}AA", '\U');
97is("\L$b" , "\x{101}\x{101}aa", '\L');
838f2281 98is("\F$b" , "\x{101}\x{101}aa", '\F');
3a2263fe
RGS
99
100is(quotemeta($b) , "\x{101}\x{100}aA", 'quotemeta');
101is(ucfirst($b) , "\x{100}\x{100}aA", 'ucfirst');
102is(lcfirst($b) , "\x{101}\x{100}aA", 'lcfirst');
103is(uc($b) , "\x{100}\x{100}AA", 'uc');
104is(lc($b) , "\x{101}\x{101}aa", 'lc');
838f2281 105is(fc($b) , "\x{101}\x{101}aa", 'fc');
983ffd37 106
8451602e 107my $sharp_s = uni_to_native("\x{DF}");
983ffd37
JH
108# \x{DF} is LATIN SMALL LETTER SHARP S, its uppercase is SS or \x{53}\x{53};
109# \x{149} is LATIN SMALL LETTER N PRECEDED BY APOSTROPHE, its uppercase is
110# \x{2BC}\x{E4} or MODIFIER LETTER APOSTROPHE and N.
111
8451602e 112is("\U${sharp_s}aB\x{149}cD", "SSAB\x{2BC}NCD", "multicharacter uppercase");
983ffd37
JH
113
114# The \x{DF} is its own lowercase, ditto for \x{149}.
115# There are no single character -> multiple characters lowercase mappings.
b0f2b690 116
8451602e 117is("\L${sharp_s}aB\x{149}cD", "${sharp_s}ab\x{149}cd",
c811e616 118 "multicharacter lowercase");
b0f2b690 119
838f2281
BF
120# \x{DF} is LATIN SMALL LETTER SHARP S, its foldcase is ss or \x{73}\x{73};
121# \x{149} is LATIN SMALL LETTER N PRECEDED BY APOSTROPHE, its foldcase is
122# \x{2BC}\x{6E} or MODIFIER LETTER APOSTROPHE and n.
123# Note that is this further tested in t/uni/fold.t
124
8451602e 125is("\F${sharp_s}aB\x{149}cD", "ssab\x{2BC}ncd", "multicharacter foldcase");
838f2281
BF
126
127
44bc797b
JH
128# titlecase is used for \u / ucfirst.
129
130# \x{587} is ARMENIAN SMALL LIGATURE ECH YIWN and its titlecase is
131# \x{535}\x{582} ARMENIAN CAPITAL LETTER ECH + ARMENIAN SMALL LETTER YIWN
132# while its lowercase is
133# \x{587} itself
134# and its uppercase is
135# \x{535}\x{552} ARMENIAN CAPITAL LETTER ECH + ARMENIAN CAPITAL LETTER YIWN
838f2281 136# The foldcase is \x{565}\x{582} ARMENIAN SMALL LETTER ECH + ARMENIAN SMALL LETTER YIWN
44bc797b
JH
137
138$a = "\x{587}";
139
3a2263fe
RGS
140is("\L\x{587}" , "\x{587}", "ligature lowercase");
141is("\u\x{587}" , "\x{535}\x{582}", "ligature titlecase");
142is("\U\x{587}" , "\x{535}\x{552}", "ligature uppercase");
838f2281 143is("\F\x{587}" , "\x{565}\x{582}", "ligature foldcase");
44bc797b 144
2e3dedfe 145# mktables had problems where many-to-one case mappings didn't work right.
89ebb4a3 146# The lib/uni/fold.t should give the fourth folding, "casefolding", a good
838f2281 147# workout.
83171573
JH
148# \x{01C4} is LATIN CAPITAL LETTER DZ WITH CARON
149# \x{01C5} is LATIN CAPITAL LETTER D WITH SMALL LETTER Z WITH CARON
150# \x{01C6} is LATIN SMALL LETTER DZ WITH CARON
151# \x{03A3} is GREEK CAPITAL LETTER SIGMA
152# \x{03C2} is GREEK SMALL LETTER FINAL SIGMA
153# \x{03C3} is GREEK SMALL LETTER SIGMA
2e3dedfe 154
3a2263fe
RGS
155is(lc("\x{1C4}") , "\x{1C6}", "U+01C4 lc is U+01C6");
156is(lc("\x{1C5}") , "\x{1C6}", "U+01C5 lc is U+01C6, too");
2e3dedfe 157
3a2263fe
RGS
158is(ucfirst("\x{3C2}") , "\x{3A3}", "U+03C2 ucfirst is U+03A3");
159is(ucfirst("\x{3C3}") , "\x{3A3}", "U+03C3 ucfirst is U+03A3, too");
2e3dedfe 160
3a2263fe
RGS
161is(uc("\x{1C5}") , "\x{1C4}", "U+01C5 uc is U+01C4");
162is(uc("\x{1C6}") , "\x{1C4}", "U+01C6 uc is U+01C4, too");
2e3dedfe 163
ada6e8a9
AMS
164# #18107: A host of bugs involving [ul]c{,first}. AMS 20021106
165$a = "\x{3c3}foo.bar"; # \x{3c3} == GREEK SMALL LETTER SIGMA.
166$b = "\x{3a3}FOO.BAR"; # \x{3a3} == GREEK CAPITAL LETTER SIGMA.
167
168($c = $b) =~ s/(\w+)/lc($1)/ge;
3a2263fe 169is($c , $a, "Using s///e to change case.");
ada6e8a9 170
d1eb3177 171($c = $a) =~ s/(\p{IsWord}+)/uc($1)/ge;
3a2263fe 172is($c , $b, "Using s///e to change case.");
ada6e8a9 173
838f2281
BF
174($c = $a) =~ s/(\p{IsWord}+)/fc($1)/ge;
175is($c , $a, "Using s///e to foldcase.");
176
d1eb3177 177($c = $b) =~ s/(\p{IsWord}+)/lcfirst($1)/ge;
3a2263fe 178is($c , "\x{3c3}FOO.bAR", "Using s///e to change case.");
ada6e8a9 179
d1eb3177 180($c = $a) =~ s/(\p{IsWord}+)/ucfirst($1)/ge;
3a2263fe
RGS
181is($c , "\x{3a3}foo.Bar", "Using s///e to change case.");
182
183# #18931: perl5.8.0 bug in \U..\E processing
04d26ece 184# Test case from Nicholas Clark.
3a2263fe
RGS
185for my $a (0,1) {
186 $_ = 'abcdefgh';
187 $_ .= chr 256;
188 chop;
189 /(.*)/;
190 is(uc($1), "ABCDEFGH", "[perl #18931]");
191}
192
193{
194 foreach (0, 1) {
195 $a = v10.v257;
196 chop $a;
197 $a =~ s/^(\s*)(\w*)/$1\u$2/;
198 is($a, v10, "[perl #18857]");
3a2263fe
RGS
199 }
200}
6818a357
TS
201
202
203# [perl #38619] Bug in lc and uc (interaction between UTF-8, substr, and lc/uc)
204
205for ("a\x{100}", "xyz\x{100}") {
206 is(substr(uc($_), 0), uc($_), "[perl #38619] uc");
207}
208for ("A\x{100}", "XYZ\x{100}") {
209 is(substr(lc($_), 0), lc($_), "[perl #38619] lc");
210}
211for ("a\x{100}", "ßyz\x{100}") { # ß to Ss (different length)
212 is(substr(ucfirst($_), 0), ucfirst($_), "[perl #38619] ucfirst");
213}
214
838f2281
BF
215#fc() didn't exist back then, but coverage is coverage.
216for ("a\x{100}", "ßyz\x{100}", "xyz\x{100}", "XYZ\x{100}") { # ß to Ss (different length)
217 is(substr(fc($_), 0), fc($_), "[perl #38619] fc");
218}
219
6818a357
TS
220# Related to [perl #38619]
221# the original report concerns PERL_MAGIC_utf8.
222# these cases concern PERL_MAGIC_regex_global.
223
224for (map { $_ } "a\x{100}", "abc\x{100}", "\x{100}") {
225 chop; # get ("a", "abc", "") in utf8
226 my $return = uc($_) =~ /\G(.?)/g;
227 my $result = $return ? $1 : "not";
228 my $expect = (uc($_) =~ /(.?)/g)[0];
229 is($return, 1, "[perl #38619]");
230 is($result, $expect, "[perl #38619]");
231}
232
233for (map { $_ } "A\x{100}", "ABC\x{100}", "\x{100}") {
234 chop; # get ("A", "ABC", "") in utf8
235 my $return = lc($_) =~ /\G(.?)/g;
236 my $result = $return ? $1 : "not";
237 my $expect = (lc($_) =~ /(.?)/g)[0];
238 is($return, 1, "[perl #38619]");
239 is($result, $expect, "[perl #38619]");
240}
241
838f2281
BF
242for (map { $_ } "A\x{100}", "ABC\x{100}", "\x{100}") {
243 chop; # get ("A", "ABC", "") in utf8
244 my $return = fc($_) =~ /\G(.?)/g;
245 my $result = $return ? $1 : "not";
246 my $expect = (fc($_) =~ /(.?)/g)[0];
247 is($return, 1, "[perl #38619]");
248 is($result, $expect, "[perl #38619]");
249}
250
c2955298
NC
251for (1, 4, 9, 16, 25) {
252 is(uc "\x{03B0}" x $_, "\x{3a5}\x{308}\x{301}" x $_,
253 'uc U+03B0 grows threefold');
254
255 is(lc "\x{0130}" x $_, "i\x{307}" x $_, 'lc U+0130 grows');
838f2281
BF
256
257 is(fc "\x{03B0}" x $_, "\x{3C5}\x{308}\x{301}" x $_,
258 'fc U+03B0 grows threefold');
c2955298 259}
17fa0776
RGS
260
261# bug #43207
4b342708 262my $temp = "HellO";
17fa0776
RGS
263for ("$temp") {
264 lc $_;
4b342708 265 is($_, "HellO", '[perl #43207] lc($_) modifying $_');
17fa0776 266}
838f2281
BF
267for ("$temp") {
268 fc $_;
4b342708
FC
269 is($_, "HellO", '[perl #43207] fc($_) modifying $_');
270}
271for ("$temp") {
272 uc $_;
273 is($_, "HellO", '[perl #43207] uc($_) modifying $_');
274}
275for ("$temp") {
276 ucfirst $_;
277 is($_, "HellO", '[perl #43207] ucfirst($_) modifying $_');
278}
279for ("$temp") {
280 lcfirst $_;
281 is($_, "HellO", '[perl #43207] lcfirst($_) modifying $_');
838f2281
BF
282}
283
aa4af542 284# new in Unicode 5.1.0
8451602e 285is(lc("\x{1E9E}"), uni_to_native("\x{df}"), "lc(LATIN CAPITAL LETTER SHARP S)");
b7cc4340
KW
286
287{
288 use feature 'unicode_strings';
289 use bytes;
8451602e
KW
290 is(lc(uni_to_native("\xc0")), uni_to_native("\xc0"), "lc of above-ASCII Latin1 is itself under use bytes");
291 is(lcfirst(uni_to_native("\xc0")), uni_to_native("\xc0"), "lcfirst of above-ASCII Latin1 is itself under use bytes");
292 is(uc(uni_to_native("\xe0")), uni_to_native("\xe0"), "uc of above-ASCII Latin1 is itself under use bytes");
293 is(ucfirst(uni_to_native("\xe0")), uni_to_native("\xe0"), "ucfirst of above-ASCII Latin1 is itself under use bytes");
b7cc4340 294}
a547fd21
FC
295
296# Brought up in ticket #117855: Constant folding applied to uc() should use
297# the right set of hints.
298fresh_perl_like(<<'constantfolding', qr/^(\d+),\1\z/, {},
299 my $function = "uc";
300 my $char = "\xff";
301 {
302 use feature 'unicode_strings';
303 print ord uc($char), ",",
304 ord eval "$function('$char')", "\n";
305 }
306constantfolding
307 'folded uc() in string eval uses the right hints');
5cd5e2d6
FC
308
309# In-place lc/uc should not corrupt string buffers when given a non-utf8-
310# flagged thingy that stringifies to utf8
311$h{k} = bless[], "\x{3b0}\x{3b0}\x{3b0}bcde"; # U+03B0 grows with uc()
312 # using delete marks it as TEMP, so uc-in-place is permitted
313like uc delete $h{k}, qr "^(?:\x{3a5}\x{308}\x{301}){3}BCDE=ARRAY\(.*\)",
314 'uc(TEMP ref) does not produce a corrupt string';
315$h{k} = bless[], "\x{130}bcde"; # U+0130 grows with lc()
316 # using delete marks it as TEMP, so uc-in-place is permitted
317like lc delete $h{k}, qr "^i\x{307}bcde=array\(.*\)",
318 'lc(TEMP ref) does not produce a corrupt string';
31f05a37
KW
319
320
9b0711ee 321my $utf8_locale = find_utf8_ctype_locale();
31f05a37
KW
322
323SKIP: {
324 skip 'Can\'t find a UTF-8 locale', 4*256 unless defined $utf8_locale;
325
326 use feature qw( unicode_strings );
327
328 no locale;
329
330 my @unicode_lc;
331 my @unicode_uc;
332 my @unicode_lcfirst;
333 my @unicode_ucfirst;
334
335 # Get all the values outside of 'locale'
336 for my $i (0 .. 255) {
337 push @unicode_lc, lc(chr $i);
338 push @unicode_uc, uc(chr $i);
339 push @unicode_lcfirst, lcfirst(chr $i);
340 push @unicode_ucfirst, ucfirst(chr $i);
341 }
342
31f05a37
KW
343 use locale;
344 setlocale(LC_CTYPE, $utf8_locale);
345
346 for my $i (0 .. 255) {
347 is(lc(chr $i), $unicode_lc[$i], "In a UTF-8 locale, lc(chr $i) is the same as official Unicode");
348 is(uc(chr $i), $unicode_uc[$i], "In a UTF-8 locale, uc(chr $i) is the same as official Unicode");
349 is(lcfirst(chr $i), $unicode_lcfirst[$i], "In a UTF-8 locale, lcfirst(chr $i) is the same as official Unicode");
350 is(ucfirst(chr $i), $unicode_ucfirst[$i], "In a UTF-8 locale, ucfirst(chr $i) is the same as official Unicode");
351 }
352}