blob: 3c8da77ae8b0d1a511c44b2b5c5c5829c6f7d479 [file] [log] [blame]
Christian Heimes3feef612008-02-11 06:19:17 +00001"""Tests for Lib/fractions.py."""
Guido van Rossum7736b5b2008-01-15 21:44:53 +00002
3from decimal import Decimal
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test.support import run_unittest
Guido van Rossum7736b5b2008-01-15 21:44:53 +00005import math
6import operator
Christian Heimes3feef612008-02-11 06:19:17 +00007import fractions
Guido van Rossum7736b5b2008-01-15 21:44:53 +00008import unittest
Christian Heimes969fe572008-01-25 11:23:10 +00009from copy import copy, deepcopy
Neal Norwitz6ff93fe2008-01-26 21:52:30 +000010from pickle import dumps, loads
Christian Heimes68f5fbe2008-02-14 08:27:37 +000011F = fractions.Fraction
Christian Heimes3feef612008-02-11 06:19:17 +000012gcd = fractions.gcd
Christian Heimesaf98da12008-01-27 15:18:18 +000013
14
15class 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 Rossum7736b5b2008-01-15 21:44:53 +000029
30def _components(r):
31 return (r.numerator, r.denominator)
32
Christian Heimesaf98da12008-01-27 15:18:18 +000033
Christian Heimes3feef612008-02-11 06:19:17 +000034class FractionTest(unittest.TestCase):
Guido van Rossum7736b5b2008-01-15 21:44:53 +000035
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 Heimes68f5fbe2008-02-14 08:27:37 +000052 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 Rossum7736b5b2008-01-15 21:44:53 +000055
Christian Heimes68f5fbe2008-02-14 08:27:37 +000056 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 Rossum7736b5b2008-01-15 21:44:53 +000062
Christian Heimes3feef612008-02-11 06:19:17 +000063 self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000064 F, 12, 0)
65 self.assertRaises(AttributeError, F, 1.5)
66 self.assertRaises(AttributeError, F, 1.5 + 3j)
Guido van Rossum7736b5b2008-01-15 21:44:53 +000067
Christian Heimes68f5fbe2008-02-14 08:27:37 +000068 self.assertRaises(AttributeError, F, F(1, 2), 3)
69 self.assertRaises(AttributeError, F, "3/2", 3)
Christian Heimes587c2bf2008-01-19 16:21:02 +000070
71 def testFromString(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +000072 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 Heimes587c2bf2008-01-19 16:21:02 +000081
82 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +000083 ZeroDivisionError, "Fraction(3, 0)",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000084 F, "3/0")
Christian Heimes587c2bf2008-01-19 16:21:02 +000085 self.assertRaisesMessage(
Benjamin Petersondcf97b92008-07-02 17:30:14 +000086 ValueError, "Invalid literal for Fraction: '3/'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000087 F, "3/")
Christian Heimes587c2bf2008-01-19 16:21:02 +000088 self.assertRaisesMessage(
Benjamin Petersondcf97b92008-07-02 17:30:14 +000089 ValueError, "Invalid literal for Fraction: '3 /2'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000090 F, "3 /2")
Christian Heimes587c2bf2008-01-19 16:21:02 +000091 self.assertRaisesMessage(
92 # Denominators don't need a sign.
Benjamin Petersondcf97b92008-07-02 17:30:14 +000093 ValueError, "Invalid literal for Fraction: '3/+2'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000094 F, "3/+2")
Christian Heimes587c2bf2008-01-19 16:21:02 +000095 self.assertRaisesMessage(
96 # Imitate float's parsing.
Benjamin Petersondcf97b92008-07-02 17:30:14 +000097 ValueError, "Invalid literal for Fraction: '+ 3/2'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +000098 F, "+ 3/2")
Christian Heimes587c2bf2008-01-19 16:21:02 +000099 self.assertRaisesMessage(
Christian Heimesaf98da12008-01-27 15:18:18 +0000100 # Avoid treating '.' as a regex special character.
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000101 ValueError, "Invalid literal for Fraction: '3a2'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000102 F, "3a2")
Christian Heimesaf98da12008-01-27 15:18:18 +0000103 self.assertRaisesMessage(
104 # Only parse ordinary decimals, not scientific form.
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000105 ValueError, "Invalid literal for Fraction: '3.2e4'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000106 F, "3.2e4")
Christian Heimesaf98da12008-01-27 15:18:18 +0000107 self.assertRaisesMessage(
108 # Don't accept combinations of decimals and rationals.
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000109 ValueError, "Invalid literal for Fraction: '3/7.2'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000110 F, "3/7.2")
Christian Heimesaf98da12008-01-27 15:18:18 +0000111 self.assertRaisesMessage(
112 # Don't accept combinations of decimals and rationals.
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000113 ValueError, "Invalid literal for Fraction: '3.2/7'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000114 F, "3.2/7")
Christian Heimes292d3512008-02-03 16:51:08 +0000115 self.assertRaisesMessage(
116 # Allow 3. and .3, but not .
Benjamin Petersondcf97b92008-07-02 17:30:14 +0000117 ValueError, "Invalid literal for Fraction: '.'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000118 F, ".")
Christian Heimes587c2bf2008-01-19 16:21:02 +0000119
120 def testImmutable(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000121 r = F(7, 3)
Christian Heimes587c2bf2008-01-19 16:21:02 +0000122 r.__init__(2, 15)
123 self.assertEquals((7, 3), _components(r))
124
Christian Heimes400adb02008-02-01 08:12:03 +0000125 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 Heimes68f5fbe2008-02-14 08:27:37 +0000134 self.assertNotEquals(F(4, 2), r)
Christian Heimes400adb02008-02-01 08:12:03 +0000135
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000136 def testFromFloat(self):
137 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000138 TypeError, "Fraction.from_float() only takes floats, not 3 (int)",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000139 F.from_float, 3)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000140
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000141 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 Rossum7736b5b2008-01-15 21:44:53 +0000144 self.assertEquals((99999999999999991611392, 1),
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000145 _components(F.from_float(1e23)))
146 self.assertEquals(float(10**23), float(F.from_float(1e23)))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000147 self.assertEquals((3602879701896397, 1125899906842624),
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000148 _components(F.from_float(3.2)))
149 self.assertEquals(3.2, float(F.from_float(3.2)))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000150
151 inf = 1e1000
152 nan = inf - inf
153 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000154 TypeError, "Cannot convert inf to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000155 F.from_float, inf)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000156 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000157 TypeError, "Cannot convert -inf to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000158 F.from_float, -inf)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000159 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000160 TypeError, "Cannot convert nan to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000161 F.from_float, nan)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000162
Christian Heimes587c2bf2008-01-19 16:21:02 +0000163 def testFromDecimal(self):
164 self.assertRaisesMessage(
165 TypeError,
Christian Heimes3feef612008-02-11 06:19:17 +0000166 "Fraction.from_decimal() only takes Decimals, not 3 (int)",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000167 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 Heimes587c2bf2008-01-19 16:21:02 +0000174
175 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000176 TypeError, "Cannot convert Infinity to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000177 F.from_decimal, Decimal("inf"))
Christian Heimes587c2bf2008-01-19 16:21:02 +0000178 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000179 TypeError, "Cannot convert -Infinity to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000180 F.from_decimal, Decimal("-inf"))
Christian Heimes587c2bf2008-01-19 16:21:02 +0000181 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000182 TypeError, "Cannot convert NaN to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000183 F.from_decimal, Decimal("nan"))
Christian Heimes587c2bf2008-01-19 16:21:02 +0000184 self.assertRaisesMessage(
Christian Heimes3feef612008-02-11 06:19:17 +0000185 TypeError, "Cannot convert sNaN to Fraction.",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000186 F.from_decimal, Decimal("snan"))
Christian Heimes587c2bf2008-01-19 16:21:02 +0000187
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000188 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 Heimesbbffeb62008-01-24 09:42:52 +0000197
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000198 def testConversions(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000199 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 Rossum7736b5b2008-01-15 21:44:53 +0000208
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000209 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 Rossum7736b5b2008-01-15 21:44:53 +0000212
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 Heimes68f5fbe2008-02-14 08:27:37 +0000217 float(F(int('2'*400+'7'), int('3'*400+'1'))))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000218
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000219 self.assertTypedEquals(0.1+0j, complex(F(1,10)))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000220
221 def testRound(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000222 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 Rossum7736b5b2008-01-15 21:44:53 +0000227
228
229 def testArithmetic(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000230 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 Rossum7736b5b2008-01-15 21:44:53 +0000241 self.assertAlmostEquals(z.real, 0)
242 self.assertEquals(z.imag, 1)
243
244 def testMixedArithmetic(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000245 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 Rossum7736b5b2008-01-15 21:44:53 +0000251
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000252 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 Rossum7736b5b2008-01-15 21:44:53 +0000258
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000259 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 Rossum7736b5b2008-01-15 21:44:53 +0000265
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000266 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 Rossum7736b5b2008-01-15 21:44:53 +0000272
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000273 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 Rossum7736b5b2008-01-15 21:44:53 +0000278
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000279 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 Rossum7736b5b2008-01-15 21:44:53 +0000283
284 # No need for divmod since we don't override it.
285
286 # ** has more interesting conversion rules.
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000287 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 Rossum7736b5b2008-01-15 21:44:53 +0000293 self.assertAlmostEquals(0, z.real)
294 self.assertEquals(1, z.imag)
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000295 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 Rossum7736b5b2008-01-15 21:44:53 +0000299
300 def testMixingWithDecimal(self):
Christian Heimes587c2bf2008-01-19 16:21:02 +0000301 # Decimal refuses mixed comparisons.
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000302 self.assertRaisesMessage(
303 TypeError,
Christian Heimes3feef612008-02-11 06:19:17 +0000304 "unsupported operand type(s) for +: 'Fraction' and 'Decimal'",
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000305 operator.add, F(3,11), Decimal('3.1415926'))
306 self.assertNotEquals(F(5, 2), Decimal('2.5'))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000307
308 def testComparisons(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000309 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 Rossum7736b5b2008-01-15 21:44:53 +0000318
319 def testMixedLess(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000320 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 Rossum7736b5b2008-01-15 21:44:53 +0000324
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000325 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 Rossum7736b5b2008-01-15 21:44:53 +0000329
330 def testMixedLessEqual(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000331 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 Rossum7736b5b2008-01-15 21:44:53 +0000339
340 def testBigFloatComparisons(self):
341 # Because 10**23 can't be represented exactly as a float:
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000342 self.assertFalse(F(10**23) == float(10**23))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000343 # The first test demonstrates why these are important.
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000344 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 Rossum7736b5b2008-01-15 21:44:53 +0000349
350 def testBigComplexComparisons(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000351 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 Rossum7736b5b2008-01-15 21:44:53 +0000354
355 def testMixedEqual(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000356 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 Rossum7736b5b2008-01-15 21:44:53 +0000364
365 def testStringification(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000366 self.assertEquals("Fraction(7, 3)", repr(F(7, 3)))
Benjamin Peterson41181742008-07-02 20:22:54 +0000367 self.assertEquals("Fraction(6283185307, 2000000000)",
368 repr(F('3.1415926535')))
369 self.assertEquals("Fraction(-1, 100000000000000000000)",
370 repr(F(1, -10**20)))
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000371 self.assertEquals("7/3", str(F(7, 3)))
372 self.assertEquals("7", str(F(7, 1)))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000373
374 def testHash(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000375 self.assertEquals(hash(2.5), hash(F(5, 2)))
376 self.assertEquals(hash(10**50), hash(F(10**50)))
377 self.assertNotEquals(hash(float(10**23)), hash(F(10**23)))
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000378
379 def testApproximatePi(self):
380 # Algorithm borrowed from
381 # http://docs.python.org/lib/decimal-recipes.html
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000382 three = F(3)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000383 lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000384 while abs(s - lasts) > F(1, 10**9):
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000385 lasts = s
386 n, na = n+na, na+8
387 d, da = d+da, da+32
388 t = (t * n) / d
389 s += t
390 self.assertAlmostEquals(math.pi, s)
391
392 def testApproximateCos1(self):
393 # Algorithm borrowed from
394 # http://docs.python.org/lib/decimal-recipes.html
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000395 x = F(1)
396 i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1
397 while abs(s - lasts) > F(1, 10**9):
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000398 lasts = s
399 i += 2
400 fact *= i * (i-1)
401 num *= x * x
402 sign *= -1
403 s += num / fact * sign
404 self.assertAlmostEquals(math.cos(1), s)
405
Christian Heimes969fe572008-01-25 11:23:10 +0000406 def test_copy_deepcopy_pickle(self):
Christian Heimes68f5fbe2008-02-14 08:27:37 +0000407 r = F(13, 7)
Christian Heimes969fe572008-01-25 11:23:10 +0000408 self.assertEqual(r, loads(dumps(r)))
409 self.assertEqual(id(r), id(copy(r)))
410 self.assertEqual(id(r), id(deepcopy(r)))
411
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000412def test_main():
Christian Heimes3feef612008-02-11 06:19:17 +0000413 run_unittest(FractionTest, GcdTest)
Guido van Rossum7736b5b2008-01-15 21:44:53 +0000414
415if __name__ == '__main__':
416 test_main()