Mapytex/pymath/calculus/test/test_fraction.py

105 lines
3.2 KiB
Python
Raw Normal View History

#!/usr/bin/env python
# encoding: utf-8
import unittest
2016-01-07 16:56:30 +00:00
from pymath.calculus.fraction import Fraction
2016-02-13 03:49:37 +00:00
class TestFraction(unittest.TestCase):
2016-01-07 16:56:30 +00:00
"""Testing functions from pymath.calculus.Fraction"""
def setUp(self):
2016-02-13 03:49:37 +00:00
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):
2016-02-13 03:49:37 +00:00
ans = [
[
Fraction(
2, 3), 1, Fraction(
17, 15), 0, 0, Fraction(
4, 3)], [
Fraction(
4, 3), Fraction(
5, 3), Fraction(
9, 5), Fraction(
2, 3), Fraction(
2, 3), 2]]
for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst):
res = f1 + f2
self.assertEqual(res, ans[i][j])
def test_sub(self):
2016-02-13 03:49:37 +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 17:02:50 +00:00
for (i, f1) in enumerate(self.listFrom):
for (j, f2) in enumerate(self.listAgainst):
res = f1 - f2
self.assertEqual(res, ans[i][j])
def test_neg(self):
2016-02-13 03:49:37 +00:00
ans = [Fraction(-1, 3),
Fraction(-2, 3),
Fraction(-4, 5),
Fraction(1, 3),
Fraction(1, 3),
-1
]
2014-11-14 16:00:26 +00:00
for (j, f) in enumerate(self.listAgainst):
res = -f
self.assertEqual(res, ans[j])
def test_mul(self):
2016-02-13 03:49:37 +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]]
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
self.assertEqual(res, ans[i][j])
def test_truediv(self):
2016-02-13 03:49:37 +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]
]
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
self.assertEqual(res, ans[i][j])
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)
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