This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlsub: Fix new typo
[perl5.git] / t / uni / case.pl
1 require "test.pl";
2
3 sub unidump {
4     join " ", map { sprintf "%04X", $_ } unpack "U*", $_[0];
5 }
6
7 sub casetest {
8     my ($already_run, $base, $spec, @funcs) = @_;
9     # For each provided function run it, and run a version with some extra
10     # characters afterwards. Use a recycling symbol, as it doesn't change case.
11     # $already_run is the number of extra tests the caller has run before this
12     # call.
13     my $ballast = chr (0x2672) x 3;
14     @funcs = map {my $f = $_;
15                   ($f,
16                    sub {my $r = $f->($_[0] . $ballast); # Add it before
17                         $r =~ s/$ballast\z//so # Remove it afterwards
18                             or die "'$_[0]' to '$r' mangled";
19                         $r; # Result with $ballast removed.
20                     },
21                    )} @funcs;
22
23     my $file = "../lib/unicore/To/$base.pl";
24     my $simple = do $file or die $@;
25     my %simple;
26     for my $i (split(/\n/, $simple)) {
27         my ($k, $v) = split(' ', $i);
28
29         # Add the simple mapping to the simples test list, except the input
30         # may include code points that the specials override, so don't add
31         # those to the test list.  The specials keys are the code points,
32         # encoded in utf8,, but without the utf8 flag on, so pack with C0.
33         $simple{$k} = $v unless exists $spec->{pack("C0U", hex $k)};
34     }
35     my %seen;
36
37     for my $i (sort keys %simple) {
38         $seen{$i}++;
39     }
40     print "# ", scalar keys %simple, " simple mappings\n";
41
42     my $both;
43
44     for my $i (sort keys %$spec) {
45         if (++$seen{$i} == 2) {
46             warn sprintf "$base: $i seen twice\n";
47             $both++;
48         }
49     }
50     print "# ", scalar keys %$spec, " special mappings\n";
51
52     exit(1) if $both;
53
54     my %none;
55     for my $i (map { ord } split //,
56                "\e !\"#\$%&'()+,-./0123456789:;<=>?\@[\\]^_{|}~\b") {
57         next if pack("U0U", $i) =~ /\w/;
58         $none{$i}++ unless $seen{$i};
59     }
60     print "# ", scalar keys %none, " noncase mappings\n";
61
62     my $tests = 
63         $already_run +
64         ((scalar keys %simple) +
65          (scalar keys %$spec) +
66          (scalar keys %none)) * @funcs;
67
68     my $test = $already_run + 1;
69
70     for my $i (sort keys %simple) {
71         my $w = $simple{$i};
72         my $c = pack "U0U", hex $i;
73         foreach my $func (@funcs) {
74             my $d = $func->($c);
75             my $e = unidump($d);
76             print $d eq pack("U0U", hex $simple{$i}) ?
77                 "ok $test # $i -> $w\n" : "not ok $test # $i -> $e ($w)\n";
78                 $test++;
79         }
80     }
81
82     for my $i (sort keys %$spec) {
83         my $w = unidump($spec->{$i});
84         if (ord('A') == 193 && $i eq "\x8A\x73") {
85             $w = '0178'; # It's a Latin small Y with diaeresis and not a Latin small letter sharp 's'.
86         }
87         my $u = unpack "C0U", $i;
88         my $h = sprintf "%04X", $u;
89         my $c = chr($u); $c .= chr(0x100); chop $c;
90         foreach my $func (@funcs) {
91             my $d = $func->($c);
92             my $e = unidump($d);
93             if (ord "A" == 193) { # EBCDIC
94                 # We need to a little bit of remapping.
95                 #
96                 # For example, in titlecase (ucfirst) mapping
97                 # of U+0149 the Unicode mapping is U+02BC U+004E.
98                 # The 4E is N, which in EBCDIC is 2B--
99                 # and the ucfirst() does that right.
100                 # The problem is that our reference
101                 # data is in Unicode code points.
102                 #
103                 # The Right Way here would be to use, say,
104                 # Encode, to remap the less-than 0x100 code points,
105                 # but let's try to be Encode-independent here. 
106                 #
107                 # These are the titlecase exceptions:
108                 #
109                 #         Unicode   Unicode+EBCDIC  
110                 #
111                 # 0149 -> 02BC 004E (02BC 002B)
112                 # 01F0 -> 004A 030C (00A2 030C)
113                 # 1E96 -> 0048 0331 (00E7 0331)
114                 # 1E97 -> 0054 0308 (00E8 0308)
115                 # 1E98 -> 0057 030A (00EF 030A)
116                 # 1E99 -> 0059 030A (00DF 030A)
117                 # 1E9A -> 0041 02BE (00A0 02BE)
118                 #
119                 # The uppercase exceptions are identical.
120                 #
121                 # The lowercase has one more:
122                 #
123                 #         Unicode   Unicode+EBCDIC  
124                 #
125                 # 0130 -> 0069 0307 (00D1 0307)
126                 #
127                 if ($h =~ /^(0130|0149|01F0|1E96|1E97|1E98|1E99|1E9A)$/) {
128                     $e =~ s/004E/002B/; # N
129                     $e =~ s/004A/00A2/; # J
130                     $e =~ s/0048/00E7/; # H
131                     $e =~ s/0054/00E8/; # T
132                     $e =~ s/0057/00EF/; # W
133                     $e =~ s/0059/00DF/; # Y
134                     $e =~ s/0041/00A0/; # A
135                     $e =~ s/0069/00D1/; # i
136                 }
137                 # We have to map the output, not the input, because
138                 # pack/unpack U has been EBCDICified, too, it would
139                 # just undo our remapping.
140             }
141             print $w eq $e ?
142                 "ok $test # $i -> $w\n" : "not ok $test # $h -> $e ($w)\n";
143                 $test++;
144         }
145     }
146
147     for my $i (sort { $a <=> $b } keys %none) {
148         my $w = $i = sprintf "%04X", $i;
149         my $c = pack "U0U", hex $i;
150         foreach my $func (@funcs) {
151             my $d = $func->($c);
152             my $e = unidump($d);
153             print $d eq $c ?
154                 "ok $test # $i -> $w\n" : "not ok $test # $i -> $e ($w)\n";
155                 $test++;
156         }
157     }
158
159     print "1..$tests\n";
160 }
161
162 1;