Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
test.test_long.LongTest Class Reference

List of all members.

Public Member Functions

def getran
def getran2
def check_division
def test_division
def test_karatsuba
def check_bitop_identities_1
def check_bitop_identities_2
def check_bitop_identities_3
def test_bitop_identities
def slow_format
def check_format_1
def test_format
def test_long
def test_conversion
def test_float_conversion
def test_float_overflow
def test_logs
def test_mixed_compares
def test__format__
def test_nan_inf
def test_true_division
def check_truediv
def test_correctly_rounded_true_division
def test_small_ints
def test_bit_length
def test_round
def test_to_bytes
def test_from_bytes

Public Attributes

 n
 d

Detailed Description

Definition at line 77 of file test_long.py.


Member Function Documentation

Definition at line 193 of file test_long.py.

00193 
00194     def check_bitop_identities_1(self, x):
00195         eq = self.assertEqual
00196         eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
00197         eq(x | 0, x, Frm("x | 0 != x for x=%r", x))
00198         eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x))
00199         eq(x & -1, x, Frm("x & -1 != x for x=%r", x))
00200         eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x))
00201         eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x))
00202         eq(x, ~~x, Frm("x != ~~x for x=%r", x))
00203         eq(x & x, x, Frm("x & x != x for x=%r", x))
00204         eq(x | x, x, Frm("x | x != x for x=%r", x))
00205         eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x))
00206         eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x))
00207         eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x))
00208         eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x))
00209         eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x))
00210         eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x))
00211         for n in range(2*SHIFT):
00212             p2 = 2 ** n
00213             eq(x << n >> n, x,
00214                 Frm("x << n >> n != x for x=%r, n=%r", (x, n)))
00215             eq(x // p2, x >> n,
00216                 Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2)))
00217             eq(x * p2, x << n,
00218                 Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2)))
00219             eq(x & -p2, x >> n << n,
00220                 Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2)))
00221             eq(x & -p2, x & ~(p2 - 1),
00222                 Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2)))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.check_bitop_identities_2 (   self,
  x,
  y 
)

Definition at line 223 of file test_long.py.

00223 
00224     def check_bitop_identities_2(self, x, y):
00225         eq = self.assertEqual
00226         eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y)))
00227         eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y)))
00228         eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y)))
00229         eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y)))
00230         eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y)))
00231         eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y)))
00232         eq(x ^ y, (x | y) & ~(x & y),
00233              Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y)))
00234         eq(x ^ y, (x & ~y) | (~x & y),
00235              Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y)))
00236         eq(x ^ y, (x | y) & (~x | ~y),
00237              Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y)))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.check_bitop_identities_3 (   self,
  x,
  y,
  z 
)

Definition at line 238 of file test_long.py.

00238 
00239     def check_bitop_identities_3(self, x, y, z):
00240         eq = self.assertEqual
00241         eq((x & y) & z, x & (y & z),
00242              Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z)))
00243         eq((x | y) | z, x | (y | z),
00244              Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z)))
00245         eq((x ^ y) ^ z, x ^ (y ^ z),
00246              Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z)))
00247         eq(x & (y | z), (x & y) | (x & z),
00248              Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z)))
00249         eq(x | (y & z), (x | y) & (x | z),
00250              Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z)))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.check_division (   self,
  x,
  y 
)

Definition at line 117 of file test_long.py.

