Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
Walter Dörwald | 9b77553 | 2007-06-08 14:30:53 +0000 | [diff] [blame] | 3 | from test.test_support import run_unittest |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 4 | import unittest |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 5 | import sys |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 6 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 7 | class TypesTests(unittest.TestCase): |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 8 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 9 | def test_truth_values(self): |
| 10 | if None: self.fail('None is true instead of false') |
| 11 | if 0: self.fail('0 is true instead of false') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 12 | if 0: self.fail('0L is true instead of false') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 13 | if 0.0: self.fail('0.0 is true instead of false') |
| 14 | if '': self.fail('\'\' is true instead of false') |
| 15 | if not 1: self.fail('1 is false instead of true') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 16 | if not 1: self.fail('1L is false instead of true') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 17 | if not 1.0: self.fail('1.0 is false instead of true') |
| 18 | if not 'x': self.fail('\'x\' is false instead of true') |
| 19 | if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true') |
| 20 | def f(): pass |
| 21 | class C: pass |
| 22 | import sys |
| 23 | x = C() |
| 24 | if not f: self.fail('f is false instead of true') |
| 25 | if not C: self.fail('C is false instead of true') |
| 26 | if not sys: self.fail('sys is false instead of true') |
| 27 | if not x: self.fail('x is false instead of true') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 28 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 29 | def test_boolean_ops(self): |
| 30 | if 0 or 0: self.fail('0 or 0 is true instead of false') |
| 31 | if 1 and 1: pass |
| 32 | else: self.fail('1 and 1 is false instead of true') |
| 33 | if not 1: self.fail('not 1 is true instead of false') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 34 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 35 | def test_comparisons(self): |
| 36 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
| 37 | else: self.fail('int comparisons failed') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 38 | if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 39 | else: self.fail('long int comparisons failed') |
| 40 | if 0.0 < 1.0 <= 1.0 == 1.0 >= 1.0 > 0.0 != 1.0: pass |
| 41 | else: self.fail('float comparisons failed') |
| 42 | if '' < 'a' <= 'a' == 'a' < 'abc' < 'abd' < 'b': pass |
| 43 | else: self.fail('string comparisons failed') |
| 44 | if None is None: pass |
| 45 | else: self.fail('identity test failed') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 46 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 47 | def test_float_constructor(self): |
| 48 | self.assertRaises(ValueError, float, '') |
| 49 | self.assertRaises(ValueError, float, '5\0') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 50 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 51 | def test_zero_division(self): |
| 52 | try: 5.0 / 0.0 |
| 53 | except ZeroDivisionError: pass |
| 54 | else: self.fail("5.0 / 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 55 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 56 | try: 5.0 // 0.0 |
| 57 | except ZeroDivisionError: pass |
| 58 | else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 59 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 60 | try: 5.0 % 0.0 |
| 61 | except ZeroDivisionError: pass |
| 62 | else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 63 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 64 | try: 5 / 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 65 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame^] | 66 | else: self.fail("5 / 0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 67 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 68 | try: 5 // 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 69 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame^] | 70 | else: self.fail("5 // 0 didn't raise ZeroDivisionError") |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 71 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 72 | try: 5 % 0 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 73 | except ZeroDivisionError: pass |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame^] | 74 | else: self.fail("5 % 0 didn't raise ZeroDivisionError") |
Tim Peters | a3c01ce | 2001-12-04 23:05:10 +0000 | [diff] [blame] | 75 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 76 | def test_numeric_types(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 77 | if 0 != 0 or 0 != 0.0 or 0 != 0.0: self.fail('mixed comparisons') |
| 78 | if 1 != 1 or 1 != 1.0 or 1 != 1.0: self.fail('mixed comparisons') |
| 79 | if -1 != -1 or -1 != -1.0 or -1 != -1.0: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 80 | self.fail('int/long/float value not equal') |
| 81 | # calling built-in types without argument must return 0 |
| 82 | if int() != 0: self.fail('int() does not return 0') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame^] | 83 | if int() != 0: self.fail('long() does not return 0') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 84 | if float() != 0.0: self.fail('float() does not return 0.0') |
| 85 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
| 86 | else: self.fail('int() does not round properly') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 87 | if int(1.9) == 1 == int(1.1) and int(-1.1) == -1 == int(-1.9): pass |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | else: self.fail('long() does not round properly') |
| 89 | if float(1) == 1.0 and float(-1) == -1.0 and float(0) == 0.0: pass |
| 90 | else: self.fail('float() does not work properly') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 91 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 92 | def test_normal_integers(self): |
| 93 | # Ensure the first 256 integers are shared |
| 94 | a = 256 |
| 95 | b = 128*2 |
| 96 | if a is not b: self.fail('256 is not shared') |
| 97 | if 12 + 24 != 36: self.fail('int op') |
| 98 | if 12 + (-24) != -12: self.fail('int op') |
| 99 | if (-12) + 24 != 12: self.fail('int op') |
| 100 | if (-12) + (-24) != -36: self.fail('int op') |
| 101 | if not 12 < 24: self.fail('int op') |
| 102 | if not -24 < -12: self.fail('int op') |
| 103 | # Test for a particular bug in integer multiply |
| 104 | xsize, ysize, zsize = 238, 356, 4 |
| 105 | if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): |
| 106 | self.fail('int mul commutativity') |
| 107 | # And another. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 108 | m = -sys.maxsize - 1 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 109 | for divisor in 1, 2, 4, 8, 16, 32: |
| 110 | j = m // divisor |
| 111 | prod = divisor * j |
| 112 | if prod != m: |
| 113 | self.fail("%r * %r == %r != %r" % (divisor, j, prod, m)) |
| 114 | if type(prod) is not int: |
| 115 | self.fail("expected type(prod) to be int, not %r" % |
| 116 | type(prod)) |
| 117 | # Check for expected * overflow to long. |
| 118 | for divisor in 1, 2, 4, 8, 16, 32: |
| 119 | j = m // divisor - 1 |
| 120 | prod = divisor * j |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 121 | if type(prod) is not int: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 122 | self.fail("expected type(%r) to be long, not %r" % |
| 123 | (prod, type(prod))) |
| 124 | # Check for expected * overflow to long. |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 125 | m = sys.maxsize |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 126 | for divisor in 1, 2, 4, 8, 16, 32: |
| 127 | j = m // divisor + 1 |
| 128 | prod = divisor * j |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 129 | if type(prod) is not int: |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 130 | self.fail("expected type(%r) to be long, not %r" % |
| 131 | (prod, type(prod))) |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 132 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 133 | def test_long_integers(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 134 | if 12 + 24 != 36: self.fail('long op') |
| 135 | if 12 + (-24) != -12: self.fail('long op') |
| 136 | if (-12) + 24 != 12: self.fail('long op') |
| 137 | if (-12) + (-24) != -36: self.fail('long op') |
| 138 | if not 12 < 24: self.fail('long op') |
| 139 | if not -24 < -12: self.fail('long op') |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 140 | x = sys.maxsize |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 141 | if int(int(x)) != x: self.fail('long op') |
| 142 | try: y = int(int(x)+1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 143 | except OverflowError: self.fail('long op') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 144 | if not isinstance(y, int): self.fail('long op') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 145 | x = -x |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 146 | if int(int(x)) != x: self.fail('long op') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 147 | x = x-1 |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 148 | if int(int(x)) != x: self.fail('long op') |
| 149 | try: y = int(int(x)-1) |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 150 | except OverflowError: self.fail('long op') |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 151 | if not isinstance(y, int): self.fail('long op') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 152 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 153 | try: 5 << -5 |
| 154 | except ValueError: pass |
| 155 | else: self.fail('int negative shift <<') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 156 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 157 | try: 5 << -5 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 158 | except ValueError: pass |
| 159 | else: self.fail('long negative shift <<') |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 160 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 161 | try: 5 >> -5 |
| 162 | except ValueError: pass |
| 163 | else: self.fail('int negative shift >>') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 164 | |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 165 | try: 5 >> -5 |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 166 | except ValueError: pass |
| 167 | else: self.fail('long negative shift >>') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 168 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 169 | def test_floats(self): |
| 170 | if 12.0 + 24.0 != 36.0: self.fail('float op') |
| 171 | if 12.0 + (-24.0) != -12.0: self.fail('float op') |
| 172 | if (-12.0) + 24.0 != 12.0: self.fail('float op') |
| 173 | if (-12.0) + (-24.0) != -36.0: self.fail('float op') |
| 174 | if not 12.0 < 24.0: self.fail('float op') |
| 175 | if not -24.0 < -12.0: self.fail('float op') |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 176 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 177 | def test_strings(self): |
| 178 | if len('') != 0: self.fail('len(\'\')') |
| 179 | if len('a') != 1: self.fail('len(\'a\')') |
| 180 | if len('abcdef') != 6: self.fail('len(\'abcdef\')') |
| 181 | if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation') |
| 182 | if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3') |
| 183 | if 0*'abcde' != '': self.fail('string repetition 0*') |
| 184 | if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string') |
| 185 | if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass |
| 186 | else: self.fail('in/not in string') |
| 187 | x = 'x'*103 |
| 188 | if '%s!'%x != x+'!': self.fail('nasty string formatting bug') |
Michael W. Hudson | 5efaf7e | 2002-06-11 10:55:12 +0000 | [diff] [blame] | 189 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 190 | #extended slices for strings |
| 191 | a = '0123456789' |
| 192 | self.assertEqual(a[::], a) |
| 193 | self.assertEqual(a[::2], '02468') |
| 194 | self.assertEqual(a[1::2], '13579') |
| 195 | self.assertEqual(a[::-1],'9876543210') |
| 196 | self.assertEqual(a[::-2], '97531') |
| 197 | self.assertEqual(a[3::-2], '31') |
| 198 | self.assertEqual(a[-100:100:], a) |
| 199 | self.assertEqual(a[100:-100:-1], a[::-1]) |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 200 | self.assertEqual(a[-100:100:2], '02468') |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 201 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 202 | def test_type_function(self): |
| 203 | self.assertRaises(TypeError, type, 1, 2) |
| 204 | self.assertRaises(TypeError, type, 1, 2, 3, 4) |
Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 205 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame^] | 206 | def test_int__format__(self): |
| 207 | def test(i, format_spec, result): |
| 208 | # just make sure I'm not accidentally checking longs |
| 209 | assert type(i) == int |
| 210 | assert type(format_spec) == str |
| 211 | self.assertEqual(i.__format__(format_spec), result) |
| 212 | |
| 213 | test(123456789, 'd', '123456789') |
| 214 | test(123456789, 'd', '123456789') |
| 215 | |
| 216 | test(1, 'c', '\01') |
| 217 | |
| 218 | # sign and aligning are interdependent |
| 219 | test(1, "-", '1') |
| 220 | test(-1, "-", '-1') |
| 221 | test(1, "-3", ' 1') |
| 222 | test(-1, "-3", ' -1') |
| 223 | test(1, "+3", ' +1') |
| 224 | test(-1, "+3", ' -1') |
| 225 | test(1, " 3", ' 1') |
| 226 | test(-1, " 3", ' -1') |
| 227 | test(1, " ", ' 1') |
| 228 | test(-1, " ", '-1') |
| 229 | |
| 230 | # hex |
| 231 | test(3, "x", "3") |
| 232 | test(3, "X", "3") |
| 233 | test(1234, "x", "4d2") |
| 234 | test(-1234, "x", "-4d2") |
| 235 | test(1234, "8x", " 4d2") |
| 236 | test(-1234, "8x", " -4d2") |
| 237 | test(1234, "x", "4d2") |
| 238 | test(-1234, "x", "-4d2") |
| 239 | test(-3, "x", "-3") |
| 240 | test(-3, "X", "-3") |
| 241 | test(int('be', 16), "x", "be") |
| 242 | test(int('be', 16), "X", "BE") |
| 243 | test(-int('be', 16), "x", "-be") |
| 244 | test(-int('be', 16), "X", "-BE") |
| 245 | |
| 246 | # octal |
| 247 | test(3, "o", "3") |
| 248 | test(-3, "o", "-3") |
| 249 | test(65, "o", "101") |
| 250 | test(-65, "o", "-101") |
| 251 | test(1234, "o", "2322") |
| 252 | test(-1234, "o", "-2322") |
| 253 | test(1234, "-o", "2322") |
| 254 | test(-1234, "-o", "-2322") |
| 255 | test(1234, " o", " 2322") |
| 256 | test(-1234, " o", "-2322") |
| 257 | test(1234, "+o", "+2322") |
| 258 | test(-1234, "+o", "-2322") |
| 259 | |
| 260 | # binary |
| 261 | test(3, "b", "11") |
| 262 | test(-3, "b", "-11") |
| 263 | test(1234, "b", "10011010010") |
| 264 | test(-1234, "b", "-10011010010") |
| 265 | test(1234, "-b", "10011010010") |
| 266 | test(-1234, "-b", "-10011010010") |
| 267 | test(1234, " b", " 10011010010") |
| 268 | test(-1234, " b", "-10011010010") |
| 269 | test(1234, "+b", "+10011010010") |
| 270 | test(-1234, "+b", "-10011010010") |
| 271 | |
| 272 | # make sure these are errors |
| 273 | |
| 274 | # precision disallowed |
| 275 | self.assertRaises(ValueError, 3 .__format__, "1.3") |
| 276 | # sign not allowed with 'c' |
| 277 | self.assertRaises(ValueError, 3 .__format__, "+c") |
| 278 | # format spec must be string |
| 279 | self.assertRaises(TypeError, 3 .__format__, None) |
| 280 | self.assertRaises(TypeError, 3 .__format__, 0) |
| 281 | |
| 282 | # ensure that only int and float type specifiers work |
| 283 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 284 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 285 | if not format_spec in 'bcdoxXeEfFgGn%': |
| 286 | self.assertRaises(ValueError, 0 .__format__, format_spec) |
| 287 | self.assertRaises(ValueError, 1 .__format__, format_spec) |
| 288 | self.assertRaises(ValueError, (-1) .__format__, format_spec) |
| 289 | |
| 290 | # ensure that float type specifiers work; format converts |
| 291 | # the int to a float |
| 292 | for format_spec in 'eEfFgGn%': |
| 293 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 294 | self.assertEqual(value.__format__(format_spec), |
| 295 | float(value).__format__(format_spec)) |
| 296 | |
| 297 | def test_long__format__(self): |
| 298 | def test(i, format_spec, result): |
| 299 | # make sure we're not accidentally checking ints |
| 300 | assert type(i) == int |
| 301 | assert type(format_spec) == str |
| 302 | self.assertEqual(i.__format__(format_spec), result) |
| 303 | |
| 304 | test(10**100, 'd', '1' + '0' * 100) |
| 305 | test(10**100+100, 'd', '1' + '0' * 97 + '100') |
| 306 | |
| 307 | test(123456789, 'd', '123456789') |
| 308 | test(123456789, 'd', '123456789') |
| 309 | |
| 310 | # sign and aligning are interdependent |
| 311 | test(1, "-", '1') |
| 312 | test(-1, "-", '-1') |
| 313 | test(1, "-3", ' 1') |
| 314 | test(-1, "-3", ' -1') |
| 315 | test(1, "+3", ' +1') |
| 316 | test(-1, "+3", ' -1') |
| 317 | test(1, " 3", ' 1') |
| 318 | test(-1, " 3", ' -1') |
| 319 | test(1, " ", ' 1') |
| 320 | test(-1, " ", '-1') |
| 321 | |
| 322 | test(1, 'c', '\01') |
| 323 | |
| 324 | # hex |
| 325 | test(3, "x", "3") |
| 326 | test(3, "X", "3") |
| 327 | test(1234, "x", "4d2") |
| 328 | test(-1234, "x", "-4d2") |
| 329 | test(1234, "8x", " 4d2") |
| 330 | test(-1234, "8x", " -4d2") |
| 331 | test(1234, "x", "4d2") |
| 332 | test(-1234, "x", "-4d2") |
| 333 | test(-3, "x", "-3") |
| 334 | test(-3, "X", "-3") |
| 335 | |
| 336 | # octal |
| 337 | test(3, "o", "3") |
| 338 | test(-3, "o", "-3") |
| 339 | test(65, "o", "101") |
| 340 | test(-65, "o", "-101") |
| 341 | test(1234, "o", "2322") |
| 342 | test(-1234, "o", "-2322") |
| 343 | test(1234, "-o", "2322") |
| 344 | test(-1234, "-o", "-2322") |
| 345 | test(1234, " o", " 2322") |
| 346 | test(-1234, " o", "-2322") |
| 347 | test(1234, "+o", "+2322") |
| 348 | test(-1234, "+o", "-2322") |
| 349 | |
| 350 | # binary |
| 351 | test(3, "b", "11") |
| 352 | test(-3, "b", "-11") |
| 353 | test(1234, "b", "10011010010") |
| 354 | test(-1234, "b", "-10011010010") |
| 355 | test(1234, "-b", "10011010010") |
| 356 | test(-1234, "-b", "-10011010010") |
| 357 | test(1234, " b", " 10011010010") |
| 358 | test(-1234, " b", "-10011010010") |
| 359 | test(1234, "+b", "+10011010010") |
| 360 | test(-1234, "+b", "-10011010010") |
| 361 | |
| 362 | # make sure these are errors |
| 363 | |
| 364 | # precision disallowed |
| 365 | self.assertRaises(ValueError, 3 .__format__, "1.3") |
| 366 | # sign not allowed with 'c' |
| 367 | self.assertRaises(ValueError, 3 .__format__, "+c") |
| 368 | # format spec must be string |
| 369 | self.assertRaises(TypeError, 3 .__format__, None) |
| 370 | self.assertRaises(TypeError, 3 .__format__, 0) |
| 371 | |
| 372 | # ensure that only int and float type specifiers work |
| 373 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 374 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 375 | if not format_spec in 'bcdoxXeEfFgGn%': |
| 376 | self.assertRaises(ValueError, 0 .__format__, format_spec) |
| 377 | self.assertRaises(ValueError, 1 .__format__, format_spec) |
| 378 | self.assertRaises(ValueError, (-1) .__format__, format_spec) |
| 379 | |
| 380 | # ensure that float type specifiers work; format converts |
| 381 | # the long to a float |
| 382 | for format_spec in 'eEfFgGn%': |
| 383 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 384 | self.assertEqual(value.__format__(format_spec), |
| 385 | float(value).__format__(format_spec)) |
| 386 | |
| 387 | def test_float__format__(self): |
| 388 | # these should be rewritten to use both format(x, spec) and |
| 389 | # x.__format__(spec) |
| 390 | |
| 391 | def test(f, format_spec, result): |
| 392 | assert type(f) == float |
| 393 | assert type(format_spec) == str |
| 394 | self.assertEqual(f.__format__(format_spec), result) |
| 395 | |
| 396 | test(0.0, 'f', '0.000000') |
| 397 | |
| 398 | # the default is 'g', except for empty format spec |
| 399 | test(0.0, '', '0.0') |
| 400 | test(0.01, '', '0.01') |
| 401 | test(0.01, 'g', '0.01') |
| 402 | |
| 403 | test( 1.0, ' g', ' 1') |
| 404 | test(-1.0, ' g', '-1') |
| 405 | test( 1.0, '+g', '+1') |
| 406 | test(-1.0, '+g', '-1') |
| 407 | test(1.1234e200, 'g', '1.1234e+200') |
| 408 | test(1.1234e200, 'G', '1.1234E+200') |
| 409 | |
| 410 | |
| 411 | test(1.0, 'f', '1.000000') |
| 412 | |
| 413 | test(-1.0, 'f', '-1.000000') |
| 414 | |
| 415 | test( 1.0, ' f', ' 1.000000') |
| 416 | test(-1.0, ' f', '-1.000000') |
| 417 | test( 1.0, '+f', '+1.000000') |
| 418 | test(-1.0, '+f', '-1.000000') |
| 419 | test(1.1234e200, 'f', '1.1234e+200') |
| 420 | test(1.1234e200, 'F', '1.1234e+200') |
| 421 | |
| 422 | # temporarily removed. see issue 1600 |
| 423 | # test( 1.0, 'e', '1.000000e+00') |
| 424 | # test(-1.0, 'e', '-1.000000e+00') |
| 425 | # test( 1.0, 'E', '1.000000E+00') |
| 426 | # test(-1.0, 'E', '-1.000000E+00') |
| 427 | # test(1.1234e20, 'e', '1.123400e+20') |
| 428 | # test(1.1234e20, 'E', '1.123400E+20') |
| 429 | |
| 430 | # % formatting |
| 431 | test(-1.0, '%', '-100.000000%') |
| 432 | |
| 433 | # format spec must be string |
| 434 | self.assertRaises(TypeError, 3.0.__format__, None) |
| 435 | self.assertRaises(TypeError, 3.0.__format__, 0) |
| 436 | |
| 437 | # other format specifiers shouldn't work on floats, |
| 438 | # in particular int specifiers |
| 439 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 440 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 441 | if not format_spec in 'eEfFgGn%': |
| 442 | self.assertRaises(ValueError, format, 0.0, format_spec) |
| 443 | self.assertRaises(ValueError, format, 1.0, format_spec) |
| 444 | self.assertRaises(ValueError, format, -1.0, format_spec) |
| 445 | self.assertRaises(ValueError, format, 1e100, format_spec) |
| 446 | self.assertRaises(ValueError, format, -1e100, format_spec) |
| 447 | self.assertRaises(ValueError, format, 1e-100, format_spec) |
| 448 | self.assertRaises(ValueError, format, -1e-100, format_spec) |
| 449 | |
| 450 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 451 | def test_main(): |
| 452 | run_unittest(TypesTests) |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 453 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 454 | if __name__ == '__main__': |
| 455 | test_main() |