Back to index

python3.2  3.2.2
Public Member Functions
test.test_fractions.FractionTest Class Reference

List of all members.

Public Member Functions

def assertTypedEquals
def assertRaisesMessage
def testInit
def testInitFromFloat
def testInitFromDecimal
def testFromString
def testImmutable
def testFromFloat
def testFromDecimal
def testLimitDenominator
def testConversions
def testRound
def testArithmetic
def testMixedArithmetic
def testMixingWithDecimal
def testComparisons
def testComparisonsDummyRational
def testComparisonsDummyFloat
def testMixedLess
def testMixedLessEqual
def testBigFloatComparisons
def testBigComplexComparisons
def testMixedEqual
def testStringification
def testHash
def testApproximatePi
def testApproximateCos1
def test_copy_deepcopy_pickle
def test_slots

Detailed Description

Definition at line 98 of file test_fractions.py.


Member Function Documentation

def test.test_fractions.FractionTest.assertRaisesMessage (   self,
  exc_type,
  message,
  callable,
  args,
  kwargs 
)
Asserts that callable(*args, **kwargs) raises exc_type(message).

Definition at line 106 of file test_fractions.py.

00106 
00107                             callable, *args, **kwargs):
00108         """Asserts that callable(*args, **kwargs) raises exc_type(message)."""
00109         try:
00110             callable(*args, **kwargs)
00111         except exc_type as e:
00112             self.assertEqual(message, str(e))
00113         else:
00114             self.fail("%s not raised" % exc_type.__name__)

Here is the call graph for this function:

Here is the caller graph for this function:

def test.test_fractions.FractionTest.assertTypedEquals (   self,
  expected,
  actual 
)
Asserts that both the types and values are the same.

Definition at line 100 of file test_fractions.py.

00100 
00101     def assertTypedEquals(self, expected, actual):
00102         """Asserts that both the types and values are the same."""
00103         self.assertEqual(type(expected), type(actual))
00104         self.assertEqual(expected, actual)

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 575 of file test_fractions.py.

00575 
00576     def test_copy_deepcopy_pickle(self):
00577         r = F(13, 7)
00578         self.assertEqual(r, loads(dumps(r)))
00579         self.assertEqual(id(r), id(copy(r)))
00580         self.assertEqual(id(r), id(deepcopy(r)))

Here is the call graph for this function:

Definition at line 581 of file test_fractions.py.

00581 
00582     def test_slots(self):
00583         # Issue 4998
00584         r = F(13, 7)
00585         self.assertRaises(AttributeError, setattr, r, 'a', 10)

Here is the call graph for this function:

Definition at line 561 of file test_fractions.py.

00561 
00562     def testApproximateCos1(self):
00563         # Algorithm borrowed from
00564         # http://docs.python.org/lib/decimal-recipes.html
00565         x = F(1)
00566         i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1
00567         while abs(s - lasts) > F(1, 10**9):
00568             lasts = s
00569             i += 2
00570             fact *= i * (i-1)
00571             num *= x * x
00572             sign *= -1
00573             s += num / fact * sign
00574         self.assertAlmostEqual(math.cos(1), s)

Here is the call graph for this function:

Definition at line 548 of file test_fractions.py.

00548 
00549     def testApproximatePi(self):
00550         # Algorithm borrowed from
00551         # http://docs.python.org/lib/decimal-recipes.html
00552         three = F(3)
00553         lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
00554         while abs(s - lasts) > F(1, 10**9):
00555             lasts = s
00556             n, na = n+na, na+8
00557             d, da = d+da, da+32
00558             t = (t * n) / d
00559             s += t
00560         self.assertAlmostEqual(math.pi, s)

Here is the call graph for this function:

Definition at line 321 of file test_fractions.py.

