test_b1.py 18.9 KB
Newer Older
Guido van Rossum's avatar
Guido van Rossum committed
1 2 3 4
# Python test set -- part 4a, built-in functions a-m

from test_support import *

5 6
print '__import__'
__import__('sys')
7
__import__('time')
8 9 10 11 12
__import__('string')
try: __import__('spamspam')
except ImportError: pass
else: raise TestFailed, "__import__('spamspam') should fail"

Guido van Rossum's avatar
Guido van Rossum committed
13
print 'abs'
14 15 16
if abs(0) != 0: raise TestFailed, 'abs(0)'
if abs(1234) != 1234: raise TestFailed, 'abs(1234)'
if abs(-1234) != 1234: raise TestFailed, 'abs(-1234)'
Guido van Rossum's avatar
Guido van Rossum committed
17
#
18 19 20
if abs(0.0) != 0.0: raise TestFailed, 'abs(0.0)'
if abs(3.14) != 3.14: raise TestFailed, 'abs(3.14)'
if abs(-3.14) != 3.14: raise TestFailed, 'abs(-3.14)'
Guido van Rossum's avatar
Guido van Rossum committed
21
#
22 23 24
if abs(0L) != 0L: raise TestFailed, 'abs(0L)'
if abs(1234L) != 1234L: raise TestFailed, 'abs(1234L)'
if abs(-1234L) != 1234L: raise TestFailed, 'abs(-1234L)'
Guido van Rossum's avatar
Guido van Rossum committed
25 26 27

print 'apply'
def f0(*args):
28
    if args != (): raise TestFailed, 'f0 called with ' + `args`
Guido van Rossum's avatar
Guido van Rossum committed
29
def f1(a1):
30
    if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
Guido van Rossum's avatar
Guido van Rossum committed
31
def f2(a1, a2):
32 33
    if a1 != 1 or a2 != 2:
        raise TestFailed, 'f2 called with ' + `a1, a2`
Guido van Rossum's avatar
Guido van Rossum committed
34
def f3(a1, a2, a3):
35 36
    if a1 != 1 or a2 != 2 or a3 != 3:
        raise TestFailed, 'f3 called with ' + `a1, a2, a3`
Guido van Rossum's avatar
Guido van Rossum committed
37 38 39 40 41
apply(f0, ())
apply(f1, (1,))
apply(f2, (1, 2))
apply(f3, (1, 2, 3))

42 43 44 45 46 47 48 49 50 51 52
# A PyCFunction that takes only positional parameters should allow an
# empty keyword dictionary to pass without a complaint, but raise a
# TypeError if the dictionary is non-empty.
apply(id, (1,), {})
try:
    apply(id, (1,), {"foo": 1})
except TypeError:
    pass
else:
    raise TestFailed, 'expected TypeError; no exception raised'

53 54 55 56 57
print 'callable'
if not callable(len):raise TestFailed, 'callable(len)'
def f(): pass
if not callable(f): raise TestFailed, 'callable(f)'
class C:
58
    def meth(self): pass
59 60 61 62 63
if not callable(C): raise TestFailed, 'callable(C)'
x = C()
if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
if callable(x): raise TestFailed, 'callable(x)'
class D(C):
64
    def __call__(self): pass
65 66
y = D()
if not callable(y): raise TestFailed, 'callable(y)'
67
y()
68

Guido van Rossum's avatar
Guido van Rossum committed
69
print 'chr'
70 71 72
if chr(32) != ' ': raise TestFailed, 'chr(32)'
if chr(65) != 'A': raise TestFailed, 'chr(65)'
if chr(97) != 'a': raise TestFailed, 'chr(97)'
Guido van Rossum's avatar
Guido van Rossum committed
73

