Martin v. Löwis | a729daf | 2002-08-04 17:28:33 +0000 | [diff] [blame] | 1 | # -*- coding: iso-8859-1 -*- |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 2 | """ Test script for the Unicode implementation. |
| 3 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 4 | Written by Marc-Andre Lemburg (mal@lemburg.com). |
| 5 | |
| 6 | (c) Copyright CNRI, All Rights Reserved. NO WARRANTY. |
| 7 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 8 | """#" |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 9 | from test.test_support import verify, vereq, verbose, TestFailed |
Andrew M. Kuchling | eddd68d | 2002-03-29 16:21:44 +0000 | [diff] [blame] | 10 | import sys, string |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 11 | |
Finn Bock | 2b29cb2 | 2001-12-10 20:57:34 +0000 | [diff] [blame] | 12 | if not sys.platform.startswith('java'): |
| 13 | # Test basic sanity of repr() |
| 14 | verify(repr(u'abc') == "u'abc'") |
| 15 | verify(repr(u'ab\\c') == "u'ab\\\\c'") |
| 16 | verify(repr(u'ab\\') == "u'ab\\\\'") |
| 17 | verify(repr(u'\\c') == "u'\\\\c'") |
| 18 | verify(repr(u'\\') == "u'\\\\'") |
| 19 | verify(repr(u'\n') == "u'\\n'") |
| 20 | verify(repr(u'\r') == "u'\\r'") |
| 21 | verify(repr(u'\t') == "u'\\t'") |
| 22 | verify(repr(u'\b') == "u'\\x08'") |
| 23 | verify(repr(u"'\"") == """u'\\'"'""") |
| 24 | verify(repr(u"'\"") == """u'\\'"'""") |
| 25 | verify(repr(u"'") == '''u"'"''') |
| 26 | verify(repr(u'"') == """u'"'""") |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 27 | latin1repr = ( |
| 28 | "u'\\x00\\x01\\x02\\x03\\x04\\x05\\x06\\x07\\x08\\t\\n\\x0b\\x0c\\r" |
| 29 | "\\x0e\\x0f\\x10\\x11\\x12\\x13\\x14\\x15\\x16\\x17\\x18\\x19\\x1a" |
| 30 | "\\x1b\\x1c\\x1d\\x1e\\x1f !\"#$%&\\'()*+,-./0123456789:;<=>?@ABCDEFGHI" |
| 31 | "JKLMNOPQRSTUVWXYZ[\\\\]^_`abcdefghijklmnopqrstuvwxyz{|}~\\x7f" |
| 32 | "\\x80\\x81\\x82\\x83\\x84\\x85\\x86\\x87\\x88\\x89\\x8a\\x8b\\x8c\\x8d" |
| 33 | "\\x8e\\x8f\\x90\\x91\\x92\\x93\\x94\\x95\\x96\\x97\\x98\\x99\\x9a\\x9b" |
| 34 | "\\x9c\\x9d\\x9e\\x9f\\xa0\\xa1\\xa2\\xa3\\xa4\\xa5\\xa6\\xa7\\xa8\\xa9" |
| 35 | "\\xaa\\xab\\xac\\xad\\xae\\xaf\\xb0\\xb1\\xb2\\xb3\\xb4\\xb5\\xb6\\xb7" |
| 36 | "\\xb8\\xb9\\xba\\xbb\\xbc\\xbd\\xbe\\xbf\\xc0\\xc1\\xc2\\xc3\\xc4\\xc5" |
| 37 | "\\xc6\\xc7\\xc8\\xc9\\xca\\xcb\\xcc\\xcd\\xce\\xcf\\xd0\\xd1\\xd2\\xd3" |
| 38 | "\\xd4\\xd5\\xd6\\xd7\\xd8\\xd9\\xda\\xdb\\xdc\\xdd\\xde\\xdf\\xe0\\xe1" |
| 39 | "\\xe2\\xe3\\xe4\\xe5\\xe6\\xe7\\xe8\\xe9\\xea\\xeb\\xec\\xed\\xee\\xef" |
| 40 | "\\xf0\\xf1\\xf2\\xf3\\xf4\\xf5\\xf6\\xf7\\xf8\\xf9\\xfa\\xfb\\xfc\\xfd" |
| 41 | "\\xfe\\xff'") |
| 42 | testrepr = repr(u''.join(map(unichr, range(256)))) |
| 43 | verify(testrepr == latin1repr) |
Guido van Rossum | e4874ae | 2001-09-21 15:36:41 +0000 | [diff] [blame] | 44 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 45 | def test(method, input, output, *args): |
| 46 | if verbose: |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 47 | print '%s.%s%s =? %s... ' % (repr(input), method, args, repr(output)), |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 48 | try: |
| 49 | f = getattr(input, method) |
| 50 | value = apply(f, args) |
| 51 | except: |
| 52 | value = sys.exc_type |
Guido van Rossum | 6650320 | 2000-04-28 20:39:58 +0000 | [diff] [blame] | 53 | exc = sys.exc_info()[:2] |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 54 | else: |
| 55 | exc = None |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 56 | if value == output and type(value) is type(output): |
| 57 | # if the original is returned make sure that |
| 58 | # this doesn't happen with subclasses |
| 59 | if value is input: |
| 60 | class usub(unicode): |
| 61 | def __repr__(self): |
| 62 | return 'usub(%r)' % unicode.__repr__(self) |
| 63 | input = usub(input) |
| 64 | try: |
| 65 | f = getattr(input, method) |
| 66 | value = apply(f, args) |
| 67 | except: |
| 68 | value = sys.exc_type |
| 69 | exc = sys.exc_info()[:2] |
| 70 | if value is input: |
| 71 | if verbose: |
Tim Peters | 8ac1495 | 2002-05-23 15:15:30 +0000 | [diff] [blame] | 72 | print 'no' |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 73 | print '*',f, `input`, `output`, `value` |
| 74 | return |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 75 | if value != output or type(value) is not type(output): |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 76 | if verbose: |
| 77 | print 'no' |
| 78 | print '*',f, `input`, `output`, `value` |
| 79 | if exc: |
Guido van Rossum | 6650320 | 2000-04-28 20:39:58 +0000 | [diff] [blame] | 80 | print ' value == %s: %s' % (exc) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 81 | else: |
| 82 | if verbose: |
| 83 | print 'yes' |
| 84 | |
| 85 | test('capitalize', u' hello ', u' hello ') |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 86 | test('capitalize', u'Hello ', u'Hello ') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 87 | test('capitalize', u'hello ', u'Hello ') |
Marc-André Lemburg | fde66e1 | 2001-01-29 11:14:16 +0000 | [diff] [blame] | 88 | test('capitalize', u'aaaa', u'Aaaa') |
| 89 | test('capitalize', u'AaAa', u'Aaaa') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 90 | |
Marc-André Lemburg | 3a645e4 | 2001-01-16 11:54:12 +0000 | [diff] [blame] | 91 | test('count', u'aaa', 3, u'a') |
| 92 | test('count', u'aaa', 0, u'b') |
| 93 | test('count', 'aaa', 3, u'a') |
| 94 | test('count', 'aaa', 0, u'b') |
| 95 | test('count', u'aaa', 3, 'a') |
| 96 | test('count', u'aaa', 0, 'b') |
| 97 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 98 | test('title', u' hello ', u' Hello ') |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 99 | test('title', u'Hello ', u'Hello ') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 100 | test('title', u'hello ', u'Hello ') |
| 101 | test('title', u"fOrMaT thIs aS titLe String", u'Format This As Title String') |
| 102 | test('title', u"fOrMaT,thIs-aS*titLe;String", u'Format,This-As*Title;String') |
| 103 | test('title', u"getInt", u'Getint') |
| 104 | |
| 105 | test('find', u'abcdefghiabc', 0, u'abc') |
| 106 | test('find', u'abcdefghiabc', 9, u'abc', 1) |
| 107 | test('find', u'abcdefghiabc', -1, u'def', 4) |
| 108 | |
| 109 | test('rfind', u'abcdefghiabc', 9, u'abc') |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 110 | test('rfind', 'abcdefghiabc', 9, u'abc') |
| 111 | test('rfind', 'abcdefghiabc', 12, u'') |
| 112 | test('rfind', u'abcdefghiabc', 12, '') |
| 113 | test('rfind', u'abcdefghiabc', 12, u'') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 114 | |
| 115 | test('lower', u'HeLLo', u'hello') |
| 116 | test('lower', u'hello', u'hello') |
| 117 | |
| 118 | test('upper', u'HeLLo', u'HELLO') |
| 119 | test('upper', u'HELLO', u'HELLO') |
| 120 | |
| 121 | if 0: |
| 122 | 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' |
| 123 | |
| 124 | test('maketrans', u'abc', transtable, u'xyz') |
| 125 | test('maketrans', u'abc', ValueError, u'xyzq') |
| 126 | |
| 127 | test('split', u'this is the split function', |
| 128 | [u'this', u'is', u'the', u'split', u'function']) |
| 129 | test('split', u'a|b|c|d', [u'a', u'b', u'c', u'd'], u'|') |
| 130 | test('split', u'a|b|c|d', [u'a', u'b', u'c|d'], u'|', 2) |
| 131 | test('split', u'a b c d', [u'a', u'b c d'], None, 1) |
| 132 | test('split', u'a b c d', [u'a', u'b', u'c d'], None, 2) |
| 133 | test('split', u'a b c d', [u'a', u'b', u'c', u'd'], None, 3) |
| 134 | test('split', u'a b c d', [u'a', u'b', u'c', u'd'], None, 4) |
| 135 | test('split', u'a b c d', [u'a b c d'], None, 0) |
| 136 | test('split', u'a b c d', [u'a', u'b', u'c d'], None, 2) |
| 137 | test('split', u'a b c d ', [u'a', u'b', u'c', u'd']) |
Guido van Rossum | 8b26454 | 2000-12-19 02:22:31 +0000 | [diff] [blame] | 138 | test('split', u'a//b//c//d', [u'a', u'b', u'c', u'd'], u'//') |
| 139 | test('split', u'a//b//c//d', [u'a', u'b', u'c', u'd'], '//') |
| 140 | test('split', 'a//b//c//d', [u'a', u'b', u'c', u'd'], u'//') |
| 141 | test('split', u'endcase test', [u'endcase ', u''], u'test') |
| 142 | test('split', u'endcase test', [u'endcase ', u''], 'test') |
| 143 | test('split', 'endcase test', [u'endcase ', u''], u'test') |
| 144 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 145 | |
| 146 | # join now works with any sequence type |
| 147 | class Sequence: |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 148 | def __init__(self, seq): self.seq = seq |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 149 | def __len__(self): return len(self.seq) |
| 150 | def __getitem__(self, i): return self.seq[i] |
| 151 | |
| 152 | test('join', u' ', u'a b c d', [u'a', u'b', u'c', u'd']) |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 153 | test('join', u' ', u'a b c d', ['a', 'b', u'c', u'd']) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 154 | test('join', u'', u'abcd', (u'a', u'b', u'c', u'd')) |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 155 | test('join', u' ', u'w x y z', Sequence('wxyz')) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 156 | test('join', u' ', TypeError, 7) |
Guido van Rossum | 15ffc71 | 2000-11-29 12:13:59 +0000 | [diff] [blame] | 157 | test('join', u' ', TypeError, Sequence([7, u'hello', 123L])) |
| 158 | test('join', ' ', u'a b c d', [u'a', u'b', u'c', u'd']) |
| 159 | test('join', ' ', u'a b c d', ['a', 'b', u'c', u'd']) |
| 160 | test('join', '', u'abcd', (u'a', u'b', u'c', u'd')) |
| 161 | test('join', ' ', u'w x y z', Sequence(u'wxyz')) |
| 162 | test('join', ' ', TypeError, 7) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 163 | |
| 164 | result = u'' |
| 165 | for i in range(10): |
| 166 | if i > 0: |
| 167 | result = result + u':' |
| 168 | result = result + u'x'*10 |
| 169 | test('join', u':', result, [u'x' * 10] * 10) |
| 170 | test('join', u':', result, (u'x' * 10,) * 10) |
| 171 | |
| 172 | test('strip', u' hello ', u'hello') |
| 173 | test('lstrip', u' hello ', u'hello ') |
| 174 | test('rstrip', u' hello ', u' hello') |
| 175 | test('strip', u'hello', u'hello') |
| 176 | |
Walter Dörwald | de02bcb | 2002-04-22 17:42:37 +0000 | [diff] [blame] | 177 | # strip/lstrip/rstrip with None arg |
| 178 | test('strip', u' hello ', u'hello', None) |
| 179 | test('lstrip', u' hello ', u'hello ', None) |
| 180 | test('rstrip', u' hello ', u' hello', None) |
| 181 | test('strip', u'hello', u'hello', None) |
| 182 | |
| 183 | # strip/lstrip/rstrip with unicode arg |
| 184 | test('strip', u'xyzzyhelloxyzzy', u'hello', u'xyz') |
| 185 | test('lstrip', u'xyzzyhelloxyzzy', u'helloxyzzy', u'xyz') |
| 186 | test('rstrip', u'xyzzyhelloxyzzy', u'xyzzyhello', u'xyz') |
| 187 | test('strip', u'hello', u'hello', u'xyz') |
| 188 | |
| 189 | # strip/lstrip/rstrip with str arg |
| 190 | test('strip', u'xyzzyhelloxyzzy', u'hello', 'xyz') |
| 191 | test('lstrip', u'xyzzyhelloxyzzy', u'helloxyzzy', 'xyz') |
| 192 | test('rstrip', u'xyzzyhelloxyzzy', u'xyzzyhello', 'xyz') |
| 193 | test('strip', u'hello', u'hello', 'xyz') |
| 194 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 195 | test('swapcase', u'HeLLo cOmpUteRs', u'hEllO CoMPuTErS') |
| 196 | |
| 197 | if 0: |
| 198 | test('translate', u'xyzabcdef', u'xyzxyz', transtable, u'def') |
| 199 | |
| 200 | table = string.maketrans('a', u'A') |
| 201 | test('translate', u'abc', u'Abc', table) |
| 202 | test('translate', u'xyz', u'xyz', table) |
| 203 | |
| 204 | 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] | 205 | test('replace', u'one!two!three!', u'onetwothree', '!', '') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 206 | test('replace', u'one!two!three!', u'one@two@three!', u'!', u'@', 2) |
| 207 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@', 3) |
| 208 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@', 4) |
| 209 | test('replace', u'one!two!three!', u'one!two!three!', u'!', u'@', 0) |
| 210 | test('replace', u'one!two!three!', u'one@two@three@', u'!', u'@') |
| 211 | test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@') |
| 212 | test('replace', u'one!two!three!', u'one!two!three!', u'x', u'@', 2) |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 213 | test('replace', u'abc', u'-a-b-c-', u'', u'-') |
| 214 | test('replace', u'abc', u'-a-b-c', u'', u'-', 3) |
| 215 | test('replace', u'abc', u'abc', u'', u'-', 0) |
Guido van Rossum | 2023c9b | 2002-08-23 18:50:21 +0000 | [diff] [blame] | 216 | test('replace', u'abc', u'abc', u'ab', u'--', 0) |
| 217 | test('replace', u'abc', u'abc', u'xy', u'--') |
Guido van Rossum | 8b1a6d6 | 2002-08-23 18:21:28 +0000 | [diff] [blame] | 218 | test('replace', u'', u'', u'', u'') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 220 | test('startswith', u'hello', True, u'he') |
| 221 | test('startswith', u'hello', True, u'hello') |
| 222 | test('startswith', u'hello', False, u'hello world') |
| 223 | test('startswith', u'hello', True, u'') |
| 224 | test('startswith', u'hello', False, u'ello') |
| 225 | test('startswith', u'hello', True, u'ello', 1) |
| 226 | test('startswith', u'hello', True, u'o', 4) |
| 227 | test('startswith', u'hello', False, u'o', 5) |
| 228 | test('startswith', u'hello', True, u'', 5) |
| 229 | test('startswith', u'hello', False, u'lo', 6) |
| 230 | test('startswith', u'helloworld', True, u'lowo', 3) |
| 231 | test('startswith', u'helloworld', True, u'lowo', 3, 7) |
| 232 | test('startswith', u'helloworld', False, u'lowo', 3, 6) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 234 | test('endswith', u'hello', True, u'lo') |
| 235 | test('endswith', u'hello', False, u'he') |
| 236 | test('endswith', u'hello', True, u'') |
| 237 | test('endswith', u'hello', False, u'hello world') |
| 238 | test('endswith', u'helloworld', False, u'worl') |
| 239 | test('endswith', u'helloworld', True, u'worl', 3, 9) |
| 240 | test('endswith', u'helloworld', True, u'world', 3, 12) |
| 241 | test('endswith', u'helloworld', True, u'lowo', 1, 7) |
| 242 | test('endswith', u'helloworld', True, u'lowo', 2, 7) |
| 243 | test('endswith', u'helloworld', True, u'lowo', 3, 7) |
| 244 | test('endswith', u'helloworld', False, u'lowo', 4, 7) |
| 245 | test('endswith', u'helloworld', False, u'lowo', 3, 8) |
| 246 | test('endswith', u'ab', False, u'ab', 0, 1) |
| 247 | test('endswith', u'ab', False, u'ab', 0, 0) |
Guido van Rossum | 76afbd9 | 2002-08-20 17:29:29 +0000 | [diff] [blame] | 248 | test('endswith', 'helloworld', True, u'd') |
| 249 | test('endswith', 'helloworld', False, u'l') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 250 | |
| 251 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi') |
| 252 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 8) |
| 253 | test('expandtabs', u'abc\rab\tdef\ng\thi', u'abc\rab def\ng hi', 4) |
| 254 | test('expandtabs', u'abc\r\nab\tdef\ng\thi', u'abc\r\nab def\ng hi', 4) |
Walter Dörwald | 2ee4be0 | 2002-04-17 21:34:05 +0000 | [diff] [blame] | 255 | test('expandtabs', u'abc\r\nab\r\ndef\ng\r\nhi', u'abc\r\nab\r\ndef\ng\r\nhi', 4) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 256 | |
| 257 | if 0: |
| 258 | test('capwords', u'abc def ghi', u'Abc Def Ghi') |
| 259 | test('capwords', u'abc\tdef\nghi', u'Abc Def Ghi') |
| 260 | test('capwords', u'abc\t def \nghi', u'Abc Def Ghi') |
| 261 | |
Walter Dörwald | 068325e | 2002-04-15 13:36:47 +0000 | [diff] [blame] | 262 | test('zfill', u'123', u'123', 2) |
| 263 | test('zfill', u'123', u'123', 3) |
| 264 | test('zfill', u'123', u'0123', 4) |
| 265 | test('zfill', u'+123', u'+123', 3) |
| 266 | test('zfill', u'+123', u'+123', 4) |
| 267 | test('zfill', u'+123', u'+0123', 5) |
| 268 | test('zfill', u'-123', u'-123', 3) |
| 269 | test('zfill', u'-123', u'-123', 4) |
| 270 | test('zfill', u'-123', u'-0123', 5) |
| 271 | test('zfill', u'', u'000', 3) |
| 272 | test('zfill', u'34', u'34', 1) |
| 273 | test('zfill', u'34', u'00034', 5) |
Andrew M. Kuchling | eddd68d | 2002-03-29 16:21:44 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 275 | # Comparisons: |
| 276 | print 'Testing Unicode comparisons...', |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 277 | verify(u'abc' == 'abc') |
| 278 | verify('abc' == u'abc') |
| 279 | verify(u'abc' == u'abc') |
| 280 | verify(u'abcd' > 'abc') |
| 281 | verify('abcd' > u'abc') |
| 282 | verify(u'abcd' > u'abc') |
| 283 | verify(u'abc' < 'abcd') |
| 284 | verify('abc' < u'abcd') |
| 285 | verify(u'abc' < u'abcd') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 286 | print 'done.' |
| 287 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 288 | if 0: |
| 289 | # Move these tests to a Unicode collation module test... |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 290 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 291 | print 'Testing UTF-16 code point order comparisons...', |
| 292 | #No surrogates, no fixup required. |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 293 | verify(u'\u0061' < u'\u20ac') |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 294 | # Non surrogate below surrogate value, no fixup required |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 295 | verify(u'\u0061' < u'\ud800\udc02') |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 296 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 297 | # Non surrogate above surrogate value, fixup required |
| 298 | def test_lecmp(s, s2): |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 299 | verify(s < s2 , "comparison failed on %s < %s" % (s, s2)) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 300 | |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 301 | def test_fixup(s): |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 302 | s2 = u'\ud800\udc01' |
| 303 | test_lecmp(s, s2) |
| 304 | s2 = u'\ud900\udc01' |
| 305 | test_lecmp(s, s2) |
| 306 | s2 = u'\uda00\udc01' |
| 307 | test_lecmp(s, s2) |
| 308 | s2 = u'\udb00\udc01' |
| 309 | test_lecmp(s, s2) |
| 310 | s2 = u'\ud800\udd01' |
| 311 | test_lecmp(s, s2) |
| 312 | s2 = u'\ud900\udd01' |
| 313 | test_lecmp(s, s2) |
| 314 | s2 = u'\uda00\udd01' |
| 315 | test_lecmp(s, s2) |
| 316 | s2 = u'\udb00\udd01' |
| 317 | test_lecmp(s, s2) |
| 318 | s2 = u'\ud800\ude01' |
| 319 | test_lecmp(s, s2) |
| 320 | s2 = u'\ud900\ude01' |
| 321 | test_lecmp(s, s2) |
| 322 | s2 = u'\uda00\ude01' |
| 323 | test_lecmp(s, s2) |
| 324 | s2 = u'\udb00\ude01' |
| 325 | test_lecmp(s, s2) |
| 326 | s2 = u'\ud800\udfff' |
| 327 | test_lecmp(s, s2) |
| 328 | s2 = u'\ud900\udfff' |
| 329 | test_lecmp(s, s2) |
| 330 | s2 = u'\uda00\udfff' |
| 331 | test_lecmp(s, s2) |
| 332 | s2 = u'\udb00\udfff' |
| 333 | test_lecmp(s, s2) |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 334 | |
| 335 | test_fixup(u'\ue000') |
| 336 | test_fixup(u'\uff61') |
| 337 | |
| 338 | # Surrogates on both sides, no fixup required |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 339 | verify(u'\ud800\udc02' < u'\ud84d\udc56') |
Marc-André Lemburg | e503437 | 2000-08-08 08:04:29 +0000 | [diff] [blame] | 340 | print 'done.' |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 342 | test('ljust', u'abc', u'abc ', 10) |
| 343 | test('rjust', u'abc', u' abc', 10) |
| 344 | test('center', u'abc', u' abc ', 10) |
| 345 | test('ljust', u'abc', u'abc ', 6) |
| 346 | test('rjust', u'abc', u' abc', 6) |
| 347 | test('center', u'abc', u' abc ', 6) |
| 348 | test('ljust', u'abc', u'abc', 2) |
| 349 | test('rjust', u'abc', u'abc', 2) |
| 350 | test('center', u'abc', u'abc', 2) |
| 351 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 352 | test('islower', u'a', True) |
| 353 | test('islower', u'A', False) |
| 354 | test('islower', u'\n', False) |
| 355 | test('islower', u'\u1FFc', False) |
| 356 | test('islower', u'abc', True) |
| 357 | test('islower', u'aBc', False) |
| 358 | test('islower', u'abc\n', True) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 359 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 360 | test('isupper', u'a', False) |
| 361 | test('isupper', u'A', True) |
| 362 | test('isupper', u'\n', False) |
Marc-André Lemburg | ef0a032 | 2001-02-10 14:09:31 +0000 | [diff] [blame] | 363 | if sys.platform[:4] != 'java': |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 364 | test('isupper', u'\u1FFc', False) |
| 365 | test('isupper', u'ABC', True) |
| 366 | test('isupper', u'AbC', False) |
| 367 | test('isupper', u'ABC\n', True) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 369 | test('istitle', u'a', False) |
| 370 | test('istitle', u'A', True) |
| 371 | test('istitle', u'\n', False) |
| 372 | test('istitle', u'\u1FFc', True) |
| 373 | test('istitle', u'A Titlecased Line', True) |
| 374 | test('istitle', u'A\nTitlecased Line', True) |
| 375 | test('istitle', u'A Titlecased, Line', True) |
| 376 | test('istitle', u'Greek \u1FFcitlecases ...', True) |
| 377 | test('istitle', u'Not a capitalized String', False) |
| 378 | test('istitle', u'Not\ta Titlecase String', False) |
| 379 | test('istitle', u'Not--a Titlecase String', False) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 380 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 381 | test('isalpha', u'a', True) |
| 382 | test('isalpha', u'A', True) |
| 383 | test('isalpha', u'\n', False) |
| 384 | test('isalpha', u'\u1FFc', True) |
| 385 | test('isalpha', u'abc', True) |
| 386 | test('isalpha', u'aBc123', False) |
| 387 | test('isalpha', u'abc\n', False) |
Marc-André Lemburg | 9d46741 | 2000-07-05 09:46:40 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 389 | test('isalnum', u'a', True) |
| 390 | test('isalnum', u'A', True) |
| 391 | test('isalnum', u'\n', False) |
| 392 | test('isalnum', u'123abc456', True) |
| 393 | test('isalnum', u'a1b3c', True) |
| 394 | test('isalnum', u'aBc000 ', False) |
| 395 | test('isalnum', u'abc\n', False) |
Marc-André Lemburg | 9d46741 | 2000-07-05 09:46:40 +0000 | [diff] [blame] | 396 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 397 | test('splitlines', u"abc\ndef\n\rghi", [u'abc', u'def', u'', u'ghi']) |
| 398 | test('splitlines', u"abc\ndef\n\r\nghi", [u'abc', u'def', u'', u'ghi']) |
| 399 | test('splitlines', u"abc\ndef\r\nghi", [u'abc', u'def', u'ghi']) |
| 400 | test('splitlines', u"abc\ndef\r\nghi\n", [u'abc', u'def', u'ghi']) |
| 401 | test('splitlines', u"abc\ndef\r\nghi\n\r", [u'abc', u'def', u'ghi', u'']) |
| 402 | test('splitlines', u"\nabc\ndef\r\nghi\n\r", [u'', u'abc', u'def', u'ghi', u'']) |
Guido van Rossum | 77f6a65 | 2002-04-03 22:41:51 +0000 | [diff] [blame] | 403 | test('splitlines', u"\nabc\ndef\r\nghi\n\r", [u'\n', u'abc\n', u'def\r\n', u'ghi\n', u'\r'], True) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 404 | |
| 405 | test('translate', u"abababc", u'bbbc', {ord('a'):None}) |
| 406 | test('translate', u"abababc", u'iiic', {ord('a'):None, ord('b'):ord('i')}) |
| 407 | test('translate', u"abababc", u'iiix', {ord('a'):None, ord('b'):ord('i'), ord('c'):u'x'}) |
Walter Dörwald | 5c1ee17 | 2002-09-04 20:31:32 +0000 | [diff] [blame] | 408 | test('translate', u"abababc", u'<i><i><i>c', {ord('a'):None, ord('b'):u'<i>'}) |
| 409 | test('translate', u"abababc", u'c', {ord('a'):None, ord('b'):u''}) |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 410 | |
Guido van Rossum | d4d2684 | 2000-03-13 23:21:48 +0000 | [diff] [blame] | 411 | # Contains: |
| 412 | print 'Testing Unicode contains method...', |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 413 | vereq(('a' in u'abdb'), True) |
| 414 | vereq(('a' in u'bdab'), True) |
| 415 | vereq(('a' in u'bdaba'), True) |
| 416 | vereq(('a' in u'bdba'), True) |
| 417 | vereq(('a' in u'bdba'), True) |
| 418 | vereq((u'a' in u'bdba'), True) |
| 419 | vereq((u'a' in u'bdb'), False) |
| 420 | vereq((u'a' in 'bdb'), False) |
| 421 | vereq((u'a' in 'bdba'), True) |
| 422 | vereq((u'a' in ('a',1,None)), True) |
| 423 | vereq((u'a' in (1,None,'a')), True) |
| 424 | vereq((u'a' in (1,None,u'a')), True) |
| 425 | vereq(('a' in ('a',1,None)), True) |
| 426 | vereq(('a' in (1,None,'a')), True) |
| 427 | vereq(('a' in (1,None,u'a')), True) |
| 428 | vereq(('a' in ('x',1,u'y')), False) |
| 429 | vereq(('a' in ('x',1,None)), False) |
Barry Warsaw | e067417 | 2002-08-06 19:03:56 +0000 | [diff] [blame] | 430 | vereq(u'abcd' in u'abcxxxx', False) |
Raymond Hettinger | ca84d65 | 2002-08-06 23:08:51 +0000 | [diff] [blame] | 431 | vereq((u'ab' in u'abcd'), True) |
| 432 | vereq(('ab' in u'abc'), True) |
| 433 | vereq((u'ab' in 'abc'), True) |
| 434 | vereq((u'ab' in (1,None,u'ab')), True) |
| 435 | vereq((u'' in u'abc'), True) |
| 436 | vereq(('' in u'abc'), True) |
Guido van Rossum | d4d2684 | 2000-03-13 23:21:48 +0000 | [diff] [blame] | 437 | print 'done.' |
| 438 | |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 439 | # Formatting: |
| 440 | print 'Testing Unicode formatting strings...', |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 441 | verify(u"%s, %s" % (u"abc", "abc") == u'abc, abc') |
| 442 | verify(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, 2, 3) == u'abc, abc, 1, 2.000000, 3.00') |
| 443 | verify(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", 1, -2, 3) == u'abc, abc, 1, -2.000000, 3.00') |
| 444 | verify(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.5) == u'abc, abc, -1, -2.000000, 3.50') |
| 445 | verify(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 3.57) == u'abc, abc, -1, -2.000000, 3.57') |
| 446 | verify(u"%s, %s, %i, %f, %5.2f" % (u"abc", "abc", -1, -2, 1003.57) == u'abc, abc, -1, -2.000000, 1003.57') |
| 447 | verify(u"%c" % (u"a",) == u'a') |
| 448 | verify(u"%c" % ("a",) == u'a') |
| 449 | verify(u"%c" % (34,) == u'"') |
| 450 | verify(u"%c" % (36,) == u'$') |
Marc-André Lemburg | ef0a032 | 2001-02-10 14:09:31 +0000 | [diff] [blame] | 451 | if sys.platform[:4] != 'java': |
| 452 | value = u"%r, %r" % (u"abc", "abc") |
| 453 | if value != u"u'abc', 'abc'": |
| 454 | print '*** formatting failed for "%s"' % 'u"%r, %r" % (u"abc", "abc")' |
Marc-André Lemburg | 8462573 | 2000-06-13 12:05:36 +0000 | [diff] [blame] | 455 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 456 | verify(u"%(x)s, %(y)s" % {'x':u"abc", 'y':"def"} == u'abc, def') |
Marc-André Lemburg | 8462573 | 2000-06-13 12:05:36 +0000 | [diff] [blame] | 457 | try: |
Marc-André Lemburg | 72f8213 | 2001-11-20 15:18:49 +0000 | [diff] [blame] | 458 | value = u"%(x)s, %(ä)s" % {'x':u"abc", u'ä':"def"} |
Marc-André Lemburg | 8462573 | 2000-06-13 12:05:36 +0000 | [diff] [blame] | 459 | except KeyError: |
| 460 | print '*** formatting failed for "%s"' % "u'abc, def'" |
| 461 | else: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 462 | verify(value == u'abc, def') |
Marc-André Lemburg | 8462573 | 2000-06-13 12:05:36 +0000 | [diff] [blame] | 463 | |
Martin v. Löwis | 766e300 | 2002-09-14 09:10:04 +0000 | [diff] [blame] | 464 | for ordinal in (-100, 0x200000): |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 465 | try: |
| 466 | u"%c" % ordinal |
| 467 | except ValueError: |
| 468 | pass |
| 469 | else: |
Martin v. Löwis | 766e300 | 2002-09-14 09:10:04 +0000 | [diff] [blame] | 470 | print '*** formatting u"%%c" %% %i should give a ValueError' % ordinal |
Marc-André Lemburg | cc8764c | 2002-08-11 12:23:04 +0000 | [diff] [blame] | 471 | |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 472 | # formatting jobs delegated from the string implementation: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 473 | verify('...%(foo)s...' % {'foo':u"abc"} == u'...abc...') |
| 474 | verify('...%(foo)s...' % {'foo':"abc"} == '...abc...') |
| 475 | verify('...%(foo)s...' % {u'foo':"abc"} == '...abc...') |
| 476 | verify('...%(foo)s...' % {u'foo':u"abc"} == u'...abc...') |
| 477 | verify('...%(foo)s...' % {u'foo':u"abc",'def':123} == u'...abc...') |
| 478 | verify('...%(foo)s...' % {u'foo':u"abc",u'def':123} == u'...abc...') |
| 479 | verify('...%s...%s...%s...%s...' % (1,2,3,u"abc") == u'...1...2...3...abc...') |
| 480 | verify('...%%...%%s...%s...%s...%s...%s...' % (1,2,3,u"abc") == u'...%...%s...1...2...3...abc...') |
| 481 | verify('...%s...' % u"abc" == u'...abc...') |
Marc-André Lemburg | 542fe56 | 2001-05-02 14:21:53 +0000 | [diff] [blame] | 482 | verify('%*s' % (5,u'abc',) == u' abc') |
| 483 | verify('%*s' % (-5,u'abc',) == u'abc ') |
| 484 | verify('%*.*s' % (5,2,u'abc',) == u' ab') |
| 485 | verify('%*.*s' % (5,3,u'abc',) == u' abc') |
| 486 | verify('%i %*.*s' % (10, 5,3,u'abc',) == u'10 abc') |
| 487 | verify('%i%s %*.*s' % (10, 3, 5,3,u'abc',) == u'103 abc') |
Guido van Rossum | a831cac | 2000-03-10 23:23:21 +0000 | [diff] [blame] | 488 | print 'done.' |
| 489 | |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 490 | print 'Testing builtin unicode()...', |
| 491 | |
| 492 | # unicode(obj) tests (this maps to PyObject_Unicode() at C level) |
| 493 | |
| 494 | verify(unicode(u'unicode remains unicode') == u'unicode remains unicode') |
| 495 | |
| 496 | class UnicodeSubclass(unicode): |
| 497 | pass |
| 498 | |
| 499 | verify(unicode(UnicodeSubclass('unicode subclass becomes unicode')) |
| 500 | == u'unicode subclass becomes unicode') |
| 501 | |
| 502 | verify(unicode('strings are converted to unicode') |
| 503 | == u'strings are converted to unicode') |
| 504 | |
| 505 | class UnicodeCompat: |
| 506 | def __init__(self, x): |
| 507 | self.x = x |
| 508 | def __unicode__(self): |
| 509 | return self.x |
| 510 | |
| 511 | verify(unicode(UnicodeCompat('__unicode__ compatible objects are recognized')) |
| 512 | == u'__unicode__ compatible objects are recognized') |
| 513 | |
| 514 | class StringCompat: |
| 515 | def __init__(self, x): |
| 516 | self.x = x |
| 517 | def __str__(self): |
| 518 | return self.x |
| 519 | |
| 520 | verify(unicode(StringCompat('__str__ compatible objects are recognized')) |
| 521 | == u'__str__ compatible objects are recognized') |
| 522 | |
| 523 | # unicode(obj) is compatible to str(): |
| 524 | |
| 525 | o = StringCompat('unicode(obj) is compatible to str()') |
| 526 | verify(unicode(o) == u'unicode(obj) is compatible to str()') |
| 527 | verify(str(o) == 'unicode(obj) is compatible to str()') |
| 528 | |
| 529 | for obj in (123, 123.45, 123L): |
| 530 | verify(unicode(obj) == unicode(str(obj))) |
| 531 | |
| 532 | # unicode(obj, encoding, error) tests (this maps to |
| 533 | # PyUnicode_FromEncodedObject() at C level) |
| 534 | |
Finn Bock | 2b29cb2 | 2001-12-10 20:57:34 +0000 | [diff] [blame] | 535 | if not sys.platform.startswith('java'): |
| 536 | try: |
| 537 | unicode(u'decoding unicode is not supported', 'utf-8', 'strict') |
| 538 | except TypeError: |
| 539 | pass |
| 540 | else: |
| 541 | raise TestFailed, "decoding unicode should NOT be supported" |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 542 | |
| 543 | verify(unicode('strings are decoded to unicode', 'utf-8', 'strict') |
| 544 | == u'strings are decoded to unicode') |
| 545 | |
Finn Bock | 2b29cb2 | 2001-12-10 20:57:34 +0000 | [diff] [blame] | 546 | if not sys.platform.startswith('java'): |
| 547 | verify(unicode(buffer('character buffers are decoded to unicode'), |
| 548 | 'utf-8', 'strict') |
| 549 | == u'character buffers are decoded to unicode') |
Marc-André Lemburg | b5507ec | 2001-10-19 12:02:29 +0000 | [diff] [blame] | 550 | |
| 551 | print 'done.' |
| 552 | |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 553 | # Test builtin codecs |
| 554 | print 'Testing builtin codecs...', |
| 555 | |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 556 | # UTF-7 specific encoding tests: |
| 557 | utfTests = [(u'A\u2262\u0391.', 'A+ImIDkQ.'), # RFC2152 example |
| 558 | (u'Hi Mom -\u263a-!', 'Hi Mom -+Jjo--!'), # RFC2152 example |
| 559 | (u'\u65E5\u672C\u8A9E', '+ZeVnLIqe-'), # RFC2152 example |
| 560 | (u'Item 3 is \u00a31.', 'Item 3 is +AKM-1.'), # RFC2152 example |
| 561 | (u'+', '+-'), |
| 562 | (u'+-', '+--'), |
| 563 | (u'+?', '+-?'), |
| 564 | (u'\?', '+AFw?'), |
| 565 | (u'+?', '+-?'), |
| 566 | (ur'\\?', '+AFwAXA?'), |
| 567 | (ur'\\\?', '+AFwAXABc?'), |
| 568 | (ur'++--', '+-+---')] |
| 569 | |
| 570 | for x,y in utfTests: |
| 571 | verify( x.encode('utf-7') == y ) |
| 572 | |
Tim Peters | 527e64f | 2001-10-04 05:36:56 +0000 | [diff] [blame] | 573 | try: |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 574 | unicode('+3ADYAA-', 'utf-7') # surrogates not supported |
| 575 | except UnicodeError: |
| 576 | pass |
| 577 | else: |
| 578 | raise TestFailed, "unicode('+3ADYAA-', 'utf-7') failed to raise an exception" |
| 579 | |
| 580 | verify(unicode('+3ADYAA-', 'utf-7', 'replace') == u'\ufffd') |
| 581 | |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 582 | # UTF-8 specific encoding tests: |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 583 | verify(u''.encode('utf-8') == '') |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 584 | verify(u'\u20ac'.encode('utf-8') == '\xe2\x82\xac') |
| 585 | verify(u'\ud800\udc02'.encode('utf-8') == '\xf0\x90\x80\x82') |
| 586 | verify(u'\ud84d\udc56'.encode('utf-8') == '\xf0\xa3\x91\x96') |
| 587 | verify(u'\ud800'.encode('utf-8') == '\xed\xa0\x80') |
| 588 | verify(u'\udc00'.encode('utf-8') == '\xed\xb0\x80') |
| 589 | verify((u'\ud800\udc02'*1000).encode('utf-8') == |
| 590 | '\xf0\x90\x80\x82'*1000) |
Marc-André Lemburg | ce0b664 | 2002-04-10 17:18:02 +0000 | [diff] [blame] | 591 | verify(u'\u6b63\u78ba\u306b\u8a00\u3046\u3068\u7ffb\u8a33\u306f' |
| 592 | u'\u3055\u308c\u3066\u3044\u307e\u305b\u3093\u3002\u4e00' |
| 593 | u'\u90e8\u306f\u30c9\u30a4\u30c4\u8a9e\u3067\u3059\u304c' |
| 594 | u'\u3001\u3042\u3068\u306f\u3067\u305f\u3089\u3081\u3067' |
| 595 | u'\u3059\u3002\u5b9f\u969b\u306b\u306f\u300cWenn ist das' |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 596 | u' Nunstuck git und'.encode('utf-8') == |
Marc-André Lemburg | ce0b664 | 2002-04-10 17:18:02 +0000 | [diff] [blame] | 597 | '\xe6\xad\xa3\xe7\xa2\xba\xe3\x81\xab\xe8\xa8\x80\xe3\x81' |
| 598 | '\x86\xe3\x81\xa8\xe7\xbf\xbb\xe8\xa8\xb3\xe3\x81\xaf\xe3' |
| 599 | '\x81\x95\xe3\x82\x8c\xe3\x81\xa6\xe3\x81\x84\xe3\x81\xbe' |
| 600 | '\xe3\x81\x9b\xe3\x82\x93\xe3\x80\x82\xe4\xb8\x80\xe9\x83' |
| 601 | '\xa8\xe3\x81\xaf\xe3\x83\x89\xe3\x82\xa4\xe3\x83\x84\xe8' |
| 602 | '\xaa\x9e\xe3\x81\xa7\xe3\x81\x99\xe3\x81\x8c\xe3\x80\x81' |
| 603 | '\xe3\x81\x82\xe3\x81\xa8\xe3\x81\xaf\xe3\x81\xa7\xe3\x81' |
| 604 | '\x9f\xe3\x82\x89\xe3\x82\x81\xe3\x81\xa7\xe3\x81\x99\xe3' |
| 605 | '\x80\x82\xe5\xae\x9f\xe9\x9a\x9b\xe3\x81\xab\xe3\x81\xaf' |
| 606 | '\xe3\x80\x8cWenn ist das Nunstuck git und') |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 607 | |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 608 | # UTF-8 specific decoding tests |
Marc-André Lemburg | 3688a88 | 2002-02-06 18:09:02 +0000 | [diff] [blame] | 609 | verify(unicode('\xf0\xa3\x91\x96', 'utf-8') == u'\U00023456' ) |
| 610 | verify(unicode('\xf0\x90\x80\x82', 'utf-8') == u'\U00010002' ) |
| 611 | verify(unicode('\xe2\x82\xac', 'utf-8') == u'\u20ac' ) |
Marc-André Lemburg | d6d06ad | 2000-07-07 17:48:52 +0000 | [diff] [blame] | 612 | |
| 613 | # Other possible utf-8 test cases: |
| 614 | # * strict decoding testing for all of the |
| 615 | # UTF8_ERROR cases in PyUnicode_DecodeUTF8 |
| 616 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 617 | verify(unicode('hello','ascii') == u'hello') |
| 618 | verify(unicode('hello','utf-8') == u'hello') |
| 619 | verify(unicode('hello','utf8') == u'hello') |
| 620 | verify(unicode('hello','latin-1') == u'hello') |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 621 | |
Marc-André Lemburg | 6871f6a | 2001-09-20 12:53:16 +0000 | [diff] [blame] | 622 | # Error handling |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 623 | try: |
| 624 | u'Andr\202 x'.encode('ascii') |
| 625 | u'Andr\202 x'.encode('ascii','strict') |
| 626 | except ValueError: |
| 627 | pass |
| 628 | else: |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 629 | raise TestFailed, "u'Andr\202'.encode('ascii') failed to raise an exception" |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 630 | verify(u'Andr\202 x'.encode('ascii','ignore') == "Andr x") |
| 631 | verify(u'Andr\202 x'.encode('ascii','replace') == "Andr? x") |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 632 | |
| 633 | try: |
| 634 | unicode('Andr\202 x','ascii') |
| 635 | unicode('Andr\202 x','ascii','strict') |
| 636 | except ValueError: |
| 637 | pass |
| 638 | else: |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 639 | raise TestFailed, "unicode('Andr\202') failed to raise an exception" |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 640 | verify(unicode('Andr\202 x','ascii','ignore') == u"Andr x") |
| 641 | verify(unicode('Andr\202 x','ascii','replace') == u'Andr\uFFFD x') |
Guido van Rossum | 9706486 | 2000-04-10 13:52:48 +0000 | [diff] [blame] | 642 | |
Martin v. Löwis | 047c05e | 2002-03-21 08:55:28 +0000 | [diff] [blame] | 643 | verify("\\N{foo}xx".decode("unicode-escape", "ignore") == u"xx") |
| 644 | try: |
| 645 | "\\".decode("unicode-escape") |
| 646 | except ValueError: |
| 647 | pass |
| 648 | else: |
| 649 | raise TestFailed, '"\\".decode("unicode-escape") should fail' |
| 650 | |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 651 | verify(u'hello'.encode('ascii') == 'hello') |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 652 | verify(u'hello'.encode('utf-7') == 'hello') |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 653 | verify(u'hello'.encode('utf-8') == 'hello') |
| 654 | verify(u'hello'.encode('utf8') == 'hello') |
| 655 | verify(u'hello'.encode('utf-16-le') == 'h\000e\000l\000l\000o\000') |
| 656 | verify(u'hello'.encode('utf-16-be') == '\000h\000e\000l\000l\000o') |
| 657 | verify(u'hello'.encode('latin-1') == 'hello') |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 658 | |
Marc-André Lemburg | 6c6bfb7 | 2001-07-20 17:39:11 +0000 | [diff] [blame] | 659 | # Roundtrip safety for BMP (just the first 1024 chars) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 660 | u = u''.join(map(unichr, range(1024))) |
Marc-André Lemburg | c60e6f7 | 2001-09-20 10:35:46 +0000 | [diff] [blame] | 661 | for encoding in ('utf-7', 'utf-8', 'utf-16', 'utf-16-le', 'utf-16-be', |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 662 | 'raw_unicode_escape', 'unicode_escape', 'unicode_internal'): |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 663 | verify(unicode(u.encode(encoding),encoding) == u) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 664 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 665 | # Roundtrip safety for BMP (just the first 256 chars) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 666 | u = u''.join(map(unichr, range(256))) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 667 | for encoding in ( |
| 668 | 'latin-1', |
| 669 | ): |
| 670 | try: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 671 | verify(unicode(u.encode(encoding),encoding) == u) |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 672 | except TestFailed: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 673 | print '*** codec "%s" failed round-trip' % encoding |
| 674 | except ValueError,why: |
| 675 | print '*** codec for "%s" failed: %s' % (encoding, why) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 676 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 677 | # Roundtrip safety for BMP (just the first 128 chars) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 678 | u = u''.join(map(unichr, range(128))) |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 679 | for encoding in ( |
| 680 | 'ascii', |
| 681 | ): |
| 682 | try: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 683 | verify(unicode(u.encode(encoding),encoding) == u) |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 684 | except TestFailed: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 685 | print '*** codec "%s" failed round-trip' % encoding |
| 686 | except ValueError,why: |
| 687 | print '*** codec for "%s" failed: %s' % (encoding, why) |
| 688 | |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 689 | # Roundtrip safety for non-BMP (just a few chars) |
| 690 | u = u'\U00010001\U00020002\U00030003\U00040004\U00050005' |
| 691 | for encoding in ('utf-8', |
| 692 | 'utf-16', 'utf-16-le', 'utf-16-be', |
| 693 | #'raw_unicode_escape', |
| 694 | 'unicode_escape', 'unicode_internal'): |
| 695 | verify(unicode(u.encode(encoding),encoding) == u) |
| 696 | |
| 697 | # UTF-8 must be roundtrip safe for all UCS-2 code points |
Martin v. Löwis | 1ce4ae3 | 2002-09-14 09:19:53 +0000 | [diff] [blame] | 698 | # This excludes surrogates: in the full range, there would be |
| 699 | # a surrogate pair (\udbff\udc00), which gets converted back |
| 700 | # to a non-BMP character (\U0010fc00) |
| 701 | u = u''.join(map(unichr, range(0,0xd800)+range(0xe000,0x10000))) |
Marc-André Lemburg | bd3be8f | 2002-02-07 11:33:49 +0000 | [diff] [blame] | 702 | for encoding in ('utf-8',): |
| 703 | verify(unicode(u.encode(encoding),encoding) == u) |
| 704 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 705 | print 'done.' |
| 706 | |
| 707 | print 'Testing standard mapping codecs...', |
| 708 | |
| 709 | print '0-127...', |
| 710 | s = ''.join(map(chr, range(128))) |
| 711 | for encoding in ( |
| 712 | 'cp037', 'cp1026', |
| 713 | 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', |
| 714 | 'cp852', 'cp855', 'cp860', 'cp861', 'cp862', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 715 | 'cp863', 'cp865', 'cp866', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 716 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
| 717 | 'iso8859_2', 'iso8859_3', 'iso8859_4', 'iso8859_5', 'iso8859_6', |
| 718 | 'iso8859_7', 'iso8859_9', 'koi8_r', 'latin_1', |
| 719 | 'mac_cyrillic', 'mac_latin2', |
| 720 | |
| 721 | 'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 722 | 'cp1256', 'cp1257', 'cp1258', |
| 723 | 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
| 724 | |
| 725 | 'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 726 | 'cp1006', 'iso8859_8', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 727 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 728 | ### These have undefined mappings: |
| 729 | #'cp424', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 730 | |
Tim Peters | 2f228e7 | 2001-05-13 00:19:31 +0000 | [diff] [blame] | 731 | ### These fail the round-trip: |
| 732 | #'cp875' |
| 733 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 734 | ): |
| 735 | try: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 736 | verify(unicode(s,encoding).encode(encoding) == s) |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 737 | except TestFailed: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 738 | print '*** codec "%s" failed round-trip' % encoding |
| 739 | except ValueError,why: |
| 740 | print '*** codec for "%s" failed: %s' % (encoding, why) |
| 741 | |
| 742 | print '128-255...', |
| 743 | s = ''.join(map(chr, range(128,256))) |
| 744 | for encoding in ( |
| 745 | 'cp037', 'cp1026', |
| 746 | 'cp437', 'cp500', 'cp737', 'cp775', 'cp850', |
| 747 | 'cp852', 'cp855', 'cp860', 'cp861', 'cp862', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 748 | 'cp863', 'cp865', 'cp866', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 749 | 'iso8859_10', 'iso8859_13', 'iso8859_14', 'iso8859_15', |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 750 | 'iso8859_2', 'iso8859_4', 'iso8859_5', |
Marc-André Lemburg | a866df8 | 2001-01-03 21:29:14 +0000 | [diff] [blame] | 751 | 'iso8859_9', 'koi8_r', 'latin_1', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 752 | 'mac_cyrillic', 'mac_latin2', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 753 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 754 | ### These have undefined mappings: |
| 755 | #'cp1250', 'cp1251', 'cp1252', 'cp1253', 'cp1254', 'cp1255', |
| 756 | #'cp1256', 'cp1257', 'cp1258', |
| 757 | #'cp424', 'cp856', 'cp857', 'cp864', 'cp869', 'cp874', |
Tim Peters | d2bf3b7 | 2001-01-18 02:22:22 +0000 | [diff] [blame] | 758 | #'iso8859_3', 'iso8859_6', 'iso8859_7', |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 759 | #'mac_greek', 'mac_iceland','mac_roman', 'mac_turkish', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 760 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 761 | ### These fail the round-trip: |
| 762 | #'cp1006', 'cp875', 'iso8859_8', |
Fred Drake | 004d5e6 | 2000-10-23 17:22:08 +0000 | [diff] [blame] | 763 | |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 764 | ): |
| 765 | try: |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 766 | verify(unicode(s,encoding).encode(encoding) == s) |
Guido van Rossum | a1374e4 | 2001-01-19 19:01:56 +0000 | [diff] [blame] | 767 | except TestFailed: |
Guido van Rossum | 9e896b3 | 2000-04-05 20:11:21 +0000 | [diff] [blame] | 768 | print '*** codec "%s" failed round-trip' % encoding |
| 769 | except ValueError,why: |
| 770 | print '*** codec for "%s" failed: %s' % (encoding, why) |
Guido van Rossum | d8855fd | 2000-03-24 22:14:19 +0000 | [diff] [blame] | 771 | |
| 772 | print 'done.' |
Fred Drake | e0243e2 | 2000-04-13 14:11:56 +0000 | [diff] [blame] | 773 | |
| 774 | print 'Testing Unicode string concatenation...', |
Marc-André Lemburg | 3661908 | 2001-01-17 19:11:13 +0000 | [diff] [blame] | 775 | verify((u"abc" u"def") == u"abcdef") |
| 776 | verify(("abc" u"def") == u"abcdef") |
| 777 | verify((u"abc" "def") == u"abcdef") |
| 778 | verify((u"abc" u"def" "ghi") == u"abcdefghi") |
| 779 | verify(("abc" "def" u"ghi") == u"abcdefghi") |
Fred Drake | e0243e2 | 2000-04-13 14:11:56 +0000 | [diff] [blame] | 780 | print 'done.' |
Marc-André Lemburg | 0c4d8d0 | 2001-11-20 15:17:25 +0000 | [diff] [blame] | 781 | |
| 782 | print 'Testing Unicode printing...', |
| 783 | print u'abc' |
| 784 | print u'abc', u'def' |
| 785 | print u'abc', 'def' |
| 786 | print 'abc', u'def' |
| 787 | print u'abc\n' |
| 788 | print u'abc\n', |
| 789 | print u'abc\n', |
| 790 | print u'def\n' |
| 791 | print u'def\n' |
| 792 | print 'done.' |
Barry Warsaw | 817918c | 2002-08-06 16:58:21 +0000 | [diff] [blame] | 793 | |
| 794 | def test_exception(lhs, rhs, msg): |
| 795 | try: |
| 796 | lhs in rhs |
| 797 | except TypeError: |
| 798 | pass |
| 799 | else: |
| 800 | raise TestFailed, msg |
| 801 | |
| 802 | def run_contains_tests(): |
| 803 | vereq(u'' in '', True) |
| 804 | vereq('' in u'', True) |
| 805 | vereq(u'' in u'', True) |
| 806 | vereq(u'' in 'abc', True) |
| 807 | vereq('' in u'abc', True) |
| 808 | vereq(u'' in u'abc', True) |
| 809 | vereq(u'\0' in 'abc', False) |
| 810 | vereq('\0' in u'abc', False) |
| 811 | vereq(u'\0' in u'abc', False) |
| 812 | vereq(u'\0' in '\0abc', True) |
| 813 | vereq('\0' in u'\0abc', True) |
| 814 | vereq(u'\0' in u'\0abc', True) |
| 815 | vereq(u'\0' in 'abc\0', True) |
| 816 | vereq('\0' in u'abc\0', True) |
| 817 | vereq(u'\0' in u'abc\0', True) |
| 818 | vereq(u'a' in '\0abc', True) |
| 819 | vereq('a' in u'\0abc', True) |
| 820 | vereq(u'a' in u'\0abc', True) |
| 821 | vereq(u'asdf' in 'asdf', True) |
| 822 | vereq('asdf' in u'asdf', True) |
| 823 | vereq(u'asdf' in u'asdf', True) |
| 824 | vereq(u'asdf' in 'asd', False) |
| 825 | vereq('asdf' in u'asd', False) |
| 826 | vereq(u'asdf' in u'asd', False) |
| 827 | vereq(u'asdf' in '', False) |
| 828 | vereq('asdf' in u'', False) |
| 829 | vereq(u'asdf' in u'', False) |
| 830 | |
| 831 | run_contains_tests() |