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