4 # re/fold_grind.t has more complex tests, but doesn't test every fold
12 binmode *STDOUT, ":utf8";
19 # Read in the official case folding definitions.
20 my $CF = File::Spec->catfile(File::Spec->catdir(File::Spec->updir,
24 die qq[$0: failed to open "$CF": $!\n] if ! open(my $fh, "<", $CF);
29 # Skip S since we are going for 'F'ull case folding. I is obsolete starting
30 # with Unicode 3.2, but leaving it in does no harm, and allows backward
32 next unless my ($code, $type, $mapping, $name) = $_ =~
33 /^([0-9A-F]+); ([CFI]); ((?:[0-9A-F]+)(?: [0-9A-F]+)*); \# (.+)/;
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 $_)) }
41 push @CF, [$code, $mapping, $type, $name];
43 # Get the inverse fold for single-char mappings.
44 $reverse_fold{pack "U0U*", hex $mapping} = pack "U0U*", hex $code if $type ne 'F';
47 close($fh) or die "$0 Couldn't close $CF";
49 foreach my $test_ref (@CF) {
50 my ($code, $mapping, $type, $name) = @$test_ref;
51 my $c = pack("U0U*", hex $code);
52 my $f = pack("U0U*", map { hex } split " ", $mapping);
53 my $f_length = length $f;
56 qq[":$c:" =~ /:$c:/i],
57 qq[":$c:" =~ /:[_$c]:/], # Place two chars in [] so doesn't get
58 # optimized to a non-charclass
59 qq[":$c:" =~ /:[_$c]:/i],
60 qq[":$c:" =~ /:$f:/i],
61 qq[":$f:" =~ /:$c:/i],
63 ok eval $test, "$code - $name - $mapping - $type - $test";
66 # Certain tests weren't convenient to put in the list above since they are
67 # TODO's in multi-character folds.
70 # The qq loses the utf8ness of ":$f:". These tests are not about
71 # finding bugs in utf8ness, so make sure it's utf8.
72 my $test = qq[my \$s = ":$f:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
73 ok eval $test, "$code - $name - $mapping - $type - $test";
74 $test = qq[":$c:" =~ /:[_$f]:/i];
75 ok eval $test, "$code - $name - $mapping - $type - $test";
79 # There are two classes of multi-char folds that don't pass. For
81 # ":ß:" =~ /:[_s]{2}:/i
84 # Some of the old tests for the second case happened to pass somewhat
85 # coincidentally. But none would pass if changed to this.
88 # As the capital SS doesn't get folded. When those pass, it means
89 # that the code has been changed to take into account folding in the
90 # string, and all should pass, capitalized or not. So, what is done
91 # is to essentially upper-case the string for this class (but use the
92 # reverse fold not uc(), as that is more correct)
94 for my $i (0 .. $f_length - 1) {
95 my $cur_char = substr($f, $i, 1);
96 $u .= $reverse_fold{$cur_char} || $cur_char;
100 local $TODO = 'Multi-char fold in [character class]';
102 TODO: { # e.g., ":ß:" !~ /:[_s]:/i # A multi-char fold should not
103 # match just one char
104 $test = qq[":$c:" !~ /:[_$f]:/i];
105 ok eval $test, "$code - $name - $mapping - $type - $test";
107 TODO: { # e.g., ":ß:" =~ /:[_s]{2}:/i
108 $test = qq[":$c:" =~ /:[_$f]{$f_length}:/i];
109 ok eval $test, "$code - $name - $mapping - $type - $test";
111 TODO: { # e.g., ":SS:" =~ /:[_ß]:/i
112 $test = qq[ my \$s = ":$u:"; utf8::upgrade(\$s); \$s =~ /:[_$c]:/i];
113 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;