# Python code coverage for Lib/test/test_fractions.py

# | count | content |
---|---|---|

1 | n/a | """Tests for Lib/fractions.py.""" |

2 | n/a | |

3 | n/a | from decimal import Decimal |

4 | n/a | from test.support import requires_IEEE_754 |

5 | n/a | import math |

6 | n/a | import numbers |

7 | n/a | import operator |

8 | n/a | import fractions |

9 | n/a | import sys |

10 | n/a | import unittest |

11 | n/a | import warnings |

12 | n/a | from copy import copy, deepcopy |

13 | n/a | from pickle import dumps, loads |

14 | n/a | F = fractions.Fraction |

15 | n/a | gcd = fractions.gcd |

16 | n/a | |

17 | n/a | class DummyFloat(object): |

18 | n/a | """Dummy float class for testing comparisons with Fractions""" |

19 | n/a | |

20 | n/a | def __init__(self, value): |

21 | n/a | if not isinstance(value, float): |

22 | n/a | raise TypeError("DummyFloat can only be initialized from float") |

23 | n/a | self.value = value |

24 | n/a | |

25 | n/a | def _richcmp(self, other, op): |

26 | n/a | if isinstance(other, numbers.Rational): |

27 | n/a | return op(F.from_float(self.value), other) |

28 | n/a | elif isinstance(other, DummyFloat): |

29 | n/a | return op(self.value, other.value) |

30 | n/a | else: |

31 | n/a | return NotImplemented |

32 | n/a | |

33 | n/a | def __eq__(self, other): return self._richcmp(other, operator.eq) |

34 | n/a | def __le__(self, other): return self._richcmp(other, operator.le) |

35 | n/a | def __lt__(self, other): return self._richcmp(other, operator.lt) |

36 | n/a | def __ge__(self, other): return self._richcmp(other, operator.ge) |

37 | n/a | def __gt__(self, other): return self._richcmp(other, operator.gt) |

38 | n/a | |

39 | n/a | # shouldn't be calling __float__ at all when doing comparisons |

40 | n/a | def __float__(self): |

41 | n/a | assert False, "__float__ should not be invoked for comparisons" |

42 | n/a | |

43 | n/a | # same goes for subtraction |

44 | n/a | def __sub__(self, other): |

45 | n/a | assert False, "__sub__ should not be invoked for comparisons" |

46 | n/a | __rsub__ = __sub__ |

47 | n/a | |

48 | n/a | |

49 | n/a | class DummyRational(object): |

50 | n/a | """Test comparison of Fraction with a naive rational implementation.""" |

51 | n/a | |

52 | n/a | def __init__(self, num, den): |

53 | n/a | g = math.gcd(num, den) |

54 | n/a | self.num = num // g |

55 | n/a | self.den = den // g |

56 | n/a | |

57 | n/a | def __eq__(self, other): |

58 | n/a | if isinstance(other, fractions.Fraction): |

59 | n/a | return (self.num == other._numerator and |

60 | n/a | self.den == other._denominator) |

61 | n/a | else: |

62 | n/a | return NotImplemented |

63 | n/a | |

64 | n/a | def __lt__(self, other): |

65 | n/a | return(self.num * other._denominator < self.den * other._numerator) |

66 | n/a | |

67 | n/a | def __gt__(self, other): |

68 | n/a | return(self.num * other._denominator > self.den * other._numerator) |

69 | n/a | |

70 | n/a | def __le__(self, other): |

71 | n/a | return(self.num * other._denominator <= self.den * other._numerator) |

72 | n/a | |

73 | n/a | def __ge__(self, other): |

74 | n/a | return(self.num * other._denominator >= self.den * other._numerator) |

75 | n/a | |

76 | n/a | # this class is for testing comparisons; conversion to float |

77 | n/a | # should never be used for a comparison, since it loses accuracy |

78 | n/a | def __float__(self): |

79 | n/a | assert False, "__float__ should not be invoked" |

80 | n/a | |

81 | n/a | class DummyFraction(fractions.Fraction): |

82 | n/a | """Dummy Fraction subclass for copy and deepcopy testing.""" |

83 | n/a | |

84 | n/a | class GcdTest(unittest.TestCase): |

85 | n/a | |

86 | n/a | def testMisc(self): |

87 | n/a | # fractions.gcd() is deprecated |

88 | n/a | with self.assertWarnsRegex(DeprecationWarning, r'fractions\.gcd'): |

89 | n/a | gcd(1, 1) |

90 | n/a | with warnings.catch_warnings(): |

91 | n/a | warnings.filterwarnings('ignore', r'fractions\.gcd', |

92 | n/a | DeprecationWarning) |

93 | n/a | self.assertEqual(0, gcd(0, 0)) |

94 | n/a | self.assertEqual(1, gcd(1, 0)) |

95 | n/a | self.assertEqual(-1, gcd(-1, 0)) |

96 | n/a | self.assertEqual(1, gcd(0, 1)) |

97 | n/a | self.assertEqual(-1, gcd(0, -1)) |

98 | n/a | self.assertEqual(1, gcd(7, 1)) |

99 | n/a | self.assertEqual(-1, gcd(7, -1)) |

100 | n/a | self.assertEqual(1, gcd(-23, 15)) |

101 | n/a | self.assertEqual(12, gcd(120, 84)) |

