test_unicodedata.py 12 KB

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