Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 1 | """ Test script for the Unicode implementation. |
| 2 | |
| 3 | |
| 4 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | |
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | |
| 8 | """ |
| 9 | from test_support import verbose |
| 10 | import sys |
| 11 | |
| 12 | def test(method, input, output, *args): |
| 13 | if verbose: |
| 14 | print '%s.%s%s =? %s... ' % (repr(input), method, args, output), |
| 15 | try: |
| 16 | f = getattr(input, method) |
| 17 | value = apply(f, args) |
| 18 | except: |
| 19 | value = sys.exc_type |
| 20 | exc = sys.exc_info() |
| 21 | else: |
| 22 | exc = None |
| 23 | if value != output: |
| 24 | if verbose: |
| 25 | print 'no' |
| 26 | print '*',f, `input`, `output`, `value` |
| 27 | if exc: |
| 28 | print ' value == %s: %s' % (exc[:2]) |
| 29 | else: |
| 30 | if verbose: |
| 31 | print 'yes' |
| 32 | |
| 33 | test('capitalize', u' hello ', u' hello ') |
| 34 | test('capitalize', u'hello ', u'Hello ') |
| 35 | |
| 36 | test('title', u' hello ', u' Hello ') |
| 37 | test('title', u'hello ', u'Hello ') |
| 38 | test('title', u"fOrMaT thIs aS titLe String", u'Format This As Title String') |
| 39 | test('title', u"fOrMaT,thIs-aS*titLe;String", u'Format,This-As*Title;String') |
| 40 | test('title', u"getInt", u'Getint') |
| 41 | |
| 42 | test('find', u'abcdefghiabc', 0, u'abc') |
| 43 | test('find', u'abcdefghiabc', 9, u'abc', 1) |
| 44 | test('find', u'abcdefghiabc', -1, u'def', 4) |
| 45 | |
| 46 | test('rfind', u'abcdefghiabc', 9, u'abc') |
| 47 | |
| 48 | test('lower', u'HeLLo', u'hello') |
| 49 | test('lower', u'hello', u'hello') |
| 50 | |
| 51 | test('upper', u'HeLLo', u'HELLO') |
| 52 | test('upper', u'HELLO', u'HELLO') |
| 53 | |
| 54 | if 0: |
| 55 | transtable = '\000\001\002\003\004\005\006\007\010\011\012\013\014\015\016\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036\037 !"#$%&\'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`xyzdefghijklmnopqrstuvwxyz{|}~\177\200\201\202\203\204\205\206\207\210\211\212\213\214\215\216\217\220\221\222\223\224\225\226\227\230\231\232\233\234\235\236\237\240\241\242\243\244\245\246\247\250\251\252\253\254\255\256\257\260\261\262\263\264\265\266\267\270\271\272\273\274\275\276\277\300\301\302\303\304\305\306\307\310\311\312\313\314\315\316\317\320\321\322\323\324\325\326\327\330\331\332\333\334\335\336\337\340\341\342\343\344\345\346\347\350\351\352\353\354\355\356\357\360\361\362\363\364\365\366\367\370\371\372\373\374\375\376\377' |
| 56 | |
| 57 | test('maketrans', u'abc', transtable, u'xyz') |
| 58 | test('maketrans', u'abc', ValueError, u'xyzq') |
| 59 | |
| 60 | test('split', u'this is the split function', |
| 61 | [u'this', u'is', u'the', u'split', u'function']) |
| 62 | test('split', u'a|b|c|d', [u'a', u'b', u'c', u'd'], u'|') |
| 63 | test('split', u'a|b|c|d', [u'a', u'b', u'c|d'], u'|', 2) |
| 64 | test('split', u'a b c d', [u'a', u'b c d'], None, 1) |
| 65 | test('split', u'a b c d', [u'a', u'b', u'c d'], None, 2) |
| 66 | test('split', u'a b c d', [u'a', u'b', u'c', u'd'], None, 3) |
| 67 | test('split', u'a b c d', [u'a', u'b', u'c', u'd'], None, 4) |
| 68 | test('split', u'a b c d', [u'a b c d'], None, 0) |
| 69 | test('split', u'a b c d', [u'a', u'b', u'c d'], None, 2) |
| 70 | test('split', u'a b c d ', [u'a', u'b', u'c', u'd']) |
| 71 | |
| 72 | # join now works with any sequence type |
| 73 | class Sequence: |
| 74 | def __init__(self): self.seq = 'wxyz' |
| 75 | def __len__(self): return len(self.seq) |
| 76 | def __getitem__(self, i): return self.seq[i] |
| 77 | |
| 78 | test('join', u' ', u'a b c d', [u'a', u'b', u'c', u'd']) |
| 79 | test('join', u'', u'abcd', (u'a', u'b', u'c', u'd')) |
| 80 | test('join', u' ', u'w x y z', Sequence()) |
| 81 | test('join', u' ', TypeError, 7) |
| 82 | |
| 83 | class BadSeq(Sequence): |
| 84 | def __init__(self): self.seq = [7, u'hello', 123L] |
| 85 | |
| 86 | test('join', u' ', TypeError, BadSeq()) |
| 87 | |
| 88 | result = u'' |
| 89 | for i in range(10): |
| 90 | if i > 0: |
| 91 | result = result + u':' |
| 92 | result = result + u'x'*10 |
| 93 | test('join', u':', result, [u'x' * 10] * 10) |
| 94 | test('join', u':', result, (u'x' * 10,) * 10) |
| 95 | |
| 96 | test('strip', u' hello ', u'hello') |
| 97 | test('lstrip', u' hello ', u'hello ') |
| 98 | test('rstrip', u' hello ', u' hello') |
| 99 | test('strip', u'hello', u'hello') |
| 100 | |
| 101 | test('swapcase', u'HeLLo cOmpUteRs', u'hEllO CoMPuTErS') |
| 102 | |
| 103 | if 0: |
| 104 | test('translate', u'xyzabcdef', u'xyzxyz', transtable, u'def') |
| 105 | |
| 106 | table = string.maketrans('a', u'A') |
| 107 | test('translate', u'abc', u'Abc', table) |
| 108 | test('translate', u'xyz', u'xyz', table) |
| 109 | |
| 110 | test('replace', u'one!two!three!', u'one@two!three!', u'!', u'@', 1) |
Barry Warsaw | 51ac580 | 2000-03-20 16:36:48 +0000 | [diff] [blame] | 111 | test('replace', u'one!two!three!', u'onetwothree', '!', '') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 112 | test('replace', u'one!two!three!', u'one@two@three!', u'!', u'@', 2) |
| 113 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@', 3) |
| 114 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@', 4) |
| 115 | test('replace', u'one!two!three!', u'one!two!three!', u'!', u'@', 0) |
| 116 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@') |
| 117 | test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@') |
| 118 | test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@', 2) |
| 119 | |
| 120 | test('startswith', u'hello', 1, u'he') |
| 121 | test('startswith', u'hello', 1, u'hello') |
| 122 | test('startswith', u'hello', 0, u'hello world') |
| 123 | test('startswith', u'hello', 1, u'') |
| 124 | test('startswith', u'hello', 0, u'ello') |
| 125 | test('startswith', u'hello', 1, u'ello', 1) |
| 126 | test('startswith', u'hello', 1, u'o', 4) |
| 127 | test('startswith', u'hello', 0, u'o', 5) |
| 128 | test('startswith', u'hello', 1, u'', 5) |
| 129 | test('startswith', u'hello', 0, u'lo', 6) |
| 130 | test('startswith', u'helloworld', 1, u'lowo', 3) |
| 131 | test('startswith', u'helloworld', 1, u'lowo', 3, 7) |
| 132 | test('startswith', u'helloworld', 0, u'lowo', 3, 6) |
| 133 | |
| 134 | test('endswith', u'hello', 1, u'lo') |
| 135 | test('endswith', u'hello', 0, u'he') |
| 136 | test('endswith', u'hello', 1, u'') |
| 137 | test('endswith', u'hello', 0, u'hello world') |
| 138 | test('endswith', u'helloworld', 0, u'worl') |
| 139 | test('endswith', u'helloworld', 1, u'worl', 3, 9) |
| 140 | test('endswith', u'helloworld', 1, u'world', 3, 12) |
| 141 | test('endswith', u'helloworld', 1, u'lowo', 1, 7) |
| 142 | test('endswith', u'helloworld', 1, u'lowo', 2, 7) |
| 143 | test('endswith', u'helloworld', 1, u'lowo', 3, 7) |
| 144 | test('endswith', u'helloworld', 0, u'lowo', 4, 7) |
| 145 | test('endswith', u'helloworld', 0, u'lowo', 3, 8) |
| 146 | test('endswith', u'ab', 0, u'ab', 0, 1) |
| 147 | test('endswith', u'ab', 0, u'ab', 0, 0) |
| 148 | |
| 149 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi') |
| 150 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 8) |
| 151 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 4) |
| 152 | test('expandtabs', u'abc\r\nab\tdef\ng\thi', u'abc\r\nab def\ng hi', 4) |
| 153 | |
| 154 | if 0: |
| 155 | test('capwords', u'abc def ghi', u'Abc Def Ghi') |
| 156 | test('capwords', u'abc\tdef\nghi', u'Abc Def Ghi') |
| 157 | test('capwords', u'abc\t def \nghi', u'Abc Def Ghi') |
| 158 | |
| 159 | # Comparisons: |
| 160 | print 'Testing Unicode comparisons...', |
| 161 | assert u'abc' == 'abc' |
| 162 | assert 'abc' == u'abc' |
| 163 | assert u'abc' == u'abc' |
| 164 | assert u'abcd' > 'abc' |
| 165 | assert 'abcd' > u'abc' |
| 166 | assert u'abcd' > u'abc' |
| 167 | assert u'abc' < 'abcd' |
| 168 | assert 'abc' < u'abcd' |
| 169 | assert u'abc' < u'abcd' |
| 170 | print 'done.' |
| 171 | |
| 172 | test('ljust', u'abc', u'abc ', 10) |
| 173 | test('rjust', u'abc', u' abc', 10) |
| 174 | test('center', u'abc', u' abc ', 10) |
| 175 | test('ljust', u'abc', u'abc ', 6) |
| 176 | test('rjust', u'abc', u' abc', 6) |
| 177 | test('center', u'abc', u' abc ', 6) |
| 178 | test('ljust', u'abc', u'abc', 2) |
| 179 | test('rjust', u'abc', u'abc', 2) |
| 180 | test('center', u'abc', u'abc', 2) |
| 181 | |
| 182 | test('islower', u'a', 1) |
| 183 | test('islower', u'A', 0) |
| 184 | test('islower', u'\n', 0) |
| 185 | test('islower', u'\u1FFc', 0) |
| 186 | test('islower', u'abc', 1) |
| 187 | test('islower', u'aBc', 0) |
| 188 | test('islower', u'abc\n', 1) |
| 189 | |
| 190 | test('isupper', u'a', 0) |
| 191 | test('isupper', u'A', 1) |
| 192 | test('isupper', u'\n', 0) |
| 193 | test('isupper', u'\u1FFc', 0) |
| 194 | test('isupper', u'ABC', 1) |
| 195 | test('isupper', u'AbC', 0) |
| 196 | test('isupper', u'ABC\n', 1) |
| 197 | |
| 198 | test('istitle', u'a', 0) |
| 199 | test('istitle', u'A', 1) |
| 200 | test('istitle', u'\n', 0) |
| 201 | test('istitle', u'\u1FFc', 1) |
| 202 | test('istitle', u'A Titlecased Line', 1) |
| 203 | test('istitle', u'A\nTitlecased Line', 1) |
| 204 | test('istitle', u'A Titlecased, Line', 1) |
| 205 | test('istitle', u'Greek \u1FFcitlecases ...', 1) |
| 206 | test('istitle', u'Not a capitalized String', 0) |
| 207 | test('istitle', u'Not\ta Titlecase String', 0) |
| 208 | test('istitle', u'Not--a Titlecase String', 0) |
| 209 | |
| 210 | test('splitlines', u"abc\ndef\n\rghi", [u'abc', u'def', u'', u'ghi']) |
| 211 | test('splitlines', u"abc\ndef\n\r\nghi", [u'abc', u'def', u'', u'ghi']) |
| 212 | test('splitlines', u"abc\ndef\r\nghi", [u'abc', u'def', u'ghi']) |
| 213 | test('splitlines', u"abc\ndef\r\nghi\n", [u'abc', u'def', u'ghi']) |
| 214 | test('splitlines', u"abc\ndef\r\nghi\n\r", [u'abc', u'def', u'ghi', u'']) |
| 215 | test('splitlines', u"\nabc\ndef\r\nghi\n\r", [u'', u'abc', u'def', u'ghi', u'']) |
| 216 | test('splitlines', u"\nabc\ndef\r\nghi\n\r", [u'', u'abc\012def\015\012ghi\012\015'], 1) |
| 217 | test('splitlines', u"\nabc\ndef\r\nghi\n\r", [u'', u'abc', u'def\015\012ghi\012\015'], 2) |
| 218 | |
| 219 | test('translate', u"abababc", u'bbbc', {ord('a'):None}) |
| 220 | test('translate', u"abababc", u'iiic', {ord('a'):None, ord('b'):ord('i')}) |
| 221 | test('translate', u"abababc", u'iiix', {ord('a'):None, ord('b'):ord('i'), ord('c'):u'x'}) |
| 222 | |
Guido van Rossum | d4d2684 | 2000-03-13 23:21:48 +0000 | [diff] [blame] | 223 | # Contains: |
| 224 | print 'Testing Unicode contains method...', |
| 225 | assert ('a' in 'abdb') == 1 |
| 226 | assert ('a' in 'bdab') == 1 |
| 227 | assert ('a' in 'bdaba') == 1 |
| 228 | assert ('a' in 'bdba') == 1 |
| 229 | assert ('a' in u'bdba') == 1 |
| 230 | assert (u'a' in u'bdba') == 1 |
| 231 | assert (u'a' in u'bdb') == 0 |
| 232 | assert (u'a' in 'bdb') == 0 |
| 233 | assert (u'a' in 'bdba') == 1 |
| 234 | print 'done.' |
| 235 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 236 | # Formatting: |
| 237 | print 'Testing Unicode formatting strings...', |
| 238 | assert u"%s, %s" % (u"abc", "abc") == u'abc, abc' |
| 239 | assert u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, 2, 3) == u'abc, abc, 1, 2.000000, 3.00' |
| 240 | assert u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, -2, 3) == u'abc, abc, 1, -2.000000, 3.00' |
| 241 | assert u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.5) == u'abc, abc, -1, -2.000000, 3.50' |
| 242 | assert u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.57) == u'abc, abc, -1, -2.000000, 3.57' |
| 243 | assert u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 1003.57) == u'abc, abc, -1, -2.000000, 1003.57' |
| 244 | assert u"%c" % (u"abc",) == u'a' |
| 245 | assert u"%c" % ("abc",) == u'a' |
| 246 | assert u"%c" % (34,) == u'"' |
| 247 | assert u"%c" % (36,) == u'$' |
| 248 | assert u"%r, %r" % (u"abc", "abc") == u"u'abc', 'abc'" |
| 249 | assert u"%(x)s, %(y)s" % {'x':u"abc", 'y':"def"} == u'abc, def' |
| 250 | assert u"%(x)s, %(ä)s" % {'x':u"abc", u'ä'.encode('utf-8'):"def"} == u'abc, def' |
| 251 | print 'done.' |
| 252 | |
| 253 | # Test Unicode database APIs |
| 254 | try: |
| 255 | import unicodedata |
| 256 | except ImportError: |
| 257 | pass |
| 258 | else: |
| 259 | print 'Testing unicodedata module...', |
| 260 | |
| 261 | assert unicodedata.digit(u'A',None) is None |
| 262 | assert unicodedata.digit(u'9') == 9 |
| 263 | assert unicodedata.digit(u'\u215b',None) is None |
| 264 | assert unicodedata.digit(u'\u2468') == 9 |
| 265 | |
| 266 | assert unicodedata.numeric(u'A',None) is None |
| 267 | assert unicodedata.numeric(u'9') == 9 |
| 268 | assert unicodedata.numeric(u'\u215b') == 0.125 |
| 269 | assert unicodedata.numeric(u'\u2468') == 9.0 |
| 270 | |
| 271 | assert unicodedata.decimal(u'A',None) is None |
| 272 | assert unicodedata.decimal(u'9') == 9 |
| 273 | assert unicodedata.decimal(u'\u215b',None) is None |
| 274 | assert unicodedata.decimal(u'\u2468',None) is None |
| 275 | |
| 276 | assert unicodedata.category(u'\uFFFE') == 'Cn' |
| 277 | assert unicodedata.category(u'a') == 'Ll' |
| 278 | assert unicodedata.category(u'A') == 'Lu' |
| 279 | |
| 280 | assert unicodedata.bidirectional(u'\uFFFE') == '' |
| 281 | assert unicodedata.bidirectional(u' ') == 'WS' |
| 282 | assert unicodedata.bidirectional(u'A') == 'L' |
| 283 | |
| 284 | assert unicodedata.decomposition(u'\uFFFE') == '' |
| 285 | assert unicodedata.decomposition(u'\u00bc') == '<fraction> 0031 2044 0034' |
| 286 | |
| 287 | assert unicodedata.mirrored(u'\uFFFE') == 0 |
| 288 | assert unicodedata.mirrored(u'a') == 0 |
| 289 | assert unicodedata.mirrored(u'\u2201') == 1 |
| 290 | |
| 291 | assert unicodedata.combining(u'\uFFFE') == 0 |
| 292 | assert unicodedata.combining(u'a') == 0 |
| 293 | assert unicodedata.combining(u'\u20e1') == 230 |
| 294 | |
| 295 | print 'done.' |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 296 | |
| 297 | # Test builtin codecs |
| 298 | print 'Testing builtin codecs...', |
| 299 | |
| 300 | assert unicode('hello','ascii') == u'hello' |
| 301 | assert unicode('hello','utf-8') == u'hello' |
| 302 | assert unicode('hello','utf8') == u'hello' |
| 303 | assert unicode('hello','latin-1') == u'hello' |
| 304 | |
| 305 | assert u'hello'.encode('ascii') == 'hello' |
| 306 | assert u'hello'.encode('utf-8') == 'hello' |
| 307 | assert u'hello'.encode('utf8') == 'hello' |
| 308 | assert u'hello'.encode('utf-16-le') == 'h\000e\000l\000l\000o\000' |
| 309 | assert u'hello'.encode('utf-16-be') == '\000h\000e\000l\000l\000o' |
| 310 | assert u'hello'.encode('latin-1') == 'hello' |
| 311 | |
| 312 | u = u''.join(map(unichr, range(1024))) |
| 313 | for encoding in ('utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
| 314 | 'raw_unicode_escape', 'unicode_escape', 'unicode_internal'): |
| 315 | assert unicode(u.encode(encoding),encoding) == u |
| 316 | |
| 317 | u = u''.join(map(unichr, range(256))) |
| 318 | for encoding in ('latin-1',): |
| 319 | assert unicode(u.encode(encoding),encoding) == u |
| 320 | |
| 321 | u = u''.join(map(unichr, range(128))) |
| 322 | for encoding in ('ascii',): |
| 323 | assert unicode(u.encode(encoding),encoding) == u |
| 324 | |
| 325 | print 'done.' |