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