xref: /openbsd/gnu/usr.bin/perl/t/uni/fold.t (revision 91f110e0)
1use strict;
2use warnings;
3
4# re/fold_grind.t has more complex tests, but doesn't test every fold
5# This file also tests the fc() keyword.
6
7BEGIN {
8    chdir 't' if -d 't';
9    @INC = '../lib';
10    require './test.pl';
11}
12
13use feature 'unicode_strings';
14
15binmode *STDOUT, ":utf8";
16
17our $TODO;
18
19plan("no_plan");
20# Read in the official case folding definitions.
21my $CF = '../lib/unicore/CaseFolding.txt';
22
23die qq[$0: failed to open "$CF": $!\n] if ! open(my $fh, "<", $CF);
24
25my @CF;
26my @simple_folds;
27my %reverse_fold;
28while (<$fh>) {
29    # We only use 'S' in simple folded fc(), since the regex engine uses
30    # 'F'ull case folding.  I is obsolete starting with Unicode 3.2, but
31    # leaving it in does no harm, and allows backward compatibility
32    next unless my ($code, $type, $mapping, $name) = $_ =~
33            /^([0-9A-F]+); ([CFIS]); ((?:[0-9A-F]+)(?: [0-9A-F]+)*); \# (.+)/;
34
35    # Convert any 0-255 range chars to native.
36    $code = sprintf("%04X", ord_latin1_to_native(hex $code)) if hex $code < 0x100;
37    $mapping = join " ", map { $_ =
38                                sprintf("%04X", ord_latin1_to_native(hex $_)) }
39                                                            split / /, $mapping;
40
41    if ( $type eq "S" ) {
42        push @simple_folds, [$code, $mapping, $type, $name];
43        next;
44    }
45
46    push @CF, [$code, $mapping, $type, $name];
47
48    # Get the inverse fold for single-char mappings.
49    $reverse_fold{pack "U0U*", hex $mapping} = pack "U0U*", hex $code if $type ne 'F';
50}
51
52close($fh) or die "$0 Couldn't close $CF";
53
54foreach my $test_ref ( @simple_folds ) {
55    use feature 'fc';
56    my ($code, $mapping, $type, $name) = @$test_ref;
57    my $c = pack("U0U*", hex $code);
58    my $f = pack("U0U*", map { hex } split " ", $mapping);
59
60    my $against = join "", "qq{", map("\\x{$_}", split " ", $mapping), "}";
61    {
62        isnt(fc($c), $f, "$code - $name - $mapping - $type - Full casefolding, fc(\\x{$code}) ne $against");
63        isnt("\F$c", $f, "$code - $name - $mapping - $type - Full casefolding, qq{\\F\\x{$code}} ne $against");
64    }
65}
66
67foreach my $test_ref (@CF) {
68    my ($code, $mapping, $type, $name) = @$test_ref;
69    my $c = pack("U0U*", hex $code);
70    my $f = pack("U0U*", map { hex } split " ", $mapping);
71    my $f_length = length $f;
72    foreach my $test (
73            qq[":$c:" =~ /:$c:/],
74            qq[":$c:" =~ /:$c:/i],
75            qq[":$c:" =~ /:[_$c]:/], # Place two chars in [] so doesn't get
76                                     # optimized to a non-charclass
77            qq[":$c:" =~ /:[_$c]:/i],
78            qq[":$c:" =~ /:$f:/i],
79            qq[":$f:" =~ /:$c:/i],
80    ) {
81        ok eval $test, "$code - $name - $mapping - $type - $test";
82    }
83
84    {
85        # fc() tests
86        my $against = join "", "qq{", map("\\x{$_}", split " ", $mapping), "}";
87        is(CORE::fc($c), $f,
88            "$code - $name - $mapping - $type - fc(\\x{$code}) eq $against");
89        is("\F$c", $f, "$code - $name - $mapping - $type - qq{\\F\\x{$code}} eq $against");
90
91        # And here we test bytes. For [A-Za-z0-9], the fold is the same as lc under
92        # bytes. For everything else, it's the bytes that formed the original string.
93        if ( $c =~ /[A-Za-z0-9]/ ) {
94            use bytes;
95            is(CORE::fc($c), lc($c), "$code - $name - fc and use bytes, ascii");
96        } else {
97            my $copy = "" . $c;
98            utf8::encode($copy);
99            is($copy, do { use bytes; CORE::fc($c) }, "$code - $name - fc and use bytes");
100        }
101    }
102    # Certain tests weren't convenient to put in the list above since they are
103    # TODO's in multi-character folds.
104    if ($f_length == 1) {
105
106        # The qq loses the utf8ness of ":$f:".  These tests are not about
107        # finding bugs in utf8ness, so make sure it's utf8.
108        my $test = qq[my \$s = ":$f:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
109        ok eval $test, "$code - $name - $mapping - $type - $test";
110        $test = qq[":$c:" =~ /:[_$f]:/i];
111        ok eval $test, "$code - $name - $mapping - $type - $test";
112    }
113    else {
114
115        # There are two classes of multi-char folds that need more work.  For
116        # example,
117        #   ":ß:" =~ /:[_s]{2}:/i
118        #   ":ss:" =~ /:[_ß]:/i
119        #
120        # Some of the old tests for the second case happened to pass somewhat
121        # coincidentally.  But none would pass if changed to this.
122        #   ":SS:" =~ /:[_ß]:/i
123        #
124        # As the capital SS doesn't get folded.  When those pass, it means
125        # that the code has been changed to take into account folding in the
126        # string, and all should pass, capitalized or not (this wouldn't be
127        # true for [^complemented character classes], for which the fold case
128        # is better, but these aren't used in this .t currently.  So, what is
129        # done is to essentially upper-case the string for this class (but use
130        # the reverse fold not uc(), as that is more correct)
131        my $u;
132        for my $i (0 .. $f_length - 1) {
133            my $cur_char = substr($f, $i, 1);
134            $u .= $reverse_fold{$cur_char} || $cur_char;
135        }
136        my $test;
137
138        # A multi-char fold should not match just one char;
139        # e.g., ":ß:" !~ /:[_s]:/i
140        $test = qq[":$c:" !~ /:[_$f]:/i];
141        ok eval $test, "$code - $name - $mapping - $type - $test";
142
143        TODO: { # e.g., ":ß:" =~ /:[_s]{2}:/i
144            local $TODO = 'Multi-char fold in [character class]';
145
146            $test = qq[":$c:" =~ /:[_$f]{$f_length}:/i];
147            ok eval $test, "$code - $name - $mapping - $type - $test";
148        }
149
150        # e.g., ":SS:" =~ /:[_ß]:/i now pass, so TODO has been removed, but
151        # since they use '$u', they are left out of the main loop
152        $test = qq[ my \$s = ":$u:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
153        ok eval $test, "$code - $name - $mapping - $type - $test";
154    }
155}
156
157{
158    use utf8;
159    use feature qw(fc);
160    # These three come from the ICU project's test suite, more especifically
161    # http://icu.sourcearchive.com/documentation/4.4~rc1-1/strcase_8cpp-source.html
162
163    my $s = "A\N{U+00df}\N{U+00b5}\N{U+fb03}\N{U+1040C}\N{U+0130}\N{U+0131}";
164    #\N{LATIN CAPITAL LETTER A}\N{LATIN SMALL LETTER SHARP S}\N{MICRO SIGN}\N{LATIN SMALL LIGATURE FFI}\N{DESERET CAPITAL LETTER AY}\N{LATIN CAPITAL LETTER I WITH DOT ABOVE}\N{LATIN SMALL LETTER DOTLESS I}
165
166    my $f = "ass\N{U+03bc}ffi\N{U+10434}i\N{U+0307}\N{U+0131}";
167    #\N{LATIN SMALL LETTER A}\N{LATIN SMALL LETTER S}\N{LATIN SMALL LETTER S}\N{GREEK SMALL LETTER MU}\N{LATIN SMALL LETTER F}\N{LATIN SMALL LETTER F}\N{LATIN SMALL LETTER I}\N{DESERET SMALL LETTER AY}\N{LATIN SMALL LETTER I}\N{COMBINING DOT ABOVE}\N{LATIN SMALL LETTER DOTLESS I}
168
169    is(fc($s), $f, "ICU's casefold test passes");
170    is("\F$s", $f, "ICU's casefold test passes");
171
172    is( fc("aBİIıϐßffi��"), "abi̇iıβssffi��" );
173    is( "\FaBİIıϐßffi��", "abi̇iıβssffi��" );
174#    TODO: {
175#        local $::TODO = "turkic special cases";
176#        is( fc "aBİIıϐßffi��", "abiııβssffi��" );
177#    }
178
179    # The next batch come from http://www.devdaily.com/java/jwarehouse/lucene/contrib/icu/src/test/org/apache/lucene/analysis/icu/TestICUFoldingFilter.java.shtml
180    # Except the article got most casings wrong. Or maybe Lucene does.
181
182    is( fc("This is a test"), "this is a test" );
183    is( fc("Ruß"), "russ"    );
184    is( fc("ΜΆΪΟΣ"), "μάϊοσ" );
185    is( fc("Μάϊος"), "μάϊοσ" );
186    is( fc("��"), "��"       );
187    is( fc("r\xe9sum\xe9"), "r\xe9sum\xe9" );
188    is( fc("re\x{0301}sume\x{0301}"), "re\x{301}sume\x{301}" );
189    is( fc("ELİF"), "eli\x{307}f" );
190    is( fc("eli\x{307}f"), "eli\x{307}f");
191
192    # This batch comes from
193    # http://www.java2s.com/Open-Source/Java-Document/Internationalization-Localization/icu4j/com/ibm/icu/dev/test/lang/UCharacterCaseTest.java.htm
194    # Which uses ICU as the backend.
195
196    my @folding_mixed = (
197        "\x{61}\x{42}\x{130}\x{49}\x{131}\x{3d0}\x{df}\x{fb03}",
198        "A\x{df}\x{b5}\x{fb03}\x{1040C}\x{130}\x{131}",
199    );
200
201    my @folding_default = (
202        "\x{61}\x{62}\x{69}\x{307}\x{69}\x{131}\x{3b2}\x{73}\x{73}\x{66}\x{66}\x{69}",
203        "ass\x{3bc}ffi\x{10434}i\x{307}\x{131}",
204    );
205
206    my @folding_exclude_turkic = (
207        "\x{61}\x{62}\x{69}\x{131}\x{131}\x{3b2}\x{73}\x{73}\x{66}\x{66}\x{69}",
208        "ass\x{3bc}ffi\x{10434}i\x{131}",
209    );
210
211    is( fc($folding_mixed[1]), $folding_default[1] );
212
213    is( fc($folding_mixed[0]), $folding_default[0] );
214
215}
216
217{
218    use utf8;
219    # Table stolen from tchrist's mail in
220    # http://bugs.python.org/file23051/casing-tests.py
221    # and http://98.245.80.27/tcpc/OSCON2011/case-test.python3
222    # For reference, it's a longer version of what he posted here:
223    # http://stackoverflow.com/questions/6991038/case-insensitive-storage-and-unicode-compatibility
224
225    #Couple of repeats because I'm lazy, not tchrist's fault.
226
227    #This should probably go in t/op/lc.t
228
229    my @test_table = (
230# ORIG LC_SIMPLE TC_SIMPLE UC_SIMPLE LC_FULL TC_FULL UC_FULL FC_SIMPLE FC_TURKIC FC_FULL
231[ 'þǽr rihtes', 'þǽr rihtes', 'Þǽr Rihtes', 'ÞǼR RIHTES', 'þǽr rihtes', 'Þǽr Rihtes', 'ÞǼR RIHTES', 'þǽr rihtes', 'þǽr rihtes', 'þǽr rihtes',  ],
232[ 'duȝeðlice', 'duȝeðlice', 'Duȝeðlice', 'DUȜEÐLICE', 'duȝeðlice', 'Duȝeðlice', 'DUȜEÐLICE', 'duȝeðlice', 'duȝeðlice', 'duȝeðlice',  ],
233[ 'Ævar Arnfjörð Bjarmason', 'ævar arnfjörð bjarmason', 'Ævar Arnfjörð Bjarmason', 'ÆVAR ARNFJÖRÐ BJARMASON', 'ævar arnfjörð bjarmason', 'Ævar Arnfjörð Bjarmason', 'ÆVAR ARNFJÖRÐ BJARMASON', 'ævar arnfjörð bjarmason', 'ævar arnfjörð bjarmason', 'ævar arnfjörð bjarmason',  ],
234[ 'Кириллица', 'кириллица', 'Кириллица', 'КИРИЛЛИЦА', 'кириллица', 'Кириллица', 'КИРИЛЛИЦА', 'кириллица', 'кириллица', 'кириллица',  ],
235[ 'ij', 'ij', 'IJ', 'IJ', 'ij', 'IJ', 'IJ', 'ij', 'ij', 'ij',  ],
236[ 'Van Dijke', 'van dijke', 'Van Dijke', 'VAN DIJKE', 'van dijke', 'Van Dijke', 'VAN DIJKE', 'van dijke', 'van dijke', 'van dijke',  ],
237[ 'VAN DIJKE', 'van dijke', 'Van Dijke', 'VAN DIJKE', 'van dijke', 'Van Dijke', 'VAN DIJKE', 'van dijke', 'van dijke', 'van dijke',  ],
238[ 'efficient', 'efficient', 'Efficient', 'EffiCIENT', 'efficient', 'Efficient', 'EFFICIENT', 'efficient', 'efficient', 'efficient',  ],
239[ 'flour', 'flour', 'flour', 'flOUR', 'flour', 'Flour', 'FLOUR', 'flour', 'flour', 'flour',  ],
240[ 'flour and water', 'flour and water', 'flour And Water', 'flOUR AND WATER', 'flour and water', 'Flour And Water', 'FLOUR AND WATER', 'flour and water', 'flour and water', 'flour and water',  ],
241[ 'dzur', 'dzur', 'Dzur', 'DZUR', 'dzur', 'Dzur', 'DZUR', 'dzur', 'dzur', 'dzur',  ],
242[ 'Dzur', 'dzur', 'Dzur', 'DZUR', 'dzur', 'Dzur', 'DZUR', 'dzur', 'dzur', 'dzur',  ],
243[ 'DZUR', 'dzur', 'Dzur', 'DZUR', 'dzur', 'Dzur', 'DZUR', 'dzur', 'dzur', 'dzur',  ],
244[ 'dzur mountain', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'dzur mountain', 'dzur mountain',  ],
245[ 'Dzur Mountain', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'dzur mountain', 'dzur mountain',  ],
246[ 'DZUR MOUNTAIN', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'Dzur Mountain', 'DZUR MOUNTAIN', 'dzur mountain', 'dzur mountaın', 'dzur mountain',  ],
247[ 'poſt', 'poſt', 'Poſt', 'POST', 'poſt', 'Poſt', 'POST', 'post', 'post', 'post',  ],
248[ 'poſt', 'poſt', 'Poſt', 'POſt', 'poſt', 'Poſt', 'POST', 'poſt', 'post', 'post',  ],
249[ 'ſtop', 'ſtop', 'ſtop', 'ſtOP', 'ſtop', 'Stop', 'STOP', 'ſtop', 'stop', 'stop',  ],
250[ 'tschüß', 'tschüß', 'Tschüß', 'TSCHÜß', 'tschüß', 'Tschüß', 'TSCHÜSS', 'tschüß', 'tschüss', 'tschüss',  ],
251[ 'TSCHÜẞ', 'tschüß', 'Tschüß', 'TSCHÜẞ', 'tschüß', 'Tschüß', 'TSCHÜẞ', 'tschüß', 'tschüss', 'tschüss',  ],
252[ 'weiß', 'weiß', 'Weiß', 'WEIß', 'weiß', 'Weiß', 'WEISS', 'weiß', 'weiss', 'weiss',  ],
253[ 'WEIẞ', 'weiß', 'Weiß', 'WEIẞ', 'weiß', 'Weiß', 'WEIẞ', 'weiß', 'weıss', 'weiss',  ],
254[ 'ẞIEW', 'ßiew', 'ẞiew', 'ẞIEW', 'ßiew', 'ẞiew', 'ẞIEW', 'ßiew', 'ssıew', 'ssiew',  ],
255[ 'ᾲ', 'ᾲ', 'Ὰͅ', 'ᾺΙ', 'ᾲ', 'Ὰͅ', 'ᾺΙ', 'ὰι', 'ὰι', 'ὰι',  ],
256[ 'Ὰι', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'ὰι', 'ὰι',  ],
257[ 'ᾺΙ', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'ὰι', 'ὰι',  ],
258[ 'ᾲ', 'ᾲ', 'ᾲ', 'ᾲ', 'ᾲ', 'Ὰͅ', 'ᾺΙ', 'ᾲ', 'ὰι', 'ὰι',  ],
259[ 'Ὰͅ', 'ᾲ', 'Ὰͅ', 'ᾺΙ', 'ᾲ', 'Ὰͅ', 'ᾺΙ', 'ὰι', 'ὰι', 'ὰι',  ],
260[ 'ᾺΙ', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'Ὰι', 'ᾺΙ', 'ὰι', 'ὰι', 'ὰι',  ],
261[ 'ᾲ στο διάολο', 'ᾲ στο διάολο', 'ᾲ Στο Διάολο', 'ᾲ ΣΤΟ ΔΙΆΟΛΟ', 'ᾲ στο διάολο', 'Ὰͅ Στο Διάολο', 'ᾺΙ ΣΤΟ ΔΙΆΟΛΟ', 'ᾲ στο διάολο', 'ὰι στο διάολο', 'ὰι στο διάολο',  ],
262[ 'ᾲ στο διάολο', 'ᾲ στο διάολο', 'Ὰͅ Στο Διάολο', 'ᾺΙ ΣΤΟ ΔΙΆΟΛΟ', 'ᾲ στο διάολο', 'Ὰͅ Στο Διάολο', 'ᾺΙ ΣΤΟ ΔΙΆΟΛΟ', 'ὰι στο διάολο', 'ὰι στο διάολο', 'ὰι στο διάολο',  ],
263[ '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������',  ],
264[ '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������',  ],
265[ '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������', '��������������',  ],
266[ 'henry ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'henry ⅷ', 'henry ⅷ',  ],
267[ 'Henry Ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'henry ⅷ', 'henry ⅷ',  ],
268[ 'HENRY Ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'Henry Ⅷ', 'HENRY Ⅷ', 'henry ⅷ', 'henry ⅷ', 'henry ⅷ',  ],
269[ 'i work at ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'i work at ⓚ', 'i work at ⓚ',  ],
270[ 'I Work At Ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'ı work at ⓚ', 'i work at ⓚ',  ],
271[ 'I WORK AT Ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'I Work At Ⓚ', 'I WORK AT Ⓚ', 'i work at ⓚ', 'ı work at ⓚ', 'i work at ⓚ',  ],
272[ 'istambul', 'istambul', 'Istambul', 'ISTAMBUL', 'istambul', 'Istambul', 'ISTAMBUL', 'istambul', 'istambul', 'istambul',  ],
273[ 'i̇stanbul', 'i̇stanbul', 'İstanbul', 'İSTANBUL', 'i̇stanbul', 'İstanbul', 'İSTANBUL', 'i̇stanbul', 'i̇stanbul', 'i̇stanbul',  ],
274[ 'İstanbul', 'i̇stanbul', 'İstanbul', 'İSTANBUL', 'i̇stanbul', 'İstanbul', 'İSTANBUL', 'i̇stanbul', 'ı̇stanbul', 'i̇stanbul',  ],
275[ 'İSTANBUL', 'istanbul', 'İstanbul', 'İSTANBUL', 'i̇stanbul', 'İstanbul', 'İSTANBUL', 'İstanbul', 'istanbul', 'i̇stanbul',  ],
276[ 'στιγμας', 'στιγμας', 'Στιγμας', 'ΣΤΙΓΜΑΣ', 'στιγμας', 'Στιγμας', 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'στιγμασ', 'στιγμασ',  ],
277[ 'στιγμασ', 'στιγμασ', 'Στιγμασ', 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'Στιγμασ', 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'στιγμασ', 'στιγμασ',  ],
278[ 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'Στιγμασ', 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'Στιγμασ', 'ΣΤΙΓΜΑΣ', 'στιγμασ', 'στιγμασ', 'στιγμασ',  ],
279[ 'ʀᴀʀᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ',  ],
280[ 'Ʀᴀʀᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ',  ],
281[ 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'Ʀᴀʀᴇ', 'ƦᴀƦᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ', 'ʀᴀʀᴇ',  ],
282[ 'Ԧԧ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'ԧԧ', 'ԧԧ',  ],
283[ 'ԧԧ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'ԧԧ', 'ԧԧ',  ],
284[ 'Ԧԧ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'ԧԧ', 'ԧԧ',  ],
285[ 'ԦԦ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'Ԧԧ', 'ԦԦ', 'ԧԧ', 'ԧԧ', 'ԧԧ',  ],
286[ "þǽr rihtes", "þǽr rihtes", "Þǽr Rihtes", "ÞǼR RIHTES", "þǽr rihtes", "Þǽr Rihtes", "ÞǼR RIHTES", "þǽr rihtes", "þǽr rihtes", "þǽr rihtes",  ],
287[ "duȝeðlice", "duȝeðlice", "Duȝeðlice", "DUȜEÐLICE", "duȝeðlice", "Duȝeðlice", "DUȜEÐLICE", "duȝeðlice", "duȝeðlice", "duȝeðlice",  ],
288[ "Van Dijke", "van dijke", "Van Dijke", "VAN DIJKE", "van dijke", "Van Dijke", "VAN DIJKE", "van dijke", "van dijke", "van dijke",  ],
289[ "fi", "fi", "fi", "fi", "fi", "Fi", "FI", "fi", "fi", "fi",  ],
290[ "filesystem", "filesystem", "filesystem", "fiLESYSTEM", "filesystem", "Filesystem", "FILESYSTEM", "filesystem", "filesystem", "filesystem",  ],
291[ "efficient", "efficient", "Efficient", "EffiCIENT", "efficient", "Efficient", "EFFICIENT", "efficient", "efficient", "efficient",  ],
292[ "flour and water", "flour and water", "flour And Water", "flOUR AND WATER", "flour and water", "Flour And Water", "FLOUR AND WATER", "flour and water", "flour and water", "flour and water",  ],
293[ "dz", "dz", "Dz", "DZ", "dz", "Dz", "DZ", "dz", "dz", "dz",  ],
294[ "dzur mountain", "dzur mountain", "Dzur Mountain", "DZUR MOUNTAIN", "dzur mountain", "Dzur Mountain", "DZUR MOUNTAIN", "dzur mountain", "dzur mountain", "dzur mountain",  ],
295[ "poſt", "poſt", "Poſt", "POST", "poſt", "Poſt", "POST", "post", "post", "post",  ],
296[ "poſt", "poſt", "Poſt", "POſt", "poſt", "Poſt", "POST", "poſt", "post", "post",  ],
297[ "ſtop", "ſtop", "ſtop", "ſtOP", "ſtop", "Stop", "STOP", "ſtop", "stop", "stop",  ],
298[ "tschüß", "tschüß", "Tschüß", "TSCHÜß", "tschüß", "Tschüß", "TSCHÜSS", "tschüß", "tschüss", "tschüss",  ],
299[ "TSCHÜẞ", "tschüß", "Tschüß", "TSCHÜẞ", "tschüß", "Tschüß", "TSCHÜẞ", "tschüß", "tschüss", "tschüss",  ],
300[ "rußland", "rußland", "Rußland", "RUßLAND", "rußland", "Rußland", "RUSSLAND", "rußland", "russland", "russland",  ],
301[ "RUẞLAND", "rußland", "Rußland", "RUẞLAND", "rußland", "Rußland", "RUẞLAND", "rußland", "russland", "russland",  ],
302[ "weiß", "weiß", "Weiß", "WEIß", "weiß", "Weiß", "WEISS", "weiß", "weiss", "weiss",  ],
303[ "WEIẞ", "weiß", "Weiß", "WEIẞ", "weiß", "Weiß", "WEIẞ", "weiß", "weıss", "weiss",  ],
304[ "ẞIEW", "ßiew", "ẞiew", "ẞIEW", "ßiew", "ẞiew", "ẞIEW", "ßiew", "ssıew", "ssiew",  ],
305[ "ͅ", "ͅ", "Ι", "Ι", "ͅ", "Ι", "Ι", "ι", "ι", "ι",  ],
306[ "ᾲ", "ᾲ", "Ὰͅ", "ᾺΙ", "ᾲ", "Ὰͅ", "ᾺΙ", "ὰι", "ὰι", "ὰι",  ],
307[ "Ὰι", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "ὰι", "ὰι",  ],
308[ "ᾺΙ", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "ὰι", "ὰι",  ],
309[ "ᾲ", "ᾲ", "ᾲ", "ᾲ", "ᾲ", "Ὰͅ", "ᾺΙ", "ᾲ", "ὰι", "ὰι",  ],
310[ "Ὰͅ", "ᾲ", "Ὰͅ", "ᾺΙ", "ᾲ", "Ὰͅ", "ᾺΙ", "ὰι", "ὰι", "ὰι",  ],
311[ "ᾺΙ", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "Ὰι", "ᾺΙ", "ὰι", "ὰι", "ὰι",  ],
312[ "ᾲ στο διάολο", "ᾲ στο διάολο", "ᾲ Στο Διάολο", "ᾲ ΣΤΟ ΔΙΆΟΛΟ", "ᾲ στο διάολο", "Ὰͅ Στο Διάολο", "ᾺΙ ΣΤΟ ΔΙΆΟΛΟ", "ᾲ στο διάολο", "ὰι στο διάολο", "ὰι στο διάολο",  ],
313[ "ᾲ στο διάολο", "ᾲ στο διάολο", "Ὰͅ Στο Διάολο", "ᾺΙ ΣΤΟ ΔΙΆΟΛΟ", "ᾲ στο διάολο", "Ὰͅ Στο Διάολο", "ᾺΙ ΣΤΟ ΔΙΆΟΛΟ", "ὰι στο διάολο", "ὰι στο διάολο", "ὰι στο διάολο",  ],
314[ "ⅷ", "ⅷ", "Ⅷ", "Ⅷ", "ⅷ", "Ⅷ", "Ⅷ", "ⅷ", "ⅷ", "ⅷ",  ],
315[ "henry ⅷ", "henry ⅷ", "Henry Ⅷ", "HENRY Ⅷ", "henry ⅷ", "Henry Ⅷ", "HENRY Ⅷ", "henry ⅷ", "henry ⅷ", "henry ⅷ",  ],
316[ "ⓚ", "ⓚ", "Ⓚ", "Ⓚ", "ⓚ", "Ⓚ", "Ⓚ", "ⓚ", "ⓚ", "ⓚ",  ],
317[ "i work at ⓚ", "i work at ⓚ", "I Work At Ⓚ", "I WORK AT Ⓚ", "i work at ⓚ", "I Work At Ⓚ", "I WORK AT Ⓚ", "i work at ⓚ", "i work at ⓚ", "i work at ⓚ",  ],
318[ "istambul", "istambul", "Istambul", "ISTAMBUL", "istambul", "Istambul", "ISTAMBUL", "istambul", "istambul", "istambul",  ],
319[ "i̇stanbul", "i̇stanbul", "İstanbul", "İSTANBUL", "i̇stanbul", "İstanbul", "İSTANBUL", "i̇stanbul", "i̇stanbul", "i̇stanbul",  ],
320[ "İstanbul", "i̇stanbul", "İstanbul", "İSTANBUL", "i̇stanbul", "İstanbul", "İSTANBUL", "i̇stanbul", "ı̇stanbul", "i̇stanbul",  ],
321[ "İSTANBUL", "istanbul", "İstanbul", "İSTANBUL", "i̇stanbul", "İstanbul", "İSTANBUL", "İstanbul", "istanbul", "i̇stanbul",  ],
322[ "στιγμας", "στιγμας", "Στιγμας", "ΣΤΙΓΜΑΣ", "στιγμας", "Στιγμας", "ΣΤΙΓΜΑΣ", "στιγμασ", "στιγμασ", "στιγμασ",  ],
323[ "στιγμασ", "στιγμασ", "Στιγμασ", "ΣΤΙΓΜΑΣ", "στιγμασ", "Στιγμασ", "ΣΤΙΓΜΑΣ", "στιγμασ", "στιγμασ", "στιγμασ",  ],
324[ "ΣΤΙΓΜΑΣ", "στιγμασ", "Στιγμασ", "ΣΤΙΓΜΑΣ", "στιγμασ", "Στιγμασ", "ΣΤΙΓΜΑΣ", "στιγμασ", "στιγμασ", "στιγμασ",  ],
325[ "ʀᴀʀᴇ", "ʀᴀʀᴇ", "Ʀᴀʀᴇ", "ƦᴀƦᴇ", "ʀᴀʀᴇ", "Ʀᴀʀᴇ", "ƦᴀƦᴇ", "ʀᴀʀᴇ", "ʀᴀʀᴇ", "ʀᴀʀᴇ",  ],
326[ "��������������", "��������������", "��������������", "��������������", "��������������", "��������������", "��������������", "��������������", "��������������", "��������������",  ],
327[ "Ԧԧ", "ԧԧ", "Ԧԧ", "ԦԦ", "ԧԧ", "Ԧԧ", "ԦԦ", "ԧԧ", "ԧԧ", "ԧԧ",  ],
328[ "ﬓﬔﬕﬖﬗ", "ﬓﬔﬕﬖﬗ", "ﬓﬔﬕﬖﬗ", "ﬓﬔﬕﬖﬗ", "ﬓﬔﬕﬖﬗ", "Մնﬔﬕﬖﬗ", "ՄՆՄԵՄԻՎՆՄԽ", "ﬓﬔﬕﬖﬗ", "մնմեմիվնմխ", "մնմեմիվնմխ",  ],
329[ "ʼn groot", "ʼn groot", "ʼn Groot", "ʼn GROOT", "ʼn groot", "ʼN Groot", "ʼN GROOT", "ʼn groot", "ʼn groot", "ʼn groot",  ],
330[ "ẚ", "ẚ", "ẚ", "ẚ", "ẚ", "Aʾ", "Aʾ", "ẚ", "aʾ", "aʾ",  ],
331[ "ff", "ff", "ff", "ff", "ff", "Ff", "FF", "ff", "ff", "ff",  ],
332[ "ǰ", "ǰ", "ǰ", "ǰ", "ǰ", "J̌", "J̌", "ǰ", "ǰ", "ǰ",  ],
333[ "550 nm or Å", "550 nm or å", "550 Nm Or Å", "550 NM OR Å", "550 nm or å", "550 Nm Or Å", "550 NM OR Å", "550 nm or å", "550 nm or å", "550 nm or å",  ],
334);
335
336    use feature qw(fc);
337
338    for (@test_table) {
339        my ($simple_lc, $simple_tc, $simple_uc, $simple_fc) = @{$_}[1, 2, 3, 7];
340        my ($orig, $lower, $titlecase, $upper, $fc_turkic, $fc_full) = @{$_}[0,4,5,6,8,9];
341
342        is( fc($orig), $fc_full, 'fc works' );
343        is( "\F$orig", $fc_full, '\F works' );
344        is( lc($orig), $lower,   'lc works' );
345        is( "\L$orig", $lower,   '\L works' );
346        is( uc($orig), $upper,   'uc works' );
347        is( "\U$orig", $upper,   '\U works' );
348    }
349}
350
351{
352    use feature qw(fc);
353    package Eeyup  { use overload q{""} => sub { "\x{df}"   }, fallback => 1 }
354    package Uunope { use overload q{""} => sub { "\x{30cb}" }, fallback => 1 }
355    package Undef  { use overload q{""} => sub {   undef    }, fallback => 1 }
356
357    my $obj = bless {}, "Eeyup";
358    is(fc($obj), "ss", "fc() works on overloaded objects returning latin-1");
359    $obj = bless {}, "Eeyup";
360    is("\F$obj", "ss", '\F works on overloaded objects returning latin-1');
361
362    $obj = bless {}, "Uunope";
363    is(fc($obj), "\x{30cb}", "fc() works on overloaded objects returning UTF-8");
364    $obj = bless {}, "Uunope";
365    is("\F$obj", "\x{30cb}", '\F works on overloaded objects returning UTF-8');
366
367    $obj = bless {}, "Undef";
368    my $warnings;
369    {
370        no warnings;
371        use warnings "uninitialized";
372        local $SIG{__WARN__} = sub { $warnings++; like(shift, qr/Use of uninitialized value (?:\$obj )?in fc/) };
373        fc(undef);
374        fc($obj);
375    }
376    is( $warnings, 2, "correct number of warnings" );
377
378    my $fetched = 0;
379    package Derpy { sub TIESCALAR { bless {}, shift } sub FETCH { $fetched++; "\x{df}" } }
380
381    tie my $x, "Derpy";
382
383    is( fc($x), "ss", "fc() works on tied values" );
384    is( $fetched, 1, "and only calls the magic once" );
385
386}
387
388{
389    use feature qw( fc );
390    my $troublesome1 = "\xdf" x 11; #SvLEN should be 12, SvCUR should be 11
391                                    #So this should force fc() to grow the string.
392
393    is( fc($troublesome1), "ss" x 11, "fc() grows the string" );
394
395    my $troublesome2 = "abcdef:\x{df}:fjksjs"; #SvLEN should be 16, SvCUR should be 15
396    is( fc($troublesome2), "abcdef:ss:fjksjs", "fc() expands \\x{DF} in the middle of a string that needs to grow" );
397
398    my $troublesome3 = ":\x{df}:";
399    is( fc($troublesome3), ":ss:", "fc() expands \\x{DF} in the middle of a string" );
400
401
402    my $troublesome4 = "\x{B5}"; #\N{MICRON SIGN} is latin-1, but its foldcase is in UTF-8
403
404    is( fc($troublesome4), "\x{3BC}", "fc() for a latin-1 \x{B5} returns UTF-8" );
405    ok( !utf8::is_utf8($troublesome4), "fc() doesn't upgrade the original string" );
406
407
408    my $troublesome5 = "\x{C9}abda\x{B5}aaf\x{C8}"; # Up until foldcasing \x{B5}, the string
409                                                    # was in Latin-1. This tests that the
410                                                    # results don't have illegal UTF-8
411                                                    # (i.e. leftover latin-1) in them
412
413    is( fc($troublesome5), "\x{E9}abda\x{3BC}aaf\x{E8}" );
414}
415
416{
417    use feature qw( fc unicode_strings );
418
419    # This tests both code paths in pp_fc
420
421    for (0..0xff) {
422        my $latin1 = chr;
423        my $utf8   = $latin1;
424        utf8::downgrade($latin1); #No-op, but doesn't hurt
425        utf8::upgrade($utf8);
426        is(fc($latin1), fc($utf8), "fc() gives the same results for \\x{$_} in Latin-1 and UTF-8 under unicode_strings");
427        SKIP: {
428            if (
429                !$Config::Config{d_setlocale}
430            || $Config::Config{ccflags} =~ /\bD?NO_LOCALE(_|\b)/
431            ) {
432                skip "no locale support", 2
433            }
434            use locale;
435            is(fc($latin1), lc($latin1), "use locale; fc(qq{\\x{$_}}), lc(qq{\\x{$_}}) when qq{\\x{$_}} is in latin-1");
436            is(fc($utf8), lc($utf8), "use locale; fc(qq{\\x{$_}}), lc(qq{\\x{$_}}) when qq{\\x{$_}} is in latin-1");
437        }
438        {
439            no feature 'unicode_strings';
440            is(fc($latin1), lc($latin1), "under nothing, fc() for <256 is the same as lc");
441        }
442    }
443}
444
445my $num_tests = curr_test() - 1;
446
447die qq[$0: failed to find casefoldings from "$CF"\n] unless $num_tests > 0;
448
449plan($num_tests);
450