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 normalization => undef,
192 entry => << 'ENTRIES',
193 0061 ; [.0101.0020.0002.0061] # LATIN SMALL LETTER A
194 0041 ; [.0101.0020.0008.0041] # LATIN CAPITAL LETTER A
195 0062 ; [.0102.0020.0002.0062] # LATIN SMALL LETTER B
196 0042 ; [.0102.0020.0008.0042] # LATIN CAPITAL LETTER B
197 0063 ; [.0103.0020.0002.0063] # LATIN SMALL LETTER C
198 0043 ; [.0103.0020.0008.0043] # LATIN CAPITAL LETTER C
203 join(':', $onlyABC->sort( qw/ ABA BAC cc A Ab cAc aB / ) ),
204 join(':', qw/ A aB Ab ABA BAC cAc cc / ),
209 my $undefAE = Unicode::Collate->new(
211 normalization => undef,
212 undefChar => qr/^[aAeE]$/,
215 ok($undefAE ->gt("edge","fog"));
216 ok($Collator->lt("edge","fog"));
217 ok($undefAE ->gt("lake","like"));
218 ok($Collator->lt("lake","like"));
222 $Collator->{level} = 2;
226 my $orig = "This is a Perl book.";
229 my $ret = "This is a camel book.";
232 if (my($pos,$len) = $Collator->index($str, $sub)) {
233 substr($str, $pos, $len, $rep);
238 $Collator->{level} = $old_level;
241 if (my($pos,$len) = $Collator->index($str, $sub)) {
242 substr($str, $pos, $len, $rep);
251 $Collator->{level} = 1;
253 $str = "Pe\x{300}rl";
256 if (my($pos, $len) = $Collator->index($str, $sub)) {
257 $match = substr($str, $pos, $len);
259 ok($match, "Pe\x{300}");
261 $str = "P\x{300}e\x{300}\x{301}\x{303}rl";
264 if (my($pos, $len) = $Collator->index($str, $sub)) {
265 $match = substr($str, $pos, $len);
267 ok($match, "P\x{300}e\x{300}\x{301}\x{303}");
269 $Collator->{level} = $old_level;
275 $str = "Ich mu\x{00DF} studieren.";
276 $sub = "m\x{00FC}ss";
278 if (my($pos, $len) = $trad->index($str, $sub)) {
279 $match = substr($str, $pos, $len);
281 ok($match, "mu\x{00DF}");
283 $trad->{level} = $old_level;
285 $str = "Ich mu\x{00DF} studieren.";
286 $sub = "m\x{00FC}ss";
289 if (my($pos, $len) = $trad->index($str, $sub)) {
290 $match = substr($str, $pos, $len);
295 if (my($pos,$len) = $Collator->index("", "")) {
296 $match = substr("", $pos, $len);
301 if (my($pos,$len) = $Collator->index("", "abc")) {
302 $match = substr("", $pos, $len);
308 # Table is undefined, then no entry is defined.
310 my $undef_table = Unicode::Collate->new(
312 normalization => undef,
316 # in the Unicode code point order
317 ok($undef_table->lt('', 'A'));
318 ok($undef_table->lt('ABC', 'B'));
320 # Hangul should be decomposed (even w/o Unicode::Normalize).
322 ok($undef_table->lt("Perl", "\x{AC00}"));
323 ok($undef_table->eq("\x{AC00}", "\x{1100}\x{1161}"));
324 ok($undef_table->eq("\x{AE00}", "\x{1100}\x{1173}\x{11AF}"));
325 ok($undef_table->lt("\x{AE00}", "\x{3042}"));
327 # U+AE00: Hangul GEUL
330 # Weight for CJK Ideographs is defined, though.
332 ok($undef_table->lt("", "\x{4E00}"));
333 ok($undef_table->lt("\x{4E8C}","ABC"));
334 ok($undef_table->lt("\x{4E00}","\x{3042}"));
335 ok($undef_table->lt("\x{4E00}","\x{4E8C}"));
336 # U+4E00: Ideograph "ONE"
337 # U+4E8C: Ideograph "TWO"
342 my $few_entries = Unicode::Collate->new(
343 entry => <<'ENTRIES',
344 0050 ; [.0101.0020.0002.0050] # P
345 0045 ; [.0102.0020.0002.0045] # E
346 0052 ; [.0103.0020.0002.0052] # R
347 004C ; [.0104.0020.0002.004C] # L
348 1100 ; [.0105.0020.0002.1100] # Hangul Jamo initial G
349 1175 ; [.0106.0020.0002.1175] # Hangul Jamo middle I
350 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
353 normalization => undef,
356 # defined before undefined
358 my $sortABC = join '',
359 $few_entries->sort(split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
361 ok($sortABC eq "PERL ABCDFGHIJKMNOQSTUVWXYZ");
363 ok($few_entries->lt('E', 'D'));
364 ok($few_entries->lt("\x{5B57}", "\x{4E00}"));
365 ok($few_entries->lt("\x{AE30}", "\x{AC00}"));
367 # Hangul must be decomposed.
369 ok($few_entries->eq("\x{AC00}", "\x{1100}\x{1161}"));
373 my $all_undef = Unicode::Collate->new(
375 normalization => undef,
376 overrideCJK => undef,
377 overrideHangul => undef,
380 # All in the Unicode code point order.
381 # No hangul decomposition.
383 ok($all_undef->lt("\x{3042}", "\x{4E00}"));
384 ok($all_undef->lt("\x{4DFF}", "\x{4E00}"));
385 ok($all_undef->lt("\x{4E00}", "\x{AC00}"));
386 ok($all_undef->gt("\x{AC00}", "\x{1100}\x{1161}"));
387 ok($all_undef->gt("\x{AC00}", "\x{ABFF}"));
391 my $ignoreCJK = Unicode::Collate->new(
393 normalization => undef,
394 overrideCJK => sub {()},
395 entry => <<'ENTRIES',
396 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
400 # All CJK Unified Ideographs except U+5B57 are ignored.
402 ok($ignoreCJK->eq("\x{4E00}", ""));
403 ok($ignoreCJK->lt("\x{4E00}", "\0"));
404 ok($ignoreCJK->eq("Pe\x{4E00}rl", "Perl")); # U+4E00 is a CJK.
405 ok($ignoreCJK->gt("\x{4DFF}", "\x{4E00}")); # U+4DFF is not CJK.
406 ok($ignoreCJK->lt("Pe\x{5B57}rl", "Perl")); # 'r' is unassigned.
410 my $ignoreHangul = Unicode::Collate->new(
412 normalization => undef,
413 overrideHangul => sub {()},
414 entry => <<'ENTRIES',
415 AE00 ; [.0100.0020.0002.AE00] # Hangul GEUL
419 # All Hangul Syllables except U+AE00 are ignored.
421 ok($ignoreHangul->eq("\x{AC00}", ""));
422 ok($ignoreHangul->lt("\x{AC00}", "\0"));
423 ok($ignoreHangul->lt("\x{AC00}", "\x{AE00}"));
424 ok($ignoreHangul->lt("\x{AC00}", "\x{1100}\x{1161}")); # Jamo are not ignored.
425 ok($ignoreHangul->lt("Pe\x{AE00}rl", "Perl")); # 'r' is unassigned.
429 my $blanked = Unicode::Collate->new(
431 normalization => undef,
432 alternate => 'Blanked',
435 ok($blanked->lt("death", "de luge"));
436 ok($blanked->lt("de luge", "de-luge"));
437 ok($blanked->lt("de-luge", "deluge"));
438 ok($blanked->lt("deluge", "de\x{2010}luge"));
439 ok($blanked->lt("deluge", "de Luge"));
443 my $nonIgn = Unicode::Collate->new(
445 normalization => undef,
446 alternate => 'Non-ignorable',
449 ok($nonIgn->lt("de luge", "de Luge"));
450 ok($nonIgn->lt("de Luge", "de-luge"));
451 ok($nonIgn->lt("de-Luge", "de\x{2010}luge"));
452 ok($nonIgn->lt("de-luge", "death"));
453 ok($nonIgn->lt("death", "deluge"));
457 my $shifted = Unicode::Collate->new(
459 normalization => undef,
460 alternate => 'Shifted',
463 ok($shifted->lt("death", "de luge"));
464 ok($shifted->lt("de luge", "de-luge"));
465 ok($shifted->lt("de-luge", "deluge"));
466 ok($shifted->lt("deluge", "de Luge"));
467 ok($shifted->lt("de Luge", "deLuge"));
471 my $shTrim = Unicode::Collate->new(
473 normalization => undef,
474 alternate => 'Shift-Trimmed',
477 ok($shTrim->lt("death", "deluge"));
478 ok($shTrim->lt("deluge", "de luge"));
479 ok($shTrim->lt("de luge", "de-luge"));
480 ok($shTrim->lt("de-luge", "deLuge"));
481 ok($shTrim->lt("deLuge", "de Luge"));
485 my $overCJK = Unicode::Collate->new(
487 normalization => undef,
488 entry => <<'ENTRIES',
489 0061 ; [.0101.0020.0002.0061] # latin a
490 0041 ; [.0101.0020.0008.0041] # LATIN A
491 4E00 ; [.B1FC.0030.0004.4E00] # Ideograph; B1FC = FFFF - 4E03.
494 my $u = 0xFFFF - $_[0]; # reversed
499 ok($overCJK->lt("a", "A")); # diff. at level 3.
500 ok($overCJK->lt( "\x{4E03}", "\x{4E00}")); # diff. at level 2.
501 ok($overCJK->lt("A\x{4E03}", "A\x{4E00}"));
502 ok($overCJK->lt("A\x{4E03}", "a\x{4E00}"));
503 ok($overCJK->lt("a\x{4E03}", "A\x{4E00}"));
507 # rearranged : 0x0E40..0x0E44, 0x0EC0..0x0EC4
509 ok($Collator->lt("A", "B"));
510 ok($Collator->lt("\x{0E40}", "\x{0E41}"));
511 ok($Collator->lt("\x{0E40}A", "\x{0E41}B"));
512 ok($Collator->lt("\x{0E41}A", "\x{0E40}B"));
513 ok($Collator->lt("A\x{0E41}A", "A\x{0E40}B"));
515 ok($all_undef->lt("A", "B"));
516 ok($all_undef->lt("\x{0E40}", "\x{0E41}"));
517 ok($all_undef->lt("\x{0E40}A", "\x{0E41}B"));
518 ok($all_undef->lt("\x{0E41}A", "\x{0E40}B"));
519 ok($all_undef->lt("A\x{0E41}A", "A\x{0E40}B"));
523 my $no_rearrange = Unicode::Collate->new(
525 normalization => undef,
529 ok($no_rearrange->lt("A", "B"));
530 ok($no_rearrange->lt("\x{0E40}", "\x{0E41}"));
531 ok($no_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
532 ok($no_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
533 ok($no_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
537 # equivalent to $no_rearrange
539 my $undef_rearrange = Unicode::Collate->new(
541 normalization => undef,
545 ok($undef_rearrange->lt("A", "B"));
546 ok($undef_rearrange->lt("\x{0E40}", "\x{0E41}"));
547 ok($undef_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
548 ok($undef_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
549 ok($undef_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
553 my $dropArticles = Unicode::Collate->new(
555 normalization => undef,
558 $string =~ s/\b(?:an?|the)\s+//ig;
563 ok($dropArticles->eq("camel", "a camel"));
564 ok($dropArticles->eq("Perl", "The Perl"));
565 ok($dropArticles->lt("the pen", "a pencil"));
566 ok($Collator->lt("Perl", "The Perl"));
567 ok($Collator->gt("the pen", "a pencil"));
571 my $backLevel1 = Unicode::Collate->new(
573 normalization => undef,
577 # all strings are reversed at level 1.
579 ok($backLevel1->gt("AB", "BA"));
580 ok($backLevel1->gt("\x{3042}\x{3044}", "\x{3044}\x{3042}"));
584 my $backLevel2 = Unicode::Collate->new(
586 normalization => undef,
587 undefName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
591 ok($backLevel2->gt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
592 ok($backLevel2->gt("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}"));
594 ok($Collator ->lt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
597 # HIRAGANA and KATAKANA are made undefined via undefName.
598 # So they are after CJK Unified Ideographs.
600 ok($backLevel2->lt("\x{4E00}", $hiragana));
601 ok($backLevel2->lt("\x{4E03}", $katakana));
602 ok($Collator ->gt("\x{4E00}", $hiragana));
603 ok($Collator ->gt("\x{4E03}", $katakana));