Changeset 391 for python/trunk/Lib/test/test_long.py
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Lib/test/test_long.py
r2 r391 1 1 import unittest 2 from test import test_support3 2 import sys 4 3 5 4 import random 5 import math 6 7 from test import test_int, test_support 6 8 7 9 # Used for lazy formatting of failure messages … … 15 17 16 18 # SHIFT should match the value in longintrepr.h for best testing. 17 SHIFT = 1519 SHIFT = sys.long_info.bits_per_digit 18 20 BASE = 2 ** SHIFT 19 21 MASK = BASE - 1 … … 78 80 ] 79 81 80 81 class LongTest(unittest.TestCase): 82 class LongTest(test_int.IntLongCommonTests, unittest.TestCase): 83 84 ntype = long 82 85 83 86 # Get quasi-random long consisting of ndigits digits (in base BASE). … … 88 91 89 92 def getran(self, ndigits): 90 self.assert _(ndigits > 0)93 self.assertTrue(ndigits > 0) 91 94 nbits_hi = ndigits * SHIFT 92 95 nbits_lo = nbits_hi - SHIFT + 1 … … 97 100 bits = (r >> 1) + 1 98 101 bits = min(bits, nbits_hi - nbits) 99 self.assert _(1 <= bits <= SHIFT)102 self.assertTrue(1 <= bits <= SHIFT) 100 103 nbits = nbits + bits 101 104 answer = answer << bits … … 103 106 answer = answer | ((1 << bits) - 1) 104 107 r = int(random.random() * (SHIFT * 2)) 105 self.assert _(nbits_lo <= nbits <= nbits_hi)108 self.assertTrue(nbits_lo <= nbits <= nbits_hi) 106 109 if random.random() < 0.5: 107 110 answer = -answer … … 129 132 eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y)) 130 133 if y > 0: 131 self.assert _(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))134 self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y)) 132 135 else: 133 self.assert _(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))136 self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y)) 134 137 135 138 def test_division(self): … … 142 145 y = self.getran(leny) or 1L 143 146 self.check_division(x, y) 147 148 # specific numbers chosen to exercise corner cases of the 149 # current long division implementation 150 151 # 30-bit cases involving a quotient digit estimate of BASE+1 152 self.check_division(1231948412290879395966702881L, 153 1147341367131428698L) 154 self.check_division(815427756481275430342312021515587883L, 155 707270836069027745L) 156 self.check_division(627976073697012820849443363563599041L, 157 643588798496057020L) 158 self.check_division(1115141373653752303710932756325578065L, 159 1038556335171453937726882627L) 160 # 30-bit cases that require the post-subtraction correction step 161 self.check_division(922498905405436751940989320930368494L, 162 949985870686786135626943396L) 163 self.check_division(768235853328091167204009652174031844L, 164 1091555541180371554426545266L) 165 166 # 15-bit cases involving a quotient digit estimate of BASE+1 167 self.check_division(20172188947443L, 615611397L) 168 self.check_division(1020908530270155025L, 950795710L) 169 self.check_division(128589565723112408L, 736393718L) 170 self.check_division(609919780285761575L, 18613274546784L) 171 # 15-bit cases that require the post-subtraction correction step 172 self.check_division(710031681576388032L, 26769404391308L) 173 self.check_division(1933622614268221L, 30212853348836L) 174 175 144 176 145 177 def test_karatsuba(self): … … 291 323 self.assertEqual(long(-3.5), -3L) 292 324 self.assertEqual(long("-3"), -3L) 325 self.assertEqual(long("0b10", 2), 2L) 326 self.assertEqual(long("0o10", 8), 8L) 327 self.assertEqual(long("0x10", 16), 16L) 293 328 if test_support.have_unicode: 294 329 self.assertEqual(long(unicode("-3")), -3L) … … 323 358 self.assertRaises(ValueError, long, '53', 40) 324 359 self.assertRaises(TypeError, long, 1, 12) 360 361 # tests with base 0 362 self.assertEqual(long(' 0123 ', 0), 83) 363 self.assertEqual(long(' 0123 ', 0), 83) 364 self.assertEqual(long('000', 0), 0) 365 self.assertEqual(long('0o123', 0), 83) 366 self.assertEqual(long('0x123', 0), 291) 367 self.assertEqual(long('0b100', 0), 4) 368 self.assertEqual(long(' 0O123 ', 0), 83) 369 self.assertEqual(long(' 0X123 ', 0), 291) 370 self.assertEqual(long(' 0B100 ', 0), 4) 371 self.assertEqual(long('0', 0), 0) 372 self.assertEqual(long('+0', 0), 0) 373 self.assertEqual(long('-0', 0), 0) 374 self.assertEqual(long('00', 0), 0) 375 self.assertRaises(ValueError, long, '08', 0) 376 self.assertRaises(ValueError, long, '-012395', 0) 325 377 326 378 # SF patch #1638879: embedded NULs were not detected with … … 481 533 long(TruncReturnsNonIntegral()) 482 534 except TypeError as e: 483 self.assertEqual s(str(e),484 485 535 self.assertEqual(str(e), 536 "__trunc__ returned non-Integral" 537 " (type NonIntegral)") 486 538 else: 487 539 self.fail("Failed to raise TypeError with %s" % … … 522 574 except OverflowError: 523 575 self.fail("int(long(sys.maxint) + 1) mustn't overflow") 524 self.assert _(isinstance(y, long),576 self.assertIsInstance(y, long, 525 577 "int(long(sys.maxint) + 1) should have returned long") 526 578 … … 530 582 except OverflowError: 531 583 self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow") 532 self.assert _(isinstance(y, long),584 self.assertIsInstance(y, long, 533 585 "int(long(-sys.maxint-1) - 1) should have returned long") 534 586 … … 537 589 x = long2(1L<<100) 538 590 y = int(x) 539 self.assert _(type(y) is long,591 self.assertTrue(type(y) is long, 540 592 "overflowing int conversion must return long not long subtype") 541 593 … … 545 597 return i, j 546 598 547 self.assertEqual(X()[-5L:7L], (-5, 7)) 548 # use the clamping effect to test the smallest and largest longs 549 # that fit a Py_ssize_t 550 slicemin, slicemax = X()[-2L**100:2L**100] 551 self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax)) 599 with test_support.check_py3k_warnings(): 600 self.assertEqual(X()[-5L:7L], (-5, 7)) 601 # use the clamping effect to test the smallest and largest longs 602 # that fit a Py_ssize_t 603 slicemin, slicemax = X()[-2L**100:2L**100] 604 self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax)) 605 606 def test_issue9869(self): 607 # Issue 9869: Interpreter crash when initializing an instance 608 # of a long subclass from an object whose __long__ method returns 609 # a plain int. 610 class BadLong(object): 611 def __long__(self): 612 return 1000000 613 614 class MyLong(long): 615 pass 616 617 x = MyLong(BadLong()) 618 self.assertIsInstance(x, long) 619 self.assertEqual(x, 1000000) 620 552 621 553 622 # ----------------------------------- tests of auto int->long conversion 554 623 555 624 def test_auto_overflow(self): 556 import math, sys557 558 625 special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1] 559 626 sqrt = int(math.sqrt(sys.maxint)) … … 589 656 590 657 if y: 591 expected = longx / longy 592 got = x / y 658 with test_support.check_py3k_warnings(): 659 expected = longx / longy 660 got = x / y 593 661 checkit(x, '/', y) 594 662 … … 615 683 self.assertRaises(TypeError, pow,longx, longy, long(z)) 616 684 685 @unittest.skipUnless(float.__getformat__("double").startswith("IEEE"), 686 "test requires IEEE 754 doubles") 687 def test_float_conversion(self): 688 import sys 689 DBL_MAX = sys.float_info.max 690 DBL_MAX_EXP = sys.float_info.max_exp 691 DBL_MANT_DIG = sys.float_info.mant_dig 692 693 exact_values = [0L, 1L, 2L, 694 long(2**53-3), 695 long(2**53-2), 696 long(2**53-1), 697 long(2**53), 698 long(2**53+2), 699 long(2**54-4), 700 long(2**54-2), 701 long(2**54), 702 long(2**54+4)] 703 for x in exact_values: 704 self.assertEqual(long(float(x)), x) 705 self.assertEqual(long(float(-x)), -x) 706 707 # test round-half-even 708 for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]: 709 for p in xrange(15): 710 self.assertEqual(long(float(2L**p*(2**53+x))), 2L**p*(2**53+y)) 711 712 for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8), 713 (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12), 714 (13, 12), (14, 16), (15, 16)]: 715 for p in xrange(15): 716 self.assertEqual(long(float(2L**p*(2**54+x))), 2L**p*(2**54+y)) 717 718 # behaviour near extremes of floating-point range 719 long_dbl_max = long(DBL_MAX) 720 top_power = 2**DBL_MAX_EXP 721 halfway = (long_dbl_max + top_power)//2 722 self.assertEqual(float(long_dbl_max), DBL_MAX) 723 self.assertEqual(float(long_dbl_max+1), DBL_MAX) 724 self.assertEqual(float(halfway-1), DBL_MAX) 725 self.assertRaises(OverflowError, float, halfway) 726 self.assertEqual(float(1-halfway), -DBL_MAX) 727 self.assertRaises(OverflowError, float, -halfway) 728 self.assertRaises(OverflowError, float, top_power-1) 729 self.assertRaises(OverflowError, float, top_power) 730 self.assertRaises(OverflowError, float, top_power+1) 731 self.assertRaises(OverflowError, float, 2*top_power-1) 732 self.assertRaises(OverflowError, float, 2*top_power) 733 self.assertRaises(OverflowError, float, top_power*top_power) 734 735 for p in xrange(100): 736 x = long(2**p * (2**53 + 1) + 1) 737 y = long(2**p * (2**53+ 2)) 738 self.assertEqual(long(float(x)), y) 739 740 x = long(2**p * (2**53 + 1)) 741 y = long(2**p * 2**53) 742 self.assertEqual(long(float(x)), y) 743 617 744 def test_float_overflow(self): 618 import math619 620 745 for x in -2.0, -1.0, 0.0, 1.0, 2.0: 621 746 self.assertEqual(float(long(x)), x) … … 647 772 648 773 def test_logs(self): 649 import math650 651 774 LOG10E = math.log10(math.e) 652 775 … … 668 791 def test_mixed_compares(self): 669 792 eq = self.assertEqual 670 import math671 793 672 794 # We're mostly concerned with that mixing floats and longs does the … … 752 874 self.assertRaises(ValueError, long, float('nan')) 753 875 876 def test_bit_length(self): 877 tiny = 1e-10 878 for x in xrange(-65000, 65000): 879 x = long(x) 880 k = x.bit_length() 881 # Check equivalence with Python version 882 self.assertEqual(k, len(bin(x).lstrip('-0b'))) 883 # Behaviour as specified in the docs 884 if x != 0: 885 self.assertTrue(2**(k-1) <= abs(x) < 2**k) 886 else: 887 self.assertEqual(k, 0) 888 # Alternative definition: x.bit_length() == 1 + floor(log_2(x)) 889 if x != 0: 890 # When x is an exact power of 2, numeric errors can 891 # cause floor(log(x)/log(2)) to be one too small; for 892 # small x this can be fixed by adding a small quantity 893 # to the quotient before taking the floor. 894 self.assertEqual(k, 1 + math.floor( 895 math.log(abs(x))/math.log(2) + tiny)) 896 897 self.assertEqual((0L).bit_length(), 0) 898 self.assertEqual((1L).bit_length(), 1) 899 self.assertEqual((-1L).bit_length(), 1) 900 self.assertEqual((2L).bit_length(), 2) 901 self.assertEqual((-2L).bit_length(), 2) 902 for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]: 903 a = 2L**i 904 self.assertEqual((a-1).bit_length(), i) 905 self.assertEqual((1-a).bit_length(), i) 906 self.assertEqual((a).bit_length(), i+1) 907 self.assertEqual((-a).bit_length(), i+1) 908 self.assertEqual((a+1).bit_length(), i+1) 909 self.assertEqual((-a-1).bit_length(), i+1) 910 911 754 912 def test_main(): 755 913 test_support.run_unittest(LongTest)
Note:
See TracChangeset
for help on using the changeset viewer.