d540d992269dca97369b1f8d5a185945f9f76b42
[p5sagit/p5-mst-13.2.git] / ext / Unicode / Normalize / t / func.t
1
2 BEGIN {
3     if (ord("A") == 193) {
4         print "1..0 # Unicode::Normalize not ported to EBCDIC\n";
5         exit 0;
6     }
7 }
8
9 BEGIN {
10     if ($ENV{PERL_CORE}) {
11         chdir('t') if -d 't';
12         @INC = qw(../lib);
13     }
14 }
15
16 #########################
17
18 use Test;
19 use strict;
20 use warnings;
21 BEGIN { plan tests => 13 };
22 use Unicode::Normalize qw(:all);
23 ok(1); # If we made it this far, we're ok.
24
25 our $IsEBCDIC = ord("A") != 0x41;
26
27 sub _pack_U {
28     return $IsEBCDIC
29         ? pack('U*', map utf8::unicode_to_native($_), @_)
30         : pack('U*', @_);
31 }
32
33 sub _unpack_U {
34     return $IsEBCDIC
35         ? map(utf8::native_to_unicode($_), unpack 'U*', shift)
36         : unpack('U*', shift);
37 }
38
39 #########################
40
41 print getCombinClass(   0) == 0
42    && getCombinClass( 768) == 230
43    && getCombinClass(1809) == 36
44    && ($] < 5.007003 || getCombinClass(0x1D167) == 1) # Unicode 3.1
45   ? "ok" : "not ok", " 2\n";
46
47 print ! defined getCanon( 0)
48    && ! defined getCanon(41)
49    && getCanon(0x00C0) eq _pack_U(0x0041, 0x0300)
50    && getCanon(0x00EF) eq _pack_U(0x0069, 0x0308)
51    && getCanon(0x304C) eq _pack_U(0x304B, 0x3099)
52    && getCanon(0x1EA4) eq _pack_U(0x0041, 0x0302, 0x0301)
53    && getCanon(0x1F82) eq "\x{03B1}\x{0313}\x{0300}\x{0345}"
54    && getCanon(0x1FAF) eq _pack_U(0x03A9, 0x0314, 0x0342, 0x0345)
55    && getCanon(0xAC00) eq _pack_U(0x1100, 0x1161)
56    && getCanon(0xAE00) eq _pack_U(0x1100, 0x1173, 0x11AF)
57    && ! defined getCanon(0x212C)
58    && ! defined getCanon(0x3243)
59    && getCanon(0xFA2D) eq _pack_U(0x9DB4)
60   ? "ok" : "not ok", " 3\n";
61
62 print ! defined getCompat( 0)
63    && ! defined getCompat(41)
64    && getCompat(0x00C0) eq _pack_U(0x0041, 0x0300)
65    && getCompat(0x00EF) eq _pack_U(0x0069, 0x0308)
66    && getCompat(0x304C) eq _pack_U(0x304B, 0x3099)
67    && getCompat(0x1EA4) eq _pack_U(0x0041, 0x0302, 0x0301)
68    && getCompat(0x1F82) eq _pack_U(0x03B1, 0x0313, 0x0300, 0x0345)
69    && getCompat(0x1FAF) eq _pack_U(0x03A9, 0x0314, 0x0342, 0x0345)
70    && getCompat(0x212C) eq _pack_U(0x0042)
71    && getCompat(0x3243) eq _pack_U(0x0028, 0x81F3, 0x0029)
72    && getCompat(0xAC00) eq _pack_U(0x1100, 0x1161)
73    && getCompat(0xAE00) eq _pack_U(0x1100, 0x1173, 0x11AF)
74    && getCompat(0xFA2D) eq _pack_U(0x9DB4)
75   ? "ok" : "not ok", " 4\n";
76
77 print ! defined getComposite( 0,  0)
78    && ! defined getComposite( 0, 41)
79    && ! defined getComposite(41,  0)
80    && ! defined getComposite(41, 41)
81    && ! defined getComposite(12, 0x0300)
82    && ! defined getComposite(0x0055, 0xFF00)
83    && 0x00C0 == getComposite(0x0041, 0x0300)
84    && 0x00D9 == getComposite(0x0055, 0x0300)
85    && 0x1E14 == getComposite(0x0112, 0x0300)
86    && 0xAC00 == getComposite(0x1100, 0x1161)
87    && 0xADF8 == getComposite(0x1100, 0x1173)
88    && ! defined getComposite(0x1100, 0x11AF)
89    && ! defined getComposite(0x1173, 0x11AF)
90    && ! defined getComposite(0xAC00, 0x11A7)
91    && 0xAC01 == getComposite(0xAC00, 0x11A8)
92    && 0xAE00 == getComposite(0xADF8, 0x11AF)
93   ? "ok" : "not ok", " 5\n";
94
95 print ! isExclusion( 0)
96    && ! isExclusion(41)
97    && isExclusion(2392)  # DEVANAGARI LETTER QA
98    && isExclusion(3907)  # TIBETAN LETTER GHA
99    && isExclusion(64334) # HEBREW LETTER PE WITH RAFE
100   ? "ok" : "not ok", " 6\n";
101
102 print ! isSingleton( 0)
103    && isSingleton(0x212B) # ANGSTROM SIGN
104   ? "ok" : "not ok", " 7\n";
105
106 print reorder("") eq ""
107    && reorder(_pack_U(0x0041, 0x0300, 0x0315, 0x0313, 0x031b, 0x0061))
108       eq _pack_U(0x0041, 0x031b, 0x0300, 0x0313, 0x0315, 0x0061)
109    && reorder(_pack_U(0x00C1, 0x0300, 0x0315, 0x0313, 0x031b,
110         0x0061, 0x309A, 0x3099))
111       eq _pack_U(0x00C1, 0x031b, 0x0300, 0x0313, 0x0315,
112         0x0061, 0x309A, 0x3099)
113   ? "ok" : "not ok", " 8\n";
114
115 sub answer { defined $_[0] ? $_[0] ? "YES" : "NO" : "MAYBE" }
116
117 print answer(checkNFD(""))  eq "YES"
118   &&  answer(checkNFC(""))  eq "YES"
119   &&  answer(checkNFKD("")) eq "YES"
120   &&  answer(checkNFKC("")) eq "YES"
121   &&  answer(check("NFD", "")) eq "YES"
122   &&  answer(check("NFC", "")) eq "YES"
123   &&  answer(check("NFKD","")) eq "YES"
124   &&  answer(check("NFKC","")) eq "YES"
125 # U+0000 to U+007F are prenormalized in all the normalization forms.
126   && answer(checkNFD("AZaz\t12!#`"))  eq "YES"
127   && answer(checkNFC("AZaz\t12!#`"))  eq "YES"
128   && answer(checkNFKD("AZaz\t12!#`")) eq "YES"
129   && answer(checkNFKC("AZaz\t12!#`")) eq "YES"
130   && answer(check("D", "AZaz\t12!#`")) eq "YES"
131   && answer(check("C", "AZaz\t12!#`")) eq "YES"
132   && answer(check("KD","AZaz\t12!#`")) eq "YES"
133   && answer(check("KC","AZaz\t12!#`")) eq "YES"
134   ? "ok" : "not ok", " 9\n";
135
136 print 1
137   && answer(checkNFD(NFD(_pack_U(0xC1, 0x1100, 0x1173, 0x11AF)))) eq "YES"
138   && answer(checkNFD(_pack_U(0x20, 0xC1, 0x1100, 0x1173, 0x11AF))) eq "NO"
139   && answer(checkNFC(_pack_U(0x20, 0xC1, 0x1173, 0x11AF))) eq "MAYBE"
140   && answer(checkNFC(_pack_U(0x20, 0xC1, 0xAE00, 0x1100))) eq "YES"
141   && answer(checkNFC(_pack_U(0x20, 0xC1, 0xAE00, 0x1100, 0x300))) eq "MAYBE"
142   && answer(checkNFC(_pack_U(0x20, 0xC1, 0xFF71, 0x2025))) eq "YES"
143   && answer(check("NFC", _pack_U(0x20, 0xC1, 0x212B, 0x300))) eq "NO"
144   && answer(checkNFKD(_pack_U(0x20, 0xC1, 0xFF71, 0x2025))) eq "NO"
145   && answer(checkNFKC(_pack_U(0x20, 0xC1, 0xAE00, 0x2025))) eq "NO"
146   ? "ok" : "not ok", " 10\n";
147
148 "012ABC" =~ /(\d+)(\w+)/;
149 print "012" eq NFC $1 && "ABC" eq NFC $2
150   ? "ok" : "not ok", " 11\n";
151
152 print "012" eq normalize('C', $1) && "ABC" eq normalize('C', $2)
153   ? "ok" : "not ok", " 12\n";
154
155 print "012" eq normalize('NFC', $1) && "ABC" eq normalize('NFC', $2)
156   ? "ok" : "not ok", " 13\n";
157  # s/^NF// in normalize() must not prevent using $1, $&, etc.
158