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 | |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 7 | """ |
| 8 | |
| 9 | import sys |
| 10 | import unittest |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 11 | import hashlib |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 12 | import subprocess |
| 13 | import test.support |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 14 | |
Marc-André Lemburg | 67ceca7 | 2000-09-27 12:24:34 +0000 | [diff] [blame] | 15 | encoding = 'utf-8' |
Martin v. Löwis | 54d9d07 | 2009-05-10 09:33:21 +0000 | [diff] [blame] | 16 | errors = 'surrogatepass' |
Marc-André Lemburg | 67ceca7 | 2000-09-27 12:24:34 +0000 | [diff] [blame] | 17 | |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 18 | |
| 19 | ### Run tests |
| 20 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 21 | class UnicodeMethodsTest(unittest.TestCase): |
Marc-André Lemburg | 6a20ee7 | 2000-09-26 16:18:58 +0000 | [diff] [blame] | 22 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 23 | # update this, if the database changes |
Benjamin Peterson | 71f660e | 2012-02-20 22:24:29 -0500 | [diff] [blame] | 24 | expectedchecksum = 'bf7a78f1a532421b5033600102e23a92044dbba9' |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 25 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 26 | def test_method_checksum(self): |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 27 | h = hashlib.sha1() |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 28 | for i in range(0x10000): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 29 | char = chr(i) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 30 | data = [ |
| 31 | # Predicates (single char) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 32 | "01"[char.isalnum()], |
| 33 | "01"[char.isalpha()], |
| 34 | "01"[char.isdecimal()], |
| 35 | "01"[char.isdigit()], |
| 36 | "01"[char.islower()], |
| 37 | "01"[char.isnumeric()], |
| 38 | "01"[char.isspace()], |
| 39 | "01"[char.istitle()], |
| 40 | "01"[char.isupper()], |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 41 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 42 | # Predicates (multiple chars) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 43 | "01"[(char + 'abc').isalnum()], |
| 44 | "01"[(char + 'abc').isalpha()], |
| 45 | "01"[(char + '123').isdecimal()], |
| 46 | "01"[(char + '123').isdigit()], |
| 47 | "01"[(char + 'abc').islower()], |
| 48 | "01"[(char + '123').isnumeric()], |
| 49 | "01"[(char + ' \t').isspace()], |
| 50 | "01"[(char + 'abc').istitle()], |
| 51 | "01"[(char + 'ABC').isupper()], |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 52 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 53 | # Mappings (single char) |
| 54 | char.lower(), |
| 55 | char.upper(), |
| 56 | char.title(), |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 57 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 58 | # Mappings (multiple chars) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 59 | (char + 'abc').lower(), |
| 60 | (char + 'ABC').upper(), |
| 61 | (char + 'abc').title(), |
| 62 | (char + 'ABC').title(), |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 63 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 64 | ] |
Martin v. Löwis | db12d45 | 2009-05-02 18:52:14 +0000 | [diff] [blame] | 65 | h.update(''.join(data).encode(encoding, errors)) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 66 | result = h.hexdigest() |
| 67 | self.assertEqual(result, self.expectedchecksum) |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 68 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 69 | class UnicodeDatabaseTest(unittest.TestCase): |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 70 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 71 | def setUp(self): |
| 72 | # In case unicodedata is not available, this will raise an ImportError, |
| 73 | # but the other test cases will still be run |
| 74 | import unicodedata |
| 75 | self.db = unicodedata |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 76 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 77 | def tearDown(self): |
| 78 | del self.db |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 79 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 80 | class UnicodeFunctionsTest(UnicodeDatabaseTest): |
Guido van Rossum | 24bdb04 | 2000-03-28 20:29:59 +0000 | [diff] [blame] | 81 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 82 | # update this, if the database changes |
Benjamin Peterson | 71f660e | 2012-02-20 22:24:29 -0500 | [diff] [blame] | 83 | expectedchecksum = '17fe2f12b788e4fff5479b469c4404bb6ecf841f' |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 84 | def test_function_checksum(self): |
| 85 | data = [] |
Thomas Wouters | 477c8d5 | 2006-05-27 19:21:47 +0000 | [diff] [blame] | 86 | h = hashlib.sha1() |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 87 | |
| 88 | for i in range(0x10000): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 89 | char = chr(i) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 90 | data = [ |
| 91 | # Properties |
Mark Dickinson | 388122d | 2010-08-04 20:56:28 +0000 | [diff] [blame] | 92 | format(self.db.digit(char, -1), '.12g'), |
| 93 | format(self.db.numeric(char, -1), '.12g'), |
| 94 | format(self.db.decimal(char, -1), '.12g'), |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 95 | self.db.category(char), |
| 96 | self.db.bidirectional(char), |
| 97 | self.db.decomposition(char), |
| 98 | str(self.db.mirrored(char)), |
| 99 | str(self.db.combining(char)), |
| 100 | ] |
Guido van Rossum | 9c62772 | 2007-08-27 18:31:48 +0000 | [diff] [blame] | 101 | h.update(''.join(data).encode("ascii")) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 102 | result = h.hexdigest() |
| 103 | self.assertEqual(result, self.expectedchecksum) |
| 104 | |
| 105 | def test_digit(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 106 | self.assertEqual(self.db.digit('A', None), None) |
| 107 | self.assertEqual(self.db.digit('9'), 9) |
| 108 | self.assertEqual(self.db.digit('\u215b', None), None) |
| 109 | self.assertEqual(self.db.digit('\u2468'), 9) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 110 | self.assertEqual(self.db.digit('\U00020000', None), None) |
Ezio Melotti | f503673 | 2011-09-29 03:14:56 +0300 | [diff] [blame] | 111 | self.assertEqual(self.db.digit('\U0001D7FD'), 7) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 112 | |
| 113 | self.assertRaises(TypeError, self.db.digit) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 114 | self.assertRaises(TypeError, self.db.digit, 'xx') |
| 115 | self.assertRaises(ValueError, self.db.digit, 'x') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 116 | |
| 117 | def test_numeric(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 118 | self.assertEqual(self.db.numeric('A',None), None) |
| 119 | self.assertEqual(self.db.numeric('9'), 9) |
| 120 | self.assertEqual(self.db.numeric('\u215b'), 0.125) |
| 121 | self.assertEqual(self.db.numeric('\u2468'), 9.0) |
Amaury Forgeot d'Arc | 7d52079 | 2009-10-06 21:03:20 +0000 | [diff] [blame] | 122 | self.assertEqual(self.db.numeric('\ua627'), 7.0) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 123 | self.assertEqual(self.db.numeric('\U00020000', None), None) |
Ezio Melotti | f503673 | 2011-09-29 03:14:56 +0300 | [diff] [blame] | 124 | self.assertEqual(self.db.numeric('\U0001012A'), 9000) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 125 | |
| 126 | self.assertRaises(TypeError, self.db.numeric) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 127 | self.assertRaises(TypeError, self.db.numeric, 'xx') |
| 128 | self.assertRaises(ValueError, self.db.numeric, 'x') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 129 | |
| 130 | def test_decimal(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 131 | self.assertEqual(self.db.decimal('A',None), None) |
| 132 | self.assertEqual(self.db.decimal('9'), 9) |
| 133 | self.assertEqual(self.db.decimal('\u215b', None), None) |
| 134 | self.assertEqual(self.db.decimal('\u2468', None), None) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 135 | self.assertEqual(self.db.decimal('\U00020000', None), None) |
Ezio Melotti | f503673 | 2011-09-29 03:14:56 +0300 | [diff] [blame] | 136 | self.assertEqual(self.db.decimal('\U0001D7FD'), 7) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 137 | |
| 138 | self.assertRaises(TypeError, self.db.decimal) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 139 | self.assertRaises(TypeError, self.db.decimal, 'xx') |
| 140 | self.assertRaises(ValueError, self.db.decimal, 'x') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 141 | |
| 142 | def test_category(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 143 | self.assertEqual(self.db.category('\uFFFE'), 'Cn') |
| 144 | self.assertEqual(self.db.category('a'), 'Ll') |
| 145 | self.assertEqual(self.db.category('A'), 'Lu') |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 146 | self.assertEqual(self.db.category('\U00020000'), 'Lo') |
Ezio Melotti | f503673 | 2011-09-29 03:14:56 +0300 | [diff] [blame] | 147 | self.assertEqual(self.db.category('\U0001012A'), 'No') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 148 | |
| 149 | self.assertRaises(TypeError, self.db.category) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 150 | self.assertRaises(TypeError, self.db.category, 'xx') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 151 | |
| 152 | def test_bidirectional(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 153 | self.assertEqual(self.db.bidirectional('\uFFFE'), '') |
| 154 | self.assertEqual(self.db.bidirectional(' '), 'WS') |
| 155 | self.assertEqual(self.db.bidirectional('A'), 'L') |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 156 | self.assertEqual(self.db.bidirectional('\U00020000'), 'L') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 157 | |
| 158 | self.assertRaises(TypeError, self.db.bidirectional) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 159 | self.assertRaises(TypeError, self.db.bidirectional, 'xx') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 160 | |
| 161 | def test_decomposition(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 162 | self.assertEqual(self.db.decomposition('\uFFFE'),'') |
| 163 | self.assertEqual(self.db.decomposition('\u00bc'), '<fraction> 0031 2044 0034') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 164 | |
| 165 | self.assertRaises(TypeError, self.db.decomposition) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 166 | self.assertRaises(TypeError, self.db.decomposition, 'xx') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 167 | |
| 168 | def test_mirrored(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 169 | self.assertEqual(self.db.mirrored('\uFFFE'), 0) |
| 170 | self.assertEqual(self.db.mirrored('a'), 0) |
| 171 | self.assertEqual(self.db.mirrored('\u2201'), 1) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 172 | self.assertEqual(self.db.mirrored('\U00020000'), 0) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 173 | |
| 174 | self.assertRaises(TypeError, self.db.mirrored) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 175 | self.assertRaises(TypeError, self.db.mirrored, 'xx') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 176 | |
| 177 | def test_combining(self): |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 178 | self.assertEqual(self.db.combining('\uFFFE'), 0) |
| 179 | self.assertEqual(self.db.combining('a'), 0) |
| 180 | self.assertEqual(self.db.combining('\u20e1'), 230) |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 181 | self.assertEqual(self.db.combining('\U00020000'), 0) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 182 | |
| 183 | self.assertRaises(TypeError, self.db.combining) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 184 | self.assertRaises(TypeError, self.db.combining, 'xx') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 185 | |
| 186 | def test_normalize(self): |
| 187 | self.assertRaises(TypeError, self.db.normalize) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 188 | self.assertRaises(ValueError, self.db.normalize, 'unknown', 'xx') |
| 189 | self.assertEqual(self.db.normalize('NFKC', ''), '') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 190 | # The rest can be found in test_normalization.py |
| 191 | # which requires an external file. |
| 192 | |
Victor Stinner | 7ed9c4c | 2010-03-04 12:14:57 +0000 | [diff] [blame] | 193 | def test_pr29(self): |
| 194 | # http://www.unicode.org/review/pr-29.html |
Alexander Belopolsky | 86f65d5 | 2010-12-23 02:27:37 +0000 | [diff] [blame] | 195 | # See issues #1054943 and #10254. |
| 196 | composed = ("\u0b47\u0300\u0b3e", "\u1100\u0300\u1161", |
| 197 | 'Li\u030dt-s\u1e73\u0301', |
| 198 | '\u092e\u093e\u0930\u094d\u0915 \u091c\u093c' |
| 199 | + '\u0941\u0915\u0947\u0930\u092c\u0930\u094d\u0917', |
| 200 | '\u0915\u093f\u0930\u094d\u0917\u093f\u091c\u093c' |
| 201 | + '\u0938\u094d\u0924\u093e\u0928') |
| 202 | for text in composed: |
Victor Stinner | 7ed9c4c | 2010-03-04 12:14:57 +0000 | [diff] [blame] | 203 | self.assertEqual(self.db.normalize('NFC', text), text) |
| 204 | |
Alexander Belopolsky | 86f65d5 | 2010-12-23 02:27:37 +0000 | [diff] [blame] | 205 | def test_issue10254(self): |
| 206 | # Crash reported in #10254 |
| 207 | a = 'C\u0338' * 20 + 'C\u0327' |
| 208 | b = 'C\u0338' * 20 + '\xC7' |
| 209 | self.assertEqual(self.db.normalize('NFC', a), b) |
| 210 | |
Hye-Shik Chang | e9ddfbb | 2004-08-04 07:38:35 +0000 | [diff] [blame] | 211 | def test_east_asian_width(self): |
| 212 | eaw = self.db.east_asian_width |
Guido van Rossum | 98297ee | 2007-11-06 21:34:58 +0000 | [diff] [blame] | 213 | self.assertRaises(TypeError, eaw, b'a') |
Guido van Rossum | 254348e | 2007-11-21 19:29:53 +0000 | [diff] [blame] | 214 | self.assertRaises(TypeError, eaw, bytearray()) |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 215 | self.assertRaises(TypeError, eaw, '') |
| 216 | self.assertRaises(TypeError, eaw, 'ra') |
| 217 | self.assertEqual(eaw('\x1e'), 'N') |
| 218 | self.assertEqual(eaw('\x20'), 'Na') |
| 219 | self.assertEqual(eaw('\uC894'), 'W') |
| 220 | self.assertEqual(eaw('\uFF66'), 'H') |
| 221 | self.assertEqual(eaw('\uFF1F'), 'F') |
| 222 | self.assertEqual(eaw('\u2010'), 'A') |
Walter Dörwald | f342bfc | 2008-06-03 11:45:02 +0000 | [diff] [blame] | 223 | self.assertEqual(eaw('\U00020000'), 'W') |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 224 | |
| 225 | class UnicodeMiscTest(UnicodeDatabaseTest): |
| 226 | |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 227 | def test_failed_import_during_compiling(self): |
| 228 | # Issue 4367 |
| 229 | # Decoding \N escapes requires the unicodedata module. If it can't be |
| 230 | # imported, we shouldn't segfault. |
| 231 | |
| 232 | # This program should raise a SyntaxError in the eval. |
| 233 | code = "import sys;" \ |
| 234 | "sys.modules['unicodedata'] = None;" \ |
| 235 | """eval("'\\\\N{SOFT HYPHEN}'")""" |
| 236 | args = [sys.executable, "-c", code] |
| 237 | # We use a subprocess because the unicodedata module may already have |
| 238 | # been loaded in this process. |
| 239 | popen = subprocess.Popen(args, stderr=subprocess.PIPE) |
| 240 | popen.wait() |
| 241 | self.assertEqual(popen.returncode, 1) |
| 242 | error = "SyntaxError: (unicode error) \\N escapes not supported " \ |
| 243 | "(can't load unicodedata module)" |
Benjamin Peterson | 577473f | 2010-01-19 00:09:57 +0000 | [diff] [blame] | 244 | self.assertIn(error, popen.stderr.read().decode("ascii")) |
Antoine Pitrou | 849e12b | 2010-10-30 14:24:33 +0000 | [diff] [blame] | 245 | popen.stderr.close() |
Benjamin Peterson | 6f7fad1 | 2008-11-21 22:58:57 +0000 | [diff] [blame] | 246 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 247 | def test_decimal_numeric_consistent(self): |
| 248 | # Test that decimal and numeric are consistent, |
| 249 | # i.e. if a character has a decimal value, |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 250 | # its numeric value should be the same. |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 251 | count = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 252 | for i in range(0x10000): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 253 | c = chr(i) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 254 | dec = self.db.decimal(c, -1) |
| 255 | if dec != -1: |
| 256 | self.assertEqual(dec, self.db.numeric(c)) |
| 257 | count += 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 258 | self.assertTrue(count >= 10) # should have tested at least the ASCII digits |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 259 | |
| 260 | def test_digit_numeric_consistent(self): |
| 261 | # Test that digit and numeric are consistent, |
Tim Peters | 669454e | 2003-03-07 17:30:48 +0000 | [diff] [blame] | 262 | # i.e. if a character has a digit value, |
Georg Brandl | 7eb4b7d | 2005-07-22 21:49:32 +0000 | [diff] [blame] | 263 | # its numeric value should be the same. |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 264 | count = 0 |
Guido van Rossum | 805365e | 2007-05-07 22:24:25 +0000 | [diff] [blame] | 265 | for i in range(0x10000): |
Guido van Rossum | 84fc66d | 2007-05-03 17:18:26 +0000 | [diff] [blame] | 266 | c = chr(i) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 267 | dec = self.db.digit(c, -1) |
| 268 | if dec != -1: |
| 269 | self.assertEqual(dec, self.db.numeric(c)) |
| 270 | count += 1 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 271 | self.assertTrue(count >= 10) # should have tested at least the ASCII digits |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 272 | |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 273 | def test_bug_1704793(self): |
Ezio Melotti | b3aedd4 | 2010-11-20 19:04:17 +0000 | [diff] [blame] | 274 | self.assertEqual(self.db.lookup("GOTHIC LETTER FAIHU"), '\U00010346') |
Guido van Rossum | 806c246 | 2007-08-06 23:33:07 +0000 | [diff] [blame] | 275 | |
Martin v. Löwis | 93cbca3 | 2008-09-10 14:08:48 +0000 | [diff] [blame] | 276 | def test_ucd_510(self): |
| 277 | import unicodedata |
| 278 | # In UCD 5.1.0, a mirrored property changed wrt. UCD 3.2.0 |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 279 | self.assertTrue(unicodedata.mirrored("\u0f3a")) |
| 280 | self.assertTrue(not unicodedata.ucd_3_2_0.mirrored("\u0f3a")) |
Martin v. Löwis | 93cbca3 | 2008-09-10 14:08:48 +0000 | [diff] [blame] | 281 | # Also, we now have two ways of representing |
| 282 | # the upper-case mapping: as delta, or as absolute value |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 283 | self.assertTrue("a".upper()=='A') |
| 284 | self.assertTrue("\u1d79".upper()=='\ua77d') |
| 285 | self.assertTrue(".".upper()=='.') |
Walter Dörwald | 1b08b30 | 2009-04-25 14:13:56 +0000 | [diff] [blame] | 286 | |
| 287 | def test_bug_5828(self): |
| 288 | self.assertEqual("\u1d79".lower(), "\u1d79") |
| 289 | # Only U+0000 should have U+0000 as its upper/lower/titlecase variant |
| 290 | self.assertEqual( |
| 291 | [ |
| 292 | c for c in range(sys.maxunicode+1) |
| 293 | if "\x00" in chr(c).lower()+chr(c).upper()+chr(c).title() |
| 294 | ], |
| 295 | [0] |
| 296 | ) |
| 297 | |
Walter Dörwald | e250775 | 2009-04-26 19:16:11 +0000 | [diff] [blame] | 298 | def test_bug_4971(self): |
Martin v. Löwis | 71efeb7 | 2009-04-26 01:02:07 +0000 | [diff] [blame] | 299 | # LETTER DZ WITH CARON: DZ, Dz, dz |
| 300 | self.assertEqual("\u01c4".title(), "\u01c5") |
| 301 | self.assertEqual("\u01c5".title(), "\u01c5") |
| 302 | self.assertEqual("\u01c6".title(), "\u01c5") |
Martin v. Löwis | 93cbca3 | 2008-09-10 14:08:48 +0000 | [diff] [blame] | 303 | |
Florent Xicluna | 806d8cf | 2010-03-30 19:34:18 +0000 | [diff] [blame] | 304 | def test_linebreak_7643(self): |
| 305 | for i in range(0x10000): |
| 306 | lines = (chr(i) + 'A').splitlines() |
| 307 | if i in (0x0a, 0x0b, 0x0c, 0x0d, 0x85, |
| 308 | 0x1c, 0x1d, 0x1e, 0x2028, 0x2029): |
| 309 | self.assertEqual(len(lines), 2, |
| 310 | r"\u%.4x should be a linebreak" % i) |
| 311 | else: |
| 312 | self.assertEqual(len(lines), 1, |
| 313 | r"\u%.4x should not be a linebreak" % i) |
| 314 | |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 315 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 316 | test.support.run_unittest( |
Walter Dörwald | 21d3a32 | 2003-05-01 17:45:56 +0000 | [diff] [blame] | 317 | UnicodeMiscTest, |
| 318 | UnicodeMethodsTest, |
| 319 | UnicodeFunctionsTest |
| 320 | ) |
Walter Dörwald | 37c4728 | 2003-02-26 14:49:41 +0000 | [diff] [blame] | 321 | |
| 322 | if __name__ == "__main__": |
| 323 | test_main() |