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)' |
| 8 | if oct(-100) != '-0144': raise TestFailed, 'oct(-100)' |
| 9 | if oct(-100L) != '-0144L': raise TestFailed, 'oct(-100L)' |
| 10 | |
| 11 | print 'open' |
| 12 | # NB the first 4 lines are also used to test input and raw_input, below |
| 13 | fp = open(TESTFN, 'w') |
| 14 | try: |
| 15 | fp.write('1+1\n') |
| 16 | fp.write('1+1\n') |
| 17 | fp.write('The quick brown fox jumps over the lazy dog') |
| 18 | fp.write('.\n') |
| 19 | fp.write('Dear John\n') |
| 20 | fp.write('XXX'*100) |
| 21 | fp.write('YYY'*100) |
| 22 | finally: |
| 23 | fp.close() |
| 24 | # |
| 25 | fp = open(TESTFN, 'r') |
| 26 | try: |
| 27 | if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact' |
| 28 | if fp.readline(4) <> '1+1\n': raise TestFailed, 'readline(4) # exact' |
| 29 | if fp.readline() <> 'The quick brown fox jumps over the lazy dog.\n': |
| 30 | raise TestFailed, 'readline() # default' |
| 31 | if fp.readline(4) <> 'Dear': raise TestFailed, 'readline(4) # short' |
| 32 | if fp.readline(100) <> ' John\n': raise TestFailed, 'readline(100)' |
| 33 | if fp.read(300) <> 'XXX'*100: raise TestFailed, 'read(300)' |
| 34 | if fp.read(1000) <> 'YYY'*100: raise TestFailed, 'read(1000) # truncate' |
| 35 | finally: |
| 36 | fp.close() |
| 37 | |
| 38 | print 'ord' |
| 39 | if ord(' ') <> 32: raise TestFailed, 'ord(\' \')' |
| 40 | if ord('A') <> 65: raise TestFailed, 'ord(\'A\')' |
| 41 | if ord('a') <> 97: raise TestFailed, 'ord(\'a\')' |
| 42 | |
| 43 | print 'pow' |
| 44 | if pow(0,0) <> 1: raise TestFailed, 'pow(0,0)' |
| 45 | if pow(0,1) <> 0: raise TestFailed, 'pow(0,1)' |
| 46 | if pow(1,0) <> 1: raise TestFailed, 'pow(1,0)' |
| 47 | if pow(1,1) <> 1: raise TestFailed, 'pow(1,1)' |
| 48 | # |
| 49 | if pow(2,0) <> 1: raise TestFailed, 'pow(2,0)' |
| 50 | if pow(2,10) <> 1024: raise TestFailed, 'pow(2,10)' |
| 51 | if pow(2,20) <> 1024*1024: raise TestFailed, 'pow(2,20)' |
| 52 | if pow(2,30) <> 1024*1024*1024: raise TestFailed, 'pow(2,30)' |
| 53 | # |
| 54 | if pow(-2,0) <> 1: raise TestFailed, 'pow(-2,0)' |
| 55 | if pow(-2,1) <> -2: raise TestFailed, 'pow(-2,1)' |
| 56 | if pow(-2,2) <> 4: raise TestFailed, 'pow(-2,2)' |
| 57 | if pow(-2,3) <> -8: raise TestFailed, 'pow(-2,3)' |
| 58 | # |
| 59 | if pow(0L,0) <> 1: raise TestFailed, 'pow(0L,0)' |
| 60 | if pow(0L,1) <> 0: raise TestFailed, 'pow(0L,1)' |
| 61 | if pow(1L,0) <> 1: raise TestFailed, 'pow(1L,0)' |
| 62 | if pow(1L,1) <> 1: raise TestFailed, 'pow(1L,1)' |
| 63 | # |
| 64 | if pow(2L,0) <> 1: raise TestFailed, 'pow(2L,0)' |
| 65 | if pow(2L,10) <> 1024: raise TestFailed, 'pow(2L,10)' |
| 66 | if pow(2L,20) <> 1024*1024: raise TestFailed, 'pow(2L,20)' |
| 67 | if pow(2L,30) <> 1024*1024*1024: raise TestFailed, 'pow(2L,30)' |
| 68 | # |
| 69 | if pow(-2L,0) <> 1: raise TestFailed, 'pow(-2L,0)' |
| 70 | if pow(-2L,1) <> -2: raise TestFailed, 'pow(-2L,1)' |
| 71 | if pow(-2L,2) <> 4: raise TestFailed, 'pow(-2L,2)' |
| 72 | if pow(-2L,3) <> -8: raise TestFailed, 'pow(-2L,3)' |
| 73 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame^] | 74 | if fcmp(pow(0.,0), 1.): raise TestFailed, 'pow(0.,0)' |
| 75 | if fcmp(pow(0.,1), 0.): raise TestFailed, 'pow(0.,1)' |
| 76 | if fcmp(pow(1.,0), 1.): raise TestFailed, 'pow(1.,0)' |
| 77 | if fcmp(pow(1.,1), 1.): raise TestFailed, 'pow(1.,1)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 78 | # |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame^] | 79 | if fcmp(pow(2.,0), 1.): raise TestFailed, 'pow(2.,0)' |
| 80 | if fcmp(pow(2.,10), 1024.): raise TestFailed, 'pow(2.,10)' |
| 81 | if fcmp(pow(2.,20), 1024.*1024.): raise TestFailed, 'pow(2.,20)' |
| 82 | 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] | 83 | # |
| 84 | # XXX These don't work -- negative float to the float power... |
Guido van Rossum | 35fb82a | 1993-01-26 13:04:43 +0000 | [diff] [blame^] | 85 | #if fcmp(pow(-2.,0), 1.): raise TestFailed, 'pow(-2.,0)' |
| 86 | #if fcmp(pow(-2.,1), -2.): raise TestFailed, 'pow(-2.,1)' |
| 87 | #if fcmp(pow(-2.,2), 4.): raise TestFailed, 'pow(-2.,2)' |
| 88 | #if fcmp(pow(-2.,3), -8.): raise TestFailed, 'pow(-2.,3)' |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 89 | |
| 90 | print 'range' |
| 91 | if range(3) <> [0, 1, 2]: raise TestFailed, 'range(3)' |
| 92 | if range(1, 5) <> [1, 2, 3, 4]: raise TestFailed, 'range(1, 5)' |
| 93 | if range(0) <> []: raise TestFailed, 'range(0)' |
| 94 | if range(-3) <> []: raise TestFailed, 'range(-3)' |
| 95 | if range(1, 10, 3) <> [1, 4, 7]: raise TestFailed, 'range(1, 10, 3)' |
| 96 | if range(5, -5, -3) <> [5, 2, -1, -4]: raise TestFailed, 'range(5, -5, -3)' |
| 97 | |
| 98 | print 'input and raw_input' |
| 99 | import sys |
| 100 | fp = open(TESTFN, 'r') |
| 101 | savestdin = sys.stdin |
| 102 | try: |
| 103 | sys.stdin = fp |
| 104 | if input() <> 2: raise TestFailed, 'input()' |
| 105 | if input('testing\n') <> 2: raise TestFailed, 'input()' |
| 106 | if raw_input() <> 'The quick brown fox jumps over the lazy dog.': |
| 107 | raise TestFailed, 'raw_input()' |
| 108 | if raw_input('testing\n') <> 'Dear John': |
| 109 | raise TestFailed, 'raw_input(\'testing\\n\')' |
| 110 | finally: |
| 111 | sys.stdin = savestdin |
| 112 | fp.close() |
| 113 | |
| 114 | print 'reload' |
| 115 | import string |
| 116 | reload(string) |
| 117 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 118 | print 'repr' |
| 119 | if repr('') <> '\'\'': raise TestFailed, 'repr(\'\')' |
| 120 | if repr(0) <> '0': raise TestFailed, 'repr(0)' |
| 121 | if repr(0L) <> '0L': raise TestFailed, 'repr(0L)' |
| 122 | if repr(()) <> '()': raise TestFailed, 'repr(())' |
| 123 | if repr([]) <> '[]': raise TestFailed, 'repr([])' |
| 124 | if repr({}) <> '{}': raise TestFailed, 'repr({})' |
| 125 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 126 | print 'setattr' |
| 127 | import sys |
| 128 | setattr(sys, 'foobar', 1) |
| 129 | if sys.foobar != 1: raise TestFailed, 'setattr(sys, \'foobar\', 1)' |
| 130 | |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 131 | print 'str' |
| 132 | if str('') <> '': raise TestFailed, 'str(\'\')' |
| 133 | if str(0) <> '0': raise TestFailed, 'str(0)' |
| 134 | if str(0L) <> '0L': raise TestFailed, 'str(0L)' |
| 135 | if str(()) <> '()': raise TestFailed, 'str(())' |
| 136 | if str([]) <> '[]': raise TestFailed, 'str([])' |
| 137 | if str({}) <> '{}': raise TestFailed, 'str({})' |
| 138 | |
Guido van Rossum | 3bead09 | 1992-01-27 17:00:37 +0000 | [diff] [blame] | 139 | print 'type' |
| 140 | if type('') <> type('123') or type('') == type(()): |
| 141 | raise TestFailed, 'type()' |
| 142 | |
| 143 | |
| 144 | # Epilogue -- unlink the temp file |
| 145 | |
| 146 | unlink(TESTFN) |