4 # re/fold_grind.t has more complex tests, but doesn't test every fold
12 binmode *STDOUT, ":utf8";
18 # Read in the official case folding definitions.
19 my $CF = '../lib/unicore/CaseFolding.txt';
21 die qq[$0: failed to open "$CF": $!\n] if ! open(my $fh, "<", $CF);
26 # Skip S since we are going for 'F'ull case folding. I is obsolete starting
27 # with Unicode 3.2, but leaving it in does no harm, and allows backward
29 next unless my ($code, $type, $mapping, $name) = $_ =~
30 /^([0-9A-F]+); ([CFI]); ((?:[0-9A-F]+)(?: [0-9A-F]+)*); \# (.+)/;
32 # Convert any 0-255 range chars to native.
33 $code = sprintf("%04X", ord_latin1_to_native(hex $code)) if hex $code < 0x100;
34 $mapping = join " ", map { $_ =
35 sprintf("%04X", ord_latin1_to_native(hex $_)) }
38 push @CF, [$code, $mapping, $type, $name];
40 # Get the inverse fold for single-char mappings.
41 $reverse_fold{pack "U0U*", hex $mapping} = pack "U0U*", hex $code if $type ne 'F';
44 close($fh) or die "$0 Couldn't close $CF";
46 foreach my $test_ref (@CF) {
47 my ($code, $mapping, $type, $name) = @$test_ref;
48 my $c = pack("U0U*", hex $code);
49 my $f = pack("U0U*", map { hex } split " ", $mapping);
50 my $f_length = length $f;
53 qq[":$c:" =~ /:$c:/i],
54 qq[":$c:" =~ /:[_$c]:/], # Place two chars in [] so doesn't get
55 # optimized to a non-charclass
56 qq[":$c:" =~ /:[_$c]:/i],
57 qq[":$c:" =~ /:$f:/i],
58 qq[":$f:" =~ /:$c:/i],
60 ok eval $test, "$code - $name - $mapping - $type - $test";
63 # Certain tests weren't convenient to put in the list above since they are
64 # TODO's in multi-character folds.
67 # The qq loses the utf8ness of ":$f:". These tests are not about
68 # finding bugs in utf8ness, so make sure it's utf8.
69 my $test = qq[my \$s = ":$f:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
70 ok eval $test, "$code - $name - $mapping - $type - $test";
71 $test = qq[":$c:" =~ /:[_$f]:/i];
72 ok eval $test, "$code - $name - $mapping - $type - $test";
76 # There are two classes of multi-char folds that need more work. For
78 # ":ß:" =~ /:[_s]{2}:/i
81 # Some of the old tests for the second case happened to pass somewhat
82 # coincidentally. But none would pass if changed to this.
85 # As the capital SS doesn't get folded. When those pass, it means
86 # that the code has been changed to take into account folding in the
87 # string, and all should pass, capitalized or not (this wouldn't be
88 # true for [^complemented character classes], for which the fold case
89 # is better, but these aren't used in this .t currently. So, what is
90 # done is to essentially upper-case the string for this class (but use
91 # the reverse fold not uc(), as that is more correct)
93 for my $i (0 .. $f_length - 1) {
94 my $cur_char = substr($f, $i, 1);
95 $u .= $reverse_fold{$cur_char} || $cur_char;
99 # A multi-char fold should not match just one char;
100 # e.g., ":ß:" !~ /:[_s]:/i
101 $test = qq[":$c:" !~ /:[_$f]:/i];
102 ok eval $test, "$code - $name - $mapping - $type - $test";
104 TODO: { # e.g., ":ß:" =~ /:[_s]{2}:/i
105 local $TODO = 'Multi-char fold in [character class]';
107 $test = qq[":$c:" =~ /:[_$f]{$f_length}:/i];
108 ok eval $test, "$code - $name - $mapping - $type - $test";
111 # e.g., ":SS:" =~ /:[_ß]:/i now pass, so TODO has been removed, but
112 # since they use '$u', they are left out of the main loop
113 $test = qq[ my \$s = ":$u:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
114 ok eval $test, "$code - $name - $mapping - $type - $test";
118 my $num_tests = curr_test() - 1;
120 die qq[$0: failed to find casefoldings from "$CF"\n] unless $num_tests > 0;