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