1 # Before `make install' is performed this script should be runnable with
2 # `make test'. After `make install' it should work as `perl test.pl'
4 #########################
8 print "1..0 # Unicode::Collate not ported to EBCDIC\n";
14 BEGIN { plan tests => 160 };
16 ok(1); # If we made it this far, we're ok.
18 #########################
20 my $UCA_Version = "8.0";
22 ok(Unicode::Collate::UCA_Version, $UCA_Version);
23 ok(Unicode::Collate->UCA_Version, $UCA_Version);
25 my $Collator = Unicode::Collate->new(
27 normalization => undef,
30 ok(ref $Collator, "Unicode::Collate");
32 ok($Collator->UCA_Version, $UCA_Version);
33 ok($Collator->UCA_Version(), $UCA_Version);
36 join(':', $Collator->sort(
37 qw/ lib strict Carp ExtUtils CGI Time warnings Math overload Pod CPAN /
40 qw/ Carp CGI CPAN ExtUtils lib Math overload Pod strict Time warnings /
44 my $A_acute = pack('U', 0x00C1);
45 my $acute = pack('U', 0x0301);
47 ok($Collator->cmp("A$acute", $A_acute), -1);
48 ok($Collator->cmp("", ""), 0);
49 ok(! $Collator->ne("", "") );
50 ok( $Collator->eq("", "") );
51 ok($Collator->cmp("", "perl"), -1);
55 eval { require Unicode::Normalize };
58 my $NFD = Unicode::Collate->new(
61 0430 ; [.0B01.0020.0002.0430] # CYRILLIC SMALL LETTER A
62 0410 ; [.0B01.0020.0008.0410] # CYRILLIC CAPITAL LETTER A
63 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
64 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
65 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
66 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
67 04D2 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
68 0410 0308 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
69 0430 3099 ; [.0B10.0020.0002.04D3] # A WITH KATAKANA VOICED
70 0430 3099 0308 ; [.0B11.0020.0002.04D3] # A WITH KATAKANA VOICED, DIAERESIS
73 ok($NFD->eq("A$acute", $A_acute));
74 ok($NFD->eq("\x{4D3}\x{325}", "\x{430}\x{308}\x{325}"));
75 ok($NFD->lt("\x{430}\x{308}A", "\x{430}\x{308}B"));
76 ok($NFD->lt("\x{430}\x{3099}B", "\x{430}\x{308}\x{3099}A"));
77 ok($NFD->eq("\x{0430}\x{3099}\x{309A}\x{0308}",
78 "\x{0430}\x{309A}\x{3099}\x{0308}") );
90 my $trad = Unicode::Collate->new(
92 normalization => undef,
93 ignoreName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
95 entry => << 'ENTRIES',
96 0063 0068 ; [.0893.0020.0002.0063] % "ch" in traditional Spanish
97 0043 0068 ; [.0893.0020.0008.0043] # "Ch" in traditional Spanish
98 00DF ; [.09F3.0154.0004.00DF] [.09F3.0020.0004.00DF] # eszet in Germany
103 join(':', $trad->sort( qw/ acha aca ada acia acka / ) ),
104 join(':', qw/ aca acia acka acha ada / ),
108 join(':', $Collator->sort( qw/ acha aca ada acia acka / ) ),
109 join(':', qw/ aca acha acia acka ada / ),
112 my $hiragana = "\x{3042}\x{3044}";
113 my $katakana = "\x{30A2}\x{30A4}";
115 # HIRAGANA and KATAKANA are ignorable via ignoreName
116 ok($trad->eq($hiragana, ""));
117 ok($trad->eq("", $katakana));
118 ok($trad->eq($hiragana, $katakana));
119 ok($trad->eq($katakana, $hiragana));
123 my $old_level = $Collator->{level};
125 $Collator->{level} = 2;
127 ok( $Collator->cmp("ABC","abc"), 0);
128 ok( $Collator->eq("ABC","abc") );
129 ok( $Collator->le("ABC","abc") );
130 ok( $Collator->cmp($hiragana, $katakana), 0);
131 ok( $Collator->eq($hiragana, $katakana) );
132 ok( $Collator->ge($hiragana, $katakana) );
135 ok( $Collator->eq("a\x{AC00}b", "a\x{1100}\x{1161}b") );
136 ok( $Collator->eq("a\x{AE00}b", "a\x{1100}\x{1173}\x{11AF}b") );
137 ok( $Collator->gt("a\x{AE00}b", "a\x{1100}\x{1173}b\x{11AF}") );
138 ok( $Collator->lt("a\x{AC00}b", "a\x{AE00}b") );
139 ok( $Collator->gt("a\x{D7A3}b", "a\x{C544}b") );
140 ok( $Collator->lt("a\x{C544}b", "a\x{30A2}b") ); # hangul < hiragana
142 $Collator->{level} = $old_level;
144 $Collator->{katakana_before_hiragana} = 1;
146 ok( $Collator->cmp("abc", "ABC"), -1);
147 ok( $Collator->ne("abc", "ABC") );
148 ok( $Collator->lt("abc", "ABC") );
149 ok( $Collator->le("abc", "ABC") );
150 ok( $Collator->cmp($hiragana, $katakana), 1);
151 ok( $Collator->ne($hiragana, $katakana) );
152 ok( $Collator->gt($hiragana, $katakana) );
153 ok( $Collator->ge($hiragana, $katakana) );
155 $Collator->{upper_before_lower} = 1;
157 ok( $Collator->cmp("abc", "ABC"), 1);
158 ok( $Collator->ge("abc", "ABC"), 1);
159 ok( $Collator->gt("abc", "ABC"), 1);
160 ok( $Collator->cmp($hiragana, $katakana), 1);
161 ok( $Collator->ge($hiragana, $katakana), 1);
162 ok( $Collator->gt($hiragana, $katakana), 1);
164 $Collator->{katakana_before_hiragana} = 0;
166 ok( $Collator->cmp("abc", "ABC"), 1);
167 ok( $Collator->cmp($hiragana, $katakana), -1);
169 $Collator->{upper_before_lower} = 0;
171 ok( $Collator->cmp("abc", "ABC"), -1);
172 ok( $Collator->le("abc", "ABC") );
173 ok( $Collator->cmp($hiragana, $katakana), -1);
174 ok( $Collator->lt($hiragana, $katakana) );
178 my $ignoreAE = Unicode::Collate->new(
180 normalization => undef,
181 ignoreChar => qr/^[aAeE]$/,
184 ok($ignoreAE->eq("element","lament"));
185 ok($ignoreAE->eq("Perl","ePrl"));
189 my $onlyABC = Unicode::Collate->new(
191 entry => << 'ENTRIES',
192 0061 ; [.0101.0020.0002.0061] # LATIN SMALL LETTER A
193 0041 ; [.0101.0020.0008.0041] # LATIN CAPITAL LETTER A
194 0062 ; [.0102.0020.0002.0062] # LATIN SMALL LETTER B
195 0042 ; [.0102.0020.0008.0042] # LATIN CAPITAL LETTER B
196 0063 ; [.0103.0020.0002.0063] # LATIN SMALL LETTER C
197 0043 ; [.0103.0020.0008.0043] # LATIN CAPITAL LETTER C
202 join(':', $onlyABC->sort( qw/ ABA BAC cc A Ab cAc aB / ) ),
203 join(':', qw/ A aB Ab ABA BAC cAc cc / ),
208 my $undefAE = Unicode::Collate->new(
210 normalization => undef,
211 undefChar => qr/^[aAeE]$/,
214 ok($undefAE ->gt("edge","fog"));
215 ok($Collator->lt("edge","fog"));
216 ok($undefAE ->gt("lake","like"));
217 ok($Collator->lt("lake","like"));
221 $Collator->{level} = 2;
225 my $orig = "This is a Perl book.";
228 my $ret = "This is a camel book.";
231 if (my($pos,$len) = $Collator->index($str, $sub)) {
232 substr($str, $pos, $len, $rep);
237 $Collator->{level} = $old_level;
240 if (my($pos,$len) = $Collator->index($str, $sub)) {
241 substr($str, $pos, $len, $rep);
250 $Collator->{level} = 1;
252 $str = "Pe\x{300}rl";
255 if (my($pos, $len) = $Collator->index($str, $sub)) {
256 $match = substr($str, $pos, $len);
258 ok($match, "Pe\x{300}");
260 $str = "P\x{300}e\x{300}\x{301}\x{303}rl";
263 if (my($pos, $len) = $Collator->index($str, $sub)) {
264 $match = substr($str, $pos, $len);
266 ok($match, "P\x{300}e\x{300}\x{301}\x{303}");
268 $Collator->{level} = $old_level;
274 $str = "Ich mu\x{00DF} studieren.";
275 $sub = "m\x{00FC}ss";
277 if (my($pos, $len) = $trad->index($str, $sub)) {
278 $match = substr($str, $pos, $len);
280 ok($match, "mu\x{00DF}");
282 $trad->{level} = $old_level;
284 $str = "Ich mu\x{00DF} studieren.";
285 $sub = "m\x{00FC}ss";
288 if (my($pos, $len) = $trad->index($str, $sub)) {
289 $match = substr($str, $pos, $len);
294 if (my($pos,$len) = $Collator->index("", "")) {
295 $match = substr("", $pos, $len);
300 if (my($pos,$len) = $Collator->index("", "abc")) {
301 $match = substr("", $pos, $len);
307 # Table is undefined, then no entry is defined.
309 my $undef_table = Unicode::Collate->new(
311 normalization => undef,
315 # in the Unicode code point order
316 ok($undef_table->lt('', 'A'));
317 ok($undef_table->lt('ABC', 'B'));
319 # Hangul should be decomposed (even w/o Unicode::Normalize).
321 ok($undef_table->lt("Perl", "\x{AC00}"));
322 ok($undef_table->eq("\x{AC00}", "\x{1100}\x{1161}"));
323 ok($undef_table->eq("\x{AE00}", "\x{1100}\x{1173}\x{11AF}"));
324 ok($undef_table->lt("\x{AE00}", "\x{3042}"));
326 # U+AE00: Hangul GEUL
329 # Weight for CJK Ideographs is defined, though.
331 ok($undef_table->lt("", "\x{4E00}"));
332 ok($undef_table->lt("\x{4E8C}","ABC"));
333 ok($undef_table->lt("\x{4E00}","\x{3042}"));
334 ok($undef_table->lt("\x{4E00}","\x{4E8C}"));
335 # U+4E00: Ideograph "ONE"
336 # U+4E8C: Ideograph "TWO"
341 my $few_entries = Unicode::Collate->new(
342 entry => <<'ENTRIES',
343 0050 ; [.0101.0020.0002.0050] # P
344 0045 ; [.0102.0020.0002.0045] # E
345 0052 ; [.0103.0020.0002.0052] # R
346 004C ; [.0104.0020.0002.004C] # L
347 1100 ; [.0105.0020.0002.1100] # Hangul Jamo initial G
348 1175 ; [.0106.0020.0002.1175] # Hangul Jamo middle I
349 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
352 normalization => undef,
355 # defined before undefined
357 my $sortABC = join '',
358 $few_entries->sort(split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
360 ok($sortABC eq "PERL ABCDFGHIJKMNOQSTUVWXYZ");
362 ok($few_entries->lt('E', 'D'));
363 ok($few_entries->lt("\x{5B57}", "\x{4E00}"));
364 ok($few_entries->lt("\x{AE30}", "\x{AC00}"));
366 # Hangul must be decomposed.
368 ok($few_entries->eq("\x{AC00}", "\x{1100}\x{1161}"));
372 my $all_undef = Unicode::Collate->new(
374 normalization => undef,
375 overrideCJK => undef,
376 overrideHangul => undef,
379 # All in the Unicode code point order.
380 # No hangul decomposition.
382 ok($all_undef->lt("\x{3042}", "\x{4E00}"));
383 ok($all_undef->lt("\x{4DFF}", "\x{4E00}"));
384 ok($all_undef->lt("\x{4E00}", "\x{AC00}"));
385 ok($all_undef->gt("\x{AC00}", "\x{1100}\x{1161}"));
386 ok($all_undef->gt("\x{AC00}", "\x{ABFF}"));
390 my $ignoreCJK = Unicode::Collate->new(
392 normalization => undef,
393 overrideCJK => sub {()},
394 entry => <<'ENTRIES',
395 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
399 # All CJK Unified Ideographs except U+5B57 are ignored.
401 ok($ignoreCJK->eq("\x{4E00}", ""));
402 ok($ignoreCJK->lt("\x{4E00}", "\0"));
403 ok($ignoreCJK->eq("Pe\x{4E00}rl", "Perl")); # U+4E00 is a CJK.
404 ok($ignoreCJK->gt("\x{4DFF}", "\x{4E00}")); # U+4DFF is not CJK.
405 ok($ignoreCJK->lt("Pe\x{5B57}rl", "Perl")); # 'r' is unassigned.
409 my $ignoreHangul = Unicode::Collate->new(
411 normalization => undef,
412 overrideHangul => sub {()},
413 entry => <<'ENTRIES',
414 AE00 ; [.0100.0020.0002.AE00] # Hangul GEUL
418 # All Hangul Syllables except U+AE00 are ignored.
420 ok($ignoreHangul->eq("\x{AC00}", ""));
421 ok($ignoreHangul->lt("\x{AC00}", "\0"));
422 ok($ignoreHangul->lt("\x{AC00}", "\x{AE00}"));
423 ok($ignoreHangul->lt("\x{AC00}", "\x{1100}\x{1161}")); # Jamo are not ignored.
424 ok($ignoreHangul->lt("Pe\x{AE00}rl", "Perl")); # 'r' is unassigned.
428 my $blanked = Unicode::Collate->new(
430 normalization => undef,
431 alternate => 'Blanked',
434 ok($blanked->lt("death", "de luge"));
435 ok($blanked->lt("de luge", "de-luge"));
436 ok($blanked->lt("de-luge", "deluge"));
437 ok($blanked->lt("deluge", "de\x{2010}luge"));
438 ok($blanked->lt("deluge", "de Luge"));
442 my $nonIgn = Unicode::Collate->new(
444 normalization => undef,
445 alternate => 'Non-ignorable',
448 ok($nonIgn->lt("de luge", "de Luge"));
449 ok($nonIgn->lt("de Luge", "de-luge"));
450 ok($nonIgn->lt("de-Luge", "de\x{2010}luge"));
451 ok($nonIgn->lt("de-luge", "death"));
452 ok($nonIgn->lt("death", "deluge"));
456 my $shifted = Unicode::Collate->new(
458 normalization => undef,
459 alternate => 'Shifted',
462 ok($shifted->lt("death", "de luge"));
463 ok($shifted->lt("de luge", "de-luge"));
464 ok($shifted->lt("de-luge", "deluge"));
465 ok($shifted->lt("deluge", "de Luge"));
466 ok($shifted->lt("de Luge", "deLuge"));
470 my $shTrim = Unicode::Collate->new(
472 normalization => undef,
473 alternate => 'Shift-Trimmed',
476 ok($shTrim->lt("death", "deluge"));
477 ok($shTrim->lt("deluge", "de luge"));
478 ok($shTrim->lt("de luge", "de-luge"));
479 ok($shTrim->lt("de-luge", "deLuge"));
480 ok($shTrim->lt("deLuge", "de Luge"));
484 my $overCJK = Unicode::Collate->new(
486 normalization => undef,
487 entry => <<'ENTRIES',
488 0061 ; [.0101.0020.0002.0061] # latin a
489 0041 ; [.0101.0020.0008.0041] # LATIN A
490 4E00 ; [.B1FC.0030.0004.4E00] # Ideograph; B1FC = FFFF - 4E03.
493 my $u = 0xFFFF - $_[0]; # reversed
498 ok($overCJK->lt("a", "A")); # diff. at level 3.
499 ok($overCJK->lt( "\x{4E03}", "\x{4E00}")); # diff. at level 2.
500 ok($overCJK->lt("A\x{4E03}", "A\x{4E00}"));
501 ok($overCJK->lt("A\x{4E03}", "a\x{4E00}"));
502 ok($overCJK->lt("a\x{4E03}", "A\x{4E00}"));
506 # rearranged : 0x0E40..0x0E44, 0x0EC0..0x0EC4
508 ok($Collator->lt("A", "B"));
509 ok($Collator->lt("\x{0E40}", "\x{0E41}"));
510 ok($Collator->lt("\x{0E40}A", "\x{0E41}B"));
511 ok($Collator->lt("\x{0E41}A", "\x{0E40}B"));
512 ok($Collator->lt("A\x{0E41}A", "A\x{0E40}B"));
514 ok($all_undef->lt("A", "B"));
515 ok($all_undef->lt("\x{0E40}", "\x{0E41}"));
516 ok($all_undef->lt("\x{0E40}A", "\x{0E41}B"));
517 ok($all_undef->lt("\x{0E41}A", "\x{0E40}B"));
518 ok($all_undef->lt("A\x{0E41}A", "A\x{0E40}B"));
522 my $no_rearrange = Unicode::Collate->new(
524 normalization => undef,
528 ok($no_rearrange->lt("A", "B"));
529 ok($no_rearrange->lt("\x{0E40}", "\x{0E41}"));
530 ok($no_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
531 ok($no_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
532 ok($no_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
536 # equivalent to $no_rearrange
538 my $undef_rearrange = Unicode::Collate->new(
540 normalization => undef,
544 ok($undef_rearrange->lt("A", "B"));
545 ok($undef_rearrange->lt("\x{0E40}", "\x{0E41}"));
546 ok($undef_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
547 ok($undef_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
548 ok($undef_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
552 my $dropArticles = Unicode::Collate->new(
554 normalization => undef,
557 $string =~ s/\b(?:an?|the)\s+//ig;
562 ok($dropArticles->eq("camel", "a camel"));
563 ok($dropArticles->eq("Perl", "The Perl"));
564 ok($dropArticles->lt("the pen", "a pencil"));
565 ok($Collator->lt("Perl", "The Perl"));
566 ok($Collator->gt("the pen", "a pencil"));
570 my $backLevel1 = Unicode::Collate->new(
572 normalization => undef,
576 # all strings are reversed at level 1.
578 ok($backLevel1->gt("AB", "BA"));
579 ok($backLevel1->gt("\x{3042}\x{3044}", "\x{3044}\x{3042}"));
583 my $backLevel2 = Unicode::Collate->new(
585 normalization => undef,
586 undefName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
590 ok($backLevel2->gt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
591 ok($backLevel2->gt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
592 ok($Collator ->lt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
593 ok($Collator ->lt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
596 # HIRAGANA and KATAKANA are made undefined via undefName.
597 # So they are after CJK Unified Ideographs.
599 ok($backLevel2->lt("\x{4E00}", $hiragana));
600 ok($backLevel2->lt("\x{4E03}", $katakana));
601 ok($Collator ->gt("\x{4E00}", $hiragana));
602 ok($Collator ->gt("\x{4E03}", $katakana));