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) |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 65 | self.assertRaises(TypeError, F, 1.5) |
| 66 | self.assertRaises(TypeError, F, 1.5 + 3j) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 67 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 68 | self.assertRaises(TypeError, F, F(1, 2), 3) |
| 69 | self.assertRaises(TypeError, 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( |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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( |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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 . |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +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): |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 137 | self.assertRaises(TypeError, F.from_float, 3+4j) |
| 138 | self.assertEquals((10, 1), _components(F.from_float(10))) |
Georg Brandl | 3a9b062 | 2009-01-03 22:07:57 +0000 | [diff] [blame] | 139 | bigint = 1234567890123456789 |
| 140 | self.assertEquals((bigint, 1), _components(F.from_float(bigint))) |
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): |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 164 | self.assertRaises(TypeError, F.from_decimal, 3+4j) |
| 165 | self.assertEquals(F(10, 1), F.from_decimal(10)) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 166 | self.assertEquals(F(0), F.from_decimal(Decimal("-0"))) |
| 167 | self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5"))) |
| 168 | self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3"))) |
| 169 | self.assertEquals(F(5000), F.from_decimal(Decimal("5e3"))) |
| 170 | self.assertEquals(1 - F(1, 10**30), |
| 171 | F.from_decimal(Decimal("0." + "9" * 30))) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 172 | |
| 173 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 174 | TypeError, "Cannot convert Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 175 | F.from_decimal, Decimal("inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 176 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 177 | TypeError, "Cannot convert -Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 178 | F.from_decimal, Decimal("-inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 179 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 180 | TypeError, "Cannot convert NaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 181 | F.from_decimal, Decimal("nan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 182 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 183 | TypeError, "Cannot convert sNaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 184 | F.from_decimal, Decimal("snan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 185 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 186 | def testLimitDenominator(self): |
| 187 | rpi = F('3.1415926535897932') |
| 188 | self.assertEqual(rpi.limit_denominator(10000), F(355, 113)) |
| 189 | self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113)) |
| 190 | self.assertEqual(rpi.limit_denominator(113), F(355, 113)) |
| 191 | self.assertEqual(rpi.limit_denominator(112), F(333, 106)) |
| 192 | self.assertEqual(F(201, 200).limit_denominator(100), F(1)) |
| 193 | self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101)) |
| 194 | self.assertEqual(F(0).limit_denominator(10000), F(0)) |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 195 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 196 | def testConversions(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 197 | self.assertTypedEquals(-1, math.trunc(F(-11, 10))) |
| 198 | self.assertTypedEquals(-2, math.floor(F(-11, 10))) |
| 199 | self.assertTypedEquals(-1, math.ceil(F(-11, 10))) |
| 200 | self.assertTypedEquals(-1, math.ceil(F(-10, 10))) |
| 201 | self.assertTypedEquals(-1, int(F(-11, 10))) |
| 202 | self.assertTypedEquals(0, round(F(-1, 10))) |
| 203 | self.assertTypedEquals(0, round(F(-5, 10))) |
| 204 | self.assertTypedEquals(-2, round(F(-15, 10))) |
| 205 | self.assertTypedEquals(-1, round(F(-7, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 206 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 207 | self.assertEquals(False, bool(F(0, 1))) |
| 208 | self.assertEquals(True, bool(F(3, 2))) |
| 209 | self.assertTypedEquals(0.1, float(F(1, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 210 | |
| 211 | # Check that __float__ isn't implemented by converting the |
| 212 | # numerator and denominator to float before dividing. |
| 213 | self.assertRaises(OverflowError, float, int('2'*400+'7')) |
| 214 | self.assertAlmostEquals(2.0/3, |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 215 | float(F(int('2'*400+'7'), int('3'*400+'1')))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 216 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 217 | self.assertTypedEquals(0.1+0j, complex(F(1,10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 218 | |
| 219 | def testRound(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 220 | self.assertTypedEquals(F(-200), round(F(-150), -2)) |
| 221 | self.assertTypedEquals(F(-200), round(F(-250), -2)) |
| 222 | self.assertTypedEquals(F(30), round(F(26), -1)) |
| 223 | self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1)) |
| 224 | self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 225 | |
| 226 | |
| 227 | def testArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 228 | self.assertEquals(F(1, 2), F(1, 10) + F(2, 5)) |
| 229 | self.assertEquals(F(-3, 10), F(1, 10) - F(2, 5)) |
| 230 | self.assertEquals(F(1, 25), F(1, 10) * F(2, 5)) |
| 231 | self.assertEquals(F(1, 4), F(1, 10) / F(2, 5)) |
| 232 | self.assertTypedEquals(2, F(9, 10) // F(2, 5)) |
| 233 | self.assertTypedEquals(10**23, F(10**23, 1) // F(1)) |
| 234 | self.assertEquals(F(2, 3), F(-7, 3) % F(3, 2)) |
| 235 | self.assertEquals(F(8, 27), F(2, 3) ** F(3)) |
| 236 | self.assertEquals(F(27, 8), F(2, 3) ** F(-3)) |
| 237 | self.assertTypedEquals(2.0, F(4) ** F(1, 2)) |
| 238 | z = pow(F(-1), F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 239 | self.assertAlmostEquals(z.real, 0) |
| 240 | self.assertEquals(z.imag, 1) |
| 241 | |
| 242 | def testMixedArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 243 | self.assertTypedEquals(F(11, 10), F(1, 10) + 1) |
| 244 | self.assertTypedEquals(1.1, F(1, 10) + 1.0) |
| 245 | self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j)) |
| 246 | self.assertTypedEquals(F(11, 10), 1 + F(1, 10)) |
| 247 | self.assertTypedEquals(1.1, 1.0 + F(1, 10)) |
| 248 | self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 249 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 250 | self.assertTypedEquals(F(-9, 10), F(1, 10) - 1) |
| 251 | self.assertTypedEquals(-0.9, F(1, 10) - 1.0) |
| 252 | self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j)) |
| 253 | self.assertTypedEquals(F(9, 10), 1 - F(1, 10)) |
| 254 | self.assertTypedEquals(0.9, 1.0 - F(1, 10)) |
| 255 | self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 256 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 257 | self.assertTypedEquals(F(1, 10), F(1, 10) * 1) |
| 258 | self.assertTypedEquals(0.1, F(1, 10) * 1.0) |
| 259 | self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j)) |
| 260 | self.assertTypedEquals(F(1, 10), 1 * F(1, 10)) |
| 261 | self.assertTypedEquals(0.1, 1.0 * F(1, 10)) |
| 262 | self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 263 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 264 | self.assertTypedEquals(F(1, 10), F(1, 10) / 1) |
| 265 | self.assertTypedEquals(0.1, F(1, 10) / 1.0) |
| 266 | self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j)) |
| 267 | self.assertTypedEquals(F(10, 1), 1 / F(1, 10)) |
| 268 | self.assertTypedEquals(10.0, 1.0 / F(1, 10)) |
| 269 | self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 270 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 271 | self.assertTypedEquals(0, F(1, 10) // 1) |
| 272 | self.assertTypedEquals(0, F(1, 10) // 1.0) |
| 273 | self.assertTypedEquals(10, 1 // F(1, 10)) |
| 274 | self.assertTypedEquals(10**23, 10**22 // F(1, 10)) |
| 275 | self.assertTypedEquals(10, 1.0 // F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 276 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 277 | self.assertTypedEquals(F(1, 10), F(1, 10) % 1) |
| 278 | self.assertTypedEquals(0.1, F(1, 10) % 1.0) |
| 279 | self.assertTypedEquals(F(0, 1), 1 % F(1, 10)) |
| 280 | self.assertTypedEquals(0.0, 1.0 % F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 281 | |
| 282 | # No need for divmod since we don't override it. |
| 283 | |
| 284 | # ** has more interesting conversion rules. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 285 | self.assertTypedEquals(F(100, 1), F(1, 10) ** -2) |
| 286 | self.assertTypedEquals(F(100, 1), F(10, 1) ** 2) |
| 287 | self.assertTypedEquals(0.1, F(1, 10) ** 1.0) |
| 288 | self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j)) |
| 289 | self.assertTypedEquals(4 , 2 ** F(2, 1)) |
| 290 | z = pow(-1, F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 291 | self.assertAlmostEquals(0, z.real) |
| 292 | self.assertEquals(1, z.imag) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 293 | self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1)) |
| 294 | self.assertTypedEquals(2.0 , 4 ** F(1, 2)) |
| 295 | self.assertTypedEquals(0.25, 2.0 ** F(-2, 1)) |
| 296 | self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 297 | |
| 298 | def testMixingWithDecimal(self): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 299 | # Decimal refuses mixed comparisons. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 300 | self.assertRaisesMessage( |
| 301 | TypeError, |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 302 | "unsupported operand type(s) for +: 'Fraction' and 'Decimal'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 303 | operator.add, F(3,11), Decimal('3.1415926')) |
| 304 | self.assertNotEquals(F(5, 2), Decimal('2.5')) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 305 | |
| 306 | def testComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 307 | self.assertTrue(F(1, 2) < F(2, 3)) |
| 308 | self.assertFalse(F(1, 2) < F(1, 2)) |
| 309 | self.assertTrue(F(1, 2) <= F(2, 3)) |
| 310 | self.assertTrue(F(1, 2) <= F(1, 2)) |
| 311 | self.assertFalse(F(2, 3) <= F(1, 2)) |
| 312 | self.assertTrue(F(1, 2) == F(1, 2)) |
| 313 | self.assertFalse(F(1, 2) == F(1, 3)) |
| 314 | self.assertFalse(F(1, 2) != F(1, 2)) |
| 315 | self.assertTrue(F(1, 2) != F(1, 3)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 316 | |
| 317 | def testMixedLess(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 318 | self.assertTrue(2 < F(5, 2)) |
| 319 | self.assertFalse(2 < F(4, 2)) |
| 320 | self.assertTrue(F(5, 2) < 3) |
| 321 | self.assertFalse(F(4, 2) < 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 322 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 323 | self.assertTrue(F(1, 2) < 0.6) |
| 324 | self.assertFalse(F(1, 2) < 0.4) |
| 325 | self.assertTrue(0.4 < F(1, 2)) |
| 326 | self.assertFalse(0.5 < F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 327 | |
| 328 | def testMixedLessEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 329 | self.assertTrue(0.5 <= F(1, 2)) |
| 330 | self.assertFalse(0.6 <= F(1, 2)) |
| 331 | self.assertTrue(F(1, 2) <= 0.5) |
| 332 | self.assertFalse(F(1, 2) <= 0.4) |
| 333 | self.assertTrue(2 <= F(4, 2)) |
| 334 | self.assertFalse(2 <= F(3, 2)) |
| 335 | self.assertTrue(F(4, 2) <= 2) |
| 336 | self.assertFalse(F(5, 2) <= 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 337 | |
| 338 | def testBigFloatComparisons(self): |
| 339 | # Because 10**23 can't be represented exactly as a float: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 340 | self.assertFalse(F(10**23) == float(10**23)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 341 | # The first test demonstrates why these are important. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 342 | self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1))) |
| 343 | self.assertTrue(1e23 < F(math.trunc(1e23) + 1)) |
| 344 | self.assertFalse(1e23 <= F(math.trunc(1e23) - 1)) |
| 345 | self.assertTrue(1e23 > F(math.trunc(1e23) - 1)) |
| 346 | self.assertFalse(1e23 >= F(math.trunc(1e23) + 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 347 | |
| 348 | def testBigComplexComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 349 | self.assertFalse(F(10**23) == complex(10**23)) |
| 350 | self.assertTrue(F(10**23) > complex(10**23)) |
| 351 | self.assertFalse(F(10**23) <= complex(10**23)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 352 | |
| 353 | def testMixedEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 354 | self.assertTrue(0.5 == F(1, 2)) |
| 355 | self.assertFalse(0.6 == F(1, 2)) |
| 356 | self.assertTrue(F(1, 2) == 0.5) |
| 357 | self.assertFalse(F(1, 2) == 0.4) |
| 358 | self.assertTrue(2 == F(4, 2)) |
| 359 | self.assertFalse(2 == F(3, 2)) |
| 360 | self.assertTrue(F(4, 2) == 2) |
| 361 | self.assertFalse(F(5, 2) == 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 362 | |
| 363 | def testStringification(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 364 | self.assertEquals("Fraction(7, 3)", repr(F(7, 3))) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 365 | self.assertEquals("Fraction(6283185307, 2000000000)", |
| 366 | repr(F('3.1415926535'))) |
| 367 | self.assertEquals("Fraction(-1, 100000000000000000000)", |
| 368 | repr(F(1, -10**20))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 369 | self.assertEquals("7/3", str(F(7, 3))) |
| 370 | self.assertEquals("7", str(F(7, 1))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 371 | |
| 372 | def testHash(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 373 | self.assertEquals(hash(2.5), hash(F(5, 2))) |
| 374 | self.assertEquals(hash(10**50), hash(F(10**50))) |
| 375 | self.assertNotEquals(hash(float(10**23)), hash(F(10**23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 376 | |
| 377 | def testApproximatePi(self): |
| 378 | # Algorithm borrowed from |
| 379 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 380 | three = F(3) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 381 | 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] | 382 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 383 | lasts = s |
| 384 | n, na = n+na, na+8 |
| 385 | d, da = d+da, da+32 |
| 386 | t = (t * n) / d |
| 387 | s += t |
| 388 | self.assertAlmostEquals(math.pi, s) |
| 389 | |
| 390 | def testApproximateCos1(self): |
| 391 | # Algorithm borrowed from |
| 392 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 393 | x = F(1) |
| 394 | i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1 |
| 395 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 396 | lasts = s |
| 397 | i += 2 |
| 398 | fact *= i * (i-1) |
| 399 | num *= x * x |
| 400 | sign *= -1 |
| 401 | s += num / fact * sign |
| 402 | self.assertAlmostEquals(math.cos(1), s) |
| 403 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 404 | def test_copy_deepcopy_pickle(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 405 | r = F(13, 7) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 406 | self.assertEqual(r, loads(dumps(r))) |
| 407 | self.assertEqual(id(r), id(copy(r))) |
| 408 | self.assertEqual(id(r), id(deepcopy(r))) |
| 409 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 410 | def test_main(): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 411 | run_unittest(FractionTest, GcdTest) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 412 | |
| 413 | if __name__ == '__main__': |
| 414 | test_main() |