00117 
00118     def check_division(self, x, y):
00119         eq = self.assertEqual
00120         q, r = divmod(x, y)
00121         q2, r2 = x//y, x%y
00122         pab, pba = x*y, y*x
00123         eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y))
00124         eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y))
00125         eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y))
00126         eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
00127         if y > 0:
00128             self.assertTrue(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
00129         else:
00130             self.assertTrue(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.check_format_1 (   self,
  x 
)

Definition at line 277 of file test_long.py.

00277 
00278     def check_format_1(self, x):
00279         for base, mapper in (8, oct), (10, repr), (16, hex):
00280             got = mapper(x)
00281             expected = self.slow_format(x, base)
00282             msg = Frm("%s returned %r but expected %r for %r",
00283                 mapper.__name__, got, expected, x)
00284             self.assertEqual(got, expected, msg)
00285             self.assertEqual(int(got, 0), x, Frm('int("%s", 0) != %r', got, x))
00286         # str() has to be checked a little differently since there's no
00287         # trailing "L"
00288         got = str(x)
00289         expected = self.slow_format(x, 10)
00290         msg = Frm("%s returned %r but expected %r for %r",
00291             mapper.__name__, got, expected, x)
00292         self.assertEqual(got, expected, msg)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.check_truediv (   self,
  a,
  b,
  skip_small = True 
)
Verify that the result of a/b is correctly rounded, by
comparing it with a pure Python implementation of correctly
rounded division.  b should be nonzero.

Definition at line 671 of file test_long.py.

00671 
00672     def check_truediv(self, a, b, skip_small=True):
00673         """Verify that the result of a/b is correctly rounded, by
00674         comparing it with a pure Python implementation of correctly
00675         rounded division.  b should be nonzero."""
00676 
00677         # skip check for small a and b: in this case, the current
00678         # implementation converts the arguments to float directly and
00679         # then applies a float division.  This can give doubly-rounded
00680         # results on x87-using machines (particularly 32-bit Linux).
00681         if skip_small and max(abs(a), abs(b)) < 2**DBL_MANT_DIG:
00682             return
00683 
00684         try:
00685             # use repr so that we can distinguish between -0.0 and 0.0
00686             expected = repr(truediv(a, b))
00687         except OverflowError:
00688             expected = 'overflow'
00689         except ZeroDivisionError:
00690             expected = 'zerodivision'
00691 
00692         try:
00693             got = repr(a / b)
00694         except OverflowError:
00695             got = 'overflow'
00696         except ZeroDivisionError:
00697             got = 'zerodivision'
00698 
00699         self.assertEqual(expected, got, "Incorrectly rounded division {}/{}: "
00700                          "expected {}, got {}".format(a, b, expected, got))

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_long.LongTest.getran (   self,
  ndigits 
)

Definition at line 85 of file test_long.py.

00085 
00086     def getran(self, ndigits):
00087         self.assertTrue(ndigits > 0)
00088         nbits_hi = ndigits * SHIFT
00089         nbits_lo = nbits_hi - SHIFT + 1
00090         answer = 0
00091         nbits = 0
00092         r = int(random.random() * (SHIFT * 2)) | 1  # force 1 bits to start
00093         while nbits < nbits_lo:
00094             bits = (r >> 1) + 1
00095             bits = min(bits, nbits_hi - nbits)
00096             self.assertTrue(1 <= bits <= SHIFT)
00097             nbits = nbits + bits
00098             answer = answer << bits
00099             if r & 1:
00100                 answer = answer | ((1 << bits) - 1)
00101             r = int(random.random() * (SHIFT * 2))
00102         self.assertTrue(nbits_lo <= nbits <= nbits_hi)
00103         if random.random() < 0.5:
00104             answer = -answer
00105         return answer

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 109 of file test_long.py.

00109 
00110     def getran2(ndigits):
00111         answer = 0
00112         for i in range(ndigits):
00113             answer = (answer << SHIFT) | random.randint(0, MASK)
00114         if random.random() < 0.5:
00115             answer = -answer
00116         return answer

Here is the call graph for this function:

def test.test_long.LongTest.slow_format (   self,
  x,
  base 
)

Definition at line 263 of file test_long.py.

00263 
00264     def slow_format(self, x, base):
00265         digits = []
00266         sign = 0
00267         if x < 0:
00268             sign, x = 1, -x
00269         while x:
00270             x, r = divmod(x, base)
00271             digits.append(int(r))
00272         digits.reverse()
00273         digits = digits or [0]
00274         return '-'[:sign] + \
00275                {2: '0b', 8: '0o', 10: '', 16: '0x'}[base] + \
00276                "".join("0123456789abcdef"[i] for i in digits)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 566 of file test_long.py.

00566 
00567     def test__format__(self):
00568         self.assertEqual(format(123456789, 'd'), '123456789')
00569         self.assertEqual(format(123456789, 'd'), '123456789')
00570 
00571         # sign and aligning are interdependent
00572         self.assertEqual(format(1, "-"), '1')
00573         self.assertEqual(format(-1, "-"), '-1')
00574         self.assertEqual(format(1, "-3"), '  1')
00575         self.assertEqual(format(-1, "-3"), ' -1')
00576         self.assertEqual(format(1, "+3"), ' +1')
00577         self.assertEqual(format(-1, "+3"), ' -1')
00578         self.assertEqual(format(1, " 3"), '  1')
00579         self.assertEqual(format(-1, " 3"), ' -1')
00580         self.assertEqual(format(1, " "), ' 1')
00581         self.assertEqual(format(-1, " "), '-1')
00582 
00583         # hex
00584         self.assertEqual(format(3, "x"), "3")
00585         self.assertEqual(format(3, "X"), "3")
00586         self.assertEqual(format(1234, "x"), "4d2")
00587         self.assertEqual(format(-1234, "x"), "-4d2")
00588         self.assertEqual(format(1234, "8x"), "     4d2")
00589         self.assertEqual(format(-1234, "8x"), "    -4d2")
00590         self.assertEqual(format(1234, "x"), "4d2")
00591         self.assertEqual(format(-1234, "x"), "-4d2")
00592         self.assertEqual(format(-3, "x"), "-3")
00593         self.assertEqual(format(-3, "X"), "-3")
00594         self.assertEqual(format(int('be', 16), "x"), "be")
00595         self.assertEqual(format(int('be', 16), "X"), "BE")
00596         self.assertEqual(format(-int('be', 16), "x"), "-be")
00597         self.assertEqual(format(-int('be', 16), "X"), "-BE")
00598 
00599         # octal
00600         self.assertEqual(format(3, "b"), "11")
00601         self.assertEqual(format(-3, "b"), "-11")
00602         self.assertEqual(format(1234, "b"), "10011010010")
00603         self.assertEqual(format(-1234, "b"), "-10011010010")
00604         self.assertEqual(format(1234, "-b"), "10011010010")
00605         self.assertEqual(format(-1234, "-b"), "-10011010010")
00606         self.assertEqual(format(1234, " b"), " 10011010010")
00607         self.assertEqual(format(-1234, " b"), "-10011010010")
00608         self.assertEqual(format(1234, "+b"), "+10011010010")
00609         self.assertEqual(format(-1234, "+b"), "-10011010010")
00610 
00611         # make sure these are errors
00612         self.assertRaises(ValueError, format, 3, "1.3")  # precision disallowed
00613         self.assertRaises(ValueError, format, 3, "+c")   # sign not allowed
00614                                                          # with 'c'
00615 
00616         # ensure that only int and float type specifiers work
00617         for format_spec in ([chr(x) for x in range(ord('a'), ord('z')+1)] +
00618                             [chr(x) for x in range(ord('A'), ord('Z')+1)]):
00619             if not format_spec in 'bcdoxXeEfFgGn%':
00620                 self.assertRaises(ValueError, format, 0, format_spec)
00621                 self.assertRaises(ValueError, format, 1, format_spec)
00622                 self.assertRaises(ValueError, format, -1, format_spec)
00623                 self.assertRaises(ValueError, format, 2**100, format_spec)
00624                 self.assertRaises(ValueError, format, -(2**100), format_spec)
00625 
00626         # ensure that float type specifiers work; format converts
00627         #  the int to a float
00628         for format_spec in 'eEfFgG%':
00629             for value in [0, 1, -1, 100, -100, 1234567890, -1234567890]:
00630                 self.assertEqual(format(value, format_spec),
00631                                  format(float(value), format_spec))

Here is the call graph for this function:

Definition at line 811 of file test_long.py.

00811 
00812     def test_bit_length(self):
00813         tiny = 1e-10
00814         for x in range(-65000, 65000):
00815             k = x.bit_length()
00816             # Check equivalence with Python version
00817             self.assertEqual(k, len(bin(x).lstrip('-0b')))
00818             # Behaviour as specified in the docs
00819             if x != 0:
00820                 self.assertTrue(2**(k-1) <= abs(x) < 2**k)
00821             else:
00822                 self.assertEqual(k, 0)
00823             # Alternative definition: x.bit_length() == 1 + floor(log_2(x))
00824             if x != 0:
00825                 # When x is an exact power of 2, numeric errors can
00826                 # cause floor(log(x)/log(2)) to be one too small; for
00827                 # small x this can be fixed by adding a small quantity
00828                 # to the quotient before taking the floor.
00829                 self.assertEqual(k, 1 + math.floor(
00830                         math.log(abs(x))/math.log(2) + tiny))
00831 
00832         self.assertEqual((0).bit_length(), 0)
00833         self.assertEqual((1).bit_length(), 1)
00834         self.assertEqual((-1).bit_length(), 1)
00835         self.assertEqual((2).bit_length(), 2)
00836         self.assertEqual((-2).bit_length(), 2)
00837         for i in [2, 3, 15, 16, 17, 31, 32, 33, 63, 64, 234]:
00838             a = 2**i
00839             self.assertEqual((a-1).bit_length(), i)
00840             self.assertEqual((1-a).bit_length(), i)
00841             self.assertEqual((a).bit_length(), i+1)
00842             self.assertEqual((-a).bit_length(), i+1)
00843             self.assertEqual((a+1).bit_length(), i+1)
00844             self.assertEqual((-a-1).bit_length(), i+1)

Here is the call graph for this function:

Definition at line 251 of file test_long.py.

00251 
00252     def test_bitop_identities(self):
00253         for x in special:
00254             self.check_bitop_identities_1(x)
00255         digits = range(1, MAXDIGITS+1)
00256         for lenx in digits:
00257             x = self.getran(lenx)
00258             self.check_bitop_identities_1(x)
00259             for leny in digits:
00260                 y = self.getran(leny)
00261                 self.check_bitop_identities_2(x, y)
00262                 self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))

