Port test_long.py to unittest.

This commit is contained in:
Walter Dörwald 2005-06-13 21:44:48 +00:00
parent f2ca5af439
commit a00215983b

View file

@ -1,6 +1,16 @@
from test.test_support import verify, verbose, TestFailed, fcmp import unittest
from string import join from test import test_support
from random import random, randint
import random
# Used for lazy formatting of failure messages
class Frm(object):
def __init__(self, format, *args):
self.format = format
self.args = args
def __str__(self):
return self.format % self.args
# SHIFT should match the value in longintrepr.h for best testing. # SHIFT should match the value in longintrepr.h for best testing.
SHIFT = 15 SHIFT = 15
@ -26,86 +36,72 @@ del p2
special = special + map(lambda x: ~x, special) + \ special = special + map(lambda x: ~x, special) + \
map(lambda x: -x, special) map(lambda x: -x, special)
# ------------------------------------------------------------ utilities
# Use check instead of assert so the test still does something class LongTest(unittest.TestCase):
# under -O.
def check(ok, *args): # Get quasi-random long consisting of ndigits digits (in base BASE).
if not ok: # quasi == the most-significant digit will not be 0, and the number
raise TestFailed, join(map(str, args), " ") # is constructed to contain long strings of 0 and 1 bits. These are
# more likely than random bits to provoke digit-boundary errors.
# The sign of the number is also random.
# Get quasi-random long consisting of ndigits digits (in base BASE). def getran(self, ndigits):
# quasi == the most-significant digit will not be 0, and the number self.assert_(ndigits > 0)
# is constructed to contain long strings of 0 and 1 bits. These are
# more likely than random bits to provoke digit-boundary errors.
# The sign of the number is also random.
def getran(ndigits):
verify(ndigits > 0)
nbits_hi = ndigits * SHIFT nbits_hi = ndigits * SHIFT
nbits_lo = nbits_hi - SHIFT + 1 nbits_lo = nbits_hi - SHIFT + 1
answer = 0L answer = 0L
nbits = 0 nbits = 0
r = int(random() * (SHIFT * 2)) | 1 # force 1 bits to start r = int(random.random() * (SHIFT * 2)) | 1 # force 1 bits to start
while nbits < nbits_lo: while nbits < nbits_lo:
bits = (r >> 1) + 1 bits = (r >> 1) + 1
bits = min(bits, nbits_hi - nbits) bits = min(bits, nbits_hi - nbits)
verify(1 <= bits <= SHIFT) self.assert_(1 <= bits <= SHIFT)
nbits = nbits + bits nbits = nbits + bits
answer = answer << bits answer = answer << bits
if r & 1: if r & 1:
answer = answer | ((1 << bits) - 1) answer = answer | ((1 << bits) - 1)
r = int(random() * (SHIFT * 2)) r = int(random.random() * (SHIFT * 2))
verify(nbits_lo <= nbits <= nbits_hi) self.assert_(nbits_lo <= nbits <= nbits_hi)
if random() < 0.5: if random.random() < 0.5:
answer = -answer answer = -answer
return answer return answer
# Get random long consisting of ndigits random digits (relative to base # Get random long consisting of ndigits random digits (relative to base
# BASE). The sign bit is also random. # BASE). The sign bit is also random.
def getran2(ndigits): def getran2(ndigits):
answer = 0L answer = 0L
for i in range(ndigits): for i in xrange(ndigits):
answer = (answer << SHIFT) | randint(0, MASK) answer = (answer << SHIFT) | random.randint(0, MASK)
if random() < 0.5: if random.random() < 0.5:
answer = -answer answer = -answer
return answer return answer
# --------------------------------------------------------------- divmod def check_division(self, x, y):
eq = self.assertEqual
def test_division_2(x, y):
q, r = divmod(x, y) q, r = divmod(x, y)
q2, r2 = x//y, x%y q2, r2 = x//y, x%y
pab, pba = x*y, y*x pab, pba = x*y, y*x
check(pab == pba, "multiplication does not commute for", x, y) eq(pab, pba, Frm("multiplication does not commute for %r and %r", x, y))
check(q == q2, "divmod returns different quotient than / for", x, y) eq(q, q2, Frm("divmod returns different quotient than / for %r and %r", x, y))
check(r == r2, "divmod returns different mod than % for", x, y) eq(r, r2, Frm("divmod returns different mod than %% for %r and %r", x, y))
check(x == q*y + r, "x != q*y + r after divmod on", x, y) eq(x, q*y + r, Frm("x != q*y + r after divmod on x=%r, y=%r", x, y))
if y > 0: if y > 0:
check(0 <= r < y, "bad mod from divmod on", x, y) self.assert_(0 <= r < y, Frm("bad mod from divmod on %r and %r", x, y))
else: else:
check(y < r <= 0, "bad mod from divmod on", x, y) self.assert_(y < r <= 0, Frm("bad mod from divmod on %r and %r", x, y))
def test_division(maxdigits=MAXDIGITS): def test_division(self):
if verbose: digits = range(1, MAXDIGITS+1) + range(KARATSUBA_CUTOFF,
print "long / * % divmod"
digits = range(1, maxdigits+1) + range(KARATSUBA_CUTOFF,
KARATSUBA_CUTOFF + 14) KARATSUBA_CUTOFF + 14)
digits.append(KARATSUBA_CUTOFF * 3) digits.append(KARATSUBA_CUTOFF * 3)
for lenx in digits: for lenx in digits:
x = getran(lenx) x = self.getran(lenx)
for leny in digits: for leny in digits:
y = getran(leny) or 1L y = self.getran(leny) or 1L
test_division_2(x, y) self.check_division(x, y)
# ------------------------------------------------------------ karatsuba
def test_karatsuba():
if verbose:
print "Karatsuba"
def test_karatsuba(self):
digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10) digits = range(1, 5) + range(KARATSUBA_CUTOFF, KARATSUBA_CUTOFF + 10)
digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100]) digits.extend([KARATSUBA_CUTOFF * 10, KARATSUBA_CUTOFF * 100])
@ -124,76 +120,80 @@ def test_karatsuba():
(1L << abits) - (1L << abits) -
(1L << bbits) + (1L << bbits) +
1) 1)
check(x == y, "bad result for", a, "*", b, x, y) self.assertEqual(x, y,
# -------------------------------------------------------------- ~ & | ^ Frm("bad result for a*b: a=%r, b=%r, x=%r, y=%r", a, b, x, y))
def test_bitop_identities_1(x): def check_bitop_identities_1(self, x):
check(x & 0 == 0, "x & 0 != 0 for", x) eq = self.assertEqual
check(x | 0 == x, "x | 0 != x for", x) eq(x & 0, 0, Frm("x & 0 != 0 for x=%r", x))
check(x ^ 0 == x, "x ^ 0 != x for", x) eq(x | 0, x, Frm("x | 0 != x for x=%r", x))
check(x & -1 == x, "x & -1 != x for", x) eq(x ^ 0, x, Frm("x ^ 0 != x for x=%r", x))
check(x | -1 == -1, "x | -1 != -1 for", x) eq(x & -1, x, Frm("x & -1 != x for x=%r", x))
check(x ^ -1 == ~x, "x ^ -1 != ~x for", x) eq(x | -1, -1, Frm("x | -1 != -1 for x=%r", x))
check(x == ~~x, "x != ~~x for", x) eq(x ^ -1, ~x, Frm("x ^ -1 != ~x for x=%r", x))
check(x & x == x, "x & x != x for", x) eq(x, ~~x, Frm("x != ~~x for x=%r", x))
check(x | x == x, "x | x != x for", x) eq(x & x, x, Frm("x & x != x for x=%r", x))
check(x ^ x == 0, "x ^ x != 0 for", x) eq(x | x, x, Frm("x | x != x for x=%r", x))
check(x & ~x == 0, "x & ~x != 0 for", x) eq(x ^ x, 0, Frm("x ^ x != 0 for x=%r", x))
check(x | ~x == -1, "x | ~x != -1 for", x) eq(x & ~x, 0, Frm("x & ~x != 0 for x=%r", x))
check(x ^ ~x == -1, "x ^ ~x != -1 for", x) eq(x | ~x, -1, Frm("x | ~x != -1 for x=%r", x))
check(-x == 1 + ~x == ~(x-1), "not -x == 1 + ~x == ~(x-1) for", x) eq(x ^ ~x, -1, Frm("x ^ ~x != -1 for x=%r", x))
for n in range(2*SHIFT): eq(-x, 1 + ~x, Frm("not -x == 1 + ~x for x=%r", x))
eq(-x, ~(x-1), Frm("not -x == ~(x-1) forx =%r", x))
for n in xrange(2*SHIFT):
p2 = 2L ** n p2 = 2L ** n
check(x << n >> n == x, "x << n >> n != x for", x, n) eq(x << n >> n, x,
check(x // p2 == x >> n, "x // p2 != x >> n for x n p2", x, n, p2) Frm("x << n >> n != x for x=%r, n=%r", (x, n)))
check(x * p2 == x << n, "x * p2 != x << n for x n p2", x, n, p2) eq(x // p2, x >> n,
check(x & -p2 == x >> n << n == x & ~(p2 - 1), Frm("x // p2 != x >> n for x=%r n=%r p2=%r", (x, n, p2)))
"not x & -p2 == x >> n << n == x & ~(p2 - 1) for x n p2", eq(x * p2, x << n,
x, n, p2) Frm("x * p2 != x << n for x=%r n=%r p2=%r", (x, n, p2)))
eq(x & -p2, x >> n << n,
Frm("not x & -p2 == x >> n << n for x=%r n=%r p2=%r", (x, n, p2)))
eq(x & -p2, x & ~(p2 - 1),
Frm("not x & -p2 == x & ~(p2 - 1) for x=%r n=%r p2=%r", (x, n, p2)))
def test_bitop_identities_2(x, y): def check_bitop_identities_2(self, x, y):
check(x & y == y & x, "x & y != y & x for", x, y) eq = self.assertEqual
check(x | y == y | x, "x | y != y | x for", x, y) eq(x & y, y & x, Frm("x & y != y & x for x=%r, y=%r", (x, y)))
check(x ^ y == y ^ x, "x ^ y != y ^ x for", x, y) eq(x | y, y | x, Frm("x | y != y | x for x=%r, y=%r", (x, y)))
check(x ^ y ^ x == y, "x ^ y ^ x != y for", x, y) eq(x ^ y, y ^ x, Frm("x ^ y != y ^ x for x=%r, y=%r", (x, y)))
check(x & y == ~(~x | ~y), "x & y != ~(~x | ~y) for", x, y) eq(x ^ y ^ x, y, Frm("x ^ y ^ x != y for x=%r, y=%r", (x, y)))
check(x | y == ~(~x & ~y), "x | y != ~(~x & ~y) for", x, y) eq(x & y, ~(~x | ~y), Frm("x & y != ~(~x | ~y) for x=%r, y=%r", (x, y)))
check(x ^ y == (x | y) & ~(x & y), eq(x | y, ~(~x & ~y), Frm("x | y != ~(~x & ~y) for x=%r, y=%r", (x, y)))
"x ^ y != (x | y) & ~(x & y) for", x, y) eq(x ^ y, (x | y) & ~(x & y),
check(x ^ y == (x & ~y) | (~x & y), Frm("x ^ y != (x | y) & ~(x & y) for x=%r, y=%r", (x, y)))
"x ^ y == (x & ~y) | (~x & y) for", x, y) eq(x ^ y, (x & ~y) | (~x & y),
check(x ^ y == (x | y) & (~x | ~y), Frm("x ^ y == (x & ~y) | (~x & y) for x=%r, y=%r", (x, y)))
"x ^ y == (x | y) & (~x | ~y) for", x, y) eq(x ^ y, (x | y) & (~x | ~y),
Frm("x ^ y == (x | y) & (~x | ~y) for x=%r, y=%r", (x, y)))
def test_bitop_identities_3(x, y, z): def check_bitop_identities_3(self, x, y, z):
check((x & y) & z == x & (y & z), eq = self.assertEqual
"(x & y) & z != x & (y & z) for", x, y, z) eq((x & y) & z, x & (y & z),
check((x | y) | z == x | (y | z), Frm("(x & y) & z != x & (y & z) for x=%r, y=%r, z=%r", (x, y, z)))
"(x | y) | z != x | (y | z) for", x, y, z) eq((x | y) | z, x | (y | z),
check((x ^ y) ^ z == x ^ (y ^ z), Frm("(x | y) | z != x | (y | z) for x=%r, y=%r, z=%r", (x, y, z)))
"(x ^ y) ^ z != x ^ (y ^ z) for", x, y, z) eq((x ^ y) ^ z, x ^ (y ^ z),
check(x & (y | z) == (x & y) | (x & z), Frm("(x ^ y) ^ z != x ^ (y ^ z) for x=%r, y=%r, z=%r", (x, y, z)))
"x & (y | z) != (x & y) | (x & z) for", x, y, z) eq(x & (y | z), (x & y) | (x & z),
check(x | (y & z) == (x | y) & (x | z), Frm("x & (y | z) != (x & y) | (x & z) for x=%r, y=%r, z=%r", (x, y, z)))
"x | (y & z) != (x | y) & (x | z) for", x, y, z) eq(x | (y & z), (x | y) & (x | z),
Frm("x | (y & z) != (x | y) & (x | z) for x=%r, y=%r, z=%r", (x, y, z)))
def test_bitop_identities(maxdigits=MAXDIGITS): def test_bitop_identities(self):
if verbose:
print "long bit-operation identities"
for x in special: for x in special:
test_bitop_identities_1(x) self.check_bitop_identities_1(x)
digits = range(1, maxdigits+1) digits = xrange(1, MAXDIGITS+1)
for lenx in digits: for lenx in digits:
x = getran(lenx) x = self.getran(lenx)
test_bitop_identities_1(x) self.check_bitop_identities_1(x)
for leny in digits: for leny in digits:
y = getran(leny) y = self.getran(leny)
test_bitop_identities_2(x, y) self.check_bitop_identities_2(x, y)
test_bitop_identities_3(x, y, getran((lenx + leny)//2)) self.check_bitop_identities_3(x, y, self.getran((lenx + leny)//2))
# ------------------------------------------------- hex oct repr str atol def slow_format(self, x, base):
def slow_format(x, base):
if (x, base) == (0, 8): if (x, base) == (0, 8):
# this is an oddball! # this is an oddball!
return "0L" return "0L"
@ -208,39 +208,33 @@ def slow_format(x, base):
digits = digits or [0] digits = digits or [0]
return '-'[:sign] + \ return '-'[:sign] + \
{8: '0', 10: '', 16: '0x'}[base] + \ {8: '0', 10: '', 16: '0x'}[base] + \
join(map(lambda i: "0123456789ABCDEF"[i], digits), '') + \ "".join(map(lambda i: "0123456789ABCDEF"[i], digits)) + "L"
"L"
def test_format_1(x): def check_format_1(self, x):
from string import atol
for base, mapper in (8, oct), (10, repr), (16, hex): for base, mapper in (8, oct), (10, repr), (16, hex):
got = mapper(x) got = mapper(x)
expected = slow_format(x, base) expected = self.slow_format(x, base)
check(got == expected, mapper.__name__, "returned", msg = Frm("%s returned %r but expected %r for %r",
got, "but expected", expected, "for", x) mapper.__name__, got, expected, x)
check(atol(got, 0) == x, 'atol("%s", 0) !=' % got, x) self.assertEqual(got, expected, msg)
self.assertEqual(long(got, 0), x, Frm('long("%s", 0) != %r', got, x))
# str() has to be checked a little differently since there's no # str() has to be checked a little differently since there's no
# trailing "L" # trailing "L"
got = str(x) got = str(x)
expected = slow_format(x, 10)[:-1] expected = self.slow_format(x, 10)[:-1]
check(got == expected, mapper.__name__, "returned", msg = Frm("%s returned %r but expected %r for %r",
got, "but expected", expected, "for", x) mapper.__name__, got, expected, x)
self.assertEqual(got, expected, msg)
def test_format(maxdigits=MAXDIGITS): def test_format(self):
if verbose:
print "long str/hex/oct/atol"
for x in special: for x in special:
test_format_1(x) self.check_format_1(x)
for i in range(10): for i in xrange(10):
for lenx in range(1, maxdigits+1): for lenx in xrange(1, MAXDIGITS+1):
x = getran(lenx) x = self.getran(lenx)
test_format_1(x) self.check_format_1(x)
# ----------------------------------------------------------------- misc def test_misc(self):
def test_misc(maxdigits=MAXDIGITS):
if verbose:
print "long miscellaneous operations"
import sys import sys
# check the extremes in int<->long conversion # check the extremes in int<->long conversion
@ -248,53 +242,51 @@ def test_misc(maxdigits=MAXDIGITS):
hugeneg = -hugepos - 1 hugeneg = -hugepos - 1
hugepos_aslong = long(hugepos) hugepos_aslong = long(hugepos)
hugeneg_aslong = long(hugeneg) hugeneg_aslong = long(hugeneg)
check(hugepos == hugepos_aslong, "long(sys.maxint) != sys.maxint") self.assertEqual(hugepos, hugepos_aslong, "long(sys.maxint) != sys.maxint")
check(hugeneg == hugeneg_aslong, self.assertEqual(hugeneg, hugeneg_aslong,
"long(-sys.maxint-1) != -sys.maxint-1") "long(-sys.maxint-1) != -sys.maxint-1")
# long -> int should not fail for hugepos_aslong or hugeneg_aslong # long -> int should not fail for hugepos_aslong or hugeneg_aslong
try: try:
check(int(hugepos_aslong) == hugepos, self.assertEqual(int(hugepos_aslong), hugepos,
"converting sys.maxint to long and back to int fails") "converting sys.maxint to long and back to int fails")
except OverflowError: except OverflowError:
raise TestFailed, "int(long(sys.maxint)) overflowed!" self.fail("int(long(sys.maxint)) overflowed!")
try: try:
check(int(hugeneg_aslong) == hugeneg, self.assertEqual(int(hugeneg_aslong), hugeneg,
"converting -sys.maxint-1 to long and back to int fails") "converting -sys.maxint-1 to long and back to int fails")
except OverflowError: except OverflowError:
raise TestFailed, "int(long(-sys.maxint-1)) overflowed!" self.fail("int(long(-sys.maxint-1)) overflowed!")
# but long -> int should overflow for hugepos+1 and hugeneg-1 # but long -> int should overflow for hugepos+1 and hugeneg-1
x = hugepos_aslong + 1 x = hugepos_aslong + 1
try: try:
y = int(x) y = int(x)
except OverflowError: except OverflowError:
raise TestFailed, "int(long(sys.maxint) + 1) mustn't overflow" self.fail("int(long(sys.maxint) + 1) mustn't overflow")
if not isinstance(y, long): self.assert_(isinstance(y, long),
raise TestFailed("int(long(sys.maxint) + 1) should have returned long") "int(long(sys.maxint) + 1) should have returned long")
x = hugeneg_aslong - 1 x = hugeneg_aslong - 1
try: try:
y = int(x) y = int(x)
except OverflowError: except OverflowError:
raise TestFailed, "int(long(-sys.maxint-1) - 1) mustn't overflow" self.fail("int(long(-sys.maxint-1) - 1) mustn't overflow")
if not isinstance(y, long): self.assert_(isinstance(y, long),
raise TestFailed("int(long(-sys.maxint-1) - 1) should have returned long") "int(long(-sys.maxint-1) - 1) should have returned long")
class long2(long): class long2(long):
pass pass
x = long2(1L<<100) x = long2(1L<<100)
y = int(x) y = int(x)
if type(y) is not long: self.assert_(type(y) is long,
raise TestFailed("overflowing int conversion must return long not long subtype") "overflowing int conversion must return long not long subtype")
# ----------------------------------- tests of auto int->long conversion # ----------------------------------- tests of auto int->long conversion
def test_auto_overflow(): def test_auto_overflow(self):
import math, sys import math, sys
if verbose:
print "auto-convert int->long on overflow"
special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1] special = [0, 1, 2, 3, sys.maxint-1, sys.maxint, sys.maxint+1]
sqrt = int(math.sqrt(sys.maxint)) sqrt = int(math.sqrt(sys.maxint))
special.extend([sqrt-1, sqrt, sqrt+1]) special.extend([sqrt-1, sqrt, sqrt+1])
@ -302,8 +294,8 @@ def test_auto_overflow():
def checkit(*args): def checkit(*args):
# Heavy use of nested scopes here! # Heavy use of nested scopes here!
verify(got == expected, "for %r expected %r got %r" % self.assertEqual(got, expected,
(args, expected, got)) Frm("for %r expected %r got %r", args, expected, got))
for x in special: for x in special:
longx = long(x) longx = long(x)
@ -352,24 +344,13 @@ def test_auto_overflow():
got = pow(x, y, z) got = pow(x, y, z)
checkit('pow', x, y, '%', z) checkit('pow', x, y, '%', z)
else: else:
try: self.assertRaises(TypeError, pow,longx, longy, long(z))
pow(longx, longy, long(z))
except TypeError:
pass
else:
raise TestFailed("pow%r should have raised "
"TypeError" % ((longx, longy, long(z)),))
# ---------------------------------------- tests of long->float overflow def test_float_overflow(self):
def test_float_overflow():
import math import math
if verbose:
print "long->float overflow"
for x in -2.0, -1.0, 0.0, 1.0, 2.0: for x in -2.0, -1.0, 0.0, 1.0, 2.0:
verify(float(long(x)) == x) self.assertEqual(float(long(x)), x)
shuge = '12345' * 120 shuge = '12345' * 120
huge = 1L << 30000 huge = 1L << 30000
@ -389,61 +370,38 @@ def test_float_overflow():
"math.sqrt(huge)", "math.sqrt(mhuge)", # should do better "math.sqrt(huge)", "math.sqrt(mhuge)", # should do better
"math.floor(huge)", "math.floor(mhuge)"]: "math.floor(huge)", "math.floor(mhuge)"]:
try: self.assertRaises(OverflowError, eval, test, namespace)
eval(test, namespace)
except OverflowError:
pass
else:
raise TestFailed("expected OverflowError from %s" % test)
# XXX Perhaps float(shuge) can raise OverflowError on some box? # XXX Perhaps float(shuge) can raise OverflowError on some box?
# The comparison should not. # The comparison should not.
if float(shuge) == int(shuge): self.assertNotEqual(float(shuge), int(shuge),
raise TestFailed("float(shuge) should not equal int(shuge)") "float(shuge) should not equal int(shuge)")
# ---------------------------------------------- test huge log and log10 def test_logs(self):
def test_logs():
import math import math
if verbose:
print "log and log10"
LOG10E = math.log10(math.e) LOG10E = math.log10(math.e)
for exp in range(10) + [100, 1000, 10000]: for exp in range(10) + [100, 1000, 10000]:
value = 10 ** exp value = 10 ** exp
log10 = math.log10(value) log10 = math.log10(value)
verify(fcmp(log10, exp) == 0) self.assertAlmostEqual(log10, exp)
# log10(value) == exp, so log(value) == log10(value)/log10(e) == # log10(value) == exp, so log(value) == log10(value)/log10(e) ==
# exp/LOG10E # exp/LOG10E
expected = exp / LOG10E expected = exp / LOG10E
log = math.log(value) log = math.log(value)
verify(fcmp(log, expected) == 0) self.assertAlmostEqual(log, expected)
for bad in -(1L << 10000), -2L, 0L: for bad in -(1L << 10000), -2L, 0L:
try: self.assertRaises(ValueError, math.log, bad)
math.log(bad) self.assertRaises(ValueError, math.log10, bad)
raise TestFailed("expected ValueError from log(<= 0)")
except ValueError:
pass
try: def test_mixed_compares(self):
math.log10(bad) eq = self.assertEqual
raise TestFailed("expected ValueError from log10(<= 0)")
except ValueError:
pass
# ----------------------------------------------- test mixed comparisons
def test_mixed_compares():
import math import math
import sys import sys
if verbose:
print "mixed comparisons"
# We're mostly concerned with that mixing floats and longs does the # We're mostly concerned with that mixing floats and longs does the
# right stuff, even when longs are too large to fit in a float. # right stuff, even when longs are too large to fit in a float.
# The safest way to check the results is to use an entirely different # The safest way to check the results is to use an entirely different
@ -454,7 +412,6 @@ def test_mixed_compares():
if isinstance(value, (int, long)): if isinstance(value, (int, long)):
self.n = value self.n = value
self.d = 1 self.d = 1
elif isinstance(value, float): elif isinstance(value, float):
# Convert to exact rational equivalent. # Convert to exact rational equivalent.
f, e = math.frexp(abs(value)) f, e = math.frexp(abs(value))
@ -488,7 +445,6 @@ def test_mixed_compares():
self.n = n self.n = n
self.d = d self.d = d
assert float(n) / float(d) == value assert float(n) / float(d) == value
else: else:
raise TypeError("can't deal with %r" % val) raise TypeError("can't deal with %r" % val)
@ -515,29 +471,16 @@ def test_mixed_compares():
Ry = Rat(y) Ry = Rat(y)
Rcmp = cmp(Rx, Ry) Rcmp = cmp(Rx, Ry)
xycmp = cmp(x, y) xycmp = cmp(x, y)
if Rcmp != xycmp: eq(Rcmp, xycmp, Frm("%r %r %d %d", x, y, Rcmp, xycmp))
raise TestFailed('%r %r %d %d' % (x, y, Rcmp, xycmp)) eq(x == y, Rcmp == 0, Frm("%r == %r %d", x, y, Rcmp))
if (x == y) != (Rcmp == 0): eq(x != y, Rcmp != 0, Frm("%r != %r %d", x, y, Rcmp))
raise TestFailed('%r == %r %d' % (x, y, Rcmp)) eq(x < y, Rcmp < 0, Frm("%r < %r %d", x, y, Rcmp))
if (x != y) != (Rcmp != 0): eq(x <= y, Rcmp <= 0, Frm("%r <= %r %d", x, y, Rcmp))
raise TestFailed('%r != %r %d' % (x, y, Rcmp)) eq(x > y, Rcmp > 0, Frm("%r > %r %d", x, y, Rcmp))
if (x < y) != (Rcmp < 0): eq(x >= y, Rcmp >= 0, Frm("%r >= %r %d", x, y, Rcmp))
raise TestFailed('%r < %r %d' % (x, y, Rcmp))
if (x <= y) != (Rcmp <= 0):
raise TestFailed('%r <= %r %d' % (x, y, Rcmp))
if (x > y) != (Rcmp > 0):
raise TestFailed('%r > %r %d' % (x, y, Rcmp))
if (x >= y) != (Rcmp >= 0):
raise TestFailed('%r >= %r %d' % (x, y, Rcmp))
# ---------------------------------------------------------------- do it def test_main():
test_support.run_unittest(LongTest)
test_division() if __name__ == "__main__":
test_karatsuba() test_main()
test_bitop_identities()
test_format()
test_misc()
test_auto_overflow()
test_float_overflow()
test_logs()
test_mixed_compares()