00321 
00322     def testArithmetic(self):
00323         self.assertEqual(F(1, 2), F(1, 10) + F(2, 5))
00324         self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5))
00325         self.assertEqual(F(1, 25), F(1, 10) * F(2, 5))
00326         self.assertEqual(F(1, 4), F(1, 10) / F(2, 5))
00327         self.assertTypedEquals(2, F(9, 10) // F(2, 5))
00328         self.assertTypedEquals(10**23, F(10**23, 1) // F(1))
00329         self.assertEqual(F(2, 3), F(-7, 3) % F(3, 2))
00330         self.assertEqual(F(8, 27), F(2, 3) ** F(3))
00331         self.assertEqual(F(27, 8), F(2, 3) ** F(-3))
00332         self.assertTypedEquals(2.0, F(4) ** F(1, 2))
00333         z = pow(F(-1), F(1, 2))
00334         self.assertAlmostEqual(z.real, 0)
00335         self.assertEqual(z.imag, 1)

Here is the call graph for this function:

Definition at line 499 of file test_fractions.py.

00499 
00500     def testBigComplexComparisons(self):
00501         self.assertFalse(F(10**23) == complex(10**23))
00502         self.assertRaises(TypeError, operator.gt, F(10**23), complex(10**23))
00503         self.assertRaises(TypeError, operator.le, F(10**23), complex(10**23))
00504 
00505         x = F(3, 8)
00506         z = complex(0.375, 0.0)
00507         w = complex(0.375, 0.2)
00508         self.assertTrue(x == z)
00509         self.assertFalse(x != z)
00510         self.assertFalse(x == w)
00511         self.assertTrue(x != w)
00512         for op in operator.lt, operator.le, operator.gt, operator.ge:
00513             self.assertRaises(TypeError, op, x, z)
00514             self.assertRaises(TypeError, op, z, x)
00515             self.assertRaises(TypeError, op, x, w)
00516             self.assertRaises(TypeError, op, w, x)

Here is the call graph for this function:

Definition at line 489 of file test_fractions.py.

00489 
00490     def testBigFloatComparisons(self):
00491         # Because 10**23 can't be represented exactly as a float:
00492         self.assertFalse(F(10**23) == float(10**23))
00493         # The first test demonstrates why these are important.
00494         self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1)))
00495         self.assertTrue(1e23 < F(math.trunc(1e23) + 1))
00496         self.assertFalse(1e23 <= F(math.trunc(1e23) - 1))
00497         self.assertTrue(1e23 > F(math.trunc(1e23) - 1))
00498         self.assertFalse(1e23 >= F(math.trunc(1e23) + 1))

Here is the call graph for this function:

Definition at line 399 of file test_fractions.py.

00399 
00400     def testComparisons(self):
00401         self.assertTrue(F(1, 2) < F(2, 3))
00402         self.assertFalse(F(1, 2) < F(1, 2))
00403         self.assertTrue(F(1, 2) <= F(2, 3))
00404         self.assertTrue(F(1, 2) <= F(1, 2))
00405         self.assertFalse(F(2, 3) <= F(1, 2))
00406         self.assertTrue(F(1, 2) == F(1, 2))
00407         self.assertFalse(F(1, 2) == F(1, 3))
00408         self.assertFalse(F(1, 2) != F(1, 2))
00409         self.assertTrue(F(1, 2) != F(1, 3))

Here is the call graph for this function:

Definition at line 442 of file test_fractions.py.

00442 
00443     def testComparisonsDummyFloat(self):
00444         x = DummyFloat(1./3.)
00445         y = F(1, 3)
00446         self.assertTrue(x != y)
00447         self.assertTrue(x < y or x > y)
00448         self.assertFalse(x == y)
00449         self.assertFalse(x <= y and x >= y)
00450         self.assertTrue(y != x)
00451         self.assertTrue(y < x or y > x)
00452         self.assertFalse(y == x)
00453         self.assertFalse(y <= x and y >= x)

Here is the call graph for this function:

Definition at line 410 of file test_fractions.py.

