test_unicodedata.py 3.77 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2
""" Test script for the unicodedata module.

3
    Written by Marc-Andre Lemburg (mal@lemburg.com).
Guido van Rossum's avatar
Guido van Rossum committed
4

5
    (c) Copyright CNRI, All Rights Reserved. NO WARRANTY.
Guido van Rossum's avatar
Guido van Rossum committed
6 7

"""#"
8
from test_support import verify, verbose
9
import sha
Guido van Rossum's avatar
Guido van Rossum committed
10

11 12
encoding = 'utf-8'

13 14 15 16 17 18
def test_methods():

    h = sha.sha()
    for i in range(65536):
        char = unichr(i)
        data = [
19

20 21 22 23 24 25 26 27 28 29
            # Predicates (single char)
            char.isalnum() and u'1' or u'0',
            char.isalpha() and u'1' or u'0',
            char.isdecimal() and u'1' or u'0',
            char.isdigit() and u'1' or u'0',
            char.islower() and u'1' or u'0',
            char.isnumeric() and u'1' or u'0',
            char.isspace() and u'1' or u'0',
            char.istitle() and u'1' or u'0',
            char.isupper() and u'1' or u'0',
30

31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
            # Predicates (multiple chars)
            (char + u'abc').isalnum() and u'1' or u'0',
            (char + u'abc').isalpha() and u'1' or u'0',
            (char + u'123').isdecimal() and u'1' or u'0',
            (char + u'123').isdigit() and u'1' or u'0',
            (char + u'abc').islower() and u'1' or u'0',
            (char + u'123').isnumeric() and u'1' or u'0',
            (char + u' \t').isspace() and u'1' or u'0',
            (char + u'abc').istitle() and u'1' or u'0',
            (char + u'ABC').isupper() and u'1' or u'0',

            # Mappings (single char)
            char.lower(),
            char.upper(),
            char.title(),
46

47 48 49 50 51
            # Mappings (multiple chars)
            (char + u'abc').lower(),
            (char + u'ABC').upper(),
            (char + u'abc').title(),
            (char + u'ABC').title(),
52

53
            ]
54
        h.update(u''.join(data).encode(encoding))
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    return h.hexdigest()

def test_unicodedata():

    h = sha.sha()
    for i in range(65536):
        char = unichr(i)
        data = [
            # Properties
            str(unicodedata.digit(char, -1)),
            str(unicodedata.numeric(char, -1)),
            str(unicodedata.decimal(char, -1)),
            unicodedata.category(char),
            unicodedata.bidirectional(char),
            unicodedata.decomposition(char),
            str(unicodedata.mirrored(char)),
            str(unicodedata.combining(char)),
72
            ]
73 74 75 76 77 78 79 80 81 82 83
        h.update(''.join(data))
    return h.hexdigest()

### Run tests

print 'Testing Unicode Database...'
print 'Methods:',
print test_methods()

# In case unicodedata is not available, this will raise an ImportError,
# but still test the above cases...
Guido van Rossum's avatar
Guido van Rossum committed
84
import unicodedata
85 86
print 'Functions:',
print test_unicodedata()
Guido van Rossum's avatar
Guido van Rossum committed
87

88 89
# Some additional checks of the API:
print 'API:',
Guido van Rossum's avatar
Guido van Rossum committed
90

91 92 93 94
verify(unicodedata.digit(u'A',None) is None)
verify(unicodedata.digit(u'9') == 9)
verify(unicodedata.digit(u'\u215b',None) is None)
verify(unicodedata.digit(u'\u2468') == 9)
Guido van Rossum's avatar
Guido van Rossum committed
95

96 97 98 99
verify(unicodedata.numeric(u'A',None) is None)
verify(unicodedata.numeric(u'9') == 9)
verify(unicodedata.numeric(u'\u215b') == 0.125)
verify(unicodedata.numeric(u'\u2468') == 9.0)
Guido van Rossum's avatar
Guido van Rossum committed
100

101 102 103 104
verify(unicodedata.decimal(u'A',None) is None)
verify(unicodedata.decimal(u'9') == 9)
verify(unicodedata.decimal(u'\u215b',None) is None)
verify(unicodedata.decimal(u'\u2468',None) is None)
Guido van Rossum's avatar
Guido van Rossum committed
105

106 107 108
verify(unicodedata.category(u'\uFFFE') == 'Cn')
verify(unicodedata.category(u'a') == 'Ll')
verify(unicodedata.category(u'A') == 'Lu')
Guido van Rossum's avatar
Guido van Rossum committed
109

110 111 112
verify(unicodedata.bidirectional(u'\uFFFE') == '')
verify(unicodedata.bidirectional(u' ') == 'WS')
verify(unicodedata.bidirectional(u'A') == 'L')
Guido van Rossum's avatar
Guido van Rossum committed
113

114 115
verify(unicodedata.decomposition(u'\uFFFE') == '')
verify(unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034')
Guido van Rossum's avatar
Guido van Rossum committed
116

117 118 119
verify(unicodedata.mirrored(u'\uFFFE') == 0)
verify(unicodedata.mirrored(u'a') == 0)
verify(unicodedata.mirrored(u'\u2201') == 1)
Guido van Rossum's avatar
Guido van Rossum committed
120

121 122 123
verify(unicodedata.combining(u'\uFFFE') == 0)
verify(unicodedata.combining(u'a') == 0)
verify(unicodedata.combining(u'\u20e1') == 230)
Guido van Rossum's avatar
Guido van Rossum committed
124

125
print 'ok'