source: trunk/essentials/dev-lang/perl/t/uni/case.pl

Last change on this file was 3181, checked in by bird, 18 years ago

perl 5.8.8

File size: 3.9 KB
Line 
1use File::Spec;
2
3require "test.pl";
4
5sub unidump {
6 join " ", map { sprintf "%04X", $_ } unpack "U*", $_[0];
7}
8
9sub casetest {
10 my ($base, $spec, @funcs) = @_;
11 # For each provided function run it, and run a version with some extra
12 # characters afterwards. Use a recylcing symbol, as it doesn't change case.
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 = File::Spec->catfile(File::Spec->catdir(File::Spec->updir,
24 "lib", "unicore", "To"),
25 "$base.pl");
26 my $simple = do $file;
27 my %simple;
28 for my $i (split(/\n/, $simple)) {
29 my ($k, $v) = split(' ', $i);
30 $simple{$k} = $v;
31 }
32 my %seen;
33
34 for my $i (sort keys %simple) {
35 $seen{$i}++;
36 }
37 print "# ", scalar keys %simple, " simple mappings\n";
38
39 my $both;
40
41 for my $i (sort keys %$spec) {
42 if (++$seen{$i} == 2) {
43 warn sprintf "$base: $i seen twice\n";
44 $both++;
45 }
46 }
47 print "# ", scalar keys %$spec, " special mappings\n";
48
49 exit(1) if $both;
50
51 my %none;
52 for my $i (map { ord } split //,
53 "\e !\"#\$%&'()+,-./0123456789:;<=>?\@[\\]^_{|}~\b") {
54 next if pack("U0U", $i) =~ /\w/;
55 $none{$i}++ unless $seen{$i};
56 }
57 print "# ", scalar keys %none, " noncase mappings\n";
58
59 my $tests =
60 ((scalar keys %simple) +
61 (scalar keys %$spec) +
62 (scalar keys %none)) * @funcs;
63 print "1..$tests\n";
64
65 my $test = 1;
66
67 for my $i (sort keys %simple) {
68 my $w = $simple{$i};
69 my $c = pack "U0U", hex $i;
70 foreach my $func (@funcs) {
71 my $d = $func->($c);
72 my $e = unidump($d);
73 print $d eq pack("U0U", hex $simple{$i}) ?
74 "ok $test # $i -> $w\n" : "not ok $test # $i -> $e ($w)\n";
75 $test++;
76 }
77 }
78
79 for my $i (sort keys %$spec) {
80 my $w = unidump($spec->{$i});
81 my $u = unpack "U0U", $i;
82 my $h = sprintf "%04X", $u;
83 my $c = chr($u); $c .= chr(0x100); chop $c;
84 foreach my $func (@funcs) {
85 my $d = $func->($c);
86 my $e = unidump($d);
87 if (ord "A" == 193) { # EBCDIC
88 # We need to a little bit of remapping.
89 #
90 # For example, in titlecase (ucfirst) mapping
91 # of U+0149 the Unicode mapping is U+02BC U+004E.
92 # The 4E is N, which in EBCDIC is 2B--
93 # and the ucfirst() does that right.
94 # The problem is that our reference
95 # data is in Unicode code points.
96 #
97 # The Right Way here would be to use, say,
98 # Encode, to remap the less-than 0x100 code points,
99 # but let's try to be Encode-independent here.
100 #
101 # These are the titlecase exceptions:
102 #
103 # Unicode Unicode+EBCDIC
104 #
105 # 0149 -> 02BC 004E (02BC 002B)
106 # 01F0 -> 004A 030C (00A2 030C)
107 # 1E96 -> 0048 0331 (00E7 0331)
108 # 1E97 -> 0054 0308 (00E8 0308)
109 # 1E98 -> 0057 030A (00EF 030A)
110 # 1E99 -> 0059 030A (00DF 030A)
111 # 1E9A -> 0041 02BE (00A0 02BE)
112 #
113 # The uppercase exceptions are identical.
114 #
115 # The lowercase has one more:
116 #
117 # Unicode Unicode+EBCDIC
118 #
119 # 0130 -> 0069 0307 (00D1 0307)
120 #
121 if ($i =~ /^(0130|0149|01F0|1E96|1E97|1E98|1E99|1E9A)$/) {
122 $e =~ s/004E/002B/; # N
123 $e =~ s/004A/00A2/; # J
124 $e =~ s/0048/00E7/; # H
125 $e =~ s/0054/00E8/; # T
126 $e =~ s/0057/00EF/; # W
127 $e =~ s/0059/00DF/; # Y
128 $e =~ s/0041/00A0/; # A
129 $e =~ s/0069/00D1/; # i
130 }
131 # We have to map the output, not the input, because
132 # pack/unpack U has been EBCDICified, too, it would
133 # just undo our remapping.
134 }
135 print $w eq $e ?
136 "ok $test # $i -> $w\n" : "not ok $test # $h -> $e ($w)\n";
137 $test++;
138 }
139 }
140
141 for my $i (sort { $a <=> $b } keys %none) {
142 my $w = $i = sprintf "%04X", $i;
143 my $c = pack "U0U", hex $i;
144 foreach my $func (@funcs) {
145 my $d = $func->($c);
146 my $e = unidump($d);
147 print $d eq $c ?
148 "ok $test # $i -> $w\n" : "not ok $test # $i -> $e ($w)\n";
149 $test++;
150 }
151 }
152}
153
1541;
Note: See TracBrowser for help on using the repository browser.