1#!perl -w
2BEGIN {
3    $::IS_ASCII = (ord("A") == 65) ? 1 : 0;
4    $::IS_EBCDIC = (ord("A") == 193) ? 1 : 0;
5    chdir 't' if -d 't';
6    @INC = '../lib';
7    require Config; import Config;
8    if ($Config{'extensions'} !~ /\bStorable\b/) {
9        print "1..0 # Skip: Storable was not built; Unicode::UCD uses Storable\n";
10        exit 0;
11    }
12}
13
14my @warnings;
15local $SIG{__WARN__} = sub { push @warnings, @_  };
16
17use strict;
18use Test::More;
19
20use Unicode::UCD qw(charinfo charprop charprops_all);
21
22my $expected_version = '15.0.0';
23my $current_version = Unicode::UCD::UnicodeVersion;
24my $v_unicode_version = pack "C*", split /\./, $current_version;
25my $unknown_script = ($v_unicode_version lt v5.0.0)
26                     ? 'Common'
27                     : 'Unknown';
28my $input_record_separator = 7; # Make sure Unicode::UCD isn't affected by
29$/ = $input_record_separator;   # setting this.
30
31my $charinfo;
32
33is(charinfo(0x110000), undef, "Verify charinfo() of non-unicode is undef");
34if ($v_unicode_version ge v3.2.0) {
35    is(lc charprop(0x110000, 'age'), lc "Unassigned", "Verify charprop(age) of non-unicode is Unassigned");
36    is(charprop(0x110000, 'in'), "Unassigned", "Verify charprop(in), a bipartite Perl extension, works");
37}
38is(charprop(0x110000, 'Any'), undef, "Verify charprop of non-bipartite Perl extension returns undef");
39
40my $cp = 0;
41$charinfo = charinfo($cp);    # Null is often problematic, so test it.
42
43is($charinfo->{code},           "0000",
44                        "Next tests are for charinfo and charprop; first NULL");
45is($charinfo->{name},           "<control>");
46is(charprop($cp, "name"),       "");
47
48if ($v_unicode_version ge v6.1.0) {
49    # This gets a sl-type property returning a flattened list
50    is(charprop($cp, "name_alias"), "NULL: control,NUL: abbreviation");
51}
52is($charinfo->{category},       "Cc");
53is(charprop($cp, "category"),   "Control");
54is($charinfo->{combining},      "0");
55is(charprop($cp, "ccc"),        "Not_Reordered");
56is($charinfo->{bidi},           "BN");
57is(charprop($cp, "bc"),         "Boundary_Neutral");
58is($charinfo->{decomposition},  "");
59is(charprop($cp, "dm"),         "\0");
60is($charinfo->{decimal},        "");
61is($charinfo->{digit},          "");
62is($charinfo->{numeric},        "");
63is(charprop($cp, "nv"),         "NaN");
64is($charinfo->{mirrored},       "N");
65is(charprop($cp, "bidim"),      "No");
66is($charinfo->{unicode10},      "NULL");
67is(charprop($cp, "na1"),        "NULL");
68is($charinfo->{comment},        "");
69is(charprop($cp, "isc"),        "");
70is($charinfo->{upper},          "");
71is(charprop($cp, "uc"),         "\0");
72is($charinfo->{lower},          "");
73is(charprop($cp, "lc"),         "\0");
74is($charinfo->{title},          "");
75is(charprop($cp, "tc"),         "\0");
76is($charinfo->{block},          "Basic Latin");
77is(charprop($cp, "block"),      "Basic_Latin");
78is($charinfo->{script},         "Common") if $v_unicode_version gt v3.0.1;
79is(charprop($cp, "script"),     "Common") if $v_unicode_version gt v3.0.1;
80
81$cp = utf8::unicode_to_native(0x41);
82my $A_code = sprintf("%04X", ord("A"));
83my $a_code = sprintf("%04X", ord("a"));
84$charinfo = charinfo($cp);
85
86is($charinfo->{code},           $A_code, "LATIN CAPITAL LETTER A");
87is($charinfo->{name},           "LATIN CAPITAL LETTER A");
88is(charprop($cp, 'name'),       "LATIN CAPITAL LETTER A");
89is($charinfo->{category},       "Lu");
90is(charprop($cp, 'gc'),         "Uppercase_Letter");
91is($charinfo->{combining},      "0");
92is(charprop($cp, 'ccc'),        "Not_Reordered");
93is($charinfo->{bidi},           "L");
94is(charprop($cp, 'bc'),         "Left_To_Right");
95is($charinfo->{decomposition},  "");
96is(charprop($cp, 'dm'),         "A");
97is($charinfo->{decimal},        "");
98is($charinfo->{digit},          "");
99is($charinfo->{numeric},        "");
100is(charprop($cp, 'nv'),        "NaN");
101is($charinfo->{mirrored},       "N");
102is(charprop($cp, 'bidim'),      "No");
103is($charinfo->{unicode10},      "");
104is(charprop($cp, 'na1'),        "");
105is($charinfo->{comment},        "");
106is(charprop($cp, 'isc'),        "");
107is($charinfo->{upper},          "");
108is(charprop($cp, 'uc'),         "A");
109is($charinfo->{lower},          $a_code);
110is(charprop($cp, 'lc'),         "a");
111is($charinfo->{title},          "");
112is(charprop($cp, 'tc'),         "A");
113is($charinfo->{block},          "Basic Latin");
114is(charprop($cp, 'block'),      "Basic_Latin");
115is($charinfo->{script},         "Latin") if $v_unicode_version gt v3.0.1;
116is(charprop($cp, 'script'),     "Latin") if $v_unicode_version gt v3.0.1;
117
118$cp = 0x100;
119$charinfo = charinfo($cp);
120
121is($charinfo->{code},           "0100", "LATIN CAPITAL LETTER A WITH MACRON");
122is($charinfo->{name},           "LATIN CAPITAL LETTER A WITH MACRON");
123is(charprop($cp, 'name'),       "LATIN CAPITAL LETTER A WITH MACRON");
124is($charinfo->{category},       "Lu");
125is(charprop($cp, 'gc'),         "Uppercase_Letter");
126is($charinfo->{combining},      "0");
127is(charprop($cp, 'ccc'),        "Not_Reordered");
128is($charinfo->{bidi},           "L");
129is(charprop($cp, 'bc'),         "Left_To_Right");
130is($charinfo->{decomposition},  "$A_code 0304");
131is(charprop($cp, 'dm'),         "A\x{0304}");
132is($charinfo->{decimal},        "");
133is($charinfo->{digit},          "");
134is($charinfo->{numeric},        "");
135is(charprop($cp, 'nv'),         "NaN");
136is($charinfo->{mirrored},       "N");
137is(charprop($cp, 'bidim'),      "No");
138is($charinfo->{unicode10},      "LATIN CAPITAL LETTER A MACRON");
139is(charprop($cp, 'na1'),        "LATIN CAPITAL LETTER A MACRON");
140is($charinfo->{comment},        "");
141is(charprop($cp, 'isc'),        "");
142is($charinfo->{upper},          "");
143is(charprop($cp, 'uc'),         "\x{100}");
144is($charinfo->{lower},          "0101");
145is(charprop($cp, 'lc'),         "\x{101}");
146is($charinfo->{title},          "");
147is(charprop($cp, 'tc'),         "\x{100}");
148is($charinfo->{block},          "Latin Extended-A");
149is(charprop($cp, 'block'),      "Latin_Extended_A");
150is($charinfo->{script},         "Latin") if $v_unicode_version gt v3.0.1;
151is(charprop($cp, 'script'),     "Latin") if $v_unicode_version gt v3.0.1;
152
153$cp = 0x590;               # 0x0590 is in the Hebrew block but unused.
154$charinfo = charinfo($cp);
155
156is($charinfo->{code},           undef,	"0x0590 - unused Hebrew");
157is($charinfo->{name},           undef);
158is(charprop($cp, 'name'),       "");
159is($charinfo->{category},       undef);
160is(charprop($cp, 'gc'),         "Unassigned");
161is($charinfo->{combining},      undef);
162is(charprop($cp, 'ccc'),        "Not_Reordered");
163is($charinfo->{bidi},           undef);
164if ($v_unicode_version gt v3.2.0) {
165    is(charprop($cp, 'bc'),         "Right_To_Left");
166}
167is($charinfo->{decomposition},  undef);
168is(charprop($cp, 'dm'),         "\x{590}");
169is($charinfo->{decimal},        undef);
170is($charinfo->{digit},          undef);
171is($charinfo->{numeric},        undef);
172is(charprop($cp, 'nv'),         "NaN");
173is($charinfo->{mirrored},       undef);
174is(charprop($cp, 'bidim'),      "No");
175is($charinfo->{unicode10},      undef);
176is(charprop($cp, 'na1'),        "");
177is($charinfo->{comment},        undef);
178is(charprop($cp, 'isc'),        "");
179is($charinfo->{upper},          undef);
180is(charprop($cp, 'uc'),         "\x{590}");
181is($charinfo->{lower},          undef);
182is(charprop($cp, 'lc'),         "\x{590}");
183is($charinfo->{title},          undef);
184is(charprop($cp, 'tc'),         "\x{590}");
185is($charinfo->{block},          undef);
186is(charprop($cp, 'block'),      "Hebrew");
187is($charinfo->{script},         undef);
188is(charprop($cp, 'script'),     $unknown_script) if $v_unicode_version gt
189v3.0.1;
190
191# 0x05d0 is in the Hebrew block and used.
192
193$cp = 0x5d0;
194$charinfo = charinfo($cp);
195
196is($charinfo->{code},           "05D0", "05D0 - used Hebrew");
197is($charinfo->{name},           "HEBREW LETTER ALEF");
198is(charprop($cp, 'name'),       "HEBREW LETTER ALEF");
199is($charinfo->{category},       "Lo");
200is(charprop($cp, 'gc'),         "Other_Letter");
201is($charinfo->{combining},      "0");
202is(charprop($cp, 'ccc'),        "Not_Reordered");
203is($charinfo->{bidi},           "R");
204is(charprop($cp, 'bc'),         "Right_To_Left");
205is($charinfo->{decomposition},  "");
206is(charprop($cp, 'dm'),         "\x{5d0}");
207is($charinfo->{decimal},        "");
208is($charinfo->{digit},          "");
209is($charinfo->{numeric},        "");
210is(charprop($cp, 'nv'),         "NaN");
211is($charinfo->{mirrored},       "N");
212is(charprop($cp, 'bidim'),      "No");
213is($charinfo->{unicode10},      "");
214is(charprop($cp, 'na1'),        "");
215is($charinfo->{comment},        "");
216is(charprop($cp, 'isc'),        "");
217is($charinfo->{upper},          "");
218is(charprop($cp, 'uc'),         "\x{5d0}");
219is($charinfo->{lower},          "");
220is(charprop($cp, 'lc'),         "\x{5d0}");
221is($charinfo->{title},          "");
222is(charprop($cp, 'tc'),         "\x{5d0}");
223is($charinfo->{block},          "Hebrew");
224is(charprop($cp, 'block'),      "Hebrew");
225is($charinfo->{script},         "Hebrew") if $v_unicode_version gt v3.0.1;
226is(charprop($cp, 'script'),     "Hebrew") if $v_unicode_version gt v3.0.1;
227
228# An open syllable in Hangul.
229
230$cp = 0xAC00;
231$charinfo = charinfo($cp);
232
233is($charinfo->{code},           "AC00", "HANGUL SYLLABLE U+AC00");
234is($charinfo->{name},           "HANGUL SYLLABLE GA");
235is(charprop($cp, 'name'),       "HANGUL SYLLABLE GA");
236is($charinfo->{category},       "Lo");
237is(charprop($cp, 'gc'),         "Other_Letter");
238is($charinfo->{combining},      "0");
239is(charprop($cp, 'ccc'),        "Not_Reordered");
240is($charinfo->{bidi},           "L");
241is(charprop($cp, 'bc'),         "Left_To_Right");
242is($charinfo->{decomposition},  "1100 1161");
243is(charprop($cp, 'dm'),         "\x{1100}\x{1161}");
244is($charinfo->{decimal},        "");
245is($charinfo->{digit},          "");
246is($charinfo->{numeric},        "");
247is(charprop($cp, 'nv'),         "NaN");
248is($charinfo->{mirrored},       "N");
249is(charprop($cp, 'bidim'),      "No");
250is($charinfo->{unicode10},      "");
251is(charprop($cp, 'na1'),        "");
252is($charinfo->{comment},        "");
253is(charprop($cp, 'isc'),        "");
254is($charinfo->{upper},          "");
255is(charprop($cp, 'uc'),         "\x{AC00}");
256is($charinfo->{lower},          "");
257is(charprop($cp, 'lc'),         "\x{AC00}");
258is($charinfo->{title},          "");
259is(charprop($cp, 'tc'),         "\x{AC00}");
260is($charinfo->{block},          "Hangul Syllables");
261is(charprop($cp, 'block'),      "Hangul_Syllables");
262is($charinfo->{script},         "Hangul") if $v_unicode_version gt v3.0.1;
263is(charprop($cp, 'script'),     "Hangul") if $v_unicode_version gt v3.0.1;
264
265# A closed syllable in Hangul.
266
267$cp = 0xAE00;
268$charinfo = charinfo($cp);
269
270is($charinfo->{code},           "AE00", "HANGUL SYLLABLE U+AE00");
271is($charinfo->{name},           "HANGUL SYLLABLE GEUL");
272is(charprop($cp, 'name'),       "HANGUL SYLLABLE GEUL");
273is($charinfo->{category},       "Lo");
274is(charprop($cp, 'gc'),         "Other_Letter");
275is($charinfo->{combining},      "0");
276is(charprop($cp, 'ccc'),        "Not_Reordered");
277is($charinfo->{bidi},           "L");
278is(charprop($cp, 'bc'),         "Left_To_Right");
279is($charinfo->{decomposition},  "1100 1173 11AF");
280is(charprop($cp, 'dm'),         "\x{1100}\x{1173}\x{11AF}");
281is($charinfo->{decimal},        "");
282is($charinfo->{digit},          "");
283is($charinfo->{numeric},        "");
284is(charprop($cp, 'nv'),         "NaN");
285is($charinfo->{mirrored},       "N");
286is(charprop($cp, 'bidim'),      "No");
287is($charinfo->{unicode10},      "");
288is(charprop($cp, 'na1'),        "");
289is($charinfo->{comment},        "");
290is(charprop($cp, 'isc'),        "");
291is($charinfo->{upper},          "");
292is(charprop($cp, 'uc'),         "\x{AE00}");
293is($charinfo->{lower},          "");
294is(charprop($cp, 'lc'),         "\x{AE00}");
295is($charinfo->{title},          "");
296is(charprop($cp, 'tc'),         "\x{AE00}");
297is($charinfo->{block},          "Hangul Syllables");
298is(charprop($cp, 'block'),      "Hangul_Syllables");
299is($charinfo->{script},         "Hangul") if $v_unicode_version gt v3.0.1;
300is(charprop($cp, 'script'),     "Hangul") if $v_unicode_version gt v3.0.1;
301
302if ($v_unicode_version gt v3.0.1) {
303    $cp = 0x1D400;
304    $charinfo = charinfo($cp);
305
306    is($charinfo->{code},           "1D400", "MATHEMATICAL BOLD CAPITAL A");
307    is($charinfo->{name},           "MATHEMATICAL BOLD CAPITAL A");
308    is(charprop($cp, 'name'),       "MATHEMATICAL BOLD CAPITAL A");
309    is($charinfo->{category},       "Lu");
310    is(charprop($cp, 'gc'),         "Uppercase_Letter");
311    is($charinfo->{combining},      "0");
312    is(charprop($cp, 'ccc'),        "Not_Reordered");
313    is($charinfo->{bidi},           "L");
314    is(charprop($cp, 'bc'),         "Left_To_Right");
315    is($charinfo->{decomposition},  "<font> $A_code");
316    is(charprop($cp, 'dm'),         "A");
317    is($charinfo->{decimal},        "");
318    is($charinfo->{digit},          "");
319    is($charinfo->{numeric},        "");
320    is(charprop($cp, 'nv'),         "NaN");
321    is($charinfo->{mirrored},       "N");
322    is(charprop($cp, 'bidim'),      "No");
323    is($charinfo->{unicode10},      "");
324    is(charprop($cp, 'na1'),        "");
325    is($charinfo->{comment},        "");
326    is(charprop($cp, 'isc'),        "");
327    is($charinfo->{upper},          "");
328    is(charprop($cp, 'uc'),         "\x{1D400}");
329    is($charinfo->{lower},          "");
330    is(charprop($cp, 'lc'),         "\x{1D400}");
331    is($charinfo->{title},          "");
332    is(charprop($cp, 'tc'),         "\x{1D400}");
333    is($charinfo->{block},          "Mathematical Alphanumeric Symbols");
334    is(charprop($cp, 'block'),      "Mathematical_Alphanumeric_Symbols");
335    is($charinfo->{script},         "Common");
336    is(charprop($cp, 'script'),     "Common");
337}
338
339if ($v_unicode_version ge v4.1.0) {
340    $cp = 0x9FBA;	                #Bug 58428
341    $charinfo = charinfo(0x9FBA);
342
343    is($charinfo->{code},           "9FBA", "U+9FBA");
344    is($charinfo->{name},           "CJK UNIFIED IDEOGRAPH-9FBA");
345    is(charprop($cp, 'name'),       "CJK UNIFIED IDEOGRAPH-9FBA");
346    is($charinfo->{category},       "Lo");
347    is(charprop($cp, 'gc'),         "Other_Letter");
348    is($charinfo->{combining},      "0");
349    is(charprop($cp, 'ccc'),        "Not_Reordered");
350    is($charinfo->{bidi},           "L");
351    is(charprop($cp, 'bc'),         "Left_To_Right");
352    is($charinfo->{decomposition},  "");
353    is(charprop($cp, 'dm'),         "\x{9FBA}");
354    is($charinfo->{decimal},        "");
355    is($charinfo->{digit},          "");
356    is($charinfo->{numeric},        "");
357    is(charprop($cp, 'nv'),         "NaN");
358    is($charinfo->{mirrored},       "N");
359    is(charprop($cp, 'bidim'),      "No");
360    is($charinfo->{unicode10},      "");
361    is(charprop($cp, 'na1'),        "");
362    is($charinfo->{comment},        "");
363    is(charprop($cp, 'isc'),        "");
364    is($charinfo->{upper},          "");
365    is(charprop($cp, 'uc'),         "\x{9FBA}");
366    is($charinfo->{lower},          "");
367    is(charprop($cp, 'lc'),         "\x{9FBA}");
368    is($charinfo->{title},          "");
369    is(charprop($cp, 'tc'),         "\x{9FBA}");
370    is($charinfo->{block},          "CJK Unified Ideographs");
371    is(charprop($cp, 'block'),      "CJK_Unified_Ideographs");
372    is($charinfo->{script},         "Han");
373    is(charprop($cp, 'script'),     "Han");
374}
375
376use Unicode::UCD qw(charblock charscript);
377
378# 0x0590 is in the Hebrew block but unused.
379
380is(charblock(0x590),          "Hebrew", "0x0590 - Hebrew unused charblock");
381is(charscript(0x590),         $unknown_script, "0x0590 - Hebrew unused charscript") if $v_unicode_version gt v3.0.1;
382is(charblock(0x1FFFF),        "No_Block", "0x1FFFF - unused charblock");
383
384{
385    my @warnings;
386    local $SIG{__WARN__} = sub { push @warnings, @_  };
387    is(charblock(chr(0x6237)), undef,
388        "Verify charblock of non-code point returns <undef>");
389    cmp_ok(scalar @warnings, '==', 1, "  ... and generates 1 warning");
390    like($warnings[0], qr/unknown code/, "  ... with the right text");
391}
392
393my $fraction_3_4_code = sprintf("%04X", utf8::unicode_to_native(0xbe));
394$cp = $fraction_3_4_code;
395$charinfo = charinfo($fraction_3_4_code);
396
397is($charinfo->{code},           $fraction_3_4_code, "VULGAR FRACTION THREE QUARTERS");
398is($charinfo->{name},           "VULGAR FRACTION THREE QUARTERS");
399is(charprop($cp, 'name'),       "VULGAR FRACTION THREE QUARTERS");
400is($charinfo->{category},       "No");
401is(charprop($cp, 'gc'),         "Other_Number");
402is($charinfo->{combining},      "0");
403is(charprop($cp, 'ccc'),        "Not_Reordered");
404is($charinfo->{bidi},           "ON");
405is(charprop($cp, 'bc'),         "Other_Neutral");
406is($charinfo->{decomposition},  "<fraction> "
407                                . sprintf("%04X", ord "3")
408                                . " 2044 "
409                                . sprintf("%04X", ord "4"));
410is(charprop($cp, 'dm'),         "3\x{2044}4");
411is($charinfo->{decimal},        "");
412is($charinfo->{digit},          "");
413is($charinfo->{numeric},        "3/4");
414is(charprop($cp, 'nv'),        "0.75");
415is($charinfo->{mirrored},       "N");
416is(charprop($cp, 'bidim'),      "No");
417is($charinfo->{unicode10},      "FRACTION THREE QUARTERS");
418is(charprop($cp, 'na1'),        "FRACTION THREE QUARTERS");
419is($charinfo->{comment},        "");
420is(charprop($cp, 'isc'),        "");
421is($charinfo->{upper},          "");
422is(charprop($cp, 'uc'),         chr hex $cp);
423is($charinfo->{lower},          "");
424is(charprop($cp, 'lc'),         chr hex $cp);
425is($charinfo->{title},          "");
426is(charprop($cp, 'tc'),         chr hex $cp);
427is($charinfo->{block},          "Latin-1 Supplement");
428is(charprop($cp, 'block'),      "Latin_1_Supplement");
429is($charinfo->{script},         "Common") if $v_unicode_version gt v3.0.1;
430is(charprop($cp, 'script'),     "Common") if $v_unicode_version gt v3.0.1;
431
432# This is to test a case where both simple and full lowercases exist and
433# differ
434$cp = 0x130;
435$charinfo = charinfo($cp);
436my $I_code = sprintf("%04X", ord("I"));
437my $i_code = sprintf("%04X", ord("i"));
438
439is($charinfo->{code},           "0130", "LATIN CAPITAL LETTER I WITH DOT ABOVE");
440is($charinfo->{name},           "LATIN CAPITAL LETTER I WITH DOT ABOVE");
441is(charprop($cp, 'name'),       "LATIN CAPITAL LETTER I WITH DOT ABOVE");
442is($charinfo->{category},       "Lu");
443is(charprop($cp, 'gc'),         "Uppercase_Letter");
444is($charinfo->{combining},      "0");
445is(charprop($cp, 'ccc'),        "Not_Reordered");
446is($charinfo->{bidi},           "L");
447is(charprop($cp, 'bc'),         "Left_To_Right");
448is($charinfo->{decomposition},  "$I_code 0307");
449is(charprop($cp, 'dm'),         "I\x{0307}");
450is($charinfo->{decimal},        "");
451is($charinfo->{digit},          "");
452is($charinfo->{numeric},        "");
453is(charprop($cp, 'nv'),         "NaN");
454is($charinfo->{mirrored},       "N");
455is(charprop($cp, 'bidim'),      "No");
456is($charinfo->{unicode10},      "LATIN CAPITAL LETTER I DOT");
457is(charprop($cp, 'na1'),        "LATIN CAPITAL LETTER I DOT");
458is($charinfo->{comment},        "");
459is(charprop($cp, 'isc'),        "");
460is($charinfo->{upper},          "");
461is(charprop($cp, 'uc'),         "\x{130}");
462is($charinfo->{lower},          $i_code);
463is(charprop($cp, 'lc'),         "i\x{307}") if $v_unicode_version ge v3.2.0;
464is($charinfo->{title},          "");
465is(charprop($cp, 'tc'),         "\x{130}");
466is($charinfo->{block},          "Latin Extended-A");
467is(charprop($cp, 'block'),      "Latin_Extended_A");
468is($charinfo->{script},         "Latin") if $v_unicode_version gt v3.0.1;
469is(charprop($cp, 'script'),     "Latin") if $v_unicode_version gt v3.0.1;
470
471# This is to test a case where both simple and full uppercases exist and
472# differ
473$cp = 0x1F80;
474$charinfo = charinfo($cp);
475
476is($charinfo->{code},           "1F80", "GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI");
477is($charinfo->{name},           "GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI");
478is(charprop($cp, "name"),       "GREEK SMALL LETTER ALPHA WITH PSILI AND YPOGEGRAMMENI");
479is($charinfo->{category},       "Ll");
480is(charprop($cp, "gc"),         "Lowercase_Letter");
481is($charinfo->{combining},      "0");
482is(charprop($cp, "ccc"),        "Not_Reordered");
483is($charinfo->{bidi},           "L");
484is(charprop($cp, "bc"),         "Left_To_Right");
485is($charinfo->{decomposition},  "1F00 0345");
486is(charprop($cp, "dm"),         "\x{1F00}\x{0345}");
487is($charinfo->{decimal},        "");
488is($charinfo->{digit},          "");
489is($charinfo->{numeric},        "");
490is(charprop($cp, "nv"),         "NaN");
491is($charinfo->{mirrored},       "N");
492is(charprop($cp, "bidim"),      "No");
493is($charinfo->{unicode10},      "");
494is(charprop($cp, "na1"),        "");
495is($charinfo->{comment},        "");
496is(charprop($cp, "isc"),        "");
497is($charinfo->{upper},          "1F88");
498is(charprop($cp, "uc"),         "\x{1F08}\x{0399}");
499is(charprop($cp, "suc"),        "\x{1F88}");
500is($charinfo->{lower},          "");
501is(charprop($cp, "lc"),         "\x{1F80}");
502is($charinfo->{title},          "1F88");
503is(charprop($cp, "tc"),         "\x{1F88}");
504is($charinfo->{block},          "Greek Extended");
505is(charprop($cp, "block"),      "Greek_Extended");
506is($charinfo->{script},         "Greek") if $v_unicode_version gt v3.0.1;
507is(charprop($cp, "script"),     "Greek") if $v_unicode_version gt v3.0.1;
508
509is(charprop(ord("A"), "foo"),    undef,
510                        "Verify charprop of unknown property returns <undef>");
511
512# These were created from inspection of the code to exercise the branches
513if ($v_unicode_version ge v6.3.0) {
514    is(charprop(ord("("), "bpb"),    ")",
515            "Verify charprop figures out that s-type properties can be char");
516}
517is(charprop(ord("9"), "nv"),     9,
518                            "Verify charprop can adjust an ar-type property");
519if ($v_unicode_version ge v5.2.0) {
520    is(charprop(utf8::unicode_to_native(0xAD), "NFKC_Casefold"), "",
521                    "Verify charprop can handle an \"\" in ae-type property");
522}
523
524my $mark_props_ref = charprops_all(0x300);
525is($mark_props_ref->{'Bidi_Class'}, "Nonspacing_Mark",
526                                    "Next tests are charprops_all of 0x300");
527is($mark_props_ref->{'Bidi_Mirrored'}, "No");
528is($mark_props_ref->{'Canonical_Combining_Class'}, "Above");
529is($mark_props_ref->{'Case_Folding'}, "\x{300}");
530is($mark_props_ref->{'Decomposition_Mapping'}, "\x{300}");
531is($mark_props_ref->{'Decomposition_Type'}, ($v_unicode_version le v4.0.0)
532                                             ? "none"
533                                             : "None");
534is($mark_props_ref->{'General_Category'}, "Nonspacing_Mark");
535if ($v_unicode_version gt v5.1.0) {
536    is($mark_props_ref->{'ISO_Comment'}, "");
537}
538is($mark_props_ref->{'Lowercase_Mapping'}, "\x{300}");
539is($mark_props_ref->{'Name'}, "COMBINING GRAVE ACCENT");
540is($mark_props_ref->{'Numeric_Type'}, "None");
541is($mark_props_ref->{'Numeric_Value'}, "NaN");
542is($mark_props_ref->{'Simple_Case_Folding'}, "\x{300}");
543is($mark_props_ref->{'Simple_Lowercase_Mapping'}, "\x{300}");
544is($mark_props_ref->{'Simple_Titlecase_Mapping'}, "\x{300}");
545is($mark_props_ref->{'Simple_Uppercase_Mapping'}, "\x{300}");
546is($mark_props_ref->{'Titlecase_Mapping'}, "\x{300}");
547is($mark_props_ref->{'Unicode_1_Name'}, "NON-SPACING GRAVE");
548is($mark_props_ref->{'Uppercase_Mapping'}, "\x{300}");
549
550use Unicode::UCD qw(charblocks charscripts);
551
552my $charblocks = charblocks();
553
554ok(exists $charblocks->{Thai}, 'Thai charblock exists');
555is($charblocks->{Thai}->[0]->[0], hex('0e00'));
556ok(!exists $charblocks->{PigLatin}, 'PigLatin charblock does not exist');
557
558if ($v_unicode_version gt v3.0.1) {
559    my $charscripts = charscripts();
560
561    ok(exists $charscripts->{Armenian}, 'Armenian charscript exists');
562    is($charscripts->{Armenian}->[0]->[0], hex('0531'));
563    ok(!exists $charscripts->{PigLatin}, 'PigLatin charscript does not exist');
564
565    my $charscript;
566
567    $charscript = charscript("12ab");
568    is($charscript, 'Ethiopic', 'Ethiopic charscript');
569
570    $charscript = charscript("0x12ab");
571    is($charscript, 'Ethiopic');
572
573    $charscript = charscript("U+12ab");
574    is($charscript, 'Ethiopic');
575
576    my $ranges;
577
578    if ($v_unicode_version gt v4.0.0) {
579        $ranges = charscript('Ogham');
580        is($ranges->[0]->[0], hex('1680'), 'Ogham charscript');
581        is($ranges->[0]->[1], hex('169C'));
582    }
583
584    use Unicode::UCD qw(charinrange);
585
586    $ranges = charscript('Cherokee');
587    ok(!charinrange($ranges, "139f"), 'Cherokee charscript');
588    ok( charinrange($ranges, "13a0"));
589    ok( charinrange($ranges, "13f4"));
590    ok(!charinrange($ranges, "13ff"));
591}
592
593use Unicode::UCD qw(general_categories);
594
595my $gc = general_categories();
596
597ok(exists $gc->{L}, 'has L');
598is($gc->{L}, 'Letter', 'L is Letter');
599is($gc->{Lu}, 'UppercaseLetter', 'Lu is UppercaseLetter');
600
601use Unicode::UCD qw(bidi_types);
602
603my $bt = bidi_types();
604
605ok(exists $bt->{L}, 'has L');
606is($bt->{L}, 'Left-to-Right', 'L is Left-to-Right');
607is($bt->{AL}, 'Right-to-Left Arabic', 'AL is Right-to-Left Arabic');
608
609# If this fails, then maybe one should look at the Unicode changes to see
610# what else might need to be updated.
611ok($current_version le $expected_version,
612                    "Verify there isn't a new Unicode version to upgrade to");
613
614use Unicode::UCD qw(compexcl);
615
616ok(!compexcl(0x0100), 'compexcl');
617ok(!compexcl(0xD801), 'compexcl of surrogate');
618ok(!compexcl(0x110000), 'compexcl of non-Unicode code point');
619ok( compexcl(0x0958));
620
621use Unicode::UCD qw(casefold);
622
623my $casefold;
624
625$casefold = casefold(utf8::unicode_to_native(0x41));
626
627is($casefold->{code}, $A_code, 'casefold native(0x41) code');
628is($casefold->{status}, 'C', 'casefold native(0x41) status');
629is($casefold->{mapping}, $a_code, 'casefold native(0x41) mapping');
630is($casefold->{full}, $a_code, 'casefold native(0x41) full');
631is($casefold->{simple}, $a_code, 'casefold native(0x41) simple');
632is($casefold->{turkic}, "", 'casefold native(0x41) turkic');
633
634my $sharp_s_code = sprintf("%04X", utf8::unicode_to_native(0xdf));
635my $S_code = sprintf("%04X", ord "S");
636my $s_code = sprintf("%04X", ord "s");
637
638if ($v_unicode_version gt v3.0.0) { # These special ones don't work on early
639                                    # perls
640    $casefold = casefold(utf8::unicode_to_native(0xdf));
641
642    is($casefold->{code}, $sharp_s_code, 'casefold native(0xDF) code');
643    is($casefold->{status}, 'F', 'casefold native(0xDF) status');
644    is($casefold->{mapping}, "$s_code $s_code", 'casefold native(0xDF) mapping');
645    is($casefold->{full}, "$s_code $s_code", 'casefold native(0xDF) full');
646    is($casefold->{simple}, "", 'casefold native(0xDF) simple');
647    is($casefold->{turkic}, "", 'casefold native(0xDF) turkic');
648
649    # Do different tests depending on if version < 3.2, or not.
650    if ($v_unicode_version eq v3.0.1) {
651            # In this release, there was no special Turkic values.
652            # Both 0x130 and 0x131 folded to 'i'.
653
654            $casefold = casefold(0x130);
655
656            is($casefold->{code}, '0130', 'casefold 0x130 code');
657            is($casefold->{status}, 'C' , 'casefold 0x130 status');
658            is($casefold->{mapping}, $i_code, 'casefold 0x130 mapping');
659            is($casefold->{full}, $i_code, 'casefold 0x130 full');
660            is($casefold->{simple}, $i_code, 'casefold 0x130 simple');
661            is($casefold->{turkic}, "", 'casefold 0x130 turkic');
662
663            $casefold = casefold(0x131);
664
665            is($casefold->{code}, '0131', 'casefold 0x131 code');
666            is($casefold->{status}, 'C' , 'casefold 0x131 status');
667            is($casefold->{mapping}, $i_code, 'casefold 0x131 mapping');
668            is($casefold->{full}, $i_code, 'casefold 0x131 full');
669            is($casefold->{simple}, $i_code, 'casefold 0x131 simple');
670            is($casefold->{turkic}, "", 'casefold 0x131 turkic');
671    }
672    elsif ($v_unicode_version lt v3.2.0) {
673            $casefold = casefold(0x130);
674
675            is($casefold->{code}, '0130', 'casefold 0x130 code');
676            is($casefold->{status}, 'I' , 'casefold 0x130 status');
677            is($casefold->{mapping}, $i_code, 'casefold 0x130 mapping');
678            is($casefold->{full}, $i_code, 'casefold 0x130 full');
679            is($casefold->{simple}, $i_code, 'casefold 0x130 simple');
680            is($casefold->{turkic}, $i_code, 'casefold 0x130 turkic');
681
682            $casefold = casefold(0x131);
683
684            is($casefold->{code}, '0131', 'casefold 0x131 code');
685            is($casefold->{status}, 'I' , 'casefold 0x131 status');
686            is($casefold->{mapping}, $i_code, 'casefold 0x131 mapping');
687            is($casefold->{full}, $i_code, 'casefold 0x131 full');
688            is($casefold->{simple}, $i_code, 'casefold 0x131 simple');
689            is($casefold->{turkic}, $i_code, 'casefold 0x131 turkic');
690    } else {
691            $casefold = casefold(utf8::unicode_to_native(0x49));
692
693            is($casefold->{code}, $I_code, 'casefold native(0x49) code');
694            is($casefold->{status}, 'C' , 'casefold native(0x49) status');
695            is($casefold->{mapping}, $i_code, 'casefold native(0x49) mapping');
696            is($casefold->{full}, $i_code, 'casefold native(0x49) full');
697            is($casefold->{simple}, $i_code, 'casefold native(0x49) simple');
698            is($casefold->{turkic}, "0131", 'casefold native(0x49) turkic');
699
700            $casefold = casefold(0x130);
701
702            is($casefold->{code}, '0130', 'casefold 0x130 code');
703            is($casefold->{status}, 'F' , 'casefold 0x130 status');
704            is($casefold->{mapping}, "$i_code 0307", 'casefold 0x130 mapping');
705            is($casefold->{full}, "$i_code 0307", 'casefold 0x130 full');
706            is($casefold->{simple}, "", 'casefold 0x130 simple');
707            is($casefold->{turkic}, $i_code, 'casefold 0x130 turkic');
708    }
709
710    if ($v_unicode_version gt v3.0.1) {
711        $casefold = casefold(0x1F88);
712
713        is($casefold->{code}, '1F88', 'casefold 0x1F88 code');
714        is($casefold->{status}, 'S' , 'casefold 0x1F88 status');
715        is($casefold->{mapping}, '1F80', 'casefold 0x1F88 mapping');
716        is($casefold->{full}, '1F00 03B9', 'casefold 0x1F88 full');
717        is($casefold->{simple}, '1F80', 'casefold 0x1F88 simple');
718        is($casefold->{turkic}, "", 'casefold 0x1F88 turkic');
719    }
720}
721
722ok(!casefold(utf8::unicode_to_native(0x20)));
723
724use Unicode::UCD qw(casespec);
725
726my $casespec;
727
728ok(!casespec(utf8::unicode_to_native(0x41)));
729
730$casespec = casespec(utf8::unicode_to_native(0xdf));
731
732ok($casespec->{code} eq $sharp_s_code &&
733   $casespec->{lower} eq $sharp_s_code  &&
734   $casespec->{title} eq "$S_code $s_code"  &&
735   $casespec->{upper} eq "$S_code $S_code" &&
736   !defined $casespec->{condition}, 'casespec native(0xDF)');
737
738$casespec = casespec(0x307);
739
740if ($v_unicode_version gt v3.1.0) {
741    ok($casespec->{az}->{code} eq '0307'
742    && !defined $casespec->{az}->{lower}
743    && $casespec->{az}->{title} eq '0307'
744    && $casespec->{az}->{upper} eq '0307'
745    && $casespec->{az}->{condition} eq ($v_unicode_version le v3.2)
746                                        ? 'az After_Soft_Dotted'
747                                        : 'az After_I',
748    'casespec 0x307');
749}
750
751# perl #7305 UnicodeCD::compexcl is weird
752
753for (1) {my $a=compexcl $_}
754ok(1, 'compexcl read-only $_: perl #7305');
755map {compexcl $_} %{{1=>2}};
756ok(1, 'compexcl read-only hash: perl #7305');
757
758is(Unicode::UCD::_getcode('123'),     123, "_getcode(123)");
759is(Unicode::UCD::_getcode('0123'),  0x123, "_getcode(0123)");
760is(Unicode::UCD::_getcode('0x123'), 0x123, "_getcode(0x123)");
761is(Unicode::UCD::_getcode('0X123'), 0x123, "_getcode(0X123)");
762is(Unicode::UCD::_getcode('U+123'), 0x123, "_getcode(U+123)");
763is(Unicode::UCD::_getcode('u+123'), 0x123, "_getcode(u+123)");
764is(Unicode::UCD::_getcode('U+1234'),   0x1234, "_getcode(U+1234)");
765is(Unicode::UCD::_getcode('U+12345'), 0x12345, "_getcode(U+12345)");
766is(Unicode::UCD::_getcode('123x'),    undef, "_getcode(123x)");
767is(Unicode::UCD::_getcode('x123'),    undef, "_getcode(x123)");
768is(Unicode::UCD::_getcode('0x123x'),  undef, "_getcode(x123)");
769is(Unicode::UCD::_getcode('U+123x'),  undef, "_getcode(x123)");
770
771SKIP:
772{
773    skip("Script property not in this release", 3) if $v_unicode_version lt v3.1.0;
774
775    {
776        my @warnings;
777        local $SIG{__WARN__} = sub { push @warnings, @_  };
778        is(charscript(chr(0x6237)), undef,
779           "Verify charscript of non-code point returns <undef>");
780        cmp_ok(scalar @warnings, '==', 1, "  ... and generates 1 warning");
781        like($warnings[0], qr/unknown code/, "  ... with the right text");
782    }
783
784    my $r1 = charscript('Latin');
785    if (ok(defined $r1, "Found Latin script")) {
786        skip("Latin range count will be wrong when using older Unicode release",
787             2) if $current_version lt $expected_version;
788        my $n1 = @$r1;
789        is($n1, 39, "number of ranges in Latin script (Unicode $expected_version)") if $::IS_ASCII;
790        shift @$r1 while @$r1;
791        my $r2 = charscript('Latin');
792        is(@$r2, $n1, "modifying results should not mess up internal caches");
793    }
794}
795
796{
797	is(charinfo(0xdeadbeef), undef, "[perl #23273] warnings in Unicode::UCD");
798}
799
800if ($v_unicode_version ge v4.1.0) {
801    use Unicode::UCD qw(namedseq);
802
803    is(namedseq("KEYCAP DIGIT ZERO"), "0\x{FE0F}\x{20E3}",
804                "namedseq with char that varies under EBCDIC");
805    is(namedseq("KATAKANA LETTER AINU P"), "\x{31F7}\x{309A}", "namedseq");
806    is(namedseq("KATAKANA LETTER AINU Q"), undef);
807    is(namedseq(), undef);
808    is(namedseq(qw(foo bar)), undef);
809    my @ns = namedseq("KATAKANA LETTER AINU P");
810    is(scalar @ns, 2);
811    is($ns[0], 0x31F7);
812    is($ns[1], 0x309A);
813    my %ns = namedseq();
814    is($ns{"KATAKANA LETTER AINU P"}, "\x{31F7}\x{309A}");
815    @ns = namedseq(42);
816    is(@ns, 0);
817}
818
819use Unicode::UCD qw(num);
820use charnames ();   # Don't use \N{} on things not in original Unicode
821                    # version; else will get a compilation error when this .t
822                    # is run on an older version.
823
824my $ret_len;
825is(num("0"), 0, 'Verify num("0") == 0');
826is(num("0", \$ret_len), 0, 'Verify num("0", \$ret_len) == 0');
827is($ret_len, 1, "... and the returned length is 1");
828ok(! defined num("", \$ret_len), 'Verify num("", \$ret_len) isnt defined');
829is($ret_len, 0, "... and the returned length is 0");
830ok(! defined num("A", \$ret_len), 'Verify num("A") isnt defined');
831is($ret_len, 0, "... and the returned length is 0");
832is(num("98765", \$ret_len), 98765, 'Verify num("98765") == 98765');
833is($ret_len, 5, "... and the returned length is 5");
834ok(! defined num("98765\N{FULLWIDTH DIGIT FOUR}", \$ret_len),
835   'Verify num("98765\N{FULLWIDTH DIGIT FOUR}") isnt defined');
836is($ret_len, 5, "... but the returned length is 5");
837my $tai_lue_2;
838if ($v_unicode_version ge v4.1.0) {
839    my $tai_lue_1 = charnames::string_vianame("NEW TAI LUE DIGIT ONE");
840    $tai_lue_2 = charnames::string_vianame("NEW TAI LUE DIGIT TWO");
841    is(num($tai_lue_2), 2, 'Verify num("\N{NEW TAI LUE DIGIT TWO}") == 2');
842    is(num($tai_lue_1), 1, 'Verify num("\N{NEW TAI LUE DIGIT ONE}") == 1');
843    is(num($tai_lue_2 . $tai_lue_1), 21,
844       'Verify num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE DIGIT ONE}") == 21');
845}
846if ($v_unicode_version ge v5.2.0) {
847    ok(! defined num($tai_lue_2
848         . charnames::string_vianame("NEW TAI LUE THAM DIGIT ONE"), \$ret_len),
849         'Verify num("\N{NEW TAI LUE DIGIT TWO}\N{NEW TAI LUE THAM DIGIT ONE}") isnt defined');
850    is($ret_len, 1, "... but the returned length is 1");
851    ok(! defined num(charnames::string_vianame("NEW TAI LUE THAM DIGIT ONE")
852                     .  $tai_lue_2, \$ret_len),
853         'Verify num("\N{NEW TAI LUE THAM DIGIT ONE}\N{NEW TAI LUE DIGIT TWO}") isnt defined');
854    is($ret_len, 1, "... but the returned length is 1");
855}
856if ($v_unicode_version ge v5.1.0) {
857    my $cham_0 = charnames::string_vianame("CHAM DIGIT ZERO");
858    is(num($cham_0 . charnames::string_vianame("CHAM DIGIT THREE")), 3,
859       'Verify num("\N{CHAM DIGIT ZERO}\N{CHAM DIGIT THREE}") == 3');
860    if ($v_unicode_version ge v5.2.0) {
861        ok(! defined num(  $cham_0
862                         . charnames::string_vianame("JAVANESE DIGIT NINE"),
863                         \$ret_len),
864        'Verify num("\N{CHAM DIGIT ZERO}\N{JAVANESE DIGIT NINE}") isnt defined');
865    is($ret_len, 1, "... but the returned length is 1");
866    }
867}
868is(num("\N{SUPERSCRIPT TWO}"), 2, 'Verify num("\N{SUPERSCRIPT TWO} == 2');
869if ($v_unicode_version ge v3.0.0) {
870    is(num(charnames::string_vianame("ETHIOPIC NUMBER TEN THOUSAND")), 10000,
871       'Verify num("\N{ETHIOPIC NUMBER TEN THOUSAND}") == 10000');
872}
873if ($v_unicode_version ge v5.2.0) {
874    is(num(charnames::string_vianame("NORTH INDIC FRACTION ONE HALF")),
875       .5,
876       'Verify num("\N{NORTH INDIC FRACTION ONE HALF}") == .5');
877    is(num("\N{U+12448}"), 9, 'Verify num("\N{U+12448}") == 9');
878}
879if ($v_unicode_version gt v3.2.0) { # Is missing from non-Unihan files before
880                                    # this
881    is(num("\N{U+5146}"), 1000000000000,
882                                'Verify num("\N{U+5146}") == 1000000000000');
883}
884
885# Create a user-defined property
886sub InKana {<<'END'}
8873040    309F
88830A0    30FF
889END
890
891use Unicode::UCD qw(prop_aliases);
892
893is(prop_aliases(undef), undef, "prop_aliases(undef) returns <undef>");
894is(prop_aliases("unknown property"), undef,
895                "prop_aliases(<unknown property>) returns <undef>");
896is(prop_aliases("InKana"), undef,
897                "prop_aliases(<user-defined property>) returns <undef>");
898is(prop_aliases("Perl_Decomposition_Mapping"), undef, "prop_aliases('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
899is(prop_aliases("Perl_Charnames"), undef,
900    "prop_aliases('Perl_Charnames') returns <undef> since internal-Perl-only");
901is(prop_aliases("isgc"), undef,
902    "prop_aliases('isgc') returns <undef> since is not covered Perl extension");
903is(prop_aliases("Is_Is_Any"), undef,
904                "prop_aliases('Is_Is_Any') returns <undef> since two is's");
905is(prop_aliases("ccc=vr"), undef,
906                          "prop_aliases('ccc=vr') doesn't generate a warning");
907
908# Keys are lists of properties. Values are defined if have been tested.
909my %props;
910
911# To test for loose matching, add in the characters that are ignored there.
912my $extra_chars = "-_ ";
913
914# The one internal property we accept
915$props{'Perl_Decimal_Digit'} = 1;
916my @list = prop_aliases("perldecimaldigit");
917is_deeply(\@list,
918          [ "Perl_Decimal_Digit",
919            "Perl_Decimal_Digit"
920          ], "prop_aliases('perldecimaldigit') returns Perl_Decimal_Digit as both short and full names");
921
922# Get the official Unicode property name synonyms and test them.
923
924SKIP: {
925skip "PropertyAliases.txt is not in this Unicode version", 1 if $v_unicode_version lt v3.2.0;
926open my $props, "<", "../lib/unicore/PropertyAliases.txt"
927                or die "Can't open Unicode PropertyAliases.txt";
928local $/ = "\n";
929while (<$props>) {
930    s/\s*#.*//;           # Remove comments
931    next if /^\s* $/x;    # Ignore empty and comment lines
932
933    chomp;
934    local $/ = $input_record_separator;
935    my $count = 0;  # 0th field in line is short name; 1th is long name
936    my $short_name;
937    my $full_name;
938    my @names_via_short;
939    foreach my $alias (split /\s*;\s*/) {    # Fields are separated by
940                                             # semi-colons
941        # Add in the characters that are supposed to be ignored, to test loose
942        # matching, which the tested function does on all inputs.
943        my $mod_name = "$extra_chars$alias";
944
945        my $loose = &Unicode::UCD::loose_name(lc $alias);
946
947        # Indicate we have tested this.
948        $props{$loose} = 1;
949
950        my @all_names = prop_aliases($mod_name);
951        if (grep { $_ eq $loose } @Unicode::UCD::suppressed_properties) {
952            is(@all_names, 0, "prop_aliases('$mod_name') returns undef since $alias is not installed");
953            next;
954        }
955        elsif (! @all_names) {
956            fail("prop_aliases('$mod_name')");
957            diag("'$alias' is unknown to prop_aliases()");
958            next;
959        }
960
961        if ($count == 0) {  # Is short name
962
963            @names_via_short = prop_aliases($mod_name);
964
965            # If the 0th test fails, no sense in continuing with the others
966            last unless is($names_via_short[0], $alias,
967                    "prop_aliases: '$alias' is the short name for '$mod_name'");
968            $short_name = $alias;
969        }
970        elsif ($count == 1) {   # Is full name
971
972            # Some properties have the same short and full name; no sense
973            # repeating the test if the same.
974            if ($alias ne $short_name) {
975                my @names_via_full = prop_aliases($mod_name);
976                is_deeply(\@names_via_full, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
977            }
978
979            # Tests scalar context
980            is(prop_aliases($short_name), $alias,
981                "prop_aliases: '$alias' is the long name for '$short_name'");
982        }
983        else {  # Is another alias
984            is_deeply(\@all_names, \@names_via_short, "prop_aliases() returns the same list for both '$short_name' and '$mod_name'");
985            ok((grep { $_ =~ /^$alias$/i } @all_names),
986                "prop_aliases: '$alias' is listed as an alias for '$mod_name'");
987        }
988
989        $count++;
990    }
991}
992} # End of SKIP block
993
994# Now test anything we can find that wasn't covered by the tests of the
995# official properties.  We have no way of knowing if mktables omitted a Perl
996# extension or not, but we do the best we can from its generated lists
997
998foreach my $alias (sort keys %Unicode::UCD::loose_to_file_of) {
999    next if $alias =~ /=/;
1000    my $lc_name = lc $alias;
1001    my $loose = &Unicode::UCD::loose_name($lc_name);
1002    next if exists $props{$loose};  # Skip if already tested
1003    $props{$loose} = 1;
1004    my $mod_name = "$extra_chars$alias";    # Tests loose matching
1005    my @aliases = prop_aliases($mod_name);
1006    my $found_it = grep { &Unicode::UCD::loose_name(lc $_) eq $lc_name } @aliases;
1007    if ($found_it) {
1008        pass("prop_aliases: '$lc_name' is listed as an alias for '$mod_name'");
1009    }
1010    elsif ($lc_name =~ /l[_&]$/) {
1011
1012        # These two names are special in that they don't appear in the
1013        # returned list because they are discouraged from use.  Verify
1014        # that they return the same list as a non-discouraged version.
1015        my @LC = prop_aliases('Is_LC');
1016        is_deeply(\@aliases, \@LC, "prop_aliases: '$lc_name' returns the same list as 'Is_LC'");
1017    }
1018    else {
1019        my $stripped = $lc_name =~ s/^is//;
1020
1021        # Could be that the input includes a prefix 'is', which is rarely
1022        # returned as an alias, so having successfully stripped it off above,
1023        # try again.
1024        if ($stripped) {
1025            $found_it = grep { &Unicode::UCD::loose_name(lc $_) eq $lc_name } @aliases;
1026        }
1027
1028        # If that didn't work, it could be that it's a block, which is always
1029        # returned with a leading 'In_' to avoid ambiguity.  Try comparing
1030        # with that stripped off.
1031        if (! $found_it) {
1032            $found_it = grep { &Unicode::UCD::loose_name(s/^In_(.*)/\L$1/r) eq $lc_name }
1033                              @aliases;
1034            # Could check that is a real block, but tests for invmap will
1035            # likely pickup any errors, since this will be tested there.
1036            $lc_name = "in$lc_name" if $found_it;   # Change for message below
1037        }
1038        my $message = "prop_aliases: '$lc_name' is listed as an alias for '$mod_name'";
1039        ($found_it) ? pass($message) : fail($message);
1040    }
1041}
1042
1043# Some of the Perl extensions should always be built; make sure they have the
1044# correct full name, etc.
1045for my $prop (qw(Alnum Blank Cntrl Digit Graph Print Word XDigit)) {
1046    my @expected = ( $prop, "XPosix$prop" );
1047    my @got = prop_aliases($prop);
1048    splice @got, 2;
1049    is_deeply(\@got, \@expected, "Got expected aliases for $prop");
1050}
1051
1052my $done_equals = 0;
1053foreach my $alias (keys %Unicode::UCD::stricter_to_file_of) {
1054    if ($alias =~ /=/) {    # Only test one case where there is an equals
1055        next if $done_equals;
1056        $done_equals = 1;
1057    }
1058    my $lc_name = lc $alias;
1059    my @list = prop_aliases($alias);
1060    if ($alias =~ /^_/) {
1061        is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since it is internal_only");
1062    }
1063    elsif ($alias =~ /=/) {
1064        is(@list, 0, "prop_aliases: '$lc_name' returns an empty list since is illegal property name");
1065    }
1066    else {
1067        ok((grep { lc $_ eq $lc_name } @list),
1068                "prop_aliases: '$lc_name' is listed as an alias for '$alias'");
1069    }
1070}
1071
1072use Unicode::UCD qw(prop_values prop_value_aliases);
1073
1074is(prop_value_aliases("unknown property", "unknown value"), undef,
1075    "prop_value_aliases(<unknown property>, <unknown value>) returns <undef>");
1076is(prop_value_aliases(undef, undef), undef,
1077                           "prop_value_aliases(undef, undef) returns <undef>");
1078is((prop_value_aliases("na", "A")), "A", "test that prop_value_aliases returns its input for properties that don't have synonyms");
1079is(prop_value_aliases("isgc", "C"), undef, "prop_value_aliases('isgc', 'C') returns <undef> since is not covered Perl extension");
1080is(prop_value_aliases("gc", "isC"), undef, "prop_value_aliases('gc', 'isC') returns <undef> since is not covered Perl extension");
1081is(prop_value_aliases("Any", "None"), undef, "prop_value_aliases('Any', 'None') returns <undef> since is Perl extension and 'None' is not valid");
1082is(prop_value_aliases("lc", "A"), "A", "prop_value_aliases('lc', 'A') returns its input, as docs say it does");
1083
1084# We have no way of knowing if mktables omitted a Perl extension that it
1085# shouldn't have, but we can check if it omitted an official Unicode property
1086# name synonym.  And for those, we can check if the short and full names are
1087# correct.
1088
1089my %pva_tested;   # List of things already tested.
1090
1091SKIP: {
1092skip "PropValueAliases.txt is not in this Unicode version", 1 if $v_unicode_version lt v3.2.0;
1093open my $propvalues, "<", "../lib/unicore/PropValueAliases.txt"
1094     or die "Can't open Unicode PropValueAliases.txt";
1095local $/ = "\n";
1096
1097# Each examined line in the file is for a single value for a property.  We
1098# accumulate all the values for each property using these two variables.
1099my $prev_prop = "";
1100my @this_prop_values;
1101
1102while (<$propvalues>) {
1103    s/\s*#.*//;           # Remove comments
1104    next if /^\s* $/x;    # Ignore empty and comment lines
1105    chomp;
1106    local $/ = $input_record_separator;
1107
1108    # Fix typo in official input file
1109    s/CCC133/CCC132/g if $v_unicode_version eq v6.1.0;
1110
1111    my @fields = split /\s*;\s*/; # Fields are separated by semi-colons
1112    my $prop = shift @fields;   # 0th field is the property,
1113
1114    # 'qc' is short in early versions of the file for any of the quick check
1115    # properties.  Choose one of them.
1116    if ($prop eq 'qc' && $v_unicode_version le v4.0.0) {
1117        $prop = "NFKC_QC";
1118    }
1119
1120    # When changing properties, we examine the accumulated values for the old
1121    # one to see if our function that returns them matches.
1122    if ($prev_prop ne $prop) {
1123        if ($prev_prop ne "") { # Skip for the first time through
1124            my @ucd_function_values = prop_values($prev_prop);
1125            @ucd_function_values = () unless @ucd_function_values;
1126
1127            # The file didn't include strictly numeric values until after this
1128            if ($prev_prop eq 'ccc' && $v_unicode_version le v6.0.0) {
1129                @ucd_function_values = grep { /\D/ } @ucd_function_values;
1130            }
1131
1132            # This perl extension doesn't appear in the official file
1133            push @this_prop_values, "Non_Canon" if $prev_prop eq 'dt';
1134
1135            my @file_values = undef;
1136            @file_values = sort { lc($a =~ s/_//gr) cmp lc($b =~ s/_//gr) }
1137                                   @this_prop_values if @this_prop_values;
1138            is_deeply(\@ucd_function_values, \@file_values,
1139              "prop_values('$prev_prop') returns correct list of values");
1140        }
1141        $prev_prop = $prop;
1142        undef @this_prop_values;
1143    }
1144
1145    my $count = 0;  # 0th field in line (after shifting off the property) is
1146                    # short name; 1th is long name
1147    my $short_name;
1148    my @names_via_short;    # Saves the values between iterations
1149
1150    # The property on the lhs of the = is always loosely matched.  Add in
1151    # characters that are ignored under loose matching to test that
1152    my $mod_prop = "$extra_chars$prop";
1153
1154    if ($prop eq 'blk' && $v_unicode_version le v5.0.0) {
1155        foreach my $element (@fields) {
1156            $element =~ s/-/_/g;
1157        }
1158    }
1159
1160    if ($fields[0] eq 'n/a') {  # See comments in input file, essentially
1161                                # means full name and short name are identical
1162        $fields[0] = $fields[1];
1163    }
1164    elsif ($fields[0] ne $fields[1]
1165           && &Unicode::UCD::loose_name(lc $fields[0])
1166               eq &Unicode::UCD::loose_name(lc $fields[1])
1167           && $fields[1] !~ /[[:upper:]]/)
1168    {
1169        # Also, there is a bug in the file in which "n/a" is omitted, and
1170        # the two fields are identical except for case, and the full name
1171        # is all lower case.  Copy the "short" name unto the full one to
1172        # give it some upper case.
1173
1174        $fields[1] = $fields[0];
1175    }
1176
1177    # The ccc property in the file is special; has an extra numeric field
1178    # (0th), which should go at the end, since we use the next two fields as
1179    # the short and full names, respectively.  See comments in input file.
1180    splice (@fields, 0, 0, splice(@fields, 1, 2)) if $prop eq 'ccc';
1181
1182    my $loose_prop = &Unicode::UCD::loose_name(lc $prop);
1183    my $suppressed = grep { $_ eq $loose_prop }
1184                          @Unicode::UCD::suppressed_properties;
1185    push @this_prop_values, $fields[0] unless $suppressed;
1186    foreach my $value (@fields) {
1187        if ($suppressed) {
1188            is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop");
1189            next;
1190        }
1191        elsif (grep { $_ eq ("$loose_prop=" . &Unicode::UCD::loose_name(lc $value)) } @Unicode::UCD::suppressed_properties) {
1192            is(prop_value_aliases($prop, $value), undef, "prop_value_aliases('$prop', '$value') returns undef for suppressed property $prop=$value");
1193            next;
1194        }
1195
1196        # Add in test for loose matching.
1197        my $mod_value = "$extra_chars$value";
1198
1199        # If the value is a number, optionally negative, including a floating
1200        # point or rational numer, it should be only strictly matched, so the
1201        # loose matching should fail.
1202        if ($value =~ / ^ -? \d+ (?: [\/.] \d+ )? $ /x) {
1203            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");
1204
1205            # And reset so below tests just the strict matching.
1206            $mod_value = $value;
1207        }
1208
1209        if ($count == 0) {
1210
1211            @names_via_short = prop_value_aliases($mod_prop, $mod_value);
1212
1213            # If the 0th test fails, no sense in continuing with the others
1214            last unless is($names_via_short[0], $value, "prop_value_aliases: In '$prop', '$value' is the short name for '$mod_value'");
1215            $short_name = $value;
1216        }
1217        elsif ($count == 1) {
1218
1219            # Some properties have the same short and full name; no sense
1220            # repeating the test if the same.
1221            if ($value ne $short_name) {
1222                my @names_via_full =
1223                            prop_value_aliases($mod_prop, $mod_value);
1224                is_deeply(\@names_via_full, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
1225            }
1226
1227            # Tests scalar context
1228            is(prop_value_aliases($prop, $short_name), $value, "'$value' is the long name for prop_value_aliases('$prop', '$short_name')");
1229        }
1230        else {
1231            my @all_names = prop_value_aliases($mod_prop, $mod_value);
1232            is_deeply(\@all_names, \@names_via_short, "In '$prop', prop_value_aliases() returns the same list for both '$short_name' and '$mod_value'");
1233            ok((grep { &Unicode::UCD::loose_name(lc $_) eq &Unicode::UCD::loose_name(lc $value) } prop_value_aliases($prop, $short_name)), "'$value' is listed as an alias for prop_value_aliases('$prop', '$short_name')");
1234        }
1235
1236        $pva_tested{&Unicode::UCD::loose_name(lc $prop) . "=" . &Unicode::UCD::loose_name(lc $value)} = 1;
1237        $count++;
1238    }
1239}
1240}   # End of SKIP block
1241
1242# And test as best we can, the non-official pva's that mktables generates.
1243foreach my $hash (\%Unicode::UCD::loose_to_file_of, \%Unicode::UCD::stricter_to_file_of) {
1244    foreach my $test (sort keys %$hash) {
1245        next if exists $pva_tested{$test};  # Skip if already tested
1246
1247        my ($prop, $value) = split "=", $test;
1248        next unless defined $value; # prop_value_aliases() requires an input
1249                                    # 'value'
1250        my $mod_value;
1251        if ($hash == \%Unicode::UCD::loose_to_file_of) {
1252
1253            # Add extra characters to test loose-match rhs value
1254            $mod_value = "$extra_chars$value";
1255        }
1256        else { # Here value is strictly matched.
1257
1258            # Extra elements are added by mktables to this hash so that
1259            # something like "age=6.0" has a synonym of "age=6".  It's not
1260            # clear to me (khw) if we should be encouraging those synonyms, so
1261            # don't test for them.
1262            next if $value !~ /\D/ && exists $hash->{"$prop=$value.0"};
1263
1264            # Verify that loose matching fails when only strict is called for.
1265            next unless is(prop_value_aliases($prop, "$extra_chars$value"), undef,
1266                        "prop_value_aliases('$prop', '$extra_chars$value') returns undef since '$value' should be strictly matched"),
1267
1268            # Strict matching does allow for underscores between digits.  Test
1269            # for that.
1270            $mod_value = $value;
1271            while ($mod_value =~ s/(\d)(\d)/$1_$2/g) {}
1272        }
1273
1274        # The lhs property is always loosely matched, so add in extra
1275        # characters to test that.
1276        my $mod_prop = "$extra_chars$prop";
1277
1278        if ($prop eq 'gc' && $value =~ /l[_&]$/) {
1279            # These two names are special in that they don't appear in the
1280            # returned list because they are discouraged from use.  Verify
1281            # that they return the same list as a non-discouraged version.
1282            my @LC = prop_value_aliases('gc', 'lc');
1283            my @l_ = prop_value_aliases($mod_prop, $mod_value);
1284            is_deeply(\@l_, \@LC, "prop_value_aliases('$mod_prop', '$mod_value) returns the same list as prop_value_aliases('gc', 'lc')");
1285        }
1286        else {
1287            ok((grep { &Unicode::UCD::loose_name(lc $_) eq &Unicode::UCD::loose_name(lc $value) }
1288                prop_value_aliases($mod_prop, $mod_value)),
1289                "'$value' is listed as an alias for prop_value_aliases('$mod_prop', '$mod_value')");
1290        }
1291    }
1292}
1293
1294undef %pva_tested;
1295
1296no warnings 'once'; # We use some values once from 'required' modules.
1297
1298use Unicode::UCD qw(prop_invlist prop_invmap MAX_CP);
1299
1300# There were some problems with caching interfering with prop_invlist() vs
1301# prop_invmap() on binary properties, and also between the 3 properties where
1302# Perl used the same 'To' name as another property (see Unicode::UCD).
1303# So, before testing all of prop_invlist(),
1304#   1)  call prop_invmap() to try both orders of these name issues.  This uses
1305#       up two of the 3 properties;  the third will be left so that invlist()
1306#       on it gets called before invmap()
1307#   2)  call prop_invmap() on a generic binary property, ahead of invlist().
1308# This should test that the caching works in both directions.
1309
1310# These properties are not stable between Unicode versions, but the first few
1311# elements are; just look at the first element to see if are getting the
1312# distinction right.  The general inversion map testing below will test the
1313# whole thing.
1314
1315my $prop;
1316my ($invlist_ref, $invmap_ref, $format, $missing);
1317if ($::IS_ASCII) { # On EBCDIC, other things will come first, and can vary
1318                # according to code page
1319    $prop = "uc";
1320    ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
1321    is($format, 'al', "prop_invmap() format of '$prop' is 'al'");
1322    is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
1323    is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
1324    is($invmap_ref->[1], 0x41, "prop_invmap('$prop') map[1] is 0x41");
1325
1326    $prop = "upper";
1327    ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
1328    is($format, 's', "prop_invmap() format of '$prop' is 's");
1329    is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
1330    is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
1331    is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
1332
1333    $prop = "lower";
1334    ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
1335    is($format, 's', "prop_invmap() format of '$prop' is 's'");
1336    is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
1337    is($invlist_ref->[1], 0x61, "prop_invmap('$prop') list[1] is 0x61");
1338    is($invmap_ref->[1], 'Y', "prop_invmap('$prop') map[1] is 'Y'");
1339
1340    $prop = "lc";
1341    ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
1342    my $lc_format = ($v_unicode_version ge v3.2.0) ? 'al' : 'a';
1343    is($format, $lc_format, "prop_invmap() format of '$prop' is '$lc_format");
1344    is($missing, '0', "prop_invmap() missing of '$prop' is '0'");
1345    is($invlist_ref->[1], 0x41, "prop_invmap('$prop') list[1] is 0x41");
1346    is($invmap_ref->[1], 0x61, "prop_invmap('$prop') map[1] is 0x61");
1347}
1348
1349# This property is stable and small, so can test all of it
1350if ($v_unicode_version gt v3.1.0) {
1351    $prop = "ASCII_Hex_Digit";
1352    ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($prop);
1353    is($format, 's', "prop_invmap() format of '$prop' is 's'");
1354    is($missing, 'N', "prop_invmap() missing of '$prop' is 'N'");
1355    if ($::IS_ASCII) {
1356        is_deeply($invlist_ref, [ 0x0000, 0x0030, 0x003A,
1357                                0x0041, 0x0047,
1358                                0x0061, 0x0067, 0x110000
1359                                ],
1360            "prop_invmap('$prop') code point list is correct");
1361    }
1362    elsif ($::IS_EBCDIC) {
1363        is_deeply($invlist_ref, [
1364                utf8::unicode_to_native(0x0000),
1365                utf8::unicode_to_native(0x0061), utf8::unicode_to_native(0x0066) + 1,
1366                utf8::unicode_to_native(0x0041), utf8::unicode_to_native(0x0046) + 1,
1367                utf8::unicode_to_native(0x0030), utf8::unicode_to_native(0x0039) + 1,
1368                utf8::unicode_to_native(0x110000)
1369            ],
1370            "prop_invmap('$prop') code point list is correct");
1371    }
1372    is_deeply($invmap_ref, [ 'N', 'Y', 'N', 'Y', 'N', 'Y', 'N', 'N' ] ,
1373            "prop_invmap('$prop') map list is correct");
1374}
1375
1376is(prop_invlist("Unknown property"), undef, "prop_invlist(<Unknown property>) returns undef");
1377is(prop_invlist(undef), undef, "prop_invlist(undef) returns undef");
1378is(prop_invlist("Any"), 2, "prop_invlist('Any') returns the number of elements in scalar context");
1379my @invlist = prop_invlist("Is_Any");
1380is_deeply(\@invlist, [ 0, 0x110000 ], "prop_invlist works on 'Is_' prefixes");
1381is(prop_invlist("Is_Is_Any"), undef, "prop_invlist('Is_Is_Any') returns <undef> since two is's");
1382
1383use Storable qw(dclone);
1384
1385is(prop_invlist("InKana"), undef, "prop_invlist(<user-defined property returns undef>)");
1386
1387# The way both the tests for invlist and invmap work is that they take the
1388# lists returned by the functions and construct from them what the original
1389# file should look like, which are then compared with the file.  If they are
1390# identical, the test passes.  What this tests isn't that the results are
1391# correct, but that invlist and invmap haven't introduced errors beyond what
1392# are there in the files.  As a small hedge against that, test some
1393# prop_invlist() tables fully with the known correct result.  We choose
1394# ASCII_Hex_Digit again, as it is stable.
1395if ($v_unicode_version gt v3.1.0) {
1396    if ($::IS_ASCII) {
1397        @invlist = prop_invlist("AHex");
1398        is_deeply(\@invlist, [ 0x0030, 0x003A, 0x0041,
1399                                    0x0047, 0x0061, 0x0067 ],
1400            "prop_invlist('AHex') is exactly the expected set of points");
1401        @invlist = prop_invlist("AHex=f");
1402        is_deeply(\@invlist, [ 0x0000, 0x0030, 0x003A, 0x0041,
1403                                    0x0047, 0x0061, 0x0067 ],
1404            "prop_invlist('AHex=f') is exactly the expected set of points");
1405    }
1406    elsif ($::IS_EBCDIC) { # Relies on the ranges 0-9, a-f, and A-F each being
1407                        # contiguous
1408        @invlist = prop_invlist("AHex");
1409        is_deeply(\@invlist, [
1410                utf8::unicode_to_native(0x0061), utf8::unicode_to_native(0x0066) + 1,
1411                utf8::unicode_to_native(0x0041), utf8::unicode_to_native(0x0046) + 1,
1412                utf8::unicode_to_native(0x0030), utf8::unicode_to_native(0x0039) + 1,
1413        ],
1414        "prop_invlist('AHex') is exactly the expected set of points");
1415        @invlist = prop_invlist("AHex=f");
1416        is_deeply(\@invlist, [
1417                utf8::unicode_to_native(0x0000),
1418                utf8::unicode_to_native(0x0061),
1419                utf8::unicode_to_native(0x0066) + 1,
1420                utf8::unicode_to_native(0x0041),
1421                utf8::unicode_to_native(0x0046) + 1,
1422                utf8::unicode_to_native(0x0030),
1423                utf8::unicode_to_native(0x0039) + 1,
1424        ],
1425        "prop_invlist('AHex=f') is exactly the expected set of points");
1426    }
1427}
1428
1429sub fail_with_diff ($$$$) {
1430    # For use below to output better messages
1431    my ($prop, $official, $constructed, $tested_function_name) = @_;
1432
1433    if (! $ENV{PERL_TEST_DIFF}) {
1434
1435        is($constructed, $official, "$tested_function_name('$prop')");
1436
1437        diag("Set environment variable PERL_TEST_DIFF=diff_tool to see just "
1438           . "the differences.");
1439        return;
1440    }
1441
1442    fail("$tested_function_name('$prop')");
1443
1444    require File::Temp;
1445    my $off = File::Temp->new();
1446    local $/ = "\n";
1447    chomp $official;
1448    print $off $official, "\n";
1449    close $off || die "Can't close official";
1450
1451    chomp $constructed;
1452    my $gend = File::Temp->new();
1453    print $gend $constructed, "\n";
1454    close $gend || die "Can't close gend";
1455
1456    my $diff = File::Temp->new();
1457    system("$ENV{PERL_TEST_DIFF} $off $gend > $diff");
1458
1459    open my $fh, "<", $diff || die "Can't open $diff";
1460    my @diffs = <$fh>;
1461    diag("In the diff output below '<' marks lines from the filesystem tables;\n'>' are from $tested_function_name()");
1462    diag(@diffs);
1463}
1464
1465my %tested_invlist;
1466
1467# Look at everything we think that mktables tells us exists, both loose and
1468# strict
1469foreach my $set_of_tables (\%Unicode::UCD::stricter_to_file_of, \%Unicode::UCD::loose_to_file_of)
1470{
1471    foreach my $table (sort keys %$set_of_tables) {
1472
1473        my $mod_table;
1474        my ($prop_only, $value) = split "=", $table;
1475        if (defined $value) {
1476
1477            # If this is to be loose matched, add in characters to test that.
1478            if ($set_of_tables == \%Unicode::UCD::loose_to_file_of) {
1479                $value = "$extra_chars$value";
1480            }
1481            else {  # Strict match
1482
1483                # Verify that loose matching fails when only strict is called
1484                # for.
1485                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");
1486
1487                # Strict matching does allow for underscores between digits.
1488                # Test for that.
1489                while ($value =~ s/(\d)(\d)/$1_$2/g) {}
1490            }
1491
1492            # The property portion in compound form specifications always
1493            # matches loosely
1494            $mod_table = "$extra_chars$prop_only = $value";
1495        }
1496        else {  # Single-form.
1497
1498            # Like above, use loose if required, and insert underscores
1499            # between digits if strict.
1500            if ($set_of_tables == \%Unicode::UCD::loose_to_file_of) {
1501                $mod_table = "$extra_chars$table";
1502            }
1503            else {
1504                $mod_table = $table;
1505                while ($mod_table =~ s/(\d)(\d)/$1_$2/g) {}
1506            }
1507        }
1508
1509        my @tested = prop_invlist($mod_table);
1510        if ($table =~ /^_/) {
1511            is(@tested, 0, "prop_invlist('$mod_table') returns an empty list since is internal-only");
1512            next;
1513        }
1514
1515        # If we have already tested a property that uses the same file, this
1516        # list should be identical to the one that was tested, and can bypass
1517        # everything else.
1518        my $file = $set_of_tables->{$table};
1519        if (exists $tested_invlist{$file}) {
1520            is_deeply(\@tested, $tested_invlist{$file}, "prop_invlist('$mod_table') gave same results as its name synonym");
1521            next;
1522        }
1523        $tested_invlist{$file} = dclone \@tested;
1524
1525        # A '!' in the file name means that it is to be inverted.
1526        my $invert = $file =~ s/!//;
1527        my $official;
1528
1529        # If the file's directory is '#', it is a special case where the
1530        # contents are in-lined with semi-colons meaning new-lines, instead of
1531        # it being an actual file to read.  The file is an index in to the
1532        # array of the definitions
1533        if ($file =~ s!^#/!!) {
1534            $official = $Unicode::UCD::inline_definitions[$file];
1535        }
1536        else {
1537            $official = do "unicore/lib/$file.pl";
1538        }
1539
1540        # Get rid of any trailing space and comments in the file.
1541        $official =~ s/\s*(#.*)?$//mg;
1542        local $/ = "\n";
1543        chomp $official;
1544        $/ = $input_record_separator;
1545
1546        # If we are to test against an inverted file, it is easier to invert
1547        # our array than the file.
1548        if ($invert) {
1549            if (@tested && $tested[0] == 0) {
1550                shift @tested;
1551            } else {
1552                unshift @tested, 0;
1553            }
1554        }
1555
1556        # Now construct a string from the list that should match the file.
1557        # The file is inversion list format code points, like this:
1558        # V1216
1559        # 65      # [26]
1560        # 91
1561        # 192     # [23]
1562        # ...
1563        # The V indicates it's an inversion list, and is followed immediately
1564        # by the number of elements (lines) that follow giving its contents.
1565        # The list has even numbered elements (0th, 2nd, ...) start ranges
1566        # that are in the list, and odd ones that aren't in the list.
1567        # Therefore the odd numbered ones are one beyond the end of the
1568        # previous range, but otherwise don't get reflected in the file.
1569        my $tested =  join "\n", ("V" . scalar @tested), @tested;
1570        local $/ = "\n";
1571        chomp $tested;
1572        $/ = $input_record_separator;
1573        if ($tested ne $official) {
1574            fail_with_diff($mod_table, $official, $tested, "prop_invlist");
1575            next;
1576        }
1577
1578        pass("prop_invlist('$mod_table')");
1579    }
1580}
1581
1582# Now test prop_invmap().
1583
1584@list = prop_invmap("Unknown property");
1585is (@list, 0, "prop_invmap(<Unknown property>) returns an empty list");
1586@list = prop_invmap(undef);
1587is (@list, 0, "prop_invmap(undef) returns an empty list");
1588ok (! eval "prop_invmap('gc')" && $@ ne "",
1589                                "prop_invmap('gc') dies in scalar context");
1590@list = prop_invmap("_X_Begin");
1591is (@list, 0, "prop_invmap(<internal property>) returns an empty list");
1592@list = prop_invmap("InKana");
1593is(@list, 0, "prop_invmap(<user-defined property returns undef>)");
1594@list = prop_invmap("Perl_Decomposition_Mapping"), undef,
1595is(@list, 0, "prop_invmap('Perl_Decomposition_Mapping') returns <undef> since internal-Perl-only");
1596@list = prop_invmap("Perl_Charnames"), undef,
1597is(@list, 0, "prop_invmap('Perl_Charnames') returns <undef> since internal-Perl-only");
1598@list = prop_invmap("Is_Is_Any");
1599is(@list, 0, "prop_invmap('Is_Is_Any') returns <undef> since two is's");
1600
1601# The files for these properties shouldn't have their formats changed in case
1602# applications use them (though such use is deprecated).
1603my @legacy_file_format = (qw( Bidi_Mirroring_Glyph
1604                              NFKC_Casefold
1605                           )
1606                          );
1607
1608# The set of properties to test on has already been compiled into %props by
1609# the prop_aliases() tests.
1610
1611my %tested_invmaps;
1612
1613# Like prop_invlist(), prop_invmap() is tested by comparing the results
1614# returned by the function with the tables that mktables generates.  Some of
1615# these tables are directly stored as files on disk, in either the unicore or
1616# unicore/To directories, and most should be listed in the mktables generated
1617# hash %Unicode::UCD::loose_property_to_file_of, with a few additional ones that this
1618# handles specially.  For these, the files are read in directly, massaged, and
1619# compared with what invmap() returns.  The SPECIALS hash in some of these
1620# files overrides values in the main part of the file.
1621#
1622# The other properties are tested indirectly by generating all the possible
1623# inversion lists for the property, and seeing if those match the inversion
1624# lists returned by prop_invlist(), which has already been tested.
1625
1626PROPERTY:
1627foreach my $prop (sort(keys %props)) {
1628    my $loose_prop = &Unicode::UCD::loose_name(lc $prop);
1629    my $suppressed = grep { $_ eq $loose_prop }
1630                          @Unicode::UCD::suppressed_properties;
1631
1632    my $actual_lookup_prop;
1633    my $display_prop;        # The property name that is displayed, as opposed
1634                             # to the one that is actually used.
1635
1636    # Find the short and full names that this property goes by
1637    my ($name, $full_name) = prop_aliases($prop);
1638    if (! $name) {
1639
1640        # Here, Perl doesn't know about this property.  It could be a
1641        # suppressed one
1642            if (! $suppressed) {
1643                fail("prop_invmap('$prop')");
1644                diag("is unknown to prop_aliases(), and we need it in order to test prop_invmap");
1645            }
1646            next PROPERTY;
1647    }
1648
1649    # Normalize the short name, as it is stored in the hashes under the
1650    # normalized version.
1651    $name = &Unicode::UCD::loose_name(lc $name);
1652
1653    # In the case of a combination property, both a map table and a match
1654    # table are generated.  For all the tests except prop_invmap(), this is
1655    # irrelevant, but for prop_invmap, having an 'is' prefix forces it to
1656    # return the match table; otherwise the map.  We thus need to distinguish
1657    # between the two forms.  The property name is what has this information.
1658    $name = &Unicode::UCD::loose_name(lc $prop)
1659                         if exists $Unicode::UCD::combination_property{$name};
1660
1661    # Add in the characters that are supposed to be ignored to test loose
1662    # matching, which the tested function applies to all properties
1663    $display_prop = "$extra_chars$prop" unless $display_prop;
1664    $actual_lookup_prop = $display_prop unless $actual_lookup_prop;
1665
1666    my ($invlist_ref, $invmap_ref, $format, $missing) = prop_invmap($actual_lookup_prop);
1667    my $return_ref = [ $invlist_ref, $invmap_ref, $format, $missing ];
1668
1669    # If have already tested this property under a different name, merely
1670    # compare the return from now with the saved one from before.
1671    if (exists $tested_invmaps{$name}) {
1672        is_deeply($return_ref, $tested_invmaps{$name}, "prop_invmap('$display_prop') gave same results as its synonym, '$name'");
1673        next PROPERTY;
1674    }
1675    $tested_invmaps{$name} = dclone $return_ref;
1676
1677    # If prop_invmap() returned nothing, is ok iff is a property whose file is
1678    # not generated.
1679    if ($suppressed) {
1680        if (defined $format) {
1681            fail("prop_invmap('$display_prop')");
1682            diag("did not return undef for suppressed property $prop");
1683        }
1684        next PROPERTY;
1685    }
1686    elsif (!defined $format) {
1687        fail("prop_invmap('$display_prop')");
1688        diag("'$prop' is unknown to prop_invmap()");
1689        next PROPERTY;
1690    }
1691
1692    # The two parallel arrays must have the same number of elements.
1693    if (@$invlist_ref != @$invmap_ref) {
1694        fail("prop_invmap('$display_prop')");
1695        diag("invlist has "
1696             . scalar @$invlist_ref
1697             . " while invmap has "
1698             . scalar @$invmap_ref
1699             . " elements");
1700        next PROPERTY;
1701    }
1702
1703    # The last element must be for the above-Unicode code points, and must be
1704    # for the default value.
1705    if ($invlist_ref->[-1] != 0x110000) {
1706        fail("prop_invmap('$display_prop')");
1707        diag("The last inversion list element is not 0x110000");
1708        next PROPERTY;
1709    }
1710
1711    my $upper_limit_subtract;
1712
1713    # prop_invmap() adds an extra element not present in the disk files for
1714    # the above-Unicode code points.  For almost all properties, that will be
1715    # to $missing.  In that case we don't look further at it when comparing
1716    # with the disk files.
1717    if ($invmap_ref->[-1] eq $missing) {
1718        $upper_limit_subtract = 1;
1719    }
1720    elsif ($invmap_ref->[-1] eq 'Y' && ! grep { $_ !~ /[YN]/ } @$invmap_ref) {
1721
1722        # But that's not true for a few binary properties like 'Unassigned'
1723        # that are Perl extensions (in this case for Gc=Unassigned) which
1724        # match above-Unicode code points (hence the 'Y' in the test above).
1725        # For properties where it isn't $missing, we're going to want to look
1726        # at the whole thing when comparing with the disk file.
1727        $upper_limit_subtract = 0;
1728
1729        # In those properties like 'Unassigned, the final element should be
1730        # just a repetition of the next-to-last element, and won't be in the
1731        # disk file, so remove it for the comparison.  Otherwise, we will
1732        # compare the whole of the array with the whole of the disk file.
1733        if ($invlist_ref->[-2] <= 0x10FFFF && $invmap_ref->[-2] eq 'Y') {
1734            pop @$invlist_ref;
1735            pop @$invmap_ref;
1736        }
1737    }
1738    else {
1739        fail("prop_invmap('$display_prop')");
1740        diag("The last inversion list element is '$invmap_ref->[-1]', and should be '$missing'");
1741        next PROPERTY;
1742    }
1743
1744    if ($name eq 'bmg') {   # This one has an atypical $missing
1745        if ($missing ne "") {
1746            fail("prop_invmap('$display_prop')");
1747            diag("The missings should be \"\"; got '$missing'");
1748            next PROPERTY;
1749        }
1750    }
1751    elsif ($format =~ /^ a (?!r) /x) {
1752        if ($full_name eq 'Perl_Decimal_Digit') {
1753            if ($missing ne "") {
1754                fail("prop_invmap('$display_prop')");
1755                diag("The missings should be \"\"; got '$missing'");
1756                next PROPERTY;
1757            }
1758        }
1759    }
1760    elsif ($missing =~ /[<>]/) {
1761        fail("prop_invmap('$display_prop')");
1762        diag("The missings should NOT be something with <...>'");
1763        next PROPERTY;
1764
1765        # I don't want to hard code in what all the missings should be, so
1766        # those don't get fully tested.
1767    }
1768
1769    # Certain properties don't have their own files, but must be constructed
1770    # using proxies.
1771    my $proxy_prop = $name;
1772    if ($full_name eq 'Present_In') {
1773        $proxy_prop = "age";    # The maps for these two props are identical
1774    }
1775    elsif ($full_name eq 'Simple_Case_Folding'
1776           || $full_name =~ /Simple_ (.) .*? case_Mapping  /x)
1777    {
1778        if ($full_name eq 'Simple_Case_Folding') {
1779            $proxy_prop = 'cf';
1780        }
1781        else {
1782            # We captured the U, L, or T, leading to uc, lc, or tc.
1783            $proxy_prop = lc $1 . "c";
1784        }
1785        if ($format ne "a") {
1786            fail("prop_invmap('$display_prop')");
1787            diag("The format should be 'a'; got '$format'");
1788            next PROPERTY;
1789        }
1790    }
1791
1792    if ($format !~ / ^ (?: a [der]? | ale? | n | sl? ) $ /x) {
1793        fail("prop_invmap('$display_prop')");
1794        diag("Unknown format '$format'");
1795        next PROPERTY;
1796    }
1797
1798    my $base_file;
1799    my $official;
1800
1801    # Handle the properties that have full disk files for them (except the
1802    # Name property which is structurally enough different that it is handled
1803    # separately below.)
1804    if ($name ne 'na'
1805        && ($name eq 'blk'
1806            || defined
1807                    ($base_file = $Unicode::UCD::loose_property_to_file_of{$proxy_prop})
1808            || exists $Unicode::UCD::loose_to_file_of{$proxy_prop}
1809            || $name eq "dm"))
1810    {
1811        # In the above, blk is done unconditionally, as we need to test that
1812        # the old-style block names are returned, even if mktables has
1813        # generated a file for the new-style; the test for dm comes afterward,
1814        # so that if a file has been generated for it explicitly, we use that
1815        # file (which is valid, unlike blk) instead of the combo
1816        # Decomposition.pl files.
1817        my $file;
1818        my $is_binary = 0;
1819        if ($name eq 'blk') {
1820
1821            # The blk property is special.  The original file with old block
1822            # names is retained, and the default (on ASCII platforms) is to
1823            # not write out a new-name file.  What we do is get the old names
1824            # into a data structure, and from that create what the new file
1825            # would look like.  $base_file is needed to be defined, just to
1826            # avoid a message below.
1827            $base_file = "This is a dummy name";
1828            my $blocks_ref = charblocks();
1829
1830            if ($::IS_EBCDIC) {
1831                # On EBCDIC, the first two blocks can each contain multiple
1832                # ranges.  We create a new version with each of these
1833                # flattened, so have one level.  ($index is used as a dummy
1834                # key.)
1835                my %new_blocks;
1836                my $index = 0;
1837                foreach my $block (values %$blocks_ref) {
1838                    foreach my $range (@$block) {
1839                        $new_blocks{$index++}[0] = $range;
1840                    }
1841                }
1842                $blocks_ref = \%new_blocks;
1843            }
1844            $official = "";
1845            for my $range (sort { $a->[0][0] <=> $b->[0][0] }
1846                           values %$blocks_ref)
1847            {
1848                # Translate the charblocks() data structure to what the file
1849                # would look like.  (The sub range is for EBCDIC platforms
1850                # where Latin1 and ASCII are intermixed.)
1851                if ($range->[0][0] == $range->[0][1]) {
1852                    $official .= sprintf("%X\t\t%s\n",
1853                                         $range->[0][0],
1854                                         $range->[0][2]);
1855                }
1856                else {
1857                    $official .= sprintf("%X\t%X\t%s\n",
1858                                         $range->[0][0],
1859                                         $range->[0][1],
1860                                         $range->[0][2]);
1861                }
1862            }
1863        }
1864        else {
1865            $base_file = "Decomposition" if $format eq 'ad';
1866
1867            # Above leaves $base_file undefined only if it came from the hash
1868            # below.  This should happen only when it is a binary property
1869            # (and are accessing via a single-form name, like 'In_Latin1'),
1870            # and so it is stored in a different directory than the To ones.
1871            # XXX Currently, the only cases where it is complemented are the
1872            # ones that have no code points.  And it works out for these that
1873            # 1) complementing them, and then 2) adding or subtracting the
1874            # initial 0 and final 110000 cancel each other out.  But further
1875            # work would be needed in the unlikely event that an inverted
1876            # property comes along without these characteristics
1877            if (!defined $base_file) {
1878                $base_file = $Unicode::UCD::loose_to_file_of{$proxy_prop};
1879                $is_binary = ($base_file =~ s/!//) ? -1 : 1;
1880                $base_file = "lib/$base_file" unless $base_file =~ m!^#/!;
1881            }
1882
1883            # Read in the file.  If the file's directory is '#', it is a
1884            # special case where the contents are in-lined with semi-colons
1885            # meaning new-lines, instead of it being an actual file to read.
1886            if ($base_file =~ s!^#/!!) {
1887                $official = $Unicode::UCD::inline_definitions[$base_file];
1888            }
1889            else {
1890                $official = do "unicore/$base_file.pl";
1891            }
1892
1893            # Get rid of any trailing space and comments in the file.
1894            $official =~ s/\s*(#.*)?$//mg;
1895
1896            if ($format eq 'ad') {
1897                my @official = split /\n/, $official;
1898                $official = "";
1899                foreach my $line (@official) {
1900                    my ($start, $end, $value)
1901                                    = $line =~ / ^ (.+?) \t (.*?) \t (.+?)
1902                                                \s* ( \# .* )? $ /x;
1903                    # Decomposition.pl also has the <compatible> types in it,
1904                    # which should be removed.
1905                    $value =~ s/<.*?> //;
1906                    $official .= "$start\t\t$value\n";
1907
1908                    # If this is a multi-char range, we turn it into as many
1909                    # single character ranges as necessary.  This makes things
1910                    # easier below.
1911                    if ($end ne "") {
1912                        for my $i (hex($start) + 1 .. hex $end) {
1913                            $official .= sprintf "%X\t\t%s\n", $i, $value;
1914                        }
1915                    }
1916                }
1917            }
1918        }
1919        local $/ = "\n";
1920        chomp $official;
1921        $/ = $input_record_separator;
1922
1923        # Get the format for the file, and if there are any special elements,
1924        # get a reference to them.
1925        my $swash_name = $Unicode::UCD::file_to_swash_name{$base_file};
1926        my $specials_ref;
1927        my $file_format;    # The 'format' given inside the file
1928        if ($swash_name) {
1929            $specials_ref = $Unicode::UCD::SwashInfo{$swash_name}{'specials_name'};
1930            if ($specials_ref) {
1931
1932                # Convert from the name to the actual reference.
1933                no strict 'refs';
1934                $specials_ref = \%{$specials_ref};
1935            }
1936
1937            $file_format = $Unicode::UCD::SwashInfo{$swash_name}{'format'};
1938        }
1939
1940        # Leading zeros used to be used with the values in the files that give,
1941        # ranges, but these have been mostly stripped off, except for some
1942        # files whose formats should not change in any way.
1943        my $file_range_format = (grep { $full_name eq $_ } @legacy_file_format)
1944                              ? "%04X"
1945                              : "%X";
1946        # Currently this property still has leading zeroes in the mapped-to
1947        # values, but otherwise, those values follow the same rules as the
1948        # ranges.
1949        my $file_map_format = ($full_name eq 'Decomposition_Mapping')
1950                              ? "%04X"
1951                              : $file_range_format;
1952
1953        # Combination properties, where the same file contains mappings to both
1954        # the simple and full versions, have to be adjusted when looking at
1955        # the full versions.
1956        if ($full_name =~ /^ (   Case_Folding
1957                              | (Lower|Title|Upper) case_Mapping )
1958                           $ /x)
1959        {
1960            # The file will have a standard list containing simple mappings
1961            # (to a single code point), and a specials hash which contains all
1962            # the mappings that are to multiple code points.
1963            #
1964            # First, extract a list containing all the file's simple mappings.
1965            my @list;
1966            for (split "\n", $official) {
1967                my ($start, $end, $value) = / ^ (.+?) \t (.*?) \t (.+?)
1968                                                \s* ( \# .* )? $ /x;
1969                $end = $start if $end eq "";
1970                push @list, [ hex $start, hex $end, hex $value ];
1971            }
1972
1973            # For these mappings, the file contains all the simple mappings,
1974            # including the ones that are overridden by the specials.  These
1975            # need to be removed as the list is for just the full ones.
1976
1977            # Go through any special mappings one by one.  The keys are the
1978            # UTF-8 representation of code points.
1979            my $i = 0;
1980            foreach my $utf8_cp (sort keys %$specials_ref) {
1981                my $cp = $utf8_cp;
1982                utf8::decode($cp);
1983                $cp = ord $cp;
1984
1985                # Find the spot in the @list of simple mappings that this
1986                # special applies to; uses a linear search.
1987                while ($i < @list -1 ) {
1988                    last if  $cp <= $list[$i][1];
1989                    $i++;
1990                }
1991
1992                # Here $i is such that it points to the first range which ends
1993                # at or above cp, and hence is the only range that could
1994                # possibly contain it.
1995
1996                # If not in this range, no range contains it: nothing to
1997                # remove.
1998                next if $cp < $list[$i][0];
1999
2000                # Otherwise, remove the existing entry.  If it is the first
2001                # element of the range...
2002                if ($cp == $list[$i][0]) {
2003
2004                    # ... and there are other elements in the range, just
2005                    # shorten the range to exclude this code point.
2006                    if ($list[$i][1] > $list[$i][0]) {
2007                        $list[$i][0]++;
2008                    }
2009
2010                    # ... but if it is the only element in the range, remove
2011                    # it entirely.
2012                    else {
2013                        splice @list, $i, 1;
2014                    }
2015                }
2016                else { # Is somewhere in the middle of the range
2017                    # Split the range into two, excluding this one in the
2018                    # middle
2019                    splice @list, $i, 1,
2020                           [ $list[$i][0], $cp - 1, $list[$i][2] ],
2021                           [ $cp + 1, $list[$i][1], $list[$i][2] ];
2022                }
2023            }
2024
2025            # Here, have gone through all the specials, modifying @list as
2026            # needed.  Turn it back into what the file should look like.
2027            $official = "";
2028            for my $element (@list) {
2029                $official .= "\n" if $official;
2030                if ($element->[1] == $element->[0]) {
2031                    $official
2032                        .= sprintf "$file_range_format\t\t$file_map_format",
2033                                    $element->[0],        $element->[2];
2034                }
2035                else {
2036                    $official .= sprintf "$file_range_format\t$file_range_format\t$file_map_format",
2037                                         $element->[0],
2038                                         $element->[1],
2039                                         $element->[2];
2040                }
2041            }
2042        }
2043        elsif ($full_name
2044            =~ / ^ Simple_(Case_Folding|(Lower|Title|Upper)case_Mapping) $ /x)
2045        {
2046
2047            # These properties have everything in the regular array, and the
2048            # specials are superfluous.
2049            undef $specials_ref;
2050        }
2051        elsif ($format !~ /^a/ && defined $file_format && $file_format eq 'x') {
2052
2053            # For these properties the file is output using hex notation for the
2054            # map.  Convert from hex to decimal.
2055            my @lines = split "\n", $official;
2056            foreach my $line (@lines) {
2057                my ($lower, $upper, $map) = split "\t", $line;
2058                $line = "$lower\t$upper\t" . hex $map;
2059            }
2060            $official = join "\n", @lines;
2061        }
2062
2063        # Here, in $official, we have what the file looks like, or should like
2064        # if we've had to fix it up.  Now take the invmap() output and reverse
2065        # engineer from that what the file should look like.  Each iteration
2066        # appends the next line to the running string.
2067        my $tested_map = "";
2068
2069        # For use with files for binary properties only, which are stored in
2070        # inversion list format.  This counts the number of data lines in the
2071        # file.
2072        my $binary_count = 0;
2073
2074        # Create a copy of the file's specials hash.  (It has been undef'd if
2075        # we know it isn't relevant to this property, so if it exists, it's an
2076        # error or is relevant).  As we go along, we delete from that copy.
2077        # If a delete fails, or something is left over after we are done,
2078        # it's an error
2079        my %specials = %$specials_ref if $specials_ref;
2080
2081        # The extra -$upper_limit_subtract is because the final element may
2082        # have been tested above to be for anything above Unicode, in which
2083        # case the file may not go that high.
2084        for (my $i = 0; $i < @$invlist_ref - $upper_limit_subtract; $i++) {
2085
2086            # If the map element is a reference, have to stringify it (but
2087            # don't do so if the format doesn't allow references, so that an
2088            # improper format will generate an error.
2089            if (ref $invmap_ref->[$i]
2090                && ($format eq 'ad' || $format =~ /^ . l /x))
2091            {
2092                # The stringification depends on the format.
2093                if ($format eq 'sl') {
2094
2095                    # At the time of this writing, there are two types of 'sl'
2096                    # format  One, in Name_Alias, has multiple separate
2097                    # entries for each code point; the other, in
2098                    # Script_Extension, is space separated.  Assume the latter
2099                    # for non-Name_Alias.
2100                    if ($full_name ne 'Name_Alias') {
2101                        $invmap_ref->[$i] = join " ", @{$invmap_ref->[$i]};
2102                    }
2103                    else {
2104                        # For Name_Alias, we emulate the file.  Entries with
2105                        # just one value don't need any changes, but we
2106                        # convert the list entries into a series of lines for
2107                        # the file, starting with the first name.  The
2108                        # succeeding entries are on separate lines, with the
2109                        # code point repeated for each one and then two tabs,
2110                        # then the value.  Code at the end of the loop will
2111                        # set up the first line with its code point and two
2112                        # tabs before the value, just as it does for every
2113                        # other property; thus the special handling of the
2114                        # first line.
2115                        if (ref $invmap_ref->[$i]) {
2116                            my $hex_cp = sprintf("%X", $invlist_ref->[$i]);
2117                            my $concatenated = $invmap_ref->[$i][0];
2118                            for (my $j = 1; $j < @{$invmap_ref->[$i]}; $j++) {
2119                                $concatenated .= "\n$hex_cp\t\t"
2120                                              .  $invmap_ref->[$i][$j];
2121                            }
2122                            $invmap_ref->[$i] = $concatenated;
2123                        }
2124                    }
2125                }
2126                elsif ($format =~ / ^ al e? $/x) {
2127
2128                    # For an al property, the stringified result should be in
2129                    # the specials hash.  The key is the utf8 bytes of the
2130                    # code point, and the value is its map as a utf-8 string.
2131                    my $value;
2132                    my $key = chr $invlist_ref->[$i];
2133                    utf8::encode($key);
2134                    if (! defined ($value = delete $specials{$key})) {
2135                        fail("prop_invmap('$display_prop')");
2136                        diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
2137                        next PROPERTY;
2138                    }
2139                    my $packed = pack "W*", @{$invmap_ref->[$i]};
2140                    utf8::upgrade($packed);
2141                    if ($value ne $packed) {
2142                        fail("prop_invmap('$display_prop')");
2143                        diag(sprintf "For %04X, expected the mapping to be "
2144                         . "'$packed', but got '$value'", $invlist_ref->[$i]);
2145                        next PROPERTY;
2146                    }
2147
2148                    # As this doesn't get tested when we later compare with
2149                    # the actual file, it could be out of order and we
2150                    # wouldn't know it.
2151                    if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2152                        || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2153                    {
2154                        fail("prop_invmap('$display_prop')");
2155                        diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2156                        next PROPERTY;
2157                    }
2158                    next;
2159                }
2160                elsif ($format eq 'ad') {
2161
2162                    # The decomposition mapping file has the code points as
2163                    # a string of space-separated hex constants.
2164                    $invmap_ref->[$i] = join " ", map { sprintf "%04X", $_ }
2165                                                           @{$invmap_ref->[$i]};
2166                }
2167                else {
2168                    fail("prop_invmap('$display_prop')");
2169                    diag("Can't handle format '$format'");
2170                    next PROPERTY;
2171                }
2172            } # Otherwise, the map is to a simple scalar
2173            elsif (defined $file_format && $file_format eq 'ax') {
2174                # These maps are in hex
2175                $invmap_ref->[$i] = sprintf("%X", $invmap_ref->[$i]);
2176            }
2177            elsif ($format eq 'ad' || $format eq 'ale') {
2178
2179                # The numerics in the returned map are stored as adjusted
2180                # decimal integers.  The defaults are 0, and don't appear in
2181                # $official, and are excluded later, but the elements must be
2182                # converted back to their hex values before comparing with
2183                # $official, as these files, for backwards compatibility, are
2184                # not stored as adjusted.  (There currently is only one ale
2185                # property, nfkccf.  If that changed this would also have to.)
2186                if ($invmap_ref->[$i] =~ / ^ -? \d+ $ /x
2187                    && $invmap_ref->[$i] != 0)
2188                {
2189                    my $next = $invmap_ref->[$i] + 1;
2190                    $invmap_ref->[$i] = sprintf($file_map_format,
2191                                                $invmap_ref->[$i]);
2192
2193                    # If there are other elements in this range they need to
2194                    # be adjusted; they must individually be re-mapped.  Do
2195                    # this by splicing in a new element into the list and the
2196                    # map containing the remainder of the range.  Next time
2197                    # through we will look at that (possibly splicing again
2198                    # until the whole range is processed).
2199                    if ($invlist_ref->[$i+1] > $invlist_ref->[$i] + 1) {
2200                        splice @$invlist_ref, $i+1, 0,
2201                                $invlist_ref->[$i] + 1;
2202                        splice @$invmap_ref, $i+1, 0, $next;
2203                    }
2204                }
2205                if ($format eq 'ale' && $invmap_ref->[$i] eq "") {
2206
2207                    # ale properties have maps to the empty string that also
2208                    # should be in the specials hash, with the key the utf8
2209                    # bytes representing the code point, and the map just empty.
2210                    my $value;
2211                    my $key = chr $invlist_ref->[$i];
2212                    utf8::encode($key);
2213                    if (! defined ($value = delete $specials{$key})) {
2214                        fail("prop_invmap('$display_prop')");
2215                        diag(sprintf "There was no specials element for %04X", $invlist_ref->[$i]);
2216                        next PROPERTY;
2217                    }
2218                    if ($value ne "") {
2219                        fail("prop_invmap('$display_prop')");
2220                        diag(sprintf "For %04X, expected the mapping to be \"\", but got '$value'", $invlist_ref->[$i]);
2221                        next PROPERTY;
2222                    }
2223
2224                    # As this doesn't get tested when we later compare with
2225                    # the actual file, it could be out of order and we
2226                    # wouldn't know it.
2227                    if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2228                        || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2229                    {
2230                        fail("prop_invmap('$display_prop')");
2231                        diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2232                        next PROPERTY;
2233                    }
2234                    next;
2235                }
2236            }
2237            elsif ($is_binary) { # These binary files don't have an explicit Y
2238                $invmap_ref->[$i] =~ s/Y//;
2239            }
2240
2241            # The file doesn't include entries that map to $missing, so don't
2242            # include it in the built-up string.  But make sure that it is in
2243            # the correct order in the input.
2244            if ($invmap_ref->[$i] eq $missing) {
2245                if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2246                    || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2247                {
2248                    fail("prop_invmap('$display_prop')");
2249                    diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2250                    next PROPERTY;
2251                }
2252                next;
2253            }
2254
2255            # The ad property has one entry which isn't in the file.
2256            # Ignore it, but make sure it is in order.
2257            if ($format eq 'ad'
2258                && $invmap_ref->[$i] eq '<hangul syllable>'
2259                && $invlist_ref->[$i] == 0xAC00)
2260            {
2261                if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2262                    || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2263                {
2264                    fail("prop_invmap('$display_prop')");
2265                    diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2266                    next PROPERTY;
2267                }
2268                next;
2269            }
2270
2271            # Finally have figured out what the map column in the file should
2272            # be.  Append the line to the running string.
2273            my $start = $invlist_ref->[$i];
2274            my $end = (defined $invlist_ref->[$i+1])
2275                      ? $invlist_ref->[$i+1] - 1
2276                      : $Unicode::UCD::MAX_CP;
2277            if ($is_binary) {
2278
2279                # Files for binary properties are in inversion list format,
2280                # without ranges.
2281                $tested_map .= "$start\n";
2282                $binary_count++;
2283
2284                # If the final value is infinity, no line for it exists.
2285                if ($end < $Unicode::UCD::MAX_CP) {
2286                    $tested_map .= ($end + 1) . "\n";
2287                    $binary_count++;
2288                }
2289            }
2290            else {
2291                $end = ($start == $end) ? "" : sprintf($file_range_format, $end);
2292                if ($invmap_ref->[$i] ne "") {
2293                    $tested_map .= sprintf "$file_range_format\t%s\t%s\n",
2294                                            $start, $end, $invmap_ref->[$i];
2295                }
2296                elsif ($end ne "") {
2297                    $tested_map .= sprintf "$file_range_format\t%s\n",
2298                                            $start,             $end;
2299                }
2300                else {
2301                    $tested_map .= sprintf "$file_range_format\n", $start;
2302                }
2303            }
2304        } # End of looping over all elements.
2305
2306        # Binary property files begin with a line count line.
2307        $tested_map = "V$binary_count\n$tested_map" if $binary_count;
2308
2309        # Here are done with generating what the file should look like
2310
2311        local $/ = "\n";
2312        chomp $tested_map;
2313        $/ = $input_record_separator;
2314
2315        # And compare.
2316        if ($tested_map ne $official) {
2317            fail_with_diff($display_prop, $official, $tested_map, "prop_invmap");
2318            next PROPERTY;
2319        }
2320
2321        # There shouldn't be any specials unaccounted for.
2322        if (keys %specials) {
2323            fail("prop_invmap('$display_prop')");
2324            diag("Unexpected specials: " . join ", ", keys %specials);
2325            next PROPERTY;
2326        }
2327    }
2328    elsif ($format eq 'n') {
2329
2330        # Handle the Name property similar to the above.  But the file is
2331        # sufficiently different that it is more convenient to make a special
2332        # case for it.  It is a combination of the Name, Unicode1_Name, and
2333        # Name_Alias properties, and named sequences.  We need to remove all
2334        # but the Name in order to do the comparison.
2335
2336        if ($missing ne "") {
2337            fail("prop_invmap('$display_prop')");
2338            diag("The missings should be \"\"; got \"missing\"");
2339            next PROPERTY;
2340        }
2341
2342        $official = do "unicore/Name.pl";
2343
2344        # Change the double \n format of the file back to single lines with a tab
2345        $official =~ s/\n\n/\e/g;     # Use a control that shouldn't occur
2346                                      # in the file
2347        $official =~ s/\n/\t/g;
2348        $official =~ s/\e/\n/g;
2349
2350        # Get rid of the named sequences portion of the file.  These don't
2351        # have a tab before the first blank on a line.
2352        $official =~ s/ ^ [^\t]+ \  .*? \n //xmg;
2353
2354        # And get rid of the controls.  These are named in the file, but
2355        # shouldn't be in the property.  On all supported platforms, there are
2356        # two ranges of controls.  The first range extends from 0..SPACE-1.
2357        # The second depends on the platform.
2358        $official =~ s/ ^ 00000 .*? ( .{5} \t SPACE ) $ /$1/xms;
2359        my $range_2_start;
2360        my $range_2_end_next;
2361        if ($::IS_ASCII) {
2362            $range_2_start    = '0007F';
2363            $range_2_end_next = '000A0';
2364        }
2365        elsif (ord '^' == 106) { # POSIX-BC
2366            $range_2_start    = '005F';
2367            $range_2_end_next = '0060';
2368        }
2369        else {
2370            $range_2_start    = '00FF';
2371            $range_2_end_next = '0100';
2372        }
2373        $official =~ s/ ^ $range_2_start .*? ( $range_2_end_next ) /$1/xms;
2374
2375        # And remove the aliases.  We read in the Name_Alias property, and go
2376        # through them one by one.
2377        my ($aliases_code_points, $aliases_maps, undef, undef)
2378                = &prop_invmap('_Perl_Name_Alias', '_perl_core_internal_ok');
2379        for (my $i = 0; $i < @$aliases_code_points; $i++) {
2380            my $code_point = $aliases_code_points->[$i];
2381
2382            # Already removed these above.
2383            next if $code_point <= 0x1F
2384                    || ($code_point >= 0x7F && $code_point <= 0x9F);
2385
2386            my $hex_code_point = sprintf "%05X", $code_point;
2387
2388            # Convert to a list if not already to make the following loop
2389            # control uniform.
2390            $aliases_maps->[$i] = [ $aliases_maps->[$i] ]
2391                                                if ! ref $aliases_maps->[$i];
2392
2393            # Remove each alias for this code point from the file
2394            foreach my $alias (@{$aliases_maps->[$i]}) {
2395
2396                # Remove the alias type from the entry, retaining just the name.
2397                $alias =~ s/:.*//;
2398
2399                $alias = quotemeta($alias);
2400                $official =~ s/$hex_code_point \t $alias \n //x;
2401            }
2402        }
2403
2404        local $/ = "\n";
2405        chomp $official;
2406        $/ = $input_record_separator;
2407
2408        # Here have adjusted the file.  We also have to adjust the returned
2409        # inversion map by checking and deleting all the lines in it that
2410        # won't be in the file.  These are the lines that have generated
2411        # things, like <hangul syllable>.
2412        my $tested_map = "";        # Current running string
2413        my @code_point_in_names =
2414                               @Unicode::UCD::code_points_ending_in_code_point;
2415
2416        for my $i (0 .. @$invlist_ref - 1 - $upper_limit_subtract) {
2417            my $start = $invlist_ref->[$i];
2418            my $end = $invlist_ref->[$i+1] - 1;
2419            if ($invmap_ref->[$i] eq $missing) {
2420                if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2421                    || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2422                {
2423                    fail("prop_invmap('$display_prop')");
2424                    diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2425                    next PROPERTY;
2426                }
2427                next;
2428            }
2429            if ($invmap_ref->[$i] =~ / (.*) ( < .*? > )/x) {
2430                my $name = $1;
2431                my $type = $2;
2432                if (($i > 0 && $invlist_ref->[$i] <= $invlist_ref->[$i-1])
2433                    || $invlist_ref->[$i] >= $invlist_ref->[$i+1])
2434                {
2435                    fail("prop_invmap('$display_prop')");
2436                    diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2437                    next PROPERTY;
2438                }
2439                if ($type eq "<hangul syllable>") {
2440                    if ($name ne "") {
2441                        fail("prop_invmap('$display_prop')");
2442                        diag("Unexpected text in $invmap_ref->[$i]");
2443                        next PROPERTY;
2444                    }
2445                    if ($start != 0xAC00) {
2446                        fail("prop_invmap('$display_prop')");
2447                        diag(sprintf("<hangul syllables> should begin at 0xAC00, got %04X", $start));
2448                        next PROPERTY;
2449                    }
2450                    if ($end != $start + 11172 - 1) {
2451                        fail("prop_invmap('$display_prop')");
2452                        diag(sprintf("<hangul syllables> should end at %04X, got %04X", $start + 11172 -1, $end));
2453                        next PROPERTY;
2454                    }
2455                }
2456                elsif ($type ne "<code point>") {
2457                    fail("prop_invmap('$display_prop')");
2458                    diag("Unexpected text '$type' in $invmap_ref->[$i]");
2459                    next PROPERTY;
2460                }
2461                else {
2462
2463                    # Look through the array of names that end in code points,
2464                    # and look for this start and end.  If not found is an
2465                    # error.  If found, delete it, and at the end, make sure
2466                    # have deleted everything.
2467                    for my $i (0 .. @code_point_in_names - 1) {
2468                        my $hash = $code_point_in_names[$i];
2469                        if ($hash->{'low'} == $start
2470                            && $hash->{'high'} == $end
2471                            && "$hash->{'name'}-" eq $name)
2472                        {
2473                            splice @code_point_in_names, $i, 1;
2474                            last;
2475                        }
2476                        else {
2477                            fail("prop_invmap('$display_prop')");
2478                            diag("Unexpected code-point-in-name line '$invmap_ref->[$i]'");
2479                            next PROPERTY;
2480                        }
2481                    }
2482                }
2483
2484                next;
2485            }
2486
2487            # Have adjusted the map, as needed.  Append to running string.
2488            $end = ($start == $end) ? "" : sprintf("%05X", $end);
2489            $tested_map .= sprintf "%05X\t%s\n", $start, $invmap_ref->[$i];
2490        }
2491
2492        # Finished creating the string from the inversion map.  Can compare
2493        # with what the file is.
2494        local $/ = "\n";
2495        chomp $tested_map;
2496        $/ = $input_record_separator;
2497        if ($tested_map ne $official) {
2498            fail_with_diff($display_prop, $official, $tested_map, "prop_invmap");
2499            next PROPERTY;
2500        }
2501        if (@code_point_in_names) {
2502            fail("prop_invmap('$display_prop')");
2503            use Data::Dumper;
2504            diag("Missing code-point-in-name line(s)" . Dumper \@code_point_in_names);
2505            next PROPERTY;
2506        }
2507    }
2508    elsif ($format eq 's') {
2509
2510        # Here the map is not more or less directly from a file stored on
2511        # disk.  We try a different tack.  These should all be properties that
2512        # have just a few possible values (most of them are  binary).  We go
2513        # through the map list, sorting each range into buckets, one for each
2514        # map value.  Thus for binary properties there will be a bucket for Y
2515        # and one for N.  The buckets are inversion lists.  We compare each
2516        # constructed inversion list with what we would get for it using
2517        # prop_invlist(), which has already been tested.  If they all match,
2518        # the whole map must have matched.
2519        my %maps;
2520        my $previous_map;
2521
2522        for my $i (0 .. @$invlist_ref - 1 - $upper_limit_subtract) {
2523            my $range_start = $invlist_ref->[$i];
2524
2525            # Because we are sorting into buckets, things could be
2526            # out-of-order here, and still be in the correct order in the
2527            # bucket, and hence wouldn't show up as an error; so have to
2528            # check.
2529            if (($i > 0 && $range_start <= $invlist_ref->[$i-1])
2530                || $range_start >= $invlist_ref->[$i+1])
2531            {
2532                fail("prop_invmap('$display_prop')");
2533                diag(sprintf "Range beginning at %04X is out-of-order.", $invlist_ref->[$i]);
2534                next PROPERTY;
2535            }
2536
2537            # This new range closes out the range started in the previous
2538            # iteration.
2539            push @{$maps{$previous_map}}, $range_start if defined $previous_map;
2540
2541            # And starts a range which will be closed in the next iteration.
2542            $previous_map = $invmap_ref->[$i];
2543            push @{$maps{$previous_map}}, $range_start;
2544        }
2545
2546        # The range we just started hasn't been closed, and we didn't look at
2547        # the final element of the loop.  If that range is for the default
2548        # value, it shouldn't be closed, as it is to extend to infinity.  But
2549        # otherwise, it should end at the final Unicode code point, and the
2550        # list that maps to the default value should have another element that
2551        # does go to infinity for every above Unicode code point.
2552
2553        if (@$invlist_ref > 1) {
2554            my $penultimate_map = $invmap_ref->[-2];
2555            if ($penultimate_map ne $missing) {
2556
2557                # The -1th element contains the first non-Unicode code point.
2558                push @{$maps{$penultimate_map}}, $invlist_ref->[-1];
2559                push @{$maps{$missing}}, $invlist_ref->[-1];
2560            }
2561        }
2562
2563        # Here, we have the buckets (inversion lists) all constructed.  Go
2564        # through each and verify that matches what prop_invlist() returns.
2565        # We could use is_deeply() for the comparison, but would get multiple
2566        # messages for each $prop.
2567        foreach my $map (sort keys %maps) {
2568            my @off_invlist = prop_invlist("$prop = $map");
2569            my $min = (@off_invlist >= @{$maps{$map}})
2570                       ? @off_invlist
2571                       : @{$maps{$map}};
2572            for my $i (0 .. $min- 1) {
2573                if ($i > @off_invlist - 1) {
2574                    fail("prop_invmap('$display_prop')");
2575                    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]'");
2576                    next PROPERTY;
2577                }
2578                elsif ($i > @{$maps{$map}} - 1) {
2579                    fail("prop_invmap('$display_prop')");
2580                    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]'");
2581                    next PROPERTY;
2582                }
2583                elsif ($maps{$map}[$i] ne $off_invlist[$i]) {
2584                    fail("prop_invmap('$display_prop')");
2585                    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]'");
2586                    next PROPERTY;
2587                }
2588            }
2589        }
2590    }
2591    else {  # Don't know this property nor format.
2592
2593        fail("prop_invmap('$display_prop')");
2594        diag("Unknown property '$display_prop' or format '$format'");
2595        next PROPERTY;
2596    }
2597
2598    pass("prop_invmap('$display_prop')");
2599}
2600
2601# A few tests of search_invlist
2602use Unicode::UCD qw(search_invlist);
2603
2604if ($v_unicode_version ge v3.1.0) { # No Script property before this
2605    my ($scripts_ranges_ref, $scripts_map_ref) = prop_invmap("Script");
2606    my $index = search_invlist($scripts_ranges_ref, 0x390);
2607    is($scripts_map_ref->[$index], "Greek", "U+0390 is Greek");
2608    my @alpha_invlist = prop_invlist("Alpha");
2609    is(search_invlist(\@alpha_invlist, ord("\t")), undef, "search_invlist returns undef for code points before first one on the list");
2610}
2611
2612ok($/ eq $input_record_separator,  "The record separator didn't get overridden");
2613
2614if (! ok(@warnings == 0, "No warnings were generated")) {
2615    diag(join "\n", "The warnings are:", @warnings);
2616}
2617
2618# And make sure that the max code point returned actually fits in an IV, which
2619# currently range iterators are.
2620my $count = 0;
2621for my $i ($Unicode::UCD::MAX_CP - 1 .. $Unicode::UCD::MAX_CP) {
2622    $count++;
2623}
2624is($count, 2, "MAX_CP isn't too large");
2625
2626done_testing();
2627