This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
UCD.t: Skip tests for PropertyAlias on early Unicodes
[perl5.git] / lib / Unicode / UCD.t
CommitLineData
25a47338 1#!perl -w
8b731da2 2BEGIN {
a452d459 3 if (ord("A") != 65) {
8b731da2
JH
4 print "1..0 # Skip: EBCDIC\n";
5 exit 0;
6 }
a778afa6
AD
7 chdir 't' if -d 't';
8 @INC = '../lib';
25a47338
NC
9 require Config; import Config;
10 if ($Config{'extensions'} !~ /\bStorable\b/) {
11 print "1..0 # Skip: Storable was not built; Unicode::UCD uses Storable\n";
12 exit 0;
13 }
8b731da2
JH
14}
15
a778afa6
AD
16use strict;
17use Unicode::UCD;
f5c9f3db 18use Test::More;
8b731da2 19
55d7b906 20use Unicode::UCD 'charinfo';
561c79ed 21
ce066323
FC
22$/ = 7;
23
b08cd201
JH
24my $charinfo;
25
9e92970c
KW
26is(charinfo(0x110000), undef, "Verify charinfo() of non-unicode is undef");
27
e10d7780
KW
28$charinfo = charinfo(0); # Null is often problematic, so test it.
29
30is($charinfo->{code}, '0000', '<control>');
31is($charinfo->{name}, '<control>');
32is($charinfo->{category}, 'Cc');
33is($charinfo->{combining}, '0');
34is($charinfo->{bidi}, 'BN');
35is($charinfo->{decomposition}, '');
36is($charinfo->{decimal}, '');
37is($charinfo->{digit}, '');
38is($charinfo->{numeric}, '');
39is($charinfo->{mirrored}, 'N');
40is($charinfo->{unicode10}, 'NULL');
41is($charinfo->{comment}, '');
42is($charinfo->{upper}, '');
43is($charinfo->{lower}, '');
44is($charinfo->{title}, '');
45is($charinfo->{block}, 'Basic Latin');
46is($charinfo->{script}, 'Common');
47
b08cd201
JH
48$charinfo = charinfo(0x41);
49
f5c9f3db
JH
50is($charinfo->{code}, '0041', 'LATIN CAPITAL LETTER A');
51is($charinfo->{name}, 'LATIN CAPITAL LETTER A');
52is($charinfo->{category}, 'Lu');
53is($charinfo->{combining}, '0');
54is($charinfo->{bidi}, 'L');
55is($charinfo->{decomposition}, '');
56is($charinfo->{decimal}, '');
57is($charinfo->{digit}, '');
58is($charinfo->{numeric}, '');
59is($charinfo->{mirrored}, 'N');
60is($charinfo->{unicode10}, '');
61is($charinfo->{comment}, '');
62is($charinfo->{upper}, '');
63is($charinfo->{lower}, '0061');
64is($charinfo->{title}, '');
65is($charinfo->{block}, 'Basic Latin');
66is($charinfo->{script}, 'Latin');
b08cd201
JH
67
68$charinfo = charinfo(0x100);
69
f5c9f3db
JH
70is($charinfo->{code}, '0100', 'LATIN CAPITAL LETTER A WITH MACRON');
71is($charinfo->{name}, 'LATIN CAPITAL LETTER A WITH MACRON');
72is($charinfo->{category}, 'Lu');
73is($charinfo->{combining}, '0');
74is($charinfo->{bidi}, 'L');
75is($charinfo->{decomposition}, '0041 0304');
76is($charinfo->{decimal}, '');
77is($charinfo->{digit}, '');
78is($charinfo->{numeric}, '');
79is($charinfo->{mirrored}, 'N');
80is($charinfo->{unicode10}, 'LATIN CAPITAL LETTER A MACRON');
81is($charinfo->{comment}, '');
82is($charinfo->{upper}, '');
83is($charinfo->{lower}, '0101');
84is($charinfo->{title}, '');
85is($charinfo->{block}, 'Latin Extended-A');
86is($charinfo->{script}, 'Latin');
a196fbfd
JH
87
88# 0x0590 is in the Hebrew block but unused.
561c79ed 89
b08cd201
JH
90$charinfo = charinfo(0x590);
91
f5c9f3db
JH
92is($charinfo->{code}, undef, '0x0590 - unused Hebrew');
93is($charinfo->{name}, undef);
94is($charinfo->{category}, undef);
95is($charinfo->{combining}, undef);
96is($charinfo->{bidi}, undef);
97is($charinfo->{decomposition}, undef);
98is($charinfo->{decimal}, undef);
99is($charinfo->{digit}, undef);
100is($charinfo->{numeric}, undef);
101is($charinfo->{mirrored}, undef);
102is($charinfo->{unicode10}, undef);
103is($charinfo->{comment}, undef);
104is($charinfo->{upper}, undef);
105is($charinfo->{lower}, undef);
106is($charinfo->{title}, undef);
107is($charinfo->{block}, undef);
108is($charinfo->{script}, undef);
a196fbfd
JH
109
110# 0x05d0 is in the Hebrew block and used.
561c79ed 111
b08cd201
JH
112$charinfo = charinfo(0x5d0);
113
f5c9f3db
JH
114is($charinfo->{code}, '05D0', '05D0 - used Hebrew');
115is($charinfo->{name}, 'HEBREW LETTER ALEF');
116is($charinfo->{category}, 'Lo');
117is($charinfo->{combining}, '0');
118is($charinfo->{bidi}, 'R');
119is($charinfo->{decomposition}, '');
120is($charinfo->{decimal}, '');
121is($charinfo->{digit}, '');
122is($charinfo->{numeric}, '');
123is($charinfo->{mirrored}, 'N');
124is($charinfo->{unicode10}, '');
125is($charinfo->{comment}, '');
126is($charinfo->{upper}, '');
127is($charinfo->{lower}, '');
128is($charinfo->{title}, '');
129is($charinfo->{block}, 'Hebrew');
130is($charinfo->{script}, 'Hebrew');
561c79ed 131
74f8133e 132# An open syllable in Hangul.
a6fa416b
TS
133
134$charinfo = charinfo(0xAC00);
135
05dbc6f8
KW
136is($charinfo->{code}, 'AC00', 'HANGUL SYLLABLE U+AC00');
137is($charinfo->{name}, 'HANGUL SYLLABLE GA');
f5c9f3db
JH
138is($charinfo->{category}, 'Lo');
139is($charinfo->{combining}, '0');
140is($charinfo->{bidi}, 'L');
05dbc6f8 141is($charinfo->{decomposition}, '1100 1161');
f5c9f3db
JH
142is($charinfo->{decimal}, '');
143is($charinfo->{digit}, '');
144is($charinfo->{numeric}, '');
145is($charinfo->{mirrored}, 'N');
146is($charinfo->{unicode10}, '');
147is($charinfo->{comment}, '');
148is($charinfo->{upper}, '');
149is($charinfo->{lower}, '');
150is($charinfo->{title}, '');
151is($charinfo->{block}, 'Hangul Syllables');
152is($charinfo->{script}, 'Hangul');
a6fa416b 153
74f8133e 154# A closed syllable in Hangul.
a6fa416b
TS
155
156$charinfo = charinfo(0xAE00);
157
05dbc6f8
KW
158is($charinfo->{code}, 'AE00', 'HANGUL SYLLABLE U+AE00');
159is($charinfo->{name}, 'HANGUL SYLLABLE GEUL');
f5c9f3db
JH
160is($charinfo->{category}, 'Lo');
161is($charinfo->{combining}, '0');
162is($charinfo->{bidi}, 'L');
05dbc6f8 163is($charinfo->{decomposition}, "1100 1173 11AF");
f5c9f3db
JH
164is($charinfo->{decimal}, '');
165is($charinfo->{digit}, '');
166is($charinfo->{numeric}, '');
167is($charinfo->{mirrored}, 'N');
168is($charinfo->{unicode10}, '');
169is($charinfo->{comment}, '');
170is($charinfo->{upper}, '');
171is($charinfo->{lower}, '');
172is($charinfo->{title}, '');
173is($charinfo->{block}, 'Hangul Syllables');
174is($charinfo->{script}, 'Hangul');
a6fa416b
TS
175
176$charinfo = charinfo(0x1D400);
177
f5c9f3db
JH
178is($charinfo->{code}, '1D400', 'MATHEMATICAL BOLD CAPITAL A');
179is($charinfo->{name}, 'MATHEMATICAL BOLD CAPITAL A');
180is($charinfo->{category}, 'Lu');
181is($charinfo->{combining}, '0');
182is($charinfo->{bidi}, 'L');
183is($charinfo->{decomposition}, '<font> 0041');
184is($charinfo->{decimal}, '');
185is($charinfo->{digit}, '');
186is($charinfo->{numeric}, '');
187is($charinfo->{mirrored}, 'N');
188is($charinfo->{unicode10}, '');
189is($charinfo->{comment}, '');
190is($charinfo->{upper}, '');
191is($charinfo->{lower}, '');
192is($charinfo->{title}, '');
193is($charinfo->{block}, 'Mathematical Alphanumeric Symbols');
7be0dac3 194is($charinfo->{script}, 'Common');
a6fa416b 195
a452d459
KW
196$charinfo = charinfo(0x9FBA); #Bug 58428
197
198is($charinfo->{code}, '9FBA', 'U+9FBA');
199is($charinfo->{name}, 'CJK UNIFIED IDEOGRAPH-9FBA');
200is($charinfo->{category}, 'Lo');
201is($charinfo->{combining}, '0');
202is($charinfo->{bidi}, 'L');
203is($charinfo->{decomposition}, '');
204is($charinfo->{decimal}, '');
205is($charinfo->{digit}, '');
206is($charinfo->{numeric}, '');
207is($charinfo->{mirrored}, 'N');
208is($charinfo->{unicode10}, '');
209is($charinfo->{comment}, '');
210is($charinfo->{upper}, '');
211is($charinfo->{lower}, '');
212is($charinfo->{title}, '');
213is($charinfo->{block}, 'CJK Unified Ideographs');
214is($charinfo->{script}, 'Han');
215
55d7b906 216use Unicode::UCD qw(charblock charscript);
a196fbfd
JH
217
218# 0x0590 is in the Hebrew block but unused.
561c79ed 219
f5c9f3db 220is(charblock(0x590), 'Hebrew', '0x0590 - Hebrew unused charblock');
8079ad82 221is(charscript(0x590), 'Unknown', '0x0590 - Hebrew unused charscript');
c707cf8e 222is(charblock(0x1FFFF), 'No_Block', '0x1FFFF - unused charblock');
561c79ed 223
b08cd201
JH
224$charinfo = charinfo(0xbe);
225
f5c9f3db
JH
226is($charinfo->{code}, '00BE', 'VULGAR FRACTION THREE QUARTERS');
227is($charinfo->{name}, 'VULGAR FRACTION THREE QUARTERS');
228is($charinfo->{category}, 'No');
229is($charinfo->{combining}, '0');
230is($charinfo->{bidi}, 'ON');
231is($charinfo->{decomposition}, '<fraction> 0033 2044 0034');
232is($charinfo->{decimal}, '');
233is($charinfo->{digit}, '');
234is($charinfo->{numeric}, '3/4');
235is($charinfo->{mirrored}, 'N');
236is($charinfo->{unicode10}, 'FRACTION THREE QUARTERS');
237is($charinfo->{comment}, '');
238is($charinfo->{upper}, '');
239is($charinfo->{lower}, '');
240is($charinfo->{title}, '');
241is($charinfo->{block}, 'Latin-1 Supplement');
7be0dac3 242is($charinfo->{script}, 'Common');
10a6ecd2 243
7fcb6f68
KW
244# This is to test a case where both simple and full lowercases exist and
245# differ
246$charinfo = charinfo(0x130);
247
248is($charinfo->{code}, '0130', 'LATIN CAPITAL LETTER I WITH DOT ABOVE');
249is($charinfo->{name}, 'LATIN CAPITAL LETTER I WITH DOT ABOVE');
250is($charinfo->{category}, 'Lu');
251is($charinfo->{combining}, '0');
252is($charinfo->{bidi}, 'L');
253is($charinfo->{decomposition}, '0049 0307');
254is($charinfo->{decimal}, '');
255is($charinfo->{digit}, '');
256is($charinfo->{numeric}, '');
257is($charinfo->{mirrored}, 'N');
258is($charinfo->{unicode10}, 'LATIN CAPITAL LETTER I DOT');
259is($charinfo->{comment}, '');
260is($charinfo->{upper}, '');
261is($charinfo->{lower}, '0069');
262is($charinfo->{title}, '');
263is($charinfo->{block}, 'Latin Extended-A');
264is($charinfo->{script}, 'Latin');
265
266# This is to test a case where both simple and full uppercases exist and
267# differ
268$charinfo = charinfo(0x1F80);
269
270is($charinfo->{code}, '1F80', 'GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI');
271is($charinfo->{name}, 'GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI');
272is($charinfo->{category}, 'Ll');
273is($charinfo->{combining}, '0');
274is($charinfo->{bidi}, 'L');
275is($charinfo->{decomposition}, '1F00 0345');
276is($charinfo->{decimal}, '');
277is($charinfo->{digit}, '');
278is($charinfo->{numeric}, '');
279is($charinfo->{mirrored}, 'N');
280is($charinfo->{unicode10}, '');
281is($charinfo->{comment}, '');
282is($charinfo->{upper}, '1F88');
283is($charinfo->{lower}, '');
284is($charinfo->{title}, '1F88');
285is($charinfo->{block}, 'Greek Extended');
286is($charinfo->{script}, 'Greek');
287
55d7b906 288use Unicode::UCD qw(charblocks charscripts);
10a6ecd2 289
b08cd201 290my $charblocks = charblocks();
10a6ecd2 291
f5c9f3db
JH
292ok(exists $charblocks->{Thai}, 'Thai charblock exists');
293is($charblocks->{Thai}->[0]->[0], hex('0e00'));
294ok(!exists $charblocks->{PigLatin}, 'PigLatin charblock does not exist');
10a6ecd2 295
b08cd201 296my $charscripts = charscripts();
10a6ecd2 297
f5c9f3db
JH
298ok(exists $charscripts->{Armenian}, 'Armenian charscript exists');
299is($charscripts->{Armenian}->[0]->[0], hex('0531'));
300ok(!exists $charscripts->{PigLatin}, 'PigLatin charscript does not exist');
10a6ecd2
JH
301
302my $charscript;
303
304$charscript = charscript("12ab");
f5c9f3db 305is($charscript, 'Ethiopic', 'Ethiopic charscript');
10a6ecd2
JH
306
307$charscript = charscript("0x12ab");
f5c9f3db 308is($charscript, 'Ethiopic');
10a6ecd2
JH
309
310$charscript = charscript("U+12ab");
f5c9f3db 311is($charscript, 'Ethiopic');
10a6ecd2
JH
312
313my $ranges;
314
315$ranges = charscript('Ogham');
7bccef0b
KW
316is($ranges->[0]->[0], hex('1680'), 'Ogham charscript');
317is($ranges->[0]->[1], hex('169C'));
10a6ecd2 318
55d7b906 319use Unicode::UCD qw(charinrange);
10a6ecd2
JH
320
321$ranges = charscript('Cherokee');
f5c9f3db 322ok(!charinrange($ranges, "139f"), 'Cherokee charscript');
10a6ecd2
JH
323ok( charinrange($ranges, "13a0"));
324ok( charinrange($ranges, "13f4"));
325ok(!charinrange($ranges, "13f5"));
326
ea508aee
JH
327use Unicode::UCD qw(general_categories);
328
329my $gc = general_categories();
330
331ok(exists $gc->{L}, 'has L');
332is($gc->{L}, 'Letter', 'L is Letter');
333is($gc->{Lu}, 'UppercaseLetter', 'Lu is UppercaseLetter');
334
335use Unicode::UCD qw(bidi_types);
336
337my $bt = bidi_types();
338
339ok(exists $bt->{L}, 'has L');
340is($bt->{L}, 'Left-to-Right', 'L is Left-to-Right');
341is($bt->{AL}, 'Right-to-Left Arabic', 'AL is Right-to-Left Arabic');
342
a452d459
KW
343# If this fails, then maybe one should look at the Unicode changes to see
344# what else might need to be updated.
7620cb10 345is(Unicode::UCD::UnicodeVersion, '6.1.0', 'UnicodeVersion');
b08cd201 346
55d7b906 347use Unicode::UCD qw(compexcl);
b08cd201 348
f5c9f3db 349ok(!compexcl(0x0100), 'compexcl');
71a442a8
KW
350ok(!compexcl(0xD801), 'compexcl of surrogate');
351ok(!compexcl(0x110000), 'compexcl of non-Unicode code point');
b08cd201
JH
352ok( compexcl(0x0958));
353
55d7b906 354use Unicode::UCD qw(casefold);
b08cd201
JH
355
356my $casefold;
357
358$casefold = casefold(0x41);
359
a452d459
KW
360is($casefold->{code}, '0041', 'casefold 0x41 code');
361is($casefold->{status}, 'C', 'casefold 0x41 status');
362is($casefold->{mapping}, '0061', 'casefold 0x41 mapping');
363is($casefold->{full}, '0061', 'casefold 0x41 full');
364is($casefold->{simple}, '0061', 'casefold 0x41 simple');
365is($casefold->{turkic}, "", 'casefold 0x41 turkic');
b08cd201
JH
366
367$casefold = casefold(0xdf);
368
a452d459
KW
369is($casefold->{code}, '00DF', 'casefold 0xDF code');
370is($casefold->{status}, 'F', 'casefold 0xDF status');
371is($casefold->{mapping}, '0073 0073', 'casefold 0xDF mapping');
372is($casefold->{full}, '0073 0073', 'casefold 0xDF full');
373is($casefold->{simple}, "", 'casefold 0xDF simple');
374is($casefold->{turkic}, "", 'casefold 0xDF turkic');
375
863a4fdf
KW
376# Do different tests depending on if version < 3.2, or not.
377my $v_unicode_version = pack "C*", split /\./, Unicode::UCD::UnicodeVersion();
378if ($v_unicode_version lt v3.2.0) {
a452d459
KW
379 $casefold = casefold(0x130);
380
381 is($casefold->{code}, '0130', 'casefold 0x130 code');
382 is($casefold->{status}, 'I' , 'casefold 0x130 status');
383 is($casefold->{mapping}, '0069', 'casefold 0x130 mapping');
384 is($casefold->{full}, '0069', 'casefold 0x130 full');
385 is($casefold->{simple}, "0069", 'casefold 0x130 simple');
386 is($casefold->{turkic}, "0069", 'casefold 0x130 turkic');
387
388 $casefold = casefold(0x131);
389
390 is($casefold->{code}, '0131', 'casefold 0x131 code');
391 is($casefold->{status}, 'I' , 'casefold 0x131 status');
392 is($casefold->{mapping}, '0069', 'casefold 0x131 mapping');
393 is($casefold->{full}, '0069', 'casefold 0x131 full');
394 is($casefold->{simple}, "0069", 'casefold 0x131 simple');
395 is($casefold->{turkic}, "0069", 'casefold 0x131 turkic');
396} else {
397 $casefold = casefold(0x49);
398
399 is($casefold->{code}, '0049', 'casefold 0x49 code');
400 is($casefold->{status}, 'C' , 'casefold 0x49 status');
401 is($casefold->{mapping}, '0069', 'casefold 0x49 mapping');
402 is($casefold->{full}, '0069', 'casefold 0x49 full');
403 is($casefold->{simple}, "0069", 'casefold 0x49 simple');
404 is($casefold->{turkic}, "0131", 'casefold 0x49 turkic');
405
406 $casefold = casefold(0x130);
407
408 is($casefold->{code}, '0130', 'casefold 0x130 code');
409 is($casefold->{status}, 'F' , 'casefold 0x130 status');
410 is($casefold->{mapping}, '0069 0307', 'casefold 0x130 mapping');
411 is($casefold->{full}, '0069 0307', 'casefold 0x130 full');
412 is($casefold->{simple}, "", 'casefold 0x130 simple');
413 is($casefold->{turkic}, "0069", 'casefold 0x130 turkic');
414}
415
416$casefold = casefold(0x1F88);
417
418is($casefold->{code}, '1F88', 'casefold 0x1F88 code');
419is($casefold->{status}, 'S' , 'casefold 0x1F88 status');
420is($casefold->{mapping}, '1F80', 'casefold 0x1F88 mapping');
421is($casefold->{full}, '1F00 03B9', 'casefold 0x1F88 full');
422is($casefold->{simple}, '1F80', 'casefold 0x1F88 simple');
423is($casefold->{turkic}, "", 'casefold 0x1F88 turkic');
b08cd201
JH
424
425ok(!casefold(0x20));
426
55d7b906 427use Unicode::UCD qw(casespec);
b08cd201
JH
428
429my $casespec;
430
431ok(!casespec(0x41));
432
433$casespec = casespec(0xdf);
434
435ok($casespec->{code} eq '00DF' &&
436 $casespec->{lower} eq '00DF' &&
437 $casespec->{title} eq '0053 0073' &&
438 $casespec->{upper} eq '0053 0053' &&
2d3cf3ee 439 !defined $casespec->{condition}, 'casespec 0xDF');
b08cd201
JH
440
441$casespec = casespec(0x307);
442
f499c386 443ok($casespec->{az}->{code} eq '0307' &&
2d3cf3ee 444 !defined $casespec->{az}->{lower} &&
f499c386
JH
445 $casespec->{az}->{title} eq '0307' &&
446 $casespec->{az}->{upper} eq '0307' &&
9c3dc587 447 $casespec->{az}->{condition} eq 'az After_I',
f5c9f3db 448 'casespec 0x307');
6c8d78fb
HS
449
450# perl #7305 UnicodeCD::compexcl is weird
451
2d3cf3ee 452for (1) {my $a=compexcl $_}
6c8d78fb 453ok(1, 'compexcl read-only $_: perl #7305');
1f27373c 454map {compexcl $_} %{{1=>2}};
6c8d78fb
HS
455ok(1, 'compexcl read-only hash: perl #7305');
456
d7829152
JH
457is(Unicode::UCD::_getcode('123'), 123, "_getcode(123)");
458is(Unicode::UCD::_getcode('0123'), 0x123, "_getcode(0123)");
459is(Unicode::UCD::_getcode('0x123'), 0x123, "_getcode(0x123)");
460is(Unicode::UCD::_getcode('0X123'), 0x123, "_getcode(0X123)");
461is(Unicode::UCD::_getcode('U+123'), 0x123, "_getcode(U+123)");
462is(Unicode::UCD::_getcode('u+123'), 0x123, "_getcode(u+123)");
463is(Unicode::UCD::_getcode('U+1234'), 0x1234, "_getcode(U+1234)");
464is(Unicode::UCD::_getcode('U+12345'), 0x12345, "_getcode(U+12345)");
465is(Unicode::UCD::_getcode('123x'), undef, "_getcode(123x)");
466is(Unicode::UCD::_getcode('x123'), undef, "_getcode(x123)");
467is(Unicode::UCD::_getcode('0x123x'), undef, "_getcode(x123)");
468is(Unicode::UCD::_getcode('U+123x'), undef, "_getcode(x123)");
741297c1
JH
469
470{
471 my $r1 = charscript('Latin');
49ea58c8 472 if (ok(defined $r1, "Found Latin script")) {
f7ef59f7
KW
473 my $n1 = @$r1;
474 is($n1, 30, "number of ranges in Latin script (Unicode 6.1.0)");
475 shift @$r1 while @$r1;
476 my $r2 = charscript('Latin');
477 is(@$r2, $n1, "modifying results should not mess up internal caches");
49ea58c8 478 }
741297c1
JH
479}
480
c5a29f40
LM
481{
482 is(charinfo(0xdeadbeef), undef, "[perl #23273] warnings in Unicode::UCD");
2d3cf3ee 483}
a2bd7410
JH
484
485use Unicode::UCD qw(namedseq);
486
487is(namedseq("KATAKANA LETTER AINU P"), "\x{31F7}\x{309A}", "namedseq");
488is(namedseq("KATAKANA LETTER AINU Q"), undef);
489is(namedseq(), undef);
490is(namedseq(qw(foo bar)), undef);
491my @ns = namedseq("KATAKANA LETTER AINU P");
492is(scalar @ns, 2);
493is($ns[0], 0x31F7);
494is($ns[1], 0x309A);
495my %ns = namedseq();
496is($ns{"KATAKANA LETTER AINU P"}, "\x{31F7}\x{309A}");
497@ns = namedseq(42);
498is(@ns, 0);
499
7319f91d
KW
500use Unicode::UCD qw(num);
501use charnames ":full";
502
503is(num("0"), 0, 'Verify num("0") == 0');
504is(num("98765"), 98765, 'Verify num("98765") == 98765');
505ok(! defined num("98765\N{FULLWIDTH DIGIT FOUR}"), 'Verify num("98765\N{FULLWIDTH DIGIT FOUR}") isnt defined');
c4b3d89d
KW
506is(num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE DIGIT ONE}"), 21, 'Verify num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE DIGIT ONE}") == 21');
507ok(! defined num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE THAM DIGIT ONE}"), 'Verify num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE THAM DIGIT ONE}") isnt defined');
7319f91d
KW
508is(num("\N{CHAM DIGIT ZERO}\N{CHAM DIGIT THREE}"), 3, 'Verify num("\N{CHAM DIGIT ZERO}\N{CHAM DIGIT THREE}") == 3');
509ok(! defined num("\N{CHAM DIGIT ZERO}\N{JAVANESE DIGIT NINE}"), 'Verify num("\N{CHAM DIGIT ZERO}\N{JAVANESE DIGIT NINE}") isnt defined');
510is(num("\N{SUPERSCRIPT TWO}"), 2, 'Verify num("\N{SUPERSCRIPT TWO} == 2');
511is(num("\N{ETHIOPIC NUMBER TEN THOUSAND}"), 10000, 'Verify num("\N{ETHIOPIC NUMBER TEN THOUSAND}") == 10000');
512is(num("\N{NORTH INDIC FRACTION ONE HALF}"), .5, 'Verify num("\N{NORTH INDIC FRACTION ONE HALF}") == .5');
98025745 513is(num("\N{U+12448}"), 9, 'Verify num("\N{U+12448}") == 9');
4f143a72 514is(num("\N{U+5146}"), 1000000000000, 'Verify num("\N{U+5146}") == 1000000000000');
eaebe4db 515
7ef25837
KW
516# Create a user-defined property
517sub InKana {<<'END'}
5183040 309F
51930A0 30FF
520END
521
522use Unicode::UCD qw(prop_aliases);
523
524is(prop_aliases(undef), undef, "prop_aliases(undef) returns <undef>");
525is(prop_aliases("unknown property"), undef,
526 "prop_aliases(<unknown property>) returns <undef>");
527is(prop_aliases("InKana"), undef,
528 "prop_aliases(<user-defined property>) returns <undef>");
529is(prop_aliases("Perl_Decomposition_Mapping"), undef, "prop_aliases('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
530is(prop_aliases("Perl_Charnames"), undef,
531 "prop_aliases('Perl_Charnames') returns <undef> since internal-Perl-only");
532is(prop_aliases("isgc"), undef,
533 "prop_aliases('isgc') returns <undef> since is not covered Perl extension");
534is(prop_aliases("Is_Is_Any"), undef,
535 "prop_aliases('Is_Is_Any') returns <undef> since two is's");
536
537require 'utf8_heavy.pl';
538require "unicore/Heavy.pl";
539
540# Keys are lists of properties. Values are defined if have been tested.
541my %props;
542
543# To test for loose matching, add in the characters that are ignored there.
544my $extra_chars = "-_ ";
545
546# The one internal property we accept
547$props{'Perl_Decimal_Digit'} = 1;
548my @list = prop_aliases("perldecimaldigit");
549is_deeply(\@list,
550 [ "Perl_Decimal_Digit",
551 "Perl_Decimal_Digit"
552 ], "prop_aliases('perldecimaldigit') returns Perl_Decimal_Digit as both short and full names");
553
554# Get the official Unicode property name synonyms and test them.
0a2ae3b5
KW
555
556SKIP: {
557skip "PropertyAliases.txt is not in this Unicode version", 1 if $v_unicode_version lt v3.2.0;
7ef25837
KW
558open my $props, "<", "../lib/unicore/PropertyAliases.txt"
559 or die "Can't open Unicode PropertyAliases.txt";
ce066323 560$/ = "\n";
7ef25837
KW
561while (<$props>) {
562 s/\s*#.*//; # Remove comments
563 next if /^\s* $/x; # Ignore empty and comment lines
564
565 chomp;
566 my $count = 0; # 0th field in line is short name; 1th is long name
567 my $short_name;
568 my $full_name;
569 my @names_via_short;
570 foreach my $alias (split /\s*;\s*/) { # Fields are separated by
571 # semi-colons
572 # Add in the characters that are supposed to be ignored, to test loose
573 # matching, which the tested function does on all inputs.
574 my $mod_name = "$extra_chars$alias";
575
e72b6605 576 my $loose = &utf8::_loose_name(lc $alias);
7ef25837
KW
577
578 # Indicate we have tested this.
579 $props{$loose} = 1;
580
581 my @all_names = prop_aliases($mod_name);
582 if (grep { $_ eq $loose } @Unicode::UCD::suppressed_properties) {
583 is(@all_names, 0, "prop_aliases('$mod_name') returns undef since $alias is not installed");
584 next;
585 }
586 elsif (! @all_names) {
587 fail("prop_aliases('$mod_name')");
588 diag("'$alias' is unknown to prop_aliases()");
589 next;
590 }
591
592 if ($count == 0) { # Is short name
593
594 @names_via_short = prop_aliases($mod_name);
595
596 # If the 0th test fails, no sense in continuing with the others
597 last unless is($names_via_short[0], $alias,
598 "prop_aliases: '$alias' is the short name for '$mod_name'");
599 $short_name = $alias;
600 }
601 elsif ($count == 1) { # Is full name
602
603 # Some properties have the same short and full name; no sense
604 # repeating the test if the same.
605 if ($alias ne $short_name) {
606 my @names_via_full = prop_aliases($mod_name);
607 is_deeply(\@names_via_full, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
608 }
609
610 # Tests scalar context
611 is(prop_aliases($short_name), $alias,
612 "prop_aliases: '$alias' is the long name for '$short_name'");
613 }
614 else { # Is another alias
615 is_deeply(\@all_names, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
616 ok((grep { $_ =~ /^$alias$/i } @all_names),
617 "prop_aliases: '$alias' is listed as an alias for '$mod_name'");
618 }
619
620 $count++;
621 }
622}
0a2ae3b5 623} # End of SKIP block
7ef25837
KW
624
625# Now test anything we can find that wasn't covered by the tests of the
626# official properties. We have no way of knowing if mktables omitted a Perl
627# extension or not, but we do the best we can from its generated lists
628
629foreach my $alias (keys %utf8::loose_to_file_of) {
630 next if $alias =~ /=/;
631 my $lc_name = lc $alias;
e72b6605 632 my $loose = &utf8::_loose_name($lc_name);
7ef25837
KW
633 next if exists $props{$loose}; # Skip if already tested
634 $props{$loose} = 1;
635 my $mod_name = "$extra_chars$alias"; # Tests loose matching
636 my @aliases = prop_aliases($mod_name);
e72b6605 637 my $found_it = grep { &utf8::_loose_name(lc $_) eq $lc_name } @aliases;
7ef25837
KW
638 if ($found_it) {
639 pass("prop_aliases: '$lc_name' is listed as an alias for '$mod_name'");
640 }
641 elsif ($lc_name =~ /l[_&]$/) {
642
643 # These two names are special in that they don't appear in the
644 # returned list because they are discouraged from use. Verify
645 # that they return the same list as a non-discouraged version.
646 my @LC = prop_aliases('Is_LC');
647 is_deeply(\@aliases, \@LC, "prop_aliases: '$lc_name' returns the same list as 'Is_LC'");
648 }
649 else {
650 my $stripped = $lc_name =~ s/^is//;
651
652 # Could be that the input includes a prefix 'is', which is rarely
653 # returned as an alias, so having successfully stripped it off above,
654 # try again.
655 if ($stripped) {
e72b6605 656 $found_it = grep { &utf8::_loose_name(lc $_) eq $lc_name } @aliases;
7ef25837
KW
657 }
658
659 # If that didn't work, it could be that it's a block, which is always
660 # returned with a leading 'In_' to avoid ambiguity. Try comparing
661 # with that stripped off.
662 if (! $found_it) {
e72b6605 663 $found_it = grep { &utf8::_loose_name(s/^In_(.*)/\L$1/r) eq $lc_name }
7ef25837
KW
664 @aliases;
665 # Could check that is a real block, but tests for invmap will
666 # likely pickup any errors, since this will be tested there.
667 $lc_name = "in$lc_name" if $found_it; # Change for message below
668 }
669 my $message = "prop_aliases: '$lc_name' is listed as an alias for '$mod_name'";
670 ($found_it) ? pass($message) : fail($message);
671 }
672}
673
674my $done_equals = 0;
675foreach my $alias (keys %utf8::stricter_to_file_of) {
676 if ($alias =~ /=/) { # Only test one case where there is an equals
677 next if $done_equals;
678 $done_equals = 1;
679 }
680 my $lc_name = lc $alias;
681 my @list = prop_aliases($alias);
682 if ($alias =~ /^_/) {
683 is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since it is internal_only");
684 }
685 elsif ($alias =~ /=/) {
686 is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since is illegal property name");
687 }
688 else {
689 ok((grep { lc $_ eq $lc_name } @list),
690 "prop_aliases: '$lc_name' is listed as an alias for '$alias'");
691 }
692}
693
694use Unicode::UCD qw(prop_value_aliases);
695
696is(prop_value_aliases("unknown property", "unknown value"), undef,
697 "prop_value_aliases(<unknown property>, <unknown value>) returns <undef>");
698is(prop_value_aliases(undef, undef), undef,
699 "prop_value_aliases(undef, undef) returns <undef>");
700is((prop_value_aliases("na", "A")), "A", "test that prop_value_aliases returns its input for properties that don't have synonyms");
701is(prop_value_aliases("isgc", "C"), undef, "prop_value_aliases('isgc', 'C') returns <undef> since is not covered Perl extension");
702is(prop_value_aliases("gc", "isC"), undef, "prop_value_aliases('gc', 'isC') returns <undef> since is not covered Perl extension");
703
704# We have no way of knowing if mktables omitted a Perl extension that it
705# shouldn't have, but we can check if it omitted an official Unicode property
706# name synonym. And for those, we can check if the short and full names are
707# correct.
708
709my %pva_tested; # List of things already tested.
710open my $propvalues, "<", "../lib/unicore/PropValueAliases.txt"
711 or die "Can't open Unicode PropValueAliases.txt";
712while (<$propvalues>) {
713 s/\s*#.*//; # Remove comments
714 next if /^\s* $/x; # Ignore empty and comment lines
715 chomp;
716
74090492 717 # Fix typo in official input file
863a4fdf 718 s/CCC133/CCC132/g if $v_unicode_version eq v6.1.0;
74090492 719
7ef25837
KW
720 my @fields = split /\s*;\s*/; # Fields are separated by semi-colons
721 my $prop = shift @fields; # 0th field is the property,
722 my $count = 0; # 0th field in line (after shifting off the property) is
723 # short name; 1th is long name
724 my $short_name;
725 my @names_via_short; # Saves the values between iterations
726
727 # The property on the lhs of the = is always loosely matched. Add in
728 # characters that are ignored under loose matching to test that
729 my $mod_prop = "$extra_chars$prop";
730
731 if ($fields[0] eq 'n/a') { # See comments in input file, essentially
732 # means full name and short name are identical
733 $fields[0] = $fields[1];
734 }
735 elsif ($fields[0] ne $fields[1]
e72b6605
KW
736 && &utf8::_loose_name(lc $fields[0])
737 eq &utf8::_loose_name(lc $fields[1])
7ef25837
KW
738 && $fields[1] !~ /[[:upper:]]/)
739 {
740 # Also, there is a bug in the file in which "n/a" is omitted, and
741 # the two fields are identical except for case, and the full name
742 # is all lower case. Copy the "short" name unto the full one to
743 # give it some upper case.
744
745 $fields[1] = $fields[0];
746 }
747
748 # The ccc property in the file is special; has an extra numeric field
749 # (0th), which should go at the end, since we use the next two fields as
750 # the short and full names, respectively. See comments in input file.
751 splice (@fields, 0, 0, splice(@fields, 1, 2)) if $prop eq 'ccc';
752
e72b6605 753 my $loose_prop = &utf8::_loose_name(lc $prop);
7ef25837
KW
754 my $suppressed = grep { $_ eq $loose_prop }
755 @Unicode::UCD::suppressed_properties;
756 foreach my $value (@fields) {
757 if ($suppressed) {
758 is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop");
759 next;
760 }
e72b6605 761 elsif (grep { $_ eq ("$loose_prop=" . &utf8::_loose_name(lc $value)) } @Unicode::UCD::suppressed_properties) {
7ef25837
KW
762 is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop=$value");
763 next;
764 }
765
766 # Add in test for loose matching.
767 my $mod_value = "$extra_chars$value";
768
769 # If the value is a number, optionally negative, including a floating
770 # point or rational numer, it should be only strictly matched, so the
771 # loose matching should fail.
772 if ($value =~ / ^ -? \d+ (?: [\/.] \d+ )? $ /x) {
773 is(prop_value_aliases($mod_prop, $mod_value), undef, "prop_value_aliases('$mod_prop', '$mod_value') returns undef because '$mod_value' should be strictly matched");
774
775 # And reset so below tests just the strict matching.
776 $mod_value = $value;
777 }
778
779 if ($count == 0) {
780
781 @names_via_short = prop_value_aliases($mod_prop, $mod_value);
782
783 # If the 0th test fails, no sense in continuing with the others
784 last unless is($names_via_short[0], $value, "prop_value_aliases: In '$prop', '$value' is the short name for '$mod_value'");
785 $short_name = $value;
786 }
787 elsif ($count == 1) {
788
789 # Some properties have the same short and full name; no sense
790 # repeating the test if the same.
791 if ($value ne $short_name) {
792 my @names_via_full =
793 prop_value_aliases($mod_prop, $mod_value);
794 is_deeply(\@names_via_full, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
795 }
796
797 # Tests scalar context
798 is(prop_value_aliases($prop, $short_name), $value, "'$value' is the long name for prop_value_aliases('$prop', '$short_name')");
799 }
800 else {
801 my @all_names = prop_value_aliases($mod_prop, $mod_value);
802 is_deeply(\@all_names, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
e72b6605 803 ok((grep { &utf8::_loose_name(lc $_) eq &utf8::_loose_name(lc $value) } prop_value_aliases($prop, $short_name)), "'$value' is listed as an alias for prop_value_aliases('$prop', '$short_name')");
7ef25837
KW
804 }
805
e72b6605 806 $pva_tested{&utf8::_loose_name(lc $prop) . "=" . &utf8::_loose_name(lc $value)} = 1;
7ef25837
KW
807 $count++;
808 }
809}
810
811# And test as best we can, the non-official pva's that mktables generates.
812foreach my $hash (\%utf8::loose_to_file_of, \%utf8::stricter_to_file_of) {
813 foreach my $test (keys %$hash) {
814 next if exists $pva_tested{$test}; # Skip if already tested
815
816 my ($prop, $value) = split "=", $test;
817 next unless defined $value; # prop_value_aliases() requires an input
818 # 'value'
819 my $mod_value;
820 if ($hash == \%utf8::loose_to_file_of) {
821
822 # Add extra characters to test loose-match rhs value
823 $mod_value = "$extra_chars$value";
824 }
825 else { # Here value is strictly matched.
826
827 # Extra elements are added by mktables to this hash so that
828 # something like "age=6.0" has a synonym of "age=6". It's not
829 # clear to me (khw) if we should be encouraging those synonyms, so
830 # don't test for them.
831 next if $value !~ /\D/ && exists $hash->{"$prop=$value.0"};
832
833 # Verify that loose matching fails when only strict is called for.
834 next unless is(prop_value_aliases($prop, "$extra_chars$value"), undef,
835 "prop_value_aliases('$prop', '$extra_chars$value') returns undef since '$value' should be strictly matched"),
836
837 # Strict matching does allow for underscores between digits. Test
838 # for that.
839 $mod_value = $value;
840 while ($mod_value =~ s/(\d)(\d)/$1_$2/g) {}
841 }
842
843 # The lhs property is always loosely matched, so add in extra
844 # characters to test that.
845 my $mod_prop = "$extra_chars$prop";
846
847 if ($prop eq 'gc' && $value =~ /l[_&]$/) {
848 # These two names are special in that they don't appear in the
849 # returned list because they are discouraged from use. Verify
850 # that they return the same list as a non-discouraged version.
851 my @LC = prop_value_aliases('gc', 'lc');
852 my @l_ = prop_value_aliases($mod_prop, $mod_value);
853 is_deeply(\@l_, \@LC, "prop_value_aliases('$mod_prop', '$mod_value) returns the same list as prop_value_aliases('gc', 'lc')");
854 }
855 else {
e72b6605 856 ok((grep { &utf8::_loose_name(lc $_) eq &utf8::_loose_name(lc $value) }
7ef25837
KW
857 prop_value_aliases($mod_prop, $mod_value)),
858 "'$value' is listed as an alias for prop_value_aliases('$mod_prop', '$mod_value')");
859 }
860 }
861}
862
863undef %pva_tested;
864
681d705c
KW
865no warnings 'once'; # We use some values once from 'required' modules.
866
62b3b855
KW
867use Unicode::UCD qw(prop_invlist prop_invmap MAX_CP);
868
869# There were some problems with caching interfering with prop_invlist() vs
870# prop_invmap() on binary properties, and also between the 3 properties where
871# Perl used the same 'To' name as another property (see utf8_heavy.pl).
872# So, before testing all of prop_invlist(),
873# 1) call prop_invmap() to try both orders of these name issues. This uses
874# up two of the 3 properties; the third will be left so that invlist()
875# on it gets called before invmap()
876# 2) call prop_invmap() on a generic binary property, ahead of invlist().
877# This should test that the caching works in both directions.
878
879# These properties are not stable between Unicode versions, but the first few
880# elements are; just look at the first element to see if are getting the
881# distinction right. The general inversion map testing below will test the
882# whole thing.
883my $prop = "uc";
884my ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
d11155ec 885is($format, 'al', "prop_invmap() format of '$prop' is 'al'");
bf7fe2df 886is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
62b3b855 887is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
d11155ec 888is($invmap_ref->[1], 0x41, "prop_invmap('$prop') map[1] is 0x41");
62b3b855
KW
889
890$prop = "upper";
891($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
d11155ec
KW
892is($format, 's', "prop_invmap() format of '$prop' is 's");
893is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
62b3b855
KW
894is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
895is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
896
897$prop = "lower";
898($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
d11155ec
KW
899is($format, 's', "prop_invmap() format of '$prop' is 's'");
900is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
62b3b855
KW
901is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
902is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
903
904$prop = "lc";
905($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
d11155ec 906is($format, 'al', "prop_invmap() format of '$prop' is 'al'");
bf7fe2df 907is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
62b3b855 908is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
d11155ec 909is($invmap_ref->[1], 0x61, "prop_invmap('$prop') map[1] is 0x61");
62b3b855
KW
910
911# This property is stable and small, so can test all of it
912$prop = "ASCII_Hex_Digit";
913($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
914is($format, 's', "prop_invmap() format of '$prop' is 's'");
915is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
916is_deeply($invlist_ref, [ 0x0000, 0x0030, 0x003A, 0x0041,
917 0x0047, 0x0061, 0x0067, 0x110000 ],
918 "prop_invmap('$prop') code point list is correct");
919is_deeply($invmap_ref, [ 'N', 'Y', 'N', 'Y', 'N', 'Y', 'N', 'N' ] ,
920 "prop_invmap('$prop') map list is correct");
681d705c
KW
921
922is(prop_invlist("Unknown property"), undef, "prop_invlist(<Unknown property>) returns undef");
923is(prop_invlist(undef), undef, "prop_invlist(undef) returns undef");
924is(prop_invlist("Any"), 2, "prop_invlist('Any') returns the number of elements in scalar context");
925my @invlist = prop_invlist("Is_Any");
926is_deeply(\@invlist, [ 0, 0x110000 ], "prop_invlist works on 'Is_' prefixes");
927is(prop_invlist("Is_Is_Any"), undef, "prop_invlist('Is_Is_Any') returns <undef> since two is's");
928
929use Storable qw(dclone);
930
931is(prop_invlist("InKana"), undef, "prop_invlist(<user-defined property returns undef>)");
932
62b3b855 933# The way both the tests for invlist and invmap work is that they take the
681d705c
KW
934# lists returned by the functions and construct from them what the original
935# file should look like, which are then compared with the file. If they are
936# identical, the test passes. What this tests isn't that the results are
62b3b855 937# correct, but that invlist and invmap haven't introduced errors beyond what
681d705c
KW
938# are there in the files. As a small hedge against that, test some
939# prop_invlist() tables fully with the known correct result. We choose
940# ASCII_Hex_Digit again, as it is stable.
941@invlist = prop_invlist("AHex");
942is_deeply(\@invlist, [ 0x0030, 0x003A, 0x0041,
943 0x0047, 0x0061, 0x0067 ],
944 "prop_invlist('AHex') is exactly the expected set of points");
945@invlist = prop_invlist("AHex=f");
946is_deeply(\@invlist, [ 0x0000, 0x0030, 0x003A, 0x0041,
947 0x0047, 0x0061, 0x0067 ],
948 "prop_invlist('AHex=f') is exactly the expected set of points");
949
950sub fail_with_diff ($$$$) {
951 # For use below to output better messages
952 my ($prop, $official, $constructed, $tested_function_name) = @_;
953
954 is($constructed, $official, "$tested_function_name('$prop')");
955 diag("Comment out lines " . (__LINE__ - 1) . " through " . (__LINE__ + 1) . " in '$0' on Un*x-like systems to see just the differences. Uses the 'diff' first in your \$PATH");
956 return;
957
958 fail("$tested_function_name('$prop')");
959
960 require File::Temp;
961 my $off = File::Temp->new();
962 chomp $official;
963 print $off $official, "\n";
964 close $off || die "Can't close official";
965
966 chomp $constructed;
967 my $gend = File::Temp->new();
968 print $gend $constructed, "\n";
969 close $gend || die "Can't close gend";
970
971 my $diff = File::Temp->new();
972 system("diff $off $gend > $diff");
973
974 open my $fh, "<", $diff || die "Can't open $diff";
975 my @diffs = <$fh>;
976 diag("In the diff output below '<' marks lines from the filesystem tables;\n'>' are from $tested_function_name()");
977 diag(@diffs);
978}
979
980my %tested_invlist;
981
982# Look at everything we think that mktables tells us exists, both loose and
983# strict
984foreach my $set_of_tables (\%utf8::stricter_to_file_of, \%utf8::loose_to_file_of)
985{
986 foreach my $table (keys %$set_of_tables) {
987
988 my $mod_table;
989 my ($prop_only, $value) = split "=", $table;
990 if (defined $value) {
991
992 # If this is to be loose matched, add in characters to test that.
993 if ($set_of_tables == \%utf8::loose_to_file_of) {
994 $value = "$extra_chars$value";
995 }
996 else { # Strict match
997
998 # Verify that loose matching fails when only strict is called
999 # for.
1000 next unless is(prop_invlist("$prop_only=$extra_chars$value"), undef, "prop_invlist('$prop_only=$extra_chars$value') returns undef since should be strictly matched");
1001
1002 # Strict matching does allow for underscores between digits.
1003 # Test for that.
1004 while ($value =~ s/(\d)(\d)/$1_$2/g) {}
1005 }
1006
1007 # The property portion in compound form specifications always
1008 # matches loosely
1009 $mod_table = "$extra_chars$prop_only = $value";
1010 }
1011 else { # Single-form.
1012
1013 # Like above, use looose if required, and insert underscores
1014 # between digits if strict.
1015 if ($set_of_tables == \%utf8::loose_to_file_of) {
1016 $mod_table = "$extra_chars$table";
1017 }
1018 else {
1019 $mod_table = $table;
1020 while ($mod_table =~ s/(\d)(\d)/$1_$2/g) {}
1021 }
1022 }
1023
1024 my @tested = prop_invlist($mod_table);
1025 if ($table =~ /^_/) {
1026 is(@tested, 0, "prop_invlist('$mod_table') returns an empty list since is internal-only");
1027 next;
1028 }
1029
1030 # If we have already tested a property that uses the same file, this
1031 # list should be identical to the one that was tested, and can bypass
1032 # everything else.
1033 my $file = $set_of_tables->{$table};
1034 if (exists $tested_invlist{$file}) {
1035 is_deeply(\@tested, $tested_invlist{$file}, "prop_invlist('$mod_table') gave same results as its name synonym");
1036 next;
1037 }
1038 $tested_invlist{$file} = dclone \@tested;
1039
1040 # A leading '!' in the file name means that it is to be inverted.
1041 my $invert = $file =~ s/^!//;
1042 my $official = do "unicore/lib/$file.pl";
1043
1044 # Get rid of any trailing space and comments in the file.
1045 $official =~ s/\s*(#.*)?$//mg;
1046 chomp $official;
1047
1048 # If we are to test against an inverted file, it is easier to invert
1049 # our array than the file.
1050 # The file only is valid for Unicode code points, while the inversion
1051 # list is valid for all possible code points. Therefore, we must test
1052 # just the Unicode part against the file. Later we will test for
1053 # the non-Unicode part.
1054
1055 my $before_invert; # Saves the pre-inverted table.
1056 if ($invert) {
1057 $before_invert = dclone \@tested;
1058 if (@tested && $tested[0] == 0) {
1059 shift @tested;
1060 } else {
1061 unshift @tested, 0;
1062 }
1063 if (@tested && $tested[-1] == 0x110000) {
1064 pop @tested;
1065 }
1066 else {
1067 push @tested, 0x110000;
1068 }
1069 }
1070
1071 # Now construct a string from the list that should match the file.
1072 # The file gives ranges of code points with starting and ending values
1073 # in hex, like this:
1074 # 0041\t005A
1075 # 0061\t007A
1076 # 00AA
1077 # Our list has even numbered elements start ranges that are in the
1078 # list, and odd ones that aren't in the list. Therefore the odd
1079 # numbered ones are one beyond the end of the previous range, but
1080 # otherwise don't get reflected in the file.
1081 my $tested = "";
1082 my $i = 0;
1083 for (; $i < @tested - 1; $i += 2) {
1084 my $start = $tested[$i];
1085 my $end = $tested[$i+1] - 1;
1086 if ($start == $end) {
1087 $tested .= sprintf("%04X\n", $start);
1088 }
1089 else {
1090 $tested .= sprintf "%04X\t%04X\n", $start, $end;
1091 }
1092 }
1093
1094 # As mentioned earlier, the disk files only go up through Unicode,
1095 # whereas the prop_invlist() ones go as high as necessary. The
1096 # comparison is only valid through max Unicode.
1097 if ($i == @tested - 1 && $tested[$i] <= 0x10FFFF) {
1098 $tested .= sprintf("%04X\t10FFFF\n", $tested[$i]);
1099 }
1100 chomp $tested;
1101 if ($tested ne $official) {
1102 fail_with_diff($mod_table, $official, $tested, "prop_invlist");
1103 next;
1104 }
1105
1106 # Here, it matched the table. Now need to check for if it is correct
1107 # for beyond Unicode. First, calculate if is the default table or
1108 # not. This is the same algorithm as used internally in
1109 # prop_invlist(), so if it is wrong there, this test won't catch it.
1110 my $prop = lc $table;
1111 ($prop_only, $table) = split /\s*[:=]\s*/, $prop;
1112 if (defined $table) {
1113
1114 # May have optional prefixed 'is'
e72b6605 1115 $prop = &utf8::_loose_name($prop_only) =~ s/^is//r;
681d705c 1116 $prop = $utf8::loose_property_name_of{$prop};
e72b6605 1117 $prop .= "=" . &utf8::_loose_name($table);
681d705c
KW
1118 }
1119 else {
e72b6605 1120 $prop = &utf8::_loose_name($prop);
681d705c
KW
1121 }
1122 my $is_default = exists $Unicode::UCD::loose_defaults{$prop};
1123
1124 @tested = @$before_invert if $invert; # Use the original
1125 if (@tested % 2 == 0) {
1126
1127 # If there are an even number of elements, the final one starts a
1128 # range (going to infinity) of code points that are not in the
1129 # list.
1130 if ($is_default) {
1131 fail("prop_invlist('$mod_table')");
1132 diag("default table doesn't goto infinity");
1133 use Data::Dumper;
1134 diag Dumper \@tested;
1135 next;
1136 }
1137 }
1138 else {
1139 # An odd number of elements means the final one starts a range
1140 # (going to infinity of code points that are in the list.
1141 if (! $is_default) {
1142 fail("prop_invlist('$mod_table')");
1143 diag("non-default table needs to stop in the Unicode range");
1144 use Data::Dumper;
1145 diag Dumper \@tested;
1146 next;
1147 }
1148 }
1149
1150 pass("prop_invlist('$mod_table')");
1151 }
1152}
1153
62b3b855
KW
1154# Now test prop_invmap().
1155
1156@list = prop_invmap("Unknown property");
1157is (@list, 0, "prop_invmap(<Unknown property>) returns an empty list");
1158@list = prop_invmap(undef);
1159is (@list, 0, "prop_invmap(undef) returns an empty list");
1160ok (! eval "prop_invmap('gc')" && $@ ne "",
1161 "prop_invmap('gc') dies in scalar context");
1162@list = prop_invmap("_X_Begin");
1163is (@list, 0, "prop_invmap(<internal property>) returns an empty list");
1164@list = prop_invmap("InKana");
1165is(@list, 0, "prop_invmap(<user-defined property returns undef>)");
1166@list = prop_invmap("Perl_Decomposition_Mapping"), undef,
1167is(@list, 0, "prop_invmap('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
1168@list = prop_invmap("Perl_Charnames"), undef,
1169is(@list, 0, "prop_invmap('Perl_Charnames') returns <undef> since internal-Perl-only");
1170@list = prop_invmap("Is_Is_Any");
1171is(@list, 0, "prop_invmap('Is_Is_Any') returns <undef> since two is's");
1172
1173# The set of properties to test on has already been compiled into %props by
1174# the prop_aliases() tests.
1175
1176my %tested_invmaps;
1177
1178# Like prop_invlist(), prop_invmap() is tested by comparing the results
1179# returned by the function with the tables that mktables generates. Some of
1180# these tables are directly stored as files on disk, in either the unicore or
1181# unicore/To directories, and most should be listed in the mktables generated
1182# hash %utf8::loose_property_to_file_of, with a few additional ones that this
1183# handles specially. For these, the files are read in directly, massaged, and
1184# compared with what invmap() returns. The SPECIALS hash in some of these
1185# files overrides values in the main part of the file.
1186#
1187# The other properties are tested indirectly by generating all the possible
1188# inversion lists for the property, and seeing if those match the inversion
1189# lists returned by prop_invlist(), which has already been tested.
1190
1191PROPERTY:
1192foreach my $prop (keys %props) {
e72b6605 1193 my $loose_prop = &utf8::_loose_name(lc $prop);
62b3b855
KW
1194 my $suppressed = grep { $_ eq $loose_prop }
1195 @Unicode::UCD::suppressed_properties;
1196
1197 # Find the short and full names that this property goes by
1198 my ($name, $full_name) = prop_aliases($prop);
1199 if (! $name) {
1200 if (! $suppressed) {
1201 fail("prop_invmap('$prop')");
1202 diag("is unknown to prop_aliases(), and we need it in order to test prop_invmap");
1203 }
1204 next PROPERTY;
1205 }
1206
1207 # Normalize the short name, as it is stored in the hashes under the
1208 # normalized version.
e72b6605 1209 $name = &utf8::_loose_name(lc $name);
62b3b855
KW
1210
1211 # Add in the characters that are supposed to be ignored to test loose
1212 # matching, which the tested function applies to all properties
1213 my $mod_prop = "$extra_chars$prop";
1214
1215 my ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($mod_prop);
1216 my $return_ref = [ $invlist_ref, $invmap_ref, $format, $missing ];
1217
1218 # If have already tested this property under a different name, merely
1219 # compare the return from now with the saved one from before.
1220 if (exists $tested_invmaps{$name}) {
1221 is_deeply($return_ref, $tested_invmaps{$name}, "prop_invmap('$mod_prop') gave same results as its synonym, '$name'");
1222 next PROPERTY;
1223 }
1224 $tested_invmaps{$name} = dclone $return_ref;
1225
1226 # If prop_invmap() returned nothing, is ok iff is a property whose file is
1227 # not generated.
1228 if ($suppressed) {
1229 if (defined $format) {
1230 fail("prop_invmap('$mod_prop')");
1231 diag("did not return undef for suppressed property $prop");
1232 }
1233 next PROPERTY;
1234 }
1235 elsif (!defined $format) {
1236 fail("prop_invmap('$mod_prop')");
1237 diag("'$prop' is unknown to prop_invmap()");
1238 next PROPERTY;
1239 }
1240
1241 # The two parallel arrays must have the same number of elements.
1242 if (@$invlist_ref != @$invmap_ref) {
1243 fail("prop_invmap('$mod_prop')");
1244 diag("invlist has "
1245 . scalar @$invlist_ref
1246 . " while invmap has "
1247 . scalar @$invmap_ref
1248 . " elements");
1249 next PROPERTY;
1250 }
1251
1252 # The last element must be for the above-Unicode code points, and must be
1253 # for the default value.
1254 if ($invlist_ref->[-1] != 0x110000) {
1255 fail("prop_invmap('$mod_prop')");
1256 diag("The last inversion list element is not 0x110000");
1257 next PROPERTY;
1258 }
1259 if ($invmap_ref->[-1] ne $missing) {
1260 fail("prop_invmap('$mod_prop')");
1261 diag("The last inversion list element is '$invmap_ref->[-1]', and should be '$missing'");
1262 next PROPERTY;
1263 }
1264
1265 if ($name eq 'bmg') { # This one has an atypical $missing
1266 if ($missing ne "") {
1267 fail("prop_invmap('$mod_prop')");
1268 diag("The missings should be \"\"; got '$missing'");
1269 next PROPERTY;
1270 }
1271 }
4f143a72 1272 elsif ($format =~ /^ a (?!r) /x) {
b0b13ada
KW
1273 if ($full_name eq 'Perl_Decimal_Digit') {
1274 if ($missing ne "") {
1275 fail("prop_invmap('$mod_prop')");
1276 diag("The missings should be \"\"; got '$missing'");
1277 next PROPERTY;
1278 }
1279 }
1280 elsif ($missing ne "0") {
bf7fe2df
KW
1281 fail("prop_invmap('$mod_prop')");
1282 diag("The missings should be '0'; got '$missing'");
1283 next PROPERTY;
1284 }
1285 }
62b3b855
KW
1286 elsif ($missing =~ /[<>]/) {
1287 fail("prop_invmap('$mod_prop')");
1288 diag("The missings should NOT be something with <...>'");
1289 next PROPERTY;
1290
1291 # I don't want to hard code in what all the missings should be, so
1292 # those don't get fully tested.
1293 }
1294
1295 # Certain properties don't have their own files, but must be constructed
1296 # using proxies.
1297 my $proxy_prop = $name;
1298 if ($full_name eq 'Present_In') {
1299 $proxy_prop = "age"; # The maps for these two props are identical
1300 }
1301 elsif ($full_name eq 'Simple_Case_Folding'
1302 || $full_name =~ /Simple_ (.) .*? case_Mapping /x)
1303 {
1304 if ($full_name eq 'Simple_Case_Folding') {
1305 $proxy_prop = 'cf';
1306 }
1307 else {
1308 # We captured the U, L, or T, leading to uc, lc, or tc.
1309 $proxy_prop = lc $1 . "c";
1310 }
d11155ec 1311 if ($format ne "a") {
62b3b855 1312 fail("prop_invmap('$mod_prop')");
d11155ec 1313 diag("The format should be 'a'; got '$format'");
62b3b855
KW
1314 next PROPERTY;
1315 }
1316 }
1317
5bbfa552
KW
1318 if ($format !~ / ^ (?: a [der]? | ale? | n | sl? ) $ /x) {
1319 fail("prop_invmap('$mod_prop')");
1320 diag("Unknown format '$format'");
1321 next PROPERTY;
1322 }
1323
62b3b855
KW
1324 my $base_file;
1325 my $official;
1326
1327 # Handle the properties that have full disk files for them (except the
1328 # Name property which is structurally enough different that it is handled
1329 # separately below.)
1330 if ($name ne 'na'
1331 && ($name eq 'blk'
1332 || defined
1333 ($base_file = $utf8::loose_property_to_file_of{$proxy_prop})
1334 || exists $utf8::loose_to_file_of{$proxy_prop}
1335 || $name eq "dm"))
1336 {
1337 # In the above, blk is done unconditionally, as we need to test that
1338 # the old-style block names are returned, even if mktables has
1339 # generated a file for the new-style; the test for dm comes afterward,
1340 # so that if a file has been generated for it explicitly, we use that
1341 # file (which is valid, unlike blk) instead of the combo
1342 # Decomposition.pl files.
1343 my $file;
1344 my $is_binary = 0;
1345 if ($name eq 'blk') {
1346
1347 # The blk property is special. The original file with old block
1348 # names is retained, and the default is to not write out a
1349 # new-name file. What we do is get the old names into a data
1350 # structure, and from that create what the new file would look
1351 # like. $base_file is needed to be defined, just to avoid a
1352 # message below.
1353 $base_file = "This is a dummy name";
1354 my $blocks_ref = charblocks();
1355 $official = "";
1356 for my $range (sort { $a->[0][0] <=> $b->[0][0] }
1357 values %$blocks_ref)
1358 {
1359 # Translate the charblocks() data structure to what the file
1360 # would like.
1361 $official .= sprintf"%04X\t%04X\t%s\n",
1362 $range->[0][0],
1363 $range->[0][1],
1364 $range->[0][2];
1365 }
1366 }
1367 else {
d11155ec 1368 $base_file = "Decomposition" if $format eq 'ad';
62b3b855
KW
1369
1370 # Above leaves $base_file undefined only if it came from the hash
1371 # below. This should happen only when it is a binary property
1372 # (and are accessing via a single-form name, like 'In_Latin1'),
1373 # and so it is stored in a different directory than the To ones.
1374 # XXX Currently, the only cases where it is complemented are the
1375 # ones that have no code points. And it works out for these that
1376 # 1) complementing them, and then 2) adding or subtracting the
1377 # initial 0 and final 110000 cancel each other out. But further
1378 # work would be needed in the unlikely event that an inverted
1379 # property comes along without these characteristics
1380 if (!defined $base_file) {
1381 $base_file = $utf8::loose_to_file_of{$proxy_prop};
1382 $is_binary = ($base_file =~ s/^!//) ? -1 : 1;
1383 $base_file = "lib/$base_file";
1384 }
1385
1386 # Read in the file
62b3b855
KW
1387 $file = "unicore/$base_file.pl";
1388 $official = do $file;
1389
1390 # Get rid of any trailing space and comments in the file.
1391 $official =~ s/\s*(#.*)?$//mg;
1392
d11155ec 1393 if ($format eq 'ad') {
bea2c146
KW
1394 my @official = split /\n/, $official;
1395 $official = "";
1396 foreach my $line (@official) {
1397 my ($start, $end, $value)
1398 = $line =~ / ^ (.+?) \t (.*?) \t (.+?)
1399 \s* ( \# .* )? $ /x;
1400 # Decomposition.pl also has the <compatible> types in it,
1401 # which should be removed.
1402 $value =~ s/<.*?> //;
1403 $official .= "$start\t\t$value\n";
1404
1405 # If this is a multi-char range, we turn it into as many
1406 # single character ranges as necessary. This makes things
1407 # easier below.
1408 if ($end ne "") {
1409 for my $i (hex($start) + 1 .. hex $end) {
1410 $official .= sprintf "%04X\t\t%s\n", $i, $value;
1411 }
1412 }
1413 }
1414 }
62b3b855
KW
1415 }
1416 chomp $official;
1417
1418 # If there are any special elements, get a reference to them.
bf7fe2df
KW
1419 my $swash_name = $utf8::file_to_swash_name{$base_file};
1420 my $specials_ref;
1421 if ($swash_name) {
1422 $specials_ref = $utf8::SwashInfo{$swash_name}{'specials_name'};
62b3b855
KW
1423 if ($specials_ref) {
1424
1425 # Convert from the name to the actual reference.
1426 no strict 'refs';
1427 $specials_ref = \%{$specials_ref};
1428 }
1429 }
1430
1431 # Certain of the proxy properties have to be adjusted to match the
1432 # real ones.
ae1bcb1f 1433 if ($full_name =~ /^(Case_Folding|(Lower|Title|Upper)case_Mapping)/) {
62b3b855
KW
1434
1435 # Here we have either
1436 # 1) Case_Folding; or
1437 # 2) a proxy that is a full mapping, which means that what the
1438 # real property is is the equivalent simple mapping.
1439 # In both cases, the file will have a standard list containing
1440 # simple mappings (to a single code point), and a specials hash
1441 # which contains all the mappings that are to multiple code
1442 # points. First, extract a list containing all the file's simple
1443 # mappings.
1444 my @list;
1445 for (split "\n", $official) {
1446 my ($start, $end, $value) = / ^ (.+?) \t (.*?) \t (.+?)
bea2c146 1447 \s* ( \# .* )? $ /x;
62b3b855 1448 $end = $start if $end eq "";
bf7fe2df 1449 push @list, [ hex $start, hex $end, $value ];
62b3b855
KW
1450 }
1451
ae1bcb1f 1452 # For these mappings, the file contains all the simple mappings,
62b3b855 1453 # including the ones that are overridden by the specials. These
ae1bcb1f 1454 # need to be removed as the list is for just the full ones.
62b3b855
KW
1455
1456 # Go through any special mappings one by one. They are packed.
1457 my $i = 0;
1458 foreach my $utf8_cp (sort keys %$specials_ref) {
1459 my $cp = unpack("C0U", $utf8_cp);
1460
62b3b855
KW
1461 # Find the spot in the @list of simple mappings that this
1462 # special applies to; uses a linear search.
1463 while ($i < @list -1 ) {
bea2c146 1464 last if $cp <= $list[$i][1];
62b3b855
KW
1465 $i++;
1466 }
1467
bea2c146
KW
1468 # Here $i is such that it points to the first range which ends
1469 # at or above cp, and hence is the only range that could
1470 # possibly contain it.
1471
1472 # If not in this range, no range contains it: nothing to
1473 # remove.
1474 next if $cp < $list[$i][0];
1475
1476 # Otherwise, remove the existing entry. If it is the first
1477 # element of the range...
1478 if ($cp == $list[$i][0]) {
62b3b855 1479
bea2c146
KW
1480 # ... and there are other elements in the range, just shorten
1481 # the range to exclude this code point.
1482 if ($list[$i][1] > $list[$i][0]) {
1483 $list[$i][0]++;
1484 }
62b3b855 1485
bea2c146
KW
1486 # ... but if it is the only element in the range, remove
1487 # it entirely.
1488 else {
1489 splice @list, $i, 1;
1490 }
1491 }
1492 else { # Is somewhere in the middle of the range
1493 # Split the range into two, excluding this one in the
1494 # middle
1495 splice @list, $i, 1,
1496 [ $list[$i][0], $cp - 1, $list[$i][2] ],
1497 [ $cp + 1, $list[$i][1], $list[$i][2] ];
1498 }
62b3b855
KW
1499 }
1500
1501 # Here, have gone through all the specials, modifying @list as
1502 # needed. Turn it back into what the file should look like.
bf7fe2df
KW
1503 $official = "";
1504 for my $element (@list) {
1505 $official .= "\n" if $official;
1506 if ($element->[1] == $element->[0]) {
1507 $official .= sprintf "%04X\t\t%s", $element->[0], $element->[2];
1508 }
1509 else {
1510 $official .= sprintf "%04X\t%04X\t%s", $element->[0], $element->[1], $element->[2];
1511 }
1512 }
62b3b855 1513 }
ae1bcb1f
KW
1514 elsif ($full_name =~ /Simple_(Case_Folding|(Lower|Title|Upper)case_Mapping)/)
1515 {
62b3b855 1516
ae1bcb1f 1517 # These properties have everything in the regular array, and the
62b3b855 1518 # specials are superfluous.
ae1bcb1f 1519 undef $specials_ref;
62b3b855 1520 }
bf7fe2df
KW
1521 elsif ($name eq 'bmg') {
1522
1523 # For this property, the file is output using hex notation for the
1524 # map, with all ranges equal to length 1. Convert from hex to
1525 # decimal.
1526 my @lines = split "\n", $official;
1527 foreach my $line (@lines) {
1528 my ($code_point, $map) = split "\t\t", $line;
1529 $line = $code_point . "\t\t" . hex $map;
1530 }
1531 $official = join "\n", @lines;
1532 }
62b3b855
KW
1533
1534 # Here, in $official, we have what the file looks like, or should like
1535 # if we've had to fix it up. Now take the invmap() output and reverse
1536 # engineer from that what the file should look like. Each iteration
1537 # appends the next line to the running string.
1538 my $tested_map = "";
1539
1540 # Create a copy of the file's specials hash. (It has been undef'd if
1541 # we know it isn't relevant to this property, so if it exists, it's an
1542 # error or is relevant). As we go along, we delete from that copy.
1543 # If a delete fails, or something is left over after we are done,
1544 # it's an error
1545 my %specials = %$specials_ref if $specials_ref;
1546
1547 # The extra -1 is because the final element has been tested above to
1548 # be for anything above Unicode. The file doesn't go that high.
bea2c146 1549 for (my $i = 0; $i < @$invlist_ref - 1; $i++) {
62b3b855
KW
1550
1551 # If the map element is a reference, have to stringify it (but
1552 # don't do so if the format doesn't allow references, so that an
1553 # improper format will generate an error.
1554 if (ref $invmap_ref->[$i]
d11155ec 1555 && ($format eq 'ad' || $format =~ /^ . l /x))
62b3b855 1556 {
3b6a8189 1557 # The stringification depends on the format.
62b3b855 1558 if ($format eq 'sl') {
3b6a8189
KW
1559
1560 # At the time of this writing, there are two types of 'sl'
1561 # format One, in Name_Alias, has multiple separate entries
1562 # for each code point; the other, in Script_Extension, is space
1563 # separated. Assume the latter for non-Name_Alias.
1564 if ($full_name ne 'Name_Alias') {
1565 $invmap_ref->[$i] = join " ", @{$invmap_ref->[$i]};
1566 }
1567 else {
1568 # For Name_Alias, we emulate the file. Entries with
1569 # just one value don't need any changes, but we
1570 # convert the list entries into a series of lines for
1571 # the file, starting with the first name. The
1572 # succeeding entries are on separate lines, with the
1573 # code point repeated for each one and then two tabs,
1574 # then the value. Code at the end of the loop will
1575 # set up the first line with its code point and two
1576 # tabs before the value, just as it does for every
1577 # other property; thus the special handling of the
1578 # first line.
1579 if (ref $invmap_ref->[$i]) {
1580 my $hex_cp = sprintf("%04X", $invlist_ref->[$i]);
1581 my $concatenated = $invmap_ref->[$i][0];
1582 for (my $j = 1; $j < @{$invmap_ref->[$i]}; $j++) {
1583 $concatenated .= "\n$hex_cp\t\t" . $invmap_ref->[$i][$j];
1584 }
1585 $invmap_ref->[$i] = $concatenated;
1586 }
1587 }
62b3b855 1588 }
d11155ec 1589 elsif ($format =~ / ^ al e? $/x) {
62b3b855 1590
d11155ec 1591 # For a al property, the stringified result should be in
62b3b855
KW
1592 # the specials hash. The key is the packed code point,
1593 # and the value is the packed map.
1594 my $value;
1595 if (! defined ($value = delete $specials{pack("C0U", $invlist_ref->[$i]) })) {
1596 fail("prop_invmap('$mod_prop')");
1597 diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
1598 next PROPERTY;
1599 }
1600 my $packed = pack "U*", @{$invmap_ref->[$i]};
1601 if ($value ne $packed) {
1602 fail("prop_invmap('$mod_prop')");
1603 diag(sprintf "For %04X, expected the mapping to be '$packed', but got '$value'");
1604 next PROPERTY;
1605 }
1606
1607 # As this doesn't get tested when we later compare with
1608 # the actual file, it could be out of order and we
1609 # wouldn't know it.
1610 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1611 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1612 {
1613 fail("prop_invmap('$mod_prop')");
1614 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1615 next PROPERTY;
1616 }
1617 next;
1618 }
d11155ec 1619 elsif ($format eq 'ad') {
62b3b855
KW
1620
1621 # The decomposition mapping file has the code points as
1622 # a string of space-separated hex constants.
1623 $invmap_ref->[$i] = join " ", map { sprintf "%04X", $_ } @{$invmap_ref->[$i]};
1624 }
1625 else {
1626 fail("prop_invmap('$mod_prop')");
1627 diag("Can't handle format '$format'");
1628 next PROPERTY;
1629 }
1630 }
d11155ec 1631 elsif ($format eq 'ad' || $format eq 'ale') {
34132297 1632
d11155ec
KW
1633 # The numerics in the returned map are stored as adjusted
1634 # decimal integers. The defaults are 0, and don't appear in
1635 # $official, and are excluded later, but the elements must be
1636 # converted back to their hex values before comparing with
34132297 1637 # $official, as these files, for backwards compatibility, are
d11155ec 1638 # not stored as adjusted. (There currently is only one ale
34132297 1639 # property, nfkccf. If that changed this would also have to.)
bea2c146
KW
1640 if ($invmap_ref->[$i] =~ / ^ -? \d+ $ /x
1641 && $invmap_ref->[$i] != 0)
1642 {
d11155ec
KW
1643 my $next = $invmap_ref->[$i] + 1;
1644 $invmap_ref->[$i] = sprintf("%04X", $invmap_ref->[$i]);
1645
1646 # If there are other elements in this range they need to
1647 # be adjusted; they must individually be re-mapped. Do
1648 # this by splicing in a new element into the list and the
1649 # map containing the remainder of the range. Next time
1650 # through we will look at that (possibly splicing again
1651 # until the whole range is processed).
bea2c146
KW
1652 if ($invlist_ref->[$i+1] > $invlist_ref->[$i] + 1) {
1653 splice @$invlist_ref, $i+1, 0,
1654 $invlist_ref->[$i] + 1;
d11155ec 1655 splice @$invmap_ref, $i+1, 0, $next;
bea2c146
KW
1656 }
1657 }
d11155ec 1658 if ($format eq 'ale' && $invmap_ref->[$i] eq "") {
62b3b855 1659
d11155ec 1660 # ale properties have maps to the empty string that also
4066e594
KW
1661 # should be in the specials hash, with the key the packed
1662 # code point, and the map just empty.
1663 my $value;
1664 if (! defined ($value = delete $specials{pack("C0U", $invlist_ref->[$i]) })) {
1665 fail("prop_invmap('$mod_prop')");
1666 diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
1667 next PROPERTY;
1668 }
1669 if ($value ne "") {
1670 fail("prop_invmap('$mod_prop')");
1671 diag(sprintf "For %04X, expected the mapping to be \"\", but got '$value'", $invlist_ref->[$i]);
1672 next PROPERTY;
1673 }
62b3b855 1674
4066e594
KW
1675 # As this doesn't get tested when we later compare with
1676 # the actual file, it could be out of order and we
1677 # wouldn't know it.
1678 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1679 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1680 {
1681 fail("prop_invmap('$mod_prop')");
1682 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1683 next PROPERTY;
1684 }
1685 next;
34132297 1686 }
62b3b855
KW
1687 }
1688 elsif ($is_binary) { # These binary files don't have an explicit Y
1689 $invmap_ref->[$i] =~ s/Y//;
1690 }
1691
1692 # The file doesn't include entries that map to $missing, so don't
1693 # include it in the built-up string. But make sure that it is in
1694 # the correct order in the input.
1695 if ($invmap_ref->[$i] eq $missing) {
1696 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1697 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1698 {
1699 fail("prop_invmap('$mod_prop')");
1700 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1701 next PROPERTY;
1702 }
1703 next;
1704 }
1705
7f131b96
KW
1706 # The ad property has one entry which isn't in the file.
1707 # Ignore it, but make sure it is in order.
1708 if ($format eq 'ad'
1709 && $invmap_ref->[$i] eq '<hangul syllable>'
1710 && $invlist_ref->[$i] == 0xAC00)
1711 {
1712 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1713 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
62b3b855 1714 {
7f131b96
KW
1715 fail("prop_invmap('$mod_prop')");
1716 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1717 next PROPERTY;
62b3b855 1718 }
7f131b96
KW
1719 next;
1720 }
62b3b855
KW
1721
1722 # Finally have figured out what the map column in the file should
1723 # be. Append the line to the running string.
1724 my $start = $invlist_ref->[$i];
1725 my $end = $invlist_ref->[$i+1] - 1;
1726 $end = ($start == $end) ? "" : sprintf("%04X", $end);
1727 if ($invmap_ref->[$i] ne "") {
1728 $tested_map .= sprintf "%04X\t%s\t%s\n", $start, $end, $invmap_ref->[$i];
1729 }
1730 elsif ($end ne "") {
1731 $tested_map .= sprintf "%04X\t%s\n", $start, $end;
1732 }
1733 else {
1734 $tested_map .= sprintf "%04X\n", $start;
1735 }
1736 } # End of looping over all elements.
1737
1738 # Here are done with generating what the file should look like
1739
1740 chomp $tested_map;
1741
1742 # And compare.
1743 if ($tested_map ne $official) {
1744 fail_with_diff($mod_prop, $official, $tested_map, "prop_invmap");
1745 next PROPERTY;
1746 }
1747
1748 # There shouldn't be any specials unaccounted for.
1749 if (keys %specials) {
1750 fail("prop_invmap('$mod_prop')");
1751 diag("Unexpected specials: " . join ", ", keys %specials);
1752 next PROPERTY;
1753 }
1754 }
1755 elsif ($format eq 'n') {
1756
1757 # Handle the Name property similar to the above. But the file is
1758 # sufficiently different that it is more convenient to make a special
3b6a8189
KW
1759 # case for it. It is a combination of the Name, Unicode1_Name, and
1760 # Name_Alias properties, and named sequences. We need to remove all
1761 # but the Name in order to do the comparison.
62b3b855
KW
1762
1763 if ($missing ne "") {
1764 fail("prop_invmap('$mod_prop')");
1765 diag("The missings should be \"\"; got \"missing\"");
1766 next PROPERTY;
1767 }
1768
1769 $official = do "unicore/Name.pl";
1770
1771 # Get rid of the named sequences portion of the file. These don't
1772 # have a tab before the first blank on a line.
1773 $official =~ s/ ^ [^\t]+ \ .*? \n //xmg;
1774
1775 # And get rid of the controls. These are named in the file, but
3b6a8189
KW
1776 # shouldn't be in the property. This gets rid of the two ranges in
1777 # one fell swoop, and also all the Unicode1_Name values that may not
1778 # be in Name_Alias.
62b3b855
KW
1779 $official =~ s/ 00000 \t .* 0001F .*? \n//xs;
1780 $official =~ s/ 0007F \t .* 0009F .*? \n//xs;
1781
3b6a8189
KW
1782 # And remove the aliases. We read in the Name_Alias property, and go
1783 # through them one by one.
1784 my ($aliases_code_points, $aliases_maps, undef, undef)
1785 = &prop_invmap('Name_Alias');
1786 for (my $i = 0; $i < @$aliases_code_points; $i++) {
1787 my $code_point = $aliases_code_points->[$i];
1788
1789 # Already removed these above.
1790 next if $code_point <= 0x1F
1791 || ($code_point >= 0x7F && $code_point <= 0x9F);
1792
1793 my $hex_code_point = sprintf "%05X", $code_point;
1794
1795 # Convert to a list if not already to make the following loop
1796 # control uniform.
1797 $aliases_maps->[$i] = [ $aliases_maps->[$i] ]
1798 if ! ref $aliases_maps->[$i];
1799
1800 # Remove each alias for this code point from the file
1801 foreach my $alias (@{$aliases_maps->[$i]}) {
1802
1803 # Remove the alias type from the entry, retaining just the name.
1804 $alias =~ s/:.*//;
1805
1806 $alias = quotemeta($alias);
1807 $official =~ s/$hex_code_point \t $alias \n //x;
62b3b855
KW
1808 }
1809 }
62b3b855
KW
1810 chomp $official;
1811
1812 # Here have adjusted the file. We also have to adjust the returned
1813 # inversion map by checking and deleting all the lines in it that
1814 # won't be in the file. These are the lines that have generated
1815 # things, like <hangul syllable>.
1816 my $tested_map = ""; # Current running string
1817 my @code_point_in_names =
1818 @Unicode::UCD::code_points_ending_in_code_point;
1819
1820 for my $i (0 .. @$invlist_ref - 1 - 1) {
1821 my $start = $invlist_ref->[$i];
1822 my $end = $invlist_ref->[$i+1] - 1;
1823 if ($invmap_ref->[$i] eq $missing) {
1824 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1825 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1826 {
1827 fail("prop_invmap('$mod_prop')");
1828 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1829 next PROPERTY;
1830 }
1831 next;
1832 }
1833 if ($invmap_ref->[$i] =~ / (.*) ( < .*? > )/x) {
1834 my $name = $1;
1835 my $type = $2;
1836 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1837 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1838 {
1839 fail("prop_invmap('$mod_prop')");
1840 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1841 next PROPERTY;
1842 }
1843 if ($type eq "<hangul syllable>") {
1844 if ($name ne "") {
1845 fail("prop_invmap('$mod_prop')");
1846 diag("Unexpected text in $invmap_ref->[$i]");
1847 next PROPERTY;
1848 }
1849 if ($start != 0xAC00) {
1850 fail("prop_invmap('$mod_prop')");
1851 diag(sprintf("<hangul syllables> should begin at 0xAC00, got %04X", $start));
1852 next PROPERTY;
1853 }
1854 if ($end != $start + 11172 - 1) {
1855 fail("prop_invmap('$mod_prop')");
1856 diag(sprintf("<hangul syllables> should end at %04X, got %04X", $start + 11172 -1, $end));
1857 next PROPERTY;
1858 }
1859 }
1860 elsif ($type ne "<code point>") {
1861 fail("prop_invmap('$mod_prop')");
1862 diag("Unexpected text '$type' in $invmap_ref->[$i]");
1863 next PROPERTY;
1864 }
1865 else {
1866
1867 # Look through the array of names that end in code points,
1868 # and look for this start and end. If not found is an
1869 # error. If found, delete it, and at the end, make sure
1870 # have deleted everything.
1871 for my $i (0 .. @code_point_in_names - 1) {
1872 my $hash = $code_point_in_names[$i];
1873 if ($hash->{'low'} == $start
1874 && $hash->{'high'} == $end
1875 && "$hash->{'name'}-" eq $name)
1876 {
1877 splice @code_point_in_names, $i, 1;
1878 last;
1879 }
1880 else {
1881 fail("prop_invmap('$mod_prop')");
1882 diag("Unexpected code-point-in-name line '$invmap_ref->[$i]'");
1883 next PROPERTY;
1884 }
1885 }
1886 }
1887
1888 next;
1889 }
1890
1891 # Have adjusted the map, as needed. Append to running string.
1892 $end = ($start == $end) ? "" : sprintf("%05X", $end);
1893 $tested_map .= sprintf "%05X\t%s\n", $start, $invmap_ref->[$i];
1894 }
1895
1896 # Finished creating the string from the inversion map. Can compare
1897 # with what the file is.
1898 chomp $tested_map;
1899 if ($tested_map ne $official) {
1900 fail_with_diff($mod_prop, $official, $tested_map, "prop_invmap");
1901 next PROPERTY;
1902 }
1903 if (@code_point_in_names) {
1904 fail("prop_invmap('$mod_prop')");
1905 use Data::Dumper;
1906 diag("Missing code-point-in-name line(s)" . Dumper \@code_point_in_names);
1907 next PROPERTY;
1908 }
1909 }
4f143a72 1910 elsif ($format eq 's') {
62b3b855
KW
1911
1912 # Here the map is not more or less directly from a file stored on
1913 # disk. We try a different tack. These should all be properties that
1914 # have just a few possible values (most of them are binary). We go
1915 # through the map list, sorting each range into buckets, one for each
1916 # map value. Thus for binary properties there will be a bucket for Y
1917 # and one for N. The buckets are inversion lists. We compare each
1918 # constructed inversion list with what we would get for it using
1919 # prop_invlist(), which has already been tested. If they all match,
1920 # the whole map must have matched.
1921 my %maps;
1922 my $previous_map;
1923
1924 # (The extra -1 is to not look at the final element in the loop, which
1925 # we know is the one that starts just beyond Unicode and goes to
1926 # infinity.)
1927 for my $i (0 .. @$invlist_ref - 1 - 1) {
1928 my $range_start = $invlist_ref->[$i];
1929
1930 # Because we are sorting into buckets, things could be
1931 # out-of-order here, and still be in the correct order in the
1932 # bucket, and hence wouldn't show up as an error; so have to
1933 # check.
1934 if (($i > 0 && $range_start <= $invlist_ref->[$i-1])
1935 || $range_start >= $invlist_ref->[$i+1])
1936 {
1937 fail("prop_invmap('$mod_prop')");
1938 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1939 next PROPERTY;
1940 }
1941
1942 # This new range closes out the range started in the previous
1943 # iteration.
1944 push @{$maps{$previous_map}}, $range_start if defined $previous_map;
1945
1946 # And starts a range which will be closed in the next iteration.
1947 $previous_map = $invmap_ref->[$i];
1948 push @{$maps{$previous_map}}, $range_start;
1949 }
1950
1951 # The range we just started hasn't been closed, and we didn't look at
1952 # the final element of the loop. If that range is for the default
1953 # value, it shouldn't be closed, as it is to extend to infinity. But
1954 # otherwise, it should end at the final Unicode code point, and the
1955 # list that maps to the default value should have another element that
1956 # does go to infinity for every above Unicode code point.
1957
1958 if (@$invlist_ref > 1) {
1959 my $penultimate_map = $invmap_ref->[-2];
1960 if ($penultimate_map ne $missing) {
1961
1962 # The -1th element contains the first non-Unicode code point.
1963 push @{$maps{$penultimate_map}}, $invlist_ref->[-1];
1964 push @{$maps{$missing}}, $invlist_ref->[-1];
1965 }
1966 }
1967
1968 # Here, we have the buckets (inversion lists) all constructed. Go
1969 # through each and verify that matches what prop_invlist() returns.
1970 # We could use is_deeply() for the comparison, but would get multiple
1971 # messages for each $prop.
1972 foreach my $map (keys %maps) {
1973 my @off_invlist = prop_invlist("$prop = $map");
1974 my $min = (@off_invlist >= @{$maps{$map}})
1975 ? @off_invlist
1976 : @{$maps{$map}};
1977 for my $i (0 .. $min- 1) {
1978 if ($i > @off_invlist - 1) {
1979 fail("prop_invmap('$mod_prop')");
1980 diag("There is no element [$i] for $prop=$map from prop_invlist(), while [$i] in the implicit one constructed from prop_invmap() is '$maps{$map}[$i]'");
1981 next PROPERTY;
1982 }
1983 elsif ($i > @{$maps{$map}} - 1) {
1984 fail("prop_invmap('$mod_prop')");
1985 diag("There is no element [$i] from the implicit $prop=$map constructed from prop_invmap(), while [$i] in the one from prop_invlist() is '$off_invlist[$i]'");
1986 next PROPERTY;
1987 }
1988 elsif ($maps{$map}[$i] ne $off_invlist[$i]) {
1989 fail("prop_invmap('$mod_prop')");
1990 diag("Element [$i] of the implicit $prop=$map constructed from prop_invmap() is '$maps{$map}[$i]', and the one from prop_invlist() is '$off_invlist[$i]'");
1991 next PROPERTY;
1992 }
1993 }
1994 }
1995 }
1996 else { # Don't know this property nor format.
1997
1998 fail("prop_invmap('$mod_prop')");
1999 diag("Unknown format '$format'");
2000 }
2001
2002 pass("prop_invmap('$mod_prop')");
2003}
2004
eaebe4db 2005done_testing();