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