102 | n/a | self.assertEqual(-12, gcd(84, -120)) |

103 | n/a | self.assertEqual(gcd(120.0, 84), 12.0) |

104 | n/a | self.assertEqual(gcd(120, 84.0), 12.0) |

105 | n/a | self.assertEqual(gcd(F(120), F(84)), F(12)) |

106 | n/a | self.assertEqual(gcd(F(120, 77), F(84, 55)), F(12, 385)) |

107 | n/a | |

108 | n/a | |

109 | n/a | def _components(r): |

110 | n/a | return (r.numerator, r.denominator) |

111 | n/a | |

112 | n/a | |

113 | n/a | class FractionTest(unittest.TestCase): |

114 | n/a | |

115 | n/a | def assertTypedEquals(self, expected, actual): |

116 | n/a | """Asserts that both the types and values are the same.""" |

117 | n/a | self.assertEqual(type(expected), type(actual)) |

118 | n/a | self.assertEqual(expected, actual) |

119 | n/a | |

120 | n/a | def assertRaisesMessage(self, exc_type, message, |

121 | n/a | callable, *args, **kwargs): |

122 | n/a | """Asserts that callable(*args, **kwargs) raises exc_type(message).""" |

123 | n/a | try: |

124 | n/a | callable(*args, **kwargs) |

125 | n/a | except exc_type as e: |

126 | n/a | self.assertEqual(message, str(e)) |

127 | n/a | else: |

128 | n/a | self.fail("%s not raised" % exc_type.__name__) |

129 | n/a | |

130 | n/a | def testInit(self): |

131 | n/a | self.assertEqual((0, 1), _components(F())) |

132 | n/a | self.assertEqual((7, 1), _components(F(7))) |

133 | n/a | self.assertEqual((7, 3), _components(F(F(7, 3)))) |

134 | n/a | |

135 | n/a | self.assertEqual((-1, 1), _components(F(-1, 1))) |

136 | n/a | self.assertEqual((-1, 1), _components(F(1, -1))) |

137 | n/a | self.assertEqual((1, 1), _components(F(-2, -2))) |

138 | n/a | self.assertEqual((1, 2), _components(F(5, 10))) |

139 | n/a | self.assertEqual((7, 15), _components(F(7, 15))) |

140 | n/a | self.assertEqual((10**23, 1), _components(F(10**23))) |

141 | n/a | |

142 | n/a | self.assertEqual((3, 77), _components(F(F(3, 7), 11))) |

143 | n/a | self.assertEqual((-9, 5), _components(F(2, F(-10, 9)))) |

144 | n/a | self.assertEqual((2486, 2485), _components(F(F(22, 7), F(355, 113)))) |

145 | n/a | |

146 | n/a | self.assertRaisesMessage(ZeroDivisionError, "Fraction(12, 0)", |

147 | n/a | F, 12, 0) |

148 | n/a | self.assertRaises(TypeError, F, 1.5 + 3j) |

149 | n/a | |

150 | n/a | self.assertRaises(TypeError, F, "3/2", 3) |

151 | n/a | self.assertRaises(TypeError, F, 3, 0j) |

152 | n/a | self.assertRaises(TypeError, F, 3, 1j) |

153 | n/a | self.assertRaises(TypeError, F, 1, 2, 3) |

154 | n/a | |

155 | n/a | @requires_IEEE_754 |

156 | n/a | def testInitFromFloat(self): |

157 | n/a | self.assertEqual((5, 2), _components(F(2.5))) |

158 | n/a | self.assertEqual((0, 1), _components(F(-0.0))) |

159 | n/a | self.assertEqual((3602879701896397, 36028797018963968), |

160 | n/a | _components(F(0.1))) |

161 | n/a | # bug 16469: error types should be consistent with float -> int |

162 | n/a | self.assertRaises(ValueError, F, float('nan')) |

163 | n/a | self.assertRaises(OverflowError, F, float('inf')) |

164 | n/a | self.assertRaises(OverflowError, F, float('-inf')) |

165 | n/a | |

166 | n/a | def testInitFromDecimal(self): |

167 | n/a | self.assertEqual((11, 10), |

168 | n/a | _components(F(Decimal('1.1')))) |

169 | n/a | self.assertEqual((7, 200), |

170 | n/a | _components(F(Decimal('3.5e-2')))) |

171 | n/a | self.assertEqual((0, 1), |

172 | n/a | _components(F(Decimal('.000e20')))) |

173 | n/a | # bug 16469: error types should be consistent with decimal -> int |

174 | n/a | self.assertRaises(ValueError, F, Decimal('nan')) |

175 | n/a | self.assertRaises(ValueError, F, Decimal('snan')) |

176 | n/a | self.assertRaises(OverflowError, F, Decimal('inf')) |

177 | n/a | self.assertRaises(OverflowError, F, Decimal('-inf')) |

178 | n/a | |

179 | n/a | def testFromString(self): |

180 | n/a | self.assertEqual((5, 1), _components(F("5"))) |

181 | n/a | self.assertEqual((3, 2), _components(F("3/2"))) |

182 | n/a | self.assertEqual((3, 2), _components(F(" \n +3/2"))) |

183 | n/a | self.assertEqual((-3, 2), _components(F("-3/2 "))) |