00410 
00411     def testComparisonsDummyRational(self):
00412         self.assertTrue(F(1, 2) == DummyRational(1, 2))
00413         self.assertTrue(DummyRational(1, 2) == F(1, 2))
00414         self.assertFalse(F(1, 2) == DummyRational(3, 4))
00415         self.assertFalse(DummyRational(3, 4) == F(1, 2))
00416 
00417         self.assertTrue(F(1, 2) < DummyRational(3, 4))
00418         self.assertFalse(F(1, 2) < DummyRational(1, 2))
00419         self.assertFalse(F(1, 2) < DummyRational(1, 7))
00420         self.assertFalse(F(1, 2) > DummyRational(3, 4))
00421         self.assertFalse(F(1, 2) > DummyRational(1, 2))
00422         self.assertTrue(F(1, 2) > DummyRational(1, 7))
00423         self.assertTrue(F(1, 2) <= DummyRational(3, 4))
00424         self.assertTrue(F(1, 2) <= DummyRational(1, 2))
00425         self.assertFalse(F(1, 2) <= DummyRational(1, 7))
00426         self.assertFalse(F(1, 2) >= DummyRational(3, 4))
00427         self.assertTrue(F(1, 2) >= DummyRational(1, 2))
00428         self.assertTrue(F(1, 2) >= DummyRational(1, 7))
00429 
00430         self.assertTrue(DummyRational(1, 2) < F(3, 4))
00431         self.assertFalse(DummyRational(1, 2) < F(1, 2))
00432         self.assertFalse(DummyRational(1, 2) < F(1, 7))
00433         self.assertFalse(DummyRational(1, 2) > F(3, 4))
00434         self.assertFalse(DummyRational(1, 2) > F(1, 2))
00435         self.assertTrue(DummyRational(1, 2) > F(1, 7))
00436         self.assertTrue(DummyRational(1, 2) <= F(3, 4))
00437         self.assertTrue(DummyRational(1, 2) <= F(1, 2))
00438         self.assertFalse(DummyRational(1, 2) <= F(1, 7))
00439         self.assertFalse(DummyRational(1, 2) >= F(3, 4))
00440         self.assertTrue(DummyRational(1, 2) >= F(1, 2))
00441         self.assertTrue(DummyRational(1, 2) >= F(1, 7))

Here is the call graph for this function:

Definition at line 290 of file test_fractions.py.

00290 
00291     def testConversions(self):
00292         self.assertTypedEquals(-1, math.trunc(F(-11, 10)))
00293         self.assertTypedEquals(-2, math.floor(F(-11, 10)))
00294         self.assertTypedEquals(-1, math.ceil(F(-11, 10)))
00295         self.assertTypedEquals(-1, math.ceil(F(-10, 10)))
00296         self.assertTypedEquals(-1, int(F(-11, 10)))
00297         self.assertTypedEquals(0, round(F(-1, 10)))
00298         self.assertTypedEquals(0, round(F(-5, 10)))
00299         self.assertTypedEquals(-2, round(F(-15, 10)))
00300         self.assertTypedEquals(-1, round(F(-7, 10)))
00301 
00302         self.assertEqual(False, bool(F(0, 1)))
00303         self.assertEqual(True, bool(F(3, 2)))
00304         self.assertTypedEquals(0.1, float(F(1, 10)))
00305 
00306         # Check that __float__ isn't implemented by converting the
00307         # numerator and denominator to float before dividing.
00308         self.assertRaises(OverflowError, float, int('2'*400+'7'))
00309         self.assertAlmostEqual(2.0/3,
00310                                float(F(int('2'*400+'7'), int('3'*400+'1'))))
00311 
00312         self.assertTypedEquals(0.1+0j, complex(F(1,10)))

Here is the call graph for this function:

Definition at line 257 of file test_fractions.py.

00257 
00258     def testFromDecimal(self):
00259         self.assertRaises(TypeError, F.from_decimal, 3+4j)
00260         self.assertEqual(F(10, 1), F.from_decimal(10))
00261         self.assertEqual(F(0), F.from_decimal(Decimal("-0")))
00262         self.assertEqual(F(5, 10), F.from_decimal(Decimal("0.5")))
00263         self.assertEqual(F(5, 1000), F.from_decimal(Decimal("5e-3")))
00264         self.assertEqual(F(5000), F.from_decimal(Decimal("5e3")))
00265         self.assertEqual(1 - F(1, 10**30),
00266                          F.from_decimal(Decimal("0." + "9" * 30)))
00267 
00268         self.assertRaisesMessage(
00269             TypeError, "Cannot convert Infinity to Fraction.",
00270             F.from_decimal, Decimal("inf"))
00271         self.assertRaisesMessage(
00272             TypeError, "Cannot convert -Infinity to Fraction.",
00273             F.from_decimal, Decimal("-inf"))
00274         self.assertRaisesMessage(
00275             TypeError, "Cannot convert NaN to Fraction.",
00276             F.from_decimal, Decimal("nan"))
00277         self.assertRaisesMessage(
00278             TypeError, "Cannot convert sNaN to Fraction.",
00279             F.from_decimal, Decimal("snan"))

