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 | 67ceca7 | 2000-09-27 12:24:34 +0000 | [diff] [blame] | 10 | encoding = 'utf-8' |
| 11 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 12 | def test_methods(): |
| 13 | |
| 14 | h = sha.sha() |
| 15 | for i in range(65536): |
| 16 | char = unichr(i) |
| 17 | data = [ |
| 18 | |
| 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', |
| 29 | |
| 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', |
| 40 | |
| 41 | # Mappings (single char) |
| 42 | char.lower(), |
| 43 | char.upper(), |
| 44 | char.title(), |
| 45 | |
| 46 | # Mappings (multiple chars) |
| 47 | (char + u'abc').lower(), |
| 48 | (char + u'ABC').upper(), |
| 49 | (char + u'abc').title(), |
| 50 | (char + u'ABC').title(), |
| 51 | |
| 52 | ] |
Marc-André Lemburg | 67ceca7 | 2000-09-27 12:24:34 +0000 | [diff] [blame] | 53 | h.update(u''.join(data).encode(encoding)) |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 54 | return h.hexdigest() |
| 55 | |
| 56 | def test_unicodedata(): |
| 57 | |
| 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)), |
| 71 | ] |
| 72 | h.update(''.join(data)) |
| 73 | return h.hexdigest() |
| 74 | |
| 75 | ### Run tests |
| 76 | |
| 77 | print 'Testing Unicode Database...' |
| 78 | print 'Methods:', |
| 79 | print test_methods() |
| 80 | |
| 81 | # In case unicodedata is not available, this will raise an ImportError, |
| 82 | # but still test the above cases... |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 83 | import unicodedata |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 84 | print 'Functions:', |
| 85 | print test_unicodedata() |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 86 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 87 | # Some additional checks of the API: |
| 88 | print 'API:', |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 89 | |
| 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 |
| 94 | |
| 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 |
| 99 | |
| 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 |
| 104 | |
| 105 | assert unicodedata.category(u'\uFFFE') == 'Cn' |
| 106 | assert unicodedata.category(u'a') == 'Ll' |
| 107 | assert unicodedata.category(u'A') == 'Lu' |
| 108 | |
| 109 | assert unicodedata.bidirectional(u'\uFFFE') == '' |
| 110 | assert unicodedata.bidirectional(u' ') == 'WS' |
| 111 | assert unicodedata.bidirectional(u'A') == 'L' |
| 112 | |
| 113 | assert unicodedata.decomposition(u'\uFFFE') == '' |
| 114 | assert unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034' |
| 115 | |
| 116 | assert unicodedata.mirrored(u'\uFFFE') == 0 |
| 117 | assert unicodedata.mirrored(u'a') == 0 |
| 118 | assert unicodedata.mirrored(u'\u2201') == 1 |
| 119 | |
| 120 | assert unicodedata.combining(u'\uFFFE') == 0 |
| 121 | assert unicodedata.combining(u'a') == 0 |
| 122 | assert unicodedata.combining(u'\u20e1') == 230 |
| 123 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 124 | print 'ok' |