Here is the call graph for this function:

Definition at line 354 of file test_long.py.

00354 
00355     def test_conversion(self):
00356 
00357         class JustLong:
00358             # test that __long__ no longer used in 3.x
00359             def __long__(self):
00360                 return 42
00361         self.assertRaises(TypeError, int, JustLong())
00362 
00363         class LongTrunc:
00364             # __long__ should be ignored in 3.x
00365             def __long__(self):
00366                 return 42
00367             def __trunc__(self):
00368                 return 1729
00369         self.assertEqual(int(LongTrunc()), 1729)

Here is the call graph for this function:

Definition at line 702 of file test_long.py.

00702 
00703     def test_correctly_rounded_true_division(self):
00704         # more stringent tests than those above, checking that the
00705         # result of true division of ints is always correctly rounded.
00706         # This test should probably be considered CPython-specific.
00707 
00708         # Exercise all the code paths not involving Gb-sized ints.
00709         # ... divisions involving zero
00710         self.check_truediv(123, 0)
00711         self.check_truediv(-456, 0)
00712         self.check_truediv(0, 3)
00713         self.check_truediv(0, -3)
00714         self.check_truediv(0, 0)
00715         # ... overflow or underflow by large margin
00716         self.check_truediv(671 * 12345 * 2**DBL_MAX_EXP, 12345)
00717         self.check_truediv(12345, 345678 * 2**(DBL_MANT_DIG - DBL_MIN_EXP))
00718         # ... a much larger or smaller than b
00719         self.check_truediv(12345*2**100, 98765)
00720         self.check_truediv(12345*2**30, 98765*7**81)
00721         # ... a / b near a boundary: one of 1, 2**DBL_MANT_DIG, 2**DBL_MIN_EXP,
00722         #                 2**DBL_MAX_EXP, 2**(DBL_MIN_EXP-DBL_MANT_DIG)
00723         bases = (0, DBL_MANT_DIG, DBL_MIN_EXP,
00724                  DBL_MAX_EXP, DBL_MIN_EXP - DBL_MANT_DIG)
00725         for base in bases:
00726             for exp in range(base - 15, base + 15):
00727                 self.check_truediv(75312*2**max(exp, 0), 69187*2**max(-exp, 0))
00728                 self.check_truediv(69187*2**max(exp, 0), 75312*2**max(-exp, 0))
00729 
00730         # overflow corner case
00731         for m in [1, 2, 7, 17, 12345, 7**100,
00732                   -1, -2, -5, -23, -67891, -41**50]:
00733             for n in range(-10, 10):
00734                 self.check_truediv(m*DBL_MIN_OVERFLOW + n, m)
00735                 self.check_truediv(m*DBL_MIN_OVERFLOW + n, -m)
00736 
00737         # check detection of inexactness in shifting stage
00738         for n in range(250):
00739             # (2**DBL_MANT_DIG+1)/(2**DBL_MANT_DIG) lies halfway
00740             # between two representable floats, and would usually be
00741             # rounded down under round-half-to-even.  The tiniest of
00742             # additions to the numerator should cause it to be rounded
00743             # up instead.
00744             self.check_truediv((2**DBL_MANT_DIG + 1)*12345*2**200 + 2**n,
00745                            2**DBL_MANT_DIG*12345)
00746 
00747         # 1/2731 is one of the smallest division cases that's subject
00748         # to double rounding on IEEE 754 machines working internally with
00749         # 64-bit precision.  On such machines, the next check would fail,
00750         # were it not explicitly skipped in check_truediv.
00751         self.check_truediv(1, 2731)
00752 
00753         # a particularly bad case for the old algorithm:  gives an
00754         # error of close to 3.5 ulps.
00755         self.check_truediv(295147931372582273023, 295147932265116303360)
00756         for i in range(1000):
00757             self.check_truediv(10**(i+1), 10**i)
00758             self.check_truediv(10**i, 10**(i+1))
00759 
00760         # test round-half-to-even behaviour, normal result
00761         for m in [1, 2, 4, 7, 8, 16, 17, 32, 12345, 7**100,
00762                   -1, -2, -5, -23, -67891, -41**50]:
00763             for n in range(-10, 10):
00764                 self.check_truediv(2**DBL_MANT_DIG*m + n, m)
00765 
00766         # test round-half-to-even, subnormal result
00767         for n in range(-20, 20):
00768             self.check_truediv(n, 2**1076)
00769 
00770         # largeish random divisions: a/b where |a| <= |b| <=
00771         # 2*|a|; |ans| is between 0.5 and 1.0, so error should
00772         # always be bounded by 2**-54 with equality possible only
00773         # if the least significant bit of q=ans*2**53 is zero.
00774         for M in [10**10, 10**100, 10**1000]:
00775             for i in range(1000):
00776                 a = random.randrange(1, M)
00777                 b = random.randrange(a, 2*a+1)
00778                 self.check_truediv(a, b)
00779                 self.check_truediv(-a, b)
00780                 self.check_truediv(a, -b)
00781                 self.check_truediv(-a, -b)
00782 
00783         # and some (genuinely) random tests
00784         for _ in range(10000):
00785             a_bits = random.randrange(1000)
00786             b_bits = random.randrange(1, 1000)
00787             x = random.randrange(2**a_bits)
00788             y = random.randrange(1, 2**b_bits)
00789             self.check_truediv(x, y)
00790             self.check_truediv(x, -y)
00791             self.check_truediv(-x, y)
00792             self.check_truediv(-x, -y)

