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: |
| 28 | raise TestFailed, 'f2 called with ' + `a1, a2, a3` |
| 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 | |
| 84 | print 'exec' |
| 85 | z = 0 |
| 86 | exec('z=1+1\n') |
| 87 | if z <> 2: raise TestFailed, 'exec(\'z=1+1\'\\n)' |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 88 | z = 0 |
| 89 | exec('z=1+1') |
| 90 | if z <> 2: raise TestFailed, 'exec(\'z=1+1\')' |
| 91 | |
| 92 | print 'execfile' |
| 93 | z = 0 |
| 94 | f = open(TESTFN, 'w') |
| 95 | f.write('z = z+1\n') |
| 96 | f.write('z = z*2\n') |
| 97 | f.close() |
| 98 | execfile(TESTFN) |
| 99 | unlink(TESTFN) |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 100 | |
| 101 | print 'float' |
| 102 | if float(3.14) <> 3.14: raise TestFailed, 'float(3.14)' |
| 103 | if float(314) <> 314.0: raise TestFailed, 'float(314)' |
| 104 | if float(314L) <> 314.0: raise TestFailed, 'float(314L)' |
| 105 | |
| 106 | print 'getattr' |
| 107 | import sys |
| 108 | if getattr(sys, 'stdout') is not sys.stdout: raise TestFailed, 'getattr' |
| 109 | |
| 110 | print 'hex' |
| 111 | if hex(16) != '0x10': raise TestFailed, 'hex(16)' |
| 112 | if hex(16L) != '0x10L': raise TestFailed, 'hex(16L)' |
| 113 | if hex(-16) != '-0x10': raise TestFailed, 'hex(-16)' |
| 114 | if hex(-16L) != '-0x10L': raise TestFailed, 'hex(-16L)' |
| 115 | |
| 116 | # Test input() later, together with raw_input |
| 117 | |
| 118 | print 'int' |
| 119 | if int(314) <> 314: raise TestFailed, 'int(314)' |
| 120 | if int(3.14) <> 3: raise TestFailed, 'int(3.14)' |
| 121 | if int(314L) <> 314: raise TestFailed, 'int(314L)' |
| 122 | |
| 123 | print 'len' |
| 124 | if len('123') <> 3: raise TestFailed, 'len(\'123\')' |
| 125 | if len(()) <> 0: raise TestFailed, 'len(())' |
| 126 | if len((1, 2, 3, 4)) <> 4: raise TestFailed, 'len((1, 2, 3, 4))' |
| 127 | if len([1, 2, 3, 4]) <> 4: raise TestFailed, 'len([1, 2, 3, 4])' |
| 128 | if len({}) <> 0: raise TestFailed, 'len({})' |
| 129 | if len({'a':1, 'b': 2}) <> 2: raise TestFailed, 'len({\'a\':1, \'b\': 2})' |
| 130 | |
| 131 | print 'long' |
| 132 | if long(314) <> 314L: raise TestFailed, 'long(314)' |
| 133 | if long(3.14) <> 3L: raise TestFailed, 'long(3.14)' |
| 134 | if long(314L) <> 314L: raise TestFailed, 'long(314L)' |
| 135 | |
| 136 | print 'max' |
| 137 | if max('123123') <> '3': raise TestFailed, 'max(\'123123\')' |
| 138 | if max(1, 2, 3) <> 3: raise TestFailed, 'max(1, 2, 3)' |
| 139 | if max((1, 2, 3, 1, 2, 3)) <> 3: raise TestFailed, 'max((1, 2, 3, 1, 2, 3))' |
| 140 | if max([1, 2, 3, 1, 2, 3]) <> 3: raise TestFailed, 'max([1, 2, 3, 1, 2, 3])' |
| 141 | # |
| 142 | if max(1, 2L, 3.0) <> 3.0: raise TestFailed, 'max(1, 2L, 3.0)' |
| 143 | if max(1L, 2.0, 3) <> 3: raise TestFailed, 'max(1L, 2.0, 3)' |
| 144 | if max(1.0, 2, 3L) <> 3L: raise TestFailed, 'max(1.0, 2, 3L)' |
| 145 | |
| 146 | print 'min' |
| 147 | if min('123123') <> '1': raise TestFailed, 'min(\'123123\')' |
| 148 | if min(1, 2, 3) <> 1: raise TestFailed, 'min(1, 2, 3)' |
| 149 | if min((1, 2, 3, 1, 2, 3)) <> 1: raise TestFailed, 'min((1, 2, 3, 1, 2, 3))' |
| 150 | if min([1, 2, 3, 1, 2, 3]) <> 1: raise TestFailed, 'min([1, 2, 3, 1, 2, 3])' |
| 151 | # |
| 152 | if min(1, 2L, 3.0) <> 1: raise TestFailed, 'min(1, 2L, 3.0)' |
| 153 | if min(1L, 2.0, 3) <> 1L: raise TestFailed, 'min(1L, 2.0, 3)' |
| 154 | if min(1.0, 2, 3L) <> 1.0: raise TestFailed, 'min(1.0, 2, 3L)' |