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