2014-02-21 10:09:12 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
# encoding: utf-8
|
|
|
|
|
|
|
|
import unittest
|
|
|
|
|
|
|
|
from pymath.fraction import Fraction
|
|
|
|
|
|
|
|
class TestFraction(unittest.TestCase):
|
|
|
|
"""Testing functions from pymath.Fraction"""
|
|
|
|
|
|
|
|
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
|
|
|
]
|
2014-02-22 06:23:42 +00:00
|
|
|
# TODO: Bug pour 1 + 1/-3 |sam. févr. 22 07:01:29 CET 2014
|
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
|
|
|
|
|
2014-02-21 17:02:50 +00:00
|
|
|
#print("-----------")
|
|
|
|
#print("f1 : ", f1)
|
|
|
|
#print("f2 : ", f2)
|
|
|
|
#print(res)
|
2014-02-21 10:09:12 +00:00
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
# On est obligé de faire ça pour gérer le cas de 1+1 qui ne passe pas par la classe Fraction
|
|
|
|
if type(res) == list:
|
|
|
|
self.assertEqual(res[-1], ans[i][j])
|
|
|
|
else:
|
|
|
|
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-22 06:23:42 +00:00
|
|
|
# TODO: bug pour 1 - 1/-3 |sam. févr. 22 07:05:15 CET 2014
|
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
|
|
|
|
|
|
|
|
#print("-----------")
|
|
|
|
#print("f1 : ", f1)
|
|
|
|
#print("f2 : ", f2)
|
|
|
|
#print(res)
|
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
# On est obligé de faire ça pour gérer le cas de 1-1 qui ne passe pas par la classe Fraction
|
|
|
|
if type(res) == list:
|
|
|
|
self.assertEqual(res[-1], ans[i][j])
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
if type(res) == list:
|
|
|
|
self.assertEqual(res[-1], ans[j])
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
|
|
|
#print("-----------")
|
|
|
|
#print("f1 : ", f1)
|
|
|
|
#print("f2 : ", f2)
|
|
|
|
#print(res)
|
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
# On est obligé de faire ça pour gérer le cas de 1*1 qui ne passe pas par la classe Fraction
|
|
|
|
if type(res) == list:
|
|
|
|
self.assertEqual(res[-1], ans[i][j])
|
|
|
|
else:
|
|
|
|
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
|
|
|
|
|
|
|
|
#print("-----------")
|
|
|
|
#print("f1 : ", f1)
|
|
|
|
#print("f2 : ", f2)
|
|
|
|
#print(res)
|
|
|
|
|
2014-02-22 06:23:42 +00:00
|
|
|
# On est obligé de faire ça pour gérer le cas de 1/1 qui ne passe pas par la classe Fraction
|
|
|
|
if type(res) == list:
|
|
|
|
self.assertEqual(res[-1], ans[i][j])
|
|
|
|
else:
|
|
|
|
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)
|
|
|
|
|
|
|
|
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:
|
|
|
|
# cursor: 16 del
|