184 | n/a | self.assertEqual((13, 2), _components(F(" 013/02 \n "))) |

185 | n/a | self.assertEqual((16, 5), _components(F(" 3.2 "))) |

186 | n/a | self.assertEqual((-16, 5), _components(F(" -3.2 "))) |

187 | n/a | self.assertEqual((-3, 1), _components(F(" -3. "))) |

188 | n/a | self.assertEqual((3, 5), _components(F(" .6 "))) |

189 | n/a | self.assertEqual((1, 3125), _components(F("32.e-5"))) |

190 | n/a | self.assertEqual((1000000, 1), _components(F("1E+06"))) |

191 | n/a | self.assertEqual((-12300, 1), _components(F("-1.23e4"))) |

192 | n/a | self.assertEqual((0, 1), _components(F(" .0e+0\t"))) |

193 | n/a | self.assertEqual((0, 1), _components(F("-0.000e0"))) |

194 | n/a | |

195 | n/a | self.assertRaisesMessage( |

196 | n/a | ZeroDivisionError, "Fraction(3, 0)", |

197 | n/a | F, "3/0") |

198 | n/a | self.assertRaisesMessage( |

199 | n/a | ValueError, "Invalid literal for Fraction: '3/'", |

200 | n/a | F, "3/") |

201 | n/a | self.assertRaisesMessage( |

202 | n/a | ValueError, "Invalid literal for Fraction: '/2'", |

203 | n/a | F, "/2") |

204 | n/a | self.assertRaisesMessage( |

205 | n/a | ValueError, "Invalid literal for Fraction: '3 /2'", |

206 | n/a | F, "3 /2") |

207 | n/a | self.assertRaisesMessage( |

208 | n/a | # Denominators don't need a sign. |

209 | n/a | ValueError, "Invalid literal for Fraction: '3/+2'", |

210 | n/a | F, "3/+2") |

211 | n/a | self.assertRaisesMessage( |

212 | n/a | # Imitate float's parsing. |

213 | n/a | ValueError, "Invalid literal for Fraction: '+ 3/2'", |

214 | n/a | F, "+ 3/2") |

215 | n/a | self.assertRaisesMessage( |

216 | n/a | # Avoid treating '.' as a regex special character. |

217 | n/a | ValueError, "Invalid literal for Fraction: '3a2'", |

218 | n/a | F, "3a2") |

219 | n/a | self.assertRaisesMessage( |

220 | n/a | # Don't accept combinations of decimals and rationals. |

221 | n/a | ValueError, "Invalid literal for Fraction: '3/7.2'", |

222 | n/a | F, "3/7.2") |

223 | n/a | self.assertRaisesMessage( |

224 | n/a | # Don't accept combinations of decimals and rationals. |

225 | n/a | ValueError, "Invalid literal for Fraction: '3.2/7'", |

226 | n/a | F, "3.2/7") |

227 | n/a | self.assertRaisesMessage( |

228 | n/a | # Allow 3. and .3, but not . |

229 | n/a | ValueError, "Invalid literal for Fraction: '.'", |

230 | n/a | F, ".") |

231 | n/a | |

232 | n/a | def testImmutable(self): |

233 | n/a | r = F(7, 3) |

234 | n/a | r.__init__(2, 15) |

235 | n/a | self.assertEqual((7, 3), _components(r)) |

236 | n/a | |

237 | n/a | self.assertRaises(AttributeError, setattr, r, 'numerator', 12) |

238 | n/a | self.assertRaises(AttributeError, setattr, r, 'denominator', 6) |

239 | n/a | self.assertEqual((7, 3), _components(r)) |

240 | n/a | |

241 | n/a | # But if you _really_ need to: |

242 | n/a | r._numerator = 4 |

243 | n/a | r._denominator = 2 |

244 | n/a | self.assertEqual((4, 2), _components(r)) |

245 | n/a | # Which breaks some important operations: |

246 | n/a | self.assertNotEqual(F(4, 2), r) |

247 | n/a | |

248 | n/a | def testFromFloat(self): |

249 | n/a | self.assertRaises(TypeError, F.from_float, 3+4j) |

250 | n/a | self.assertEqual((10, 1), _components(F.from_float(10))) |

251 | n/a | bigint = 1234567890123456789 |

252 | n/a | self.assertEqual((bigint, 1), _components(F.from_float(bigint))) |

253 | n/a | self.assertEqual((0, 1), _components(F.from_float(-0.0))) |

254 | n/a | self.assertEqual((10, 1), _components(F.from_float(10.0))) |

255 | n/a | self.assertEqual((-5, 2), _components(F.from_float(-2.5))) |

256 | n/a | self.assertEqual((99999999999999991611392, 1), |

257 | n/a | _components(F.from_float(1e23))) |

258 | n/a | self.assertEqual(float(10**23), float(F.from_float(1e23))) |

259 | n/a | self.assertEqual((3602879701896397, 1125899906842624), |

260 | n/a | _components(F.from_float(3.2))) |

261 | n/a | self.assertEqual(3.2, float(F.from_float(3.2))) |

262 | n/a | |

263 | n/a | inf = 1e1000 |

264 | n/a | nan = inf - inf |

265 | n/a | # bug 16469: error types should be consistent with float -> int |