Here is the call graph for this function:

Definition at line 230 of file test_fractions.py.

00230 
00231     def testFromFloat(self):
00232         self.assertRaises(TypeError, F.from_float, 3+4j)
00233         self.assertEqual((10, 1), _components(F.from_float(10)))
00234         bigint = 1234567890123456789
00235         self.assertEqual((bigint, 1), _components(F.from_float(bigint)))
00236         self.assertEqual((0, 1), _components(F.from_float(-0.0)))
00237         self.assertEqual((10, 1), _components(F.from_float(10.0)))
00238         self.assertEqual((-5, 2), _components(F.from_float(-2.5)))
00239         self.assertEqual((99999999999999991611392, 1),
00240                          _components(F.from_float(1e23)))
00241         self.assertEqual(float(10**23), float(F.from_float(1e23)))
00242         self.assertEqual((3602879701896397, 1125899906842624),
00243                          _components(F.from_float(3.2)))
00244         self.assertEqual(3.2, float(F.from_float(3.2)))
00245 
00246         inf = 1e1000
00247         nan = inf - inf
00248         self.assertRaisesMessage(
00249             TypeError, "Cannot convert inf to Fraction.",
00250             F.from_float, inf)
00251         self.assertRaisesMessage(
00252             TypeError, "Cannot convert -inf to Fraction.",
00253             F.from_float, -inf)
00254         self.assertRaisesMessage(
00255             TypeError, "Cannot convert nan to Fraction.",
00256             F.from_float, nan)

Here is the call graph for this function:

Definition at line 161 of file test_fractions.py.

00161 
00162     def testFromString(self):
00163         self.assertEqual((5, 1), _components(F("5")))
00164         self.assertEqual((3, 2), _components(F("3/2")))
00165         self.assertEqual((3, 2), _components(F(" \n  +3/2")))
00166         self.assertEqual((-3, 2), _components(F("-3/2  ")))
00167         self.assertEqual((13, 2), _components(F("    013/02 \n  ")))
00168         self.assertEqual((16, 5), _components(F(" 3.2 ")))
00169         self.assertEqual((-16, 5), _components(F(" -3.2 ")))
00170         self.assertEqual((-3, 1), _components(F(" -3. ")))
00171         self.assertEqual((3, 5), _components(F(" .6 ")))
00172         self.assertEqual((1, 3125), _components(F("32.e-5")))
00173         self.assertEqual((1000000, 1), _components(F("1E+06")))
00174         self.assertEqual((-12300, 1), _components(F("-1.23e4")))
00175         self.assertEqual((0, 1), _components(F(" .0e+0\t")))
00176         self.assertEqual((0, 1), _components(F("-0.000e0")))
00177 
00178         self.assertRaisesMessage(
00179             ZeroDivisionError, "Fraction(3, 0)",
00180             F, "3/0")
00181         self.assertRaisesMessage(
00182             ValueError, "Invalid literal for Fraction: '3/'",
00183             F, "3/")
00184         self.assertRaisesMessage(
00185             ValueError, "Invalid literal for Fraction: '/2'",
00186             F, "/2")
00187         self.assertRaisesMessage(
00188             ValueError, "Invalid literal for Fraction: '3 /2'",
00189             F, "3 /2")
00190         self.assertRaisesMessage(
00191             # Denominators don't need a sign.
00192             ValueError, "Invalid literal for Fraction: '3/+2'",
00193             F, "3/+2")
00194         self.assertRaisesMessage(
00195             # Imitate float's parsing.
00196             ValueError, "Invalid literal for Fraction: '+ 3/2'",
00197             F, "+ 3/2")
00198         self.assertRaisesMessage(
00199             # Avoid treating '.' as a regex special character.
00200             ValueError, "Invalid literal for Fraction: '3a2'",
00201             F, "3a2")
00202         self.assertRaisesMessage(
00203             # Don't accept combinations of decimals and rationals.
00204             ValueError, "Invalid literal for Fraction: '3/7.2'",
00205             F, "3/7.2")
00206         self.assertRaisesMessage(
00207             # Don't accept combinations of decimals and rationals.
00208             ValueError, "Invalid literal for Fraction: '3.2/7'",
00209             F, "3.2/7")
00210         self.assertRaisesMessage(
00211             # Allow 3. and .3, but not .
00212             ValueError, "Invalid literal for Fraction: '.'",
00213             F, ".")

