示例#1
0
    def setUp(self):
        self.zero = Rational(0, 1)
        self.neg_zero = Rational(0, -1)
        self.one_third = Rational(1, 3)
        self.two_fifth = Rational(2, 5)

        self.invalidCases = [(0, 0), (1, 0), (-1, 0), (0, -0), (-0, 0),
                             (-0, -0)]
        self.cases = {
            (0, 1): (0, 1),
            (1, 1): (1, 1),
            (2, 1): (2, 1),
            (-3, 1): (-3, 1),
            (0, 2): (0, 1),
            (-0, 3): (-0, 1),
            (1, 2): (1, 2),
            (2, -2): (-1, 1),
            (3, 2): (3, 2),
            (-0, -3): (0, 1),
            (1, 3): (1, 3),
            (2, 3): (2, 3),
            (-6, 3): (-2, 1),
            (2, 4): (1, 2),
            (6, -4): (-3, 2),
            (35, 45): (7, 9),
            (-64, -16): (4, 1),
            (27, 33): (9, 11),
            (18, 108): (1, 6)
        }
示例#2
0
    def test_simplify_result(self):
        for key, val in self.cases.items():
            with self.subTest(key=key):
                rat = Rational(key[0], key[1])
                self.assertEqual(rat.a, val[0])
                self.assertEqual(rat.b, val[1])

        with self.subTest(key="0.376f"):
            self.assertTrue(Rational(0.376), Rational(376, 1000))
示例#3
0
 def test_getitem_result(self):
     for key, val in self.cases.items():
         with self.subTest(val=val):
             rat = Rational(*key)
             self.assertEqual(rat["a"], val[0])
             self.assertEqual(rat["b"], val[1])
             self.assertEqual(rat[0], val[0])
             self.assertEqual(rat[1], val[1])
示例#4
0
    def test_is_valid(self):
        with self.subTest(type="sub pos"):
            self.assertFalse(Monoid(self.nums, self.sub))

        with self.subTest(type="add pos"):
            self.assertTrue(Monoid(self.nums, self.add))

        with self.subTest(type="mul rational w/o zero and 1/2"):
            self.assertTrue(
                Monoid(self.rationals_wo_zero.difference((Rational(1, 2), )),
                       self.mul))
示例#5
0
 def test_and_ValueError(self):
     # (a, b)  -->  (1, 3) & (a, b)
     for el in [(17, -23), (18, -20)]:
         with self.subTest(val=el):
             with self.assertRaises(ValueError):
                 self.one_third & Rational(*el)
示例#6
0
 def test_and(self):
     #((a, b), (c, d))  -->  (1, 3) & (a, b) = (c, d)
     for el in [((2, 3), (3, 6)), ((7, 15), (8, 18))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third & Rational(*el[0]),
                              Rational(*el[1]))
示例#7
0
 def test_mod(self):
     #((a, b), (c, d))  -->  (1, 3) mod (a, b) = (c, d)
     for el in [((2, 3), (1, 3)), ((-2, 3), (-1, 3)), ((1, 6), (0, 1))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third % Rational(*el[0]),
                              Rational(*el[1]))
示例#8
0
 def test_pow(self):
     #(a, (b, c))  -->  (1, 3) ** a = (b, c)
     for el in [(2, (1, 9)), (-1, (3, 1)), (0, (1, 1))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third**el[0], Rational(*el[1]))
示例#9
0
 def test_truediv(self):
     #((a, b), (c, d))  -->  (1, 3) * (a, b) = (c, d)
     for el in [((2, 3), (3, 6)), ((-2, 3), (-3, 6)), ((5, 7), (7, 15))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third / Rational(*el[0]),
                              Rational(*el[1]))
示例#10
0
 def test_mul(self):
     #((a, b), (c, d))  -->  (1, 3) * (a, b) = (c, d)
     for el in [((2, 3), (2, 9)), ((-2, 3), (-2, 9)), ((5, 7), (5, 21))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third * Rational(*el[0]),
                              Rational(*el[1]))
示例#11
0
 def test_le_representation(self):
     for el in [(3, 1), (2, 3), (-10, -29), (1, 2), (0.672, 1), (1, 3),
                (-2, -6)]:
         with self.subTest(val=el):
             self.assertTrue(self.one_third <= Rational(*el))
示例#12
0
 def test_neq_representation(self):
     for el in [(3, 1), (2, -6), (-3, 9), (-3, 9), (1, 5), (0, 3),
                (0.5, 1)]:
         with self.subTest(val=el):
             self.assertTrue(self.one_third != Rational(*el))
示例#13
0
 def test_eq_representation(self):
     for el in [(2, 6), (3, 9), (-3, -9)]:
         with self.subTest(val=el):
             self.assertTrue(self.one_third == Rational(*el))
示例#14
0
 def test_hash_representation(self):
     for el in [(2, 6), (3, 9), (-3, -9)]:
         with self.subTest(val=el):
             self.assertTrue(hash(self.one_third) == hash(Rational(*el)))
示例#15
0
 def test_init_ValueError(self):
     for el in self.invalidCases:
         with self.subTest(val=el):
             with self.assertRaises(ValueError):
                 Rational(*el)
示例#16
0
 def test_neg(self):
     # (a, b)  -->  -(a, b)
     for el in [((2, 3), (-2, 3)), ((3, 6), (-3, 6)), ((-7, 15), (7, 15)),
                ((-8, -18), (-8, 18))]:
         with self.subTest(val=el):
             self.assertEqual(-Rational(*el[0]), Rational(*el[1]))
示例#17
0
 def __test_performance_rational__(op, op_int):
     for k, j, i, l in combinations(range(-n // 2, n // 2), 4):
         if not j or not l or not k or not i: continue
         op(Rational(k, j), Rational(i, l))
         """
示例#18
0
 def test_sub(self):
     #((a, b), (c, d))  -->  (1, 3) - (a, b) = (c, d)
     for el in [((2, 3), (-1, 3)), ((-2, 3), (1, 1)), ((7, 9), (-4, 9))]:
         with self.subTest(val=el):
             self.assertEqual(self.one_third - Rational(*el[0]),
                              Rational(*el[1]))