Here is the call graph for this function:

Definition at line 131 of file test_long.py.

00131 
00132     def test_division(self):
00133         digits = list(range(1, MAXDIGITS+1)) + list(range(KARATSUBA_CUTOFF,
00134                                                       KARATSUBA_CUTOFF + 14))
00135         digits.append(KARATSUBA_CUTOFF * 3)
00136         for lenx in digits:
00137             x = self.getran(lenx)
00138             for leny in digits:
00139                 y = self.getran(leny) or 1
00140                 self.check_division(x, y)
00141 
00142         # specific numbers chosen to exercise corner cases of the
00143         # current long division implementation
00144 
00145         # 30-bit cases involving a quotient digit estimate of BASE+1
00146         self.check_division(1231948412290879395966702881,
00147                             1147341367131428698)
00148         self.check_division(815427756481275430342312021515587883,
00149                        707270836069027745)
00150         self.check_division(627976073697012820849443363563599041,
00151                        643588798496057020)
00152         self.check_division(1115141373653752303710932756325578065,
00153                        1038556335171453937726882627)
00154         # 30-bit cases that require the post-subtraction correction step
00155         self.check_division(922498905405436751940989320930368494,
00156                        949985870686786135626943396)
00157         self.check_division(768235853328091167204009652174031844,
00158                        1091555541180371554426545266)
00159 
00160         # 15-bit cases involving a quotient digit estimate of BASE+1
00161         self.check_division(20172188947443, 615611397)
00162         self.check_division(1020908530270155025, 950795710)
00163         self.check_division(128589565723112408, 736393718)
00164         self.check_division(609919780285761575, 18613274546784)
00165         # 15-bit cases that require the post-subtraction correction step
00166         self.check_division(710031681576388032, 26769404391308)
00167         self.check_division(1933622614268221, 30212853348836)
00168 
00169 

Here is the call graph for this function:

Definition at line 371 of file test_long.py.

00371 
00372     def test_float_conversion(self):
00373 
00374         exact_values = [0, 1, 2,
00375                          2**53-3,
00376                          2**53-2,
00377                          2**53-1,
00378                          2**53,
00379                          2**53+2,
00380                          2**54-4,
00381                          2**54-2,
00382                          2**54,
00383                          2**54+4]
00384         for x in exact_values:
00385             self.assertEqual(float(x), x)
00386             self.assertEqual(float(-x), -x)
00387 
00388         # test round-half-even
00389         for x, y in [(1, 0), (2, 2), (3, 4), (4, 4), (5, 4), (6, 6), (7, 8)]:
00390             for p in range(15):
00391                 self.assertEqual(int(float(2**p*(2**53+x))), 2**p*(2**53+y))
00392 
00393         for x, y in [(0, 0), (1, 0), (2, 0), (3, 4), (4, 4), (5, 4), (6, 8),
00394                      (7, 8), (8, 8), (9, 8), (10, 8), (11, 12), (12, 12),
00395                      (13, 12), (14, 16), (15, 16)]:
00396             for p in range(15):
00397                 self.assertEqual(int(float(2**p*(2**54+x))), 2**p*(2**54+y))
00398 
00399         # behaviour near extremes of floating-point range
00400         int_dbl_max = int(DBL_MAX)
00401         top_power = 2**DBL_MAX_EXP
00402         halfway = (int_dbl_max + top_power)//2
00403         self.assertEqual(float(int_dbl_max), DBL_MAX)
00404         self.assertEqual(float(int_dbl_max+1), DBL_MAX)
00405         self.assertEqual(float(halfway-1), DBL_MAX)
00406         self.assertRaises(OverflowError, float, halfway)
00407         self.assertEqual(float(1-halfway), -DBL_MAX)
00408         self.assertRaises(OverflowError, float, -halfway)
00409         self.assertRaises(OverflowError, float, top_power-1)
00410         self.assertRaises(OverflowError, float, top_power)
00411         self.assertRaises(OverflowError, float, top_power+1)
00412         self.assertRaises(OverflowError, float, 2*top_power-1)
00413         self.assertRaises(OverflowError, float, 2*top_power)
00414         self.assertRaises(OverflowError, float, top_power*top_power)
00415 
00416         for p in range(100):
00417             x = 2**p * (2**53 + 1) + 1
00418             y = 2**p * (2**53 + 2)
00419             self.assertEqual(int(float(x)), y)
00420 
00421             x = 2**p * (2**53 + 1)
00422             y = 2**p * 2**53
00423             self.assertEqual(int(float(x)), y)

Here is the call graph for this function:

Definition at line 424 of file test_long.py.

00424 
00425     def test_float_overflow(self):
00426         for x in -2.0, -1.0, 0.0, 1.0, 2.0:
00427             self.assertEqual(float(int(x)), x)
00428 
00429         shuge = '12345' * 120
00430         huge = 1 << 30000
00431         mhuge = -huge
00432         namespace = {'huge': huge, 'mhuge': mhuge, 'shuge': shuge, 'math': math}
00433         for test in ["float(huge)", "float(mhuge)",
00434                      "complex(huge)", "complex(mhuge)",
00435                      "complex(huge, 1)", "complex(mhuge, 1)",
00436                      "complex(1, huge)", "complex(1, mhuge)",
00437                      "1. + huge", "huge + 1.", "1. + mhuge", "mhuge + 1.",
00438                      "1. - huge", "huge - 1.", "1. - mhuge", "mhuge - 1.",
00439                      "1. * huge", "huge * 1.", "1. * mhuge", "mhuge * 1.",
00440                      "1. // huge", "huge // 1.", "1. // mhuge", "mhuge // 1.",
00441                      "1. / huge", "huge / 1.", "1. / mhuge", "mhuge / 1.",
00442                      "1. ** huge", "huge ** 1.", "1. ** mhuge", "mhuge ** 1.",
00443                      "math.sin(huge)", "math.sin(mhuge)",
00444                      "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
00445                      # math.floor() of an int returns an int now
00446                      ##"math.floor(huge)", "math.floor(mhuge)",
00447                      ]:
00448 
00449             self.assertRaises(OverflowError, eval, test, namespace)
00450 
00451         # XXX Perhaps float(shuge) can raise OverflowError on some box?
00452         # The comparison should not.
00453         self.assertNotEqual(float(shuge), int(shuge),
00454             "float(shuge) should not equal int(shuge)")