Here is the call graph for this function:

Definition at line 540 of file test_fractions.py.

00540 
00541     def testHash(self):
00542         self.assertEqual(hash(2.5), hash(F(5, 2)))
00543         self.assertEqual(hash(10**50), hash(F(10**50)))
00544         self.assertNotEqual(hash(float(10**23)), hash(F(10**23)))
00545         # Check that __hash__ produces the same value as hash(), for
00546         # consistency with int and Decimal.  (See issue #10356.)
00547         self.assertEqual(hash(F(-1)), F(-1).__hash__())

Here is the call graph for this function:

Definition at line 214 of file test_fractions.py.

00214 
00215     def testImmutable(self):
00216         r = F(7, 3)
00217         r.__init__(2, 15)
00218         self.assertEqual((7, 3), _components(r))
00219 
00220         self.assertRaises(AttributeError, setattr, r, 'numerator', 12)
00221         self.assertRaises(AttributeError, setattr, r, 'denominator', 6)
00222         self.assertEqual((7, 3), _components(r))
00223 
00224         # But if you _really_ need to:
00225         r._numerator = 4
00226         r._denominator = 2
00227         self.assertEqual((4, 2), _components(r))
00228         # Which breaks some important operations:
00229         self.assertNotEqual(F(4, 2), r)

Here is the call graph for this function:

Definition at line 115 of file test_fractions.py.

00115 
00116     def testInit(self):
00117         self.assertEqual((0, 1), _components(F()))
00118         self.assertEqual((7, 1), _components(F(7)))
00119         self.assertEqual((7, 3), _components(F(F(7, 3))))
00120 
00121         self.assertEqual((-1, 1), _components(F(-1, 1)))
00122         self.assertEqual((-1, 1), _components(F(1, -1)))
00123         self.assertEqual((1, 1), _components(F(-2, -2)))
00124         self.assertEqual((1, 2), _components(F(5, 10)))
00125         self.assertEqual((7, 15), _components(F(7, 15)))
00126         self.assertEqual((10**23, 1), _components(F(10**23)))
00127 
00128         self.assertEqual((3, 77), _components(F(F(3, 7), 11)))
00129         self.assertEqual((-9, 5), _components(F(2, F(-10, 9))))
00130         self.assertEqual((2486, 2485), _components(F(F(22, 7), F(355, 113))))
00131 
00132         self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
00133                                  F, 12, 0)
00134         self.assertRaises(TypeError, F, 1.5 + 3j)
00135 
00136         self.assertRaises(TypeError, F, "3/2", 3)
00137         self.assertRaises(TypeError, F, 3, 0j)
00138         self.assertRaises(TypeError, F, 3, 1j)

Here is the call graph for this function:

Definition at line 149 of file test_fractions.py.

00149 
00150     def testInitFromDecimal(self):
00151         self.assertEqual((11, 10),
00152                          _components(F(Decimal('1.1'))))
00153         self.assertEqual((7, 200),
00154                          _components(F(Decimal('3.5e-2'))))
00155         self.assertEqual((0, 1),
00156                          _components(F(Decimal('.000e20'))))
00157         self.assertRaises(TypeError, F, Decimal('nan'))
00158         self.assertRaises(TypeError, F, Decimal('snan'))
00159         self.assertRaises(TypeError, F, Decimal('inf'))
00160         self.assertRaises(TypeError, F, Decimal('-inf'))

Here is the call graph for this function:

Definition at line 140 of file test_fractions.py.

