Пример #1
0
class TestFracs(unittest.TestCase):
    def setUp(self):
        self.one = Frac(1, 2)
        self.two = Frac(1, 4)
        self.three = Frac(-1, 6)
        self.four = Frac(0, 6)
        self.five = Frac(-2, 5)

    def test_cmp(self):
        self.assertEqual(self.three.__cmp__(self.four), -1)
        self.assertEqual(self.three.__cmp__(self.three), 0)
        self.assertEqual(self.three.__cmp__(self.five), 1)

    def test_add(self):
        self.assertEqual(self.one + self.two, Frac(3, 4))
        self.assertEqual(self.one + self.four, Frac(3, 6))
        self.assertEqual(self.one + self.five, Frac(1, 10))
        self.assertEqual(self.one + 2, Frac(5, 2))
        self.assertEqual(2 + self.one, Frac(5, 2))


    def test_sub(self):
        self.assertEqual(self.one - self.two, Frac(1, 4))
        self.assertEqual(2 - self.one, Frac(3, 2))
        self.assertEqual(self.one - 1, Frac(-1, 2))

    def test_mul(self):
        self.assertEqual(self.two * self.two, Frac(1, 16))
        self.assertEqual(self.one * 5, Frac(5, 2))
        self.assertEqual(5 * self.one, Frac(5, 2))

    def test_div(self):
        self.assertEqual(self.three / self.two, Frac(-2, 3))
        self.assertEqual(self.one / 3, Frac(1, 6))
        # self.assertEqual(self.one/0, ???)
        self.assertEqual(3 / self.one, Frac(6, 1))
        #self.assertRaises(ValueError, Frac, self.one/0)

    def test_pos(self):
        self.assertEqual(self.one.__pos__(), Frac(1, 2))

    def test_neg(self):
        self.assertEqual(self.one.__neg__(), Frac(-1, 2))

    def test_invert(self):
        self.assertEqual(self.two.__invert__(), Frac(4, 1))

    def test_is_positive(self):
        self.assertEqual(self.one.is_positive(), True)
        self.assertEqual(self.five.is_positive(), False)

    def test_is_zero(self):
        self.assertEqual(self.two.is_zero(), False)
        self.assertEqual(self.four.is_zero(), True)

    def test_frac_to_float(self):
        self.assertEqual(self.five.frac2float(), -0.4)
        self.assertEqual(self.two.frac2float(), 0.25)
Пример #2
0
class TestFractions(unittest.TestCase):
    def setUp(self):
        self.f1 = Frac(1, 3)
        self.f2 = Frac(2, 6)
        self.f3 = Frac(3, 10)
        self.f0 = Frac(0, 1)

    def test_add_frac(self):
        self.assertEqual(self.f1 + self.f2 + self.f2, 1)
        self.assertEqual(self.f3 + 10, Frac(103, 10))
        self.assertEqual(10 + self.f3, Frac(103, 10))
        self.assertAlmostEqual(float(10.4 + self.f3), 10.7, places=3, msg=None)
        self.assertAlmostEqual(float(0.66666 + Frac(1,3)), 1, places=3, msg=None)

    def test_sub_frac(self):
        self.assertEqual(self.f2 - self.f3, Frac(1, 30))
        self.assertEqual(10 - self.f3, Frac(97, 10))
        self.assertEqual(self.f3 - 10, Frac(-97, 10))
        self.assertAlmostEqual(float(0.66666 - Frac(1, 3)), 0.3333, places=3, msg=None)

    def test_mul_frac(self):
        self.assertEqual(self.f1 * self.f3, Frac(1, 10))
        self.assertEqual(10 * self.f3, Frac(3, 1))
        self.assertEqual(10.2 * self.f3, Frac(31, 10))

    def test_div_frac(self):
        self.assertEqual(self.f1 / self.f3, Frac(20, 18))
        self.assertEqual(self.f1 / self.f3, Frac(10, 9))
        self.assertEqual(10 / self.f3, Frac(100, 3))
        self.assertEqual(10.5 / self.f3, Frac(35, 1))
        with self.assertRaises(ValueError):
            self.f1 / 0
        with self.assertRaises(ValueError):
            "abc" / self.f3

    def test_is_positive(self):
        f1 = Frac(-1, -5)
        f2 = Frac(1, -5)
        self.assertTrue(f1.is_positive())
        self.assertFalse(f2.is_positive())

    def test_is_zero(self):
        self.assertTrue(self.f0.is_zero())
        self.assertFalse(self.f1.is_zero())

    def test_cmp_frac(self):
        self.assertTrue(self.f2 > self.f3)
        self.assertFalse(Frac(20, 18) < Frac(10, 18))

    def test_frac2float(self):
        self.assertAlmostEqual(float(self.f1), 0.333, places=3, msg=None)
Пример #3
0
class TestFractions(unittest.TestCase):
    def setUp(self):
        self.f1 = Frac(1, 3)
        self.f2 = Frac(2, 6)
        self.f3 = Frac(3, 10)
        self.f0 = Frac(0, 1)

    def test_add_frac(self):
        self.assertEqual(self.f1 + self.f2 + self.f2, Frac(1))

    def test_sub_frac(self):
        self.assertEqual(self.f2 - self.f3, Frac(1, 30))

    def test_mul_frac(self):
        self.assertEqual(self.f1 * self.f3, Frac(1, 10))

    def test_div_frac(self):
        self.assertEqual(self.f1 / self.f3, Frac(20, 18))
        self.assertEqual(self.f1 / self.f3, Frac(10, 9))
        with self.assertRaises(ValueError):
            self.f1 / 0

    def test_is_positive(self):
        f1 = Frac(-1, -5)
        f2 = Frac(1, -5)
        self.assertTrue(f1.is_positive())
        self.assertFalse(f2.is_positive())

    def test_is_zero(self):
        self.assertTrue(self.f0.is_zero())
        self.assertFalse(self.f1.is_zero())

    def test_cmp_frac(self):
        self.assertTrue(self.f2 > self.f3)
        self.assertFalse(Frac(20, 18) < Frac(10, 18))

    def test_frac2float(self):
        self.assertAlmostEqual(float(self.f1), 0.333, places=3, msg=None)