Here is the call graph for this function:

Definition at line 293 of file test_long.py.

00293 
00294     def test_format(self):
00295         for x in special:
00296             self.check_format_1(x)
00297         for i in range(10):
00298             for lenx in range(1, MAXDIGITS+1):
00299                 x = self.getran(lenx)
00300                 self.check_format_1(x)

Here is the call graph for this function:

Definition at line 1018 of file test_long.py.

01018 
01019     def test_from_bytes(self):
01020         def check(tests, byteorder, signed=False):
01021             for test, expected in tests.items():
01022                 try:
01023                     self.assertEqual(
01024                         int.from_bytes(test, byteorder, signed=signed),
01025                         expected)
01026                 except Exception as err:
01027                     raise AssertionError(
01028                         "failed to convert {0} with byteorder={1!r} and signed={2}"
01029                         .format(test, byteorder, signed)) from err
01030 
01031         # Convert signed big-endian byte arrays to integers.
01032         tests1 = {
01033             b'': 0,
01034             b'\x00': 0,
01035             b'\x00\x00': 0,
01036             b'\x01': 1,
01037             b'\x00\x01': 1,
01038             b'\xff': -1,
01039             b'\xff\xff': -1,
01040             b'\x81': -127,
01041             b'\x80': -128,
01042             b'\xff\x7f': -129,
01043             b'\x7f': 127,
01044             b'\x00\x81': 129,
01045             b'\xff\x01': -255,
01046             b'\xff\x00': -256,
01047             b'\x00\xff': 255,
01048             b'\x01\x00': 256,
01049             b'\x7f\xff': 32767,
01050             b'\x80\x00': -32768,
01051             b'\x00\xff\xff': 65535,
01052             b'\xff\x00\x00': -65536,
01053             b'\x80\x00\x00': -8388608
01054         }
01055         check(tests1, 'big', signed=True)
01056 
01057         # Convert signed little-endian byte arrays to integers.
01058         tests2 = {
01059             b'': 0,
01060             b'\x00': 0,
01061             b'\x00\x00': 0,
01062             b'\x01': 1,
01063             b'\x00\x01': 256,
01064             b'\xff': -1,
01065             b'\xff\xff': -1,
01066             b'\x81': -127,
01067             b'\x80': -128,
01068             b'\x7f\xff': -129,
01069             b'\x7f': 127,
01070             b'\x81\x00': 129,
01071             b'\x01\xff': -255,
01072             b'\x00\xff': -256,
01073             b'\xff\x00': 255,
01074             b'\x00\x01': 256,
01075             b'\xff\x7f': 32767,
01076             b'\x00\x80': -32768,
01077             b'\xff\xff\x00': 65535,
01078             b'\x00\x00\xff': -65536,
01079             b'\x00\x00\x80': -8388608
01080         }
01081         check(tests2, 'little', signed=True)
01082 
01083         # Convert unsigned big-endian byte arrays to integers.
01084         tests3 = {
01085             b'': 0,
01086             b'\x00': 0,
01087             b'\x01': 1,
01088             b'\x7f': 127,
01089             b'\x80': 128,
01090             b'\xff': 255,
01091             b'\x01\x00': 256,
01092             b'\x7f\xff': 32767,
01093             b'\x80\x00': 32768,
01094             b'\xff\xff': 65535,
01095             b'\x01\x00\x00': 65536,
01096         }
01097         check(tests3, 'big', signed=False)
01098 
01099         # Convert integers to unsigned little-endian byte arrays.
01100         tests4 = {
01101             b'': 0,
01102             b'\x00': 0,
01103             b'\x01': 1,
01104             b'\x7f': 127,
01105             b'\x80': 128,
01106             b'\xff': 255,
01107             b'\x00\x01': 256,
01108             b'\xff\x7f': 32767,
01109             b'\x00\x80': 32768,
01110             b'\xff\xff': 65535,
01111             b'\x00\x00\x01': 65536,
01112         }
01113         check(tests4, 'little', signed=False)
01114 
01115         class myint(int):
01116             pass
01117 
01118         self.assertTrue(type(myint.from_bytes(b'\x00', 'big')) is myint)
01119         self.assertEqual(myint.from_bytes(b'\x01', 'big'), 1)
01120         self.assertTrue(
01121             type(myint.from_bytes(b'\x00', 'big', signed=False)) is myint)
01122         self.assertEqual(myint.from_bytes(b'\x01', 'big', signed=False), 1)
01123         self.assertTrue(type(myint.from_bytes(b'\x00', 'little')) is myint)
01124         self.assertEqual(myint.from_bytes(b'\x01', 'little'), 1)
01125         self.assertTrue(type(myint.from_bytes(
01126             b'\x00', 'little', signed=False)) is myint)
01127         self.assertEqual(myint.from_bytes(b'\x01', 'little', signed=False), 1)
01128         self.assertEqual(
01129             int.from_bytes([255, 0, 0], 'big', signed=True), -65536)
01130         self.assertEqual(
01131             int.from_bytes((255, 0, 0), 'big', signed=True), -65536)
01132         self.assertEqual(int.from_bytes(
01133             bytearray(b'\xff\x00\x00'), 'big', signed=True), -65536)
01134         self.assertEqual(int.from_bytes(
01135             bytearray(b'\xff\x00\x00'), 'big', signed=True), -65536)
01136         self.assertEqual(int.from_bytes(
01137             array.array('B', b'\xff\x00\x00'), 'big', signed=True), -65536)
01138         self.assertEqual(int.from_bytes(
01139             memoryview(b'\xff\x00\x00'), 'big', signed=True), -65536)
01140         self.assertRaises(ValueError, int.from_bytes, [256], 'big')
01141         self.assertRaises(ValueError, int.from_bytes, [0], 'big\x00')
01142         self.assertRaises(ValueError, int.from_bytes, [0], 'little\x00')
01143         self.assertRaises(TypeError, int.from_bytes, "", 'big')
01144         self.assertRaises(TypeError, int.from_bytes, "\x00", 'big')
01145         self.assertRaises(TypeError, int.from_bytes, 0, 'big')
01146         self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
01147         self.assertRaises(TypeError, myint.from_bytes, "", 'big')
01148         self.assertRaises(TypeError, myint.from_bytes, "\x00", 'big')
01149         self.assertRaises(TypeError, myint.from_bytes, 0, 'big')
01150         self.assertRaises(TypeError, int.from_bytes, 0, 'big', True)
01151 

