Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 1 | # Python test set -- part 4a, built-in functions a-m |
| 2 | |
| 3 | from test_support import * |
| 4 | |
| 5 | print 'abs' |
| 6 | if abs(0) <> 0: raise TestFailed, 'abs(0)' |
| 7 | if abs(1234) <> 1234: raise TestFailed, 'abs(1234)' |
| 8 | if abs(-1234) <> 1234: raise TestFailed, 'abs(-1234)' |
| 9 | # |
| 10 | if abs(0.0) <> 0.0: raise TestFailed, 'abs(0.0)' |
| 11 | if abs(3.14) <> 3.14: raise TestFailed, 'abs(3.14)' |
| 12 | if abs(-3.14) <> 3.14: raise TestFailed, 'abs(-3.14)' |
| 13 | # |
| 14 | if abs(0L) <> 0L: raise TestFailed, 'abs(0L)' |
| 15 | if abs(1234L) <> 1234L: raise TestFailed, 'abs(1234L)' |
| 16 | if abs(-1234L) <> 1234L: raise TestFailed, 'abs(-1234L)' |
| 17 | |
| 18 | print 'apply' |
| 19 | def f0(*args): |
| 20 | if args != (): raise TestFailed, 'f0 called with ' + `args` |
| 21 | def f1(a1): |
| 22 | if a1 != 1: raise TestFailed, 'f1 called with ' + `a1` |
| 23 | def f2(a1, a2): |
| 24 | if a1 != 1 or a2 != 2: |
| 25 | raise TestFailed, 'f2 called with ' + `a1, a2` |
| 26 | def f3(a1, a2, a3): |
| 27 | if a1 != 1 or a2 != 2 or a3 != 3: |
Guido van Rossum | b3b09c9 | 1993-10-22 14:24:22 +0000 | [diff] [blame] | 28 | raise TestFailed, 'f3 called with ' + `a1, a2, a3` |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 29 | apply(f0, ()) |
| 30 | apply(f1, (1,)) |
| 31 | apply(f2, (1, 2)) |
| 32 | apply(f3, (1, 2, 3)) |
| 33 | |
| 34 | print 'chr' |
| 35 | if chr(32) <> ' ': raise TestFailed, 'chr(32)' |
| 36 | if chr(65) <> 'A': raise TestFailed, 'chr(65)' |
| 37 | if chr(97) <> 'a': raise TestFailed, 'chr(97)' |
| 38 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 39 | print 'cmp' |
| 40 | if cmp(-1, 1) <> -1: raise TestFailed, 'cmp(-1, 1)' |
| 41 | if cmp(1, -1) <> 1: raise TestFailed, 'cmp(1, -1)' |
| 42 | if cmp(1, 1) <> 0: raise TestFailed, 'cmp(1, 1)' |
| 43 | |
| 44 | print 'coerce' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 45 | if fcmp(coerce(1, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1, 1.1)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 46 | if coerce(1, 1L) <> (1L, 1L): raise TestFailed, 'coerce(1, 1L)' |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 47 | if fcmp(coerce(1L, 1.1), (1.0, 1.1)): raise TestFailed, 'coerce(1L, 1.1)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 48 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 49 | print 'dir' |
| 50 | x = 1 |
| 51 | if 'x' not in dir(): raise TestFailed, 'dir()' |
| 52 | import sys |
| 53 | if 'modules' not in dir(sys): raise TestFailed, 'dir(sys)' |
| 54 | |
| 55 | print 'divmod' |
| 56 | if divmod(12, 7) <> (1, 5): raise TestFailed, 'divmod(12, 7)' |
| 57 | if divmod(-12, 7) <> (-2, 2): raise TestFailed, 'divmod(-12, 7)' |
| 58 | if divmod(12, -7) <> (-2, -2): raise TestFailed, 'divmod(12, -7)' |
| 59 | if divmod(-12, -7) <> (1, -5): raise TestFailed, 'divmod(-12, -7)' |
| 60 | # |
| 61 | if divmod(12L, 7L) <> (1L, 5L): raise TestFailed, 'divmod(12L, 7L)' |
| 62 | if divmod(-12L, 7L) <> (-2L, 2L): raise TestFailed, 'divmod(-12L, 7L)' |
| 63 | if divmod(12L, -7L) <> (-2L, -2L): raise TestFailed, 'divmod(12L, -7L)' |
| 64 | if divmod(-12L, -7L) <> (1L, -5L): raise TestFailed, 'divmod(-12L, -7L)' |
| 65 | # |
| 66 | if divmod(12, 7L) <> (1, 5L): raise TestFailed, 'divmod(12, 7L)' |
| 67 | if divmod(-12, 7L) <> (-2, 2L): raise TestFailed, 'divmod(-12, 7L)' |
| 68 | if divmod(12L, -7) <> (-2L, -2): raise TestFailed, 'divmod(12L, -7)' |
| 69 | if divmod(-12L, -7) <> (1L, -5): raise TestFailed, 'divmod(-12L, -7)' |
| 70 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame] | 71 | if fcmp(divmod(3.25, 1.0), (3.0, 0.25)): |
| 72 | raise TestFailed, 'divmod(3.25, 1.0)' |
| 73 | if fcmp(divmod(-3.25, 1.0), (-4.0, 0.75)): |
| 74 | raise TestFailed, 'divmod(-3.25, 1.0)' |
| 75 | if fcmp(divmod(3.25, -1.0), (-4.0, -0.75)): |
| 76 | raise TestFailed, 'divmod(3.25, -1.0)' |
| 77 | if fcmp(divmod(-3.25, -1.0), (3.0, -0.25)): |
| 78 | raise TestFailed, 'divmod(-3.25, -1.0)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 79 | |
| 80 | print 'eval' |
| 81 | if eval('1+1') <> 2: raise TestFailed, 'eval(\'1+1\')' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 82 | if eval(' 1+1\n') <> 2: raise TestFailed, 'eval(\' 1+1\\n\')' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 83 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 84 | print 'execfile' |
| 85 | z = 0 |
| 86 | f = open(TESTFN, 'w') |
| 87 | f.write('z = z+1\n') |
| 88 | f.write('z = z*2\n') |
| 89 | f.close() |
| 90 | execfile(TESTFN) |
| 91 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 92 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 93 | print 'filter' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 94 | if filter(lambda c: 'a' <= c <= 'z', 'Hello World') <> 'elloorld': |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 95 | raise TestFailed, 'filter (filter a string)' |
| 96 | if filter(None, [1, 'hello', [], [3], '', None, 9, 0]) <> [1, 'hello', [3], 9]: |
| 97 | raise TestFailed, 'filter (remove false values)' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 98 | if filter(lambda x: x > 0, [1, -3, 9, 0, 2]) <> [1, 9, 2]: |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 99 | raise TestFailed, 'filter (keep positives)' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame^] | 100 | class Squares: |
| 101 | def __init__(self, max): |
| 102 | self.max = max |
| 103 | self.sofar = [] |
| 104 | def __len__(self): return len(self.sofar) |
| 105 | def __getitem__(self, i): |
| 106 | if not 0 <= i < self.max: raise IndexError |
| 107 | n = len(self.sofar) |
| 108 | while n <= i: |
| 109 | self.sofar.append(n*n) |
| 110 | n = n+1 |
| 111 | return self.sofar[i] |
| 112 | if filter(None, Squares(10)) != [1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 113 | raise TestFailed, 'filter(None, Squares(10))' |
| 114 | if filter(lambda x: x%2, Squares(10)) != [1, 9, 25, 49, 81]: |
| 115 | raise TestFailed, 'filter(oddp, Squares(10))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 117 | print 'float' |
| 118 | if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' |
| 119 | if float(314) <> 314.0: raise TestFailed, 'float(314)' |
| 120 | if float(314L) <> 314.0: raise TestFailed, 'float(314L)' |
| 121 | |
| 122 | print 'getattr' |
| 123 | import sys |
| 124 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
| 125 | |
| 126 | print 'hex' |
| 127 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 128 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
| 129 | if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)' |
| 130 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 131 | |
| 132 | # Test input() later, together with raw_input |
| 133 | |
| 134 | print 'int' |
| 135 | if int(314) <> 314: raise TestFailed, 'int(314)' |
| 136 | if int(3.14) <> 3: raise TestFailed, 'int(3.14)' |
| 137 | if int(314L) <> 314: raise TestFailed, 'int(314L)' |
| 138 | |
| 139 | print 'len' |
| 140 | if len('123') <> 3: raise TestFailed, 'len(\'123\')' |
| 141 | if len(()) <> 0: raise TestFailed, 'len(())' |
| 142 | if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 143 | if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 144 | if len({}) <> 0: raise TestFailed, 'len({})' |
| 145 | if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})' |
| 146 | |
| 147 | print 'long' |
| 148 | if long(314) <> 314L: raise TestFailed, 'long(314)' |
| 149 | if long(3.14) <> 3L: raise TestFailed, 'long(3.14)' |
| 150 | if long(314L) <> 314L: raise TestFailed, 'long(314L)' |
| 151 | |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 152 | print 'map' |
| 153 | if map(None, 'hello world') <> ['h','e','l','l','o',' ','w','o','r','l','d']: |
| 154 | raise TestFailed, 'map(None, \'hello world\')' |
| 155 | if map(None, 'abcd', 'efg') <> \ |
| 156 | [('a', 'e'), ('b', 'f'), ('c', 'g'), ('d', None)]: |
| 157 | raise TestFailed, 'map(None, \'abcd\', \'efg\')' |
| 158 | if map(None, range(10)) <> [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]: |
| 159 | raise TestFailed, 'map(None, range(10))' |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 160 | if map(lambda x: x*x, range(1,4)) <> [1, 4, 9]: |
| 161 | raise TestFailed, 'map(lambda x: x*x, range(1,4))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 162 | from math import sqrt |
Guido van Rossum | ae3b3a3 | 1993-11-30 13:43:54 +0000 | [diff] [blame] | 163 | if map(lambda x: map(sqrt,x), [[16, 4], [81, 9]]) <> [[4.0, 2.0], [9.0, 3.0]]: |
| 164 | raise TestFailed, 'map(lambda x: map(sqrt,x), [[16, 4], [81, 9]])' |
| 165 | if map(lambda x, y: x+y, [1,3,2], [9,1,4]) <> [10, 4, 6]: |
| 166 | raise TestFailed, 'map(lambda x,y: x+y, [1,3,2], [9,1,4])' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 167 | def plus(*v): |
| 168 | accu = 0 |
| 169 | for i in v: accu = accu + i |
| 170 | return accu |
| 171 | if map(plus, [1, 3, 7]) <> [1, 3, 7]: |
| 172 | raise TestFailed, 'map(plus, [1, 3, 7])' |
| 173 | if map(plus, [1, 3, 7], [4, 9, 2]) <> [1+4, 3+9, 7+2]: |
| 174 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2])' |
| 175 | if map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0]) <> [1+4+1, 3+9+1, 7+2+0]: |
| 176 | raise TestFailed, 'map(plus, [1, 3, 7], [4, 9, 2], [1, 1, 0])' |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame^] | 177 | if map(None, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 178 | raise TestFailed, 'map(None, Squares(10))' |
| 179 | if map(int, Squares(10)) != [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]: |
| 180 | raise TestFailed, 'map(int, Squares(10))' |
| 181 | if map(None, Squares(3), Squares(2)) != [(0,0), (1,1), (4,None)]: |
| 182 | raise TestFailed, 'map(None: x, Squares(3), Squares(2))' |
| 183 | if map(max, Squares(3), Squares(2)) != [0, 1, 4]: |
| 184 | raise TestFailed, 'map(None: x, Squares(3), Squares(2))' |
Guido van Rossum | e65cce5 | 1993-11-08 15:05:21 +0000 | [diff] [blame] | 185 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 186 | print 'max' |
| 187 | if max('123123') <> '3': raise TestFailed, 'max(\'123123\')' |
| 188 | if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)' |
| 189 | if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 190 | if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
| 191 | # |
| 192 | if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 193 | if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 194 | if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)' |
| 195 | |
| 196 | print 'min' |
| 197 | if min('123123') <> '1': raise TestFailed, 'min(\'123123\')' |
| 198 | if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)' |
| 199 | if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 200 | if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
| 201 | # |
| 202 | if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 203 | if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 204 | if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |