struct.pack has become picky about h (short) and H (unsigned short).
[python/dscho.git] / Lib / test / test_unicodedata.py
blob0dc575615e4d6ac20139863ad8687ffdf0c97ff9
1 """ Test script for the unicodedata module.
3 Written by Marc-Andre Lemburg (mal@lemburg.com).
5 (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
7 """#"
8 import sha
10 encoding = 'utf-8'
12 def test_methods():
14 h = sha.sha()
15 for i in range(65536):
16 char = unichr(i)
17 data = [
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',
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',
41 # Mappings (single char)
42 char.lower(),
43 char.upper(),
44 char.title(),
46 # Mappings (multiple chars)
47 (char + u'abc').lower(),
48 (char + u'ABC').upper(),
49 (char + u'abc').title(),
50 (char + u'ABC').title(),
53 h.update(u''.join(data).encode(encoding))
54 return h.hexdigest()
56 def test_unicodedata():
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)),
72 h.update(''.join(data))
73 return h.hexdigest()
75 ### Run tests
77 print 'Testing Unicode Database...'
78 print 'Methods:',
79 print test_methods()
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()
87 # Some additional checks of the API:
88 print 'API:',
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
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
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
105 assert unicodedata.category(u'\uFFFE') == 'Cn'
106 assert unicodedata.category(u'a') == 'Ll'
107 assert unicodedata.category(u'A') == 'Lu'
109 assert unicodedata.bidirectional(u'\uFFFE') == ''
110 assert unicodedata.bidirectional(u' ') == 'WS'
111 assert unicodedata.bidirectional(u'A') == 'L'
113 assert unicodedata.decomposition(u'\uFFFE') == ''
114 assert unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034'
116 assert unicodedata.mirrored(u'\uFFFE') == 0
117 assert unicodedata.mirrored(u'a') == 0
118 assert unicodedata.mirrored(u'\u2201') == 1
120 assert unicodedata.combining(u'\uFFFE') == 0
121 assert unicodedata.combining(u'a') == 0
122 assert unicodedata.combining(u'\u20e1') == 230
124 print 'ok'