Here is the call graph for this function:

Definition at line 170 of file test_long.py.

00170 
00171     def test_karatsuba(self):
00172         digits = list(range(1, 5)) + list(range(KARATSUBA_CUTOFF,
00173                                                 KARATSUBA_CUTOFF + 10))
00174         digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100])
00175 
00176         bits = [digit * SHIFT for digit in digits]
00177 
00178         # Test products of long strings of 1 bits -- (2**x-1)*(2**y-1) ==
00179         # 2**(x+y) - 2**x - 2**y + 1, so the proper result is easy to check.
00180         for abits in bits:
00181             a = (1 << abits) - 1
00182             for bbits in bits:
00183                 if bbits < abits:
00184                     continue
00185                 b = (1 << bbits) - 1
00186                 x = a * b
00187                 y = ((1 << (abits + bbits)) -
00188                      (1 << abits) -
00189                      (1 << bbits) +
00190                      1)
00191                 self.assertEqual(x, y,
00192                     Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))

Here is the call graph for this function:

Definition at line 455 of file test_long.py.

00455 
00456     def test_logs(self):
00457         LOG10E = math.log10(math.e)
00458 
00459         for exp in list(range(10)) + [100, 1000, 10000]:
00460             value = 10 ** exp
00461             log10 = math.log10(value)
00462             self.assertAlmostEqual(log10, exp)
00463 
00464             # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
00465             # exp/LOG10E
00466             expected = exp / LOG10E
00467             log = math.log(value)
00468             self.assertAlmostEqual(log, expected)
00469 
00470         for bad in -(1 << 10000), -2, 0:
00471             self.assertRaises(ValueError, math.log, bad)
00472             self.assertRaises(ValueError, math.log10, bad)

Here is the call graph for this function:

Definition at line 301 of file test_long.py.

00301 
00302     def test_long(self):
00303         # Check conversions from string
00304         LL = [
00305                 ('1' + '0'*20, 10**20),
00306                 ('1' + '0'*100, 10**100)
00307         ]
00308         for s, v in LL:
00309             for sign in "", "+", "-":
00310                 for prefix in "", " ", "\t", "  \t\t  ":
00311                     ss = prefix + sign + s
00312                     vv = v
00313                     if sign == "-" and v is not ValueError:
00314                         vv = -v
00315                     try:
00316                         self.assertEqual(int(ss), vv)
00317                     except ValueError:
00318                         pass
00319 
00320         # trailing L should no longer be accepted...
00321         self.assertRaises(ValueError, int, '123L')
00322         self.assertRaises(ValueError, int, '123l')
00323         self.assertRaises(ValueError, int, '0L')
00324         self.assertRaises(ValueError, int, '-37L')
00325         self.assertRaises(ValueError, int, '0x32L', 16)
00326         self.assertRaises(ValueError, int, '1L', 21)
00327         # ... but it's just a normal digit if base >= 22
00328         self.assertEqual(int('1L', 22), 43)
00329 
00330         # tests with base 0
00331         self.assertEqual(int('000', 0), 0)
00332         self.assertEqual(int('0o123', 0), 83)
00333         self.assertEqual(int('0x123', 0), 291)
00334         self.assertEqual(int('0b100', 0), 4)
00335         self.assertEqual(int(' 0O123   ', 0), 83)
00336         self.assertEqual(int(' 0X123  ', 0), 291)
00337         self.assertEqual(int(' 0B100 ', 0), 4)
00338         self.assertEqual(int('0', 0), 0)
00339         self.assertEqual(int('+0', 0), 0)
00340         self.assertEqual(int('-0', 0), 0)
00341         self.assertEqual(int('00', 0), 0)
00342         self.assertRaises(ValueError, int, '08', 0)
00343         self.assertRaises(ValueError, int, '-012395', 0)
00344 
00345         # invalid bases
00346         invalid_bases = [-909,
00347                           2**31-1, 2**31, -2**31, -2**31-1,
00348                           2**63-1, 2**63, -2**63, -2**63-1,
00349                           2**100, -2**100,
00350                           ]
00351         for base in invalid_bases:
00352             self.assertRaises(ValueError, int, '42', base)
00353 

Here is the call graph for this function:

Definition at line 473 of file test_long.py.

00473 
00474     def test_mixed_compares(self):
00475         eq = self.assertEqual
00476 
00477         # We're mostly concerned with that mixing floats and longs does the
00478         # right stuff, even when longs are too large to fit in a float.
00479         # The safest way to check the results is to use an entirely different
00480         # method, which we do here via a skeletal rational class (which
00481         # represents all Python ints, longs and floats exactly).
00482         class Rat:
00483             def __init__(self, value):
00484                 if isinstance(value, int):
00485                     self.n = value
00486                     self.d = 1
00487                 elif isinstance(value, float):
00488                     # Convert to exact rational equivalent.
00489                     f, e = math.frexp(abs(value))
00490                     assert f == 0 or 0.5 <= f < 1.0
00491                     # |value| = f * 2**e exactly
00492 
00493                     # Suck up CHUNK bits at a time; 28 is enough so that we suck
00494                     # up all bits in 2 iterations for all known binary double-
00495                     # precision formats, and small enough to fit in an int.
00496                     CHUNK = 28
00497                     top = 0
00498                     # invariant: |value| = (top + f) * 2**e exactly
00499                     while f:
00500                         f = math.ldexp(f, CHUNK)
00501                         digit = int(f)
00502                         assert digit >> CHUNK == 0
00503                         top = (top << CHUNK) | digit
00504                         f -= digit
00505                         assert 0.0 <= f < 1.0
00506                         e -= CHUNK
00507 
00508                     # Now |value| = top * 2**e exactly.
00509                     if e >= 0:
00510                         n = top << e
00511                         d = 1
00512                     else:
00513                         n = top
00514                         d = 1 << -e
00515                     if value < 0:
00516                         n = -n
00517                     self.n = n
00518                     self.d = d
00519                     assert float(n) / float(d) == value
00520                 else:
00521                     raise TypeError("can't deal with %r" % value)
00522 
00523             def _cmp__(self, other):
00524                 if not isinstance(other, Rat):
00525                     other = Rat(other)
00526                 x, y = self.n * other.d, self.d * other.n
00527                 return (x > y) - (x < y)
00528             def __eq__(self, other):
00529                 return self._cmp__(other) == 0
00530             def __ne__(self, other):
00531                 return self._cmp__(other) != 0
00532             def __ge__(self, other):
00533                 return self._cmp__(other) >= 0
00534             def __gt__(self, other):
00535                 return self._cmp__(other) > 0
00536             def __le__(self, other):
00537                 return self._cmp__(other) <= 0
00538             def __lt__(self, other):
00539                 return self._cmp__(other) < 0
00540 
00541         cases = [0, 0.001, 0.99, 1.0, 1.5, 1e20, 1e200]
00542         # 2**48 is an important boundary in the internals.  2**53 is an
00543         # important boundary for IEEE double precision.
00544         for t in 2.0**48, 2.0**50, 2.0**53:
00545             cases.extend([t - 1.0, t - 0.3, t, t + 0.3, t + 1.0,
00546                           int(t-1), int(t), int(t+1)])
00547         cases.extend([0, 1, 2, sys.maxsize, float(sys.maxsize)])
00548         # 1 << 20000 should exceed all double formats.  int(1e200) is to
00549         # check that we get equality with 1e200 above.
00550         t = int(1e200)
00551         cases.extend([0, 1, 2, 1 << 20000, t-1, t, t+1])
00552         cases.extend([-x for x in cases])
00553         for x in cases:
00554             Rx = Rat(x)
00555             for y in cases:
00556                 Ry = Rat(y)
00557                 Rcmp = (Rx > Ry) - (Rx < Ry)
00558                 xycmp = (x > y) - (x < y)
00559                 eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
00560                 eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
00561                 eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
00562                 eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp))
00563                 eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp))
00564                 eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
00565                 eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))