00140 
00141     def testInitFromFloat(self):
00142         self.assertEqual((5, 2), _components(F(2.5)))
00143         self.assertEqual((0, 1), _components(F(-0.0)))
00144         self.assertEqual((3602879701896397, 36028797018963968),
00145                          _components(F(0.1)))
00146         self.assertRaises(TypeError, F, float('nan'))
00147         self.assertRaises(TypeError, F, float('inf'))
00148         self.assertRaises(TypeError, F, float('-inf'))

Here is the call graph for this function:

Definition at line 280 of file test_fractions.py.

00280 
00281     def testLimitDenominator(self):
00282         rpi = F('3.1415926535897932')
00283         self.assertEqual(rpi.limit_denominator(10000), F(355, 113))
00284         self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113))
00285         self.assertEqual(rpi.limit_denominator(113), F(355, 113))
00286         self.assertEqual(rpi.limit_denominator(112), F(333, 106))
00287         self.assertEqual(F(201, 200).limit_denominator(100), F(1))
00288         self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101))
00289         self.assertEqual(F(0).limit_denominator(10000), F(0))

Here is the call graph for this function:

Definition at line 336 of file test_fractions.py.

00336 
00337     def testMixedArithmetic(self):
00338         self.assertTypedEquals(F(11, 10), F(1, 10) + 1)
00339         self.assertTypedEquals(1.1, F(1, 10) + 1.0)
00340         self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j))
00341         self.assertTypedEquals(F(11, 10), 1 + F(1, 10))
00342         self.assertTypedEquals(1.1, 1.0 + F(1, 10))
00343         self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10))
00344 
00345         self.assertTypedEquals(F(-9, 10), F(1, 10) - 1)
00346         self.assertTypedEquals(-0.9, F(1, 10) - 1.0)
00347         self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j))
00348         self.assertTypedEquals(F(9, 10), 1 - F(1, 10))
00349         self.assertTypedEquals(0.9, 1.0 - F(1, 10))
00350         self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10))
00351 
00352         self.assertTypedEquals(F(1, 10), F(1, 10) * 1)
00353         self.assertTypedEquals(0.1, F(1, 10) * 1.0)
00354         self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j))
00355         self.assertTypedEquals(F(1, 10), 1 * F(1, 10))
00356         self.assertTypedEquals(0.1, 1.0 * F(1, 10))
00357         self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10))
00358 
00359         self.assertTypedEquals(F(1, 10), F(1, 10) / 1)
00360         self.assertTypedEquals(0.1, F(1, 10) / 1.0)
00361         self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j))
00362         self.assertTypedEquals(F(10, 1), 1 / F(1, 10))
00363         self.assertTypedEquals(10.0, 1.0 / F(1, 10))
00364         self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10))
00365 
00366         self.assertTypedEquals(0, F(1, 10) // 1)
00367         self.assertTypedEquals(0, F(1, 10) // 1.0)
00368         self.assertTypedEquals(10, 1 // F(1, 10))
00369         self.assertTypedEquals(10**23, 10**22 // F(1, 10))
00370         self.assertTypedEquals(10, 1.0 // F(1, 10))
00371 
00372         self.assertTypedEquals(F(1, 10), F(1, 10) % 1)
00373         self.assertTypedEquals(0.1, F(1, 10) % 1.0)
00374         self.assertTypedEquals(F(0, 1), 1 % F(1, 10))
00375         self.assertTypedEquals(0.0, 1.0 % F(1, 10))
00376 
00377         # No need for divmod since we don't override it.
00378 
00379         # ** has more interesting conversion rules.
00380         self.assertTypedEquals(F(100, 1), F(1, 10) ** -2)
00381         self.assertTypedEquals(F(100, 1), F(10, 1) ** 2)
00382         self.assertTypedEquals(0.1, F(1, 10) ** 1.0)
00383         self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j))
00384         self.assertTypedEquals(4 , 2 ** F(2, 1))
00385         z = pow(-1, F(1, 2))
00386         self.assertAlmostEqual(0, z.real)
00387         self.assertEqual(1, z.imag)
00388         self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1))
00389         self.assertTypedEquals(2.0 , 4 ** F(1, 2))
00390         self.assertTypedEquals(0.25, 2.0 ** F(-2, 1))
00391         self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10))

Here is the call graph for this function:

Definition at line 517 of file test_fractions.py.

