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 => 156 };
16 ok(1); # If we made it this far, we're ok.
18 #########################
20 my $Collator = Unicode::Collate->new(
22 normalization => undef,
25 ok(ref $Collator, "Unicode::Collate");
28 join(':', $Collator->sort(
29 qw/ lib strict Carp ExtUtils CGI Time warnings Math overload Pod CPAN /
32 qw/ Carp CGI CPAN ExtUtils lib Math overload Pod strict Time warnings /
36 my $A_acute = pack('U', 0x00C1);
37 my $acute = pack('U', 0x0301);
39 ok($Collator->cmp("A$acute", $A_acute), -1);
40 ok($Collator->cmp("", ""), 0);
41 ok(! $Collator->ne("", "") );
42 ok( $Collator->eq("", "") );
43 ok($Collator->cmp("", "perl"), -1);
47 eval { require Unicode::Normalize };
50 my $NFD = Unicode::Collate->new(
53 0430 ; [.0B01.0020.0002.0430] # CYRILLIC SMALL LETTER A
54 0410 ; [.0B01.0020.0008.0410] # CYRILLIC CAPITAL LETTER A
55 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
56 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
57 04D3 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
58 0430 0308 ; [.0B09.0020.0002.04D3] # CYRILLIC SMALL LETTER A WITH DIAERESIS
59 04D2 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
60 0410 0308 ; [.0B09.0020.0008.04D2] # CYRILLIC CAPITAL LETTER A WITH DIAERESIS
61 0430 3099 ; [.0B10.0020.0002.04D3] # A WITH KATAKANA VOICED
62 0430 3099 0308 ; [.0B11.0020.0002.04D3] # A WITH KATAKANA VOICED, DIAERESIS
65 ok($NFD->eq("A$acute", $A_acute));
66 ok($NFD->eq("\x{4D3}\x{325}", "\x{430}\x{308}\x{325}"));
67 ok($NFD->lt("\x{430}\x{308}A", "\x{430}\x{308}B"));
68 ok($NFD->lt("\x{430}\x{3099}B", "\x{430}\x{308}\x{3099}A"));
69 ok($NFD->eq("\x{0430}\x{3099}\x{309A}\x{0308}",
70 "\x{0430}\x{309A}\x{3099}\x{0308}") );
82 my $trad = Unicode::Collate->new(
84 normalization => undef,
85 ignoreName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
87 entry => << 'ENTRIES',
88 0063 0068 ; [.0893.0020.0002.0063] % "ch" in traditional Spanish
89 0043 0068 ; [.0893.0020.0008.0043] # "Ch" in traditional Spanish
90 00DF ; [.09F3.0154.0004.00DF] [.09F3.0020.0004.00DF] # eszet in Germany
95 join(':', $trad->sort( qw/ acha aca ada acia acka / ) ),
96 join(':', qw/ aca acia acka acha ada / ),
100 join(':', $Collator->sort( qw/ acha aca ada acia acka / ) ),
101 join(':', qw/ aca acha acia acka ada / ),
104 my $hiragana = "\x{3042}\x{3044}";
105 my $katakana = "\x{30A2}\x{30A4}";
107 # HIRAGANA and KATAKANA are ignorable via ignoreName
108 ok($trad->eq($hiragana, ""));
109 ok($trad->eq("", $katakana));
110 ok($trad->eq($hiragana, $katakana));
111 ok($trad->eq($katakana, $hiragana));
115 my $old_level = $Collator->{level};
117 $Collator->{level} = 2;
119 ok( $Collator->cmp("ABC","abc"), 0);
120 ok( $Collator->eq("ABC","abc") );
121 ok( $Collator->le("ABC","abc") );
122 ok( $Collator->cmp($hiragana, $katakana), 0);
123 ok( $Collator->eq($hiragana, $katakana) );
124 ok( $Collator->ge($hiragana, $katakana) );
127 ok( $Collator->eq("a\x{AC00}b", "a\x{1100}\x{1161}b") );
128 ok( $Collator->eq("a\x{AE00}b", "a\x{1100}\x{1173}\x{11AF}b") );
129 ok( $Collator->gt("a\x{AE00}b", "a\x{1100}\x{1173}b\x{11AF}") );
130 ok( $Collator->lt("a\x{AC00}b", "a\x{AE00}b") );
131 ok( $Collator->gt("a\x{D7A3}b", "a\x{C544}b") );
132 ok( $Collator->lt("a\x{C544}b", "a\x{30A2}b") ); # hangul < hiragana
134 $Collator->{level} = $old_level;
136 $Collator->{katakana_before_hiragana} = 1;
138 ok( $Collator->cmp("abc", "ABC"), -1);
139 ok( $Collator->ne("abc", "ABC") );
140 ok( $Collator->lt("abc", "ABC") );
141 ok( $Collator->le("abc", "ABC") );
142 ok( $Collator->cmp($hiragana, $katakana), 1);
143 ok( $Collator->ne($hiragana, $katakana) );
144 ok( $Collator->gt($hiragana, $katakana) );
145 ok( $Collator->ge($hiragana, $katakana) );
147 $Collator->{upper_before_lower} = 1;
149 ok( $Collator->cmp("abc", "ABC"), 1);
150 ok( $Collator->ge("abc", "ABC"), 1);
151 ok( $Collator->gt("abc", "ABC"), 1);
152 ok( $Collator->cmp($hiragana, $katakana), 1);
153 ok( $Collator->ge($hiragana, $katakana), 1);
154 ok( $Collator->gt($hiragana, $katakana), 1);
156 $Collator->{katakana_before_hiragana} = 0;
158 ok( $Collator->cmp("abc", "ABC"), 1);
159 ok( $Collator->cmp($hiragana, $katakana), -1);
161 $Collator->{upper_before_lower} = 0;
163 ok( $Collator->cmp("abc", "ABC"), -1);
164 ok( $Collator->le("abc", "ABC") );
165 ok( $Collator->cmp($hiragana, $katakana), -1);
166 ok( $Collator->lt($hiragana, $katakana) );
170 my $ignoreAE = Unicode::Collate->new(
172 normalization => undef,
173 ignoreChar => qr/^[aAeE]$/,
176 ok($ignoreAE->eq("element","lament"));
177 ok($ignoreAE->eq("Perl","ePrl"));
181 my $onlyABC = Unicode::Collate->new(
183 entry => << 'ENTRIES',
184 0061 ; [.0101.0020.0002.0061] # LATIN SMALL LETTER A
185 0041 ; [.0101.0020.0008.0041] # LATIN CAPITAL LETTER A
186 0062 ; [.0102.0020.0002.0062] # LATIN SMALL LETTER B
187 0042 ; [.0102.0020.0008.0042] # LATIN CAPITAL LETTER B
188 0063 ; [.0103.0020.0002.0063] # LATIN SMALL LETTER C
189 0043 ; [.0103.0020.0008.0043] # LATIN CAPITAL LETTER C
194 join(':', $onlyABC->sort( qw/ ABA BAC cc A Ab cAc aB / ) ),
195 join(':', qw/ A aB Ab ABA BAC cAc cc / ),
200 my $undefAE = Unicode::Collate->new(
202 normalization => undef,
203 undefChar => qr/^[aAeE]$/,
206 ok($undefAE ->gt("edge","fog"));
207 ok($Collator->lt("edge","fog"));
208 ok($undefAE ->gt("lake","like"));
209 ok($Collator->lt("lake","like"));
213 $Collator->{level} = 2;
217 my $orig = "This is a Perl book.";
220 my $ret = "This is a camel book.";
223 if (my($pos,$len) = $Collator->index($str, $sub)) {
224 substr($str, $pos, $len, $rep);
229 $Collator->{level} = $old_level;
232 if (my($pos,$len) = $Collator->index($str, $sub)) {
233 substr($str, $pos, $len, $rep);
242 $Collator->{level} = 1;
244 $str = "Pe\x{300}rl";
247 if (my($pos, $len) = $Collator->index($str, $sub)) {
248 $match = substr($str, $pos, $len);
250 ok($match, "Pe\x{300}");
252 $str = "P\x{300}e\x{300}\x{301}\x{303}rl";
255 if (my($pos, $len) = $Collator->index($str, $sub)) {
256 $match = substr($str, $pos, $len);
258 ok($match, "P\x{300}e\x{300}\x{301}\x{303}");
260 $Collator->{level} = $old_level;
266 $str = "Ich mu\x{00DF} studieren.";
267 $sub = "m\x{00FC}ss";
269 if (my($pos, $len) = $trad->index($str, $sub)) {
270 $match = substr($str, $pos, $len);
272 ok($match, "mu\x{00DF}");
274 $trad->{level} = $old_level;
276 $str = "Ich mu\x{00DF} studieren.";
277 $sub = "m\x{00FC}ss";
280 if (my($pos, $len) = $trad->index($str, $sub)) {
281 $match = substr($str, $pos, $len);
286 if (my($pos,$len) = $Collator->index("", "")) {
287 $match = substr("", $pos, $len);
292 if (my($pos,$len) = $Collator->index("", "abc")) {
293 $match = substr("", $pos, $len);
299 # Table is undefined, then no entry is defined.
301 my $undef_table = Unicode::Collate->new(
303 normalization => undef,
307 # in the Unicode code point order
308 ok($undef_table->lt('', 'A'));
309 ok($undef_table->lt('ABC', 'B'));
311 # Hangul should be decomposed (even w/o Unicode::Normalize).
313 ok($undef_table->lt("Perl", "\x{AC00}"));
314 ok($undef_table->eq("\x{AC00}", "\x{1100}\x{1161}"));
315 ok($undef_table->eq("\x{AE00}", "\x{1100}\x{1173}\x{11AF}"));
316 ok($undef_table->lt("\x{AE00}", "\x{3042}"));
318 # U+AE00: Hangul GEUL
321 # Weight for CJK Ideographs is defined, though.
323 ok($undef_table->lt("", "\x{4E00}"));
324 ok($undef_table->lt("\x{4E8C}","ABC"));
325 ok($undef_table->lt("\x{4E00}","\x{3042}"));
326 ok($undef_table->lt("\x{4E00}","\x{4E8C}"));
327 # U+4E00: Ideograph "ONE"
328 # U+4E8C: Ideograph "TWO"
333 my $few_entries = Unicode::Collate->new(
334 entry => <<'ENTRIES',
335 0050 ; [.0101.0020.0002.0050] # P
336 0045 ; [.0102.0020.0002.0045] # E
337 0052 ; [.0103.0020.0002.0052] # R
338 004C ; [.0104.0020.0002.004C] # L
339 1100 ; [.0105.0020.0002.1100] # Hangul Jamo initial G
340 1175 ; [.0106.0020.0002.1175] # Hangul Jamo middle I
341 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
344 normalization => undef,
347 # defined before undefined
349 my $sortABC = join '',
350 $few_entries->sort(split //, "ABCDEFGHIJKLMNOPQRSTUVWXYZ ");
352 ok($sortABC eq "PERL ABCDFGHIJKMNOQSTUVWXYZ");
354 ok($few_entries->lt('E', 'D'));
355 ok($few_entries->lt("\x{5B57}", "\x{4E00}"));
356 ok($few_entries->lt("\x{AE30}", "\x{AC00}"));
358 # Hangul must be decomposed.
360 ok($few_entries->eq("\x{AC00}", "\x{1100}\x{1161}"));
364 my $all_undef = Unicode::Collate->new(
366 normalization => undef,
367 overrideCJK => undef,
368 overrideHangul => undef,
371 # All in the Unicode code point order.
372 # No hangul decomposition.
374 ok($all_undef->lt("\x{3042}", "\x{4E00}"));
375 ok($all_undef->lt("\x{4DFF}", "\x{4E00}"));
376 ok($all_undef->lt("\x{4E00}", "\x{AC00}"));
377 ok($all_undef->gt("\x{AC00}", "\x{1100}\x{1161}"));
378 ok($all_undef->gt("\x{AC00}", "\x{ABFF}"));
382 my $ignoreCJK = Unicode::Collate->new(
384 normalization => undef,
385 overrideCJK => sub {()},
386 entry => <<'ENTRIES',
387 5B57 ; [.0107.0020.0002.5B57] # CJK Ideograph "Letter"
391 # All CJK Unified Ideographs except U+5B57 are ignored.
393 ok($ignoreCJK->eq("\x{4E00}", ""));
394 ok($ignoreCJK->lt("\x{4E00}", "\0"));
395 ok($ignoreCJK->eq("Pe\x{4E00}rl", "Perl")); # U+4E00 is a CJK.
396 ok($ignoreCJK->gt("\x{4DFF}", "\x{4E00}")); # U+4DFF is not CJK.
397 ok($ignoreCJK->lt("Pe\x{5B57}rl", "Perl")); # 'r' is unassigned.
401 my $ignoreHangul = Unicode::Collate->new(
403 normalization => undef,
404 overrideHangul => sub {()},
405 entry => <<'ENTRIES',
406 AE00 ; [.0100.0020.0002.AE00] # Hangul GEUL
410 # All Hangul Syllables except U+AE00 are ignored.
412 ok($ignoreHangul->eq("\x{AC00}", ""));
413 ok($ignoreHangul->lt("\x{AC00}", "\0"));
414 ok($ignoreHangul->lt("\x{AC00}", "\x{AE00}"));
415 ok($ignoreHangul->lt("\x{AC00}", "\x{1100}\x{1161}")); # Jamo are not ignored.
416 ok($ignoreHangul->lt("Pe\x{AE00}rl", "Perl")); # 'r' is unassigned.
420 my $blanked = Unicode::Collate->new(
422 normalization => undef,
423 alternate => 'Blanked',
426 ok($blanked->lt("death", "de luge"));
427 ok($blanked->lt("de luge", "de-luge"));
428 ok($blanked->lt("de-luge", "deluge"));
429 ok($blanked->lt("deluge", "de\x{2010}luge"));
430 ok($blanked->lt("deluge", "de Luge"));
434 my $nonIgn = Unicode::Collate->new(
436 normalization => undef,
437 alternate => 'Non-ignorable',
440 ok($nonIgn->lt("de luge", "de Luge"));
441 ok($nonIgn->lt("de Luge", "de-luge"));
442 ok($nonIgn->lt("de-Luge", "de\x{2010}luge"));
443 ok($nonIgn->lt("de-luge", "death"));
444 ok($nonIgn->lt("death", "deluge"));
448 my $shifted = Unicode::Collate->new(
450 normalization => undef,
451 alternate => 'Shifted',
454 ok($shifted->lt("death", "de luge"));
455 ok($shifted->lt("de luge", "de-luge"));
456 ok($shifted->lt("de-luge", "deluge"));
457 ok($shifted->lt("deluge", "de Luge"));
458 ok($shifted->lt("de Luge", "deLuge"));
462 my $shTrim = Unicode::Collate->new(
464 normalization => undef,
465 alternate => 'Shift-Trimmed',
468 ok($shTrim->lt("death", "deluge"));
469 ok($shTrim->lt("deluge", "de luge"));
470 ok($shTrim->lt("de luge", "de-luge"));
471 ok($shTrim->lt("de-luge", "deLuge"));
472 ok($shTrim->lt("deLuge", "de Luge"));
476 my $overCJK = Unicode::Collate->new(
478 normalization => undef,
479 entry => <<'ENTRIES',
480 0061 ; [.0101.0020.0002.0061] # latin a
481 0041 ; [.0101.0020.0008.0041] # LATIN A
482 4E00 ; [.B1FC.0030.0004.4E00] # Ideograph; B1FC = FFFF - 4E03.
485 my $u = 0xFFFF - $_[0]; # reversed
490 ok($overCJK->lt("a", "A")); # diff. at level 3.
491 ok($overCJK->lt( "\x{4E03}", "\x{4E00}")); # diff. at level 2.
492 ok($overCJK->lt("A\x{4E03}", "A\x{4E00}"));
493 ok($overCJK->lt("A\x{4E03}", "a\x{4E00}"));
494 ok($overCJK->lt("a\x{4E03}", "A\x{4E00}"));
498 # rearranged : 0x0E40..0x0E44, 0x0EC0..0x0EC4
500 ok($Collator->lt("A", "B"));
501 ok($Collator->lt("\x{0E40}", "\x{0E41}"));
502 ok($Collator->lt("\x{0E40}A", "\x{0E41}B"));
503 ok($Collator->lt("\x{0E41}A", "\x{0E40}B"));
504 ok($Collator->lt("A\x{0E41}A", "A\x{0E40}B"));
506 ok($all_undef->lt("A", "B"));
507 ok($all_undef->lt("\x{0E40}", "\x{0E41}"));
508 ok($all_undef->lt("\x{0E40}A", "\x{0E41}B"));
509 ok($all_undef->lt("\x{0E41}A", "\x{0E40}B"));
510 ok($all_undef->lt("A\x{0E41}A", "A\x{0E40}B"));
514 my $no_rearrange = Unicode::Collate->new(
516 normalization => undef,
520 ok($no_rearrange->lt("A", "B"));
521 ok($no_rearrange->lt("\x{0E40}", "\x{0E41}"));
522 ok($no_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
523 ok($no_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
524 ok($no_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
528 # equivalent to $no_rearrange
530 my $undef_rearrange = Unicode::Collate->new(
532 normalization => undef,
536 ok($undef_rearrange->lt("A", "B"));
537 ok($undef_rearrange->lt("\x{0E40}", "\x{0E41}"));
538 ok($undef_rearrange->lt("\x{0E40}A", "\x{0E41}B"));
539 ok($undef_rearrange->gt("\x{0E41}A", "\x{0E40}B"));
540 ok($undef_rearrange->gt("A\x{0E41}A", "A\x{0E40}B"));
544 my $dropArticles = Unicode::Collate->new(
546 normalization => undef,
549 $string =~ s/\b(?:an?|the)\s+//ig;
554 ok($dropArticles->eq("camel", "a camel"));
555 ok($dropArticles->eq("Perl", "The Perl"));
556 ok($dropArticles->lt("the pen", "a pencil"));
557 ok($Collator->lt("Perl", "The Perl"));
558 ok($Collator->gt("the pen", "a pencil"));
562 my $backLevel1 = Unicode::Collate->new(
564 normalization => undef,
568 # all strings are reversed at level 1.
570 ok($backLevel1->gt("AB", "BA"));
571 ok($backLevel1->gt("\x{3042}\x{3044}", "\x{3044}\x{3042}"));
575 my $backLevel2 = Unicode::Collate->new(
577 normalization => undef,
578 undefName => qr/HANGUL|HIRAGANA|KATAKANA|BOPOMOFO/,
582 ok($backLevel2->gt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
583 ok($backLevel2->gt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
584 ok($Collator ->lt("Ca\x{300}ca\x{302}", "ca\x{302}ca\x{300}"));
585 ok($Collator ->lt("ca\x{300}ca\x{302}", "Ca\x{302}ca\x{300}"));
588 # HIRAGANA and KATAKANA are undefined via ignoreName.
589 # So they are after CJK Ideographs.
591 ok($backLevel2->lt("\x{4E00}", $hiragana));
592 ok($backLevel2->lt("\x{4E03}", $katakana));
593 ok($Collator ->gt("\x{4E00}", $hiragana));
594 ok($Collator ->gt("\x{4E03}", $katakana));