Here is the call graph for this function:

Definition at line 632 of file test_long.py.

00632 
00633     def test_nan_inf(self):
00634         self.assertRaises(OverflowError, int, float('inf'))
00635         self.assertRaises(OverflowError, int, float('-inf'))
00636         self.assertRaises(ValueError, int, float('nan'))

Here is the call graph for this function:

Definition at line 845 of file test_long.py.

00845 
00846     def test_round(self):
00847         # check round-half-even algorithm. For round to nearest ten;
00848         # rounding map is invariant under adding multiples of 20
00849         test_dict = {0:0, 1:0, 2:0, 3:0, 4:0, 5:0,
00850                      6:10, 7:10, 8:10, 9:10, 10:10, 11:10, 12:10, 13:10, 14:10,
00851                      15:20, 16:20, 17:20, 18:20, 19:20}
00852         for offset in range(-520, 520, 20):
00853             for k, v in test_dict.items():
00854                 got = round(k+offset, -1)
00855                 expected = v+offset
00856                 self.assertEqual(got, expected)
00857                 self.assertTrue(type(got) is int)
00858 
00859         # larger second argument
00860         self.assertEqual(round(-150, -2), -200)
00861         self.assertEqual(round(-149, -2), -100)
00862         self.assertEqual(round(-51, -2), -100)
00863         self.assertEqual(round(-50, -2), 0)
00864         self.assertEqual(round(-49, -2), 0)
00865         self.assertEqual(round(-1, -2), 0)
00866         self.assertEqual(round(0, -2), 0)
00867         self.assertEqual(round(1, -2), 0)
00868         self.assertEqual(round(49, -2), 0)
00869         self.assertEqual(round(50, -2), 0)
00870         self.assertEqual(round(51, -2), 100)
00871         self.assertEqual(round(149, -2), 100)
00872         self.assertEqual(round(150, -2), 200)
00873         self.assertEqual(round(250, -2), 200)
00874         self.assertEqual(round(251, -2), 300)
00875         self.assertEqual(round(172500, -3), 172000)
00876         self.assertEqual(round(173500, -3), 174000)
00877         self.assertEqual(round(31415926535, -1), 31415926540)
00878         self.assertEqual(round(31415926535, -2), 31415926500)
00879         self.assertEqual(round(31415926535, -3), 31415927000)
00880         self.assertEqual(round(31415926535, -4), 31415930000)
00881         self.assertEqual(round(31415926535, -5), 31415900000)
00882         self.assertEqual(round(31415926535, -6), 31416000000)
00883         self.assertEqual(round(31415926535, -7), 31420000000)
00884         self.assertEqual(round(31415926535, -8), 31400000000)
00885         self.assertEqual(round(31415926535, -9), 31000000000)
00886         self.assertEqual(round(31415926535, -10), 30000000000)
00887         self.assertEqual(round(31415926535, -11), 0)
00888         self.assertEqual(round(31415926535, -12), 0)
00889         self.assertEqual(round(31415926535, -999), 0)
00890 
00891         # should get correct results even for huge inputs
00892         for k in range(10, 100):
00893             got = round(10**k + 324678, -3)
00894             expect = 10**k + 325000
00895             self.assertEqual(got, expect)
00896             self.assertTrue(type(got) is int)
00897 
00898         # nonnegative second argument: round(x, n) should just return x
00899         for n in range(5):
00900             for i in range(100):
00901                 x = random.randrange(-10000, 10000)
00902                 got = round(x, n)
00903                 self.assertEqual(got, x)
00904                 self.assertTrue(type(got) is int)
00905         for huge_n in 2**31-1, 2**31, 2**63-1, 2**63, 2**100, 10**100:
00906             self.assertEqual(round(8979323, huge_n), 8979323)
00907 
00908         # omitted second argument
00909         for i in range(100):
00910             x = random.randrange(-10000, 10000)
00911             got = round(x)
00912             self.assertEqual(got, x)
00913             self.assertTrue(type(got) is int)
00914 
00915         # bad second argument
00916         bad_exponents = ('brian', 2.0, 0j, None)
00917         for e in bad_exponents:
00918             self.assertRaises(TypeError, round, 3, e)

Here is the call graph for this function:

Definition at line 793 of file test_long.py.

