4 print "1..0 # Unicode::Collate not ported to EBCDIC\n";
10 if ($ENV{PERL_CORE}) {
17 BEGIN { plan tests => 183};
20 #########################
22 ok(1); # If we made it this far, we're ok.
24 my $UCA_Version = "9";
26 ok(Unicode::Collate::UCA_Version, $UCA_Version);
27 ok(Unicode::Collate->UCA_Version, $UCA_Version);
29 my $Collator = Unicode::Collate->new(
31 normalization => undef,
34 ok(ref $Collator, "Unicode::Collate");
36 ok($Collator->UCA_Version, $UCA_Version);
37 ok($Collator->UCA_Version(), $UCA_Version);
40 join(':', $Collator->sort(
41 qw/ lib strict Carp ExtUtils CGI Time warnings Math overload Pod CPAN /
44 qw/ Carp CGI CPAN ExtUtils lib Math overload Pod strict Time warnings /
48 ok($Collator->cmp("", ""), 0);
49 ok($Collator->eq("", ""));
50 ok($Collator->cmp("", "perl"), -1);
54 my $A_acute = pack('U', 0x00C1);
55 my $a_acute = pack('U', 0x00E1);
56 my $acute = pack('U', 0x0301);
58 ok($Collator->cmp("A$acute", $A_acute), -1);
59 ok($Collator->cmp($a_acute, $A_acute), -1);
61 my %old_level = $Collator->change(level => 1);
62 ok($Collator->eq("A$acute", $A_acute));
63 ok($Collator->eq("A", $A_acute));
65 ok($Collator->change(level => 2)->eq($a_acute, $A_acute));
66 ok($Collator->lt("A", $A_acute));
68 ok($Collator->change(%old_level)->lt("A", $A_acute));
69 ok($Collator->lt("A", $A_acute));
70 ok($Collator->lt("A", $a_acute));
71 ok($Collator->lt($a_acute, $A_acute));
75 eval { require Unicode::Normalize };
78 my $NFD = Unicode::Collate->new(
81 0430 ; [.0B01.0020.0002.0430] # CYRILLIC SMALL LETTER A
82 0410 ; [.0B01.0020.0008.0410] # CYRILLIC CAPITAL LETTER A
83 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
84 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
85 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
86 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
87 04D2 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
88 0410 0308 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
89 0430 3099 ; [.0B10.0020.0002.04D3] # A WITH KATAKANA VOICED
90 0430 3099 0308 ; [.0B11.0020.0002.04D3] # A WITH KATAKANA VOICED, DIAERESIS
93 ok($NFD->eq("A$acute", $A_acute));
94 ok($NFD->eq("\x{4D3}\x{325}", "\x{430}\x{308}\x{325}"));
95 ok($NFD->lt("\x{430}\x{308}A", "\x{430}\x{308}B"));
96 ok($NFD->lt("\x{430}\x{3099}B", "\x{430}\x{308}\x{3099}A"));
97 ok($NFD->eq("\x{0430}\x{3099}\x{309A}\x{0308}",
98 "\x{0430}\x{309A}\x{3099}\x{0308}") );
100 my %old_norm = $NFD->change(normalization => undef);
101 ok($NFD->lt("A$acute", $A_acute));
102 ok($NFD->cmp("A$acute", $A_acute), $Collator->cmp("A$acute", $A_acute));
104 $NFD->change(%old_norm);
105 ok($NFD->eq("A$acute", $A_acute));
106 ok($NFD->change(normalization => undef)->lt("A$acute", $A_acute));
107 ok($NFD->change(level => 1)->eq("A$acute", $A_acute));
125 my $trad = Unicode::Collate->new(
127 normalization => undef,
128 ignoreName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
130 entry => << 'ENTRIES',
131 0063 0068 ; [.0893.0020.0002.0063] % "ch" in traditional Spanish
132 0043 0068 ; [.0893.0020.0008.0043] # "Ch" in traditional Spanish
133 00DF ; [.09F3.0154.0004.00DF] [.09F3.0020.0004.00DF] # eszet in Germany
138 join(':', $trad->sort( qw/ acha aca ada acia acka / ) ),
139 join(':', qw/ aca acia acka acha ada / ),
143 join(':', $Collator->sort( qw/ acha aca ada acia acka / ) ),
144 join(':', qw/ aca acha acia acka ada / ),
147 my $hiragana = "\x{3042}\x{3044}";
148 my $katakana = "\x{30A2}\x{30A4}";
150 # HIRAGANA and KATAKANA are ignorable via ignoreName
151 ok($trad->eq($hiragana, ""));
152 ok($trad->eq("", $katakana));
153 ok($trad->eq($hiragana, $katakana));
154 ok($trad->eq($katakana, $hiragana));
158 $Collator->change(level => 2);
160 ok($Collator->{level}, 2);
162 ok( $Collator->cmp("ABC","abc"), 0);
163 ok( $Collator->eq("ABC","abc") );
164 ok( $Collator->le("ABC","abc") );
165 ok( $Collator->cmp($hiragana, $katakana), 0);
166 ok( $Collator->eq($hiragana, $katakana) );
167 ok( $Collator->ge($hiragana, $katakana) );
170 ok( $Collator->eq("a\x{AC00}b", "a\x{1100}\x{1161}b") );
171 ok( $Collator->eq("a\x{AE00}b", "a\x{1100}\x{1173}\x{11AF}b") );
172 ok( $Collator->gt("a\x{AE00}b", "a\x{1100}\x{1173}b\x{11AF}") );
173 ok( $Collator->lt("a\x{AC00}b", "a\x{AE00}b") );
174 ok( $Collator->gt("a\x{D7A3}b", "a\x{C544}b") );
175 ok( $Collator->lt("a\x{C544}b", "a\x{30A2}b") ); # hangul < hiragana
177 $Collator->change(%old_level, katakana_before_hiragana => 1);
179 ok($Collator->{level}, 4);
181 ok( $Collator->cmp("abc", "ABC"), -1);
182 ok( $Collator->ne("abc", "ABC") );
183 ok( $Collator->lt("abc", "ABC") );
184 ok( $Collator->le("abc", "ABC") );
185 ok( $Collator->cmp($hiragana, $katakana), 1);
186 ok( $Collator->ne($hiragana, $katakana) );
187 ok( $Collator->gt($hiragana, $katakana) );
188 ok( $Collator->ge($hiragana, $katakana) );
190 $Collator->change(upper_before_lower => 1);
192 ok( $Collator->cmp("abc", "ABC"), 1);
193 ok( $Collator->ge("abc", "ABC"), 1);
194 ok( $Collator->gt("abc", "ABC"), 1);
195 ok( $Collator->cmp($hiragana, $katakana), 1);
196 ok( $Collator->ge($hiragana, $katakana), 1);
197 ok( $Collator->gt($hiragana, $katakana), 1);
199 $Collator->change(katakana_before_hiragana => 0);
201 ok( $Collator->cmp("abc", "ABC"), 1);
202 ok( $Collator->cmp($hiragana, $katakana), -1);
204 $Collator->change(upper_before_lower => 0);
206 ok( $Collator->cmp("abc", "ABC"), -1);
207 ok( $Collator->le("abc", "ABC") );
208 ok( $Collator->cmp($hiragana, $katakana), -1);
209 ok( $Collator->lt($hiragana, $katakana) );
213 my $ignoreAE = Unicode::Collate->new(
215 normalization => undef,
216 ignoreChar => qr/^[aAeE]$/,
219 ok($ignoreAE->eq("element","lament"));
220 ok($ignoreAE->eq("Perl","ePrl"));
224 my $onlyABC = Unicode::Collate->new(
226 normalization => undef,
227 entry => << 'ENTRIES',
228 0061 ; [.0101.0020.0002.0061] # LATIN SMALL LETTER A
229 0041 ; [.0101.0020.0008.0041] # LATIN CAPITAL LETTER A
230 0062 ; [.0102.0020.0002.0062] # LATIN SMALL LETTER B
231 0042 ; [.0102.0020.0008.0042] # LATIN CAPITAL LETTER B
232 0063 ; [.0103.0020.0002.0063] # LATIN SMALL LETTER C
233 0043 ; [.0103.0020.0008.0043] # LATIN CAPITAL LETTER C
238 join(':', $onlyABC->sort( qw/ ABA BAC cc A Ab cAc aB / ) ),
239 join(':', qw/ A aB Ab ABA BAC cAc cc / ),
244 my $undefAE = Unicode::Collate->new(
246 normalization => undef,
247 undefChar => qr/^[aAeE]$/,
250 ok($undefAE ->gt("edge","fog"));
251 ok($Collator->lt("edge","fog"));
252 ok($undefAE ->gt("lake","like"));
253 ok($Collator->lt("lake","like"));
257 $Collator->change(level => 2);
261 my $orig = "This is a Perl book.";
264 my $ret = "This is a camel book.";
267 if (my($pos,$len) = $Collator->index($str, $sub)) {
268 substr($str, $pos, $len, $rep);
273 $Collator->change(%old_level);
276 if (my($pos,$len) = $Collator->index($str, $sub)) {
277 substr($str, $pos, $len, $rep);
286 $Collator->change(level => 1);
288 $str = "Pe\x{300}rl";
291 if (my($pos, $len) = $Collator->index($str, $sub)) {
292 $match = substr($str, $pos, $len);
294 ok($match, "Pe\x{300}");
296 $str = "P\x{300}e\x{300}\x{301}\x{303}rl";
299 if (my($pos, $len) = $Collator->index($str, $sub)) {
300 $match = substr($str, $pos, $len);
302 ok($match, "P\x{300}e\x{300}\x{301}\x{303}");
304 $Collator->change(%old_level);
308 %old_level = $trad->change(level => 1);
310 $str = "Ich mu\x{00DF} studieren.";
311 $sub = "m\x{00FC}ss";
313 if (my($pos, $len) = $trad->index($str, $sub)) {
314 $match = substr($str, $pos, $len);
316 ok($match, "mu\x{00DF}");
318 $trad->change(%old_level);
320 $str = "Ich mu\x{00DF} studieren.";
321 $sub = "m\x{00FC}ss";
324 if (my($pos, $len) = $trad->index($str, $sub)) {
325 $match = substr($str, $pos, $len);
330 if (my($pos,$len) = $Collator->index("", "")) {
331 $match = substr("", $pos, $len);
336 if (my($pos,$len) = $Collator->index("", "abc")) {
337 $match = substr("", $pos, $len);
343 # Table is undefined, then no entry is defined.
345 my $undef_table = Unicode::Collate->new(
347 normalization => undef,
351 # in the Unicode code point order
352 ok($undef_table->lt('', 'A'));
353 ok($undef_table->lt('ABC', 'B'));
355 # Hangul should be decomposed (even w/o Unicode::Normalize).
357 ok($undef_table->lt("Perl", "\x{AC00}"));
358 ok($undef_table->eq("\x{AC00}", "\x{1100}\x{1161}"));
359 ok($undef_table->eq("\x{AE00}", "\x{1100}\x{1173}\x{11AF}"));
360 ok($undef_table->lt("\x{AE00}", "\x{3042}"));
362 # U+AE00: Hangul GEUL
365 # Weight for CJK Ideographs is defined, though.
367 ok($undef_table->lt("", "\x{4E00}"));
368 ok($undef_table->lt("\x{4E8C}","ABC"));
369 ok($undef_table->lt("\x{4E00}","\x{3042}"));
370 ok($undef_table->lt("\x{4E00}","\x{4E8C}"));
371 # U+4E00: Ideograph "ONE"
372 # U+4E8C: Ideograph "TWO"
377 my $few_entries = Unicode::Collate->new(
378 entry => <<'ENTRIES',
379 0050 ; [.0101.0020.0002.0050] # P
380 0045 ; [.0102.0020.0002.0045] # E
381 0052 ; [.0103.0020.0002.0052] # R
382 004C ; [.0104.0020.0002.004C] # L
383 1100 ; [.0105.0020.0002.1100] # Hangul Jamo initial G
384 1175 ; [.0106.0020.0002.1175] # Hangul Jamo middle I
385 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
388 normalization => undef,
391 # defined before undefined
393 my $sortABC = join '',
394 $few_entries->sort(split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
396 ok($sortABC eq "PERL ABCDFGHIJKMNOQSTUVWXYZ");
398 ok($few_entries->lt('E', 'D'));
399 ok($few_entries->lt("\x{5B57}", "\x{4E00}"));
400 ok($few_entries->lt("\x{AE30}", "\x{AC00}"));
402 # Hangul must be decomposed.
404 ok($few_entries->eq("\x{AC00}", "\x{1100}\x{1161}"));
408 my $all_undef_8 = Unicode::Collate->new(
410 normalization => undef,
411 overrideCJK => undef,
412 overrideHangul => undef,
416 # All in the Unicode code point order.
417 # No hangul decomposition.
419 ok($all_undef_8->lt("\x{3402}", "\x{4E00}"));
420 ok($all_undef_8->lt("\x{4DFF}", "\x{4E00}"));
421 ok($all_undef_8->lt("\x{4E00}", "\x{AC00}"));
422 ok($all_undef_8->gt("\x{AC00}", "\x{1100}\x{1161}"));
423 ok($all_undef_8->gt("\x{AC00}", "\x{ABFF}"));
427 my $all_undef_9 = Unicode::Collate->new(
429 normalization => undef,
430 overrideCJK => undef,
431 overrideHangul => undef,
435 # CJK Ideo. < CJK ext A/B < Others.
436 # No hangul decomposition.
438 ok($all_undef_9->lt("\x{4E00}", "\x{3402}"));
439 ok($all_undef_9->lt("\x{3402}", "\x{20000}"));
440 ok($all_undef_9->lt("\x{20000}", "\x{AC00}"));
441 ok($all_undef_9->gt("\x{AC00}", "\x{1100}\x{1161}"));
442 ok($all_undef_9->gt("\x{AC00}", "\x{ABFF}"));
446 my $ignoreCJK = Unicode::Collate->new(
448 normalization => undef,
449 overrideCJK => sub {()},
450 entry => <<'ENTRIES',
451 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
455 # All CJK Unified Ideographs except U+5B57 are ignored.
457 ok($ignoreCJK->eq("\x{4E00}", ""));
458 ok($ignoreCJK->lt("\x{4E00}", "\0"));
459 ok($ignoreCJK->eq("Pe\x{4E00}rl", "Perl")); # U+4E00 is a CJK.
460 ok($ignoreCJK->gt("\x{4DFF}", "\x{4E00}")); # U+4DFF is not CJK.
461 ok($ignoreCJK->lt("Pe\x{5B57}rl", "Perl")); # 'r' is unassigned.
465 my $ignoreHangul = Unicode::Collate->new(
467 normalization => undef,
468 overrideHangul => sub {()},
469 entry => <<'ENTRIES',
470 AE00 ; [.0100.0020.0002.AE00] # Hangul GEUL
474 # All Hangul Syllables except U+AE00 are ignored.
476 ok($ignoreHangul->eq("\x{AC00}", ""));
477 ok($ignoreHangul->lt("\x{AC00}", "\0"));
478 ok($ignoreHangul->lt("\x{AC00}", "\x{AE00}"));
479 ok($ignoreHangul->lt("\x{AC00}", "\x{1100}\x{1161}")); # Jamo are not ignored.
480 ok($ignoreHangul->lt("Pe\x{AE00}rl", "Perl")); # 'r' is unassigned.
484 my %origAlter = $Collator->change(alternate => 'Blanked');
486 ok($Collator->lt("death", "de luge"));
487 ok($Collator->lt("de luge", "de-luge"));
488 ok($Collator->lt("de-luge", "deluge"));
489 ok($Collator->lt("deluge", "de\x{2010}luge"));
490 ok($Collator->lt("deluge", "de Luge"));
492 $Collator->change(alternate => 'Non-ignorable');
494 ok($Collator->lt("de luge", "de Luge"));
495 ok($Collator->lt("de Luge", "de-luge"));
496 ok($Collator->lt("de-Luge", "de\x{2010}luge"));
497 ok($Collator->lt("de-luge", "death"));
498 ok($Collator->lt("death", "deluge"));
500 $Collator->change(alternate => 'Shifted');
502 ok($Collator->lt("death", "de luge"));
503 ok($Collator->lt("de luge", "de-luge"));
504 ok($Collator->lt("de-luge", "deluge"));
505 ok($Collator->lt("deluge", "de Luge"));
506 ok($Collator->lt("de Luge", "deLuge"));
508 $Collator->change(alternate => 'Shift-Trimmed');
510 ok($Collator->lt("death", "deluge"));
511 ok($Collator->lt("deluge", "de luge"));
512 ok($Collator->lt("de luge", "de-luge"));
513 ok($Collator->lt("de-luge", "deLuge"));
514 ok($Collator->lt("deLuge", "de Luge"));
516 $Collator->change(%origAlter);
518 ok($Collator->{alternate}, 'shifted');
522 my $overCJK = Unicode::Collate->new(
524 normalization => undef,
525 entry => <<'ENTRIES',
526 0061 ; [.0101.0020.0002.0061] # latin a
527 0041 ; [.0101.0020.0008.0041] # LATIN A
528 4E00 ; [.B1FC.0030.0004.4E00] # Ideograph; B1FC = FFFF - 4E03.
531 my $u = 0xFFFF - $_[0]; # reversed
536 ok($overCJK->lt("a", "A")); # diff. at level 3.
537 ok($overCJK->lt( "\x{4E03}", "\x{4E00}")); # diff. at level 2.
538 ok($overCJK->lt("A\x{4E03}", "A\x{4E00}"));
539 ok($overCJK->lt("A\x{4E03}", "a\x{4E00}"));
540 ok($overCJK->lt("a\x{4E03}", "A\x{4E00}"));
544 # rearrange : 0x0E40..0x0E44, 0x0EC0..0x0EC4 (default)
546 my %old_rearrange = $Collator->change(rearrange => undef);
548 ok($Collator->gt("\x{0E41}A", "\x{0E40}B"));
549 ok($Collator->gt("A\x{0E41}A", "A\x{0E40}B"));
551 $Collator->change(rearrange => [ 0x61 ]); # 'a'
553 ok($Collator->gt("ab", "AB")); # as 'ba' > 'AB'
555 $Collator->change(%old_rearrange);
557 ok($Collator->lt("ab", "AB"));
558 ok($Collator->lt("\x{0E40}", "\x{0E41}"));
559 ok($Collator->lt("\x{0E40}A", "\x{0E41}B"));
560 ok($Collator->lt("\x{0E41}A", "\x{0E40}B"));
561 ok($Collator->lt("A\x{0E41}A", "A\x{0E40}B"));
563 ok($all_undef_8->lt("\x{0E40}", "\x{0E41}"));
564 ok($all_undef_8->lt("\x{0E40}A", "\x{0E41}B"));
565 ok($all_undef_8->lt("\x{0E41}A", "\x{0E40}B"));
566 ok($all_undef_8->lt("A\x{0E41}A", "A\x{0E40}B"));
570 my $no_rearrange = Unicode::Collate->new(
572 normalization => undef,
576 ok($no_rearrange->lt("A", "B"));
577 ok($no_rearrange->lt("\x{0E40}", "\x{0E41}"));
578 ok($no_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
579 ok($no_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
580 ok($no_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
584 my $undef_rearrange = Unicode::Collate->new(
586 normalization => undef,
590 ok($undef_rearrange->lt("A", "B"));
591 ok($undef_rearrange->lt("\x{0E40}", "\x{0E41}"));
592 ok($undef_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
593 ok($undef_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
594 ok($undef_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
598 my $dropArticles = Unicode::Collate->new(
600 normalization => undef,
603 $string =~ s/\b(?:an?|the)\s+//ig;
608 ok($dropArticles->eq("camel", "a camel"));
609 ok($dropArticles->eq("Perl", "The Perl"));
610 ok($dropArticles->lt("the pen", "a pencil"));
611 ok($Collator->lt("Perl", "The Perl"));
612 ok($Collator->gt("the pen", "a pencil"));
616 my $backLevel1 = Unicode::Collate->new(
618 normalization => undef,
622 # all strings are reversed at level 1.
624 ok($backLevel1->gt("AB", "BA"));
625 ok($backLevel1->gt("\x{3042}\x{3044}", "\x{3044}\x{3042}"));
629 my $backLevel2 = Unicode::Collate->new(
631 normalization => undef,
632 undefName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
636 ok($backLevel2->gt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
637 ok($backLevel2->gt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
638 ok($Collator ->lt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
639 ok($Collator ->lt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
642 # HIRAGANA and KATAKANA are made undefined via undefName.
643 # So they are after CJK Unified Ideographs.
645 ok($backLevel2->lt("\x{4E00}", $hiragana));
646 ok($backLevel2->lt("\x{4E03}", $katakana));
647 ok($Collator ->gt("\x{4E00}", $hiragana));
648 ok($Collator ->gt("\x{4E03}", $katakana));