Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 1 | """Tests for Lib/fractions.py.""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 2 | |
| 3 | from decimal import Decimal |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test.support import run_unittest |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 5 | import math |
| 6 | import operator |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 7 | import fractions |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 8 | import unittest |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 9 | from copy import copy, deepcopy |
Neal Norwitz | 6ff93fe | 2008-01-26 21:52:30 +0000 | [diff] [blame] | 10 | from pickle import dumps, loads |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 11 | F = fractions.Fraction |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 12 | gcd = fractions.gcd |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 13 | |
| 14 | |
| 15 | class GcdTest(unittest.TestCase): |
| 16 | |
| 17 | def testMisc(self): |
| 18 | self.assertEquals(0, gcd(0, 0)) |
| 19 | self.assertEquals(1, gcd(1, 0)) |
| 20 | self.assertEquals(-1, gcd(-1, 0)) |
| 21 | self.assertEquals(1, gcd(0, 1)) |
| 22 | self.assertEquals(-1, gcd(0, -1)) |
| 23 | self.assertEquals(1, gcd(7, 1)) |
| 24 | self.assertEquals(-1, gcd(7, -1)) |
| 25 | self.assertEquals(1, gcd(-23, 15)) |
| 26 | self.assertEquals(12, gcd(120, 84)) |
| 27 | self.assertEquals(-12, gcd(84, -120)) |
| 28 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 29 | |
| 30 | def _components(r): |
| 31 | return (r.numerator, r.denominator) |
| 32 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 33 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 34 | class FractionTest(unittest.TestCase): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 35 | |
| 36 | def assertTypedEquals(self, expected, actual): |
| 37 | """Asserts that both the types and values are the same.""" |
| 38 | self.assertEquals(type(expected), type(actual)) |
| 39 | self.assertEquals(expected, actual) |
| 40 | |
| 41 | def assertRaisesMessage(self, exc_type, message, |
| 42 | callable, *args, **kwargs): |
| 43 | """Asserts that callable(*args, **kwargs) raises exc_type(message).""" |
| 44 | try: |
| 45 | callable(*args, **kwargs) |
| 46 | except exc_type as e: |
| 47 | self.assertEquals(message, str(e)) |
| 48 | else: |
| 49 | self.fail("%s not raised" % exc_type.__name__) |
| 50 | |
| 51 | def testInit(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 52 | self.assertEquals((0, 1), _components(F())) |
| 53 | self.assertEquals((7, 1), _components(F(7))) |
| 54 | self.assertEquals((7, 3), _components(F(F(7, 3)))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 55 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 56 | self.assertEquals((-1, 1), _components(F(-1, 1))) |
| 57 | self.assertEquals((-1, 1), _components(F(1, -1))) |
| 58 | self.assertEquals((1, 1), _components(F(-2, -2))) |
| 59 | self.assertEquals((1, 2), _components(F(5, 10))) |
| 60 | self.assertEquals((7, 15), _components(F(7, 15))) |
| 61 | self.assertEquals((10**23, 1), _components(F(10**23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 62 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 63 | self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 64 | F, 12, 0) |
| 65 | self.assertRaises(AttributeError, F, 1.5) |
| 66 | self.assertRaises(AttributeError, F, 1.5 + 3j) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 67 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 68 | self.assertRaises(AttributeError, F, F(1, 2), 3) |
| 69 | self.assertRaises(AttributeError, F, "3/2", 3) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 70 | |
| 71 | def testFromString(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 72 | self.assertEquals((5, 1), _components(F("5"))) |
| 73 | self.assertEquals((3, 2), _components(F("3/2"))) |
| 74 | self.assertEquals((3, 2), _components(F(" \n +3/2"))) |
| 75 | self.assertEquals((-3, 2), _components(F("-3/2 "))) |
| 76 | self.assertEquals((13, 2), _components(F(" 013/02 \n "))) |
| 77 | self.assertEquals((16, 5), _components(F(" 3.2 "))) |
| 78 | self.assertEquals((-16, 5), _components(F(" -3.2 "))) |
| 79 | self.assertEquals((-3, 1), _components(F(" -3. "))) |
| 80 | self.assertEquals((3, 5), _components(F(" .6 "))) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 81 | |
| 82 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 83 | ZeroDivisionError, "Fraction(3, 0)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 84 | F, "3/0") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 85 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 86 | ValueError, "Invalid literal for Fraction: 3/", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 87 | F, "3/") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 88 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 89 | ValueError, "Invalid literal for Fraction: 3 /2", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 90 | F, "3 /2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 91 | self.assertRaisesMessage( |
| 92 | # Denominators don't need a sign. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 93 | ValueError, "Invalid literal for Fraction: 3/+2", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 94 | F, "3/+2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 95 | self.assertRaisesMessage( |
| 96 | # Imitate float's parsing. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 97 | ValueError, "Invalid literal for Fraction: + 3/2", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 98 | F, "+ 3/2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 99 | self.assertRaisesMessage( |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 100 | # Avoid treating '.' as a regex special character. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 101 | ValueError, "Invalid literal for Fraction: 3a2", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 102 | F, "3a2") |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 103 | self.assertRaisesMessage( |
| 104 | # Only parse ordinary decimals, not scientific form. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 105 | ValueError, "Invalid literal for Fraction: 3.2e4", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 106 | F, "3.2e4") |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 107 | self.assertRaisesMessage( |
| 108 | # Don't accept combinations of decimals and rationals. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 109 | ValueError, "Invalid literal for Fraction: 3/7.2", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 110 | F, "3/7.2") |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 111 | self.assertRaisesMessage( |
| 112 | # Don't accept combinations of decimals and rationals. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 113 | ValueError, "Invalid literal for Fraction: 3.2/7", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 114 | F, "3.2/7") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 115 | self.assertRaisesMessage( |
| 116 | # Allow 3. and .3, but not . |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 117 | ValueError, "Invalid literal for Fraction: .", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 118 | F, ".") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 119 | |
| 120 | def testImmutable(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 121 | r = F(7, 3) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 122 | r.__init__(2, 15) |
| 123 | self.assertEquals((7, 3), _components(r)) |
| 124 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 125 | self.assertRaises(AttributeError, setattr, r, 'numerator', 12) |
| 126 | self.assertRaises(AttributeError, setattr, r, 'denominator', 6) |
| 127 | self.assertEquals((7, 3), _components(r)) |
| 128 | |
| 129 | # But if you _really_ need to: |
| 130 | r._numerator = 4 |
| 131 | r._denominator = 2 |
| 132 | self.assertEquals((4, 2), _components(r)) |
| 133 | # Which breaks some important operations: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 134 | self.assertNotEquals(F(4, 2), r) |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 135 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 136 | def testFromFloat(self): |
| 137 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 138 | TypeError, "Fraction.from_float() only takes floats, not 3 (int)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 139 | F.from_float, 3) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 140 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 141 | self.assertEquals((0, 1), _components(F.from_float(-0.0))) |
| 142 | self.assertEquals((10, 1), _components(F.from_float(10.0))) |
| 143 | self.assertEquals((-5, 2), _components(F.from_float(-2.5))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 144 | self.assertEquals((99999999999999991611392, 1), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 145 | _components(F.from_float(1e23))) |
| 146 | self.assertEquals(float(10**23), float(F.from_float(1e23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 147 | self.assertEquals((3602879701896397, 1125899906842624), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 148 | _components(F.from_float(3.2))) |
| 149 | self.assertEquals(3.2, float(F.from_float(3.2))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 150 | |
| 151 | inf = 1e1000 |
| 152 | nan = inf - inf |
| 153 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 154 | TypeError, "Cannot convert inf to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 155 | F.from_float, inf) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 156 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 157 | TypeError, "Cannot convert -inf to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 158 | F.from_float, -inf) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 159 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 160 | TypeError, "Cannot convert nan to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 161 | F.from_float, nan) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 162 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 163 | def testFromDecimal(self): |
| 164 | self.assertRaisesMessage( |
| 165 | TypeError, |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 166 | "Fraction.from_decimal() only takes Decimals, not 3 (int)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 167 | F.from_decimal, 3) |
| 168 | self.assertEquals(F(0), F.from_decimal(Decimal("-0"))) |
| 169 | self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5"))) |
| 170 | self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3"))) |
| 171 | self.assertEquals(F(5000), F.from_decimal(Decimal("5e3"))) |
| 172 | self.assertEquals(1 - F(1, 10**30), |
| 173 | F.from_decimal(Decimal("0." + "9" * 30))) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 174 | |
| 175 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 176 | TypeError, "Cannot convert Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 177 | F.from_decimal, Decimal("inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 178 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 179 | TypeError, "Cannot convert -Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 180 | F.from_decimal, Decimal("-inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 181 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 182 | TypeError, "Cannot convert NaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 183 | F.from_decimal, Decimal("nan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 184 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 185 | TypeError, "Cannot convert sNaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 186 | F.from_decimal, Decimal("snan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 187 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 188 | def testLimitDenominator(self): |
| 189 | rpi = F('3.1415926535897932') |
| 190 | self.assertEqual(rpi.limit_denominator(10000), F(355, 113)) |
| 191 | self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113)) |
| 192 | self.assertEqual(rpi.limit_denominator(113), F(355, 113)) |
| 193 | self.assertEqual(rpi.limit_denominator(112), F(333, 106)) |
| 194 | self.assertEqual(F(201, 200).limit_denominator(100), F(1)) |
| 195 | self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101)) |
| 196 | self.assertEqual(F(0).limit_denominator(10000), F(0)) |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 197 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 198 | def testConversions(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 199 | self.assertTypedEquals(-1, math.trunc(F(-11, 10))) |
| 200 | self.assertTypedEquals(-2, math.floor(F(-11, 10))) |
| 201 | self.assertTypedEquals(-1, math.ceil(F(-11, 10))) |
| 202 | self.assertTypedEquals(-1, math.ceil(F(-10, 10))) |
| 203 | self.assertTypedEquals(-1, int(F(-11, 10))) |
| 204 | self.assertTypedEquals(0, round(F(-1, 10))) |
| 205 | self.assertTypedEquals(0, round(F(-5, 10))) |
| 206 | self.assertTypedEquals(-2, round(F(-15, 10))) |
| 207 | self.assertTypedEquals(-1, round(F(-7, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 208 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 209 | self.assertEquals(False, bool(F(0, 1))) |
| 210 | self.assertEquals(True, bool(F(3, 2))) |
| 211 | self.assertTypedEquals(0.1, float(F(1, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 212 | |
| 213 | # Check that __float__ isn't implemented by converting the |
| 214 | # numerator and denominator to float before dividing. |
| 215 | self.assertRaises(OverflowError, float, int('2'*400+'7')) |
| 216 | self.assertAlmostEquals(2.0/3, |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 217 | float(F(int('2'*400+'7'), int('3'*400+'1')))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 218 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 219 | self.assertTypedEquals(0.1+0j, complex(F(1,10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 220 | |
| 221 | def testRound(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 222 | self.assertTypedEquals(F(-200), round(F(-150), -2)) |
| 223 | self.assertTypedEquals(F(-200), round(F(-250), -2)) |
| 224 | self.assertTypedEquals(F(30), round(F(26), -1)) |
| 225 | self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1)) |
| 226 | self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 227 | |
| 228 | |
| 229 | def testArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 230 | self.assertEquals(F(1, 2), F(1, 10) + F(2, 5)) |
| 231 | self.assertEquals(F(-3, 10), F(1, 10) - F(2, 5)) |
| 232 | self.assertEquals(F(1, 25), F(1, 10) * F(2, 5)) |
| 233 | self.assertEquals(F(1, 4), F(1, 10) / F(2, 5)) |
| 234 | self.assertTypedEquals(2, F(9, 10) // F(2, 5)) |
| 235 | self.assertTypedEquals(10**23, F(10**23, 1) // F(1)) |
| 236 | self.assertEquals(F(2, 3), F(-7, 3) % F(3, 2)) |
| 237 | self.assertEquals(F(8, 27), F(2, 3) ** F(3)) |
| 238 | self.assertEquals(F(27, 8), F(2, 3) ** F(-3)) |
| 239 | self.assertTypedEquals(2.0, F(4) ** F(1, 2)) |
| 240 | z = pow(F(-1), F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 241 | self.assertAlmostEquals(z.real, 0) |
| 242 | self.assertEquals(z.imag, 1) |
| 243 | |
| 244 | def testMixedArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 245 | self.assertTypedEquals(F(11, 10), F(1, 10) + 1) |
| 246 | self.assertTypedEquals(1.1, F(1, 10) + 1.0) |
| 247 | self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j)) |
| 248 | self.assertTypedEquals(F(11, 10), 1 + F(1, 10)) |
| 249 | self.assertTypedEquals(1.1, 1.0 + F(1, 10)) |
| 250 | self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 251 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 252 | self.assertTypedEquals(F(-9, 10), F(1, 10) - 1) |
| 253 | self.assertTypedEquals(-0.9, F(1, 10) - 1.0) |
| 254 | self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j)) |
| 255 | self.assertTypedEquals(F(9, 10), 1 - F(1, 10)) |
| 256 | self.assertTypedEquals(0.9, 1.0 - F(1, 10)) |
| 257 | self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 258 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 259 | self.assertTypedEquals(F(1, 10), F(1, 10) * 1) |
| 260 | self.assertTypedEquals(0.1, F(1, 10) * 1.0) |
| 261 | self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j)) |
| 262 | self.assertTypedEquals(F(1, 10), 1 * F(1, 10)) |
| 263 | self.assertTypedEquals(0.1, 1.0 * F(1, 10)) |
| 264 | self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 265 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 266 | self.assertTypedEquals(F(1, 10), F(1, 10) / 1) |
| 267 | self.assertTypedEquals(0.1, F(1, 10) / 1.0) |
| 268 | self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j)) |
| 269 | self.assertTypedEquals(F(10, 1), 1 / F(1, 10)) |
| 270 | self.assertTypedEquals(10.0, 1.0 / F(1, 10)) |
| 271 | self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 272 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 273 | self.assertTypedEquals(0, F(1, 10) // 1) |
| 274 | self.assertTypedEquals(0, F(1, 10) // 1.0) |
| 275 | self.assertTypedEquals(10, 1 // F(1, 10)) |
| 276 | self.assertTypedEquals(10**23, 10**22 // F(1, 10)) |
| 277 | self.assertTypedEquals(10, 1.0 // F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 278 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 279 | self.assertTypedEquals(F(1, 10), F(1, 10) % 1) |
| 280 | self.assertTypedEquals(0.1, F(1, 10) % 1.0) |
| 281 | self.assertTypedEquals(F(0, 1), 1 % F(1, 10)) |
| 282 | self.assertTypedEquals(0.0, 1.0 % F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 283 | |
| 284 | # No need for divmod since we don't override it. |
| 285 | |
| 286 | # ** has more interesting conversion rules. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 287 | self.assertTypedEquals(F(100, 1), F(1, 10) ** -2) |
| 288 | self.assertTypedEquals(F(100, 1), F(10, 1) ** 2) |
| 289 | self.assertTypedEquals(0.1, F(1, 10) ** 1.0) |
| 290 | self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j)) |
| 291 | self.assertTypedEquals(4 , 2 ** F(2, 1)) |
| 292 | z = pow(-1, F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 293 | self.assertAlmostEquals(0, z.real) |
| 294 | self.assertEquals(1, z.imag) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 295 | self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1)) |
| 296 | self.assertTypedEquals(2.0 , 4 ** F(1, 2)) |
| 297 | self.assertTypedEquals(0.25, 2.0 ** F(-2, 1)) |
| 298 | self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 299 | |
| 300 | def testMixingWithDecimal(self): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 301 | # Decimal refuses mixed comparisons. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 302 | self.assertRaisesMessage( |
| 303 | TypeError, |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 304 | "unsupported operand type(s) for +: 'Fraction' and 'Decimal'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 305 | operator.add, F(3,11), Decimal('3.1415926')) |
| 306 | self.assertNotEquals(F(5, 2), Decimal('2.5')) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 307 | |
| 308 | def testComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 309 | self.assertTrue(F(1, 2) < F(2, 3)) |
| 310 | self.assertFalse(F(1, 2) < F(1, 2)) |
| 311 | self.assertTrue(F(1, 2) <= F(2, 3)) |
| 312 | self.assertTrue(F(1, 2) <= F(1, 2)) |
| 313 | self.assertFalse(F(2, 3) <= F(1, 2)) |
| 314 | self.assertTrue(F(1, 2) == F(1, 2)) |
| 315 | self.assertFalse(F(1, 2) == F(1, 3)) |
| 316 | self.assertFalse(F(1, 2) != F(1, 2)) |
| 317 | self.assertTrue(F(1, 2) != F(1, 3)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 318 | |
| 319 | def testMixedLess(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 320 | self.assertTrue(2 < F(5, 2)) |
| 321 | self.assertFalse(2 < F(4, 2)) |
| 322 | self.assertTrue(F(5, 2) < 3) |
| 323 | self.assertFalse(F(4, 2) < 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 324 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 325 | self.assertTrue(F(1, 2) < 0.6) |
| 326 | self.assertFalse(F(1, 2) < 0.4) |
| 327 | self.assertTrue(0.4 < F(1, 2)) |
| 328 | self.assertFalse(0.5 < F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 329 | |
| 330 | def testMixedLessEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 331 | self.assertTrue(0.5 <= F(1, 2)) |
| 332 | self.assertFalse(0.6 <= F(1, 2)) |
| 333 | self.assertTrue(F(1, 2) <= 0.5) |
| 334 | self.assertFalse(F(1, 2) <= 0.4) |
| 335 | self.assertTrue(2 <= F(4, 2)) |
| 336 | self.assertFalse(2 <= F(3, 2)) |
| 337 | self.assertTrue(F(4, 2) <= 2) |
| 338 | self.assertFalse(F(5, 2) <= 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 339 | |
| 340 | def testBigFloatComparisons(self): |
| 341 | # Because 10**23 can't be represented exactly as a float: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 342 | self.assertFalse(F(10**23) == float(10**23)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 343 | # The first test demonstrates why these are important. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 344 | self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1))) |
| 345 | self.assertTrue(1e23 < F(math.trunc(1e23) + 1)) |
| 346 | self.assertFalse(1e23 <= F(math.trunc(1e23) - 1)) |
| 347 | self.assertTrue(1e23 > F(math.trunc(1e23) - 1)) |
| 348 | self.assertFalse(1e23 >= F(math.trunc(1e23) + 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 349 | |
| 350 | def testBigComplexComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 351 | self.assertFalse(F(10**23) == complex(10**23)) |
| 352 | self.assertTrue(F(10**23) > complex(10**23)) |
| 353 | self.assertFalse(F(10**23) <= complex(10**23)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 354 | |
| 355 | def testMixedEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 356 | self.assertTrue(0.5 == F(1, 2)) |
| 357 | self.assertFalse(0.6 == F(1, 2)) |
| 358 | self.assertTrue(F(1, 2) == 0.5) |
| 359 | self.assertFalse(F(1, 2) == 0.4) |
| 360 | self.assertTrue(2 == F(4, 2)) |
| 361 | self.assertFalse(2 == F(3, 2)) |
| 362 | self.assertTrue(F(4, 2) == 2) |
| 363 | self.assertFalse(F(5, 2) == 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 364 | |
| 365 | def testStringification(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 366 | self.assertEquals("Fraction(7, 3)", repr(F(7, 3))) |
| 367 | self.assertEquals("7/3", str(F(7, 3))) |
| 368 | self.assertEquals("7", str(F(7, 1))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 369 | |
| 370 | def testHash(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 371 | self.assertEquals(hash(2.5), hash(F(5, 2))) |
| 372 | self.assertEquals(hash(10**50), hash(F(10**50))) |
| 373 | self.assertNotEquals(hash(float(10**23)), hash(F(10**23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 374 | |
| 375 | def testApproximatePi(self): |
| 376 | # Algorithm borrowed from |
| 377 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 378 | three = F(3) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 379 | lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 380 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 381 | lasts = s |
| 382 | n, na = n+na, na+8 |
| 383 | d, da = d+da, da+32 |
| 384 | t = (t * n) / d |
| 385 | s += t |
| 386 | self.assertAlmostEquals(math.pi, s) |
| 387 | |
| 388 | def testApproximateCos1(self): |
| 389 | # Algorithm borrowed from |
| 390 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 391 | x = F(1) |
| 392 | i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1 |
| 393 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 394 | lasts = s |
| 395 | i += 2 |
| 396 | fact *= i * (i-1) |
| 397 | num *= x * x |
| 398 | sign *= -1 |
| 399 | s += num / fact * sign |
| 400 | self.assertAlmostEquals(math.cos(1), s) |
| 401 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 402 | def test_copy_deepcopy_pickle(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 403 | r = F(13, 7) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 404 | self.assertEqual(r, loads(dumps(r))) |
| 405 | self.assertEqual(id(r), id(copy(r))) |
| 406 | self.assertEqual(id(r), id(deepcopy(r))) |
| 407 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 408 | def test_main(): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 409 | run_unittest(FractionTest, GcdTest) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 410 | |
| 411 | if __name__ == '__main__': |
| 412 | test_main() |