74
# cmp
75
print 'cmp'
76 77 78
if cmp(-1, 1) != -1: raise TestFailed, 'cmp(-1, 1)'
if cmp(1, -1) != 1: raise TestFailed, 'cmp(1, -1)'
if cmp(1, 1) != 0: raise TestFailed, 'cmp(1, 1)'
79 80 81 82 83 84 85 86 87
# verify that circular objects are handled
a = []; a.append(a)
b = []; b.append(b)
from UserList import UserList
c = UserList(); c.append(c)
if cmp(a, b) != 0: raise TestFailed, "cmp(%s, %s)" % (a, b)
if cmp(b, c) != 0: raise TestFailed, "cmp(%s, %s)" % (b, c)
if cmp(c, a) != 0: raise TestFailed, "cmp(%s, %s)" % (c, a)
if cmp(a, c) != 0: raise TestFailed, "cmp(%s, %s)" % (a, c)
88 89
# okay, now break the cycles
a.pop(); b.pop(); c.pop()
90 91

print 'coerce'
92
if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
93
if coerce(1, 1L) != (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
94
if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
95

96 97 98
print 'compile'
compile('print 1\n', '', 'exec')

99
print 'complex'
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
if complex(1,10) != 1+10j: raise TestFailed, 'complex(1,10)'
if complex(1,10L) != 1+10j: raise TestFailed, 'complex(1,10L)'
if complex(1,10.0) != 1+10j: raise TestFailed, 'complex(1,10.0)'
if complex(1L,10) != 1+10j: raise TestFailed, 'complex(1L,10)'
if complex(1L,10L) != 1+10j: raise TestFailed, 'complex(1L,10L)'
if complex(1L,10.0) != 1+10j: raise TestFailed, 'complex(1L,10.0)'
if complex(1.0,10) != 1+10j: raise TestFailed, 'complex(1.0,10)'
if complex(1.0,10L) != 1+10j: raise TestFailed, 'complex(1.0,10L)'
if complex(1.0,10.0) != 1+10j: raise TestFailed, 'complex(1.0,10.0)'
if complex(3.14+0j) != 3.14+0j: raise TestFailed, 'complex(3.14)'
if complex(3.14) != 3.14+0j: raise TestFailed, 'complex(3.14)'
if complex(314) != 314.0+0j: raise TestFailed, 'complex(314)'
if complex(314L) != 314.0+0j: raise TestFailed, 'complex(314L)'
if complex(3.14+0j, 0j) != 3.14+0j: raise TestFailed, 'complex(3.14, 0j)'
if complex(3.14, 0.0) != 3.14+0j: raise TestFailed, 'complex(3.14, 0.0)'
if complex(314, 0) != 314.0+0j: raise TestFailed, 'complex(314, 0)'
if complex(314L, 0L) != 314.0+0j: raise TestFailed, 'complex(314L, 0L)'
if complex(0j, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0j, 3.14j)'
if complex(0.0, 3.14j) != -3.14+0j: raise TestFailed, 'complex(0.0, 3.14j)'
if complex(0j, 3.14) != 3.14j: raise TestFailed, 'complex(0j, 3.14)'
if complex(0.0, 3.14) != 3.14j: raise TestFailed, 'complex(0.0, 3.14)'
if complex("  3.14+J  ") != 3.14+1j:  raise TestFailed, 'complex("  3.14+J  )"'
122 123 124
if have_unicode:
    if complex(unicode("  3.14+J  ")) != 3.14+1j:
        raise TestFailed, 'complex(u"  3.14+J  )"'
125 126 127
class Z:
    def __complex__(self): return 3.14j
z = Z()
128
if complex(z) != 3.14j: raise TestFailed, 'complex(classinstance)'
129

130 131 132 133 134
print 'delattr'
import sys
sys.spam = 1
delattr(sys, 'spam')

Guido van Rossum's avatar
Guido van Rossum committed
135 136 137 138 139 140 141
print 'dir'
x = 1
if 'x' not in dir(): raise TestFailed, 'dir()'
import sys
if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'

print 'divmod'
142 143 144 145
if divmod(12, 7) != (1, 5): raise TestFailed, 'divmod(12, 7)'
if divmod(-12, 7) != (-2, 2): raise TestFailed, 'divmod(-12, 7)'
if divmod(12, -7) != (-2, -2): raise TestFailed, 'divmod(12, -7)'
if divmod(-12, -7) != (1, -5): raise TestFailed, 'divmod(-12, -7)'
Guido van Rossum's avatar
Guido van Rossum committed
146
#
147 148 149 150
if divmod(12L, 7L) != (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
if divmod(-12L, 7L) != (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
if divmod(12L, -7L) != (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
if divmod(-12L, -7L) != (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
Guido van Rossum's avatar
Guido van Rossum committed
151
#
152 153 154 155
if divmod(12, 7L) != (1, 5L): raise TestFailed, 'divmod(12, 7L)'
if divmod(-12, 7L) != (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
if divmod(12L, -7) != (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
if divmod(-12L, -7) != (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
Guido van Rossum's avatar
Guido van Rossum committed
156
#
157
if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
158
    raise TestFailed, 'divmod(3.25, 1.0)'
159
if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
160
    raise TestFailed, 'divmod(-3.25, 1.0)'
161
if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
162
    raise TestFailed, 'divmod(3.25, -1.0)'
163
if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
164
    raise TestFailed, 'divmod(-3.25, -1.0)'
Guido van Rossum's avatar
Guido van Rossum committed
165 166

print 'eval'
167 168
if eval('1+1') != 2: raise TestFailed, 'eval(\'1+1\')'
if eval(' 1+1\n') != 2: raise TestFailed, 'eval(\' 1+1\\n\')'
169 170
globals = {'a': 1, 'b': 2}
locals = {'b': 200, 'c': 300}
171
if eval('a', globals) != 1:
Jeremy Hylton's avatar
Jeremy Hylton committed
172
    raise TestFailed, "eval(1) == %s" % eval('a', globals)
173
if eval('a', globals, locals) != 1:
Jeremy Hylton's avatar
Jeremy Hylton committed
174
    raise TestFailed, "eval(2)"
175
if eval('b', globals, locals) != 200:
Jeremy Hylton's avatar
Jeremy Hylton committed
176
    raise TestFailed, "eval(3)"
177
if eval('c', globals, locals) != 300:
Jeremy Hylton's avatar
Jeremy Hylton committed
178
    raise TestFailed, "eval(4)"
179 180 181
if have_unicode:
    if eval(unicode('1+1')) != 2: raise TestFailed, 'eval(u\'1+1\')'
    if eval(unicode(' 1+1\n')) != 2: raise TestFailed, 'eval(u\' 1+1\\n\')'
182 183
globals = {'a': 1, 'b': 2}
locals = {'b': 200, 'c': 300}
184 185 186 187 188 189 190 191 192
if have_unicode:
    if eval(unicode('a'), globals) != 1:
        raise TestFailed, "eval(1) == %s" % eval(unicode('a'), globals)
    if eval(unicode('a'), globals, locals) != 1:
        raise TestFailed, "eval(2)"
    if eval(unicode('b'), globals, locals) != 200:
        raise TestFailed, "eval(3)"
    if eval(unicode('c'), globals, locals) != 300:
        raise TestFailed, "eval(4)"
Guido van Rossum's avatar
Guido van Rossum committed
193

194 195 196 197 198 199 200
print 'execfile'
z = 0
f = open(TESTFN, 'w')
f.write('z = z+1\n')
f.write('z = z*2\n')
f.close()
execfile(TESTFN)
201
if z != 2: raise TestFailed, "execfile(1)"
202 203
globals['z'] = 0
execfile(TESTFN, globals)
204
if globals['z'] != 2: raise TestFailed, "execfile(1)"
205 206
locals['z'] = 0
execfile(TESTFN, globals, locals)
207
if locals['z'] != 2: raise TestFailed, "execfile(1)"
208
unlink(TESTFN)
Guido van Rossum's avatar
Guido van Rossum committed
209

210
print 'filter'
211
if filter(lambda c: 'a' <= c <= 'z', 'Hello World') != 'elloorld':
212
    raise TestFailed, 'filter (filter a string)'
213
if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) != [1, 'hello', [3], 9]:
214
    raise TestFailed, 'filter (remove false values)'
215
if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) != [1, 9, 2]:
216
    raise TestFailed, 'filter (keep positives)'
217
class Squares:
218 219 220 221 222 223 224 225 226 227 228
    def __init__(self, max):
        self.max = max
        self.sofar = []
    def __len__(self): return len(self.sofar)
    def __getitem__(self, i):
        if not 0 <= i < self.max: raise IndexError
        n = len(self.sofar)
        while n <= i:
            self.sofar.append(n*n)
            n = n+1
        return self.sofar[i]
229
if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
230
    raise TestFailed, 'filter(None, Squares(10))'
231
if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
232
    raise TestFailed, 'filter(oddp, Squares(10))'
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
class StrSquares:
    def __init__(self, max):
        self.max = max
        self.sofar = []
    def __len__(self):
        return len(self.sofar)
    def __getitem__(self, i):
        if not 0 <= i < self.max:
            raise IndexError
        n = len(self.sofar)
        while n <= i:
            self.sofar.append(str(n*n))
            n = n+1
        return self.sofar[i]
def identity(item):
    return 1
filter(identity, Squares(5))
250

Guido van Rossum's avatar
Guido van Rossum committed
251
print 'float'
252 253 254 255
if float(3.14) != 3.14: raise TestFailed, 'float(3.14)'
if float(314) != 314.0: raise TestFailed, 'float(314)'
if float(314L) != 314.0: raise TestFailed, 'float(314L)'
if float("  3.14  ") != 3.14:  raise TestFailed, 'float("  3.14  ")'
256 257 258
if have_unicode:
    if float(unicode("  3.14  ")) != 3.14:
        raise TestFailed, 'float(u"  3.14  ")'
259
    if float(unicode("  \u0663.\u0661\u0664  ",'raw-unicode-escape')) != 3.14:
260
        raise TestFailed, 'float(u"  \u0663.\u0661\u0664  ")'
Guido van Rossum's avatar
Guido van Rossum committed
261 262 263 264

print 'getattr'
import sys
if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
265 266 267 268 269 270 271 272 273 274 275 276
try:
    getattr(sys, 1)
except TypeError:
    pass
else:
    raise TestFailed, "getattr(sys, 1) should raise an exception"
try:
    getattr(sys, 1, "foo")
except TypeError:
    pass
else:
    raise TestFailed, 'getattr(sys, 1, "foo") should raise an exception'
Guido van Rossum's avatar
Guido van Rossum committed
277

278 279 280
print 'hasattr'
import sys
if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
281 282 283 284 285 286
try:
    hasattr(sys, 1)
except TypeError:
    pass
else:
    raise TestFailed, "hasattr(sys, 1) should raise an exception"
287 288 289 290 291 292 293

print 'hash'
hash(None)
if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
hash('spam')
hash((0,1,2,3))
def f(): pass
294 295 296 297 298 299
try: hash([])
except TypeError: pass
else: raise TestFailed, "hash([]) should raise an exception"
try: hash({})
except TypeError: pass
else: raise TestFailed, "hash({}) should raise an exception"
300

Guido van Rossum's avatar
Guido van Rossum committed
301 302 303
print 'hex'
if hex(16) != '0x10': raise TestFailed, 'hex(16)'
if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
304 305 306
if len(hex(-1)) != len(hex(sys.maxint)): raise TestFailed, 'len(hex(-1))'
if hex(-16) not in ('0xfffffff0', '0xfffffffffffffff0'):
    raise TestFailed, 'hex(-16)'
Guido van Rossum's avatar
Guido van Rossum committed
307 308
if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'

309 310 311 312 313 314 315 316 317 318
print 'id'
id(None)
id(1)
id(1L)
id(1.0)
id('spam')
id((0,1,2,3))
id([0,1,2,3])
id({'spam': 1, 'eggs': 2, 'ham': 3})

Guido van Rossum's avatar
Guido van Rossum committed
319 320 321
# Test input() later, together with raw_input

print 'int'
322 323 324
if int(314) != 314: raise TestFailed, 'int(314)'
if int(3.14) != 3: raise TestFailed, 'int(3.14)'
if int(314L) != 314: raise TestFailed, 'int(314L)'
325
# Check that conversion from float truncates towards zero
326 327 328 329 330
if int(-3.14) != -3: raise TestFailed, 'int(-3.14)'
if int(3.9) != 3: raise TestFailed, 'int(3.9)'
if int(-3.9) != -3: raise TestFailed, 'int(-3.9)'
if int(3.5) != 3: raise TestFailed, 'int(3.5)'
if int(-3.5) != -3: raise TestFailed, 'int(-3.5)'
331
# Different base:
332
if int("10",16) != 16L: raise TestFailed, 'int("10",16)'
333 334 335
if have_unicode:
    if int(unicode("10"),16) != 16L:
        raise TestFailed, 'int(u"10",16)'
Jeremy Hylton's avatar
Jeremy Hylton committed
336
# Test conversion from strings and various anomalies
337
L = [
338 339 340 341 342 343 344 345 346 347 348
        ('0', 0),
        ('1', 1),
        ('9', 9),
        ('10', 10),
        ('99', 99),
        ('100', 100),
        ('314', 314),
        (' 314', 314),
        ('314 ', 314),
        ('  \t\t  314  \t\t  ', 314),
        (`sys.maxint`, sys.maxint),
349 350 351
        ('  1x', ValueError),
        ('  1  ', 1),
        ('  1\02  ', ValueError),
352 353
        ('', ValueError),
        (' ', ValueError),
354 355 356 357 358 359 360 361 362 363 364 365
        ('  \t\t  ', ValueError)
]
if have_unicode:
    L += [
        (unicode('0'), 0),
        (unicode('1'), 1),
        (unicode('9'), 9),
        (unicode('10'), 10),
        (unicode('99'), 99),
        (unicode('100'), 100),
        (unicode('314'), 314),
        (unicode(' 314'), 314),
366
        (unicode('\u0663\u0661\u0664 ','raw-unicode-escape'), 314),
367 368 369 370 371 372 373
        (unicode('  \t\t  314  \t\t  '), 314),
        (unicode('  1x'), ValueError),
        (unicode('  1  '), 1),
        (unicode('  1\02  '), ValueError),
        (unicode(''), ValueError),
        (unicode(' '), ValueError),
        (unicode('  \t\t  '), ValueError),
374 375 376
]
for s, v in L:
    for sign in "", "+", "-":
377 378 379 380 381 382 383 384 385 386 387 388
        for prefix in "", " ", "\t", "  \t\t  ":
            ss = prefix + sign + s
            vv = v
            if sign == "-" and v is not ValueError:
                vv = -v
            try:
                if int(ss) != vv:
                    raise TestFailed, "int(%s)" % `ss`
            except v:
                pass
            except ValueError, e:
                raise TestFailed, "int(%s) raised ValueError: %s" % (`ss`, e)
389 390 391 392 393 394 395 396 397
s = `-1-sys.maxint`
if int(s)+1 != -sys.maxint:
    raise TestFailed, "int(%s)" % `s`
try:
    int(s[1:])
except ValueError:
    pass
else:
    raise TestFailed, "int(%s)" % `s[1:]` + " should raise ValueError"
398 399 400 401 402 403 404 405 406 407 408 409 410
try:
    int(1e100)
except OverflowError:
    pass
else:
    raise TestFailed("int(1e100) expected OverflowError")
try:
    int(-1e100)
except OverflowError:
    pass
else:
    raise TestFailed("int(-1e100) expected OverflowError")

Guido van Rossum's avatar
Guido van Rossum committed
411

412 413 414 415
# SF bug 434186:  0x80000000/2 != 0x80000000>>1.
# Worked by accident in Windows release build, but failed in debug build.
# Failed in all Linux builds.
x = -1-sys.maxint
416
if x >> 1 != x//2:
417 418
    raise TestFailed("x >> 1 != x/2 when x == -1-sys.maxint")

419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
print 'isinstance'
class C:
    pass
class D(C):
    pass
class E:
    pass
c = C()
d = D()
e = E()
if not isinstance(c, C): raise TestFailed, 'isinstance(c, C)'
if not isinstance(d, C): raise TestFailed, 'isinstance(d, C)'
if isinstance(e, C): raise TestFailed, 'isinstance(e, C)'
if isinstance(c, D): raise TestFailed, 'isinstance(c, D)'
if isinstance('foo', E): raise TestFailed, 'isinstance("Foo", E)'
try:
    isinstance(E, 'foo')
    raise TestFailed, 'isinstance(E, "foo")'
except TypeError:
    pass

print 'issubclass'
if not issubclass(D, C): raise TestFailed, 'issubclass(D, C)'
if not issubclass(C, C): raise TestFailed, 'issubclass(C, C)'
if issubclass(C, D): raise TestFailed, 'issubclass(C, D)'
try:
    issubclass('foo', E)
    raise TestFailed, 'issubclass("foo", E)'
except TypeError:
    pass
try:
    issubclass(E, 'foo')
    raise TestFailed, 'issubclass(E, "foo")'
except TypeError:
    pass

Guido van Rossum's avatar
Guido van Rossum committed
455
print 'len'
456 457 458 459 460 461
if len('123') != 3: raise TestFailed, 'len(\'123\')'
if len(()) != 0: raise TestFailed, 'len(())'
if len((1, 2, 3, 4)) != 4: raise TestFailed, 'len((1, 2, 3, 4))'
if len([1, 2, 3, 4]) != 4: raise TestFailed, 'len([1, 2, 3, 4])'
if len({}) != 0: raise TestFailed, 'len({})'
if len({'a':1, 'b': 2}) != 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
Guido van Rossum's avatar
Guido van Rossum committed
462 463

print 'long'
464 465 466
if long(314) != 314L: raise TestFailed, 'long(314)'
if long(3.14) != 3L: raise TestFailed, 'long(3.14)'
if long(314L) != 314L: raise TestFailed, 'long(314L)'
467
# Check that conversion from float truncates towards zero
468 469 470 471 472 473
if long(-3.14) != -3L: raise TestFailed, 'long(-3.14)'
if long(3.9) != 3L: raise TestFailed, 'long(3.9)'
if long(-3.9) != -3L: raise TestFailed, 'long(-3.9)'
if long(3.5) != 3L: raise TestFailed, 'long(3.5)'
if long(-3.5) != -3L: raise TestFailed, 'long(-3.5)'
if long("-3") != -3L: raise TestFailed, 'long("-3")'
474 475 476
if have_unicode:
    if long(unicode("-3")) != -3L:
        raise TestFailed, 'long(u"-3")'
477
# Different base:
478
if long("10",16) != 16L: raise TestFailed, 'long("10",16)'
479 480 481
if have_unicode:
    if long(unicode("10"),16) != 16L:
        raise TestFailed, 'long(u"10",16)'
482 483
# Check conversions from string (same test set as for int(), and then some)
LL = [
484
        ('1' + '0'*20, 10L**20),
485 486 487 488 489 490
        ('1' + '0'*100, 10L**100)
]
if have_unicode:
    L+=[
        (unicode('1') + unicode('0')*20, 10L**20),
        (unicode('1') + unicode('0')*100, 10L**100),
491 492 493
]
for s, v in L + LL:
    for sign in "", "+", "-":
494 495 496 497 498 499 500
        for prefix in "", " ", "\t", "  \t\t  ":
            ss = prefix + sign + s
            vv = v
            if sign == "-" and v is not ValueError:
                vv = -v
            try:
                if long(ss) != long(vv):
501
                    raise TestFailed, "long(%s)" % `ss`
502 503 504
            except v:
                pass
            except ValueError, e:
505
                raise TestFailed, "long(%s) raised ValueError: %s" % (`ss`, e)
Guido van Rossum's avatar
Guido van Rossum committed
506

507
print 'map'
508
if map(None, 'hello world') != ['h','e','l','l','o',' ','w','o','r','l','d']:
509
    raise TestFailed, 'map(None, \'hello world\')'
510
if map(None, 'abcd', 'efg') != \
511 512
   [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
    raise TestFailed, 'map(None, \'abcd\', \'efg\')'
513
if map(None, range(10)) != [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
514
    raise TestFailed, 'map(None, range(10))'
515
if map(lambda x: x*x, range(1,4)) != [1, 4, 9]:
516
    raise TestFailed, 'map(lambda x: x*x, range(1,4))'
517
try:
518
    from math import sqrt
519
except ImportError:
520 521
    def sqrt(x):
        return pow(x, 0.5)
522
if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) != [[4.0, 2.0], [9.0, 3.0]]:
523
    raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
524
if map(lambda x, y: x+y, [1,3,2], [9,1,4]) != [10, 4, 6]:
525
    raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
526
def plus(*v):
527 528 529
    accu = 0
    for i in v: accu = accu + i
    return accu
530
if map(plus, [1, 3, 7]) != [1, 3, 7]:
531
    raise TestFailed, 'map(plus, [1, 3, 7])'
532
if map(plus, [1, 3, 7], [4, 9, 2]) != [1+4, 3+9, 7+2]:
533
    raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
534
if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) != [1+4+1, 3+9+1, 7+2+0]:
535
    raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
536
if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
537
    raise TestFailed, 'map(None, Squares(10))'
538
if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
539
    raise TestFailed, 'map(int, Squares(10))'
540
if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
541
    raise TestFailed, 'map(None, Squares(3), Squares(2))'
542
if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
543
    raise TestFailed, 'map(max, Squares(3), Squares(2))'
544

Guido van Rossum's avatar
Guido van Rossum committed
545
print 'max'
546 547 548 549
if max('123123') != '3': raise TestFailed, 'max(\'123123\')'
if max(1, 2, 3) != 3: raise TestFailed, 'max(1, 2, 3)'
if max((1, 2, 3, 1, 2, 3)) != 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
if max([1, 2, 3, 1, 2, 3]) != 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
Guido van Rossum's avatar
Guido van Rossum committed
550
#
551 552 553
if max(1, 2L, 3.0) != 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
if max(1L, 2.0, 3) != 3: raise TestFailed, 'max(1L, 2.0, 3)'
if max(1.0, 2, 3L) != 3L: raise TestFailed, 'max(1.0, 2, 3L)'
Guido van Rossum's avatar
Guido van Rossum committed
554 555

print 'min'
556 557 558 559
if min('123123') != '1': raise TestFailed, 'min(\'123123\')'
if min(1, 2, 3) != 1: raise TestFailed, 'min(1, 2, 3)'
if min((1, 2, 3, 1, 2, 3)) != 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
if min([1, 2, 3, 1, 2, 3]) != 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
Guido van Rossum's avatar
Guido van Rossum committed
560
#
561 562 563
if min(1, 2L, 3.0) != 1: raise TestFailed, 'min(1, 2L, 3.0)'
if min(1L, 2.0, 3) != 1L: raise TestFailed, 'min(1L, 2.0, 3)'
if min(1.0, 2, 3L) != 1.0: raise TestFailed, 'min(1.0, 2, 3L)'