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