Commit | Line | Data |
---|---|---|
8ebc5c01 | 1 | #!./perl -wT |
2 | ||
66cbab2c KW |
3 | # This tests plain 'use locale' and adorned 'use locale ":not_characters"' |
4 | # Because these pragmas are compile time, and I (khw) am trying to test | |
5 | # without using 'eval' as much as possible, which might cloud the issue, the | |
6 | # crucial parts of the code are duplicated in a block for each pragma. | |
7 | ||
6c2e653d KW |
8 | # To make a TODO test, add the string 'TODO' to its %test_names value |
9 | ||
e3a2734b KW |
10 | binmode STDOUT, ':utf8'; |
11 | binmode STDERR, ':utf8'; | |
12 | ||
8ebc5c01 | 13 | BEGIN { |
14 | chdir 't' if -d 't'; | |
20822f61 | 15 | @INC = '../lib'; |
f9cbebe1 | 16 | unshift @INC, '.'; |
b002077a | 17 | require Config; import Config; |
97a0514d | 18 | if (!$Config{d_setlocale} || $Config{ccflags} =~ /\bD?NO_LOCALE\b/) { |
b002077a CS |
19 | print "1..0\n"; |
20 | exit; | |
21 | } | |
2de3dbcc | 22 | $| = 1; |
8ebc5c01 | 23 | } |
24 | ||
25 | use strict; | |
26c1569f | 26 | use feature 'fc'; |
8ebc5c01 | 27 | |
26c1569f | 28 | my $debug = 0; |
284102e8 | 29 | |
db4b7445 A |
30 | use Dumpvalue; |
31 | ||
32 | my $dumper = Dumpvalue->new( | |
33 | tick => qq{"}, | |
34 | quoteHighBit => 0, | |
35 | unctrl => "quote" | |
36 | ); | |
6be75cd7 | 37 | sub debug { |
db4b7445 A |
38 | return unless $debug; |
39 | my($mess) = join "", @_; | |
40 | chop $mess; | |
41 | print $dumper->stringify($mess,1), "\n"; | |
6be75cd7 JH |
42 | } |
43 | ||
44 | sub debugf { | |
45 | printf @_ if $debug; | |
46 | } | |
47 | ||
8ebc5c01 | 48 | my $have_setlocale = 0; |
49 | eval { | |
50 | require POSIX; | |
51 | import POSIX ':locale_h'; | |
52 | $have_setlocale++; | |
53 | }; | |
54 | ||
6dead956 | 55 | # Visual C's CRT goes silly on strings of the form "en_US.ISO8859-1" |
f6c6487a | 56 | # and mingw32 uses said silly CRT |
3a2d1764 SH |
57 | # This doesn't seem to be an issue any more, at least on Windows XP, |
58 | # so re-enable the tests for Windows XP onwards. | |
59 | my $winxp = ($^O eq 'MSWin32' && defined &Win32::GetOSVersion && | |
60 | join('.', (Win32::GetOSVersion())[1..2]) >= 5.1); | |
61 | $have_setlocale = 0 if ((($^O eq 'MSWin32' && !$winxp) || $^O eq 'NetWare') && | |
62 | $Config{cc} =~ /^(cl|gcc)/i); | |
6dead956 | 63 | |
36a42ae7 | 64 | # UWIN seems to loop after taint tests, just skip for now |
cd19b65c JH |
65 | $have_setlocale = 0 if ($^O =~ /^uwin/); |
66 | ||
9a66ea41 | 67 | sub LC_ALL (); |
8ebc5c01 | 68 | |
0e053d1e | 69 | $a = 'abc %'; |
8ebc5c01 | 70 | |
c213d471 KW |
71 | my $test_num = 0; |
72 | ||
8ebc5c01 | 73 | sub ok { |
c213d471 | 74 | my ($result, $message) = @_; |
e3a2734b | 75 | $message = "" unless defined $message; |
8ebc5c01 | 76 | |
77 | print 'not ' unless ($result); | |
c213d471 | 78 | print "ok " . ++$test_num; |
e3a2734b KW |
79 | print " $message"; |
80 | print "\n"; | |
8ebc5c01 | 81 | } |
82 | ||
83 | # First we'll do a lot of taint checking for locales. | |
84 | # This is the easiest to test, actually, as any locale, | |
85 | # even the default locale will taint under 'use locale'. | |
86 | ||
87 | sub is_tainted { # hello, camel two. | |
9f1b1f2d | 88 | no warnings 'uninitialized' ; |
8ebc5c01 | 89 | my $dummy; |
ba74571d | 90 | local $@; |
8ebc5c01 | 91 | not eval { $dummy = join("", @_), kill 0; 1 } |
92 | } | |
93 | ||
a9b7c637 KW |
94 | sub check_taint ($;$) { |
95 | my $message_tail = $_[1] // ""; | |
96 | $message_tail = ": $message_tail" if $message_tail; | |
97 | ok is_tainted($_[0]), "verify that is tainted$message_tail"; | |
8ebc5c01 | 98 | } |
99 | ||
a9b7c637 KW |
100 | sub check_taint_not ($;$) { |
101 | my $message_tail = $_[1] // ""; | |
102 | $message_tail = ": $message_tail" if $message_tail; | |
103 | ok((not is_tainted($_[0])), "verify that isn't tainted$message_tail"); | |
8ebc5c01 | 104 | } |
105 | ||
bf3cd0e6 KW |
106 | "\tb\t" =~ /^m?(\s)(.*)\1$/; |
107 | check_taint_not $&, "not tainted outside 'use locale'"; | |
108 | ; | |
109 | ||
8ebc5c01 | 110 | use locale; # engage locale and therefore locale taint. |
111 | ||
36a42ae7 | 112 | check_taint_not $a; |
8ebc5c01 | 113 | |
36a42ae7 KW |
114 | check_taint uc($a); |
115 | check_taint "\U$a"; | |
116 | check_taint ucfirst($a); | |
117 | check_taint "\u$a"; | |
118 | check_taint lc($a); | |
26c1569f | 119 | check_taint fc($a); |
36a42ae7 | 120 | check_taint "\L$a"; |
26c1569f | 121 | check_taint "\F$a"; |
36a42ae7 KW |
122 | check_taint lcfirst($a); |
123 | check_taint "\l$a"; | |
8ebc5c01 | 124 | |
36a42ae7 KW |
125 | check_taint_not sprintf('%e', 123.456); |
126 | check_taint_not sprintf('%f', 123.456); | |
127 | check_taint_not sprintf('%g', 123.456); | |
128 | check_taint_not sprintf('%d', 123.456); | |
129 | check_taint_not sprintf('%x', 123.456); | |
8ebc5c01 | 130 | |
131 | $_ = $a; # untaint $_ | |
132 | ||
133 | $_ = uc($a); # taint $_ | |
134 | ||
36a42ae7 | 135 | check_taint $_; |
8ebc5c01 | 136 | |
137 | /(\w)/; # taint $&, $`, $', $+, $1. | |
36a42ae7 KW |
138 | check_taint $&; |
139 | check_taint $`; | |
140 | check_taint $'; | |
141 | check_taint $+; | |
142 | check_taint $1; | |
143 | check_taint_not $2; | |
8ebc5c01 | 144 | |
145 | /(.)/; # untaint $&, $`, $', $+, $1. | |
36a42ae7 KW |
146 | check_taint_not $&; |
147 | check_taint_not $`; | |
148 | check_taint_not $'; | |
149 | check_taint_not $+; | |
150 | check_taint_not $1; | |
151 | check_taint_not $2; | |
8ebc5c01 | 152 | |
153 | /(\W)/; # taint $&, $`, $', $+, $1. | |
36a42ae7 KW |
154 | check_taint $&; |
155 | check_taint $`; | |
156 | check_taint $'; | |
157 | check_taint $+; | |
158 | check_taint $1; | |
159 | check_taint_not $2; | |
8ebc5c01 | 160 | |
161 | /(\s)/; # taint $&, $`, $', $+, $1. | |
36a42ae7 KW |
162 | check_taint $&; |
163 | check_taint $`; | |
164 | check_taint $'; | |
165 | check_taint $+; | |
166 | check_taint $1; | |
167 | check_taint_not $2; | |
8ebc5c01 | 168 | |
169 | /(\S)/; # taint $&, $`, $', $+, $1. | |
36a42ae7 KW |
170 | check_taint $&; |
171 | check_taint $`; | |
172 | check_taint $'; | |
173 | check_taint $+; | |
174 | check_taint $1; | |
175 | check_taint_not $2; | |
8ebc5c01 | 176 | |
177 | $_ = $a; # untaint $_ | |
178 | ||
36a42ae7 | 179 | check_taint_not $_; |
8ebc5c01 | 180 | |
181 | /(b)/; # this must not taint | |
36a42ae7 KW |
182 | check_taint_not $&; |
183 | check_taint_not $`; | |
184 | check_taint_not $'; | |
185 | check_taint_not $+; | |
186 | check_taint_not $1; | |
187 | check_taint_not $2; | |
8ebc5c01 | 188 | |
189 | $_ = $a; # untaint $_ | |
190 | ||
36a42ae7 | 191 | check_taint_not $_; |
8ebc5c01 | 192 | |
193 | $b = uc($a); # taint $b | |
194 | s/(.+)/$b/; # this must taint only the $_ | |
195 | ||
36a42ae7 KW |
196 | check_taint $_; |
197 | check_taint_not $&; | |
198 | check_taint_not $`; | |
199 | check_taint_not $'; | |
200 | check_taint_not $+; | |
201 | check_taint_not $1; | |
202 | check_taint_not $2; | |
8ebc5c01 | 203 | |
204 | $_ = $a; # untaint $_ | |
205 | ||
206 | s/(.+)/b/; # this must not taint | |
36a42ae7 KW |
207 | check_taint_not $_; |
208 | check_taint_not $&; | |
209 | check_taint_not $`; | |
210 | check_taint_not $'; | |
211 | check_taint_not $+; | |
212 | check_taint_not $1; | |
213 | check_taint_not $2; | |
8ebc5c01 | 214 | |
215 | $b = $a; # untaint $b | |
216 | ||
217 | ($b = $a) =~ s/\w/$&/; | |
36a42ae7 KW |
218 | check_taint $b; # $b should be tainted. |
219 | check_taint_not $a; # $a should be not. | |
8ebc5c01 | 220 | |
221 | $_ = $a; # untaint $_ | |
222 | ||
223 | s/(\w)/\l$1/; # this must taint | |
36a42ae7 KW |
224 | check_taint $_; |
225 | check_taint $&; | |
226 | check_taint $`; | |
227 | check_taint $'; | |
228 | check_taint $+; | |
229 | check_taint $1; | |
230 | check_taint_not $2; | |
8ebc5c01 | 231 | |
232 | $_ = $a; # untaint $_ | |
233 | ||
234 | s/(\w)/\L$1/; # this must taint | |
36a42ae7 KW |
235 | check_taint $_; |
236 | check_taint $&; | |
237 | check_taint $`; | |
238 | check_taint $'; | |
239 | check_taint $+; | |
240 | check_taint $1; | |
241 | check_taint_not $2; | |
8ebc5c01 | 242 | |
243 | $_ = $a; # untaint $_ | |
244 | ||
245 | s/(\w)/\u$1/; # this must taint | |
36a42ae7 KW |
246 | check_taint $_; |
247 | check_taint $&; | |
248 | check_taint $`; | |
249 | check_taint $'; | |
250 | check_taint $+; | |
251 | check_taint $1; | |
252 | check_taint_not $2; | |
8ebc5c01 | 253 | |
254 | $_ = $a; # untaint $_ | |
255 | ||
256 | s/(\w)/\U$1/; # this must taint | |
36a42ae7 KW |
257 | check_taint $_; |
258 | check_taint $&; | |
259 | check_taint $`; | |
260 | check_taint $'; | |
261 | check_taint $+; | |
262 | check_taint $1; | |
263 | check_taint_not $2; | |
8ebc5c01 | 264 | |
265 | # After all this tainting $a should be cool. | |
266 | ||
36a42ae7 | 267 | check_taint_not $a; |
8ebc5c01 | 268 | |
66cbab2c KW |
269 | { # This is just the previous tests copied here with a different |
270 | # compile-time pragma. | |
271 | ||
272 | use locale ':not_characters'; # engage restricted locale with different | |
273 | # tainting rules | |
274 | ||
275 | check_taint_not $a; | |
276 | ||
277 | check_taint_not uc($a); | |
278 | check_taint_not "\U$a"; | |
279 | check_taint_not ucfirst($a); | |
280 | check_taint_not "\u$a"; | |
281 | check_taint_not lc($a); | |
26c1569f | 282 | check_taint_not fc($a); |
66cbab2c | 283 | check_taint_not "\L$a"; |
26c1569f | 284 | check_taint_not "\F$a"; |
66cbab2c KW |
285 | check_taint_not lcfirst($a); |
286 | check_taint_not "\l$a"; | |
287 | ||
288 | check_taint_not sprintf('%e', 123.456); | |
289 | check_taint_not sprintf('%f', 123.456); | |
290 | check_taint_not sprintf('%g', 123.456); | |
291 | check_taint_not sprintf('%d', 123.456); | |
292 | check_taint_not sprintf('%x', 123.456); | |
293 | ||
294 | $_ = $a; # untaint $_ | |
295 | ||
296 | $_ = uc($a); # taint $_ | |
297 | ||
298 | check_taint_not $_; | |
299 | ||
300 | /(\w)/; # taint $&, $`, $', $+, $1. | |
301 | check_taint_not $&; | |
302 | check_taint_not $`; | |
303 | check_taint_not $'; | |
304 | check_taint_not $+; | |
305 | check_taint_not $1; | |
306 | check_taint_not $2; | |
307 | ||
308 | /(.)/; # untaint $&, $`, $', $+, $1. | |
309 | check_taint_not $&; | |
310 | check_taint_not $`; | |
311 | check_taint_not $'; | |
312 | check_taint_not $+; | |
313 | check_taint_not $1; | |
314 | check_taint_not $2; | |
315 | ||
316 | /(\W)/; # taint $&, $`, $', $+, $1. | |
317 | check_taint_not $&; | |
318 | check_taint_not $`; | |
319 | check_taint_not $'; | |
320 | check_taint_not $+; | |
321 | check_taint_not $1; | |
322 | check_taint_not $2; | |
323 | ||
324 | /(\s)/; # taint $&, $`, $', $+, $1. | |
325 | check_taint_not $&; | |
326 | check_taint_not $`; | |
327 | check_taint_not $'; | |
328 | check_taint_not $+; | |
329 | check_taint_not $1; | |
330 | check_taint_not $2; | |
331 | ||
332 | /(\S)/; # taint $&, $`, $', $+, $1. | |
333 | check_taint_not $&; | |
334 | check_taint_not $`; | |
335 | check_taint_not $'; | |
336 | check_taint_not $+; | |
337 | check_taint_not $1; | |
338 | check_taint_not $2; | |
339 | ||
340 | $_ = $a; # untaint $_ | |
341 | ||
342 | check_taint_not $_; | |
343 | ||
344 | /(b)/; # this must not taint | |
345 | check_taint_not $&; | |
346 | check_taint_not $`; | |
347 | check_taint_not $'; | |
348 | check_taint_not $+; | |
349 | check_taint_not $1; | |
350 | check_taint_not $2; | |
351 | ||
352 | $_ = $a; # untaint $_ | |
353 | ||
354 | check_taint_not $_; | |
355 | ||
356 | $b = uc($a); # taint $b | |
357 | s/(.+)/$b/; # this must taint only the $_ | |
358 | ||
359 | check_taint_not $_; | |
360 | check_taint_not $&; | |
361 | check_taint_not $`; | |
362 | check_taint_not $'; | |
363 | check_taint_not $+; | |
364 | check_taint_not $1; | |
365 | check_taint_not $2; | |
366 | ||
367 | $_ = $a; # untaint $_ | |
368 | ||
369 | s/(.+)/b/; # this must not taint | |
370 | check_taint_not $_; | |
371 | check_taint_not $&; | |
372 | check_taint_not $`; | |
373 | check_taint_not $'; | |
374 | check_taint_not $+; | |
375 | check_taint_not $1; | |
376 | check_taint_not $2; | |
377 | ||
378 | $b = $a; # untaint $b | |
379 | ||
380 | ($b = $a) =~ s/\w/$&/; | |
381 | check_taint_not $b; # $b should be tainted. | |
382 | check_taint_not $a; # $a should be not. | |
383 | ||
384 | $_ = $a; # untaint $_ | |
385 | ||
386 | s/(\w)/\l$1/; # this must taint | |
387 | check_taint_not $_; | |
388 | check_taint_not $&; | |
389 | check_taint_not $`; | |
390 | check_taint_not $'; | |
391 | check_taint_not $+; | |
392 | check_taint_not $1; | |
393 | check_taint_not $2; | |
394 | ||
395 | $_ = $a; # untaint $_ | |
396 | ||
397 | s/(\w)/\L$1/; # this must taint | |
398 | check_taint_not $_; | |
399 | check_taint_not $&; | |
400 | check_taint_not $`; | |
401 | check_taint_not $'; | |
402 | check_taint_not $+; | |
403 | check_taint_not $1; | |
404 | check_taint_not $2; | |
405 | ||
406 | $_ = $a; # untaint $_ | |
407 | ||
408 | s/(\w)/\u$1/; # this must taint | |
409 | check_taint_not $_; | |
410 | check_taint_not $&; | |
411 | check_taint_not $`; | |
412 | check_taint_not $'; | |
413 | check_taint_not $+; | |
414 | check_taint_not $1; | |
415 | check_taint_not $2; | |
416 | ||
417 | $_ = $a; # untaint $_ | |
418 | ||
419 | s/(\w)/\U$1/; # this must taint | |
420 | check_taint_not $_; | |
421 | check_taint_not $&; | |
422 | check_taint_not $`; | |
423 | check_taint_not $'; | |
424 | check_taint_not $+; | |
425 | check_taint_not $1; | |
426 | check_taint_not $2; | |
427 | ||
428 | # After all this tainting $a should be cool. | |
429 | ||
430 | check_taint_not $a; | |
431 | } | |
432 | ||
433 | # Here are in scope of 'use locale' | |
434 | ||
8ebc5c01 | 435 | # I think we've seen quite enough of taint. |
436 | # Let us do some *real* locale work now, | |
284102e8 | 437 | # unless setlocale() is missing (i.e. minitest). |
8ebc5c01 | 438 | |
fdf053ee KW |
439 | unless ($have_setlocale) { |
440 | print "1..$test_num\n"; | |
441 | exit; | |
442 | } | |
8ebc5c01 | 443 | |
6cf0b567 | 444 | # The test number before our first setlocale() |
66330f13 | 445 | my $final_without_setlocale = $test_num; |
6cf0b567 | 446 | |
284102e8 JH |
447 | # Find locales. |
448 | ||
6be75cd7 JH |
449 | debug "# Scanning for locales...\n"; |
450 | ||
451 | # Note that it's okay that some languages have their native names | |
452 | # capitalized here even though that's not "right". They are lowercased | |
453 | # anyway later during the scanning process (and besides, some clueless | |
98dc9551 | 454 | # vendor might have them capitalized erroneously anyway). |
6be75cd7 | 455 | |
284102e8 | 456 | my $locales = <<EOF; |
6be75cd7 | 457 | Afrikaans:af:za:1 15 |
284102e8 | 458 | Arabic:ar:dz eg sa:6 arabic8 |
6be75cd7 JH |
459 | Brezhoneg Breton:br:fr:1 15 |
460 | Bulgarski Bulgarian:bg:bg:5 | |
dd8482fc | 461 | Chinese:zh:cn tw:cn.EUC eucCN eucTW euc.CN euc.TW Big5 GB2312 tw.EUC |
6be75cd7 JH |
462 | Hrvatski Croatian:hr:hr:2 |
463 | Cymraeg Welsh:cy:cy:1 14 15 | |
284102e8 | 464 | Czech:cs:cz:2 |
df8a53a3 | 465 | Dansk Danish:da:dk:1 15 |
6be75cd7 | 466 | Nederlands Dutch:nl:be nl:1 15 |
dd8482fc | 467 | English American British:en:au ca gb ie nz us uk zw:1 15 cp850 |
6be75cd7 JH |
468 | Esperanto:eo:eo:3 |
469 | Eesti Estonian:et:ee:4 6 13 | |
470 | Suomi Finnish:fi:fi:1 15 | |
471 | Flamish::fl:1 15 | |
6be75cd7 JH |
472 | Deutsch German:de:at be ch de lu:1 15 |
473 | Euskaraz Basque:eu:es fr:1 15 | |
6be75cd7 JH |
474 | Galego Galician:gl:es:1 15 |
475 | Ellada Greek:el:gr:7 g8 | |
6be75cd7 JH |
476 | Frysk:fy:nl:1 15 |
477 | Greenlandic:kl:gl:4 6 | |
284102e8 JH |
478 | Hebrew:iw:il:8 hebrew8 |
479 | Hungarian:hu:hu:2 | |
df8a53a3 | 480 | Indonesian:id:id:1 15 |
6be75cd7 JH |
481 | Gaeilge Irish:ga:IE:1 14 15 |
482 | Italiano Italian:it:ch it:1 15 | |
483 | Nihongo Japanese:ja:jp:euc eucJP jp.EUC sjis | |
284102e8 | 484 | Korean:ko:kr: |
6be75cd7 JH |
485 | Latine Latin:la:va:1 15 |
486 | Latvian:lv:lv:4 6 13 | |
487 | Lithuanian:lt:lt:4 6 13 | |
488 | Macedonian:mk:mk:1 15 | |
489 | Maltese:mt:mt:3 | |
dd8482fc | 490 | Moldovan:mo:mo:2 |
df8a53a3 | 491 | Norsk Norwegian:no no\@nynorsk nb nn:no:1 15 |
6be75cd7 JH |
492 | Occitan:oc:es:1 15 |
493 | Polski Polish:pl:pl:2 | |
284102e8 | 494 | Rumanian:ro:ro:2 |
a528dad0 | 495 | Russki Russian:ru:ru su ua:5 koi8 koi8r KOI8-R koi8u cp1251 cp866 |
6be75cd7 | 496 | Serbski Serbian:sr:yu:5 |
284102e8 | 497 | Slovak:sk:sk:2 |
6be75cd7 | 498 | Slovene Slovenian:sl:si:2 |
d43ce814 JH |
499 | Sqhip Albanian:sq:sq:1 15 |
500 | Svenska Swedish:sv:fi se:1 15 | |
6be75cd7 | 501 | Thai:th:th:11 tis620 |
284102e8 | 502 | Turkish:tr:tr:9 turkish8 |
dd8482fc | 503 | Yiddish:yi::1 15 |
284102e8 JH |
504 | EOF |
505 | ||
ee50adbe | 506 | if ($^O eq 'os390') { |
dd8482fc | 507 | # These cause heartburn. Broken locales? |
ee50adbe PP |
508 | $locales =~ s/Svenska Swedish:sv:fi se:1 15\n//; |
509 | $locales =~ s/Thai:th:th:11 tis620\n//; | |
510 | } | |
511 | ||
ef4a39e5 | 512 | sub in_utf8 () { $^H & 0x08 || (${^OPEN} || "") =~ /:utf8/ } |
f9cbebe1 JH |
513 | |
514 | if (in_utf8) { | |
8a6cb2cb | 515 | require "lib/locale/utf8"; |
f9cbebe1 | 516 | } else { |
8a6cb2cb | 517 | require "lib/locale/latin1"; |
f9cbebe1 JH |
518 | } |
519 | ||
284102e8 JH |
520 | my @Locale; |
521 | my $Locale; | |
522 | my @Alnum_; | |
523 | ||
284102e8 JH |
524 | sub trylocale { |
525 | my $locale = shift; | |
0b9f254b | 526 | return if grep { $locale eq $_ } @Locale; |
e439cacb KW |
527 | return unless setlocale(LC_ALL, $locale); |
528 | my $badutf8; | |
529 | { | |
530 | local $SIG{__WARN__} = sub { | |
531 | $badutf8 = $_[0] =~ /Malformed UTF-8/; | |
532 | }; | |
533 | $Locale =~ /UTF-?8/i; | |
284102e8 | 534 | } |
e439cacb KW |
535 | |
536 | if ($badutf8) { | |
537 | ok(0, "Locale name contains malformed utf8"); | |
538 | return; | |
539 | } | |
540 | push @Locale, $locale; | |
284102e8 | 541 | } |
8ebc5c01 | 542 | |
284102e8 JH |
543 | sub decode_encodings { |
544 | my @enc; | |
8ebc5c01 | 545 | |
284102e8 JH |
546 | foreach (split(/ /, shift)) { |
547 | if (/^(\d+)$/) { | |
548 | push @enc, "ISO8859-$1"; | |
549 | push @enc, "iso8859$1"; # HP | |
550 | if ($1 eq '1') { | |
551 | push @enc, "roman8"; # HP | |
552 | } | |
553 | } else { | |
554 | push @enc, $_; | |
dd8482fc | 555 | push @enc, "$_.UTF-8"; |
8ebc5c01 | 556 | } |
557 | } | |
ee50adbe PP |
558 | if ($^O eq 'os390') { |
559 | push @enc, qw(IBM-037 IBM-819 IBM-1047); | |
560 | } | |
8ebc5c01 | 561 | |
284102e8 | 562 | return @enc; |
8ebc5c01 | 563 | } |
564 | ||
284102e8 JH |
565 | trylocale("C"); |
566 | trylocale("POSIX"); | |
567 | foreach (0..15) { | |
568 | trylocale("ISO8859-$_"); | |
284102e8 | 569 | trylocale("iso8859$_"); |
097ee67d JH |
570 | trylocale("iso8859-$_"); |
571 | trylocale("iso_8859_$_"); | |
572 | trylocale("isolatin$_"); | |
573 | trylocale("isolatin-$_"); | |
574 | trylocale("iso_latin_$_"); | |
8ebc5c01 | 575 | } |
576 | ||
645e49ed JH |
577 | # Sanitize the environment so that we can run the external 'locale' |
578 | # program without the taint mode getting grumpy. | |
cce5967e JH |
579 | |
580 | # $ENV{PATH} is special in VMS. | |
581 | delete $ENV{PATH} if $^O ne 'VMS' or $Config{d_setenv}; | |
582 | ||
583 | # Other subversive stuff. | |
584 | delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; | |
dd8482fc | 585 | |
21477fb4 | 586 | if (-x "/usr/bin/locale" && open(LOCALES, "/usr/bin/locale -a 2>/dev/null|")) { |
dd8482fc | 587 | while (<LOCALES>) { |
d281a6ac NC |
588 | # It seems that /usr/bin/locale steadfastly outputs 8 bit data, which |
589 | # ain't great when we're running this testPERL_UNICODE= so that utf8 | |
590 | # locales will cause all IO hadles to default to (assume) utf8 | |
591 | next unless utf8::valid($_); | |
dd8482fc JH |
592 | chomp; |
593 | trylocale($_); | |
284102e8 | 594 | } |
dd8482fc | 595 | close(LOCALES); |
a6259068 | 596 | } elsif ($^O eq 'VMS' && defined($ENV{'SYS$I18N_LOCALE'}) && -d 'SYS$I18N_LOCALE') { |
71e5cbb3 | 597 | # The SYS$I18N_LOCALE logical name search list was not present on |
a6259068 PP |
598 | # VAX VMS V5.5-12, but was on AXP && VAX VMS V6.2 as well as later versions. |
599 | opendir(LOCALES, "SYS\$I18N_LOCALE:"); | |
600 | while ($_ = readdir(LOCALES)) { | |
601 | chomp; | |
602 | trylocale($_); | |
603 | } | |
604 | close(LOCALES); | |
87e33296 SP |
605 | } elsif ($^O eq 'openbsd' && -e '/usr/share/locale') { |
606 | ||
607 | # OpenBSD doesn't have a locale executable, so reading /usr/share/locale | |
608 | # is much easier and faster than the last resort method. | |
609 | ||
610 | opendir(LOCALES, '/usr/share/locale'); | |
611 | while ($_ = readdir(LOCALES)) { | |
612 | chomp; | |
613 | trylocale($_); | |
614 | } | |
615 | close(LOCALES); | |
dd8482fc JH |
616 | } else { |
617 | ||
618 | # This is going to be slow. | |
619 | ||
620 | foreach my $locale (split(/\n/, $locales)) { | |
621 | my ($locale_name, $language_codes, $country_codes, $encodings) = | |
622 | split(/:/, $locale); | |
623 | my @enc = decode_encodings($encodings); | |
624 | foreach my $loc (split(/ /, $locale_name)) { | |
625 | trylocale($loc); | |
284102e8 | 626 | foreach my $enc (@enc) { |
dd8482fc | 627 | trylocale("$loc.$enc"); |
284102e8 | 628 | } |
dd8482fc | 629 | $loc = lc $loc; |
284102e8 | 630 | foreach my $enc (@enc) { |
dd8482fc JH |
631 | trylocale("$loc.$enc"); |
632 | } | |
633 | } | |
634 | foreach my $lang (split(/ /, $language_codes)) { | |
635 | trylocale($lang); | |
636 | foreach my $country (split(/ /, $country_codes)) { | |
637 | my $lc = "${lang}_${country}"; | |
638 | trylocale($lc); | |
639 | foreach my $enc (@enc) { | |
640 | trylocale("$lc.$enc"); | |
641 | } | |
642 | my $lC = "${lang}_\U${country}"; | |
643 | trylocale($lC); | |
644 | foreach my $enc (@enc) { | |
645 | trylocale("$lC.$enc"); | |
646 | } | |
284102e8 JH |
647 | } |
648 | } | |
649 | } | |
650 | } | |
4599a1de | 651 | |
d43ce814 JH |
652 | setlocale(LC_ALL, "C"); |
653 | ||
86f50d7d | 654 | if ($^O eq 'darwin') { |
4373e181 | 655 | # Darwin 8/Mac OS X 10.4 and 10.5 have bad Basque locales: perl bug #35895, |
86f50d7d | 656 | # Apple bug ID# 4139653. It also has a problem in Byelorussian. |
4373e181 RGS |
657 | (my $v) = $Config{osvers} =~ /^(\d+)/; |
658 | if ($v >= 8 and $v < 10) { | |
86f50d7d | 659 | debug "# Skipping eu_ES, be_BY locales -- buggy in Darwin\n"; |
a5ec937f | 660 | @Locale = grep ! m/^(eu_ES(?:\..*)?|be_BY\.CP1131)$/, @Locale; |
dfa5c78f | 661 | } elsif ($v < 12) { |
a44d0896 NC |
662 | debug "# Skipping be_BY locales -- buggy in Darwin\n"; |
663 | @Locale = grep ! m/^be_BY\.CP1131$/, @Locale; | |
a5ec937f | 664 | } |
86f50d7d DD |
665 | } |
666 | ||
4599a1de JH |
667 | @Locale = sort @Locale; |
668 | ||
887ef7ed PP |
669 | debug "# Locales =\n"; |
670 | for ( @Locale ) { | |
671 | debug "# $_\n"; | |
672 | } | |
8ebc5c01 | 673 | |
284102e8 | 674 | my %Problem; |
2a680da6 JH |
675 | my %Okay; |
676 | my %Testing; | |
9445c837 | 677 | my @Neoalpha; # Alnums that aren't in the C locale. |
c08acc4c | 678 | my %test_names; |
284102e8 | 679 | |
2a680da6 | 680 | sub tryneoalpha { |
15bbd6a2 KW |
681 | my ($Locale, $i, $test, $message) = @_; |
682 | $message //= ""; | |
683 | $message = " ($message)" if $message; | |
2a680da6 JH |
684 | unless ($test) { |
685 | $Problem{$i}{$Locale} = 1; | |
15bbd6a2 | 686 | debug "# failed $i with locale '$Locale'$message\n"; |
2a680da6 JH |
687 | } else { |
688 | push @{$Okay{$i}}, $Locale; | |
689 | } | |
690 | } | |
691 | ||
c4093d7d KW |
692 | my $first_locales_test_number = $final_without_setlocale + 1; |
693 | my $locales_test_number; | |
694 | my $not_necessarily_a_problem_test_number; | |
695 | my %setlocale_failed; # List of locales that setlocale() didn't work on | |
696 | ||
284102e8 | 697 | foreach $Locale (@Locale) { |
c4093d7d | 698 | $locales_test_number = $first_locales_test_number - 1; |
284102e8 | 699 | debug "# Locale = $Locale\n"; |
284102e8 JH |
700 | |
701 | unless (setlocale(LC_ALL, $Locale)) { | |
c4093d7d | 702 | $setlocale_failed{$Locale} = $Locale; |
284102e8 | 703 | next; |
8ebc5c01 | 704 | } |
8ebc5c01 | 705 | |
66cbab2c KW |
706 | # We test UTF-8 locales only under ':not_characters'; otherwise they have |
707 | # documented deficiencies. Non- UTF-8 locales are tested only under plain | |
708 | # 'use locale', as otherwise we would have to convert everything in them | |
709 | # to Unicode. | |
710 | my $is_utf8_locale = $Locale =~ /UTF-?8/i; | |
711 | ||
712 | my %UPPER = (); | |
713 | my %lower = (); | |
714 | my %BoThCaSe = (); | |
715 | ||
716 | if (! $is_utf8_locale) { | |
717 | use locale; | |
71e5cbb3 | 718 | @Alnum_ = sort grep /\w/, map { chr } 0..255; |
f07538a5 | 719 | |
71e5cbb3 | 720 | debug "# w = ", join("",@Alnum_), "\n"; |
e5272a46 | 721 | |
71e5cbb3 KW |
722 | # Sieve the uppercase and the lowercase. |
723 | ||
724 | for (@Alnum_) { | |
725 | if (/[^\d_]/) { # skip digits and the _ | |
726 | if (uc($_) eq $_) { | |
727 | $UPPER{$_} = $_; | |
728 | } | |
729 | if (lc($_) eq $_) { | |
730 | $lower{$_} = $_; | |
731 | } | |
732 | } | |
733 | } | |
66cbab2c KW |
734 | } |
735 | else { | |
736 | use locale ':not_characters'; | |
737 | @Alnum_ = sort grep /\w/, map { chr } 0..255; | |
738 | debug "# w = ", join("",@Alnum_), "\n"; | |
739 | for (@Alnum_) { | |
740 | if (/[^\d_]/) { # skip digits and the _ | |
741 | if (uc($_) eq $_) { | |
742 | $UPPER{$_} = $_; | |
743 | } | |
744 | if (lc($_) eq $_) { | |
745 | $lower{$_} = $_; | |
746 | } | |
747 | } | |
748 | } | |
749 | } | |
284102e8 | 750 | foreach (keys %UPPER) { |
097ee67d | 751 | $BoThCaSe{$_}++ if exists $lower{$_}; |
284102e8 JH |
752 | } |
753 | foreach (keys %lower) { | |
097ee67d | 754 | $BoThCaSe{$_}++ if exists $UPPER{$_}; |
284102e8 | 755 | } |
097ee67d | 756 | foreach (keys %BoThCaSe) { |
284102e8 JH |
757 | delete $UPPER{$_}; |
758 | delete $lower{$_}; | |
759 | } | |
760 | ||
db4b7445 A |
761 | debug "# UPPER = ", join("", sort keys %UPPER ), "\n"; |
762 | debug "# lower = ", join("", sort keys %lower ), "\n"; | |
763 | debug "# BoThCaSe = ", join("", sort keys %BoThCaSe), "\n"; | |
284102e8 | 764 | |
baa71cfd | 765 | my @failures; |
3da38613 | 766 | my @fold_failures; |
baa71cfd KW |
767 | foreach my $x (sort keys %UPPER) { |
768 | my $ok; | |
3da38613 | 769 | my $fold_ok; |
baa71cfd KW |
770 | if ($is_utf8_locale) { |
771 | use locale ':not_characters'; | |
772 | $ok = $x =~ /[[:upper:]]/; | |
3da38613 | 773 | $fold_ok = $x =~ /[[:lower:]]/i; |
baa71cfd KW |
774 | } |
775 | else { | |
776 | use locale; | |
777 | $ok = $x =~ /[[:upper:]]/; | |
3da38613 | 778 | $fold_ok = $x =~ /[[:lower:]]/i; |
baa71cfd KW |
779 | } |
780 | push @failures, $x unless $ok; | |
3da38613 | 781 | push @fold_failures, $x unless $fold_ok; |
baa71cfd KW |
782 | } |
783 | my $message = ""; | |
784 | $locales_test_number++; | |
785 | $test_names{$locales_test_number} = 'Verify that /[[:upper:]]/ matches sieved uppercase characters.'; | |
786 | $message = 'Failed for ' . join ", ", @failures if @failures; | |
787 | tryneoalpha($Locale, $locales_test_number, scalar @failures == 0, $message); | |
3da38613 KW |
788 | $message = ""; |
789 | $locales_test_number++; | |
790 | $test_names{$locales_test_number} = 'TODO Verify that /[[:lower:]]/i matches sieved uppercase characters.'; | |
791 | $message = 'Failed for ' . join ", ", @fold_failures if @fold_failures; | |
792 | tryneoalpha($Locale, $locales_test_number, scalar @fold_failures == 0, $message); | |
baa71cfd KW |
793 | |
794 | $message = ""; | |
795 | undef @failures; | |
3da38613 | 796 | undef @fold_failures; |
baa71cfd KW |
797 | |
798 | foreach my $x (sort keys %lower) { | |
799 | my $ok; | |
3da38613 | 800 | my $fold_ok; |
baa71cfd KW |
801 | if ($is_utf8_locale) { |
802 | use locale ':not_characters'; | |
803 | $ok = $x =~ /[[:lower:]]/; | |
3da38613 | 804 | $fold_ok = $x =~ /[[:upper:]]/i; |
baa71cfd KW |
805 | } |
806 | else { | |
807 | use locale; | |
808 | $ok = $x =~ /[[:lower:]]/; | |
3da38613 | 809 | $fold_ok = $x =~ /[[:upper:]]/i; |
baa71cfd KW |
810 | } |
811 | push @failures, $x unless $ok; | |
3da38613 | 812 | push @fold_failures, $x unless $fold_ok; |
baa71cfd KW |
813 | } |
814 | ||
815 | $locales_test_number++; | |
816 | $test_names{$locales_test_number} = 'Verify that /[[:lower:]]/ matches sieved lowercase characters.'; | |
817 | $message = 'Failed for ' . join ", ", @failures if @failures; | |
818 | tryneoalpha($Locale, $locales_test_number, scalar @failures == 0, $message); | |
819 | $message = ""; | |
3da38613 KW |
820 | $locales_test_number++; |
821 | $test_names{$locales_test_number} = 'TODO Verify that /[[:upper:]]/i matches sieved lowercase characters.'; | |
822 | $message = 'TODO Failed for ' . join ", ", @fold_failures if @fold_failures; | |
823 | tryneoalpha($Locale, $locales_test_number, scalar @fold_failures == 0, $message); | |
baa71cfd | 824 | |
9445c837 KW |
825 | { # Find the alphabetic characters that are not considered alphabetics |
826 | # in the default (C) locale. | |
8ebc5c01 | 827 | |
284102e8 | 828 | no locale; |
71e5cbb3 | 829 | |
284102e8 JH |
830 | @Neoalpha = (); |
831 | for (keys %UPPER, keys %lower) { | |
832 | push(@Neoalpha, $_) if (/\W/); | |
833 | } | |
8ebc5c01 | 834 | } |
8ebc5c01 | 835 | |
284102e8 | 836 | @Neoalpha = sort @Neoalpha; |
8ebc5c01 | 837 | |
db4b7445 | 838 | debug "# Neoalpha = ", join("",@Neoalpha), "\n"; |
8ebc5c01 | 839 | |
871d8451 KW |
840 | my $first_Neoalpha_test_number = $locales_test_number + 1; |
841 | my $final_Neoalpha_test_number = $first_Neoalpha_test_number + 3; | |
284102e8 JH |
842 | if (@Neoalpha == 0) { |
843 | # If we have no Neoalphas the remaining tests are no-ops. | |
871d8451 | 844 | debug "# no Neoalpha, skipping tests $first_Neoalpha_test_number..$final_Neoalpha_test_number for locale '$Locale'\n"; |
c4093d7d | 845 | foreach ($locales_test_number+1..$final_Neoalpha_test_number) { |
a88c3d7c | 846 | push @{$Okay{$_}}, $Locale; |
c4093d7d | 847 | $locales_test_number++; |
a88c3d7c | 848 | } |
6be75cd7 | 849 | } else { |
8ebc5c01 | 850 | |
6be75cd7 | 851 | # Test \w. |
71e5cbb3 | 852 | |
ef4a39e5 | 853 | my $word = join('', @Neoalpha); |
8ebc5c01 | 854 | |
c4093d7d | 855 | ++$locales_test_number; |
c08acc4c | 856 | $test_names{$locales_test_number} = 'Verify that alnums outside the C locale match \w'; |
66cbab2c KW |
857 | my $ok; |
858 | if ($is_utf8_locale) { | |
859 | use locale ':not_characters'; | |
860 | $ok = $word =~ /^(\w+)$/; | |
861 | } | |
862 | else { | |
863 | # Already in 'use locale'; this tests that exiting scopes works | |
864 | $ok = $word =~ /^(\w+)$/; | |
865 | } | |
866 | tryneoalpha($Locale, $locales_test_number, $ok); | |
ef4a39e5 | 867 | |
2a680da6 | 868 | # Cross-check the whole 8-bit character set. |
8ebc5c01 | 869 | |
c4093d7d | 870 | ++$locales_test_number; |
c08acc4c | 871 | $test_names{$locales_test_number} = 'Verify that \w and \W are mutually exclusive, as are \d, \D; \s, \S'; |
6be75cd7 | 872 | for (map { chr } 0..255) { |
66cbab2c KW |
873 | if ($is_utf8_locale) { |
874 | use locale ':not_characters'; | |
875 | $ok = (/\w/ xor /\W/) || | |
876 | (/\d/ xor /\D/) || | |
877 | (/\s/ xor /\S/); | |
878 | } | |
879 | else { | |
880 | $ok = (/\w/ xor /\W/) || | |
2a680da6 | 881 | (/\d/ xor /\D/) || |
66cbab2c KW |
882 | (/\s/ xor /\S/); |
883 | } | |
884 | tryneoalpha($Locale, $locales_test_number, $ok); | |
284102e8 | 885 | } |
8ebc5c01 | 886 | |
6be75cd7 | 887 | # Test for read-only scalars' locale vs non-locale comparisons. |
284102e8 | 888 | |
284102e8 | 889 | { |
6be75cd7 JH |
890 | no locale; |
891 | $a = "qwerty"; | |
66cbab2c KW |
892 | if ($is_utf8_locale) { |
893 | use locale ':not_characters'; | |
894 | $ok = ($a cmp "qwerty") == 0; | |
895 | } | |
896 | else { | |
897 | use locale; | |
898 | $ok = ($a cmp "qwerty") == 0; | |
899 | } | |
900 | tryneoalpha($Locale, ++$locales_test_number, $ok); | |
901 | $test_names{$locales_test_number} = 'Verify that cmp works with a read-only scalar; no- vs locale'; | |
8ebc5c01 | 902 | } |
8ebc5c01 | 903 | |
6be75cd7 JH |
904 | { |
905 | my ($from, $to, $lesser, $greater, | |
906 | @test, %test, $test, $yes, $no, $sign); | |
907 | ||
c4093d7d | 908 | ++$locales_test_number; |
c08acc4c | 909 | $test_names{$locales_test_number} = 'Verify that "le", "ne", etc work'; |
c4093d7d | 910 | $not_necessarily_a_problem_test_number = $locales_test_number; |
6be75cd7 JH |
911 | for (0..9) { |
912 | # Select a slice. | |
913 | $from = int(($_*@Alnum_)/10); | |
914 | $to = $from + int(@Alnum_/10); | |
915 | $to = $#Alnum_ if ($to > $#Alnum_); | |
916 | $lesser = join('', @Alnum_[$from..$to]); | |
917 | # Select a slice one character on. | |
918 | $from++; $to++; | |
919 | $to = $#Alnum_ if ($to > $#Alnum_); | |
920 | $greater = join('', @Alnum_[$from..$to]); | |
66cbab2c KW |
921 | if ($is_utf8_locale) { |
922 | use locale ':not_characters'; | |
923 | ($yes, $no, $sign) = ($lesser lt $greater | |
924 | ? (" ", "not ", 1) | |
925 | : ("not ", " ", -1)); | |
926 | } | |
927 | else { | |
928 | use locale; | |
71e5cbb3 | 929 | ($yes, $no, $sign) = ($lesser lt $greater |
6be75cd7 JH |
930 | ? (" ", "not ", 1) |
931 | : ("not ", " ", -1)); | |
66cbab2c | 932 | } |
6be75cd7 JH |
933 | # all these tests should FAIL (return 0). |
934 | # Exact lt or gt cannot be tested because | |
935 | # in some locales, say, eacute and E may test equal. | |
71e5cbb3 | 936 | @test = |
6be75cd7 JH |
937 | ( |
938 | $no.' ($lesser le $greater)', # 1 | |
939 | 'not ($lesser ne $greater)', # 2 | |
940 | ' ($lesser eq $greater)', # 3 | |
941 | $yes.' ($lesser ge $greater)', # 4 | |
942 | $yes.' ($lesser ge $greater)', # 5 | |
943 | $yes.' ($greater le $lesser )', # 7 | |
944 | 'not ($greater ne $lesser )', # 8 | |
945 | ' ($greater eq $lesser )', # 9 | |
946 | $no.' ($greater ge $lesser )', # 10 | |
0e053d1e | 947 | 'not (($lesser cmp $greater) == -($sign))' # 11 |
6be75cd7 JH |
948 | ); |
949 | @test{@test} = 0 x @test; | |
950 | $test = 0; | |
284102e8 | 951 | for my $ti (@test) { |
66cbab2c KW |
952 | if ($is_utf8_locale) { |
953 | use locale ':not_characters'; | |
954 | $test{$ti} = eval $ti; | |
955 | } | |
956 | else { | |
957 | # Already in 'use locale'; | |
71e5cbb3 | 958 | $test{$ti} = eval $ti; |
66cbab2c | 959 | } |
6be75cd7 | 960 | $test ||= $test{$ti} |
284102e8 | 961 | } |
c4093d7d | 962 | tryneoalpha($Locale, $locales_test_number, $test == 0); |
6be75cd7 | 963 | if ($test) { |
6be75cd7 JH |
964 | debug "# lesser = '$lesser'\n"; |
965 | debug "# greater = '$greater'\n"; | |
966 | debug "# lesser cmp greater = ", | |
967 | $lesser cmp $greater, "\n"; | |
968 | debug "# greater cmp lesser = ", | |
969 | $greater cmp $lesser, "\n"; | |
970 | debug "# (greater) from = $from, to = $to\n"; | |
971 | for my $ti (@test) { | |
972 | debugf("# %-40s %-4s", $ti, | |
973 | $test{$ti} ? 'FAIL' : 'ok'); | |
974 | if ($ti =~ /\(\.*(\$.+ +cmp +\$[^\)]+)\.*\)/) { | |
975 | debugf("(%s == %4d)", $1, eval $1); | |
976 | } | |
977 | debug "\n#"; | |
978 | } | |
284102e8 | 979 | |
6be75cd7 JH |
980 | last; |
981 | } | |
284102e8 | 982 | } |
8ebc5c01 | 983 | } |
984 | } | |
6be75cd7 | 985 | |
c4093d7d KW |
986 | if ($locales_test_number != $final_Neoalpha_test_number) { |
987 | die("The delta for \$final_Neoalpha needs to be updated from " | |
988 | . ($final_Neoalpha_test_number - $first_Neoalpha_test_number) | |
989 | . " to " | |
990 | . ($locales_test_number - $first_Neoalpha_test_number) | |
991 | ); | |
992 | } | |
993 | ||
66cbab2c KW |
994 | my $ok1; |
995 | my $ok2; | |
996 | my $ok3; | |
997 | my $ok4; | |
998 | my $ok5; | |
999 | my $ok6; | |
1000 | my $ok7; | |
1001 | my $ok8; | |
1002 | my $ok9; | |
1003 | my $ok10; | |
1004 | my $ok11; | |
1005 | my $ok12; | |
1006 | my $ok13; | |
1007 | ||
1008 | my $c; | |
1009 | my $d; | |
1010 | my $e; | |
1011 | my $f; | |
1012 | my $g; | |
1013 | ||
1014 | if (! $is_utf8_locale) { | |
71e5cbb3 | 1015 | use locale; |
6be75cd7 | 1016 | |
71e5cbb3 | 1017 | my ($x, $y) = (1.23, 1.23); |
6be75cd7 | 1018 | |
71e5cbb3 KW |
1019 | $a = "$x"; |
1020 | printf ''; # printf used to reset locale to "C" | |
1021 | $b = "$y"; | |
1022 | $ok1 = $a eq $b; | |
6be75cd7 | 1023 | |
71e5cbb3 KW |
1024 | $c = "$x"; |
1025 | my $z = sprintf ''; # sprintf used to reset locale to "C" | |
1026 | $d = "$y"; | |
1027 | $ok2 = $c eq $d; | |
1028 | { | |
66cbab2c | 1029 | |
71e5cbb3 KW |
1030 | use warnings; |
1031 | my $w = 0; | |
1032 | local $SIG{__WARN__} = | |
1033 | sub { | |
1034 | print "# @_\n"; | |
1035 | $w++; | |
1036 | }; | |
6be75cd7 | 1037 | |
71e5cbb3 KW |
1038 | # The == (among other ops) used to warn for locales |
1039 | # that had something else than "." as the radix character. | |
6be75cd7 | 1040 | |
71e5cbb3 KW |
1041 | $ok3 = $c == 1.23; |
1042 | $ok4 = $c == $x; | |
1043 | $ok5 = $c == $d; | |
1044 | { | |
1045 | no locale; | |
66cbab2c | 1046 | |
71e5cbb3 KW |
1047 | # The earlier test was $e = "$x". But this fails [perl |
1048 | # #108378], and the "no locale" was commented out. But doing | |
1049 | # that made all the tests in the block after this one | |
1050 | # meaningless, as originally it was testing the nesting of a | |
1051 | # "no locale" scope, and how it recovers after that scope is | |
1052 | # done. So I (khw) filed a bug report and changed this so it | |
1053 | # wouldn't fail. It seemed too much work to add TODOs | |
1054 | # instead. Should this be fixed, the following test names | |
1055 | # would need to be revised; they mostly don't really test | |
1056 | # anything currently. | |
1057 | $e = $x; | |
1058 | ||
1059 | $ok6 = $e == 1.23; | |
1060 | $ok7 = $e == $x; | |
1061 | $ok8 = $e == $c; | |
1062 | } | |
66cbab2c | 1063 | |
71e5cbb3 KW |
1064 | $f = "1.23"; |
1065 | $g = 2.34; | |
66cbab2c | 1066 | |
71e5cbb3 KW |
1067 | $ok9 = $f == 1.23; |
1068 | $ok10 = $f == $x; | |
1069 | $ok11 = $f == $c; | |
1070 | $ok12 = abs(($f + $g) - 3.57) < 0.01; | |
1071 | $ok13 = $w == 0; | |
1072 | } | |
66cbab2c KW |
1073 | } |
1074 | else { | |
1075 | use locale ':not_characters'; | |
1076 | ||
1077 | my ($x, $y) = (1.23, 1.23); | |
1078 | $a = "$x"; | |
1079 | printf ''; # printf used to reset locale to "C" | |
1080 | $b = "$y"; | |
1081 | $ok1 = $a eq $b; | |
1082 | ||
1083 | $c = "$x"; | |
1084 | my $z = sprintf ''; # sprintf used to reset locale to "C" | |
1085 | $d = "$y"; | |
1086 | $ok2 = $c eq $d; | |
1087 | { | |
1088 | use warnings; | |
1089 | my $w = 0; | |
1090 | local $SIG{__WARN__} = | |
1091 | sub { | |
1092 | print "# @_\n"; | |
1093 | $w++; | |
1094 | }; | |
1095 | $ok3 = $c == 1.23; | |
1096 | $ok4 = $c == $x; | |
1097 | $ok5 = $c == $d; | |
1098 | { | |
1099 | no locale; | |
1100 | $e = $x; | |
1101 | ||
1102 | $ok6 = $e == 1.23; | |
1103 | $ok7 = $e == $x; | |
1104 | $ok8 = $e == $c; | |
1105 | } | |
1106 | ||
1107 | $f = "1.23"; | |
1108 | $g = 2.34; | |
1109 | ||
1110 | $ok9 = $f == 1.23; | |
1111 | $ok10 = $f == $x; | |
1112 | $ok11 = $f == $c; | |
1113 | $ok12 = abs(($f + $g) - 3.57) < 0.01; | |
1114 | $ok13 = $w == 0; | |
1115 | } | |
1116 | } | |
1117 | ||
1118 | tryneoalpha($Locale, ++$locales_test_number, $ok1); | |
1119 | $test_names{$locales_test_number} = 'Verify that an intervening printf doesn\'t change assignment results'; | |
1120 | my $first_a_test = $locales_test_number; | |
1121 | ||
1122 | debug "# $first_a_test..$locales_test_number: \$a = $a, \$b = $b, Locale = $Locale\n"; | |
1123 | ||
1124 | tryneoalpha($Locale, ++$locales_test_number, $ok2); | |
1125 | $test_names{$locales_test_number} = 'Verify that an intervening sprintf doesn\'t change assignment results'; | |
1126 | ||
1127 | my $first_c_test = $locales_test_number; | |
1128 | ||
71e5cbb3 KW |
1129 | tryneoalpha($Locale, ++$locales_test_number, $ok3); |
1130 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a constant'; | |
6be75cd7 | 1131 | |
71e5cbb3 KW |
1132 | tryneoalpha($Locale, ++$locales_test_number, $ok4); |
1133 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar'; | |
66cbab2c | 1134 | |
71e5cbb3 KW |
1135 | tryneoalpha($Locale, ++$locales_test_number, $ok5); |
1136 | $test_names{$locales_test_number} = 'Verify that a different locale radix works when doing "==" with a scalar and an intervening sprintf'; | |
66cbab2c | 1137 | |
71e5cbb3 | 1138 | debug "# $first_c_test..$locales_test_number: \$c = $c, \$d = $d, Locale = $Locale\n"; |
66cbab2c | 1139 | |
71e5cbb3 KW |
1140 | tryneoalpha($Locale, ++$locales_test_number, $ok6); |
1141 | $test_names{$locales_test_number} = 'Verify that can assign numerically under inner no-locale block'; | |
1142 | my $first_e_test = $locales_test_number; | |
6be75cd7 | 1143 | |
71e5cbb3 KW |
1144 | tryneoalpha($Locale, ++$locales_test_number, $ok7); |
1145 | $test_names{$locales_test_number} = 'Verify that "==" with a scalar still works in inner no locale'; | |
66cbab2c | 1146 | |
71e5cbb3 KW |
1147 | tryneoalpha($Locale, ++$locales_test_number, $ok8); |
1148 | $test_names{$locales_test_number} = 'Verify that "==" with a scalar and an intervening sprintf still works in inner no locale'; | |
c4093d7d | 1149 | |
71e5cbb3 | 1150 | debug "# $first_e_test..$locales_test_number: \$e = $e, no locale\n"; |
2a680da6 | 1151 | |
71e5cbb3 KW |
1152 | tryneoalpha($Locale, ++$locales_test_number, $ok9); |
1153 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a constant'; | |
1154 | my $first_f_test = $locales_test_number; | |
6be75cd7 | 1155 | |
71e5cbb3 KW |
1156 | tryneoalpha($Locale, ++$locales_test_number, $ok10); |
1157 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar'; | |
66cbab2c | 1158 | |
71e5cbb3 KW |
1159 | tryneoalpha($Locale, ++$locales_test_number, $ok11); |
1160 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix still works when doing "==" with a scalar and an intervening sprintf'; | |
906f284f | 1161 | |
71e5cbb3 KW |
1162 | tryneoalpha($Locale, ++$locales_test_number, $ok12); |
1163 | $test_names{$locales_test_number} = 'Verify that after a no-locale block, a different locale radix can participate in an addition and function call as numeric'; | |
c4093d7d | 1164 | |
71e5cbb3 KW |
1165 | tryneoalpha($Locale, ++$locales_test_number, $ok13); |
1166 | $test_names{$locales_test_number} = 'Verify that don\'t get warning under "==" even if radix is not a dot'; | |
c4093d7d | 1167 | |
71e5cbb3 | 1168 | debug "# $first_f_test..$locales_test_number: \$f = $f, \$g = $g, back to locale = $Locale\n"; |
906f284f | 1169 | |
26d80d95 LC |
1170 | # Does taking lc separately differ from taking |
1171 | # the lc "in-line"? (This was the bug 19990704.002, change #3568.) | |
1172 | # The bug was in the caching of the 'o'-magic. | |
66cbab2c | 1173 | if (! $is_utf8_locale) { |
2a680da6 | 1174 | use locale; |
6be75cd7 | 1175 | |
2a680da6 JH |
1176 | sub lcA { |
1177 | my $lc0 = lc $_[0]; | |
1178 | my $lc1 = lc $_[1]; | |
1179 | return $lc0 cmp $lc1; | |
1180 | } | |
6be75cd7 | 1181 | |
2a680da6 JH |
1182 | sub lcB { |
1183 | return lc($_[0]) cmp lc($_[1]); | |
1184 | } | |
6be75cd7 | 1185 | |
2a680da6 JH |
1186 | my $x = "ab"; |
1187 | my $y = "aa"; | |
1188 | my $z = "AB"; | |
6be75cd7 | 1189 | |
c4093d7d | 1190 | tryneoalpha($Locale, ++$locales_test_number, |
2a680da6 JH |
1191 | lcA($x, $y) == 1 && lcB($x, $y) == 1 || |
1192 | lcA($x, $z) == 0 && lcB($x, $z) == 0); | |
6be75cd7 | 1193 | } |
66cbab2c KW |
1194 | else { |
1195 | use locale ':not_characters'; | |
1196 | ||
1197 | sub lcC { | |
1198 | my $lc0 = lc $_[0]; | |
1199 | my $lc1 = lc $_[1]; | |
1200 | return $lc0 cmp $lc1; | |
1201 | } | |
1202 | ||
1203 | sub lcD { | |
1204 | return lc($_[0]) cmp lc($_[1]); | |
1205 | } | |
1206 | ||
1207 | my $x = "ab"; | |
1208 | my $y = "aa"; | |
1209 | my $z = "AB"; | |
1210 | ||
1211 | tryneoalpha($Locale, ++$locales_test_number, | |
1212 | lcC($x, $y) == 1 && lcD($x, $y) == 1 || | |
1213 | lcC($x, $z) == 0 && lcD($x, $z) == 0); | |
1214 | } | |
1215 | $test_names{$locales_test_number} = 'Verify "lc(foo) cmp lc(bar)" is the same as using intermediaries for the cmp'; | |
d8093b23 | 1216 | |
26d80d95 LC |
1217 | # Does lc of an UPPER (if different from the UPPER) match |
1218 | # case-insensitively the UPPER, and does the UPPER match | |
1219 | # case-insensitively the lc of the UPPER. And vice versa. | |
3ba0e062 | 1220 | { |
ef4a39e5 JH |
1221 | use locale; |
1222 | no utf8; | |
1223 | my $re = qr/[\[\(\{\*\+\?\|\^\$\\]/; | |
1224 | ||
1225 | my @f = (); | |
c4093d7d | 1226 | ++$locales_test_number; |
c08acc4c | 1227 | $test_names{$locales_test_number} = 'Verify case insensitive matching works'; |
f78d9f29 | 1228 | foreach my $x (sort keys %UPPER) { |
66cbab2c | 1229 | if (! $is_utf8_locale) { |
71e5cbb3 KW |
1230 | my $y = lc $x; |
1231 | next unless uc $y eq $x; | |
1232 | print "# UPPER $x lc $y ", | |
faf0c248 KW |
1233 | $x =~ /$y/i ? 1 : 0, " ", |
1234 | $y =~ /$x/i ? 1 : 0, "\n" if 0; | |
71e5cbb3 KW |
1235 | # |
1236 | # If $x and $y contain regular expression characters | |
1237 | # AND THEY lowercase (/i) to regular expression characters, | |
1238 | # regcomp() will be mightily confused. No, the \Q doesn't | |
1239 | # help here (maybe regex engine internal lowercasing | |
1240 | # is done after the \Q?) An example of this happening is | |
1241 | # the bg_BG (Bulgarian) locale under EBCDIC (OS/390 USS): | |
1242 | # the chr(173) (the "[") is the lowercase of the chr(235). | |
1243 | # | |
1244 | # Similarly losing EBCDIC locales include cs_cz, cs_CZ, | |
1245 | # el_gr, el_GR, en_us.IBM-037 (!), en_US.IBM-037 (!), | |
1246 | # et_ee, et_EE, hr_hr, hr_HR, hu_hu, hu_HU, lt_LT, | |
1247 | # mk_mk, mk_MK, nl_nl.IBM-037, nl_NL.IBM-037, | |
1248 | # pl_pl, pl_PL, ro_ro, ro_RO, ru_ru, ru_RU, | |
1249 | # sk_sk, sk_SK, sl_si, sl_SI, tr_tr, tr_TR. | |
1250 | # | |
1251 | # Similar things can happen even under (bastardised) | |
1252 | # non-EBCDIC locales: in many European countries before the | |
1253 | # advent of ISO 8859-x nationally customised versions of | |
1254 | # ISO 646 were devised, reusing certain punctuation | |
1255 | # characters for modified characters needed by the | |
1256 | # country/language. For example, the "|" might have | |
1257 | # stood for U+00F6 or LATIN SMALL LETTER O WITH DIAERESIS. | |
1258 | # | |
1259 | if ($x =~ $re || $y =~ $re) { | |
1260 | print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n"; | |
1261 | next; | |
1262 | } | |
1263 | # With utf8 both will fail since the locale concept | |
1264 | # of upper/lower does not work well in Unicode. | |
1265 | push @f, $x unless $x =~ /$y/i == $y =~ /$x/i; | |
26c1569f KW |
1266 | |
1267 | # fc is not a locale concept, so Perl uses lc for it. | |
1268 | push @f, $x unless lc $x eq fc $x; | |
66cbab2c KW |
1269 | } |
1270 | else { | |
1271 | use locale ':not_characters'; | |
1272 | my $y = lc $x; | |
1273 | next unless uc $y eq $x; | |
1274 | print "# UPPER $x lc $y ", | |
faf0c248 KW |
1275 | $x =~ /$y/i ? 1 : 0, " ", |
1276 | $y =~ /$x/i ? 1 : 0, "\n" if 0; | |
66cbab2c KW |
1277 | |
1278 | # Here, we can fully test things, unlike plain 'use locale', | |
1279 | # because this form does work well with Unicode | |
1280 | push @f, $x unless $x =~ /$y/i && $y =~ /$x/i; | |
26c1569f KW |
1281 | |
1282 | # The places where Unicode's lc is different from fc are | |
1283 | # skipped here by virtue of the 'next unless uc...' line above | |
1284 | push @f, $x unless lc $x eq fc $x; | |
66cbab2c | 1285 | } |
c00ff1c7 | 1286 | } |
ef4a39e5 | 1287 | |
f78d9f29 | 1288 | foreach my $x (sort keys %lower) { |
66cbab2c | 1289 | if (! $is_utf8_locale) { |
71e5cbb3 KW |
1290 | my $y = uc $x; |
1291 | next unless lc $y eq $x; | |
1292 | print "# lower $x uc $y ", | |
faf0c248 KW |
1293 | $x =~ /$y/i ? 1 : 0, " ", |
1294 | $y =~ /$x/i ? 1 : 0, "\n" if 0; | |
71e5cbb3 KW |
1295 | if ($x =~ $re || $y =~ $re) { # See above. |
1296 | print "# Regex characters in '$x' or '$y', skipping test $locales_test_number for locale '$Locale'\n"; | |
1297 | next; | |
1298 | } | |
1299 | # With utf8 both will fail since the locale concept | |
1300 | # of upper/lower does not work well in Unicode. | |
1301 | push @f, $x unless $x =~ /$y/i == $y =~ /$x/i; | |
26c1569f KW |
1302 | |
1303 | push @f, $x unless lc $x eq fc $x; | |
66cbab2c KW |
1304 | } |
1305 | else { | |
1306 | use locale ':not_characters'; | |
1307 | my $y = uc $x; | |
1308 | next unless lc $y eq $x; | |
1309 | print "# lower $x uc $y ", | |
faf0c248 KW |
1310 | $x =~ /$y/i ? 1 : 0, " ", |
1311 | $y =~ /$x/i ? 1 : 0, "\n" if 0; | |
66cbab2c | 1312 | push @f, $x unless $x =~ /$y/i && $y =~ /$x/i; |
26c1569f KW |
1313 | |
1314 | push @f, $x unless lc $x eq fc $x; | |
66cbab2c | 1315 | } |
c00ff1c7 | 1316 | } |
c4093d7d | 1317 | tryneoalpha($Locale, $locales_test_number, @f == 0); |
c00ff1c7 | 1318 | if (@f) { |
c4093d7d | 1319 | print "# failed $locales_test_number locale '$Locale' characters @f\n" |
c00ff1c7 | 1320 | } |
d8093b23 | 1321 | } |
78787052 JL |
1322 | |
1323 | # [perl #109318] | |
1324 | { | |
1325 | my @f = (); | |
1326 | ++$locales_test_number; | |
1327 | $test_names{$locales_test_number} = 'Verify atof with locale radix and negative exponent'; | |
1328 | ||
1329 | my $radix = POSIX::localeconv()->{decimal_point}; | |
1330 | my @nums = ( | |
1331 | "3.14e+9", "3${radix}14e+9", "3.14e-9", "3${radix}14e-9", | |
1332 | "-3.14e+9", "-3${radix}14e+9", "-3.14e-9", "-3${radix}14e-9", | |
1333 | ); | |
1334 | ||
1335 | if (! $is_utf8_locale) { | |
1336 | use locale; | |
1337 | for my $num (@nums) { | |
1338 | push @f, $num | |
1339 | unless sprintf("%g", $num) =~ /3.+14/; | |
1340 | } | |
1341 | } | |
1342 | else { | |
1343 | use locale ':not_characters'; | |
1344 | for my $num (@nums) { | |
1345 | push @f, $num | |
1346 | unless sprintf("%g", $num) =~ /3.+14/; | |
1347 | } | |
1348 | } | |
1349 | ||
1350 | tryneoalpha($Locale, $locales_test_number, @f == 0); | |
1351 | if (@f) { | |
1352 | print "# failed $locales_test_number locale '$Locale' numbers @f\n" | |
1353 | } | |
1354 | } | |
8ebc5c01 | 1355 | } |
284102e8 | 1356 | |
c4093d7d | 1357 | my $final_locales_test_number = $locales_test_number; |
6cf0b567 | 1358 | |
2a680da6 JH |
1359 | # Recount the errors. |
1360 | ||
c4093d7d KW |
1361 | foreach ($first_locales_test_number..$final_locales_test_number) { |
1362 | if (%setlocale_failed) { | |
1363 | print "not "; | |
1364 | } | |
1365 | elsif ($Problem{$_} || !defined $Okay{$_} || !@{$Okay{$_}}) { | |
1366 | if (defined $not_necessarily_a_problem_test_number | |
1367 | && $_ == $not_necessarily_a_problem_test_number) | |
1368 | { | |
1369 | print "# The failure of test $not_necessarily_a_problem_test_number is not necessarily fatal.\n"; | |
b4e009be | 1370 | print "# It usually indicates a problem in the environment,\n"; |
284102e8 JH |
1371 | print "# not in Perl itself.\n"; |
1372 | } | |
1373 | print "not "; | |
8ebc5c01 | 1374 | } |
c4093d7d | 1375 | print "ok $_"; |
6c2e653d KW |
1376 | if (defined $test_names{$_}) { |
1377 | # If TODO is in the test name, make it thus | |
1378 | my $todo = $test_names{$_} =~ s/TODO\s*//; | |
1379 | print " $test_names{$_}"; | |
1380 | print " # TODO" if $todo; | |
1381 | } | |
c4093d7d | 1382 | print "\n"; |
8ebc5c01 | 1383 | } |
fb73857a | 1384 | |
2a680da6 JH |
1385 | # Give final advice. |
1386 | ||
284102e8 JH |
1387 | my $didwarn = 0; |
1388 | ||
c4093d7d | 1389 | foreach ($first_locales_test_number..$final_locales_test_number) { |
284102e8 JH |
1390 | if ($Problem{$_}) { |
1391 | my @f = sort keys %{ $Problem{$_} }; | |
1392 | my $f = join(" ", @f); | |
1393 | $f =~ s/(.{50,60}) /$1\n#\t/g; | |
2a680da6 JH |
1394 | |
1395 | "#\n", | |
1396 | "# The locale ", (@f == 1 ? "definition" : "definitions"), "\n#\n", | |
284102e8 JH |
1397 | "#\t", $f, "\n#\n", |
1398 | "# on your system may have errors because the locale test $_\n", | |
1399 | "# failed in ", (@f == 1 ? "that locale" : "those locales"), | |
1400 | ".\n"; | |
2a680da6 | 1401 | print <<EOW; |
284102e8 JH |
1402 | # |
1403 | # If your users are not using these locales you are safe for the moment, | |
1404 | # but please report this failure first to perlbug\@perl.com using the | |
1405 | # perlbug script (as described in the INSTALL file) so that the exact | |
1406 | # details of the failures can be sorted out first and then your operating | |
1407 | # system supplier can be alerted about these anomalies. | |
1408 | # | |
1409 | EOW | |
1410 | $didwarn = 1; | |
fb73857a | 1411 | } |
1412 | } | |
774d564b | 1413 | |
26d80d95 | 1414 | # Tell which locales were okay and which were not. |
2a680da6 | 1415 | |
284102e8 | 1416 | if ($didwarn) { |
26d80d95 | 1417 | my (@s, @F); |
71e5cbb3 | 1418 | |
284102e8 JH |
1419 | foreach my $l (@Locale) { |
1420 | my $p = 0; | |
c4093d7d KW |
1421 | if ($setlocale_failed{$l}) { |
1422 | $p++; | |
1423 | } | |
1424 | else { | |
1f5852c9 KW |
1425 | foreach my $t |
1426 | ($first_locales_test_number..$final_locales_test_number) | |
1427 | { | |
1428 | $p++ if $Problem{$t}{$l}; | |
1429 | } | |
c4093d7d | 1430 | } |
284102e8 | 1431 | push @s, $l if $p == 0; |
9445c837 | 1432 | push @F, $l unless $p == 0; |
8ebc5c01 | 1433 | } |
71e5cbb3 | 1434 | |
68d47915 CK |
1435 | if (@s) { |
1436 | my $s = join(" ", @s); | |
1437 | $s =~ s/(.{50,60}) /$1\n#\t/g; | |
1438 | ||
1439 | warn | |
1440 | "# The following locales\n#\n", | |
1441 | "#\t", $s, "\n#\n", | |
1442 | "# tested okay.\n#\n", | |
1443 | } else { | |
26d80d95 LC |
1444 | warn "# None of your locales were fully okay.\n"; |
1445 | } | |
1446 | ||
1447 | if (@F) { | |
1448 | my $F = join(" ", @F); | |
1449 | $F =~ s/(.{50,60}) /$1\n#\t/g; | |
1450 | ||
1451 | warn | |
1452 | "# The following locales\n#\n", | |
0e053d1e | 1453 | "#\t", $F, "\n#\n", |
26d80d95 LC |
1454 | "# had problems.\n#\n", |
1455 | } else { | |
1456 | warn "# None of your locales were broken.\n"; | |
68d47915 | 1457 | } |
8ebc5c01 | 1458 | } |
90248788 | 1459 | |
c4093d7d | 1460 | $test_num = $final_locales_test_number; |
c213d471 | 1461 | |
094a2f8c | 1462 | # Test that tainting and case changing works on utf8 strings. These tests are |
1f5852c9 KW |
1463 | # placed last to avoid disturbing the hard-coded test numbers that existed at |
1464 | # the time these were added above this in this file. | |
0099bb8d KW |
1465 | # This also tests that locale overrides unicode_strings in the same scope for |
1466 | # non-utf8 strings. | |
094a2f8c KW |
1467 | setlocale(LC_ALL, "C"); |
1468 | { | |
1469 | use locale; | |
0099bb8d | 1470 | use feature 'unicode_strings'; |
094a2f8c | 1471 | |
26c1569f | 1472 | foreach my $function ("uc", "ucfirst", "lc", "lcfirst", "fc") { |
094a2f8c KW |
1473 | my @list; # List of code points to test for $function |
1474 | ||
1475 | # Used to calculate the changed case for ASCII characters by using the | |
1476 | # ord, instead of using one of the functions under test. | |
1477 | my $ascii_case_change_delta; | |
1478 | my $above_latin1_case_change_delta; # Same for the specific ords > 255 | |
1479 | # that we use | |
1480 | ||
1481 | # We test an ASCII character, which should change case and be tainted; | |
1482 | # a Latin1 character, which shouldn't change case under this C locale, | |
1483 | # and is tainted. | |
1484 | # an above-Latin1 character that when the case is changed would cross | |
1485 | # the 255/256 boundary, so doesn't change case and isn't tainted | |
1486 | # (the \x{149} is one of these, but changes into 2 characters, the | |
1487 | # first one of which doesn't cross the boundary. | |
1488 | # the final one in each list is an above-Latin1 character whose case | |
1489 | # does change, and shouldn't be tainted. The code below uses its | |
1490 | # position in its list as a marker to indicate that it, unlike the | |
1491 | # other code points above ASCII, has a successful case change | |
1492 | if ($function =~ /^u/) { | |
094a2f8c KW |
1493 | @list = ("", "a", "\xe0", "\xff", "\x{fb00}", "\x{149}", "\x{101}"); |
1494 | $ascii_case_change_delta = -32; | |
1495 | $above_latin1_case_change_delta = -1; | |
1496 | } | |
1497 | else { | |
1498 | @list = ("", "A", "\xC0", "\x{1E9E}", "\x{100}"); | |
1499 | $ascii_case_change_delta = +32; | |
1500 | $above_latin1_case_change_delta = +1; | |
1501 | } | |
66cbab2c | 1502 | foreach my $is_utf8_locale (0 .. 1) { |
71e5cbb3 KW |
1503 | foreach my $j (0 .. $#list) { |
1504 | my $char = $list[$j]; | |
0099bb8d KW |
1505 | |
1506 | for my $encoded_in_utf8 (0 .. 1) { | |
faf0c248 KW |
1507 | my $should_be; |
1508 | my $changed; | |
1509 | if (! $is_utf8_locale) { | |
1510 | $should_be = ($j == $#list) | |
1511 | ? chr(ord($char) + $above_latin1_case_change_delta) | |
1512 | : (length $char == 0 || ord($char) > 127) | |
1513 | ? $char | |
1514 | : chr(ord($char) + $ascii_case_change_delta); | |
1515 | ||
1516 | # This monstrosity is in order to avoid using an eval, | |
1517 | # which might perturb the results | |
1518 | $changed = ($function eq "uc") | |
1519 | ? uc($char) | |
1520 | : ($function eq "ucfirst") | |
1521 | ? ucfirst($char) | |
1522 | : ($function eq "lc") | |
1523 | ? lc($char) | |
1524 | : ($function eq "lcfirst") | |
1525 | ? lcfirst($char) | |
26c1569f KW |
1526 | : ($function eq "fc") |
1527 | ? fc($char) | |
faf0c248 KW |
1528 | : die("Unexpected function \"$function\""); |
1529 | } | |
1530 | else { | |
1531 | { | |
1532 | no locale; | |
71e5cbb3 | 1533 | |
faf0c248 KW |
1534 | # For utf8-locales the case changing functions |
1535 | # should work just like they do outside of locale. | |
1536 | # Can use eval here because not testing it when | |
1537 | # not in locale. | |
1538 | $should_be = eval "$function('$char')"; | |
1539 | die "Unexpected eval error $@ from 'eval \"$function('$char')\"'" if $@; | |
71e5cbb3 | 1540 | |
faf0c248 KW |
1541 | } |
1542 | use locale ':not_characters'; | |
1543 | $changed = ($function eq "uc") | |
1544 | ? uc($char) | |
1545 | : ($function eq "ucfirst") | |
1546 | ? ucfirst($char) | |
1547 | : ($function eq "lc") | |
1548 | ? lc($char) | |
1549 | : ($function eq "lcfirst") | |
1550 | ? lcfirst($char) | |
26c1569f KW |
1551 | : ($function eq "fc") |
1552 | ? fc($char) | |
faf0c248 | 1553 | : die("Unexpected function \"$function\""); |
71e5cbb3 | 1554 | } |
faf0c248 KW |
1555 | ok($changed eq $should_be, |
1556 | "$function(\"$char\") in C locale " | |
1557 | . (($is_utf8_locale) | |
1558 | ? "(use locale ':not_characters'" | |
1559 | : "(use locale") | |
1560 | . (($encoded_in_utf8) | |
1561 | ? "; encoded in utf8)" | |
1562 | : "; not encoded in utf8)") | |
1563 | . " should be \"$should_be\", got \"$changed\""); | |
1564 | ||
1565 | # Tainting shouldn't happen for utf8 locales, empty | |
1566 | # strings, or those characters above 255. | |
1567 | (! $is_utf8_locale && length($char) > 0 && ord($char) < 256) | |
1568 | ? check_taint($changed) | |
1569 | : check_taint_not($changed); | |
1570 | ||
1571 | # Use UTF-8 next time through the loop | |
1572 | utf8::upgrade($char); | |
0099bb8d | 1573 | } |
66cbab2c | 1574 | } |
094a2f8c KW |
1575 | } |
1576 | } | |
1577 | } | |
1578 | ||
fdf053ee | 1579 | print "1..$test_num\n"; |
906f284f | 1580 | |
90248788 | 1581 | # eof |