This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
mktables: Generate some delta tables
[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
ST
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
ST
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
ST
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
376# Do different tests depending on if version <= 3.1, or not.
377(my $version = Unicode::UCD::UnicodeVersion) =~ /^(\d+)\.(\d+)/;
378if (defined $1 && ($1 <= 2 || $1 == 3 && defined $2 && $2 <= 1)) {
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');
472 my $n1 = @$r1;
7620cb10 473 is($n1, 30, "number of ranges in Latin script (Unicode 6.1.0)");
741297c1
JH
474 shift @$r1 while @$r1;
475 my $r2 = charscript('Latin');
476 is(@$r2, $n1, "modifying results should not mess up internal caches");
477}
478
c5a29f40
LM
479{
480 is(charinfo(0xdeadbeef), undef, "[perl #23273] warnings in Unicode::UCD");
2d3cf3ee 481}
a2bd7410
JH
482
483use Unicode::UCD qw(namedseq);
484
485is(namedseq("KATAKANA LETTER AINU P"), "\x{31F7}\x{309A}", "namedseq");
486is(namedseq("KATAKANA LETTER AINU Q"), undef);
487is(namedseq(), undef);
488is(namedseq(qw(foo bar)), undef);
489my @ns = namedseq("KATAKANA LETTER AINU P");
490is(scalar @ns, 2);
491is($ns[0], 0x31F7);
492is($ns[1], 0x309A);
493my %ns = namedseq();
494is($ns{"KATAKANA LETTER AINU P"}, "\x{31F7}\x{309A}");
495@ns = namedseq(42);
496is(@ns, 0);
497
7319f91d
KW
498use Unicode::UCD qw(num);
499use charnames ":full";
500
501is(num("0"), 0, 'Verify num("0") == 0');
502is(num("98765"), 98765, 'Verify num("98765") == 98765');
503ok(! defined num("98765\N{FULLWIDTH DIGIT FOUR}"), 'Verify num("98765\N{FULLWIDTH DIGIT FOUR}") isnt defined');
c4b3d89d
KW
504is(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');
505ok(! 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
506is(num("\N{CHAM DIGIT ZERO}\N{CHAM DIGIT THREE}"), 3, 'Verify num("\N{CHAM DIGIT ZERO}\N{CHAM DIGIT THREE}") == 3');
507ok(! defined num("\N{CHAM DIGIT ZERO}\N{JAVANESE DIGIT NINE}"), 'Verify num("\N{CHAM DIGIT ZERO}\N{JAVANESE DIGIT NINE}") isnt defined');
508is(num("\N{SUPERSCRIPT TWO}"), 2, 'Verify num("\N{SUPERSCRIPT TWO} == 2');
509is(num("\N{ETHIOPIC NUMBER TEN THOUSAND}"), 10000, 'Verify num("\N{ETHIOPIC NUMBER TEN THOUSAND}") == 10000');
510is(num("\N{NORTH INDIC FRACTION ONE HALF}"), .5, 'Verify num("\N{NORTH INDIC FRACTION ONE HALF}") == .5');
98025745 511is(num("\N{U+12448}"), 9, 'Verify num("\N{U+12448}") == 9');
eaebe4db 512
7ef25837
KW
513# Create a user-defined property
514sub InKana {<<'END'}
5153040 309F
51630A0 30FF
517END
518
519use Unicode::UCD qw(prop_aliases);
520
521is(prop_aliases(undef), undef, "prop_aliases(undef) returns <undef>");
522is(prop_aliases("unknown property"), undef,
523 "prop_aliases(<unknown property>) returns <undef>");
524is(prop_aliases("InKana"), undef,
525 "prop_aliases(<user-defined property>) returns <undef>");
526is(prop_aliases("Perl_Decomposition_Mapping"), undef, "prop_aliases('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
527is(prop_aliases("Perl_Charnames"), undef,
528 "prop_aliases('Perl_Charnames') returns <undef> since internal-Perl-only");
529is(prop_aliases("isgc"), undef,
530 "prop_aliases('isgc') returns <undef> since is not covered Perl extension");
531is(prop_aliases("Is_Is_Any"), undef,
532 "prop_aliases('Is_Is_Any') returns <undef> since two is's");
533
534require 'utf8_heavy.pl';
535require "unicore/Heavy.pl";
536
537# Keys are lists of properties. Values are defined if have been tested.
538my %props;
539
540# To test for loose matching, add in the characters that are ignored there.
541my $extra_chars = "-_ ";
542
543# The one internal property we accept
544$props{'Perl_Decimal_Digit'} = 1;
545my @list = prop_aliases("perldecimaldigit");
546is_deeply(\@list,
547 [ "Perl_Decimal_Digit",
548 "Perl_Decimal_Digit"
549 ], "prop_aliases('perldecimaldigit') returns Perl_Decimal_Digit as both short and full names");
550
551# Get the official Unicode property name synonyms and test them.
552open my $props, "<", "../lib/unicore/PropertyAliases.txt"
553 or die "Can't open Unicode PropertyAliases.txt";
ce066323 554$/ = "\n";
7ef25837
KW
555while (<$props>) {
556 s/\s*#.*//; # Remove comments
557 next if /^\s* $/x; # Ignore empty and comment lines
558
559 chomp;
560 my $count = 0; # 0th field in line is short name; 1th is long name
561 my $short_name;
562 my $full_name;
563 my @names_via_short;
564 foreach my $alias (split /\s*;\s*/) { # Fields are separated by
565 # semi-colons
566 # Add in the characters that are supposed to be ignored, to test loose
567 # matching, which the tested function does on all inputs.
568 my $mod_name = "$extra_chars$alias";
569
e72b6605 570 my $loose = &utf8::_loose_name(lc $alias);
7ef25837
KW
571
572 # Indicate we have tested this.
573 $props{$loose} = 1;
574
575 my @all_names = prop_aliases($mod_name);
576 if (grep { $_ eq $loose } @Unicode::UCD::suppressed_properties) {
577 is(@all_names, 0, "prop_aliases('$mod_name') returns undef since $alias is not installed");
578 next;
579 }
580 elsif (! @all_names) {
581 fail("prop_aliases('$mod_name')");
582 diag("'$alias' is unknown to prop_aliases()");
583 next;
584 }
585
586 if ($count == 0) { # Is short name
587
588 @names_via_short = prop_aliases($mod_name);
589
590 # If the 0th test fails, no sense in continuing with the others
591 last unless is($names_via_short[0], $alias,
592 "prop_aliases: '$alias' is the short name for '$mod_name'");
593 $short_name = $alias;
594 }
595 elsif ($count == 1) { # Is full name
596
597 # Some properties have the same short and full name; no sense
598 # repeating the test if the same.
599 if ($alias ne $short_name) {
600 my @names_via_full = prop_aliases($mod_name);
601 is_deeply(\@names_via_full, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
602 }
603
604 # Tests scalar context
605 is(prop_aliases($short_name), $alias,
606 "prop_aliases: '$alias' is the long name for '$short_name'");
607 }
608 else { # Is another alias
609 is_deeply(\@all_names, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
610 ok((grep { $_ =~ /^$alias$/i } @all_names),
611 "prop_aliases: '$alias' is listed as an alias for '$mod_name'");
612 }
613
614 $count++;
615 }
616}
617
618# Now test anything we can find that wasn't covered by the tests of the
619# official properties. We have no way of knowing if mktables omitted a Perl
620# extension or not, but we do the best we can from its generated lists
621
622foreach my $alias (keys %utf8::loose_to_file_of) {
623 next if $alias =~ /=/;
624 my $lc_name = lc $alias;
e72b6605 625 my $loose = &utf8::_loose_name($lc_name);
7ef25837
KW
626 next if exists $props{$loose}; # Skip if already tested
627 $props{$loose} = 1;
628 my $mod_name = "$extra_chars$alias"; # Tests loose matching
629 my @aliases = prop_aliases($mod_name);
e72b6605 630 my $found_it = grep { &utf8::_loose_name(lc $_) eq $lc_name } @aliases;
7ef25837
KW
631 if ($found_it) {
632 pass("prop_aliases: '$lc_name' is listed as an alias for '$mod_name'");
633 }
634 elsif ($lc_name =~ /l[_&]$/) {
635
636 # These two names are special in that they don't appear in the
637 # returned list because they are discouraged from use. Verify
638 # that they return the same list as a non-discouraged version.
639 my @LC = prop_aliases('Is_LC');
640 is_deeply(\@aliases, \@LC, "prop_aliases: '$lc_name' returns the same list as 'Is_LC'");
641 }
642 else {
643 my $stripped = $lc_name =~ s/^is//;
644
645 # Could be that the input includes a prefix 'is', which is rarely
646 # returned as an alias, so having successfully stripped it off above,
647 # try again.
648 if ($stripped) {
e72b6605 649 $found_it = grep { &utf8::_loose_name(lc $_) eq $lc_name } @aliases;
7ef25837
KW
650 }
651
652 # If that didn't work, it could be that it's a block, which is always
653 # returned with a leading 'In_' to avoid ambiguity. Try comparing
654 # with that stripped off.
655 if (! $found_it) {
e72b6605 656 $found_it = grep { &utf8::_loose_name(s/^In_(.*)/\L$1/r) eq $lc_name }
7ef25837
KW
657 @aliases;
658 # Could check that is a real block, but tests for invmap will
659 # likely pickup any errors, since this will be tested there.
660 $lc_name = "in$lc_name" if $found_it; # Change for message below
661 }
662 my $message = "prop_aliases: '$lc_name' is listed as an alias for '$mod_name'";
663 ($found_it) ? pass($message) : fail($message);
664 }
665}
666
667my $done_equals = 0;
668foreach my $alias (keys %utf8::stricter_to_file_of) {
669 if ($alias =~ /=/) { # Only test one case where there is an equals
670 next if $done_equals;
671 $done_equals = 1;
672 }
673 my $lc_name = lc $alias;
674 my @list = prop_aliases($alias);
675 if ($alias =~ /^_/) {
676 is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since it is internal_only");
677 }
678 elsif ($alias =~ /=/) {
679 is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since is illegal property name");
680 }
681 else {
682 ok((grep { lc $_ eq $lc_name } @list),
683 "prop_aliases: '$lc_name' is listed as an alias for '$alias'");
684 }
685}
686
687use Unicode::UCD qw(prop_value_aliases);
688
689is(prop_value_aliases("unknown property", "unknown value"), undef,
690 "prop_value_aliases(<unknown property>, <unknown value>) returns <undef>");
691is(prop_value_aliases(undef, undef), undef,
692 "prop_value_aliases(undef, undef) returns <undef>");
693is((prop_value_aliases("na", "A")), "A", "test that prop_value_aliases returns its input for properties that don't have synonyms");
694is(prop_value_aliases("isgc", "C"), undef, "prop_value_aliases('isgc', 'C') returns <undef> since is not covered Perl extension");
695is(prop_value_aliases("gc", "isC"), undef, "prop_value_aliases('gc', 'isC') returns <undef> since is not covered Perl extension");
696
697# We have no way of knowing if mktables omitted a Perl extension that it
698# shouldn't have, but we can check if it omitted an official Unicode property
699# name synonym. And for those, we can check if the short and full names are
700# correct.
701
702my %pva_tested; # List of things already tested.
703open my $propvalues, "<", "../lib/unicore/PropValueAliases.txt"
704 or die "Can't open Unicode PropValueAliases.txt";
705while (<$propvalues>) {
706 s/\s*#.*//; # Remove comments
707 next if /^\s* $/x; # Ignore empty and comment lines
708 chomp;
709
710 my @fields = split /\s*;\s*/; # Fields are separated by semi-colons
711 my $prop = shift @fields; # 0th field is the property,
712 my $count = 0; # 0th field in line (after shifting off the property) is
713 # short name; 1th is long name
714 my $short_name;
715 my @names_via_short; # Saves the values between iterations
716
717 # The property on the lhs of the = is always loosely matched. Add in
718 # characters that are ignored under loose matching to test that
719 my $mod_prop = "$extra_chars$prop";
720
721 if ($fields[0] eq 'n/a') { # See comments in input file, essentially
722 # means full name and short name are identical
723 $fields[0] = $fields[1];
724 }
725 elsif ($fields[0] ne $fields[1]
e72b6605
KW
726 && &utf8::_loose_name(lc $fields[0])
727 eq &utf8::_loose_name(lc $fields[1])
7ef25837
KW
728 && $fields[1] !~ /[[:upper:]]/)
729 {
730 # Also, there is a bug in the file in which "n/a" is omitted, and
731 # the two fields are identical except for case, and the full name
732 # is all lower case. Copy the "short" name unto the full one to
733 # give it some upper case.
734
735 $fields[1] = $fields[0];
736 }
737
738 # The ccc property in the file is special; has an extra numeric field
739 # (0th), which should go at the end, since we use the next two fields as
740 # the short and full names, respectively. See comments in input file.
741 splice (@fields, 0, 0, splice(@fields, 1, 2)) if $prop eq 'ccc';
742
e72b6605 743 my $loose_prop = &utf8::_loose_name(lc $prop);
7ef25837
KW
744 my $suppressed = grep { $_ eq $loose_prop }
745 @Unicode::UCD::suppressed_properties;
746 foreach my $value (@fields) {
747 if ($suppressed) {
748 is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop");
749 next;
750 }
e72b6605 751 elsif (grep { $_ eq ("$loose_prop=" . &utf8::_loose_name(lc $value)) } @Unicode::UCD::suppressed_properties) {
7ef25837
KW
752 is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop=$value");
753 next;
754 }
755
756 # Add in test for loose matching.
757 my $mod_value = "$extra_chars$value";
758
759 # If the value is a number, optionally negative, including a floating
760 # point or rational numer, it should be only strictly matched, so the
761 # loose matching should fail.
762 if ($value =~ / ^ -? \d+ (?: [\/.] \d+ )? $ /x) {
763 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");
764
765 # And reset so below tests just the strict matching.
766 $mod_value = $value;
767 }
768
769 if ($count == 0) {
770
771 @names_via_short = prop_value_aliases($mod_prop, $mod_value);
772
773 # If the 0th test fails, no sense in continuing with the others
774 last unless is($names_via_short[0], $value, "prop_value_aliases: In '$prop', '$value' is the short name for '$mod_value'");
775 $short_name = $value;
776 }
777 elsif ($count == 1) {
778
779 # Some properties have the same short and full name; no sense
780 # repeating the test if the same.
781 if ($value ne $short_name) {
782 my @names_via_full =
783 prop_value_aliases($mod_prop, $mod_value);
784 is_deeply(\@names_via_full, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
785 }
786
787 # Tests scalar context
788 is(prop_value_aliases($prop, $short_name), $value, "'$value' is the long name for prop_value_aliases('$prop', '$short_name')");
789 }
790 else {
791 my @all_names = prop_value_aliases($mod_prop, $mod_value);
792 is_deeply(\@all_names, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
e72b6605 793 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
794 }
795
e72b6605 796 $pva_tested{&utf8::_loose_name(lc $prop) . "=" . &utf8::_loose_name(lc $value)} = 1;
7ef25837
KW
797 $count++;
798 }
799}
800
801# And test as best we can, the non-official pva's that mktables generates.
802foreach my $hash (\%utf8::loose_to_file_of, \%utf8::stricter_to_file_of) {
803 foreach my $test (keys %$hash) {
804 next if exists $pva_tested{$test}; # Skip if already tested
805
806 my ($prop, $value) = split "=", $test;
807 next unless defined $value; # prop_value_aliases() requires an input
808 # 'value'
809 my $mod_value;
810 if ($hash == \%utf8::loose_to_file_of) {
811
812 # Add extra characters to test loose-match rhs value
813 $mod_value = "$extra_chars$value";
814 }
815 else { # Here value is strictly matched.
816
817 # Extra elements are added by mktables to this hash so that
818 # something like "age=6.0" has a synonym of "age=6". It's not
819 # clear to me (khw) if we should be encouraging those synonyms, so
820 # don't test for them.
821 next if $value !~ /\D/ && exists $hash->{"$prop=$value.0"};
822
823 # Verify that loose matching fails when only strict is called for.
824 next unless is(prop_value_aliases($prop, "$extra_chars$value"), undef,
825 "prop_value_aliases('$prop', '$extra_chars$value') returns undef since '$value' should be strictly matched"),
826
827 # Strict matching does allow for underscores between digits. Test
828 # for that.
829 $mod_value = $value;
830 while ($mod_value =~ s/(\d)(\d)/$1_$2/g) {}
831 }
832
833 # The lhs property is always loosely matched, so add in extra
834 # characters to test that.
835 my $mod_prop = "$extra_chars$prop";
836
837 if ($prop eq 'gc' && $value =~ /l[_&]$/) {
838 # These two names are special in that they don't appear in the
839 # returned list because they are discouraged from use. Verify
840 # that they return the same list as a non-discouraged version.
841 my @LC = prop_value_aliases('gc', 'lc');
842 my @l_ = prop_value_aliases($mod_prop, $mod_value);
843 is_deeply(\@l_, \@LC, "prop_value_aliases('$mod_prop', '$mod_value) returns the same list as prop_value_aliases('gc', 'lc')");
844 }
845 else {
e72b6605 846 ok((grep { &utf8::_loose_name(lc $_) eq &utf8::_loose_name(lc $value) }
7ef25837
KW
847 prop_value_aliases($mod_prop, $mod_value)),
848 "'$value' is listed as an alias for prop_value_aliases('$mod_prop', '$mod_value')");
849 }
850 }
851}
852
853undef %pva_tested;
854
681d705c
KW
855no warnings 'once'; # We use some values once from 'required' modules.
856
62b3b855
KW
857use Unicode::UCD qw(prop_invlist prop_invmap MAX_CP);
858
859# There were some problems with caching interfering with prop_invlist() vs
860# prop_invmap() on binary properties, and also between the 3 properties where
861# Perl used the same 'To' name as another property (see utf8_heavy.pl).
862# So, before testing all of prop_invlist(),
863# 1) call prop_invmap() to try both orders of these name issues. This uses
864# up two of the 3 properties; the third will be left so that invlist()
865# on it gets called before invmap()
866# 2) call prop_invmap() on a generic binary property, ahead of invlist().
867# This should test that the caching works in both directions.
868
869# These properties are not stable between Unicode versions, but the first few
870# elements are; just look at the first element to see if are getting the
871# distinction right. The general inversion map testing below will test the
872# whole thing.
873my $prop = "uc";
874my ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
875is($format, 'cl', "prop_invmap() format of '$prop' is 'cl'");
bf7fe2df 876is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
62b3b855 877is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
bf7fe2df 878is($invmap_ref->[1], -32, "prop_invmap('$prop') map[1] is -32");
62b3b855
KW
879
880$prop = "upper";
881($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
882is($format, 's', "prop_invmap() format of '$prop' is 'cl'");
883is($missing, 'N', "prop_invmap() missing of '$prop' is '<code point>'");
884is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
885is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
886
887$prop = "lower";
888($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
889is($format, 's', "prop_invmap() format of '$prop' is 'cl'");
890is($missing, 'N', "prop_invmap() missing of '$prop' is '<code point>'");
891is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
892is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
893
894$prop = "lc";
895($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
896is($format, 'cl', "prop_invmap() format of '$prop' is 'cl'");
bf7fe2df 897is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
62b3b855 898is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
bf7fe2df 899is($invmap_ref->[1], 32, "prop_invmap('$prop') map[1] is 32");
62b3b855
KW
900
901# This property is stable and small, so can test all of it
902$prop = "ASCII_Hex_Digit";
903($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
904is($format, 's', "prop_invmap() format of '$prop' is 's'");
905is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
906is_deeply($invlist_ref, [ 0x0000, 0x0030, 0x003A, 0x0041,
907 0x0047, 0x0061, 0x0067, 0x110000 ],
908 "prop_invmap('$prop') code point list is correct");
909is_deeply($invmap_ref, [ 'N', 'Y', 'N', 'Y', 'N', 'Y', 'N', 'N' ] ,
910 "prop_invmap('$prop') map list is correct");
681d705c
KW
911
912is(prop_invlist("Unknown property"), undef, "prop_invlist(<Unknown property>) returns undef");
913is(prop_invlist(undef), undef, "prop_invlist(undef) returns undef");
914is(prop_invlist("Any"), 2, "prop_invlist('Any') returns the number of elements in scalar context");
915my @invlist = prop_invlist("Is_Any");
916is_deeply(\@invlist, [ 0, 0x110000 ], "prop_invlist works on 'Is_' prefixes");
917is(prop_invlist("Is_Is_Any"), undef, "prop_invlist('Is_Is_Any') returns <undef> since two is's");
918
919use Storable qw(dclone);
920
921is(prop_invlist("InKana"), undef, "prop_invlist(<user-defined property returns undef>)");
922
62b3b855 923# The way both the tests for invlist and invmap work is that they take the
681d705c
KW
924# lists returned by the functions and construct from them what the original
925# file should look like, which are then compared with the file. If they are
926# identical, the test passes. What this tests isn't that the results are
62b3b855 927# correct, but that invlist and invmap haven't introduced errors beyond what
681d705c
KW
928# are there in the files. As a small hedge against that, test some
929# prop_invlist() tables fully with the known correct result. We choose
930# ASCII_Hex_Digit again, as it is stable.
931@invlist = prop_invlist("AHex");
932is_deeply(\@invlist, [ 0x0030, 0x003A, 0x0041,
933 0x0047, 0x0061, 0x0067 ],
934 "prop_invlist('AHex') is exactly the expected set of points");
935@invlist = prop_invlist("AHex=f");
936is_deeply(\@invlist, [ 0x0000, 0x0030, 0x003A, 0x0041,
937 0x0047, 0x0061, 0x0067 ],
938 "prop_invlist('AHex=f') is exactly the expected set of points");
939
940sub fail_with_diff ($$$$) {
941 # For use below to output better messages
942 my ($prop, $official, $constructed, $tested_function_name) = @_;
943
944 is($constructed, $official, "$tested_function_name('$prop')");
945 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");
946 return;
947
948 fail("$tested_function_name('$prop')");
949
950 require File::Temp;
951 my $off = File::Temp->new();
952 chomp $official;
953 print $off $official, "\n";
954 close $off || die "Can't close official";
955
956 chomp $constructed;
957 my $gend = File::Temp->new();
958 print $gend $constructed, "\n";
959 close $gend || die "Can't close gend";
960
961 my $diff = File::Temp->new();
962 system("diff $off $gend > $diff");
963
964 open my $fh, "<", $diff || die "Can't open $diff";
965 my @diffs = <$fh>;
966 diag("In the diff output below '<' marks lines from the filesystem tables;\n'>' are from $tested_function_name()");
967 diag(@diffs);
968}
969
970my %tested_invlist;
971
972# Look at everything we think that mktables tells us exists, both loose and
973# strict
974foreach my $set_of_tables (\%utf8::stricter_to_file_of, \%utf8::loose_to_file_of)
975{
976 foreach my $table (keys %$set_of_tables) {
977
978 my $mod_table;
979 my ($prop_only, $value) = split "=", $table;
980 if (defined $value) {
981
982 # If this is to be loose matched, add in characters to test that.
983 if ($set_of_tables == \%utf8::loose_to_file_of) {
984 $value = "$extra_chars$value";
985 }
986 else { # Strict match
987
988 # Verify that loose matching fails when only strict is called
989 # for.
990 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");
991
992 # Strict matching does allow for underscores between digits.
993 # Test for that.
994 while ($value =~ s/(\d)(\d)/$1_$2/g) {}
995 }
996
997 # The property portion in compound form specifications always
998 # matches loosely
999 $mod_table = "$extra_chars$prop_only = $value";
1000 }
1001 else { # Single-form.
1002
1003 # Like above, use looose if required, and insert underscores
1004 # between digits if strict.
1005 if ($set_of_tables == \%utf8::loose_to_file_of) {
1006 $mod_table = "$extra_chars$table";
1007 }
1008 else {
1009 $mod_table = $table;
1010 while ($mod_table =~ s/(\d)(\d)/$1_$2/g) {}
1011 }
1012 }
1013
1014 my @tested = prop_invlist($mod_table);
1015 if ($table =~ /^_/) {
1016 is(@tested, 0, "prop_invlist('$mod_table') returns an empty list since is internal-only");
1017 next;
1018 }
1019
1020 # If we have already tested a property that uses the same file, this
1021 # list should be identical to the one that was tested, and can bypass
1022 # everything else.
1023 my $file = $set_of_tables->{$table};
1024 if (exists $tested_invlist{$file}) {
1025 is_deeply(\@tested, $tested_invlist{$file}, "prop_invlist('$mod_table') gave same results as its name synonym");
1026 next;
1027 }
1028 $tested_invlist{$file} = dclone \@tested;
1029
1030 # A leading '!' in the file name means that it is to be inverted.
1031 my $invert = $file =~ s/^!//;
1032 my $official = do "unicore/lib/$file.pl";
1033
1034 # Get rid of any trailing space and comments in the file.
1035 $official =~ s/\s*(#.*)?$//mg;
1036 chomp $official;
1037
1038 # If we are to test against an inverted file, it is easier to invert
1039 # our array than the file.
1040 # The file only is valid for Unicode code points, while the inversion
1041 # list is valid for all possible code points. Therefore, we must test
1042 # just the Unicode part against the file. Later we will test for
1043 # the non-Unicode part.
1044
1045 my $before_invert; # Saves the pre-inverted table.
1046 if ($invert) {
1047 $before_invert = dclone \@tested;
1048 if (@tested && $tested[0] == 0) {
1049 shift @tested;
1050 } else {
1051 unshift @tested, 0;
1052 }
1053 if (@tested && $tested[-1] == 0x110000) {
1054 pop @tested;
1055 }
1056 else {
1057 push @tested, 0x110000;
1058 }
1059 }
1060
1061 # Now construct a string from the list that should match the file.
1062 # The file gives ranges of code points with starting and ending values
1063 # in hex, like this:
1064 # 0041\t005A
1065 # 0061\t007A
1066 # 00AA
1067 # Our list has even numbered elements start ranges that are in the
1068 # list, and odd ones that aren't in the list. Therefore the odd
1069 # numbered ones are one beyond the end of the previous range, but
1070 # otherwise don't get reflected in the file.
1071 my $tested = "";
1072 my $i = 0;
1073 for (; $i < @tested - 1; $i += 2) {
1074 my $start = $tested[$i];
1075 my $end = $tested[$i+1] - 1;
1076 if ($start == $end) {
1077 $tested .= sprintf("%04X\n", $start);
1078 }
1079 else {
1080 $tested .= sprintf "%04X\t%04X\n", $start, $end;
1081 }
1082 }
1083
1084 # As mentioned earlier, the disk files only go up through Unicode,
1085 # whereas the prop_invlist() ones go as high as necessary. The
1086 # comparison is only valid through max Unicode.
1087 if ($i == @tested - 1 && $tested[$i] <= 0x10FFFF) {
1088 $tested .= sprintf("%04X\t10FFFF\n", $tested[$i]);
1089 }
1090 chomp $tested;
1091 if ($tested ne $official) {
1092 fail_with_diff($mod_table, $official, $tested, "prop_invlist");
1093 next;
1094 }
1095
1096 # Here, it matched the table. Now need to check for if it is correct
1097 # for beyond Unicode. First, calculate if is the default table or
1098 # not. This is the same algorithm as used internally in
1099 # prop_invlist(), so if it is wrong there, this test won't catch it.
1100 my $prop = lc $table;
1101 ($prop_only, $table) = split /\s*[:=]\s*/, $prop;
1102 if (defined $table) {
1103
1104 # May have optional prefixed 'is'
e72b6605 1105 $prop = &utf8::_loose_name($prop_only) =~ s/^is//r;
681d705c 1106 $prop = $utf8::loose_property_name_of{$prop};
e72b6605 1107 $prop .= "=" . &utf8::_loose_name($table);
681d705c
KW
1108 }
1109 else {
e72b6605 1110 $prop = &utf8::_loose_name($prop);
681d705c
KW
1111 }
1112 my $is_default = exists $Unicode::UCD::loose_defaults{$prop};
1113
1114 @tested = @$before_invert if $invert; # Use the original
1115 if (@tested % 2 == 0) {
1116
1117 # If there are an even number of elements, the final one starts a
1118 # range (going to infinity) of code points that are not in the
1119 # list.
1120 if ($is_default) {
1121 fail("prop_invlist('$mod_table')");
1122 diag("default table doesn't goto infinity");
1123 use Data::Dumper;
1124 diag Dumper \@tested;
1125 next;
1126 }
1127 }
1128 else {
1129 # An odd number of elements means the final one starts a range
1130 # (going to infinity of code points that are in the list.
1131 if (! $is_default) {
1132 fail("prop_invlist('$mod_table')");
1133 diag("non-default table needs to stop in the Unicode range");
1134 use Data::Dumper;
1135 diag Dumper \@tested;
1136 next;
1137 }
1138 }
1139
1140 pass("prop_invlist('$mod_table')");
1141 }
1142}
1143
62b3b855
KW
1144# Now test prop_invmap().
1145
1146@list = prop_invmap("Unknown property");
1147is (@list, 0, "prop_invmap(<Unknown property>) returns an empty list");
1148@list = prop_invmap(undef);
1149is (@list, 0, "prop_invmap(undef) returns an empty list");
1150ok (! eval "prop_invmap('gc')" && $@ ne "",
1151 "prop_invmap('gc') dies in scalar context");
1152@list = prop_invmap("_X_Begin");
1153is (@list, 0, "prop_invmap(<internal property>) returns an empty list");
1154@list = prop_invmap("InKana");
1155is(@list, 0, "prop_invmap(<user-defined property returns undef>)");
1156@list = prop_invmap("Perl_Decomposition_Mapping"), undef,
1157is(@list, 0, "prop_invmap('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
1158@list = prop_invmap("Perl_Charnames"), undef,
1159is(@list, 0, "prop_invmap('Perl_Charnames') returns <undef> since internal-Perl-only");
1160@list = prop_invmap("Is_Is_Any");
1161is(@list, 0, "prop_invmap('Is_Is_Any') returns <undef> since two is's");
1162
1163# The set of properties to test on has already been compiled into %props by
1164# the prop_aliases() tests.
1165
1166my %tested_invmaps;
1167
1168# Like prop_invlist(), prop_invmap() is tested by comparing the results
1169# returned by the function with the tables that mktables generates. Some of
1170# these tables are directly stored as files on disk, in either the unicore or
1171# unicore/To directories, and most should be listed in the mktables generated
1172# hash %utf8::loose_property_to_file_of, with a few additional ones that this
1173# handles specially. For these, the files are read in directly, massaged, and
1174# compared with what invmap() returns. The SPECIALS hash in some of these
1175# files overrides values in the main part of the file.
1176#
1177# The other properties are tested indirectly by generating all the possible
1178# inversion lists for the property, and seeing if those match the inversion
1179# lists returned by prop_invlist(), which has already been tested.
1180
1181PROPERTY:
1182foreach my $prop (keys %props) {
e72b6605 1183 my $loose_prop = &utf8::_loose_name(lc $prop);
62b3b855
KW
1184 my $suppressed = grep { $_ eq $loose_prop }
1185 @Unicode::UCD::suppressed_properties;
1186
1187 # Find the short and full names that this property goes by
1188 my ($name, $full_name) = prop_aliases($prop);
1189 if (! $name) {
1190 if (! $suppressed) {
1191 fail("prop_invmap('$prop')");
1192 diag("is unknown to prop_aliases(), and we need it in order to test prop_invmap");
1193 }
1194 next PROPERTY;
1195 }
1196
1197 # Normalize the short name, as it is stored in the hashes under the
1198 # normalized version.
e72b6605 1199 $name = &utf8::_loose_name(lc $name);
62b3b855
KW
1200
1201 # Add in the characters that are supposed to be ignored to test loose
1202 # matching, which the tested function applies to all properties
1203 my $mod_prop = "$extra_chars$prop";
1204
1205 my ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($mod_prop);
1206 my $return_ref = [ $invlist_ref, $invmap_ref, $format, $missing ];
1207
1208 # If have already tested this property under a different name, merely
1209 # compare the return from now with the saved one from before.
1210 if (exists $tested_invmaps{$name}) {
1211 is_deeply($return_ref, $tested_invmaps{$name}, "prop_invmap('$mod_prop') gave same results as its synonym, '$name'");
1212 next PROPERTY;
1213 }
1214 $tested_invmaps{$name} = dclone $return_ref;
1215
1216 # If prop_invmap() returned nothing, is ok iff is a property whose file is
1217 # not generated.
1218 if ($suppressed) {
1219 if (defined $format) {
1220 fail("prop_invmap('$mod_prop')");
1221 diag("did not return undef for suppressed property $prop");
1222 }
1223 next PROPERTY;
1224 }
1225 elsif (!defined $format) {
1226 fail("prop_invmap('$mod_prop')");
1227 diag("'$prop' is unknown to prop_invmap()");
1228 next PROPERTY;
1229 }
1230
1231 # The two parallel arrays must have the same number of elements.
1232 if (@$invlist_ref != @$invmap_ref) {
1233 fail("prop_invmap('$mod_prop')");
1234 diag("invlist has "
1235 . scalar @$invlist_ref
1236 . " while invmap has "
1237 . scalar @$invmap_ref
1238 . " elements");
1239 next PROPERTY;
1240 }
1241
1242 # The last element must be for the above-Unicode code points, and must be
1243 # for the default value.
1244 if ($invlist_ref->[-1] != 0x110000) {
1245 fail("prop_invmap('$mod_prop')");
1246 diag("The last inversion list element is not 0x110000");
1247 next PROPERTY;
1248 }
1249 if ($invmap_ref->[-1] ne $missing) {
1250 fail("prop_invmap('$mod_prop')");
1251 diag("The last inversion list element is '$invmap_ref->[-1]', and should be '$missing'");
1252 next PROPERTY;
1253 }
1254
1255 if ($name eq 'bmg') { # This one has an atypical $missing
1256 if ($missing ne "") {
1257 fail("prop_invmap('$mod_prop')");
1258 diag("The missings should be \"\"; got '$missing'");
1259 next PROPERTY;
1260 }
1261 }
bf7fe2df
KW
1262 elsif ($name eq 'nfkccf') { # This one has an atypical $missing
1263 if ($missing ne "<code point>") {
1264 fail("prop_invmap('$mod_prop')");
1265 diag("The missings should be \"\"; got '$missing'");
1266 next PROPERTY;
1267 }
1268 }
1269 elsif ($format =~ /^ c /x) {
1270 if ($missing ne "0") {
1271 fail("prop_invmap('$mod_prop')");
1272 diag("The missings should be '0'; got '$missing'");
1273 next PROPERTY;
1274 }
1275 }
1276 elsif ($format =~ /^ d /x) {
62b3b855
KW
1277 if ($missing ne "<code point>") {
1278 fail("prop_invmap('$mod_prop')");
1279 diag("The missings should be '<code point>'; got '$missing'");
1280 next PROPERTY;
1281 }
1282 }
1283 elsif ($missing =~ /[<>]/) {
1284 fail("prop_invmap('$mod_prop')");
1285 diag("The missings should NOT be something with <...>'");
1286 next PROPERTY;
1287
1288 # I don't want to hard code in what all the missings should be, so
1289 # those don't get fully tested.
1290 }
1291
1292 # Certain properties don't have their own files, but must be constructed
1293 # using proxies.
1294 my $proxy_prop = $name;
1295 if ($full_name eq 'Present_In') {
1296 $proxy_prop = "age"; # The maps for these two props are identical
1297 }
1298 elsif ($full_name eq 'Simple_Case_Folding'
1299 || $full_name =~ /Simple_ (.) .*? case_Mapping /x)
1300 {
1301 if ($full_name eq 'Simple_Case_Folding') {
1302 $proxy_prop = 'cf';
1303 }
1304 else {
1305 # We captured the U, L, or T, leading to uc, lc, or tc.
1306 $proxy_prop = lc $1 . "c";
1307 }
1308 if ($format ne "c") {
1309 fail("prop_invmap('$mod_prop')");
1310 diag("The format should be 'c'; got '$format'");
1311 next PROPERTY;
1312 }
1313 }
1314
1315 my $base_file;
1316 my $official;
1317
1318 # Handle the properties that have full disk files for them (except the
1319 # Name property which is structurally enough different that it is handled
1320 # separately below.)
1321 if ($name ne 'na'
1322 && ($name eq 'blk'
1323 || defined
1324 ($base_file = $utf8::loose_property_to_file_of{$proxy_prop})
1325 || exists $utf8::loose_to_file_of{$proxy_prop}
1326 || $name eq "dm"))
1327 {
1328 # In the above, blk is done unconditionally, as we need to test that
1329 # the old-style block names are returned, even if mktables has
1330 # generated a file for the new-style; the test for dm comes afterward,
1331 # so that if a file has been generated for it explicitly, we use that
1332 # file (which is valid, unlike blk) instead of the combo
1333 # Decomposition.pl files.
1334 my $file;
1335 my $is_binary = 0;
1336 if ($name eq 'blk') {
1337
1338 # The blk property is special. The original file with old block
1339 # names is retained, and the default is to not write out a
1340 # new-name file. What we do is get the old names into a data
1341 # structure, and from that create what the new file would look
1342 # like. $base_file is needed to be defined, just to avoid a
1343 # message below.
1344 $base_file = "This is a dummy name";
1345 my $blocks_ref = charblocks();
1346 $official = "";
1347 for my $range (sort { $a->[0][0] <=> $b->[0][0] }
1348 values %$blocks_ref)
1349 {
1350 # Translate the charblocks() data structure to what the file
1351 # would like.
1352 $official .= sprintf"%04X\t%04X\t%s\n",
1353 $range->[0][0],
1354 $range->[0][1],
1355 $range->[0][2];
1356 }
1357 }
1358 else {
18e9d873 1359 $base_file = "Decomposition" if $format eq 'd';
62b3b855
KW
1360
1361 # Above leaves $base_file undefined only if it came from the hash
1362 # below. This should happen only when it is a binary property
1363 # (and are accessing via a single-form name, like 'In_Latin1'),
1364 # and so it is stored in a different directory than the To ones.
1365 # XXX Currently, the only cases where it is complemented are the
1366 # ones that have no code points. And it works out for these that
1367 # 1) complementing them, and then 2) adding or subtracting the
1368 # initial 0 and final 110000 cancel each other out. But further
1369 # work would be needed in the unlikely event that an inverted
1370 # property comes along without these characteristics
1371 if (!defined $base_file) {
1372 $base_file = $utf8::loose_to_file_of{$proxy_prop};
1373 $is_binary = ($base_file =~ s/^!//) ? -1 : 1;
1374 $base_file = "lib/$base_file";
1375 }
1376
1377 # Read in the file
62b3b855
KW
1378 $file = "unicore/$base_file.pl";
1379 $official = do $file;
1380
1381 # Get rid of any trailing space and comments in the file.
1382 $official =~ s/\s*(#.*)?$//mg;
1383
1384 # Decomposition.pl also has the <compatible> types in it, which
1385 # should be removed.
1386 $official =~ s/<.*?> //mg if $format eq 'd';
1387 }
1388 chomp $official;
1389
1390 # If there are any special elements, get a reference to them.
bf7fe2df
KW
1391 my $swash_name = $utf8::file_to_swash_name{$base_file};
1392 my $specials_ref;
1393 if ($swash_name) {
1394 $specials_ref = $utf8::SwashInfo{$swash_name}{'specials_name'};
62b3b855
KW
1395 if ($specials_ref) {
1396
1397 # Convert from the name to the actual reference.
1398 no strict 'refs';
1399 $specials_ref = \%{$specials_ref};
1400 }
1401 }
1402
1403 # Certain of the proxy properties have to be adjusted to match the
1404 # real ones.
ae1bcb1f 1405 if ($full_name =~ /^(Case_Folding|(Lower|Title|Upper)case_Mapping)/) {
62b3b855
KW
1406
1407 # Here we have either
1408 # 1) Case_Folding; or
1409 # 2) a proxy that is a full mapping, which means that what the
1410 # real property is is the equivalent simple mapping.
1411 # In both cases, the file will have a standard list containing
1412 # simple mappings (to a single code point), and a specials hash
1413 # which contains all the mappings that are to multiple code
1414 # points. First, extract a list containing all the file's simple
1415 # mappings.
1416 my @list;
1417 for (split "\n", $official) {
1418 my ($start, $end, $value) = / ^ (.+?) \t (.*?) \t (.+?)
1419 \s* ( \# .* )?
1420 $ /x;
1421 $end = $start if $end eq "";
bf7fe2df 1422 push @list, [ hex $start, hex $end, $value ];
62b3b855
KW
1423 }
1424
ae1bcb1f 1425 # For these mappings, the file contains all the simple mappings,
62b3b855 1426 # including the ones that are overridden by the specials. These
ae1bcb1f 1427 # need to be removed as the list is for just the full ones.
62b3b855
KW
1428
1429 # Go through any special mappings one by one. They are packed.
1430 my $i = 0;
1431 foreach my $utf8_cp (sort keys %$specials_ref) {
1432 my $cp = unpack("C0U", $utf8_cp);
1433
62b3b855
KW
1434 # Find the spot in the @list of simple mappings that this
1435 # special applies to; uses a linear search.
1436 while ($i < @list -1 ) {
1437 last if $cp <= $list[$i][0];
1438 $i++;
1439 }
1440
1441 #note $i-1 . ": " . join " => ", @{$list[$i-1]};
1442 #note $i-0 . ": " . join " => ", @{$list[$i-0]};
1443 #note $i+1 . ": " . join " => ", @{$list[$i+1]};
1444
500fca8d
KW
1445 # Then, remove any existing entry for this code point.
1446 next if $cp != $list[$i][0];
1447 splice @list, $i, 1;
62b3b855
KW
1448
1449 #note __LINE__ . ": $cp";
1450 #note $i-1 . ": " . join " => ", @{$list[$i-1]};
1451 #note $i-0 . ": " . join " => ", @{$list[$i-0]};
1452 #note $i+1 . ": " . join " => ", @{$list[$i+1]};
1453 }
1454
1455 # Here, have gone through all the specials, modifying @list as
1456 # needed. Turn it back into what the file should look like.
bf7fe2df
KW
1457 $official = "";
1458 for my $element (@list) {
1459 $official .= "\n" if $official;
1460 if ($element->[1] == $element->[0]) {
1461 $official .= sprintf "%04X\t\t%s", $element->[0], $element->[2];
1462 }
1463 else {
1464 $official .= sprintf "%04X\t%04X\t%s", $element->[0], $element->[1], $element->[2];
1465 }
1466 }
62b3b855 1467 }
ae1bcb1f
KW
1468 elsif ($full_name =~ /Simple_(Case_Folding|(Lower|Title|Upper)case_Mapping)/)
1469 {
62b3b855 1470
ae1bcb1f 1471 # These properties have everything in the regular array, and the
62b3b855 1472 # specials are superfluous.
ae1bcb1f 1473 undef $specials_ref;
62b3b855 1474 }
bf7fe2df
KW
1475 elsif ($name eq 'bmg') {
1476
1477 # For this property, the file is output using hex notation for the
1478 # map, with all ranges equal to length 1. Convert from hex to
1479 # decimal.
1480 my @lines = split "\n", $official;
1481 foreach my $line (@lines) {
1482 my ($code_point, $map) = split "\t\t", $line;
1483 $line = $code_point . "\t\t" . hex $map;
1484 }
1485 $official = join "\n", @lines;
1486 }
62b3b855
KW
1487
1488 # Here, in $official, we have what the file looks like, or should like
1489 # if we've had to fix it up. Now take the invmap() output and reverse
1490 # engineer from that what the file should look like. Each iteration
1491 # appends the next line to the running string.
1492 my $tested_map = "";
1493
1494 # Create a copy of the file's specials hash. (It has been undef'd if
1495 # we know it isn't relevant to this property, so if it exists, it's an
1496 # error or is relevant). As we go along, we delete from that copy.
1497 # If a delete fails, or something is left over after we are done,
1498 # it's an error
1499 my %specials = %$specials_ref if $specials_ref;
1500
1501 # The extra -1 is because the final element has been tested above to
1502 # be for anything above Unicode. The file doesn't go that high.
1503 for my $i (0 .. @$invlist_ref - 1 - 1) {
1504
1505 # If the map element is a reference, have to stringify it (but
1506 # don't do so if the format doesn't allow references, so that an
1507 # improper format will generate an error.
1508 if (ref $invmap_ref->[$i]
1509 && ($format eq 'd' || $format =~ /^ . l /x))
1510 {
3b6a8189 1511 # The stringification depends on the format.
62b3b855 1512 if ($format eq 'sl') {
3b6a8189
KW
1513
1514 # At the time of this writing, there are two types of 'sl'
1515 # format One, in Name_Alias, has multiple separate entries
1516 # for each code point; the other, in Script_Extension, is space
1517 # separated. Assume the latter for non-Name_Alias.
1518 if ($full_name ne 'Name_Alias') {
1519 $invmap_ref->[$i] = join " ", @{$invmap_ref->[$i]};
1520 }
1521 else {
1522 # For Name_Alias, we emulate the file. Entries with
1523 # just one value don't need any changes, but we
1524 # convert the list entries into a series of lines for
1525 # the file, starting with the first name. The
1526 # succeeding entries are on separate lines, with the
1527 # code point repeated for each one and then two tabs,
1528 # then the value. Code at the end of the loop will
1529 # set up the first line with its code point and two
1530 # tabs before the value, just as it does for every
1531 # other property; thus the special handling of the
1532 # first line.
1533 if (ref $invmap_ref->[$i]) {
1534 my $hex_cp = sprintf("%04X", $invlist_ref->[$i]);
1535 my $concatenated = $invmap_ref->[$i][0];
1536 for (my $j = 1; $j < @{$invmap_ref->[$i]}; $j++) {
1537 $concatenated .= "\n$hex_cp\t\t" . $invmap_ref->[$i][$j];
1538 }
1539 $invmap_ref->[$i] = $concatenated;
1540 }
1541 }
62b3b855
KW
1542 }
1543 elsif ($format =~ / ^ cl e? $/x) {
1544
1545 # For a cl property, the stringified result should be in
1546 # the specials hash. The key is the packed code point,
1547 # and the value is the packed map.
1548 my $value;
1549 if (! defined ($value = delete $specials{pack("C0U", $invlist_ref->[$i]) })) {
1550 fail("prop_invmap('$mod_prop')");
1551 diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
1552 next PROPERTY;
1553 }
1554 my $packed = pack "U*", @{$invmap_ref->[$i]};
1555 if ($value ne $packed) {
1556 fail("prop_invmap('$mod_prop')");
1557 diag(sprintf "For %04X, expected the mapping to be '$packed', but got '$value'");
1558 next PROPERTY;
1559 }
1560
1561 # As this doesn't get tested when we later compare with
1562 # the actual file, it could be out of order and we
1563 # wouldn't know it.
1564 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1565 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1566 {
1567 fail("prop_invmap('$mod_prop')");
1568 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1569 next PROPERTY;
1570 }
1571 next;
1572 }
1573 elsif ($format eq 'd') {
1574
1575 # The decomposition mapping file has the code points as
1576 # a string of space-separated hex constants.
1577 $invmap_ref->[$i] = join " ", map { sprintf "%04X", $_ } @{$invmap_ref->[$i]};
1578 }
1579 else {
1580 fail("prop_invmap('$mod_prop')");
1581 diag("Can't handle format '$format'");
1582 next PROPERTY;
1583 }
1584 }
1585 elsif ($format eq 'cle' && $invmap_ref->[$i] eq "") {
1586
1587 # cle properties have maps to the empty string that also
1588 # should be in the specials hash, with the key the packed code
1589 # point, and the map just empty.
1590 my $value;
1591 if (! defined ($value = delete $specials{pack("C0U", $invlist_ref->[$i]) })) {
1592 fail("prop_invmap('$mod_prop')");
1593 diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
1594 next PROPERTY;
1595 }
1596 if ($value ne "") {
1597 fail("prop_invmap('$mod_prop')");
1598 diag(sprintf "For %04X, expected the mapping to be \"\", but got '$value'", $invlist_ref->[$i]);
1599 next PROPERTY;
1600 }
1601
1602 # As this doesn't get tested when we later compare with
1603 # the actual file, it could be out of order and we
1604 # wouldn't know it.
1605 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1606 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1607 {
1608 fail("prop_invmap('$mod_prop')");
1609 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1610 next PROPERTY;
1611 }
1612 next;
1613 }
1614 elsif ($is_binary) { # These binary files don't have an explicit Y
1615 $invmap_ref->[$i] =~ s/Y//;
1616 }
1617
1618 # The file doesn't include entries that map to $missing, so don't
1619 # include it in the built-up string. But make sure that it is in
1620 # the correct order in the input.
1621 if ($invmap_ref->[$i] eq $missing) {
1622 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1623 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1624 {
1625 fail("prop_invmap('$mod_prop')");
1626 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1627 next PROPERTY;
1628 }
1629 next;
1630 }
1631
bf7fe2df
KW
1632 # The 'd' property and 'c' properties whose underlying format is
1633 # hexadecimal have the mapping expressed in hex in the file
1634 if ($format eq 'd'
1635 || ($format =~ /^c/
1636 && $swash_name
1637 && $utf8::SwashInfo{$swash_name}{'format'} eq 'x'))
1638 {
62b3b855
KW
1639
1640 # The d property has one entry which isn't in the file.
1641 # Ignore it, but make sure it is in order.
1642 if ($format eq 'd'
1643 && $invmap_ref->[$i] eq '<hangul syllable>'
1644 && $invlist_ref->[$i] == 0xAC00)
1645 {
1646 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1647 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1648 {
1649 fail("prop_invmap('$mod_prop')");
1650 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1651 next PROPERTY;
1652 }
1653 next;
1654 }
1655 $invmap_ref->[$i] = sprintf("%04X", $invmap_ref->[$i])
1656 if $invmap_ref->[$i] =~ / ^ [A-Fa-f0-9]+ $/x;
1657 }
1658
1659 # Finally have figured out what the map column in the file should
1660 # be. Append the line to the running string.
1661 my $start = $invlist_ref->[$i];
1662 my $end = $invlist_ref->[$i+1] - 1;
1663 $end = ($start == $end) ? "" : sprintf("%04X", $end);
1664 if ($invmap_ref->[$i] ne "") {
1665 $tested_map .= sprintf "%04X\t%s\t%s\n", $start, $end, $invmap_ref->[$i];
1666 }
1667 elsif ($end ne "") {
1668 $tested_map .= sprintf "%04X\t%s\n", $start, $end;
1669 }
1670 else {
1671 $tested_map .= sprintf "%04X\n", $start;
1672 }
1673 } # End of looping over all elements.
1674
1675 # Here are done with generating what the file should look like
1676
1677 chomp $tested_map;
1678
1679 # And compare.
1680 if ($tested_map ne $official) {
1681 fail_with_diff($mod_prop, $official, $tested_map, "prop_invmap");
1682 next PROPERTY;
1683 }
1684
1685 # There shouldn't be any specials unaccounted for.
1686 if (keys %specials) {
1687 fail("prop_invmap('$mod_prop')");
1688 diag("Unexpected specials: " . join ", ", keys %specials);
1689 next PROPERTY;
1690 }
1691 }
1692 elsif ($format eq 'n') {
1693
1694 # Handle the Name property similar to the above. But the file is
1695 # sufficiently different that it is more convenient to make a special
3b6a8189
KW
1696 # case for it. It is a combination of the Name, Unicode1_Name, and
1697 # Name_Alias properties, and named sequences. We need to remove all
1698 # but the Name in order to do the comparison.
62b3b855
KW
1699
1700 if ($missing ne "") {
1701 fail("prop_invmap('$mod_prop')");
1702 diag("The missings should be \"\"; got \"missing\"");
1703 next PROPERTY;
1704 }
1705
1706 $official = do "unicore/Name.pl";
1707
1708 # Get rid of the named sequences portion of the file. These don't
1709 # have a tab before the first blank on a line.
1710 $official =~ s/ ^ [^\t]+ \ .*? \n //xmg;
1711
1712 # And get rid of the controls. These are named in the file, but
3b6a8189
KW
1713 # shouldn't be in the property. This gets rid of the two ranges in
1714 # one fell swoop, and also all the Unicode1_Name values that may not
1715 # be in Name_Alias.
62b3b855
KW
1716 $official =~ s/ 00000 \t .* 0001F .*? \n//xs;
1717 $official =~ s/ 0007F \t .* 0009F .*? \n//xs;
1718
3b6a8189
KW
1719 # And remove the aliases. We read in the Name_Alias property, and go
1720 # through them one by one.
1721 my ($aliases_code_points, $aliases_maps, undef, undef)
1722 = &prop_invmap('Name_Alias');
1723 for (my $i = 0; $i < @$aliases_code_points; $i++) {
1724 my $code_point = $aliases_code_points->[$i];
1725
1726 # Already removed these above.
1727 next if $code_point <= 0x1F
1728 || ($code_point >= 0x7F && $code_point <= 0x9F);
1729
1730 my $hex_code_point = sprintf "%05X", $code_point;
1731
1732 # Convert to a list if not already to make the following loop
1733 # control uniform.
1734 $aliases_maps->[$i] = [ $aliases_maps->[$i] ]
1735 if ! ref $aliases_maps->[$i];
1736
1737 # Remove each alias for this code point from the file
1738 foreach my $alias (@{$aliases_maps->[$i]}) {
1739
1740 # Remove the alias type from the entry, retaining just the name.
1741 $alias =~ s/:.*//;
1742
1743 $alias = quotemeta($alias);
1744 $official =~ s/$hex_code_point \t $alias \n //x;
62b3b855
KW
1745 }
1746 }
62b3b855
KW
1747 chomp $official;
1748
1749 # Here have adjusted the file. We also have to adjust the returned
1750 # inversion map by checking and deleting all the lines in it that
1751 # won't be in the file. These are the lines that have generated
1752 # things, like <hangul syllable>.
1753 my $tested_map = ""; # Current running string
1754 my @code_point_in_names =
1755 @Unicode::UCD::code_points_ending_in_code_point;
1756
1757 for my $i (0 .. @$invlist_ref - 1 - 1) {
1758 my $start = $invlist_ref->[$i];
1759 my $end = $invlist_ref->[$i+1] - 1;
1760 if ($invmap_ref->[$i] eq $missing) {
1761 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1762 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1763 {
1764 fail("prop_invmap('$mod_prop')");
1765 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1766 next PROPERTY;
1767 }
1768 next;
1769 }
1770 if ($invmap_ref->[$i] =~ / (.*) ( < .*? > )/x) {
1771 my $name = $1;
1772 my $type = $2;
1773 if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
1774 || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
1775 {
1776 fail("prop_invmap('$mod_prop')");
1777 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1778 next PROPERTY;
1779 }
1780 if ($type eq "<hangul syllable>") {
1781 if ($name ne "") {
1782 fail("prop_invmap('$mod_prop')");
1783 diag("Unexpected text in $invmap_ref->[$i]");
1784 next PROPERTY;
1785 }
1786 if ($start != 0xAC00) {
1787 fail("prop_invmap('$mod_prop')");
1788 diag(sprintf("<hangul syllables> should begin at 0xAC00, got %04X", $start));
1789 next PROPERTY;
1790 }
1791 if ($end != $start + 11172 - 1) {
1792 fail("prop_invmap('$mod_prop')");
1793 diag(sprintf("<hangul syllables> should end at %04X, got %04X", $start + 11172 -1, $end));
1794 next PROPERTY;
1795 }
1796 }
1797 elsif ($type ne "<code point>") {
1798 fail("prop_invmap('$mod_prop')");
1799 diag("Unexpected text '$type' in $invmap_ref->[$i]");
1800 next PROPERTY;
1801 }
1802 else {
1803
1804 # Look through the array of names that end in code points,
1805 # and look for this start and end. If not found is an
1806 # error. If found, delete it, and at the end, make sure
1807 # have deleted everything.
1808 for my $i (0 .. @code_point_in_names - 1) {
1809 my $hash = $code_point_in_names[$i];
1810 if ($hash->{'low'} == $start
1811 && $hash->{'high'} == $end
1812 && "$hash->{'name'}-" eq $name)
1813 {
1814 splice @code_point_in_names, $i, 1;
1815 last;
1816 }
1817 else {
1818 fail("prop_invmap('$mod_prop')");
1819 diag("Unexpected code-point-in-name line '$invmap_ref->[$i]'");
1820 next PROPERTY;
1821 }
1822 }
1823 }
1824
1825 next;
1826 }
1827
1828 # Have adjusted the map, as needed. Append to running string.
1829 $end = ($start == $end) ? "" : sprintf("%05X", $end);
1830 $tested_map .= sprintf "%05X\t%s\n", $start, $invmap_ref->[$i];
1831 }
1832
1833 # Finished creating the string from the inversion map. Can compare
1834 # with what the file is.
1835 chomp $tested_map;
1836 if ($tested_map ne $official) {
1837 fail_with_diff($mod_prop, $official, $tested_map, "prop_invmap");
1838 next PROPERTY;
1839 }
1840 if (@code_point_in_names) {
1841 fail("prop_invmap('$mod_prop')");
1842 use Data::Dumper;
1843 diag("Missing code-point-in-name line(s)" . Dumper \@code_point_in_names);
1844 next PROPERTY;
1845 }
1846 }
1847 elsif ($format eq 's' || $format eq 'r') {
1848
1849 # Here the map is not more or less directly from a file stored on
1850 # disk. We try a different tack. These should all be properties that
1851 # have just a few possible values (most of them are binary). We go
1852 # through the map list, sorting each range into buckets, one for each
1853 # map value. Thus for binary properties there will be a bucket for Y
1854 # and one for N. The buckets are inversion lists. We compare each
1855 # constructed inversion list with what we would get for it using
1856 # prop_invlist(), which has already been tested. If they all match,
1857 # the whole map must have matched.
1858 my %maps;
1859 my $previous_map;
1860
1861 # (The extra -1 is to not look at the final element in the loop, which
1862 # we know is the one that starts just beyond Unicode and goes to
1863 # infinity.)
1864 for my $i (0 .. @$invlist_ref - 1 - 1) {
1865 my $range_start = $invlist_ref->[$i];
1866
1867 # Because we are sorting into buckets, things could be
1868 # out-of-order here, and still be in the correct order in the
1869 # bucket, and hence wouldn't show up as an error; so have to
1870 # check.
1871 if (($i > 0 && $range_start <= $invlist_ref->[$i-1])
1872 || $range_start >= $invlist_ref->[$i+1])
1873 {
1874 fail("prop_invmap('$mod_prop')");
1875 diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
1876 next PROPERTY;
1877 }
1878
1879 # This new range closes out the range started in the previous
1880 # iteration.
1881 push @{$maps{$previous_map}}, $range_start if defined $previous_map;
1882
1883 # And starts a range which will be closed in the next iteration.
1884 $previous_map = $invmap_ref->[$i];
1885 push @{$maps{$previous_map}}, $range_start;
1886 }
1887
1888 # The range we just started hasn't been closed, and we didn't look at
1889 # the final element of the loop. If that range is for the default
1890 # value, it shouldn't be closed, as it is to extend to infinity. But
1891 # otherwise, it should end at the final Unicode code point, and the
1892 # list that maps to the default value should have another element that
1893 # does go to infinity for every above Unicode code point.
1894
1895 if (@$invlist_ref > 1) {
1896 my $penultimate_map = $invmap_ref->[-2];
1897 if ($penultimate_map ne $missing) {
1898
1899 # The -1th element contains the first non-Unicode code point.
1900 push @{$maps{$penultimate_map}}, $invlist_ref->[-1];
1901 push @{$maps{$missing}}, $invlist_ref->[-1];
1902 }
1903 }
1904
1905 # Here, we have the buckets (inversion lists) all constructed. Go
1906 # through each and verify that matches what prop_invlist() returns.
1907 # We could use is_deeply() for the comparison, but would get multiple
1908 # messages for each $prop.
1909 foreach my $map (keys %maps) {
1910 my @off_invlist = prop_invlist("$prop = $map");
1911 my $min = (@off_invlist >= @{$maps{$map}})
1912 ? @off_invlist
1913 : @{$maps{$map}};
1914 for my $i (0 .. $min- 1) {
1915 if ($i > @off_invlist - 1) {
1916 fail("prop_invmap('$mod_prop')");
1917 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]'");
1918 next PROPERTY;
1919 }
1920 elsif ($i > @{$maps{$map}} - 1) {
1921 fail("prop_invmap('$mod_prop')");
1922 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]'");
1923 next PROPERTY;
1924 }
1925 elsif ($maps{$map}[$i] ne $off_invlist[$i]) {
1926 fail("prop_invmap('$mod_prop')");
1927 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]'");
1928 next PROPERTY;
1929 }
1930 }
1931 }
1932 }
1933 else { # Don't know this property nor format.
1934
1935 fail("prop_invmap('$mod_prop')");
1936 diag("Unknown format '$format'");
1937 }
1938
1939 pass("prop_invmap('$mod_prop')");
1940}
1941
eaebe4db 1942done_testing();