1 # Python test set -- part 6, built-in types 2 3 from test.test_support import run_unittest, have_unicode, run_with_locale, \ 4 check_py3k_warnings 5 import unittest 6 import sys 7 import locale 8 9 class TypesTests(unittest.TestCase): 10 11 def test_truth_values(self): 12 if None: self.fail('None is true instead of false') 13 if 0: self.fail('0 is true instead of false') 14 if 0L: self.fail('0L is true instead of false') 15 if 0.0: self.fail('0.0 is true instead of false') 16 if '': self.fail('\'\' is true instead of false') 17 if not 1: self.fail('1 is false instead of true') 18 if not 1L: self.fail('1L is false instead of true') 19 if not 1.0: self.fail('1.0 is false instead of true') 20 if not 'x': self.fail('\'x\' is false instead of true') 21 if not {'x': 1}: self.fail('{\'x\': 1} is false instead of true') 22 def f(): pass 23 class C: pass 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') 29 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') 35 36 def test_comparisons(self): 37 if 0 < 1 <= 1 == 1 >= 1 > 0 != 1: pass 38 else: self.fail('int comparisons failed') 39 if 0L < 1L <= 1L == 1L >= 1L > 0L != 1L: pass 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') 47 48 def test_float_constructor(self): 49 self.assertRaises(ValueError, float, '') 50 self.assertRaises(ValueError, float, '5\0') 51 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") 56 57 try: 5.0 // 0.0 58 except ZeroDivisionError: pass 59 else: self.fail("5.0 // 0.0 didn't raise ZeroDivisionError") 60 61 try: 5.0 % 0.0 62 except ZeroDivisionError: pass 63 else: self.fail("5.0 % 0.0 didn't raise ZeroDivisionError") 64 65 try: 5 / 0L 66 except ZeroDivisionError: pass 67 else: self.fail("5 / 0L didn't raise ZeroDivisionError") 68 69 try: 5 // 0L 70 except ZeroDivisionError: pass 71 else: self.fail("5 // 0L didn't raise ZeroDivisionError") 72 73 try: 5 % 0L 74 except ZeroDivisionError: pass 75 else: self.fail("5 % 0L didn't raise ZeroDivisionError") 76 77 def test_numeric_types(self): 78 if 0 != 0L or 0 != 0.0 or 0L != 0.0: self.fail('mixed comparisons') 79 if 1 != 1L or 1 != 1.0 or 1L != 1.0: self.fail('mixed comparisons') 80 if -1 != -1L or -1 != -1.0 or -1L != -1.0: 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') 84 if long() != 0L: self.fail('long() does not return 0L') 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') 88 if long(1.9) == 1L == long(1.1) and long(-1.1) == -1L == long(-1.9): pass 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') 92 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 116 self.assertEqual('%g' % 1.0, '1') 117 self.assertEqual('%#g' % 1.0, '1.00000') 118 119 def test_normal_integers(self): 120 # Ensure the first 256 integers are shared 121 a = 256 122 b = 128*2 123 if a is not b: self.fail('256 is not shared') 124 if 12 + 24 != 36: self.fail('int op') 125 if 12 + (-24) != -12: self.fail('int op') 126 if (-12) + 24 != 12: self.fail('int op') 127 if (-12) + (-24) != -36: self.fail('int op') 128 if not 12 < 24: self.fail('int op') 129 if not -24 < -12: self.fail('int op') 130 # Test for a particular bug in integer multiply 131 xsize, ysize, zsize = 238, 356, 4 132 if not (xsize*ysize*zsize == zsize*xsize*ysize == 338912): 133 self.fail('int mul commutativity') 134 # And another. 135 m = -sys.maxint - 1 136 for divisor in 1, 2, 4, 8, 16, 32: 137 j = m // divisor 138 prod = divisor * j 139 if prod != m: 140 self.fail("%r * %r == %r != %r" % (divisor, j, prod, m)) 141 if type(prod) is not int: 142 self.fail("expected type(prod) to be int, not %r" % 143 type(prod)) 144 # Check for expected * overflow to long. 145 for divisor in 1, 2, 4, 8, 16, 32: 146 j = m // divisor - 1 147 prod = divisor * j 148 if type(prod) is not long: 149 self.fail("expected type(%r) to be long, not %r" % 150 (prod, type(prod))) 151 # Check for expected * overflow to long. 152 m = sys.maxint 153 for divisor in 1, 2, 4, 8, 16, 32: 154 j = m // divisor + 1 155 prod = divisor * j 156 if type(prod) is not long: 157 self.fail("expected type(%r) to be long, not %r" % 158 (prod, type(prod))) 159 160 def test_long_integers(self): 161 if 12L + 24L != 36L: self.fail('long op') 162 if 12L + (-24L) != -12L: self.fail('long op') 163 if (-12L) + 24L != 12L: self.fail('long op') 164 if (-12L) + (-24L) != -36L: self.fail('long op') 165 if not 12L < 24L: self.fail('long op') 166 if not -24L < -12L: self.fail('long op') 167 x = sys.maxint 168 if int(long(x)) != x: self.fail('long op') 169 try: y = int(long(x)+1L) 170 except OverflowError: self.fail('long op') 171 if not isinstance(y, long): self.fail('long op') 172 x = -x 173 if int(long(x)) != x: self.fail('long op') 174 x = x-1 175 if int(long(x)) != x: self.fail('long op') 176 try: y = int(long(x)-1L) 177 except OverflowError: self.fail('long op') 178 if not isinstance(y, long): self.fail('long op') 179 180 try: 5 << -5 181 except ValueError: pass 182 else: self.fail('int negative shift <<') 183 184 try: 5L << -5L 185 except ValueError: pass 186 else: self.fail('long negative shift <<') 187 188 try: 5 >> -5 189 except ValueError: pass 190 else: self.fail('int negative shift >>') 191 192 try: 5L >> -5L 193 except ValueError: pass 194 else: self.fail('long negative shift >>') 195 196 def test_floats(self): 197 if 12.0 + 24.0 != 36.0: self.fail('float op') 198 if 12.0 + (-24.0) != -12.0: self.fail('float op') 199 if (-12.0) + 24.0 != 12.0: self.fail('float op') 200 if (-12.0) + (-24.0) != -36.0: self.fail('float op') 201 if not 12.0 < 24.0: self.fail('float op') 202 if not -24.0 < -12.0: self.fail('float op') 203 204 def test_strings(self): 205 if len('') != 0: self.fail('len(\'\')') 206 if len('a') != 1: self.fail('len(\'a\')') 207 if len('abcdef') != 6: self.fail('len(\'abcdef\')') 208 if 'xyz' + 'abcde' != 'xyzabcde': self.fail('string concatenation') 209 if 'xyz'*3 != 'xyzxyzxyz': self.fail('string repetition *3') 210 if 0*'abcde' != '': self.fail('string repetition 0*') 211 if min('abc') != 'a' or max('abc') != 'c': self.fail('min/max string') 212 if 'a' in 'abc' and 'b' in 'abc' and 'c' in 'abc' and 'd' not in 'abc': pass 213 else: self.fail('in/not in string') 214 x = 'x'*103 215 if '%s!'%x != x+'!': self.fail('nasty string formatting bug') 216 217 #extended slices for strings 218 a = '0123456789' 219 self.assertEqual(a[::], a) 220 self.assertEqual(a[::2], '02468') 221 self.assertEqual(a[1::2], '13579') 222 self.assertEqual(a[::-1],'9876543210') 223 self.assertEqual(a[::-2], '97531') 224 self.assertEqual(a[3::-2], '31') 225 self.assertEqual(a[-100:100:], a) 226 self.assertEqual(a[100:-100:-1], a[::-1]) 227 self.assertEqual(a[-100L:100L:2L], '02468') 228 229 if have_unicode: 230 a = unicode('0123456789', 'ascii') 231 self.assertEqual(a[::], a) 232 self.assertEqual(a[::2], unicode('02468', 'ascii')) 233 self.assertEqual(a[1::2], unicode('13579', 'ascii')) 234 self.assertEqual(a[::-1], unicode('9876543210', 'ascii')) 235 self.assertEqual(a[::-2], unicode('97531', 'ascii')) 236 self.assertEqual(a[3::-2], unicode('31', 'ascii')) 237 self.assertEqual(a[-100:100:], a) 238 self.assertEqual(a[100:-100:-1], a[::-1]) 239 self.assertEqual(a[-100L:100L:2L], unicode('02468', 'ascii')) 240 241 242 def test_type_function(self): 243 self.assertRaises(TypeError, type, 1, 2) 244 self.assertRaises(TypeError, type, 1, 2, 3, 4) 245 246 def test_buffers(self): 247 self.assertRaises(ValueError, buffer, 'asdf', -1) 248 cmp(buffer("abc"), buffer("def")) # used to raise a warning: tp_compare didn't return -1, 0, or 1 249 250 self.assertRaises(TypeError, buffer, None) 251 252 a = buffer('asdf') 253 hash(a) 254 b = a * 5 255 if a == b: 256 self.fail('buffers should not be equal') 257 if str(b) != ('asdf' * 5): 258 self.fail('repeated buffer has wrong content') 259 if str(a * 0) != '': 260 self.fail('repeated buffer zero times has wrong content') 261 if str(a + buffer('def')) != 'asdfdef': 262 self.fail('concatenation of buffers yields wrong content') 263 if str(buffer(a)) != 'asdf': 264 self.fail('composing buffers failed') 265 if str(buffer(a, 2)) != 'df': 266 self.fail('specifying buffer offset failed') 267 if str(buffer(a, 0, 2)) != 'as': 268 self.fail('specifying buffer size failed') 269 if str(buffer(a, 1, 2)) != 'sd': 270 self.fail('specifying buffer offset and size failed') 271 self.assertRaises(ValueError, buffer, buffer('asdf', 1), -1) 272 if str(buffer(buffer('asdf', 0, 2), 0)) != 'as': 273 self.fail('composing length-specified buffer failed') 274 if str(buffer(buffer('asdf', 0, 2), 0, 5000)) != 'as': 275 self.fail('composing length-specified buffer failed') 276 if str(buffer(buffer('asdf', 0, 2), 0, -1)) != 'as': 277 self.fail('composing length-specified buffer failed') 278 if str(buffer(buffer('asdf', 0, 2), 1, 2)) != 's': 279 self.fail('composing length-specified buffer failed') 280 281 try: a[1] = 'g' 282 except TypeError: pass 283 else: self.fail("buffer assignment should raise TypeError") 284 285 try: a[0:1] = 'g' 286 except TypeError: pass 287 else: self.fail("buffer slice assignment should raise TypeError") 288 289 # array.array() returns an object that does not implement a char buffer, 290 # something which int() uses for conversion. 291 import array 292 try: int(buffer(array.array('c'))) 293 except TypeError: pass 294 else: self.fail("char buffer (at C level) not working") 295 296 def test_int__format__(self): 297 def test(i, format_spec, result): 298 # just make sure I'm not accidentally checking longs 299 assert type(i) == int 300 assert type(format_spec) == str 301 self.assertEqual(i.__format__(format_spec), result) 302 self.assertEqual(i.__format__(unicode(format_spec)), result) 303 304 test(123456789, 'd', '123456789') 305 test(123456789, 'd', '123456789') 306 307 test(1, 'c', '\01') 308 309 # sign and aligning are interdependent 310 test(1, "-", '1') 311 test(-1, "-", '-1') 312 test(1, "-3", ' 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, " ", ' 1') 319 test(-1, " ", '-1') 320 321 # hex 322 test(3, "x", "3") 323 test(3, "X", "3") 324 test(1234, "x", "4d2") 325 test(-1234, "x", "-4d2") 326 test(1234, "8x", " 4d2") 327 test(-1234, "8x", " -4d2") 328 test(1234, "x", "4d2") 329 test(-1234, "x", "-4d2") 330 test(-3, "x", "-3") 331 test(-3, "X", "-3") 332 test(int('be', 16), "x", "be") 333 test(int('be', 16), "X", "BE") 334 test(-int('be', 16), "x", "-be") 335 test(-int('be', 16), "X", "-BE") 336 337 # octal 338 test(3, "o", "3") 339 test(-3, "o", "-3") 340 test(65, "o", "101") 341 test(-65, "o", "-101") 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 test(-1234, "+o", "-2322") 350 351 # binary 352 test(3, "b", "11") 353 test(-3, "b", "-11") 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 test(-1234, "+b", "-10011010010") 362 363 # alternate (#) formatting 364 test(0, "#b", '0b0') 365 test(0, "-#b", '0b0') 366 test(1, "-#b", '0b1') 367 test(-1, "-#b", '-0b1') 368 test(-1, "-#5b", ' -0b1') 369 test(1, "+#5b", ' +0b1') 370 test(100, "+#b", '+0b1100100') 371 test(100, "#012b", '0b0001100100') 372 test(-100, "#012b", '-0b001100100') 373 374 test(0, "#o", '0o0') 375 test(0, "-#o", '0o0') 376 test(1, "-#o", '0o1') 377 test(-1, "-#o", '-0o1') 378 test(-1, "-#5o", ' -0o1') 379 test(1, "+#5o", ' +0o1') 380 test(100, "+#o", '+0o144') 381 test(100, "#012o", '0o0000000144') 382 test(-100, "#012o", '-0o000000144') 383 384 test(0, "#x", '0x0') 385 test(0, "-#x", '0x0') 386 test(1, "-#x", '0x1') 387 test(-1, "-#x", '-0x1') 388 test(-1, "-#5x", ' -0x1') 389 test(1, "+#5x", ' +0x1') 390 test(100, "+#x", '+0x64') 391 test(100, "#012x", '0x0000000064') 392 test(-100, "#012x", '-0x000000064') 393 test(123456, "#012x", '0x000001e240') 394 test(-123456, "#012x", '-0x00001e240') 395 396 test(0, "#X", '0X0') 397 test(0, "-#X", '0X0') 398 test(1, "-#X", '0X1') 399 test(-1, "-#X", '-0X1') 400 test(-1, "-#5X", ' -0X1') 401 test(1, "+#5X", ' +0X1') 402 test(100, "+#X", '+0X64') 403 test(100, "#012X", '0X0000000064') 404 test(-100, "#012X", '-0X000000064') 405 test(123456, "#012X", '0X000001E240') 406 test(-123456, "#012X", '-0X00001E240') 407 408 # issue 5782, commas with no specifier type 409 test(1234, '010,', '00,001,234') 410 411 # make sure these are errors 412 413 # precision disallowed 414 self.assertRaises(ValueError, 3 .__format__, "1.3") 415 # sign not allowed with 'c' 416 self.assertRaises(ValueError, 3 .__format__, "+c") 417 # format spec must be string 418 self.assertRaises(TypeError, 3 .__format__, None) 419 self.assertRaises(TypeError, 3 .__format__, 0) 420 421 # can't have ',' with 'c' 422 self.assertRaises(ValueError, 3 .__format__, ",c") 423 424 # ensure that only int and float type specifiers work 425 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + 426 [chr(x) for x in range(ord('A'), ord('Z')+1)]): 427 if not format_spec in 'bcdoxXeEfFgGn%': 428 self.assertRaises(ValueError, 0 .__format__, format_spec) 429 self.assertRaises(ValueError, 1 .__format__, format_spec) 430 self.assertRaises(ValueError, (-1) .__format__, format_spec) 431 432 # ensure that float type specifiers work; format converts 433 # the int to a float 434 for format_spec in 'eEfFgG%': 435 for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]: 436 self.assertEqual(value.__format__(format_spec), 437 float(value).__format__(format_spec)) 438 439 # Issue 6902 440 test(123456, "0<20", '12345600000000000000') 441 test(123456, "1<20", '12345611111111111111') 442 test(123456, "*<20", '123456**************') 443 test(123456, "0>20", '00000000000000123456') 444 test(123456, "1>20", '11111111111111123456') 445 test(123456, "*>20", '**************123456') 446 test(123456, "0=20", '00000000000000123456') 447 test(123456, "1=20", '11111111111111123456') 448 test(123456, "*=20", '**************123456') 449 450 def test_long__format__(self): 451 def test(i, format_spec, result): 452 # make sure we're not accidentally checking ints 453 assert type(i) == long 454 assert type(format_spec) == str 455 self.assertEqual(i.__format__(format_spec), result) 456 self.assertEqual(i.__format__(unicode(format_spec)), result) 457 458 test(10**100, 'd', '1' + '0' * 100) 459 test(10**100+100, 'd', '1' + '0' * 97 + '100') 460 461 test(123456789L, 'd', '123456789') 462 test(123456789L, 'd', '123456789') 463 464 # sign and aligning are interdependent 465 test(1L, "-", '1') 466 test(-1L, "-", '-1') 467 test(1L, "-3", ' 1') 468 test(-1L, "-3", ' -1') 469 test(1L, "+3", ' +1') 470 test(-1L, "+3", ' -1') 471 test(1L, " 3", ' 1') 472 test(-1L, " 3", ' -1') 473 test(1L, " ", ' 1') 474 test(-1L, " ", '-1') 475 476 test(1L, 'c', '\01') 477 478 # hex 479 test(3L, "x", "3") 480 test(3L, "X", "3") 481 test(1234L, "x", "4d2") 482 test(-1234L, "x", "-4d2") 483 test(1234L, "8x", " 4d2") 484 test(-1234L, "8x", " -4d2") 485 test(1234L, "x", "4d2") 486 test(-1234L, "x", "-4d2") 487 test(-3L, "x", "-3") 488 test(-3L, "X", "-3") 489 test(long('be', 16), "x", "be") 490 test(long('be', 16), "X", "BE") 491 test(-long('be', 16), "x", "-be") 492 test(-long('be', 16), "X", "-BE") 493 494 # octal 495 test(3L, "o", "3") 496 test(-3L, "o", "-3") 497 test(65L, "o", "101") 498 test(-65L, "o", "-101") 499 test(1234L, "o", "2322") 500 test(-1234L, "o", "-2322") 501 test(1234L, "-o", "2322") 502 test(-1234L, "-o", "-2322") 503 test(1234L, " o", " 2322") 504 test(-1234L, " o", "-2322") 505 test(1234L, "+o", "+2322") 506 test(-1234L, "+o", "-2322") 507 508 # binary 509 test(3L, "b", "11") 510 test(-3L, "b", "-11") 511 test(1234L, "b", "10011010010") 512 test(-1234L, "b", "-10011010010") 513 test(1234L, "-b", "10011010010") 514 test(-1234L, "-b", "-10011010010") 515 test(1234L, " b", " 10011010010") 516 test(-1234L, " b", "-10011010010") 517 test(1234L, "+b", "+10011010010") 518 test(-1234L, "+b", "-10011010010") 519 520 # make sure these are errors 521 522 # precision disallowed 523 self.assertRaises(ValueError, 3L .__format__, "1.3") 524 # sign not allowed with 'c' 525 self.assertRaises(ValueError, 3L .__format__, "+c") 526 # format spec must be string 527 self.assertRaises(TypeError, 3L .__format__, None) 528 self.assertRaises(TypeError, 3L .__format__, 0) 529 # alternate specifier in wrong place 530 self.assertRaises(ValueError, 1L .__format__, "#+5x") 531 self.assertRaises(ValueError, 1L .__format__, "+5#x") 532 533 # ensure that only int and float type specifiers work 534 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + 535 [chr(x) for x in range(ord('A'), ord('Z')+1)]): 536 if not format_spec in 'bcdoxXeEfFgGn%': 537 self.assertRaises(ValueError, 0L .__format__, format_spec) 538 self.assertRaises(ValueError, 1L .__format__, format_spec) 539 self.assertRaises(ValueError, (-1L) .__format__, format_spec) 540 541 # ensure that float type specifiers work; format converts 542 # the long to a float 543 for format_spec in 'eEfFgG%': 544 for value in [0L, 1L, -1L, 100L, -100L, 1234567890L, -1234567890L]: 545 self.assertEqual(value.__format__(format_spec), 546 float(value).__format__(format_spec)) 547 # Issue 6902 548 test(123456L, "0<20", '12345600000000000000') 549 test(123456L, "1<20", '12345611111111111111') 550 test(123456L, "*<20", '123456**************') 551 test(123456L, "0>20", '00000000000000123456') 552 test(123456L, "1>20", '11111111111111123456') 553 test(123456L, "*>20", '**************123456') 554 test(123456L, "0=20", '00000000000000123456') 555 test(123456L, "1=20", '11111111111111123456') 556 test(123456L, "*=20", '**************123456') 557 558 @run_with_locale('LC_NUMERIC', 'en_US.UTF8') 559 def test_float__format__locale(self): 560 # test locale support for __format__ code 'n' 561 562 for i in range(-10, 10): 563 x = 1234567890.0 * (10.0 ** i) 564 self.assertEqual(locale.format('%g', x, grouping=True), format(x, 'n')) 565 self.assertEqual(locale.format('%.10g', x, grouping=True), format(x, '.10n')) 566 567 @run_with_locale('LC_NUMERIC', 'en_US.UTF8') 568 def test_int__format__locale(self): 569 # test locale support for __format__ code 'n' for integers 570 571 x = 123456789012345678901234567890 572 for i in range(0, 30): 573 self.assertEqual(locale.format('%d', x, grouping=True), format(x, 'n')) 574 575 # move to the next integer to test 576 x = x // 10 577 578 rfmt = ">20n" 579 lfmt = "<20n" 580 cfmt = "^20n" 581 for x in (1234, 12345, 123456, 1234567, 12345678, 123456789, 1234567890, 12345678900): 582 self.assertEqual(len(format(0, rfmt)), len(format(x, rfmt))) 583 self.assertEqual(len(format(0, lfmt)), len(format(x, lfmt))) 584 self.assertEqual(len(format(0, cfmt)), len(format(x, cfmt))) 585 586 def test_float__format__(self): 587 # these should be rewritten to use both format(x, spec) and 588 # x.__format__(spec) 589 590 def test(f, format_spec, result): 591 assert type(f) == float 592 assert type(format_spec) == str 593 self.assertEqual(f.__format__(format_spec), result) 594 self.assertEqual(f.__format__(unicode(format_spec)), result) 595 596 test(0.0, 'f', '0.000000') 597 598 # the default is 'g', except for empty format spec 599 test(0.0, '', '0.0') 600 test(0.01, '', '0.01') 601 test(0.01, 'g', '0.01') 602 603 # test for issue 3411 604 test(1.23, '1', '1.23') 605 test(-1.23, '1', '-1.23') 606 test(1.23, '1g', '1.23') 607 test(-1.23, '1g', '-1.23') 608 609 test( 1.0, ' g', ' 1') 610 test(-1.0, ' g', '-1') 611 test( 1.0, '+g', '+1') 612 test(-1.0, '+g', '-1') 613 test(1.1234e200, 'g', '1.1234e+200') 614 test(1.1234e200, 'G', '1.1234E+200') 615 616 617 test(1.0, 'f', '1.000000') 618 619 test(-1.0, 'f', '-1.000000') 620 621 test( 1.0, ' f', ' 1.000000') 622 test(-1.0, ' f', '-1.000000') 623 test( 1.0, '+f', '+1.000000') 624 test(-1.0, '+f', '-1.000000') 625 626 # Python versions <= 2.6 switched from 'f' to 'g' formatting for 627 # values larger than 1e50. No longer. 628 f = 1.1234e90 629 for fmt in 'f', 'F': 630 # don't do a direct equality check, since on some 631 # platforms only the first few digits of dtoa 632 # will be reliable 633 result = f.__format__(fmt) 634 self.assertEqual(len(result), 98) 635 self.assertEqual(result[-7], '.') 636 self.assertIn(result[:12], ('112340000000', '112339999999')) 637 f = 1.1234e200 638 for fmt in 'f', 'F': 639 result = f.__format__(fmt) 640 self.assertEqual(len(result), 208) 641 self.assertEqual(result[-7], '.') 642 self.assertIn(result[:12], ('112340000000', '112339999999')) 643 644 645 test( 1.0, 'e', '1.000000e+00') 646 test(-1.0, 'e', '-1.000000e+00') 647 test( 1.0, 'E', '1.000000E+00') 648 test(-1.0, 'E', '-1.000000E+00') 649 test(1.1234e20, 'e', '1.123400e+20') 650 test(1.1234e20, 'E', '1.123400E+20') 651 652 # No format code means use g, but must have a decimal 653 # and a number after the decimal. This is tricky, because 654 # a totaly empty format specifier means something else. 655 # So, just use a sign flag 656 test(1e200, '+g', '+1e+200') 657 test(1e200, '+', '+1e+200') 658 test(1.1e200, '+g', '+1.1e+200') 659 test(1.1e200, '+', '+1.1e+200') 660 661 test(1.1e200, '+g', '+1.1e+200') 662 test(1.1e200, '+', '+1.1e+200') 663 664 # 0 padding 665 test(1234., '010f', '1234.000000') 666 test(1234., '011f', '1234.000000') 667 test(1234., '012f', '01234.000000') 668 test(-1234., '011f', '-1234.000000') 669 test(-1234., '012f', '-1234.000000') 670 test(-1234., '013f', '-01234.000000') 671 test(-1234.12341234, '013f', '-01234.123412') 672 test(-123456.12341234, '011.2f', '-0123456.12') 673 674 # issue 5782, commas with no specifier type 675 test(1.2, '010,.2', '0,000,001.2') 676 677 # 0 padding with commas 678 test(1234., '011,f', '1,234.000000') 679 test(1234., '012,f', '1,234.000000') 680 test(1234., '013,f', '01,234.000000') 681 test(-1234., '012,f', '-1,234.000000') 682 test(-1234., '013,f', '-1,234.000000') 683 test(-1234., '014,f', '-01,234.000000') 684 test(-12345., '015,f', '-012,345.000000') 685 test(-123456., '016,f', '-0,123,456.000000') 686 test(-123456., '017,f', '-0,123,456.000000') 687 test(-123456.12341234, '017,f', '-0,123,456.123412') 688 test(-123456.12341234, '013,.2f', '-0,123,456.12') 689 690 # % formatting 691 test(-1.0, '%', '-100.000000%') 692 693 # format spec must be string 694 self.assertRaises(TypeError, 3.0.__format__, None) 695 self.assertRaises(TypeError, 3.0.__format__, 0) 696 697 # other format specifiers shouldn't work on floats, 698 # in particular int specifiers 699 for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] + 700 [chr(x) for x in range(ord('A'), ord('Z')+1)]): 701 if not format_spec in 'eEfFgGn%': 702 self.assertRaises(ValueError, format, 0.0, format_spec) 703 self.assertRaises(ValueError, format, 1.0, format_spec) 704 self.assertRaises(ValueError, format, -1.0, format_spec) 705 self.assertRaises(ValueError, format, 1e100, format_spec) 706 self.assertRaises(ValueError, format, -1e100, format_spec) 707 self.assertRaises(ValueError, format, 1e-100, format_spec) 708 self.assertRaises(ValueError, format, -1e-100, format_spec) 709 710 # Alternate formatting is not supported 711 self.assertRaises(ValueError, format, 0.0, '#') 712 self.assertRaises(ValueError, format, 0.0, '#20f') 713 714 # Issue 6902 715 test(12345.6, "0<20", '12345.60000000000000') 716 test(12345.6, "1<20", '12345.61111111111111') 717 test(12345.6, "*<20", '12345.6*************') 718 test(12345.6, "0>20", '000000000000012345.6') 719 test(12345.6, "1>20", '111111111111112345.6') 720 test(12345.6, "*>20", '*************12345.6') 721 test(12345.6, "0=20", '000000000000012345.6') 722 test(12345.6, "1=20", '111111111111112345.6') 723 test(12345.6, "*=20", '*************12345.6') 724 725 def test_format_spec_errors(self): 726 # int, float, and string all share the same format spec 727 # mini-language parser. 728 729 # Check that we can't ask for too many digits. This is 730 # probably a CPython specific test. It tries to put the width 731 # into a C long. 732 self.assertRaises(ValueError, format, 0, '1'*10000 + 'd') 733 734 # Similar with the precision. 735 self.assertRaises(ValueError, format, 0, '.' + '1'*10000 + 'd') 736 737 # And may as well test both. 738 self.assertRaises(ValueError, format, 0, '1'*1000 + '.' + '1'*10000 + 'd') 739 740 # Make sure commas aren't allowed with various type codes 741 for code in 'xXobns': 742 self.assertRaises(ValueError, format, 0, ',' + code) 743 744 def test_internal_sizes(self): 745 self.assertGreater(object.__basicsize__, 0) 746 self.assertGreater(tuple.__itemsize__, 0) 747 748 749 def test_main(): 750 with check_py3k_warnings( 751 ("buffer.. not supported", DeprecationWarning), 752 ("classic long division", DeprecationWarning)): 753 run_unittest(TypesTests) 754 755 if __name__ == '__main__': 756 test_main() 757