Updated for 2.1a3
[python/dscho.git] / Lib / test / test_unicodedata.py
blobdb03d35708e18f58d601a61a188d6a1c1f49819b
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 from test_support import verify, verbose
9 import sha
11 encoding = 'utf-8'
13 def test_methods():
15 h = sha.sha()
16 for i in range(65536):
17 char = unichr(i)
18 data = [
20 # Predicates (single char)
21 char.isalnum() and u'1' or u'0',
22 char.isalpha() and u'1' or u'0',
23 char.isdecimal() and u'1' or u'0',
24 char.isdigit() and u'1' or u'0',
25 char.islower() and u'1' or u'0',
26 char.isnumeric() and u'1' or u'0',
27 char.isspace() and u'1' or u'0',
28 char.istitle() and u'1' or u'0',
29 char.isupper() and u'1' or u'0',
31 # Predicates (multiple chars)
32 (char + u'abc').isalnum() and u'1' or u'0',
33 (char + u'abc').isalpha() and u'1' or u'0',
34 (char + u'123').isdecimal() and u'1' or u'0',
35 (char + u'123').isdigit() and u'1' or u'0',
36 (char + u'abc').islower() and u'1' or u'0',
37 (char + u'123').isnumeric() and u'1' or u'0',
38 (char + u' \t').isspace() and u'1' or u'0',
39 (char + u'abc').istitle() and u'1' or u'0',
40 (char + u'ABC').isupper() and u'1' or u'0',
42 # Mappings (single char)
43 char.lower(),
44 char.upper(),
45 char.title(),
47 # Mappings (multiple chars)
48 (char + u'abc').lower(),
49 (char + u'ABC').upper(),
50 (char + u'abc').title(),
51 (char + u'ABC').title(),
54 h.update(u''.join(data).encode(encoding))
55 return h.hexdigest()
57 def test_unicodedata():
59 h = sha.sha()
60 for i in range(65536):
61 char = unichr(i)
62 data = [
63 # Properties
64 str(unicodedata.digit(char, -1)),
65 str(unicodedata.numeric(char, -1)),
66 str(unicodedata.decimal(char, -1)),
67 unicodedata.category(char),
68 unicodedata.bidirectional(char),
69 unicodedata.decomposition(char),
70 str(unicodedata.mirrored(char)),
71 str(unicodedata.combining(char)),
73 h.update(''.join(data))
74 return h.hexdigest()
76 ### Run tests
78 print 'Testing Unicode Database...'
79 print 'Methods:',
80 print test_methods()
82 # In case unicodedata is not available, this will raise an ImportError,
83 # but still test the above cases...
84 import unicodedata
85 print 'Functions:',
86 print test_unicodedata()
88 # Some additional checks of the API:
89 print 'API:',
91 verify(unicodedata.digit(u'A',None) is None)
92 verify(unicodedata.digit(u'9') == 9)
93 verify(unicodedata.digit(u'\u215b',None) is None)
94 verify(unicodedata.digit(u'\u2468') == 9)
96 verify(unicodedata.numeric(u'A',None) is None)
97 verify(unicodedata.numeric(u'9') == 9)
98 verify(unicodedata.numeric(u'\u215b') == 0.125)
99 verify(unicodedata.numeric(u'\u2468') == 9.0)
101 verify(unicodedata.decimal(u'A',None) is None)
102 verify(unicodedata.decimal(u'9') == 9)
103 verify(unicodedata.decimal(u'\u215b',None) is None)
104 verify(unicodedata.decimal(u'\u2468',None) is None)
106 verify(unicodedata.category(u'\uFFFE') == 'Cn')
107 verify(unicodedata.category(u'a') == 'Ll')
108 verify(unicodedata.category(u'A') == 'Lu')
110 verify(unicodedata.bidirectional(u'\uFFFE') == '')
111 verify(unicodedata.bidirectional(u' ') == 'WS')
112 verify(unicodedata.bidirectional(u'A') == 'L')
114 verify(unicodedata.decomposition(u'\uFFFE') == '')
115 verify(unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034')
117 verify(unicodedata.mirrored(u'\uFFFE') == 0)
118 verify(unicodedata.mirrored(u'a') == 0)
119 verify(unicodedata.mirrored(u'\u2201') == 1)
121 verify(unicodedata.combining(u'\uFFFE') == 0)
122 verify(unicodedata.combining(u'a') == 0)
123 verify(unicodedata.combining(u'\u20e1') == 230)
125 print 'ok'