home *** CD-ROM | disk | FTP | other *** search
/ PC World 2001 April / PCWorld_2001-04_cd.bin / Software / TemaCD / webclean / !!!python!!! / BeOpen-Python-2.0.exe / TEST_UNICODEDATA.PY < prev    next >
Encoding:
Python Source  |  2000-09-28  |  3.9 KB  |  125 lines

  1. """ Test script for the unicodedata module.
  2.  
  3.     Written by Marc-Andre Lemburg (mal@lemburg.com).
  4.  
  5.     (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
  6.  
  7. """#"
  8. import sha
  9.  
  10. encoding = 'utf-8'
  11.  
  12. def test_methods():
  13.  
  14.     h = sha.sha()
  15.     for i in range(65536):
  16.         char = unichr(i)
  17.         data = [
  18.             
  19.             # Predicates (single char)
  20.             char.isalnum() and u'1' or u'0',
  21.             char.isalpha() and u'1' or u'0',
  22.             char.isdecimal() and u'1' or u'0',
  23.             char.isdigit() and u'1' or u'0',
  24.             char.islower() and u'1' or u'0',
  25.             char.isnumeric() and u'1' or u'0',
  26.             char.isspace() and u'1' or u'0',
  27.             char.istitle() and u'1' or u'0',
  28.             char.isupper() and u'1' or u'0',
  29.             
  30.             # Predicates (multiple chars)
  31.             (char + u'abc').isalnum() and u'1' or u'0',
  32.             (char + u'abc').isalpha() and u'1' or u'0',
  33.             (char + u'123').isdecimal() and u'1' or u'0',
  34.             (char + u'123').isdigit() and u'1' or u'0',
  35.             (char + u'abc').islower() and u'1' or u'0',
  36.             (char + u'123').isnumeric() and u'1' or u'0',
  37.             (char + u' \t').isspace() and u'1' or u'0',
  38.             (char + u'abc').istitle() and u'1' or u'0',
  39.             (char + u'ABC').isupper() and u'1' or u'0',
  40.  
  41.             # Mappings (single char)
  42.             char.lower(),
  43.             char.upper(),
  44.             char.title(),
  45.             
  46.             # Mappings (multiple chars)
  47.             (char + u'abc').lower(),
  48.             (char + u'ABC').upper(),
  49.             (char + u'abc').title(),
  50.             (char + u'ABC').title(),
  51.             
  52.             ]
  53.         h.update(u''.join(data).encode(encoding))
  54.     return h.hexdigest()
  55.  
  56. def test_unicodedata():
  57.  
  58.     h = sha.sha()
  59.     for i in range(65536):
  60.         char = unichr(i)
  61.         data = [
  62.             # Properties
  63.             str(unicodedata.digit(char, -1)),
  64.             str(unicodedata.numeric(char, -1)),
  65.             str(unicodedata.decimal(char, -1)),
  66.             unicodedata.category(char),
  67.             unicodedata.bidirectional(char),
  68.             unicodedata.decomposition(char),
  69.             str(unicodedata.mirrored(char)),
  70.             str(unicodedata.combining(char)),
  71.             ] 
  72.         h.update(''.join(data))
  73.     return h.hexdigest()
  74.  
  75. ### Run tests
  76.  
  77. print 'Testing Unicode Database...'
  78. print 'Methods:',
  79. print test_methods()
  80.  
  81. # In case unicodedata is not available, this will raise an ImportError,
  82. # but still test the above cases...
  83. import unicodedata
  84. print 'Functions:',
  85. print test_unicodedata()
  86.  
  87. # Some additional checks of the API:
  88. print 'API:',
  89.  
  90. assert unicodedata.digit(u'A',None) is None
  91. assert unicodedata.digit(u'9') == 9
  92. assert unicodedata.digit(u'\u215b',None) is None
  93. assert unicodedata.digit(u'\u2468') == 9
  94.  
  95. assert unicodedata.numeric(u'A',None) is None
  96. assert unicodedata.numeric(u'9') == 9
  97. assert unicodedata.numeric(u'\u215b') == 0.125
  98. assert unicodedata.numeric(u'\u2468') == 9.0
  99.  
  100. assert unicodedata.decimal(u'A',None) is None
  101. assert unicodedata.decimal(u'9') == 9
  102. assert unicodedata.decimal(u'\u215b',None) is None
  103. assert unicodedata.decimal(u'\u2468',None) is None
  104.  
  105. assert unicodedata.category(u'\uFFFE') == 'Cn'
  106. assert unicodedata.category(u'a') == 'Ll'
  107. assert unicodedata.category(u'A') == 'Lu'
  108.  
  109. assert unicodedata.bidirectional(u'\uFFFE') == ''
  110. assert unicodedata.bidirectional(u' ') == 'WS'
  111. assert unicodedata.bidirectional(u'A') == 'L'
  112.  
  113. assert unicodedata.decomposition(u'\uFFFE') == ''
  114. assert unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034'
  115.  
  116. assert unicodedata.mirrored(u'\uFFFE') == 0
  117. assert unicodedata.mirrored(u'a') == 0
  118. assert unicodedata.mirrored(u'\u2201') == 1
  119.  
  120. assert unicodedata.combining(u'\uFFFE') == 0
  121. assert unicodedata.combining(u'a') == 0
  122. assert unicodedata.combining(u'\u20e1') == 230
  123.  
  124. print 'ok'
  125.