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 |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 6 | import numbers |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 7 | import operator |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 8 | import fractions |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 9 | import unittest |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 10 | from copy import copy, deepcopy |
Neal Norwitz | 6ff93fe | 2008-01-26 21:52:30 +0000 | [diff] [blame] | 11 | from pickle import dumps, loads |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 12 | F = fractions.Fraction |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 13 | gcd = fractions.gcd |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 14 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 15 | # decorator for skipping tests on non-IEEE 754 platforms |
| 16 | requires_IEEE_754 = unittest.skipUnless( |
| 17 | float.__getformat__("double").startswith("IEEE"), |
| 18 | "test requires IEEE 754 doubles") |
| 19 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 20 | class DummyFloat(object): |
| 21 | """Dummy float class for testing comparisons with Fractions""" |
| 22 | |
| 23 | def __init__(self, value): |
| 24 | if not isinstance(value, float): |
| 25 | raise TypeError("DummyFloat can only be initialized from float") |
| 26 | self.value = value |
| 27 | |
| 28 | def _richcmp(self, other, op): |
| 29 | if isinstance(other, numbers.Rational): |
| 30 | return op(F.from_float(self.value), other) |
| 31 | elif isinstance(other, DummyFloat): |
| 32 | return op(self.value, other.value) |
| 33 | else: |
| 34 | return NotImplemented |
| 35 | |
| 36 | def __eq__(self, other): return self._richcmp(other, operator.eq) |
| 37 | def __le__(self, other): return self._richcmp(other, operator.le) |
| 38 | def __lt__(self, other): return self._richcmp(other, operator.lt) |
| 39 | def __ge__(self, other): return self._richcmp(other, operator.ge) |
| 40 | def __gt__(self, other): return self._richcmp(other, operator.gt) |
| 41 | |
| 42 | # shouldn't be calling __float__ at all when doing comparisons |
| 43 | def __float__(self): |
| 44 | assert False, "__float__ should not be invoked for comparisons" |
| 45 | |
| 46 | # same goes for subtraction |
| 47 | def __sub__(self, other): |
| 48 | assert False, "__sub__ should not be invoked for comparisons" |
| 49 | __rsub__ = __sub__ |
| 50 | |
| 51 | |
| 52 | class DummyRational(object): |
| 53 | """Test comparison of Fraction with a naive rational implementation.""" |
| 54 | |
| 55 | def __init__(self, num, den): |
| 56 | g = gcd(num, den) |
| 57 | self.num = num // g |
| 58 | self.den = den // g |
| 59 | |
| 60 | def __eq__(self, other): |
| 61 | if isinstance(other, fractions.Fraction): |
| 62 | return (self.num == other._numerator and |
| 63 | self.den == other._denominator) |
| 64 | else: |
| 65 | return NotImplemented |
| 66 | |
| 67 | def __lt__(self, other): |
| 68 | return(self.num * other._denominator < self.den * other._numerator) |
| 69 | |
| 70 | def __gt__(self, other): |
| 71 | return(self.num * other._denominator > self.den * other._numerator) |
| 72 | |
| 73 | def __le__(self, other): |
| 74 | return(self.num * other._denominator <= self.den * other._numerator) |
| 75 | |
| 76 | def __ge__(self, other): |
| 77 | return(self.num * other._denominator >= self.den * other._numerator) |
| 78 | |
| 79 | # this class is for testing comparisons; conversion to float |
| 80 | # should never be used for a comparison, since it loses accuracy |
| 81 | def __float__(self): |
| 82 | assert False, "__float__ should not be invoked" |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 83 | |
| 84 | class GcdTest(unittest.TestCase): |
| 85 | |
| 86 | def testMisc(self): |
| 87 | self.assertEquals(0, gcd(0, 0)) |
| 88 | self.assertEquals(1, gcd(1, 0)) |
| 89 | self.assertEquals(-1, gcd(-1, 0)) |
| 90 | self.assertEquals(1, gcd(0, 1)) |
| 91 | self.assertEquals(-1, gcd(0, -1)) |
| 92 | self.assertEquals(1, gcd(7, 1)) |
| 93 | self.assertEquals(-1, gcd(7, -1)) |
| 94 | self.assertEquals(1, gcd(-23, 15)) |
| 95 | self.assertEquals(12, gcd(120, 84)) |
| 96 | self.assertEquals(-12, gcd(84, -120)) |
| 97 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 98 | |
| 99 | def _components(r): |
| 100 | return (r.numerator, r.denominator) |
| 101 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 102 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 103 | class FractionTest(unittest.TestCase): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 104 | |
| 105 | def assertTypedEquals(self, expected, actual): |
| 106 | """Asserts that both the types and values are the same.""" |
| 107 | self.assertEquals(type(expected), type(actual)) |
| 108 | self.assertEquals(expected, actual) |
| 109 | |
| 110 | def assertRaisesMessage(self, exc_type, message, |
| 111 | callable, *args, **kwargs): |
| 112 | """Asserts that callable(*args, **kwargs) raises exc_type(message).""" |
| 113 | try: |
| 114 | callable(*args, **kwargs) |
| 115 | except exc_type as e: |
| 116 | self.assertEquals(message, str(e)) |
| 117 | else: |
| 118 | self.fail("%s not raised" % exc_type.__name__) |
| 119 | |
| 120 | def testInit(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 121 | self.assertEquals((0, 1), _components(F())) |
| 122 | self.assertEquals((7, 1), _components(F(7))) |
| 123 | self.assertEquals((7, 3), _components(F(F(7, 3)))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 124 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 125 | self.assertEquals((-1, 1), _components(F(-1, 1))) |
| 126 | self.assertEquals((-1, 1), _components(F(1, -1))) |
| 127 | self.assertEquals((1, 1), _components(F(-2, -2))) |
| 128 | self.assertEquals((1, 2), _components(F(5, 10))) |
| 129 | self.assertEquals((7, 15), _components(F(7, 15))) |
| 130 | self.assertEquals((10**23, 1), _components(F(10**23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 131 | |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 132 | self.assertEquals((3, 77), _components(F(F(3, 7), 11))) |
| 133 | self.assertEquals((-9, 5), _components(F(2, F(-10, 9)))) |
| 134 | self.assertEquals((2486, 2485), _components(F(F(22, 7), F(355, 113)))) |
| 135 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 136 | self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 137 | F, 12, 0) |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 138 | self.assertRaises(TypeError, F, 1.5 + 3j) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 139 | |
Georg Brandl | 86b2fb9 | 2008-07-16 03:43:04 +0000 | [diff] [blame] | 140 | self.assertRaises(TypeError, F, "3/2", 3) |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 141 | self.assertRaises(TypeError, F, 3, 0j) |
| 142 | self.assertRaises(TypeError, F, 3, 1j) |
| 143 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 144 | @requires_IEEE_754 |
| 145 | def testInitFromFloat(self): |
| 146 | self.assertEquals((5, 2), _components(F(2.5))) |
| 147 | self.assertEquals((0, 1), _components(F(-0.0))) |
| 148 | self.assertEquals((3602879701896397, 36028797018963968), |
| 149 | _components(F(0.1))) |
| 150 | self.assertRaises(TypeError, F, float('nan')) |
| 151 | self.assertRaises(TypeError, F, float('inf')) |
| 152 | self.assertRaises(TypeError, F, float('-inf')) |
| 153 | |
| 154 | def testInitFromDecimal(self): |
| 155 | self.assertEquals((11, 10), |
| 156 | _components(F(Decimal('1.1')))) |
| 157 | self.assertEquals((7, 200), |
| 158 | _components(F(Decimal('3.5e-2')))) |
| 159 | self.assertEquals((0, 1), |
| 160 | _components(F(Decimal('.000e20')))) |
| 161 | self.assertRaises(TypeError, F, Decimal('nan')) |
| 162 | self.assertRaises(TypeError, F, Decimal('snan')) |
| 163 | self.assertRaises(TypeError, F, Decimal('inf')) |
| 164 | self.assertRaises(TypeError, F, Decimal('-inf')) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 165 | |
| 166 | def testFromString(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 167 | self.assertEquals((5, 1), _components(F("5"))) |
| 168 | self.assertEquals((3, 2), _components(F("3/2"))) |
| 169 | self.assertEquals((3, 2), _components(F(" \n +3/2"))) |
| 170 | self.assertEquals((-3, 2), _components(F("-3/2 "))) |
| 171 | self.assertEquals((13, 2), _components(F(" 013/02 \n "))) |
| 172 | self.assertEquals((16, 5), _components(F(" 3.2 "))) |
| 173 | self.assertEquals((-16, 5), _components(F(" -3.2 "))) |
| 174 | self.assertEquals((-3, 1), _components(F(" -3. "))) |
| 175 | self.assertEquals((3, 5), _components(F(" .6 "))) |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 176 | self.assertEquals((1, 3125), _components(F("32.e-5"))) |
| 177 | self.assertEquals((1000000, 1), _components(F("1E+06"))) |
| 178 | self.assertEquals((-12300, 1), _components(F("-1.23e4"))) |
| 179 | self.assertEquals((0, 1), _components(F(" .0e+0\t"))) |
| 180 | self.assertEquals((0, 1), _components(F("-0.000e0"))) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 181 | |
| 182 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 183 | ZeroDivisionError, "Fraction(3, 0)", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 184 | F, "3/0") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 185 | self.assertRaisesMessage( |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 186 | ValueError, "Invalid literal for Fraction: '3/'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 187 | F, "3/") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 188 | self.assertRaisesMessage( |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 189 | ValueError, "Invalid literal for Fraction: '/2'", |
| 190 | F, "/2") |
| 191 | self.assertRaisesMessage( |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 192 | ValueError, "Invalid literal for Fraction: '3 /2'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 193 | F, "3 /2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 194 | self.assertRaisesMessage( |
| 195 | # Denominators don't need a sign. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 196 | ValueError, "Invalid literal for Fraction: '3/+2'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 197 | F, "3/+2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 198 | self.assertRaisesMessage( |
| 199 | # Imitate float's parsing. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 200 | ValueError, "Invalid literal for Fraction: '+ 3/2'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 201 | F, "+ 3/2") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 202 | self.assertRaisesMessage( |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 203 | # Avoid treating '.' as a regex special character. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 204 | ValueError, "Invalid literal for Fraction: '3a2'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 205 | F, "3a2") |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 206 | self.assertRaisesMessage( |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 207 | # Don't accept combinations of decimals and rationals. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 208 | ValueError, "Invalid literal for Fraction: '3/7.2'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 209 | F, "3/7.2") |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 210 | self.assertRaisesMessage( |
| 211 | # Don't accept combinations of decimals and rationals. |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 212 | ValueError, "Invalid literal for Fraction: '3.2/7'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 213 | F, "3.2/7") |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 214 | self.assertRaisesMessage( |
| 215 | # Allow 3. and .3, but not . |
Benjamin Peterson | dcf97b9 | 2008-07-02 17:30:14 +0000 | [diff] [blame] | 216 | ValueError, "Invalid literal for Fraction: '.'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 217 | F, ".") |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 218 | |
| 219 | def testImmutable(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 220 | r = F(7, 3) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 221 | r.__init__(2, 15) |
| 222 | self.assertEquals((7, 3), _components(r)) |
| 223 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 224 | self.assertRaises(AttributeError, setattr, r, 'numerator', 12) |
| 225 | self.assertRaises(AttributeError, setattr, r, 'denominator', 6) |
| 226 | self.assertEquals((7, 3), _components(r)) |
| 227 | |
| 228 | # But if you _really_ need to: |
| 229 | r._numerator = 4 |
| 230 | r._denominator = 2 |
| 231 | self.assertEquals((4, 2), _components(r)) |
| 232 | # Which breaks some important operations: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 233 | self.assertNotEquals(F(4, 2), r) |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 234 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 235 | def testFromFloat(self): |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 236 | self.assertRaises(TypeError, F.from_float, 3+4j) |
| 237 | self.assertEquals((10, 1), _components(F.from_float(10))) |
Georg Brandl | 3a9b062 | 2009-01-03 22:07:57 +0000 | [diff] [blame] | 238 | bigint = 1234567890123456789 |
| 239 | self.assertEquals((bigint, 1), _components(F.from_float(bigint))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 240 | self.assertEquals((0, 1), _components(F.from_float(-0.0))) |
| 241 | self.assertEquals((10, 1), _components(F.from_float(10.0))) |
| 242 | self.assertEquals((-5, 2), _components(F.from_float(-2.5))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 243 | self.assertEquals((99999999999999991611392, 1), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 244 | _components(F.from_float(1e23))) |
| 245 | self.assertEquals(float(10**23), float(F.from_float(1e23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 246 | self.assertEquals((3602879701896397, 1125899906842624), |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 247 | _components(F.from_float(3.2))) |
| 248 | self.assertEquals(3.2, float(F.from_float(3.2))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 249 | |
| 250 | inf = 1e1000 |
| 251 | nan = inf - inf |
| 252 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 253 | TypeError, "Cannot convert inf to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 254 | F.from_float, inf) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 255 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 256 | TypeError, "Cannot convert -inf to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 257 | F.from_float, -inf) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 258 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 259 | TypeError, "Cannot convert nan to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 260 | F.from_float, nan) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 261 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 262 | def testFromDecimal(self): |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 263 | self.assertRaises(TypeError, F.from_decimal, 3+4j) |
| 264 | self.assertEquals(F(10, 1), F.from_decimal(10)) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 265 | self.assertEquals(F(0), F.from_decimal(Decimal("-0"))) |
| 266 | self.assertEquals(F(5, 10), F.from_decimal(Decimal("0.5"))) |
| 267 | self.assertEquals(F(5, 1000), F.from_decimal(Decimal("5e-3"))) |
| 268 | self.assertEquals(F(5000), F.from_decimal(Decimal("5e3"))) |
| 269 | self.assertEquals(1 - F(1, 10**30), |
| 270 | F.from_decimal(Decimal("0." + "9" * 30))) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 271 | |
| 272 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 273 | TypeError, "Cannot convert Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 274 | F.from_decimal, Decimal("inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 275 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 276 | TypeError, "Cannot convert -Infinity to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 277 | F.from_decimal, Decimal("-inf")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 278 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 279 | TypeError, "Cannot convert NaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 280 | F.from_decimal, Decimal("nan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 281 | self.assertRaisesMessage( |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 282 | TypeError, "Cannot convert sNaN to Fraction.", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 283 | F.from_decimal, Decimal("snan")) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 284 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 285 | def testLimitDenominator(self): |
| 286 | rpi = F('3.1415926535897932') |
| 287 | self.assertEqual(rpi.limit_denominator(10000), F(355, 113)) |
| 288 | self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113)) |
| 289 | self.assertEqual(rpi.limit_denominator(113), F(355, 113)) |
| 290 | self.assertEqual(rpi.limit_denominator(112), F(333, 106)) |
| 291 | self.assertEqual(F(201, 200).limit_denominator(100), F(1)) |
| 292 | self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101)) |
| 293 | self.assertEqual(F(0).limit_denominator(10000), F(0)) |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 294 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 295 | def testConversions(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 296 | self.assertTypedEquals(-1, math.trunc(F(-11, 10))) |
| 297 | self.assertTypedEquals(-2, math.floor(F(-11, 10))) |
| 298 | self.assertTypedEquals(-1, math.ceil(F(-11, 10))) |
| 299 | self.assertTypedEquals(-1, math.ceil(F(-10, 10))) |
| 300 | self.assertTypedEquals(-1, int(F(-11, 10))) |
| 301 | self.assertTypedEquals(0, round(F(-1, 10))) |
| 302 | self.assertTypedEquals(0, round(F(-5, 10))) |
| 303 | self.assertTypedEquals(-2, round(F(-15, 10))) |
| 304 | self.assertTypedEquals(-1, round(F(-7, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 305 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 306 | self.assertEquals(False, bool(F(0, 1))) |
| 307 | self.assertEquals(True, bool(F(3, 2))) |
| 308 | self.assertTypedEquals(0.1, float(F(1, 10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 309 | |
| 310 | # Check that __float__ isn't implemented by converting the |
| 311 | # numerator and denominator to float before dividing. |
| 312 | self.assertRaises(OverflowError, float, int('2'*400+'7')) |
| 313 | self.assertAlmostEquals(2.0/3, |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 314 | float(F(int('2'*400+'7'), int('3'*400+'1')))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 315 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 316 | self.assertTypedEquals(0.1+0j, complex(F(1,10))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 317 | |
| 318 | def testRound(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 319 | self.assertTypedEquals(F(-200), round(F(-150), -2)) |
| 320 | self.assertTypedEquals(F(-200), round(F(-250), -2)) |
| 321 | self.assertTypedEquals(F(30), round(F(26), -1)) |
| 322 | self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1)) |
| 323 | self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 324 | |
| 325 | |
| 326 | def testArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 327 | self.assertEquals(F(1, 2), F(1, 10) + F(2, 5)) |
| 328 | self.assertEquals(F(-3, 10), F(1, 10) - F(2, 5)) |
| 329 | self.assertEquals(F(1, 25), F(1, 10) * F(2, 5)) |
| 330 | self.assertEquals(F(1, 4), F(1, 10) / F(2, 5)) |
| 331 | self.assertTypedEquals(2, F(9, 10) // F(2, 5)) |
| 332 | self.assertTypedEquals(10**23, F(10**23, 1) // F(1)) |
| 333 | self.assertEquals(F(2, 3), F(-7, 3) % F(3, 2)) |
| 334 | self.assertEquals(F(8, 27), F(2, 3) ** F(3)) |
| 335 | self.assertEquals(F(27, 8), F(2, 3) ** F(-3)) |
| 336 | self.assertTypedEquals(2.0, F(4) ** F(1, 2)) |
| 337 | z = pow(F(-1), F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 338 | self.assertAlmostEquals(z.real, 0) |
| 339 | self.assertEquals(z.imag, 1) |
| 340 | |
| 341 | def testMixedArithmetic(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 342 | self.assertTypedEquals(F(11, 10), F(1, 10) + 1) |
| 343 | self.assertTypedEquals(1.1, F(1, 10) + 1.0) |
| 344 | self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j)) |
| 345 | self.assertTypedEquals(F(11, 10), 1 + F(1, 10)) |
| 346 | self.assertTypedEquals(1.1, 1.0 + F(1, 10)) |
| 347 | self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 348 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 349 | self.assertTypedEquals(F(-9, 10), F(1, 10) - 1) |
| 350 | self.assertTypedEquals(-0.9, F(1, 10) - 1.0) |
| 351 | self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j)) |
| 352 | self.assertTypedEquals(F(9, 10), 1 - F(1, 10)) |
| 353 | self.assertTypedEquals(0.9, 1.0 - F(1, 10)) |
| 354 | self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 355 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 356 | self.assertTypedEquals(F(1, 10), F(1, 10) * 1) |
| 357 | self.assertTypedEquals(0.1, F(1, 10) * 1.0) |
| 358 | self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j)) |
| 359 | self.assertTypedEquals(F(1, 10), 1 * F(1, 10)) |
| 360 | self.assertTypedEquals(0.1, 1.0 * F(1, 10)) |
| 361 | self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 362 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 363 | self.assertTypedEquals(F(1, 10), F(1, 10) / 1) |
| 364 | self.assertTypedEquals(0.1, F(1, 10) / 1.0) |
| 365 | self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j)) |
| 366 | self.assertTypedEquals(F(10, 1), 1 / F(1, 10)) |
| 367 | self.assertTypedEquals(10.0, 1.0 / F(1, 10)) |
| 368 | self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 369 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 370 | self.assertTypedEquals(0, F(1, 10) // 1) |
| 371 | self.assertTypedEquals(0, F(1, 10) // 1.0) |
| 372 | self.assertTypedEquals(10, 1 // F(1, 10)) |
| 373 | self.assertTypedEquals(10**23, 10**22 // F(1, 10)) |
| 374 | self.assertTypedEquals(10, 1.0 // F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 375 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 376 | self.assertTypedEquals(F(1, 10), F(1, 10) % 1) |
| 377 | self.assertTypedEquals(0.1, F(1, 10) % 1.0) |
| 378 | self.assertTypedEquals(F(0, 1), 1 % F(1, 10)) |
| 379 | self.assertTypedEquals(0.0, 1.0 % F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 380 | |
| 381 | # No need for divmod since we don't override it. |
| 382 | |
| 383 | # ** has more interesting conversion rules. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 384 | self.assertTypedEquals(F(100, 1), F(1, 10) ** -2) |
| 385 | self.assertTypedEquals(F(100, 1), F(10, 1) ** 2) |
| 386 | self.assertTypedEquals(0.1, F(1, 10) ** 1.0) |
| 387 | self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j)) |
| 388 | self.assertTypedEquals(4 , 2 ** F(2, 1)) |
| 389 | z = pow(-1, F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 390 | self.assertAlmostEquals(0, z.real) |
| 391 | self.assertEquals(1, z.imag) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 392 | self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1)) |
| 393 | self.assertTypedEquals(2.0 , 4 ** F(1, 2)) |
| 394 | self.assertTypedEquals(0.25, 2.0 ** F(-2, 1)) |
| 395 | self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 396 | |
| 397 | def testMixingWithDecimal(self): |
Mark Dickinson | 08ade6f | 2010-06-11 10:44:52 +0000 | [diff] [blame] | 398 | # Decimal refuses mixed arithmetic (but not mixed comparisons) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 399 | self.assertRaisesMessage( |
| 400 | TypeError, |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 401 | "unsupported operand type(s) for +: 'Fraction' and 'Decimal'", |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 402 | operator.add, F(3,11), Decimal('3.1415926')) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 403 | |
| 404 | def testComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 405 | self.assertTrue(F(1, 2) < F(2, 3)) |
| 406 | self.assertFalse(F(1, 2) < F(1, 2)) |
| 407 | self.assertTrue(F(1, 2) <= F(2, 3)) |
| 408 | self.assertTrue(F(1, 2) <= F(1, 2)) |
| 409 | self.assertFalse(F(2, 3) <= F(1, 2)) |
| 410 | self.assertTrue(F(1, 2) == F(1, 2)) |
| 411 | self.assertFalse(F(1, 2) == F(1, 3)) |
| 412 | self.assertFalse(F(1, 2) != F(1, 2)) |
| 413 | self.assertTrue(F(1, 2) != F(1, 3)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 414 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 415 | def testComparisonsDummyRational(self): |
| 416 | self.assertTrue(F(1, 2) == DummyRational(1, 2)) |
| 417 | self.assertTrue(DummyRational(1, 2) == F(1, 2)) |
| 418 | self.assertFalse(F(1, 2) == DummyRational(3, 4)) |
| 419 | self.assertFalse(DummyRational(3, 4) == F(1, 2)) |
| 420 | |
| 421 | self.assertTrue(F(1, 2) < DummyRational(3, 4)) |
| 422 | self.assertFalse(F(1, 2) < DummyRational(1, 2)) |
| 423 | self.assertFalse(F(1, 2) < DummyRational(1, 7)) |
| 424 | self.assertFalse(F(1, 2) > DummyRational(3, 4)) |
| 425 | self.assertFalse(F(1, 2) > DummyRational(1, 2)) |
| 426 | self.assertTrue(F(1, 2) > DummyRational(1, 7)) |
| 427 | self.assertTrue(F(1, 2) <= DummyRational(3, 4)) |
| 428 | self.assertTrue(F(1, 2) <= DummyRational(1, 2)) |
| 429 | self.assertFalse(F(1, 2) <= DummyRational(1, 7)) |
| 430 | self.assertFalse(F(1, 2) >= DummyRational(3, 4)) |
| 431 | self.assertTrue(F(1, 2) >= DummyRational(1, 2)) |
| 432 | self.assertTrue(F(1, 2) >= DummyRational(1, 7)) |
| 433 | |
| 434 | self.assertTrue(DummyRational(1, 2) < F(3, 4)) |
| 435 | self.assertFalse(DummyRational(1, 2) < F(1, 2)) |
| 436 | self.assertFalse(DummyRational(1, 2) < F(1, 7)) |
| 437 | self.assertFalse(DummyRational(1, 2) > F(3, 4)) |
| 438 | self.assertFalse(DummyRational(1, 2) > F(1, 2)) |
| 439 | self.assertTrue(DummyRational(1, 2) > F(1, 7)) |
| 440 | self.assertTrue(DummyRational(1, 2) <= F(3, 4)) |
| 441 | self.assertTrue(DummyRational(1, 2) <= F(1, 2)) |
| 442 | self.assertFalse(DummyRational(1, 2) <= F(1, 7)) |
| 443 | self.assertFalse(DummyRational(1, 2) >= F(3, 4)) |
| 444 | self.assertTrue(DummyRational(1, 2) >= F(1, 2)) |
| 445 | self.assertTrue(DummyRational(1, 2) >= F(1, 7)) |
| 446 | |
| 447 | def testComparisonsDummyFloat(self): |
| 448 | x = DummyFloat(1./3.) |
| 449 | y = F(1, 3) |
| 450 | self.assertTrue(x != y) |
| 451 | self.assertTrue(x < y or x > y) |
| 452 | self.assertFalse(x == y) |
| 453 | self.assertFalse(x <= y and x >= y) |
| 454 | self.assertTrue(y != x) |
| 455 | self.assertTrue(y < x or y > x) |
| 456 | self.assertFalse(y == x) |
| 457 | self.assertFalse(y <= x and y >= x) |
| 458 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 459 | def testMixedLess(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 460 | self.assertTrue(2 < F(5, 2)) |
| 461 | self.assertFalse(2 < F(4, 2)) |
| 462 | self.assertTrue(F(5, 2) < 3) |
| 463 | self.assertFalse(F(4, 2) < 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 464 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 465 | self.assertTrue(F(1, 2) < 0.6) |
| 466 | self.assertFalse(F(1, 2) < 0.4) |
| 467 | self.assertTrue(0.4 < F(1, 2)) |
| 468 | self.assertFalse(0.5 < F(1, 2)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 469 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 470 | self.assertFalse(float('inf') < F(1, 2)) |
| 471 | self.assertTrue(float('-inf') < F(0, 10)) |
| 472 | self.assertFalse(float('nan') < F(-3, 7)) |
| 473 | self.assertTrue(F(1, 2) < float('inf')) |
| 474 | self.assertFalse(F(17, 12) < float('-inf')) |
| 475 | self.assertFalse(F(144, -89) < float('nan')) |
| 476 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 477 | def testMixedLessEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 478 | self.assertTrue(0.5 <= F(1, 2)) |
| 479 | self.assertFalse(0.6 <= F(1, 2)) |
| 480 | self.assertTrue(F(1, 2) <= 0.5) |
| 481 | self.assertFalse(F(1, 2) <= 0.4) |
| 482 | self.assertTrue(2 <= F(4, 2)) |
| 483 | self.assertFalse(2 <= F(3, 2)) |
| 484 | self.assertTrue(F(4, 2) <= 2) |
| 485 | self.assertFalse(F(5, 2) <= 2) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 486 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 487 | self.assertFalse(float('inf') <= F(1, 2)) |
| 488 | self.assertTrue(float('-inf') <= F(0, 10)) |
| 489 | self.assertFalse(float('nan') <= F(-3, 7)) |
| 490 | self.assertTrue(F(1, 2) <= float('inf')) |
| 491 | self.assertFalse(F(17, 12) <= float('-inf')) |
| 492 | self.assertFalse(F(144, -89) <= float('nan')) |
| 493 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 494 | def testBigFloatComparisons(self): |
| 495 | # Because 10**23 can't be represented exactly as a float: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 496 | self.assertFalse(F(10**23) == float(10**23)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 497 | # The first test demonstrates why these are important. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 498 | self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1))) |
| 499 | self.assertTrue(1e23 < F(math.trunc(1e23) + 1)) |
| 500 | self.assertFalse(1e23 <= F(math.trunc(1e23) - 1)) |
| 501 | self.assertTrue(1e23 > F(math.trunc(1e23) - 1)) |
| 502 | self.assertFalse(1e23 >= F(math.trunc(1e23) + 1)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 503 | |
| 504 | def testBigComplexComparisons(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 505 | self.assertFalse(F(10**23) == complex(10**23)) |
Mark Dickinson | 327f02c | 2010-03-27 11:11:13 +0000 | [diff] [blame] | 506 | self.assertRaises(TypeError, operator.gt, F(10**23), complex(10**23)) |
| 507 | self.assertRaises(TypeError, operator.le, F(10**23), complex(10**23)) |
| 508 | |
| 509 | x = F(3, 8) |
| 510 | z = complex(0.375, 0.0) |
| 511 | w = complex(0.375, 0.2) |
| 512 | self.assertTrue(x == z) |
| 513 | self.assertFalse(x != z) |
| 514 | self.assertFalse(x == w) |
| 515 | self.assertTrue(x != w) |
| 516 | for op in operator.lt, operator.le, operator.gt, operator.ge: |
| 517 | self.assertRaises(TypeError, op, x, z) |
| 518 | self.assertRaises(TypeError, op, z, x) |
| 519 | self.assertRaises(TypeError, op, x, w) |
| 520 | self.assertRaises(TypeError, op, w, x) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 521 | |
| 522 | def testMixedEqual(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 523 | self.assertTrue(0.5 == F(1, 2)) |
| 524 | self.assertFalse(0.6 == F(1, 2)) |
| 525 | self.assertTrue(F(1, 2) == 0.5) |
| 526 | self.assertFalse(F(1, 2) == 0.4) |
| 527 | self.assertTrue(2 == F(4, 2)) |
| 528 | self.assertFalse(2 == F(3, 2)) |
| 529 | self.assertTrue(F(4, 2) == 2) |
| 530 | self.assertFalse(F(5, 2) == 2) |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 531 | self.assertFalse(F(5, 2) == float('nan')) |
| 532 | self.assertFalse(float('nan') == F(3, 7)) |
| 533 | self.assertFalse(F(5, 2) == float('inf')) |
| 534 | self.assertFalse(float('-inf') == F(2, 5)) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 535 | |
| 536 | def testStringification(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 537 | self.assertEquals("Fraction(7, 3)", repr(F(7, 3))) |
Benjamin Peterson | 4118174 | 2008-07-02 20:22:54 +0000 | [diff] [blame] | 538 | self.assertEquals("Fraction(6283185307, 2000000000)", |
| 539 | repr(F('3.1415926535'))) |
| 540 | self.assertEquals("Fraction(-1, 100000000000000000000)", |
| 541 | repr(F(1, -10**20))) |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 542 | self.assertEquals("7/3", str(F(7, 3))) |
| 543 | self.assertEquals("7", str(F(7, 1))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 544 | |
| 545 | def testHash(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 546 | self.assertEquals(hash(2.5), hash(F(5, 2))) |
| 547 | self.assertEquals(hash(10**50), hash(F(10**50))) |
| 548 | self.assertNotEquals(hash(float(10**23)), hash(F(10**23))) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 549 | |
| 550 | def testApproximatePi(self): |
| 551 | # Algorithm borrowed from |
| 552 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 553 | three = F(3) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 554 | 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] | 555 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 556 | lasts = s |
| 557 | n, na = n+na, na+8 |
| 558 | d, da = d+da, da+32 |
| 559 | t = (t * n) / d |
| 560 | s += t |
| 561 | self.assertAlmostEquals(math.pi, s) |
| 562 | |
| 563 | def testApproximateCos1(self): |
| 564 | # Algorithm borrowed from |
| 565 | # http://docs.python.org/lib/decimal-recipes.html |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 566 | x = F(1) |
| 567 | i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1 |
| 568 | while abs(s - lasts) > F(1, 10**9): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 569 | lasts = s |
| 570 | i += 2 |
| 571 | fact *= i * (i-1) |
| 572 | num *= x * x |
| 573 | sign *= -1 |
| 574 | s += num / fact * sign |
| 575 | self.assertAlmostEquals(math.cos(1), s) |
| 576 | |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 577 | def test_copy_deepcopy_pickle(self): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 578 | r = F(13, 7) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 579 | self.assertEqual(r, loads(dumps(r))) |
| 580 | self.assertEqual(id(r), id(copy(r))) |
| 581 | self.assertEqual(id(r), id(deepcopy(r))) |
| 582 | |
Mark Dickinson | c28ad27 | 2009-02-12 17:58:36 +0000 | [diff] [blame] | 583 | def test_slots(self): |
| 584 | # Issue 4998 |
| 585 | r = F(13, 7) |
| 586 | self.assertRaises(AttributeError, setattr, r, 'a', 10) |
| 587 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 588 | def test_main(): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 589 | run_unittest(FractionTest, GcdTest) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 590 | |
| 591 | if __name__ == '__main__': |
| 592 | test_main() |