blob: d7a1eca104b59a8e7b7c73c80d5e48745d27a53f [file] [log] [blame]
Guido van Rossum24bdb042000-03-28 20:29:59 +00001""" Test script for the unicodedata module.
2
Marc-André Lemburg6a20ee72000-09-26 16:18:58 +00003 Written by Marc-Andre Lemburg (mal@lemburg.com).
Guido van Rossum24bdb042000-03-28 20:29:59 +00004
Marc-André Lemburg6a20ee72000-09-26 16:18:58 +00005 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
Guido van Rossum24bdb042000-03-28 20:29:59 +00006
Benjamin Peterson6f7fad12008-11-21 22:58:57 +00007"""
8
9import sys
10import unittest
Thomas Wouters477c8d52006-05-27 19:21:47 +000011import hashlib
Benjamin Peterson6f7fad12008-11-21 22:58:57 +000012import subprocess
13import test.support
Guido van Rossum24bdb042000-03-28 20:29:59 +000014
Marc-André Lemburg67ceca72000-09-27 12:24:34 +000015encoding = 'utf-8'
Martin v. Löwis54d9d072009-05-10 09:33:21 +000016errors = 'surrogatepass'
Marc-André Lemburg67ceca72000-09-27 12:24:34 +000017
Marc-André Lemburg6a20ee72000-09-26 16:18:58 +000018
19### Run tests
20
Walter Dörwald37c47282003-02-26 14:49:41 +000021class UnicodeMethodsTest(unittest.TestCase):
Marc-André Lemburg6a20ee72000-09-26 16:18:58 +000022
Walter Dörwald37c47282003-02-26 14:49:41 +000023 # update this, if the database changes
Florent Xiclunafaa663f2010-03-19 13:37:08 +000024 expectedchecksum = '4504dffd035baea02c5b9de82bebc3d65e0e0baf'
Guido van Rossum24bdb042000-03-28 20:29:59 +000025
Walter Dörwald37c47282003-02-26 14:49:41 +000026 def test_method_checksum(self):
Thomas Wouters477c8d52006-05-27 19:21:47 +000027 h = hashlib.sha1()
Florent Xicluna806d8cf2010-03-30 19:34:18 +000028 for i in range(0x10000):
Guido van Rossum84fc66d2007-05-03 17:18:26 +000029 char = chr(i)
Walter Dörwald37c47282003-02-26 14:49:41 +000030 data = [
31 # Predicates (single char)
Guido van Rossumef87d6e2007-05-02 19:09:54 +000032 "01"[char.isalnum()],
33 "01"[char.isalpha()],
34 "01"[char.isdecimal()],
35 "01"[char.isdigit()],
36 "01"[char.islower()],
37 "01"[char.isnumeric()],
38 "01"[char.isspace()],
39 "01"[char.istitle()],
40 "01"[char.isupper()],
Guido van Rossum24bdb042000-03-28 20:29:59 +000041
Walter Dörwald37c47282003-02-26 14:49:41 +000042 # Predicates (multiple chars)
Guido van Rossumef87d6e2007-05-02 19:09:54 +000043 "01"[(char + 'abc').isalnum()],
44 "01"[(char + 'abc').isalpha()],
45 "01"[(char + '123').isdecimal()],
46 "01"[(char + '123').isdigit()],
47 "01"[(char + 'abc').islower()],
48 "01"[(char + '123').isnumeric()],
49 "01"[(char + ' \t').isspace()],
50 "01"[(char + 'abc').istitle()],
51 "01"[(char + 'ABC').isupper()],
Guido van Rossum24bdb042000-03-28 20:29:59 +000052
Walter Dörwald37c47282003-02-26 14:49:41 +000053 # Mappings (single char)
54 char.lower(),
55 char.upper(),
56 char.title(),
Guido van Rossum24bdb042000-03-28 20:29:59 +000057
Walter Dörwald37c47282003-02-26 14:49:41 +000058 # Mappings (multiple chars)
Guido van Rossumef87d6e2007-05-02 19:09:54 +000059 (char + 'abc').lower(),
60 (char + 'ABC').upper(),
61 (char + 'abc').title(),
62 (char + 'ABC').title(),
Guido van Rossum24bdb042000-03-28 20:29:59 +000063
Walter Dörwald37c47282003-02-26 14:49:41 +000064 ]
Martin v. Löwisdb12d452009-05-02 18:52:14 +000065 h.update(''.join(data).encode(encoding, errors))
Walter Dörwald37c47282003-02-26 14:49:41 +000066 result = h.hexdigest()
67 self.assertEqual(result, self.expectedchecksum)
Guido van Rossum24bdb042000-03-28 20:29:59 +000068
Walter Dörwald37c47282003-02-26 14:49:41 +000069class UnicodeDatabaseTest(unittest.TestCase):
Guido van Rossum24bdb042000-03-28 20:29:59 +000070
Walter Dörwald37c47282003-02-26 14:49:41 +000071 def setUp(self):
72 # In case unicodedata is not available, this will raise an ImportError,
73 # but the other test cases will still be run
74 import unicodedata
75 self.db = unicodedata
Guido van Rossum24bdb042000-03-28 20:29:59 +000076
Walter Dörwald37c47282003-02-26 14:49:41 +000077 def tearDown(self):
78 del self.db
Guido van Rossum24bdb042000-03-28 20:29:59 +000079
Walter Dörwald37c47282003-02-26 14:49:41 +000080class UnicodeFunctionsTest(UnicodeDatabaseTest):
Guido van Rossum24bdb042000-03-28 20:29:59 +000081
Walter Dörwald37c47282003-02-26 14:49:41 +000082 # update this, if the database changes
Mark Dickinson388122d2010-08-04 20:56:28 +000083 expectedchecksum = 'e89a6380093a00a7685ac7b92e7367d737fcb79b'
Walter Dörwald37c47282003-02-26 14:49:41 +000084 def test_function_checksum(self):
85 data = []
Thomas Wouters477c8d52006-05-27 19:21:47 +000086 h = hashlib.sha1()
Walter Dörwald37c47282003-02-26 14:49:41 +000087
88 for i in range(0x10000):
Guido van Rossum84fc66d2007-05-03 17:18:26 +000089 char = chr(i)
Walter Dörwald37c47282003-02-26 14:49:41 +000090 data = [
91 # Properties
Mark Dickinson388122d2010-08-04 20:56:28 +000092 format(self.db.digit(char, -1), '.12g'),
93 format(self.db.numeric(char, -1), '.12g'),
94 format(self.db.decimal(char, -1), '.12g'),
Walter Dörwald37c47282003-02-26 14:49:41 +000095 self.db.category(char),
96 self.db.bidirectional(char),
97 self.db.decomposition(char),
98 str(self.db.mirrored(char)),
99 str(self.db.combining(char)),
100 ]
Guido van Rossum9c627722007-08-27 18:31:48 +0000101 h.update(''.join(data).encode("ascii"))
Walter Dörwald37c47282003-02-26 14:49:41 +0000102 result = h.hexdigest()
103 self.assertEqual(result, self.expectedchecksum)
104
105 def test_digit(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000106 self.assertEqual(self.db.digit('A', None), None)
107 self.assertEqual(self.db.digit('9'), 9)
108 self.assertEqual(self.db.digit('\u215b', None), None)
109 self.assertEqual(self.db.digit('\u2468'), 9)
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000110 self.assertEqual(self.db.digit('\U00020000', None), None)
Walter Dörwald37c47282003-02-26 14:49:41 +0000111
112 self.assertRaises(TypeError, self.db.digit)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000113 self.assertRaises(TypeError, self.db.digit, 'xx')
114 self.assertRaises(ValueError, self.db.digit, 'x')
Walter Dörwald37c47282003-02-26 14:49:41 +0000115
116 def test_numeric(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000117 self.assertEqual(self.db.numeric('A',None), None)
118 self.assertEqual(self.db.numeric('9'), 9)
119 self.assertEqual(self.db.numeric('\u215b'), 0.125)
120 self.assertEqual(self.db.numeric('\u2468'), 9.0)
Amaury Forgeot d'Arc7d520792009-10-06 21:03:20 +0000121 self.assertEqual(self.db.numeric('\ua627'), 7.0)
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000122 self.assertEqual(self.db.numeric('\U00020000', None), None)
Walter Dörwald37c47282003-02-26 14:49:41 +0000123
124 self.assertRaises(TypeError, self.db.numeric)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000125 self.assertRaises(TypeError, self.db.numeric, 'xx')
126 self.assertRaises(ValueError, self.db.numeric, 'x')
Walter Dörwald37c47282003-02-26 14:49:41 +0000127
128 def test_decimal(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000129 self.assertEqual(self.db.decimal('A',None), None)
130 self.assertEqual(self.db.decimal('9'), 9)
131 self.assertEqual(self.db.decimal('\u215b', None), None)
132 self.assertEqual(self.db.decimal('\u2468', None), None)
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000133 self.assertEqual(self.db.decimal('\U00020000', None), None)
Walter Dörwald37c47282003-02-26 14:49:41 +0000134
135 self.assertRaises(TypeError, self.db.decimal)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000136 self.assertRaises(TypeError, self.db.decimal, 'xx')
137 self.assertRaises(ValueError, self.db.decimal, 'x')
Walter Dörwald37c47282003-02-26 14:49:41 +0000138
139 def test_category(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000140 self.assertEqual(self.db.category('\uFFFE'), 'Cn')
141 self.assertEqual(self.db.category('a'), 'Ll')
142 self.assertEqual(self.db.category('A'), 'Lu')
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000143 self.assertEqual(self.db.category('\U00020000'), 'Lo')
Walter Dörwald37c47282003-02-26 14:49:41 +0000144
145 self.assertRaises(TypeError, self.db.category)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000146 self.assertRaises(TypeError, self.db.category, 'xx')
Walter Dörwald37c47282003-02-26 14:49:41 +0000147
148 def test_bidirectional(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000149 self.assertEqual(self.db.bidirectional('\uFFFE'), '')
150 self.assertEqual(self.db.bidirectional(' '), 'WS')
151 self.assertEqual(self.db.bidirectional('A'), 'L')
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000152 self.assertEqual(self.db.bidirectional('\U00020000'), 'L')
Walter Dörwald37c47282003-02-26 14:49:41 +0000153
154 self.assertRaises(TypeError, self.db.bidirectional)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000155 self.assertRaises(TypeError, self.db.bidirectional, 'xx')
Walter Dörwald37c47282003-02-26 14:49:41 +0000156
157 def test_decomposition(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000158 self.assertEqual(self.db.decomposition('\uFFFE'),'')
159 self.assertEqual(self.db.decomposition('\u00bc'), '<fraction> 0031 2044 0034')
Walter Dörwald37c47282003-02-26 14:49:41 +0000160
161 self.assertRaises(TypeError, self.db.decomposition)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000162 self.assertRaises(TypeError, self.db.decomposition, 'xx')
Walter Dörwald37c47282003-02-26 14:49:41 +0000163
164 def test_mirrored(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000165 self.assertEqual(self.db.mirrored('\uFFFE'), 0)
166 self.assertEqual(self.db.mirrored('a'), 0)
167 self.assertEqual(self.db.mirrored('\u2201'), 1)
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000168 self.assertEqual(self.db.mirrored('\U00020000'), 0)
Walter Dörwald37c47282003-02-26 14:49:41 +0000169
170 self.assertRaises(TypeError, self.db.mirrored)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000171 self.assertRaises(TypeError, self.db.mirrored, 'xx')
Walter Dörwald37c47282003-02-26 14:49:41 +0000172
173 def test_combining(self):
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000174 self.assertEqual(self.db.combining('\uFFFE'), 0)
175 self.assertEqual(self.db.combining('a'), 0)
176 self.assertEqual(self.db.combining('\u20e1'), 230)
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000177 self.assertEqual(self.db.combining('\U00020000'), 0)
Walter Dörwald37c47282003-02-26 14:49:41 +0000178
179 self.assertRaises(TypeError, self.db.combining)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000180 self.assertRaises(TypeError, self.db.combining, 'xx')
Walter Dörwald37c47282003-02-26 14:49:41 +0000181
182 def test_normalize(self):
183 self.assertRaises(TypeError, self.db.normalize)
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000184 self.assertRaises(ValueError, self.db.normalize, 'unknown', 'xx')
185 self.assertEqual(self.db.normalize('NFKC', ''), '')
Walter Dörwald37c47282003-02-26 14:49:41 +0000186 # The rest can be found in test_normalization.py
187 # which requires an external file.
188
Victor Stinner7ed9c4c2010-03-04 12:14:57 +0000189 def test_pr29(self):
190 # http://www.unicode.org/review/pr-29.html
Victor Stinner931bb022010-03-04 12:47:32 +0000191 for text in ("\u0b47\u0300\u0b3e", "\u1100\u0300\u1161"):
Victor Stinner7ed9c4c2010-03-04 12:14:57 +0000192 self.assertEqual(self.db.normalize('NFC', text), text)
193
Hye-Shik Change9ddfbb2004-08-04 07:38:35 +0000194 def test_east_asian_width(self):
195 eaw = self.db.east_asian_width
Guido van Rossum98297ee2007-11-06 21:34:58 +0000196 self.assertRaises(TypeError, eaw, b'a')
Guido van Rossum254348e2007-11-21 19:29:53 +0000197 self.assertRaises(TypeError, eaw, bytearray())
Guido van Rossumef87d6e2007-05-02 19:09:54 +0000198 self.assertRaises(TypeError, eaw, '')
199 self.assertRaises(TypeError, eaw, 'ra')
200 self.assertEqual(eaw('\x1e'), 'N')
201 self.assertEqual(eaw('\x20'), 'Na')
202 self.assertEqual(eaw('\uC894'), 'W')
203 self.assertEqual(eaw('\uFF66'), 'H')
204 self.assertEqual(eaw('\uFF1F'), 'F')
205 self.assertEqual(eaw('\u2010'), 'A')
Walter Dörwaldf342bfc2008-06-03 11:45:02 +0000206 self.assertEqual(eaw('\U00020000'), 'W')
Walter Dörwald37c47282003-02-26 14:49:41 +0000207
208class UnicodeMiscTest(UnicodeDatabaseTest):
209
Benjamin Peterson6f7fad12008-11-21 22:58:57 +0000210 def test_failed_import_during_compiling(self):
211 # Issue 4367
212 # Decoding \N escapes requires the unicodedata module. If it can't be
213 # imported, we shouldn't segfault.
214
215 # This program should raise a SyntaxError in the eval.
216 code = "import sys;" \
217 "sys.modules['unicodedata'] = None;" \
218 """eval("'\\\\N{SOFT HYPHEN}'")"""
219 args = [sys.executable, "-c", code]
220 # We use a subprocess because the unicodedata module may already have
221 # been loaded in this process.
222 popen = subprocess.Popen(args, stderr=subprocess.PIPE)
223 popen.wait()
224 self.assertEqual(popen.returncode, 1)
225 error = "SyntaxError: (unicode error) \\N escapes not supported " \
226 "(can't load unicodedata module)"
Benjamin Peterson577473f2010-01-19 00:09:57 +0000227 self.assertIn(error, popen.stderr.read().decode("ascii"))
Benjamin Peterson6f7fad12008-11-21 22:58:57 +0000228
Walter Dörwald37c47282003-02-26 14:49:41 +0000229 def test_decimal_numeric_consistent(self):
230 # Test that decimal and numeric are consistent,
231 # i.e. if a character has a decimal value,
Georg Brandl7eb4b7d2005-07-22 21:49:32 +0000232 # its numeric value should be the same.
Walter Dörwald37c47282003-02-26 14:49:41 +0000233 count = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000234 for i in range(0x10000):
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000235 c = chr(i)
Walter Dörwald37c47282003-02-26 14:49:41 +0000236 dec = self.db.decimal(c, -1)
237 if dec != -1:
238 self.assertEqual(dec, self.db.numeric(c))
239 count += 1
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000240 self.assertTrue(count >= 10) # should have tested at least the ASCII digits
Walter Dörwald37c47282003-02-26 14:49:41 +0000241
242 def test_digit_numeric_consistent(self):
243 # Test that digit and numeric are consistent,
Tim Peters669454e2003-03-07 17:30:48 +0000244 # i.e. if a character has a digit value,
Georg Brandl7eb4b7d2005-07-22 21:49:32 +0000245 # its numeric value should be the same.
Walter Dörwald37c47282003-02-26 14:49:41 +0000246 count = 0
Guido van Rossum805365e2007-05-07 22:24:25 +0000247 for i in range(0x10000):
Guido van Rossum84fc66d2007-05-03 17:18:26 +0000248 c = chr(i)
Walter Dörwald37c47282003-02-26 14:49:41 +0000249 dec = self.db.digit(c, -1)
250 if dec != -1:
251 self.assertEqual(dec, self.db.numeric(c))
252 count += 1
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000253 self.assertTrue(count >= 10) # should have tested at least the ASCII digits
Walter Dörwald37c47282003-02-26 14:49:41 +0000254
Guido van Rossum806c2462007-08-06 23:33:07 +0000255 def test_bug_1704793(self):
256 self.assertEquals(self.db.lookup("GOTHIC LETTER FAIHU"), '\U00010346')
257
Martin v. Löwis93cbca32008-09-10 14:08:48 +0000258 def test_ucd_510(self):
259 import unicodedata
260 # In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000261 self.assertTrue(unicodedata.mirrored("\u0f3a"))
262 self.assertTrue(not unicodedata.ucd_3_2_0.mirrored("\u0f3a"))
Martin v. Löwis93cbca32008-09-10 14:08:48 +0000263 # Also, we now have two ways of representing
264 # the upper-case mapping: as delta, or as absolute value
Benjamin Petersonc9c0f202009-06-30 23:06:06 +0000265 self.assertTrue("a".upper()=='A')
266 self.assertTrue("\u1d79".upper()=='\ua77d')
267 self.assertTrue(".".upper()=='.')
Walter Dörwald1b08b302009-04-25 14:13:56 +0000268
269 def test_bug_5828(self):
270 self.assertEqual("\u1d79".lower(), "\u1d79")
271 # Only U+0000 should have U+0000 as its upper/lower/titlecase variant
272 self.assertEqual(
273 [
274 c for c in range(sys.maxunicode+1)
275 if "\x00" in chr(c).lower()+chr(c).upper()+chr(c).title()
276 ],
277 [0]
278 )
279
Walter Dörwalde2507752009-04-26 19:16:11 +0000280 def test_bug_4971(self):
Martin v. Löwis71efeb72009-04-26 01:02:07 +0000281 # LETTER DZ WITH CARON: DZ, Dz, dz
282 self.assertEqual("\u01c4".title(), "\u01c5")
283 self.assertEqual("\u01c5".title(), "\u01c5")
284 self.assertEqual("\u01c6".title(), "\u01c5")
Martin v. Löwis93cbca32008-09-10 14:08:48 +0000285
Florent Xicluna806d8cf2010-03-30 19:34:18 +0000286 def test_linebreak_7643(self):
287 for i in range(0x10000):
288 lines = (chr(i) + 'A').splitlines()
289 if i in (0x0a, 0x0b, 0x0c, 0x0d, 0x85,
290 0x1c, 0x1d, 0x1e, 0x2028, 0x2029):
291 self.assertEqual(len(lines), 2,
292 r"\u%.4x should be a linebreak" % i)
293 else:
294 self.assertEqual(len(lines), 1,
295 r"\u%.4x should not be a linebreak" % i)
296
Amaury Forgeot d'Arc324ac652010-08-18 20:44:58 +0000297 def test_UCS4(self):
298 # unicodedata should work with code points outside the BMP
299 # even on a narrow Unicode build
Amaury Forgeot d'Arc56ab01b2010-08-18 21:12:52 +0000300 self.assertEqual(self.db.category("\U0001012A"), "No")
301 self.assertEqual(self.db.numeric("\U0001012A"), 9000)
Amaury Forgeot d'Arc7e44b6b2010-08-18 22:07:15 +0000302 self.assertEqual(self.db.decimal("\U0001D7FD"), 7)
303 self.assertEqual(self.db.digit("\U0001D7FD"), 7)
Amaury Forgeot d'Arc324ac652010-08-18 20:44:58 +0000304
Walter Dörwald37c47282003-02-26 14:49:41 +0000305def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000306 test.support.run_unittest(
Walter Dörwald21d3a322003-05-01 17:45:56 +0000307 UnicodeMiscTest,
308 UnicodeMethodsTest,
309 UnicodeFunctionsTest
310 )
Walter Dörwald37c47282003-02-26 14:49:41 +0000311
312if __name__ == "__main__":
313 test_main()