Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 1 | """ Test script for the unicodedata module. |
| 2 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 3 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 4 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 5 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 6 | |
| 7 | """#" |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 8 | import sha |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 9 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 10 | def 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 | |
| 54 | def 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 | |
| 75 | print 'Testing Unicode Database...' |
| 76 | print 'Methods:', |
| 77 | print test_methods() |
| 78 | |
| 79 | # In case unicodedata is not available, this will raise an ImportError, |
| 80 | # but still test the above cases... |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 81 | import unicodedata |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 82 | print 'Functions:', |
| 83 | print test_unicodedata() |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 84 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 85 | # Some additional checks of the API: |
| 86 | print 'API:', |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 87 | |
| 88 | assert unicodedata.digit(u'A',None) is None |
| 89 | assert unicodedata.digit(u'9') == 9 |
| 90 | assert unicodedata.digit(u'\u215b',None) is None |
| 91 | assert unicodedata.digit(u'\u2468') == 9 |
| 92 | |
| 93 | assert unicodedata.numeric(u'A',None) is None |
| 94 | assert unicodedata.numeric(u'9') == 9 |
| 95 | assert unicodedata.numeric(u'\u215b') == 0.125 |
| 96 | assert unicodedata.numeric(u'\u2468') == 9.0 |
| 97 | |
| 98 | assert unicodedata.decimal(u'A',None) is None |
| 99 | assert unicodedata.decimal(u'9') == 9 |
| 100 | assert unicodedata.decimal(u'\u215b',None) is None |
| 101 | assert unicodedata.decimal(u'\u2468',None) is None |
| 102 | |
| 103 | assert unicodedata.category(u'\uFFFE') == 'Cn' |
| 104 | assert unicodedata.category(u'a') == 'Ll' |
| 105 | assert unicodedata.category(u'A') == 'Lu' |
| 106 | |
| 107 | assert unicodedata.bidirectional(u'\uFFFE') == '' |
| 108 | assert unicodedata.bidirectional(u' ') == 'WS' |
| 109 | assert unicodedata.bidirectional(u'A') == 'L' |
| 110 | |
| 111 | assert unicodedata.decomposition(u'\uFFFE') == '' |
| 112 | assert unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034' |
| 113 | |
| 114 | assert unicodedata.mirrored(u'\uFFFE') == 0 |
| 115 | assert unicodedata.mirrored(u'a') == 0 |
| 116 | assert unicodedata.mirrored(u'\u2201') == 1 |
| 117 | |
| 118 | assert unicodedata.combining(u'\uFFFE') == 0 |
| 119 | assert unicodedata.combining(u'a') == 0 |
| 120 | assert unicodedata.combining(u'\u20e1') == 230 |
| 121 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame^] | 122 | print 'ok' |