Debian Codegraph @

rpython/rlib/unicodedata/test/test_unicodedata.py

raw symbols SCIP
1 # encoding: utf-8
2 import random
3 import unicodedata
4
5 import py
6 import pytest
7
8 from rpython.rlib.unicodedata import (
9 unicodedb_3_2_0, unicodedb_5_2_0,
10 unicodedb_11_0_0, unicodedb_12_1_0, unicodedb_13_0_0,
11 unicodedb_14_0_0)
12
13
14 class TestUnicodeData(object):
15 def test_all_charnames_2bytes(self):
16 if unicodedata.unidata_version != '5.2.0':
17 py.test.skip('Needs python with unicode 5.2.0 database.')
18
19 for i in range(65536):
20 chr = unichr(i)
21 try:
22 name = unicodedata.name(chr)
23 except ValueError:
24 py.test.raises(KeyError, unicodedb_5_2_0.name, ord(chr))
25 else:
26 assert unicodedb_5_2_0.name(ord(chr)) == name
27 assert unicodedb_5_2_0.lookup(name) == ord(chr)
28
29 def test_isprintable(self):
30 assert unicodedb_5_2_0.isprintable(ord(' '))
31 assert unicodedb_5_2_0.isprintable(ord('a'))
32 assert not unicodedb_5_2_0.isprintable(127)
33 assert unicodedb_5_2_0.isprintable(0x00010346) # GOTHIC LETTER FAIHU
34 assert unicodedb_5_2_0.isprintable(0xfffd) # REPLACEMENT CHARACTER
35 assert unicodedb_5_2_0.isprintable(0xfffd) # REPLACEMENT CHARACTER
36 assert not unicodedb_5_2_0.isprintable(0xd800) # SURROGATE
37 assert not unicodedb_5_2_0.isprintable(0xE0020) # TAG SPACE
38
39 def test_identifier(self):
40 assert unicodedb_5_2_0.isxidstart(ord('A'))
41 assert not unicodedb_5_2_0.isxidstart(ord('_'))
42 assert not unicodedb_5_2_0.isxidstart(ord('0'))
43 assert not unicodedb_5_2_0.isxidstart(ord('('))
44 assert unicodedb_5_2_0.isxidcontinue(ord('A'))
45 assert unicodedb_5_2_0.isxidcontinue(ord('_'))
46 assert unicodedb_5_2_0.isxidcontinue(ord('0'))
47 assert not unicodedb_5_2_0.isxidcontinue(ord('('))
48 oc = ord(u'日')
49 assert unicodedb_5_2_0.isxidstart(oc)
50
51 def test_compare_functions(self):
52 def getX(fun, code):
53 try:
54 return getattr(unicodedb_5_2_0, fun)(code)
55 except KeyError:
56 return -1
57
58 for code in range(0x10000):
59 char = unichr(code)
60 assert unicodedata.digit(char, -1) == getX('digit', code)
61 assert unicodedata.numeric(char, -1) == getX('numeric', code)
62 assert unicodedata.decimal(char, -1) == getX('decimal', code)
63 assert unicodedata.category(char) == unicodedb_5_2_0.category(code)
64 assert unicodedata.bidirectional(char) == unicodedb_5_2_0.bidirectional(code)
65 assert unicodedata.decomposition(char) == unicodedb_5_2_0.decomposition(code)
66 assert unicodedata.mirrored(char) == unicodedb_5_2_0.mirrored(code)
67 assert unicodedata.combining(char) == unicodedb_5_2_0.combining(code)
68 # east_asian_width has a different default for unassigned
69 # characters on cpython (unicode says it should be 'N', CPython
70 # returns 'F')
71 n1 = None
72 try:
73 n1 = unicodedata.name(char)
74 except ValueError:
75 pass
76 else:
77 assert unicodedata.east_asian_width(char) == unicodedb_5_2_0.east_asian_width(code)
78
79 def test_compare_methods(self):
80 for code in range(0x10000):
81 char = unichr(code)
82 assert char.isalnum() == unicodedb_5_2_0.isalnum(code)
83 assert char.isalpha() == unicodedb_5_2_0.isalpha(code)
84 assert char.isdecimal() == unicodedb_5_2_0.isdecimal(code)
85 assert char.isdigit() == unicodedb_5_2_0.isdigit(code)
86 assert char.islower() == unicodedb_5_2_0.islower(code)
87 assert char.isnumeric() == unicodedb_5_2_0.isnumeric(code)
88 assert char.isspace() == unicodedb_5_2_0.isspace(code), hex(code)
89 assert char.istitle() == (unicodedb_5_2_0.isupper(code) or unicodedb_5_2_0.istitle(code)), code
90 assert char.isupper() == unicodedb_5_2_0.isupper(code)
91
92 assert char.lower() == unichr(unicodedb_5_2_0.tolower(code))
93 assert char.upper() == unichr(unicodedb_5_2_0.toupper(code))
94 assert char.title() == unichr(unicodedb_5_2_0.totitle(code)), hex(code)
95
96 def test_hangul_difference_520(self):
97 assert unicodedb_5_2_0.name(40874) == 'CJK UNIFIED IDEOGRAPH-9FAA'
98
99 def test_differences(self):
100 assert unicodedb_5_2_0.name(9187) == 'BENZENE RING WITH CIRCLE'
101 assert unicodedb_5_2_0.lookup('BENZENE RING WITH CIRCLE') == 9187
102 py.test.raises(KeyError, unicodedb_3_2_0.lookup, 'BENZENE RING WITH CIRCLE')
103 py.test.raises(KeyError, unicodedb_3_2_0.name, 9187)
104
105 def test_casefolding(self):
106 assert unicodedb_5_2_0.casefold_lookup(42592) == [42592]
107 # 1010 has been remove between 3.2.0 and 5.2.0
108 assert unicodedb_3_2_0.casefold_lookup(1010) == [963]
109 assert unicodedb_5_2_0.casefold_lookup(1010) == [1010]
110 # 7838 has been added in 5.2.0
111 assert unicodedb_3_2_0.casefold_lookup(7838) == [7838]
112 assert unicodedb_5_2_0.casefold_lookup(7838) == [115, 115]
113 # Only lookup who cannot be resolved by `lower` are stored in database
114 assert unicodedb_3_2_0.casefold_lookup(ord('E')) == [ord('e')]
115
116 def test_canon_decomposition(self):
117 assert unicodedb_3_2_0.compat_decomposition(296) == [73, 771]
118 assert unicodedb_3_2_0.canon_decomposition(296) == [73, 771]
119 assert unicodedb_3_2_0.compat_decomposition(32) == []
120 assert unicodedb_3_2_0.canon_decomposition(32) == []
121
122 def test_composition(self):
123 # e + circumflex
124 assert unicodedb_3_2_0.composition(ord('e'), 770) == 0xea
125 with pytest.raises(KeyError):
126 unicodedb_3_2_0.composition(ord('e'), ord('e'))
127
128 def test_alias(self):
129 with pytest.raises(KeyError):
130 unicodedb_5_2_0.lookup("LATIN SMALL LETTER GHA")
131 unicodedb_5_2_0.lookup("LATIN SMALL LETTER OI")
132 assert unicodedb_5_2_0.name(0x01a3) == "LATIN SMALL LETTER OI"
133 assert unicodedb_5_2_0.lookup_with_alias("LATIN SMALL LETTER GHA") == 0x01a3
134
135 class TestUnicodeData1100(object):
136 def test_some_additions(self):
137 additions = {
138 ord(u"\u20B9"): 'INDIAN RUPEE SIGN',
139 # u'\U0001F37A'
140 127866: 'BEER MUG',
141 # u'\U0001F37B'
142 127867: 'CLINKING BEER MUGS',
143 # u"\U0001F0AD"
144 127149: 'PLAYING CARD QUEEN OF SPADES',
145 # u"\U0002B740"
146 177984: "CJK UNIFIED IDEOGRAPH-2B740",
147 }
148 for un, name in additions.iteritems():
149 assert unicodedb_11_0_0.name(un) == name
150 assert unicodedb_11_0_0.isprintable(un)
151
152 def test_special_casing(self):
153 assert unicodedb_11_0_0.tolower_full(ord('A')) == [ord('a')]
154 # The German es-zed is special--the normal mapping is to SS.
155 assert unicodedb_11_0_0.tolower_full(ord(u'\xdf')) == [0xdf]
156 assert unicodedb_11_0_0.toupper_full(ord(u'\xdf')) == map(ord, 'SS')
157 assert unicodedb_11_0_0.totitle_full(ord(u'\xdf')) == map(ord, 'Ss')
158
159 def test_islower(self):
160 assert unicodedb_11_0_0.islower(0x2177)
161
162 def test_changed_in_version_8(self):
163 assert unicodedb_5_2_0.toupper_full(0x025C) == [0x025C]
164 assert unicodedb_11_0_0.toupper_full(0x025C) == [0xA7AB]
165
166 def test_casefold(self):
167 # when there is no special casefolding rule,
168 # tolower_full() is returned instead
169 assert unicodedb_11_0_0.casefold_lookup(0x1000) == unicodedb_11_0_0.tolower_full(0x1000)
170 assert unicodedb_11_0_0.casefold_lookup(0x0061) == unicodedb_11_0_0.tolower_full(0x0061)
171 assert unicodedb_11_0_0.casefold_lookup(0x0041) == unicodedb_11_0_0.tolower_full(0x0041)
172 # a case where casefold() != lower()
173 assert unicodedb_11_0_0.casefold_lookup(0x00DF) == [ord('s'), ord('s')]
174 # returns the argument itself, and not None, in rare cases
175 # where tolower_full() would return something different
176 assert unicodedb_11_0_0.casefold_lookup(0x13A0) == [0x13A0]
177
178 assert unicodedb_11_0_0.casefold_lookup(223) == [115, 115]
179 assert unicodedb_11_0_0.casefold_lookup(976) == [946]
180
181 def test_changed_in_version_11(self):
182 unicodedb_11_0_0.name(0x1f970) == 'SMILING FACE WITH SMILING EYES AND THREE HEARTS'
183
184 @pytest.mark.parametrize('db', [
185 unicodedb_5_2_0, unicodedb_11_0_0])
186 def test_turkish_i(db):
187 assert db.tolower_full(0x0130) == [0x69, 0x307]
188
189 def test_era_reiwa():
190 assert unicodedb_12_1_0.name(0x32ff) == 'SQUARE ERA NAME REIWA'
191
192 def test_unicode13():
193 assert unicodedb_13_0_0.name(0x1fa97) == 'ACCORDION'
194 assert unicodedb_13_0_0.name(0xd04) == 'MALAYALAM LETTER VEDIC ANUSVARA'
195
196 def test_unicode13_composition():
197 # some random checks to see that stuff didn't completely break
198 s = """
199 65 777 7842
200 65 772 256
201 69 803 7864
202 69 785 518
203 73 775 304
204 73 774 300
205 73 816 7724
206 75 807 310
207 76 769 313
208 79 795 416
209 79 772 332
210 79 770 212
211 79 779 336
212 85 770 219
213 85 771 360
214 89 776 376
215 97 778 229
216 97 803 7841
217 105 816 7725
218 105 776 239
219 110 769 324
220 111 808 491
221 117 769 250
222 119 776 7813
223 212 777 7892
224 416 769 7898
225 491 772 493
226 913 788 7945
227 953 776 970
228 1069 776 1260
229 1140 783 1142
230 1610 1620 1574
231 6929 6965 6930
232 7945 837 8073
233 8805 824 8817
234 8827 824 8833
235 69938 69927 69935"""
236 l = []
237 for line in s.strip().splitlines():
238 a, b, x = line.split()
239 l.append((int(a), int(b), int(x)))
240
241 for a, b, x in l:
242 assert unicodedb_13_0_0.composition(a, b) == x
243
244
245 def test_named_sequence():
246 with pytest.raises(KeyError):
247 unicodedb_13_0_0.lookup("KEYCAP DIGIT FIVE", with_named_sequence=False)
248 assert unicodedb_13_0_0.lookup("KEYCAP DIGIT FIVE", with_named_sequence=True)
249 code = unicodedb_13_0_0.lookup("KEYCAP DIGIT FIVE", with_named_sequence=True)
250 assert unicodedb_13_0_0.lookup_named_sequence(code) == b'5\xef\xb8\x8f\xe2\x83\xa3'
251 assert unicodedb_13_0_0.lookup_named_sequence_length(code) == 3
252
253 def test_cjk_13_missing_range_bug():
254 assert unicodedb_13_0_0.name(0x30000) == 'CJK UNIFIED IDEOGRAPH-30000'
255 assert unicodedb_13_0_0.name(0x3134a) == 'CJK UNIFIED IDEOGRAPH-3134A'
256 assert unicodedb_13_0_0.name(0x3104f) == 'CJK UNIFIED IDEOGRAPH-3104F'
257
258 def test_unicode14():
259 assert unicodedb_14_0_0.name(0x1e290) == 'TOTO LETTER PA'
260 assert unicodedb_14_0_0.name(0x1FAC3) == 'PREGNANT MAN'
261
262 def test_cjk_14_new_chars():
263 assert unicodedb_14_0_0.name(0x9ffd) == 'CJK UNIFIED IDEOGRAPH-9FFD'
264 assert unicodedb_14_0_0.name(0x2b735) == 'CJK UNIFIED IDEOGRAPH-2B735'
265