annotate Lib/test/test_b1.py @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 11b937ea0026
children ba11b58b2b0a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 # Python test set -- part 4a, built-in functions a-m
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 from test_support import *
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
4
2046
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
5 print '__import__'
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
6 __import__('sys')
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
7 __import__('strop')
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
8 __import__('string')
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
9 try: __import__('spamspam')
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
10 except ImportError: pass
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
11 else: raise TestFailed, "__import__('spamspam') should fail"
1ce3dbeeb890 Test new __import__ module, test reload of built-in module, test
Guido van Rossum <guido@python.org>
parents: 1998
diff changeset
12
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
13 print 'abs'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 if abs(0) <> 0: raise TestFailed, 'abs(0)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
15 if abs(1234) <> 1234: raise TestFailed, 'abs(1234)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
16 if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
18 if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
20 if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
21 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 if abs(0L) <> 0L: raise TestFailed, 'abs(0L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
23 if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
26 print 'apply'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 def f0(*args):
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
28 if args != (): raise TestFailed, 'f0 called with ' + `args`
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 def f1(a1):
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 if a1 != 1: raise TestFailed, 'f1 called with ' + `a1`
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 def f2(a1, a2):
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 if a1 != 1 or a2 != 2:
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33 raise TestFailed, 'f2 called with ' + `a1, a2`
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 def f3(a1, a2, a3):
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35 if a1 != 1 or a2 != 2 or a3 != 3:
1359
5d74a9873589 added builtin b/w compat module.
Guido van Rossum <guido@python.org>
parents: 1197
diff changeset
36 raise TestFailed, 'f3 called with ' + `a1, a2, a3`
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 apply(f0, ())
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 apply(f1, (1,))
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 apply(f2, (1, 2))
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
40 apply(f3, (1, 2, 3))
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
1998
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
42 print 'callable'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
43 if not callable(len):raise TestFailed, 'callable(len)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
44 def f(): pass
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
45 if not callable(f): raise TestFailed, 'callable(f)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
46 class C:
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
47 def meth(self): pass
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
48 if not callable(C): raise TestFailed, 'callable(C)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
49 x = C()
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
50 if not callable(x.meth): raise TestFailed, 'callable(x.meth)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
51 if callable(x): raise TestFailed, 'callable(x)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
52 class D(C):
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
53 def __call__(self): pass
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
54 y = D()
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
55 if not callable(y): raise TestFailed, 'callable(y)'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
56
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57 print 'chr'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 if chr(32) <> ' ': raise TestFailed, 'chr(32)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 if chr(65) <> 'A': raise TestFailed, 'chr(65)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 if chr(97) <> 'a': raise TestFailed, 'chr(97)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
62 print 'cmp'
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
63 if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)'
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
64 if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)'
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
65 if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)'
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
66
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
67 print 'coerce'
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
68 if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)'
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
69 if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)'
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
70 if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)'
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
71
1998
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
72 print 'compile'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
73 compile('print 1\n', '', 'exec')
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
74
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
75 print 'delattr'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
76 import sys
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
77 sys.spam = 1
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
78 delattr(sys, 'spam')
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
79
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
80 print 'dir'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
81 x = 1
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
82 if 'x' not in dir(): raise TestFailed, 'dir()'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
83 import sys
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
84 if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
85
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
86 print 'divmod'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
87 if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
90 if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92 if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
97 if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100 if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 #
1197
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
102 if fcmp(divmod(3.25, 1.0), (3.0, 0.25)):
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
103 raise TestFailed, 'divmod(3.25, 1.0)'
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
104 if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)):
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
105 raise TestFailed, 'divmod(-3.25, 1.0)'
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
106 if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)):
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
107 raise TestFailed, 'divmod(3.25, -1.0)'
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
108 if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)):
675a02820d66 * os.py: _exit doesn't exist in all variations of posix
Guido van Rossum <guido@python.org>
parents: 1097
diff changeset
109 raise TestFailed, 'divmod(-3.25, -1.0)'
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
110
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
111 print 'eval'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
112 if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')'
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
113 if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')'
2051
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
114 globals = {'a': 1, 'b': 2}
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
115 locals = {'b': 200, 'c': 300}
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
116 if eval('a', globals) <> 1: raise TestFailed, "eval(1)"
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
117 if eval('a', globals, locals) <> 1: raise TestFailed, "eval(2)"
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
118 if eval('b', globals, locals) <> 200: raise TestFailed, "eval(3)"
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
119 if eval('c', globals, locals) <> 300: raise TestFailed, "eval(4)"
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
120
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
121 print 'execfile'
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
122 z = 0
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
123 f = open(TESTFN, 'w')
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
124 f.write('z = z+1\n')
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
125 f.write('z = z*2\n')
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
126 f.close()
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
127 execfile(TESTFN)
2051
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
128 if z <> 2: raise TestFailed, "execfile(1)"
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
129 globals['z'] = 0
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
130 execfile(TESTFN, globals)
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
131 if globals['z'] <> 2: raise TestFailed, "execfile(1)"
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
132 locals['z'] = 0
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
133 execfile(TESTFN, globals, locals)
23dfca8aa73b * Lib/test/test_b1.py: test eval() and execfile() with globals,
Guido van Rossum <guido@python.org>
parents: 2046
diff changeset
134 if locals['z'] <> 2: raise TestFailed, "execfile(1)"
1097
c644bc626098 Added some new tests and two new files for testing: test_types.py
Guido van Rossum <guido@python.org>
parents: 653
diff changeset
135 unlink(TESTFN)
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
136
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
137 print 'filter'
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
138 if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld':
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
139 raise TestFailed, 'filter (filter a string)'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
140 if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
141 raise TestFailed, 'filter (remove false values)'
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
142 if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]:
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
143 raise TestFailed, 'filter (keep positives)'
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
144 class Squares:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
145 def __init__(self, max):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
146 self.max = max
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
147 self.sofar = []
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
148 def __len__(self): return len(self.sofar)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
149 def __getitem__(self, i):
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
150 if not 0 <= i < self.max: raise IndexError
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
151 n = len(self.sofar)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
152 while n <= i:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
153 self.sofar.append(n*n)
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
154 n = n+1
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
155 return self.sofar[i]
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
156 if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
157 raise TestFailed, 'filter(None, Squares(10))'
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
158 if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
159 raise TestFailed, 'filter(oddp, Squares(10))'
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
160
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
161 print 'float'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
162 if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
163 if float(314) <> 314.0: raise TestFailed, 'float(314)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
164 if float(314L) <> 314.0: raise TestFailed, 'float(314L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
165
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
166 print 'getattr'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
167 import sys
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
168 if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
169
1998
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
170 print 'hasattr'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
171 import sys
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
172 if not hasattr(sys, 'stdout'): raise TestFailed, 'hasattr'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
173
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
174 print 'hash'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
175 hash(None)
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
176 if not hash(1) == hash(1L) == hash(1.0): raise TestFailed, 'numeric hash()'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
177 hash('spam')
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
178 hash((0,1,2,3))
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
179 def f(): pass
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
180
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 print 'hex'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 if hex(16) != '0x10': raise TestFailed, 'hex(16)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
183 if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
186
1998
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
187 print 'id'
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
188 id(None)
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
189 id(1)
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
190 id(1L)
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
191 id(1.0)
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
192 id('spam')
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
193 id((0,1,2,3))
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
194 id([0,1,2,3])
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
195 id({'spam': 1, 'eggs': 2, 'ham': 3})
cf4d8862f19b more complete tests of built-in functions
Guido van Rossum <guido@python.org>
parents: 1746
diff changeset
196
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 # Test input() later, together with raw_input
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
198
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 print 'int'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
200 if int(314) <> 314: raise TestFailed, 'int(314)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
201 if int(3.14) <> 3: raise TestFailed, 'int(3.14)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
202 if int(314L) <> 314: raise TestFailed, 'int(314L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
203
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
204 print 'len'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
205 if len('123') <> 3: raise TestFailed, 'len(\'123\')'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
206 if len(()) <> 0: raise TestFailed, 'len(())'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
207 if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
208 if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
209 if len({}) <> 0: raise TestFailed, 'len({})'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
212 print 'long'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
213 if long(314) <> 314L: raise TestFailed, 'long(314)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
214 if long(3.14) <> 3L: raise TestFailed, 'long(3.14)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
215 if long(314L) <> 314L: raise TestFailed, 'long(314L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
216
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
217 print 'map'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
218 if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
219 raise TestFailed, 'map(None, \'hello world\')'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
220 if map(None, 'abcd', 'efg') <> \
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
221 [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
222 raise TestFailed, 'map(None, \'abcd\', \'efg\')'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
223 if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
224 raise TestFailed, 'map(None, range(10))'
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
225 if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
226 raise TestFailed, 'map(lambda x: x*x, range(1,4))'
2477
11b937ea0026 avoid math, don't abort when overflow check fails
Guido van Rossum <guido@python.org>
parents: 2051
diff changeset
227 try:
11b937ea0026 avoid math, don't abort when overflow check fails
Guido van Rossum <guido@python.org>
parents: 2051
diff changeset
228 from math import sqrt
11b937ea0026 avoid math, don't abort when overflow check fails
Guido van Rossum <guido@python.org>
parents: 2051
diff changeset
229 except ImportError:
11b937ea0026 avoid math, don't abort when overflow check fails
Guido van Rossum <guido@python.org>
parents: 2051
diff changeset
230 def sqrt(x):
11b937ea0026 avoid math, don't abort when overflow check fails
Guido van Rossum <guido@python.org>
parents: 2051
diff changeset
231 return pow(x, 0.5)
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
232 if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
233 raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])'
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
234 if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]:
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents: 1403
diff changeset
235 raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])'
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
236 def plus(*v):
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
237 accu = 0
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
238 for i in v: accu = accu + i
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
239 return accu
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
240 if map(plus, [1, 3, 7]) <> [1, 3, 7]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
241 raise TestFailed, 'map(plus, [1, 3, 7])'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
242 if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
243 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])'
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
244 if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]:
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
245 raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])'
1746
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
246 if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
247 raise TestFailed, 'map(None, Squares(10))'
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
248 if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
249 raise TestFailed, 'map(int, Squares(10))'
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
250 if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
251 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
252 if map(max, Squares(3), Squares(2)) != [0, 1, 4]:
1940892d2fe3 Merge alpha100 branch back to main trunk
Guido van Rossum <guido@python.org>
parents: 1416
diff changeset
253 raise TestFailed, 'map(None: x, Squares(3), Squares(2))'
1403
6278933d02e3 * string.py: added rindex(), rfind(); changed index() to interpret
Guido van Rossum <guido@python.org>
parents: 1359
diff changeset
254
653
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
255 print 'max'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
256 if max('123123') <> '3': raise TestFailed, 'max(\'123123\')'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
257 if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
258 if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
259 if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
260 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
261 if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
262 if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
263 if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
264
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
265 print 'min'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
266 if min('123123') <> '1': raise TestFailed, 'min(\'123123\')'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
267 if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
268 if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
269 if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
270 #
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
271 if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
272 if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)'
74942ec70430 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
273 if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)'