266 | n/a | self.assertRaisesMessage( |

267 | n/a | OverflowError, "cannot convert Infinity to integer ratio", |

268 | n/a | F.from_float, inf) |

269 | n/a | self.assertRaisesMessage( |

270 | n/a | OverflowError, "cannot convert Infinity to integer ratio", |

271 | n/a | F.from_float, -inf) |

272 | n/a | self.assertRaisesMessage( |

273 | n/a | ValueError, "cannot convert NaN to integer ratio", |

274 | n/a | F.from_float, nan) |

275 | n/a | |

276 | n/a | def testFromDecimal(self): |

277 | n/a | self.assertRaises(TypeError, F.from_decimal, 3+4j) |

278 | n/a | self.assertEqual(F(10, 1), F.from_decimal(10)) |

279 | n/a | self.assertEqual(F(0), F.from_decimal(Decimal("-0"))) |

280 | n/a | self.assertEqual(F(5, 10), F.from_decimal(Decimal("0.5"))) |

281 | n/a | self.assertEqual(F(5, 1000), F.from_decimal(Decimal("5e-3"))) |

282 | n/a | self.assertEqual(F(5000), F.from_decimal(Decimal("5e3"))) |

283 | n/a | self.assertEqual(1 - F(1, 10**30), |

284 | n/a | F.from_decimal(Decimal("0." + "9" * 30))) |

285 | n/a | |

286 | n/a | # bug 16469: error types should be consistent with decimal -> int |

287 | n/a | self.assertRaisesMessage( |

288 | n/a | OverflowError, "cannot convert Infinity to integer ratio", |

289 | n/a | F.from_decimal, Decimal("inf")) |

290 | n/a | self.assertRaisesMessage( |

291 | n/a | OverflowError, "cannot convert Infinity to integer ratio", |

292 | n/a | F.from_decimal, Decimal("-inf")) |

293 | n/a | self.assertRaisesMessage( |

294 | n/a | ValueError, "cannot convert NaN to integer ratio", |

295 | n/a | F.from_decimal, Decimal("nan")) |

296 | n/a | self.assertRaisesMessage( |

297 | n/a | ValueError, "cannot convert NaN to integer ratio", |

298 | n/a | F.from_decimal, Decimal("snan")) |

299 | n/a | |

300 | n/a | def testLimitDenominator(self): |

301 | n/a | rpi = F('3.1415926535897932') |

302 | n/a | self.assertEqual(rpi.limit_denominator(10000), F(355, 113)) |

303 | n/a | self.assertEqual(-rpi.limit_denominator(10000), F(-355, 113)) |

304 | n/a | self.assertEqual(rpi.limit_denominator(113), F(355, 113)) |

305 | n/a | self.assertEqual(rpi.limit_denominator(112), F(333, 106)) |

306 | n/a | self.assertEqual(F(201, 200).limit_denominator(100), F(1)) |

307 | n/a | self.assertEqual(F(201, 200).limit_denominator(101), F(102, 101)) |

308 | n/a | self.assertEqual(F(0).limit_denominator(10000), F(0)) |

309 | n/a | for i in (0, -1): |

310 | n/a | self.assertRaisesMessage( |

311 | n/a | ValueError, "max_denominator should be at least 1", |

312 | n/a | F(1).limit_denominator, i) |

313 | n/a | |

314 | n/a | def testConversions(self): |

315 | n/a | self.assertTypedEquals(-1, math.trunc(F(-11, 10))) |

316 | n/a | self.assertTypedEquals(1, math.trunc(F(11, 10))) |

317 | n/a | self.assertTypedEquals(-2, math.floor(F(-11, 10))) |

318 | n/a | self.assertTypedEquals(-1, math.ceil(F(-11, 10))) |

319 | n/a | self.assertTypedEquals(-1, math.ceil(F(-10, 10))) |

320 | n/a | self.assertTypedEquals(-1, int(F(-11, 10))) |

321 | n/a | self.assertTypedEquals(0, round(F(-1, 10))) |

322 | n/a | self.assertTypedEquals(0, round(F(-5, 10))) |

323 | n/a | self.assertTypedEquals(-2, round(F(-15, 10))) |

324 | n/a | self.assertTypedEquals(-1, round(F(-7, 10))) |

325 | n/a | |

326 | n/a | self.assertEqual(False, bool(F(0, 1))) |

327 | n/a | self.assertEqual(True, bool(F(3, 2))) |

328 | n/a | self.assertTypedEquals(0.1, float(F(1, 10))) |

329 | n/a | |

330 | n/a | # Check that __float__ isn't implemented by converting the |

331 | n/a | # numerator and denominator to float before dividing. |

332 | n/a | self.assertRaises(OverflowError, float, int('2'*400+'7')) |

333 | n/a | self.assertAlmostEqual(2.0/3, |

334 | n/a | float(F(int('2'*400+'7'), int('3'*400+'1')))) |

335 | n/a | |

336 | n/a | self.assertTypedEquals(0.1+0j, complex(F(1,10))) |

337 | n/a | |

338 | n/a | def testRound(self): |

339 | n/a | self.assertTypedEquals(F(-200), round(F(-150), -2)) |

340 | n/a | self.assertTypedEquals(F(-200), round(F(-250), -2)) |

341 | n/a | self.assertTypedEquals(F(30), round(F(26), -1)) |