00793 
00794     def test_small_ints(self):
00795         for i in range(-5, 257):
00796             self.assertTrue(i is i + 0)
00797             self.assertTrue(i is i * 1)
00798             self.assertTrue(i is i - 0)
00799             self.assertTrue(i is i // 1)
00800             self.assertTrue(i is i & -1)
00801             self.assertTrue(i is i | 0)
00802             self.assertTrue(i is i ^ 0)
00803             self.assertTrue(i is ~~i)
00804             self.assertTrue(i is i**1)
00805             self.assertTrue(i is int(str(i)))
00806             self.assertTrue(i is i<<2>>2, str(i))
00807         # corner cases
00808         i = 1 << 70
00809         self.assertTrue(i - i is 0)
00810         self.assertTrue(0 * i is 0)

Here is the call graph for this function:

Definition at line 919 of file test_long.py.

00919 
00920     def test_to_bytes(self):
00921         def check(tests, byteorder, signed=False):
00922             for test, expected in tests.items():
00923                 try:
00924                     self.assertEqual(
00925                         test.to_bytes(len(expected), byteorder, signed=signed),
00926                         expected)
00927                 except Exception as err:
00928                     raise AssertionError(
00929                         "failed to convert {0} with byteorder={1} and signed={2}"
00930                         .format(test, byteorder, signed)) from err
00931 
00932         # Convert integers to signed big-endian byte arrays.
00933         tests1 = {
00934             0: b'\x00',
00935             1: b'\x01',
00936             -1: b'\xff',
00937             -127: b'\x81',
00938             -128: b'\x80',
00939             -129: b'\xff\x7f',
00940             127: b'\x7f',
00941             129: b'\x00\x81',
00942             -255: b'\xff\x01',
00943             -256: b'\xff\x00',
00944             255: b'\x00\xff',
00945             256: b'\x01\x00',
00946             32767: b'\x7f\xff',
00947             -32768: b'\xff\x80\x00',
00948             65535: b'\x00\xff\xff',
00949             -65536: b'\xff\x00\x00',
00950             -8388608: b'\x80\x00\x00'
00951         }
00952         check(tests1, 'big', signed=True)
00953 
00954         # Convert integers to signed little-endian byte arrays.
00955         tests2 = {
00956             0: b'\x00',
00957             1: b'\x01',
00958             -1: b'\xff',
00959             -127: b'\x81',
00960             -128: b'\x80',
00961             -129: b'\x7f\xff',
00962             127: b'\x7f',
00963             129: b'\x81\x00',
00964             -255: b'\x01\xff',
00965             -256: b'\x00\xff',
00966             255: b'\xff\x00',
00967             256: b'\x00\x01',
00968             32767: b'\xff\x7f',
00969             -32768: b'\x00\x80',
00970             65535: b'\xff\xff\x00',
00971             -65536: b'\x00\x00\xff',
00972             -8388608: b'\x00\x00\x80'
00973         }
00974         check(tests2, 'little', signed=True)
00975 
00976         # Convert integers to unsigned big-endian byte arrays.
00977         tests3 = {
00978             0: b'\x00',
00979             1: b'\x01',
00980             127: b'\x7f',
00981             128: b'\x80',
00982             255: b'\xff',
00983             256: b'\x01\x00',
00984             32767: b'\x7f\xff',
00985             32768: b'\x80\x00',
00986             65535: b'\xff\xff',
00987             65536: b'\x01\x00\x00'
00988         }
00989         check(tests3, 'big', signed=False)
00990 
00991         # Convert integers to unsigned little-endian byte arrays.
00992         tests4 = {
00993             0: b'\x00',
00994             1: b'\x01',
00995             127: b'\x7f',
00996             128: b'\x80',
00997             255: b'\xff',
00998             256: b'\x00\x01',
00999             32767: b'\xff\x7f',
01000             32768: b'\x00\x80',
01001             65535: b'\xff\xff',
01002             65536: b'\x00\x00\x01'
01003         }
01004         check(tests4, 'little', signed=False)
01005 
01006         self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=False)
01007         self.assertRaises(OverflowError, (256).to_bytes, 1, 'big', signed=True)
01008         self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=False)
01009         self.assertRaises(OverflowError, (256).to_bytes, 1, 'little', signed=True)
01010         self.assertRaises(OverflowError, (-1).to_bytes, 2, 'big', signed=False),
01011         self.assertRaises(OverflowError, (-1).to_bytes, 2, 'little', signed=False)
01012         self.assertEqual((0).to_bytes(0, 'big'), b'')
01013         self.assertEqual((1).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x01')
01014         self.assertEqual((0).to_bytes(5, 'big'), b'\x00\x00\x00\x00\x00')
01015         self.assertEqual((-1).to_bytes(5, 'big', signed=True),
01016                          b'\xff\xff\xff\xff\xff')
01017         self.assertRaises(OverflowError, (1).to_bytes, 0, 'big')

Here is the call graph for this function:

Definition at line 637 of file test_long.py.

00637 
00638     def test_true_division(self):
00639         huge = 1 << 40000
00640         mhuge = -huge
00641         self.assertEqual(huge / huge, 1.0)
00642         self.assertEqual(mhuge / mhuge, 1.0)
00643         self.assertEqual(huge / mhuge, -1.0)
00644         self.assertEqual(mhuge / huge, -1.0)
00645         self.assertEqual(1 / huge, 0.0)
00646         self.assertEqual(1 / huge, 0.0)
00647         self.assertEqual(1 / mhuge, 0.0)
00648         self.assertEqual(1 / mhuge, 0.0)
00649         self.assertEqual((666 * huge + (huge >> 1)) / huge, 666.5)
00650         self.assertEqual((666 * mhuge + (mhuge >> 1)) / mhuge, 666.5)
00651         self.assertEqual((666 * huge + (huge >> 1)) / mhuge, -666.5)
00652         self.assertEqual((666 * mhuge + (mhuge >> 1)) / huge, -666.5)
00653         self.assertEqual(huge / (huge << 1), 0.5)
00654         self.assertEqual((1000000 * huge) / huge, 1000000)
00655 
00656         namespace = {'huge': huge, 'mhuge': mhuge}
00657 
00658         for overflow in ["float(huge)", "float(mhuge)",
00659                          "huge / 1", "huge / 2", "huge / -1", "huge / -2",
00660                          "mhuge / 100", "mhuge / 200"]:
00661             self.assertRaises(OverflowError, eval, overflow, namespace)
00662 
00663         for underflow in ["1 / huge", "2 / huge", "-1 / huge", "-2 / huge",
00664                          "100 / mhuge", "200 / mhuge"]:
00665             result = eval(underflow, namespace)
00666             self.assertEqual(result, 0.0,
00667                              "expected underflow to 0 from %r" % underflow)
00668 
00669         for zero in ["huge / 0", "mhuge / 0"]:
00670             self.assertRaises(ZeroDivisionError, eval, zero, namespace)

Here is the call graph for this function:


Member Data Documentation

Definition at line 485 of file test_long.py.

Definition at line 484 of file test_long.py.


The documentation for this class was generated from the following file: