Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 4b, built-in functions n-z |
| 2 | |
| 3 | from test_support import * |
| 4 | |
| 5 | print 'oct' |
| 6 | if oct(100) != '0144': raise TestFailed, 'oct(100)' |
| 7 | if oct(100L) != '0144L': raise TestFailed, 'oct(100L)' |
Guido van Rossum | d9c6f4f | 1997-06-06 21:14:14 +0000 | [diff] [blame] | 8 | if oct(-100) not in ('037777777634', '01777777777777777777634'): |
Guido van Rossum | 26fd982 | 1998-05-22 15:05:36 +0000 | [diff] [blame] | 9 | raise TestFailed, 'oct(-100)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 10 | if oct(-100L) != '-0144L': raise TestFailed, 'oct(-100L)' |
| 11 | |
| 12 | print 'open' |
| 13 | # NB the first 4 lines are also used to test input and raw_input, below |
| 14 | fp = open(TESTFN, 'w') |
| 15 | try: |
| 16 | fp.write('1+1\n') |
| 17 | fp.write('1+1\n') |
| 18 | fp.write('The quick brown fox jumps over the lazy dog') |
| 19 | fp.write('.\n') |
| 20 | fp.write('Dear John\n') |
| 21 | fp.write('XXX'*100) |
| 22 | fp.write('YYY'*100) |
| 23 | finally: |
| 24 | fp.close() |
| 25 | # |
| 26 | fp = open(TESTFN, 'r') |
| 27 | try: |
| 28 | if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact' |
| 29 | if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact' |
| 30 | if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n': |
| 31 | raise TestFailed, 'readline() # default' |
| 32 | if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short' |
| 33 | if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)' |
| 34 | if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)' |
| 35 | if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate' |
| 36 | finally: |
| 37 | fp.close() |
| 38 | |
| 39 | print 'ord' |
| 40 | if ord(' ') <> 32: raise TestFailed, 'ord(\' \')' |
| 41 | if ord('A') <> 65: raise TestFailed, 'ord(\'A\')' |
| 42 | if ord('a') <> 97: raise TestFailed, 'ord(\'a\')' |
| 43 | |
| 44 | print 'pow' |
| 45 | if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)' |
| 46 | if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)' |
| 47 | if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)' |
| 48 | if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)' |
| 49 | # |
| 50 | if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)' |
| 51 | if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)' |
| 52 | if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)' |
| 53 | if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)' |
| 54 | # |
| 55 | if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)' |
| 56 | if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)' |
| 57 | if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)' |
| 58 | if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)' |
| 59 | # |
| 60 | if pow(0L,0) <> 1: raise TestFailed, 'pow(0L,0)' |
| 61 | if pow(0L,1) <> 0: raise TestFailed, 'pow(0L,1)' |
| 62 | if pow(1L,0) <> 1: raise TestFailed, 'pow(1L,0)' |
| 63 | if pow(1L,1) <> 1: raise TestFailed, 'pow(1L,1)' |
| 64 | # |
| 65 | if pow(2L,0) <> 1: raise TestFailed, 'pow(2L,0)' |
| 66 | if pow(2L,10) <> 1024: raise TestFailed, 'pow(2L,10)' |
| 67 | if pow(2L,20) <> 1024*1024: raise TestFailed, 'pow(2L,20)' |
| 68 | if pow(2L,30) <> 1024*1024*1024: raise TestFailed, 'pow(2L,30)' |
| 69 | # |
| 70 | if pow(-2L,0) <> 1: raise TestFailed, 'pow(-2L,0)' |
| 71 | if pow(-2L,1) <> -2: raise TestFailed, 'pow(-2L,1)' |
| 72 | if pow(-2L,2) <> 4: raise TestFailed, 'pow(-2L,2)' |
| 73 | if pow(-2L,3) <> -8: raise TestFailed, 'pow(-2L,3)' |
| 74 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 75 | if fcmp(pow(0.,0), 1.): raise TestFailed, 'pow(0.,0)' |
| 76 | if fcmp(pow(0.,1), 0.): raise TestFailed, 'pow(0.,1)' |
| 77 | if fcmp(pow(1.,0), 1.): raise TestFailed, 'pow(1.,0)' |
| 78 | if fcmp(pow(1.,1), 1.): raise TestFailed, 'pow(1.,1)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 79 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 80 | if fcmp(pow(2.,0), 1.): raise TestFailed, 'pow(2.,0)' |
| 81 | if fcmp(pow(2.,10), 1024.): raise TestFailed, 'pow(2.,10)' |
| 82 | if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed, 'pow(2.,20)' |
| 83 | if fcmp(pow(2.,30), 1024.*1024.*1024.): raise TestFailed, 'pow(2.,30)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 84 | # |
| 85 | # XXX These don't work -- negative float to the float power... |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 86 | #if fcmp(pow(-2.,0), 1.): raise TestFailed, 'pow(-2.,0)' |
| 87 | #if fcmp(pow(-2.,1), -2.): raise TestFailed, 'pow(-2.,1)' |
| 88 | #if fcmp(pow(-2.,2), 4.): raise TestFailed, 'pow(-2.,2)' |
| 89 | #if fcmp(pow(-2.,3), -8.): raise TestFailed, 'pow(-2.,3)' |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 90 | # |
| 91 | for x in 2, 2L, 2.0: |
| 92 | for y in 10, 10L, 10.0: |
| 93 | for z in 1000, 1000L, 1000.0: |
| 94 | if fcmp(pow(x, y, z), 24.0): |
| 95 | raise TestFailed, 'pow(%s, %s, %s)' % (x, y, z) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 96 | |
| 97 | print 'range' |
| 98 | if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)' |
| 99 | if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)' |
| 100 | if range(0) <> []: raise TestFailed, 'range(0)' |
| 101 | if range(-3) <> []: raise TestFailed, 'range(-3)' |
| 102 | if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)' |
| 103 | if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)' |
| 104 | |
| 105 | print 'input and raw_input' |
| 106 | import sys |
| 107 | fp = open(TESTFN, 'r') |
| 108 | savestdin = sys.stdin |
| 109 | try: |
| 110 | sys.stdin = fp |
| 111 | if input() <> 2: raise TestFailed, 'input()' |
| 112 | if input('testing\n') <> 2: raise TestFailed, 'input()' |
| 113 | if raw_input() <> 'The quick brown fox jumps over the lazy dog.': |
| 114 | raise TestFailed, 'raw_input()' |
| 115 | if raw_input('testing\n') <> 'Dear John': |
| 116 | raise TestFailed, 'raw_input(\'testing\\n\')' |
| 117 | finally: |
| 118 | sys.stdin = savestdin |
| 119 | fp.close() |
| 120 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 121 | print 'reduce' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 122 | if reduce(lambda x, y: x+y, ['a', 'b', 'c'], '') <> 'abc': |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 123 | raise TestFailed, 'reduce(): implode a string' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 124 | if reduce(lambda x, y: x+y, |
| 125 | [['a', 'c'], [], ['d', 'w']], []) <> ['a','c','d','w']: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 126 | raise TestFailed, 'reduce(): append' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 127 | if reduce(lambda x, y: x*y, range(2,8), 1) <> 5040: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 128 | raise TestFailed, 'reduce(): compute 7!' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 129 | if reduce(lambda x, y: x*y, range(2,21), 1L) <> 2432902008176640000L: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 130 | raise TestFailed, 'reduce(): compute 20!, use long' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 131 | class Squares: |
| 132 | def __init__(self, max): |
| 133 | self.max = max |
| 134 | self.sofar = [] |
| 135 | def __len__(self): return len(self.sofar) |
| 136 | def __getitem__(self, i): |
| 137 | if not 0 <= i < self.max: raise IndexError |
| 138 | n = len(self.sofar) |
| 139 | while n <= i: |
| 140 | self.sofar.append(n*n) |
| 141 | n = n+1 |
| 142 | return self.sofar[i] |
| 143 | if reduce(lambda x, y: x+y, Squares(10)) != 285: |
| 144 | raise TestFailed, 'reduce(<+>, Squares(10))' |
| 145 | if reduce(lambda x, y: x+y, Squares(10), 0) != 285: |
| 146 | raise TestFailed, 'reduce(<+>, Squares(10), 0)' |
| 147 | if reduce(lambda x, y: x+y, Squares(0), 0) != 0: |
| 148 | raise TestFailed, 'reduce(<+>, Squares(0), 0)' |
| 149 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 150 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 151 | print 'reload' |
Guido van Rossum | eecf035 | 1994-12-30 17:17:46 +0000 | [diff] [blame] | 152 | import marshal |
| 153 | reload(marshal) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 154 | import string |
| 155 | reload(string) |
Guido van Rossum | 7995ed2 | 1997-08-02 03:19:26 +0000 | [diff] [blame] | 156 | ## import sys |
| 157 | ## try: reload(sys) |
| 158 | ## except ImportError: pass |
| 159 | ## else: raise TestFailed, 'reload(sys) should fail' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 160 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 161 | print 'repr' |
| 162 | if repr('') <> '\'\'': raise TestFailed, 'repr(\'\')' |
| 163 | if repr(0) <> '0': raise TestFailed, 'repr(0)' |
| 164 | if repr(0L) <> '0L': raise TestFailed, 'repr(0L)' |
| 165 | if repr(()) <> '()': raise TestFailed, 'repr(())' |
| 166 | if repr([]) <> '[]': raise TestFailed, 'repr([])' |
| 167 | if repr({}) <> '{}': raise TestFailed, 'repr({})' |
| 168 | |
Guido van Rossum | e7113b6 | 1993-03-29 11:30:50 +0000 | [diff] [blame] | 169 | print 'round' |
| 170 | if round(0.0) <> 0.0: raise TestFailed, 'round(0.0)' |
| 171 | if round(1.0) <> 1.0: raise TestFailed, 'round(1.0)' |
| 172 | if round(10.0) <> 10.0: raise TestFailed, 'round(10.0)' |
| 173 | if round(1000000000.0) <> 1000000000.0: |
| 174 | raise TestFailed, 'round(1000000000.0)' |
| 175 | if round(1e20) <> 1e20: raise TestFailed, 'round(1e20)' |
| 176 | |
| 177 | if round(-1.0) <> -1.0: raise TestFailed, 'round(-1.0)' |
| 178 | if round(-10.0) <> -10.0: raise TestFailed, 'round(-10.0)' |
| 179 | if round(-1000000000.0) <> -1000000000.0: |
| 180 | raise TestFailed, 'round(-1000000000.0)' |
| 181 | if round(-1e20) <> -1e20: raise TestFailed, 'round(-1e20)' |
| 182 | |
| 183 | if round(0.1) <> 0.0: raise TestFailed, 'round(0.0)' |
| 184 | if round(1.1) <> 1.0: raise TestFailed, 'round(1.0)' |
| 185 | if round(10.1) <> 10.0: raise TestFailed, 'round(10.0)' |
| 186 | if round(1000000000.1) <> 1000000000.0: |
| 187 | raise TestFailed, 'round(1000000000.0)' |
| 188 | |
| 189 | if round(-1.1) <> -1.0: raise TestFailed, 'round(-1.0)' |
| 190 | if round(-10.1) <> -10.0: raise TestFailed, 'round(-10.0)' |
| 191 | if round(-1000000000.1) <> -1000000000.0: |
| 192 | raise TestFailed, 'round(-1000000000.0)' |
| 193 | |
| 194 | if round(0.9) <> 1.0: raise TestFailed, 'round(0.9)' |
| 195 | if round(9.9) <> 10.0: raise TestFailed, 'round(9.9)' |
| 196 | if round(999999999.9) <> 1000000000.0: |
| 197 | raise TestFailed, 'round(999999999.9)' |
| 198 | |
| 199 | if round(-0.9) <> -1.0: raise TestFailed, 'round(-0.9)' |
| 200 | if round(-9.9) <> -10.0: raise TestFailed, 'round(-9.9)' |
| 201 | if round(-999999999.9) <> -1000000000.0: |
| 202 | raise TestFailed, 'round(-999999999.9)' |
| 203 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 204 | print 'setattr' |
| 205 | import sys |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 206 | setattr(sys, 'spam', 1) |
| 207 | if sys.spam != 1: raise TestFailed, 'setattr(sys, \'spam\', 1)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 208 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 209 | print 'str' |
| 210 | if str('') <> '': raise TestFailed, 'str(\'\')' |
| 211 | if str(0) <> '0': raise TestFailed, 'str(0)' |
| 212 | if str(0L) <> '0L': raise TestFailed, 'str(0L)' |
| 213 | if str(()) <> '()': raise TestFailed, 'str(())' |
| 214 | if str([]) <> '[]': raise TestFailed, 'str([])' |
| 215 | if str({}) <> '{}': raise TestFailed, 'str({})' |
| 216 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 217 | print 'tuple' |
| 218 | if tuple(()) <> (): raise TestFailed, 'tuple(())' |
| 219 | if tuple((0, 1, 2, 3)) <> (0, 1, 2, 3): raise TestFailed, 'tuple((0, 1, 2, 3))' |
| 220 | if tuple([]) <> (): raise TestFailed, 'tuple([])' |
| 221 | if tuple([0, 1, 2, 3]) <> (0, 1, 2, 3): raise TestFailed, 'tuple([0, 1, 2, 3])' |
| 222 | if tuple('') <> (): raise TestFailed, 'tuple('')' |
| 223 | if tuple('spam') <> ('s', 'p', 'a', 'm'): raise TestFailed, "tuple('spam')" |
| 224 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 225 | print 'type' |
| 226 | if type('') <> type('123') or type('') == type(()): |
| 227 | raise TestFailed, 'type()' |
| 228 | |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 229 | print 'vars' |
| 230 | a = b = None |
| 231 | a = vars().keys() |
| 232 | b = dir() |
| 233 | a.sort() |
| 234 | b.sort() |
| 235 | if a <> b: raise TestFailed, 'vars()' |
| 236 | import sys |
| 237 | a = vars(sys).keys() |
| 238 | b = dir(sys) |
| 239 | a.sort() |
| 240 | b.sort() |
| 241 | if a <> b: raise TestFailed, 'vars(sys)' |
Guido van Rossum | a49d94a | 1995-08-11 14:24:35 +0000 | [diff] [blame] | 242 | def f0(): |
| 243 | if vars() != {}: raise TestFailed, 'vars() in f0()' |
| 244 | f0() |
| 245 | def f2(): |
| 246 | f0() |
| 247 | a = 1 |
| 248 | b = 2 |
| 249 | if vars() != {'a': a, 'b': b}: raise TestFailed, 'vars() in f2()' |
| 250 | f2() |
Guido van Rossum | e23b62f | 1994-11-10 22:25:26 +0000 | [diff] [blame] | 251 | |
| 252 | print 'xrange' |
| 253 | if tuple(xrange(10)) <> tuple(range(10)): raise TestFailed, 'xrange(10)' |
| 254 | if tuple(xrange(5,10)) <> tuple(range(5,10)): raise TestFailed, 'xrange(5,10)' |
| 255 | if tuple(xrange(0,10,2)) <> tuple(range(0,10,2)): |
| 256 | raise TestFailed, 'xrange(0,10,2)' |
| 257 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 258 | |
| 259 | # Epilogue -- unlink the temp file |
| 260 | |
| 261 | unlink(TESTFN) |