PageRenderTime 170ms CodeModel.GetById 26ms RepoModel.GetById 10ms app.codeStats 0ms

/Lib/test/test_unicodedata.py

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