Guido van Rossum | 85f1820 | 1992-11-27 22:53:50 +0000 | [diff] [blame] | 1 | # Python test set -- part 6, built-in types |
| 2 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 3 | from 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 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 296 | # alternate (#) formatting |
| 297 | test(0, "#b", '0b0') |
| 298 | test(0, "-#b", '0b0') |
| 299 | test(1, "-#b", '0b1') |
| 300 | test(-1, "-#b", '-0b1') |
| 301 | test(-1, "-#5b", ' -0b1') |
| 302 | test(1, "+#5b", ' +0b1') |
| 303 | test(100, "+#b", '+0b1100100') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 304 | test(100, "#012b", '0b0001100100') |
| 305 | test(-100, "#012b", '-0b001100100') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 306 | |
| 307 | test(0, "#o", '0o0') |
| 308 | test(0, "-#o", '0o0') |
| 309 | test(1, "-#o", '0o1') |
| 310 | test(-1, "-#o", '-0o1') |
| 311 | test(-1, "-#5o", ' -0o1') |
| 312 | test(1, "+#5o", ' +0o1') |
| 313 | test(100, "+#o", '+0o144') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 314 | test(100, "#012o", '0o0000000144') |
| 315 | test(-100, "#012o", '-0o000000144') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 316 | |
| 317 | test(0, "#x", '0x0') |
| 318 | test(0, "-#x", '0x0') |
| 319 | test(1, "-#x", '0x1') |
| 320 | test(-1, "-#x", '-0x1') |
| 321 | test(-1, "-#5x", ' -0x1') |
| 322 | test(1, "+#5x", ' +0x1') |
| 323 | test(100, "+#x", '+0x64') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 324 | test(100, "#012x", '0x0000000064') |
| 325 | test(-100, "#012x", '-0x000000064') |
| 326 | test(123456, "#012x", '0x000001e240') |
| 327 | test(-123456, "#012x", '-0x00001e240') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 328 | |
| 329 | test(0, "#X", '0X0') |
| 330 | test(0, "-#X", '0X0') |
| 331 | test(1, "-#X", '0X1') |
| 332 | test(-1, "-#X", '-0X1') |
| 333 | test(-1, "-#5X", ' -0X1') |
| 334 | test(1, "+#5X", ' +0X1') |
| 335 | test(100, "+#X", '+0X64') |
Eric Smith | d68af8f | 2008-07-16 00:15:35 +0000 | [diff] [blame] | 336 | test(100, "#012X", '0X0000000064') |
| 337 | test(-100, "#012X", '-0X000000064') |
| 338 | test(123456, "#012X", '0X000001E240') |
| 339 | test(-123456, "#012X", '-0X00001E240') |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 340 | |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 341 | test(123, ',', '123') |
| 342 | test(-123, ',', '-123') |
| 343 | test(1234, ',', '1,234') |
| 344 | test(-1234, ',', '-1,234') |
| 345 | test(123456, ',', '123,456') |
| 346 | test(-123456, ',', '-123,456') |
| 347 | test(1234567, ',', '1,234,567') |
| 348 | test(-1234567, ',', '-1,234,567') |
| 349 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 350 | # make sure these are errors |
| 351 | |
| 352 | # precision disallowed |
| 353 | self.assertRaises(ValueError, 3 .__format__, "1.3") |
| 354 | # sign not allowed with 'c' |
| 355 | self.assertRaises(ValueError, 3 .__format__, "+c") |
| 356 | # format spec must be string |
| 357 | self.assertRaises(TypeError, 3 .__format__, None) |
| 358 | self.assertRaises(TypeError, 3 .__format__, 0) |
Eric Smith | a3b1ac8 | 2009-04-03 14:45:06 +0000 | [diff] [blame^] | 359 | # can't have ',' with 'n' |
| 360 | self.assertRaises(ValueError, 3 .__format__, ",n") |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 361 | |
| 362 | # ensure that only int and float type specifiers work |
| 363 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 364 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 365 | if not format_spec in 'bcdoxXeEfFgGn%': |
| 366 | self.assertRaises(ValueError, 0 .__format__, format_spec) |
| 367 | self.assertRaises(ValueError, 1 .__format__, format_spec) |
| 368 | self.assertRaises(ValueError, (-1) .__format__, format_spec) |
| 369 | |
| 370 | # ensure that float type specifiers work; format converts |
| 371 | # the int to a float |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 372 | for format_spec in 'eEfFgG%': |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 373 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 374 | self.assertEqual(value.__format__(format_spec), |
| 375 | float(value).__format__(format_spec)) |
| 376 | |
| 377 | def test_long__format__(self): |
| 378 | def test(i, format_spec, result): |
| 379 | # make sure we're not accidentally checking ints |
| 380 | assert type(i) == int |
| 381 | assert type(format_spec) == str |
| 382 | self.assertEqual(i.__format__(format_spec), result) |
| 383 | |
| 384 | test(10**100, 'd', '1' + '0' * 100) |
| 385 | test(10**100+100, 'd', '1' + '0' * 97 + '100') |
| 386 | |
| 387 | test(123456789, 'd', '123456789') |
| 388 | test(123456789, 'd', '123456789') |
| 389 | |
| 390 | # sign and aligning are interdependent |
| 391 | test(1, "-", '1') |
| 392 | test(-1, "-", '-1') |
| 393 | test(1, "-3", ' 1') |
| 394 | test(-1, "-3", ' -1') |
| 395 | test(1, "+3", ' +1') |
| 396 | test(-1, "+3", ' -1') |
| 397 | test(1, " 3", ' 1') |
| 398 | test(-1, " 3", ' -1') |
| 399 | test(1, " ", ' 1') |
| 400 | test(-1, " ", '-1') |
| 401 | |
| 402 | test(1, 'c', '\01') |
| 403 | |
| 404 | # hex |
| 405 | test(3, "x", "3") |
| 406 | test(3, "X", "3") |
| 407 | test(1234, "x", "4d2") |
| 408 | test(-1234, "x", "-4d2") |
| 409 | test(1234, "8x", " 4d2") |
| 410 | test(-1234, "8x", " -4d2") |
| 411 | test(1234, "x", "4d2") |
| 412 | test(-1234, "x", "-4d2") |
| 413 | test(-3, "x", "-3") |
| 414 | test(-3, "X", "-3") |
| 415 | |
| 416 | # octal |
| 417 | test(3, "o", "3") |
| 418 | test(-3, "o", "-3") |
| 419 | test(65, "o", "101") |
| 420 | test(-65, "o", "-101") |
| 421 | test(1234, "o", "2322") |
| 422 | test(-1234, "o", "-2322") |
| 423 | test(1234, "-o", "2322") |
| 424 | test(-1234, "-o", "-2322") |
| 425 | test(1234, " o", " 2322") |
| 426 | test(-1234, " o", "-2322") |
| 427 | test(1234, "+o", "+2322") |
| 428 | test(-1234, "+o", "-2322") |
| 429 | |
| 430 | # binary |
| 431 | test(3, "b", "11") |
| 432 | test(-3, "b", "-11") |
| 433 | test(1234, "b", "10011010010") |
| 434 | test(-1234, "b", "-10011010010") |
| 435 | test(1234, "-b", "10011010010") |
| 436 | test(-1234, "-b", "-10011010010") |
| 437 | test(1234, " b", " 10011010010") |
| 438 | test(-1234, " b", "-10011010010") |
| 439 | test(1234, "+b", "+10011010010") |
| 440 | test(-1234, "+b", "-10011010010") |
| 441 | |
| 442 | # make sure these are errors |
| 443 | |
| 444 | # precision disallowed |
| 445 | self.assertRaises(ValueError, 3 .__format__, "1.3") |
| 446 | # sign not allowed with 'c' |
| 447 | self.assertRaises(ValueError, 3 .__format__, "+c") |
| 448 | # format spec must be string |
| 449 | self.assertRaises(TypeError, 3 .__format__, None) |
| 450 | self.assertRaises(TypeError, 3 .__format__, 0) |
| 451 | |
| 452 | # ensure that only int and float type specifiers work |
| 453 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 454 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 455 | if not format_spec in 'bcdoxXeEfFgGn%': |
| 456 | self.assertRaises(ValueError, 0 .__format__, format_spec) |
| 457 | self.assertRaises(ValueError, 1 .__format__, format_spec) |
| 458 | self.assertRaises(ValueError, (-1) .__format__, format_spec) |
| 459 | |
| 460 | # ensure that float type specifiers work; format converts |
| 461 | # the long to a float |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 462 | for format_spec in 'eEfFgG%': |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 463 | for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: |
| 464 | self.assertEqual(value.__format__(format_spec), |
| 465 | float(value).__format__(format_spec)) |
| 466 | |
Eric Smith | b2c7af8 | 2008-04-30 02:12:09 +0000 | [diff] [blame] | 467 | @run_with_locale('LC_NUMERIC', 'en_US.UTF8') |
| 468 | def test_float__format__locale(self): |
| 469 | # test locale support for __format__ code 'n' |
| 470 | |
| 471 | for i in range(-10, 10): |
| 472 | x = 1234567890.0 * (10.0 ** i) |
| 473 | self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n')) |
| 474 | self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n')) |
| 475 | |
Eric Smith | 5807c41 | 2008-05-11 21:00:57 +0000 | [diff] [blame] | 476 | @run_with_locale('LC_NUMERIC', 'en_US.UTF8') |
| 477 | def test_int__format__locale(self): |
| 478 | # test locale support for __format__ code 'n' for integers |
| 479 | |
| 480 | x = 123456789012345678901234567890 |
| 481 | for i in range(0, 30): |
| 482 | self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n')) |
| 483 | |
| 484 | # move to the next integer to test |
| 485 | x = x // 10 |
| 486 | |
Eric Smith | b151a45 | 2008-06-24 11:21:04 +0000 | [diff] [blame] | 487 | rfmt = ">20n" |
| 488 | lfmt = "<20n" |
| 489 | cfmt = "^20n" |
| 490 | for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900): |
| 491 | self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt))) |
| 492 | self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt))) |
| 493 | self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt))) |
| 494 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 495 | def test_float__format__(self): |
| 496 | # these should be rewritten to use both format(x, spec) and |
| 497 | # x.__format__(spec) |
| 498 | |
| 499 | def test(f, format_spec, result): |
| 500 | assert type(f) == float |
| 501 | assert type(format_spec) == str |
| 502 | self.assertEqual(f.__format__(format_spec), result) |
| 503 | |
| 504 | test(0.0, 'f', '0.000000') |
| 505 | |
| 506 | # the default is 'g', except for empty format spec |
| 507 | test(0.0, '', '0.0') |
| 508 | test(0.01, '', '0.01') |
| 509 | test(0.01, 'g', '0.01') |
| 510 | |
Eric Smith | 2ad79e8 | 2008-07-19 00:33:23 +0000 | [diff] [blame] | 511 | # test for issue 3411 |
| 512 | test(1.23, '1', '1.23') |
| 513 | test(-1.23, '1', '-1.23') |
| 514 | test(1.23, '1g', '1.23') |
| 515 | test(-1.23, '1g', '-1.23') |
| 516 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 517 | test( 1.0, ' g', ' 1') |
| 518 | test(-1.0, ' g', '-1') |
| 519 | test( 1.0, '+g', '+1') |
| 520 | test(-1.0, '+g', '-1') |
| 521 | test(1.1234e200, 'g', '1.1234e+200') |
| 522 | test(1.1234e200, 'G', '1.1234E+200') |
| 523 | |
| 524 | |
| 525 | test(1.0, 'f', '1.000000') |
| 526 | |
| 527 | test(-1.0, 'f', '-1.000000') |
| 528 | |
| 529 | test( 1.0, ' f', ' 1.000000') |
| 530 | test(-1.0, ' f', '-1.000000') |
| 531 | test( 1.0, '+f', '+1.000000') |
| 532 | test(-1.0, '+f', '-1.000000') |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 533 | test(1.1234e90, 'f', '1.1234e+90') |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 534 | test(1.1234e90, 'F', '1.1234e+90') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 535 | test(1.1234e200, 'f', '1.1234e+200') |
Eric Smith | 22b85b3 | 2008-07-17 19:18:29 +0000 | [diff] [blame] | 536 | test(1.1234e200, 'F', '1.1234e+200') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 537 | |
Christian Heimes | c3f30c4 | 2008-02-22 16:37:40 +0000 | [diff] [blame] | 538 | test( 1.0, 'e', '1.000000e+00') |
| 539 | test(-1.0, 'e', '-1.000000e+00') |
| 540 | test( 1.0, 'E', '1.000000E+00') |
| 541 | test(-1.0, 'E', '-1.000000E+00') |
| 542 | test(1.1234e20, 'e', '1.123400e+20') |
| 543 | test(1.1234e20, 'E', '1.123400E+20') |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 544 | |
Christian Heimes | b186d00 | 2008-03-18 15:15:01 +0000 | [diff] [blame] | 545 | # No format code means use g, but must have a decimal |
| 546 | # and a number after the decimal. This is tricky, because |
| 547 | # a totaly empty format specifier means something else. |
| 548 | # So, just use a sign flag |
| 549 | test(1e200, '+g', '+1e+200') |
| 550 | test(1e200, '+', '+1.0e+200') |
| 551 | test(1.1e200, '+g', '+1.1e+200') |
| 552 | test(1.1e200, '+', '+1.1e+200') |
| 553 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 554 | # % formatting |
| 555 | test(-1.0, '%', '-100.000000%') |
| 556 | |
| 557 | # format spec must be string |
| 558 | self.assertRaises(TypeError, 3.0.__format__, None) |
| 559 | self.assertRaises(TypeError, 3.0.__format__, 0) |
| 560 | |
| 561 | # other format specifiers shouldn't work on floats, |
| 562 | # in particular int specifiers |
| 563 | for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + |
| 564 | [chr(x) for x in range(ord('A'), ord('Z')+1)]): |
| 565 | if not format_spec in 'eEfFgGn%': |
| 566 | self.assertRaises(ValueError, format, 0.0, format_spec) |
| 567 | self.assertRaises(ValueError, format, 1.0, format_spec) |
| 568 | self.assertRaises(ValueError, format, -1.0, format_spec) |
| 569 | self.assertRaises(ValueError, format, 1e100, format_spec) |
| 570 | self.assertRaises(ValueError, format, -1e100, format_spec) |
| 571 | self.assertRaises(ValueError, format, 1e-100, format_spec) |
| 572 | self.assertRaises(ValueError, format, -1e-100, format_spec) |
| 573 | |
Eric Smith | b1ebcc6 | 2008-07-15 13:02:41 +0000 | [diff] [blame] | 574 | # Alternate formatting is not supported |
| 575 | self.assertRaises(ValueError, format, 0.0, '#') |
| 576 | self.assertRaises(ValueError, format, 0.0, '#20f') |
| 577 | |
Christian Heimes | 7131fd9 | 2008-02-19 14:21:46 +0000 | [diff] [blame] | 578 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 579 | def test_main(): |
| 580 | run_unittest(TypesTests) |
Neil Schemenauer | eff7244 | 2002-03-24 01:24:54 +0000 | [diff] [blame] | 581 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 582 | if __name__ == '__main__': |
| 583 | test_main() |