342 | n/a | self.assertTypedEquals(F(-2, 10), round(F(-15, 100), 1)) |

343 | n/a | self.assertTypedEquals(F(-2, 10), round(F(-25, 100), 1)) |

344 | n/a | |

345 | n/a | def testArithmetic(self): |

346 | n/a | self.assertEqual(F(1, 2), F(1, 10) + F(2, 5)) |

347 | n/a | self.assertEqual(F(-3, 10), F(1, 10) - F(2, 5)) |

348 | n/a | self.assertEqual(F(1, 25), F(1, 10) * F(2, 5)) |

349 | n/a | self.assertEqual(F(1, 4), F(1, 10) / F(2, 5)) |

350 | n/a | self.assertTypedEquals(2, F(9, 10) // F(2, 5)) |

351 | n/a | self.assertTypedEquals(10**23, F(10**23, 1) // F(1)) |

352 | n/a | self.assertEqual(F(2, 3), F(-7, 3) % F(3, 2)) |

353 | n/a | self.assertEqual(F(8, 27), F(2, 3) ** F(3)) |

354 | n/a | self.assertEqual(F(27, 8), F(2, 3) ** F(-3)) |

355 | n/a | self.assertTypedEquals(2.0, F(4) ** F(1, 2)) |

356 | n/a | self.assertEqual(F(1, 1), +F(1, 1)) |

357 | n/a | z = pow(F(-1), F(1, 2)) |

358 | n/a | self.assertAlmostEqual(z.real, 0) |

359 | n/a | self.assertEqual(z.imag, 1) |

360 | n/a | # Regression test for #27539. |

361 | n/a | p = F(-1, 2) ** 0 |

362 | n/a | self.assertEqual(p, F(1, 1)) |

363 | n/a | self.assertEqual(p.numerator, 1) |

364 | n/a | self.assertEqual(p.denominator, 1) |

365 | n/a | p = F(-1, 2) ** -1 |

366 | n/a | self.assertEqual(p, F(-2, 1)) |

367 | n/a | self.assertEqual(p.numerator, -2) |

368 | n/a | self.assertEqual(p.denominator, 1) |

369 | n/a | p = F(-1, 2) ** -2 |

370 | n/a | self.assertEqual(p, F(4, 1)) |

371 | n/a | self.assertEqual(p.numerator, 4) |

372 | n/a | self.assertEqual(p.denominator, 1) |

373 | n/a | |

374 | n/a | def testMixedArithmetic(self): |

375 | n/a | self.assertTypedEquals(F(11, 10), F(1, 10) + 1) |

376 | n/a | self.assertTypedEquals(1.1, F(1, 10) + 1.0) |

377 | n/a | self.assertTypedEquals(1.1 + 0j, F(1, 10) + (1.0 + 0j)) |

378 | n/a | self.assertTypedEquals(F(11, 10), 1 + F(1, 10)) |

379 | n/a | self.assertTypedEquals(1.1, 1.0 + F(1, 10)) |

380 | n/a | self.assertTypedEquals(1.1 + 0j, (1.0 + 0j) + F(1, 10)) |

381 | n/a | |

382 | n/a | self.assertTypedEquals(F(-9, 10), F(1, 10) - 1) |

383 | n/a | self.assertTypedEquals(-0.9, F(1, 10) - 1.0) |

384 | n/a | self.assertTypedEquals(-0.9 + 0j, F(1, 10) - (1.0 + 0j)) |

385 | n/a | self.assertTypedEquals(F(9, 10), 1 - F(1, 10)) |

386 | n/a | self.assertTypedEquals(0.9, 1.0 - F(1, 10)) |

387 | n/a | self.assertTypedEquals(0.9 + 0j, (1.0 + 0j) - F(1, 10)) |

388 | n/a | |

389 | n/a | self.assertTypedEquals(F(1, 10), F(1, 10) * 1) |

390 | n/a | self.assertTypedEquals(0.1, F(1, 10) * 1.0) |

391 | n/a | self.assertTypedEquals(0.1 + 0j, F(1, 10) * (1.0 + 0j)) |

392 | n/a | self.assertTypedEquals(F(1, 10), 1 * F(1, 10)) |

393 | n/a | self.assertTypedEquals(0.1, 1.0 * F(1, 10)) |

394 | n/a | self.assertTypedEquals(0.1 + 0j, (1.0 + 0j) * F(1, 10)) |

395 | n/a | |

396 | n/a | self.assertTypedEquals(F(1, 10), F(1, 10) / 1) |

397 | n/a | self.assertTypedEquals(0.1, F(1, 10) / 1.0) |

398 | n/a | self.assertTypedEquals(0.1 + 0j, F(1, 10) / (1.0 + 0j)) |

399 | n/a | self.assertTypedEquals(F(10, 1), 1 / F(1, 10)) |

400 | n/a | self.assertTypedEquals(10.0, 1.0 / F(1, 10)) |

401 | n/a | self.assertTypedEquals(10.0 + 0j, (1.0 + 0j) / F(1, 10)) |

402 | n/a | |

403 | n/a | self.assertTypedEquals(0, F(1, 10) // 1) |

404 | n/a | self.assertTypedEquals(0, F(1, 10) // 1.0) |

405 | n/a | self.assertTypedEquals(10, 1 // F(1, 10)) |

406 | n/a | self.assertTypedEquals(10**23, 10**22 // F(1, 10)) |

407 | n/a | self.assertTypedEquals(10, 1.0 // F(1, 10)) |

408 | n/a | |

409 | n/a | self.assertTypedEquals(F(1, 10), F(1, 10) % 1) |

410 | n/a | self.assertTypedEquals(0.1, F(1, 10) % 1.0) |

411 | n/a | self.assertTypedEquals(F(0, 1), 1 % F(1, 10)) |

412 | n/a | self.assertTypedEquals(0.0, 1.0 % F(1, 10)) |

413 | n/a | |

414 | n/a | # No need for divmod since we don't override it. |

415 | n/a | |

416 | n/a | # ** has more interesting conversion rules. |

417 | n/a | self.assertTypedEquals(F(100, 1), F(1, 10) ** -2) |

418 | n/a | self.assertTypedEquals(F(100, 1), F(10, 1) ** 2) |

419 | n/a | self.assertTypedEquals(0.1, F(1, 10) ** 1.0) |

420 | n/a | self.assertTypedEquals(0.1 + 0j, F(1, 10) ** (1.0 + 0j)) |

421 | n/a | self.assertTypedEquals(4 , 2 ** F(2, 1)) |

422 | n/a | z = pow(-1, F(1, 2)) |

423 | n/a | self.assertAlmostEqual(0, z.real) |

424 | n/a | self.assertEqual(1, z.imag) |

425 | n/a | self.assertTypedEquals(F(1, 4) , 2 ** F(-2, 1)) |

426 | n/a | self.assertTypedEquals(2.0 , 4 ** F(1, 2)) |

427 | n/a | self.assertTypedEquals(0.25, 2.0 ** F(-2, 1)) |

428 | n/a | self.assertTypedEquals(1.0 + 0j, (1.0 + 0j) ** F(1, 10)) |

429 | n/a | self.assertRaises(ZeroDivisionError, operator.pow, |

430 | n/a | F(0, 1), -2) |

431 | n/a | |

432 | n/a | def testMixingWithDecimal(self): |

433 | n/a | # Decimal refuses mixed arithmetic (but not mixed comparisons) |

434 | n/a | self.assertRaises(TypeError, operator.add, |

435 | n/a | F(3,11), Decimal('3.1415926')) |

436 | n/a | self.assertRaises(TypeError, operator.add, |

437 | n/a | Decimal('3.1415926'), F(3,11)) |

438 | n/a | |

439 | n/a | def testComparisons(self): |

440 | n/a | self.assertTrue(F(1, 2) < F(2, 3)) |

441 | n/a | self.assertFalse(F(1, 2) < F(1, 2)) |

442 | n/a | self.assertTrue(F(1, 2) <= F(2, 3)) |

443 | n/a | self.assertTrue(F(1, 2) <= F(1, 2)) |

444 | n/a | self.assertFalse(F(2, 3) <= F(1, 2)) |

445 | n/a | self.assertTrue(F(1, 2) == F(1, 2)) |

446 | n/a | self.assertFalse(F(1, 2) == F(1, 3)) |

447 | n/a | self.assertFalse(F(1, 2) != F(1, 2)) |

448 | n/a | self.assertTrue(F(1, 2) != F(1, 3)) |

449 | n/a | |

450 | n/a | def testComparisonsDummyRational(self): |

451 | n/a | self.assertTrue(F(1, 2) == DummyRational(1, 2)) |

452 | n/a | self.assertTrue(DummyRational(1, 2) == F(1, 2)) |

453 | n/a | self.assertFalse(F(1, 2) == DummyRational(3, 4)) |

454 | n/a | self.assertFalse(DummyRational(3, 4) == F(1, 2)) |

455 | n/a | |

456 | n/a | self.assertTrue(F(1, 2) < DummyRational(3, 4)) |

457 | n/a | self.assertFalse(F(1, 2) < DummyRational(1, 2)) |

458 | n/a | self.assertFalse(F(1, 2) < DummyRational(1, 7)) |

459 | n/a | self.assertFalse(F(1, 2) > DummyRational(3, 4)) |

460 | n/a | self.assertFalse(F(1, 2) > DummyRational(1, 2)) |

461 | n/a | self.assertTrue(F(1, 2) > DummyRational(1, 7)) |

462 | n/a | self.assertTrue(F(1, 2) <= DummyRational(3, 4)) |

463 | n/a | self.assertTrue(F(1, 2) <= DummyRational(1, 2)) |

464 | n/a | self.assertFalse(F(1, 2) <= DummyRational(1, 7)) |

465 | n/a | self.assertFalse(F(1, 2) >= DummyRational(3, 4)) |

466 | n/a | self.assertTrue(F(1, 2) >= DummyRational(1, 2)) |

467 | n/a | self.assertTrue(F(1, 2) >= DummyRational(1, 7)) |

468 | n/a | |

469 | n/a | self.assertTrue(DummyRational(1, 2) < F(3, 4)) |

470 | n/a | self.assertFalse(DummyRational(1, 2) < F(1, 2)) |

471 | n/a | self.assertFalse(DummyRational(1, 2) < F(1, 7)) |

472 | n/a | self.assertFalse(DummyRational(1, 2) > F(3, 4)) |

473 | n/a | self.assertFalse(DummyRational(1, 2) > F(1, 2)) |

474 | n/a | self.assertTrue(DummyRational(1, 2) > F(1, 7)) |

475 | n/a | self.assertTrue(DummyRational(1, 2) <= F(3, 4)) |

476 | n/a | self.assertTrue(DummyRational(1, 2) <= F(1, 2)) |

477 | n/a | self.assertFalse(DummyRational(1, 2) <= F(1, 7)) |

478 | n/a | self.assertFalse(DummyRational(1, 2) >= F(3, 4)) |

479 | n/a | self.assertTrue(DummyRational(1, 2) >= F(1, 2)) |

480 | n/a | self.assertTrue(DummyRational(1, 2) >= F(1, 7)) |

481 | n/a | |

482 | n/a | def testComparisonsDummyFloat(self): |

483 | n/a | x = DummyFloat(1./3.) |

484 | n/a | y = F(1, 3) |

485 | n/a | self.assertTrue(x != y) |

486 | n/a | self.assertTrue(x < y or x > y) |

487 | n/a | self.assertFalse(x == y) |

488 | n/a | self.assertFalse(x <= y and x >= y) |

489 | n/a | self.assertTrue(y != x) |

490 | n/a | self.assertTrue(y < x or y > x) |

491 | n/a | self.assertFalse(y == x) |

492 | n/a | self.assertFalse(y <= x and y >= x) |

493 | n/a | |

494 | n/a | def testMixedLess(self): |

495 | n/a | self.assertTrue(2 < F(5, 2)) |

496 | n/a | self.assertFalse(2 < F(4, 2)) |

497 | n/a | self.assertTrue(F(5, 2) < 3) |

498 | n/a | self.assertFalse(F(4, 2) < 2) |

499 | n/a | |

500 | n/a | self.assertTrue(F(1, 2) < 0.6) |

501 | n/a | self.assertFalse(F(1, 2) < 0.4) |

502 | n/a | self.assertTrue(0.4 < F(1, 2)) |

503 | n/a | self.assertFalse(0.5 < F(1, 2)) |

504 | n/a | |

505 | n/a | self.assertFalse(float('inf') < F(1, 2)) |

506 | n/a | self.assertTrue(float('-inf') < F(0, 10)) |

507 | n/a | self.assertFalse(float('nan') < F(-3, 7)) |

508 | n/a | self.assertTrue(F(1, 2) < float('inf')) |

509 | n/a | self.assertFalse(F(17, 12) < float('-inf')) |

510 | n/a | self.assertFalse(F(144, -89) < float('nan')) |

511 | n/a | |

512 | n/a | def testMixedLessEqual(self): |

513 | n/a | self.assertTrue(0.5 <= F(1, 2)) |

514 | n/a | self.assertFalse(0.6 <= F(1, 2)) |

515 | n/a | self.assertTrue(F(1, 2) <= 0.5) |

516 | n/a | self.assertFalse(F(1, 2) <= 0.4) |

517 | n/a | self.assertTrue(2 <= F(4, 2)) |

518 | n/a | self.assertFalse(2 <= F(3, 2)) |

519 | n/a | self.assertTrue(F(4, 2) <= 2) |

520 | n/a | self.assertFalse(F(5, 2) <= 2) |

521 | n/a | |

522 | n/a | self.assertFalse(float('inf') <= F(1, 2)) |

523 | n/a | self.assertTrue(float('-inf') <= F(0, 10)) |

524 | n/a | self.assertFalse(float('nan') <= F(-3, 7)) |

525 | n/a | self.assertTrue(F(1, 2) <= float('inf')) |

526 | n/a | self.assertFalse(F(17, 12) <= float('-inf')) |

527 | n/a | self.assertFalse(F(144, -89) <= float('nan')) |

528 | n/a | |

529 | n/a | def testBigFloatComparisons(self): |

530 | n/a | # Because 10**23 can't be represented exactly as a float: |

531 | n/a | self.assertFalse(F(10**23) == float(10**23)) |

532 | n/a | # The first test demonstrates why these are important. |

533 | n/a | self.assertFalse(1e23 < float(F(math.trunc(1e23) + 1))) |

534 | n/a | self.assertTrue(1e23 < F(math.trunc(1e23) + 1)) |

535 | n/a | self.assertFalse(1e23 <= F(math.trunc(1e23) - 1)) |

536 | n/a | self.assertTrue(1e23 > F(math.trunc(1e23) - 1)) |

537 | n/a | self.assertFalse(1e23 >= F(math.trunc(1e23) + 1)) |

538 | n/a | |

539 | n/a | def testBigComplexComparisons(self): |

540 | n/a | self.assertFalse(F(10**23) == complex(10**23)) |

541 | n/a | self.assertRaises(TypeError, operator.gt, F(10**23), complex(10**23)) |

542 | n/a | self.assertRaises(TypeError, operator.le, F(10**23), complex(10**23)) |

543 | n/a | |

544 | n/a | x = F(3, 8) |

545 | n/a | z = complex(0.375, 0.0) |

546 | n/a | w = complex(0.375, 0.2) |

547 | n/a | self.assertTrue(x == z) |

548 | n/a | self.assertFalse(x != z) |

549 | n/a | self.assertFalse(x == w) |

550 | n/a | self.assertTrue(x != w) |

551 | n/a | for op in operator.lt, operator.le, operator.gt, operator.ge: |

552 | n/a | self.assertRaises(TypeError, op, x, z) |

553 | n/a | self.assertRaises(TypeError, op, z, x) |

554 | n/a | self.assertRaises(TypeError, op, x, w) |

555 | n/a | self.assertRaises(TypeError, op, w, x) |

556 | n/a | |

557 | n/a | def testMixedEqual(self): |

558 | n/a | self.assertTrue(0.5 == F(1, 2)) |

559 | n/a | self.assertFalse(0.6 == F(1, 2)) |

560 | n/a | self.assertTrue(F(1, 2) == 0.5) |

561 | n/a | self.assertFalse(F(1, 2) == 0.4) |

562 | n/a | self.assertTrue(2 == F(4, 2)) |

563 | n/a | self.assertFalse(2 == F(3, 2)) |

564 | n/a | self.assertTrue(F(4, 2) == 2) |

565 | n/a | self.assertFalse(F(5, 2) == 2) |

566 | n/a | self.assertFalse(F(5, 2) == float('nan')) |

567 | n/a | self.assertFalse(float('nan') == F(3, 7)) |

568 | n/a | self.assertFalse(F(5, 2) == float('inf')) |

569 | n/a | self.assertFalse(float('-inf') == F(2, 5)) |

570 | n/a | |

571 | n/a | def testStringification(self): |

572 | n/a | self.assertEqual("Fraction(7, 3)", repr(F(7, 3))) |

573 | n/a | self.assertEqual("Fraction(6283185307, 2000000000)", |

574 | n/a | repr(F('3.1415926535'))) |

575 | n/a | self.assertEqual("Fraction(-1, 100000000000000000000)", |

576 | n/a | repr(F(1, -10**20))) |

577 | n/a | self.assertEqual("7/3", str(F(7, 3))) |

578 | n/a | self.assertEqual("7", str(F(7, 1))) |

579 | n/a | |

580 | n/a | def testHash(self): |

581 | n/a | hmod = sys.hash_info.modulus |

582 | n/a | hinf = sys.hash_info.inf |

583 | n/a | self.assertEqual(hash(2.5), hash(F(5, 2))) |

584 | n/a | self.assertEqual(hash(10**50), hash(F(10**50))) |

585 | n/a | self.assertNotEqual(hash(float(10**23)), hash(F(10**23))) |

586 | n/a | self.assertEqual(hinf, hash(F(1, hmod))) |

587 | n/a | # Check that __hash__ produces the same value as hash(), for |

588 | n/a | # consistency with int and Decimal. (See issue #10356.) |

589 | n/a | self.assertEqual(hash(F(-1)), F(-1).__hash__()) |

590 | n/a | |

591 | n/a | def testApproximatePi(self): |

592 | n/a | # Algorithm borrowed from |

593 | n/a | # http://docs.python.org/lib/decimal-recipes.html |

594 | n/a | three = F(3) |

595 | n/a | lasts, t, s, n, na, d, da = 0, three, 3, 1, 0, 0, 24 |

596 | n/a | while abs(s - lasts) > F(1, 10**9): |

597 | n/a | lasts = s |

598 | n/a | n, na = n+na, na+8 |

599 | n/a | d, da = d+da, da+32 |

600 | n/a | t = (t * n) / d |

601 | n/a | s += t |

602 | n/a | self.assertAlmostEqual(math.pi, s) |

603 | n/a | |

604 | n/a | def testApproximateCos1(self): |

605 | n/a | # Algorithm borrowed from |

606 | n/a | # http://docs.python.org/lib/decimal-recipes.html |

607 | n/a | x = F(1) |

608 | n/a | i, lasts, s, fact, num, sign = 0, 0, F(1), 1, 1, 1 |

609 | n/a | while abs(s - lasts) > F(1, 10**9): |

610 | n/a | lasts = s |

611 | n/a | i += 2 |

612 | n/a | fact *= i * (i-1) |

613 | n/a | num *= x * x |

614 | n/a | sign *= -1 |

615 | n/a | s += num / fact * sign |

616 | n/a | self.assertAlmostEqual(math.cos(1), s) |

617 | n/a | |

618 | n/a | def test_copy_deepcopy_pickle(self): |

619 | n/a | r = F(13, 7) |

620 | n/a | dr = DummyFraction(13, 7) |

621 | n/a | self.assertEqual(r, loads(dumps(r))) |

622 | n/a | self.assertEqual(id(r), id(copy(r))) |

623 | n/a | self.assertEqual(id(r), id(deepcopy(r))) |

624 | n/a | self.assertNotEqual(id(dr), id(copy(dr))) |

625 | n/a | self.assertNotEqual(id(dr), id(deepcopy(dr))) |

626 | n/a | self.assertTypedEquals(dr, copy(dr)) |

627 | n/a | self.assertTypedEquals(dr, deepcopy(dr)) |

628 | n/a | |

629 | n/a | def test_slots(self): |

630 | n/a | # Issue 4998 |

631 | n/a | r = F(13, 7) |

632 | n/a | self.assertRaises(AttributeError, setattr, r, 'a', 10) |

633 | n/a | |

634 | n/a | if __name__ == '__main__': |

635 | n/a | unittest.main() |