]> git.proxmox.com Git - mirror_edk2.git/blame - AppPkg/Applications/Python/Python-2.7.2/Lib/test/test_unicodedata.py
EmbeddedPkg: Extend NvVarStoreFormattedLib LIBRARY_CLASS
[mirror_edk2.git] / AppPkg / Applications / Python / Python-2.7.2 / Lib / test / test_unicodedata.py
CommitLineData
4710c53d 1""" Test script for the unicodedata module.\r
2\r
3 Written by Marc-Andre Lemburg (mal@lemburg.com).\r
4\r
5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.\r
6\r
7"""\r
8\r
9import sys\r
10import unittest\r
11import hashlib\r
12import subprocess\r
13import test.test_support\r
14\r
15encoding = 'utf-8'\r
16\r
17\r
18### Run tests\r
19\r
20class UnicodeMethodsTest(unittest.TestCase):\r
21\r
22 # update this, if the database changes\r
23 expectedchecksum = '4504dffd035baea02c5b9de82bebc3d65e0e0baf'\r
24\r
25 def test_method_checksum(self):\r
26 h = hashlib.sha1()\r
27 for i in range(0x10000):\r
28 char = unichr(i)\r
29 data = [\r
30 # Predicates (single char)\r
31 u"01"[char.isalnum()],\r
32 u"01"[char.isalpha()],\r
33 u"01"[char.isdecimal()],\r
34 u"01"[char.isdigit()],\r
35 u"01"[char.islower()],\r
36 u"01"[char.isnumeric()],\r
37 u"01"[char.isspace()],\r
38 u"01"[char.istitle()],\r
39 u"01"[char.isupper()],\r
40\r
41 # Predicates (multiple chars)\r
42 u"01"[(char + u'abc').isalnum()],\r
43 u"01"[(char + u'abc').isalpha()],\r
44 u"01"[(char + u'123').isdecimal()],\r
45 u"01"[(char + u'123').isdigit()],\r
46 u"01"[(char + u'abc').islower()],\r
47 u"01"[(char + u'123').isnumeric()],\r
48 u"01"[(char + u' \t').isspace()],\r
49 u"01"[(char + u'abc').istitle()],\r
50 u"01"[(char + u'ABC').isupper()],\r
51\r
52 # Mappings (single char)\r
53 char.lower(),\r
54 char.upper(),\r
55 char.title(),\r
56\r
57 # Mappings (multiple chars)\r
58 (char + u'abc').lower(),\r
59 (char + u'ABC').upper(),\r
60 (char + u'abc').title(),\r
61 (char + u'ABC').title(),\r
62\r
63 ]\r
64 h.update(u''.join(data).encode(encoding))\r
65 result = h.hexdigest()\r
66 self.assertEqual(result, self.expectedchecksum)\r
67\r
68class UnicodeDatabaseTest(unittest.TestCase):\r
69\r
70 def setUp(self):\r
71 # In case unicodedata is not available, this will raise an ImportError,\r
72 # but the other test cases will still be run\r
73 import unicodedata\r
74 self.db = unicodedata\r
75\r
76 def tearDown(self):\r
77 del self.db\r
78\r
79class UnicodeFunctionsTest(UnicodeDatabaseTest):\r
80\r
81 # update this, if the database changes\r
82 expectedchecksum = '6ccf1b1a36460d2694f9b0b0f0324942fe70ede6'\r
83\r
84 def test_function_checksum(self):\r
85 data = []\r
86 h = hashlib.sha1()\r
87\r
88 for i in range(0x10000):\r
89 char = unichr(i)\r
90 data = [\r
91 # Properties\r
92 str(self.db.digit(char, -1)),\r
93 str(self.db.numeric(char, -1)),\r
94 str(self.db.decimal(char, -1)),\r
95 self.db.category(char),\r
96 self.db.bidirectional(char),\r
97 self.db.decomposition(char),\r
98 str(self.db.mirrored(char)),\r
99 str(self.db.combining(char)),\r
100 ]\r
101 h.update(''.join(data))\r
102 result = h.hexdigest()\r
103 self.assertEqual(result, self.expectedchecksum)\r
104\r
105 def test_digit(self):\r
106 self.assertEqual(self.db.digit(u'A', None), None)\r
107 self.assertEqual(self.db.digit(u'9'), 9)\r
108 self.assertEqual(self.db.digit(u'\u215b', None), None)\r
109 self.assertEqual(self.db.digit(u'\u2468'), 9)\r
110 self.assertEqual(self.db.digit(u'\U00020000', None), None)\r
111\r
112 self.assertRaises(TypeError, self.db.digit)\r
113 self.assertRaises(TypeError, self.db.digit, u'xx')\r
114 self.assertRaises(ValueError, self.db.digit, u'x')\r
115\r
116 def test_numeric(self):\r
117 self.assertEqual(self.db.numeric(u'A',None), None)\r
118 self.assertEqual(self.db.numeric(u'9'), 9)\r
119 self.assertEqual(self.db.numeric(u'\u215b'), 0.125)\r
120 self.assertEqual(self.db.numeric(u'\u2468'), 9.0)\r
121 self.assertEqual(self.db.numeric(u'\ua627'), 7.0)\r
122 self.assertEqual(self.db.numeric(u'\U00020000', None), None)\r
123\r
124 self.assertRaises(TypeError, self.db.numeric)\r
125 self.assertRaises(TypeError, self.db.numeric, u'xx')\r
126 self.assertRaises(ValueError, self.db.numeric, u'x')\r
127\r
128 def test_decimal(self):\r
129 self.assertEqual(self.db.decimal(u'A',None), None)\r
130 self.assertEqual(self.db.decimal(u'9'), 9)\r
131 self.assertEqual(self.db.decimal(u'\u215b', None), None)\r
132 self.assertEqual(self.db.decimal(u'\u2468', None), None)\r
133 self.assertEqual(self.db.decimal(u'\U00020000', None), None)\r
134\r
135 self.assertRaises(TypeError, self.db.decimal)\r
136 self.assertRaises(TypeError, self.db.decimal, u'xx')\r
137 self.assertRaises(ValueError, self.db.decimal, u'x')\r
138\r
139 def test_category(self):\r
140 self.assertEqual(self.db.category(u'\uFFFE'), 'Cn')\r
141 self.assertEqual(self.db.category(u'a'), 'Ll')\r
142 self.assertEqual(self.db.category(u'A'), 'Lu')\r
143 self.assertEqual(self.db.category(u'\U00020000'), 'Lo')\r
144\r
145 self.assertRaises(TypeError, self.db.category)\r
146 self.assertRaises(TypeError, self.db.category, u'xx')\r
147\r
148 def test_bidirectional(self):\r
149 self.assertEqual(self.db.bidirectional(u'\uFFFE'), '')\r
150 self.assertEqual(self.db.bidirectional(u' '), 'WS')\r
151 self.assertEqual(self.db.bidirectional(u'A'), 'L')\r
152 self.assertEqual(self.db.bidirectional(u'\U00020000'), 'L')\r
153\r
154 self.assertRaises(TypeError, self.db.bidirectional)\r
155 self.assertRaises(TypeError, self.db.bidirectional, u'xx')\r
156\r
157 def test_decomposition(self):\r
158 self.assertEqual(self.db.decomposition(u'\uFFFE'),'')\r
159 self.assertEqual(self.db.decomposition(u'\u00bc'), '<fraction> 0031 2044 0034')\r
160\r
161 self.assertRaises(TypeError, self.db.decomposition)\r
162 self.assertRaises(TypeError, self.db.decomposition, u'xx')\r
163\r
164 def test_mirrored(self):\r
165 self.assertEqual(self.db.mirrored(u'\uFFFE'), 0)\r
166 self.assertEqual(self.db.mirrored(u'a'), 0)\r
167 self.assertEqual(self.db.mirrored(u'\u2201'), 1)\r
168 self.assertEqual(self.db.mirrored(u'\U00020000'), 0)\r
169\r
170 self.assertRaises(TypeError, self.db.mirrored)\r
171 self.assertRaises(TypeError, self.db.mirrored, u'xx')\r
172\r
173 def test_combining(self):\r
174 self.assertEqual(self.db.combining(u'\uFFFE'), 0)\r
175 self.assertEqual(self.db.combining(u'a'), 0)\r
176 self.assertEqual(self.db.combining(u'\u20e1'), 230)\r
177 self.assertEqual(self.db.combining(u'\U00020000'), 0)\r
178\r
179 self.assertRaises(TypeError, self.db.combining)\r
180 self.assertRaises(TypeError, self.db.combining, u'xx')\r
181\r
182 def test_normalize(self):\r
183 self.assertRaises(TypeError, self.db.normalize)\r
184 self.assertRaises(ValueError, self.db.normalize, 'unknown', u'xx')\r
185 self.assertEqual(self.db.normalize('NFKC', u''), u'')\r
186 # The rest can be found in test_normalization.py\r
187 # which requires an external file.\r
188\r
189 def test_pr29(self):\r
190 # http://www.unicode.org/review/pr-29.html\r
191 # See issues #1054943 and #10254.\r
192 composed = (u"\u0b47\u0300\u0b3e", u"\u1100\u0300\u1161",\r
193 u'Li\u030dt-s\u1e73\u0301',\r
194 u'\u092e\u093e\u0930\u094d\u0915 \u091c\u093c'\r
195 + u'\u0941\u0915\u0947\u0930\u092c\u0930\u094d\u0917',\r
196 u'\u0915\u093f\u0930\u094d\u0917\u093f\u091c\u093c'\r
197 + 'u\u0938\u094d\u0924\u093e\u0928')\r
198 for text in composed:\r
199 self.assertEqual(self.db.normalize('NFC', text), text)\r
200\r
201 def test_issue10254(self):\r
202 # Crash reported in #10254\r
203 a = u'C\u0338' * 20 + u'C\u0327'\r
204 b = u'C\u0338' * 20 + u'\xC7'\r
205 self.assertEqual(self.db.normalize('NFC', a), b)\r
206\r
207 def test_east_asian_width(self):\r
208 eaw = self.db.east_asian_width\r
209 self.assertRaises(TypeError, eaw, 'a')\r
210 self.assertRaises(TypeError, eaw, u'')\r
211 self.assertRaises(TypeError, eaw, u'ra')\r
212 self.assertEqual(eaw(u'\x1e'), 'N')\r
213 self.assertEqual(eaw(u'\x20'), 'Na')\r
214 self.assertEqual(eaw(u'\uC894'), 'W')\r
215 self.assertEqual(eaw(u'\uFF66'), 'H')\r
216 self.assertEqual(eaw(u'\uFF1F'), 'F')\r
217 self.assertEqual(eaw(u'\u2010'), 'A')\r
218 self.assertEqual(eaw(u'\U00020000'), 'W')\r
219\r
220class UnicodeMiscTest(UnicodeDatabaseTest):\r
221\r
222 def test_failed_import_during_compiling(self):\r
223 # Issue 4367\r
224 # Decoding \N escapes requires the unicodedata module. If it can't be\r
225 # imported, we shouldn't segfault.\r
226\r
227 # This program should raise a SyntaxError in the eval.\r
228 code = "import sys;" \\r
229 "sys.modules['unicodedata'] = None;" \\r
230 """eval("u'\N{SOFT HYPHEN}'")"""\r
231 args = [sys.executable, "-c", code]\r
232 # We use a subprocess because the unicodedata module may already have\r
233 # been loaded in this process.\r
234 popen = subprocess.Popen(args, stderr=subprocess.PIPE)\r
235 popen.wait()\r
236 self.assertEqual(popen.returncode, 1)\r
237 error = "SyntaxError: (unicode error) \N escapes not supported " \\r
238 "(can't load unicodedata module)"\r
239 self.assertIn(error, popen.stderr.read())\r
240\r
241 def test_decimal_numeric_consistent(self):\r
242 # Test that decimal and numeric are consistent,\r
243 # i.e. if a character has a decimal value,\r
244 # its numeric value should be the same.\r
245 count = 0\r
246 for i in xrange(0x10000):\r
247 c = unichr(i)\r
248 dec = self.db.decimal(c, -1)\r
249 if dec != -1:\r
250 self.assertEqual(dec, self.db.numeric(c))\r
251 count += 1\r
252 self.assertTrue(count >= 10) # should have tested at least the ASCII digits\r
253\r
254 def test_digit_numeric_consistent(self):\r
255 # Test that digit and numeric are consistent,\r
256 # i.e. if a character has a digit value,\r
257 # its numeric value should be the same.\r
258 count = 0\r
259 for i in xrange(0x10000):\r
260 c = unichr(i)\r
261 dec = self.db.digit(c, -1)\r
262 if dec != -1:\r
263 self.assertEqual(dec, self.db.numeric(c))\r
264 count += 1\r
265 self.assertTrue(count >= 10) # should have tested at least the ASCII digits\r
266\r
267 def test_bug_1704793(self):\r
268 self.assertEqual(self.db.lookup("GOTHIC LETTER FAIHU"), u'\U00010346')\r
269\r
270 def test_ucd_510(self):\r
271 import unicodedata\r
272 # In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0\r
273 self.assertTrue(unicodedata.mirrored(u"\u0f3a"))\r
274 self.assertTrue(not unicodedata.ucd_3_2_0.mirrored(u"\u0f3a"))\r
275 # Also, we now have two ways of representing\r
276 # the upper-case mapping: as delta, or as absolute value\r
277 self.assertTrue(u"a".upper()==u'A')\r
278 self.assertTrue(u"\u1d79".upper()==u'\ua77d')\r
279 self.assertTrue(u".".upper()==u".")\r
280\r
281 def test_bug_5828(self):\r
282 self.assertEqual(u"\u1d79".lower(), u"\u1d79")\r
283 # Only U+0000 should have U+0000 as its upper/lower/titlecase variant\r
284 self.assertEqual(\r
285 [\r
286 c for c in range(sys.maxunicode+1)\r
287 if u"\x00" in unichr(c).lower()+unichr(c).upper()+unichr(c).title()\r
288 ],\r
289 [0]\r
290 )\r
291\r
292 def test_bug_4971(self):\r
293 # LETTER DZ WITH CARON: DZ, Dz, dz\r
294 self.assertEqual(u"\u01c4".title(), u"\u01c5")\r
295 self.assertEqual(u"\u01c5".title(), u"\u01c5")\r
296 self.assertEqual(u"\u01c6".title(), u"\u01c5")\r
297\r
298 def test_linebreak_7643(self):\r
299 for i in range(0x10000):\r
300 lines = (unichr(i) + u'A').splitlines()\r
301 if i in (0x0a, 0x0b, 0x0c, 0x0d, 0x85,\r
302 0x1c, 0x1d, 0x1e, 0x2028, 0x2029):\r
303 self.assertEqual(len(lines), 2,\r
304 r"\u%.4x should be a linebreak" % i)\r
305 else:\r
306 self.assertEqual(len(lines), 1,\r
307 r"\u%.4x should not be a linebreak" % i)\r
308\r
309def test_main():\r
310 test.test_support.run_unittest(\r
311 UnicodeMiscTest,\r
312 UnicodeMethodsTest,\r
313 UnicodeFunctionsTest\r
314 )\r
315\r
316if __name__ == "__main__":\r
317 test_main()\r