2014-02-21 10:09:12 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
2016-01-07 16:56:30 +00:00
|
|
|
from pymath.calculus.fraction import Fraction
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
class TestFraction(unittest.TestCase):
|
2016-01-07 16:56:30 +00:00
|
|
|
"""Testing functions from pymath.calculus.Fraction"""
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def setUp(self):
|
|
|
|
self.listFrom = [Fraction(1,3), 1]
|
|
|
|
self.listAgainst = [ Fraction(1,3), \
|
|
|
|
Fraction(2,3), \
|
|
|
|
Fraction(4,5), \
|
|
|
|
Fraction(-1, 3), \
|
|
|
|
Fraction(1,-3), \
|
|
|
|
1,
|
|
|
|
]
|
|
|
|
|
|
|
|
def test_add(self):
|
2014-02-21 17:02:50 +00:00
|
|
|
ans = [[Fraction(2, 3), 1, Fraction(17, 15), 0, 0, Fraction(4,3)], \
|
2014-02-22 06:23:42 +00:00
|
|
|
[Fraction(4,3), Fraction(5,3), Fraction(9,5), Fraction(2,3), Fraction(2,3), 2] \
|
2014-02-21 10:09:12 +00:00
|
|
|
]
|
|
|
|
|
|
|
|
for (i, f1) in enumerate(self.listFrom):
|
|
|
|
for (j, f2) in enumerate(self.listAgainst):
|
|
|
|
res = f1 + f2
|
2015-02-27 15:57:13 +00:00
|
|
|
self.assertEqual(res, ans[i][j])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_sub(self):
|
2014-02-21 17:02:50 +00:00
|
|
|
ans = [[0, Fraction(-1,3), Fraction(-7, 15), Fraction(2,3), Fraction(2,3), Fraction(-2,3)], \
|
|
|
|
[Fraction(2,3), Fraction(1,3), Fraction(1,5), Fraction(4,3), Fraction(4,3), 0] \
|
|
|
|
]
|
2014-02-21 10:09:12 +00:00
|
|
|
|
2014-02-21 17:02:50 +00:00
|
|
|
for (i, f1) in enumerate(self.listFrom):
|
|
|
|
for (j, f2) in enumerate(self.listAgainst):
|
|
|
|
res = f1 - f2
|
2015-02-27 15:57:13 +00:00
|
|
|
self.assertEqual(res, ans[i][j])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_neg(self):
|
2014-11-14 16:00:26 +00:00
|
|
|
ans = [ Fraction(-1,3), \
|
|
|
|
Fraction(-2,3), \
|
|
|
|
Fraction(-4,5), \
|
|
|
|
Fraction(1, 3), \
|
|
|
|
Fraction(1,3), \
|
|
|
|
-1
|
|
|
|
]
|
|
|
|
for (j, f) in enumerate(self.listAgainst):
|
|
|
|
res = -f
|
2015-02-27 15:57:13 +00:00
|
|
|
self.assertEqual(res, ans[j])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_mul(self):
|
2014-02-21 17:02:50 +00:00
|
|
|
ans = [[Fraction(1, 9), Fraction(2,9), Fraction(4, 15), Fraction(-1,9), Fraction(-1,9), Fraction(1,3)], \
|
|
|
|
[ Fraction(1,3), Fraction(2,3), Fraction(4,5), Fraction(-1, 3), Fraction(1,-3), 1] \
|
|
|
|
]
|
|
|
|
|
|
|
|
for (i, f1) in enumerate(self.listFrom):
|
|
|
|
for (j, f2) in enumerate(self.listAgainst):
|
|
|
|
res = f1 * f2
|
2015-02-27 15:57:13 +00:00
|
|
|
self.assertEqual(res, ans[i][j])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_truediv(self):
|
2014-02-21 17:02:50 +00:00
|
|
|
ans = [[1, Fraction(1,2), Fraction(5, 12), -1, -1, Fraction(1,3)], \
|
|
|
|
[3, Fraction(3,2), Fraction(5,4), -3, -3, 1] \
|
|
|
|
]
|
|
|
|
|
|
|
|
for (i, f1) in enumerate(self.listFrom):
|
|
|
|
for (j, f2) in enumerate(self.listAgainst):
|
|
|
|
res = f1 / f2
|
2015-02-27 15:57:13 +00:00
|
|
|
self.assertEqual(res, ans[i][j])
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
def test_lt(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def test_le(self):
|
|
|
|
pass
|
|
|
|
|
2014-12-03 14:14:50 +00:00
|
|
|
def test_tex(self):
|
|
|
|
f = Fraction(2, 3)
|
|
|
|
ans = "\\frac{ 2 }{ 3 }"
|
|
|
|
self.assertEqual(f.__tex__(), ans)
|
2016-02-13 03:29:26 +00:00
|
|
|
|
2014-12-03 14:14:50 +00:00
|
|
|
def test_txt(self):
|
|
|
|
f = Fraction(2, 3)
|
|
|
|
ans = "2 / 3"
|
|
|
|
self.assertEqual(f.__txt__(), ans)
|
2014-02-21 10:09:12 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
unittest.main()
|
|
|
|
|
|
|
|
# -----------------------------
|
|
|
|
# Reglages pour 'vim'
|
|
|
|
# vim:set autoindent expandtab tabstop=4 shiftwidth=4:
|
2016-02-13 03:29:26 +00:00
|
|
|
# cursor: 16 del
|