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