00517 
00518     def testMixedEqual(self):
00519         self.assertTrue(0.5 == F(1, 2))
00520         self.assertFalse(0.6 == F(1, 2))
00521         self.assertTrue(F(1, 2) == 0.5)
00522         self.assertFalse(F(1, 2) == 0.4)
00523         self.assertTrue(2 == F(4, 2))
00524         self.assertFalse(2 == F(3, 2))
00525         self.assertTrue(F(4, 2) == 2)
00526         self.assertFalse(F(5, 2) == 2)
00527         self.assertFalse(F(5, 2) == float('nan'))
00528         self.assertFalse(float('nan') == F(3, 7))
00529         self.assertFalse(F(5, 2) == float('inf'))
00530         self.assertFalse(float('-inf') == F(2, 5))

Here is the call graph for this function:

Definition at line 454 of file test_fractions.py.

00454 
00455     def testMixedLess(self):
00456         self.assertTrue(2 < F(5, 2))
00457         self.assertFalse(2 < F(4, 2))
00458         self.assertTrue(F(5, 2) < 3)
00459         self.assertFalse(F(4, 2) < 2)
00460 
00461         self.assertTrue(F(1, 2) < 0.6)
00462         self.assertFalse(F(1, 2) < 0.4)
00463         self.assertTrue(0.4 < F(1, 2))
00464         self.assertFalse(0.5 < F(1, 2))
00465 
00466         self.assertFalse(float('inf') < F(1, 2))
00467         self.assertTrue(float('-inf') < F(0, 10))
00468         self.assertFalse(float('nan') < F(-3, 7))
00469         self.assertTrue(F(1, 2) < float('inf'))
00470         self.assertFalse(F(17, 12) < float('-inf'))
00471         self.assertFalse(F(144, -89) < float('nan'))

Here is the call graph for this function:

Definition at line 472 of file test_fractions.py.

00472 
00473     def testMixedLessEqual(self):
00474         self.assertTrue(0.5 <= F(1, 2))
00475         self.assertFalse(0.6 <= F(1, 2))
00476         self.assertTrue(F(1, 2) <= 0.5)
00477         self.assertFalse(F(1, 2) <= 0.4)
00478         self.assertTrue(2 <= F(4, 2))
00479         self.assertFalse(2 <= F(3, 2))
00480         self.assertTrue(F(4, 2) <= 2)
00481         self.assertFalse(F(5, 2) <= 2)
00482 
00483         self.assertFalse(float('inf') <= F(1, 2))
00484         self.assertTrue(float('-inf') <= F(0, 10))
00485         self.assertFalse(float('nan') <= F(-3, 7))
00486         self.assertTrue(F(1, 2) <= float('inf'))
00487         self.assertFalse(F(17, 12) <= float('-inf'))
00488         self.assertFalse(F(144, -89) <= float('nan'))

Here is the call graph for this function:

Definition at line 392 of file test_fractions.py.

00392 
00393     def testMixingWithDecimal(self):
00394         # Decimal refuses mixed arithmetic (but not mixed comparisons)
00395         self.assertRaisesMessage(
00396             TypeError,
00397             "unsupported operand type(s) for +: 'Fraction' and 'Decimal'",
00398             operator.add, F(3,11), Decimal('3.1415926'))

Here is the call graph for this function:

Definition at line 313 of file test_fractions.py.

00313 
00314     def testRound(self):
00315         self.assertTypedEquals(F(-200), round(F(-150), -2))
00316         self.assertTypedEquals(F(-200), round(F(-250), -2))
00317         self.assertTypedEquals(F(30), round(F(26), -1))
00318         self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1))
00319         self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1))
00320 

Here is the call graph for this function:

Definition at line 531 of file test_fractions.py.

00531 
00532     def testStringification(self):
00533         self.assertEqual("Fraction(7, 3)", repr(F(7, 3)))
00534         self.assertEqual("Fraction(6283185307, 2000000000)",
00535                          repr(F('3.1415926535')))
00536         self.assertEqual("Fraction(-1, 100000000000000000000)",
00537                          repr(F(1, -10**20)))
00538         self.assertEqual("7/3", str(F(7, 3)))
00539         self.assertEqual("7", str(F(7, 1)))

Here is the call graph for this function:


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