Exemplo n.º 1
0
 def test_str(self):
     from constructible import Constructible
     self.assertEqual(str(Constructible(2)), '2')
     self.assertEqual(
         str(
             Constructible(Constructible(2), Constructible(3),
                           (Constructible(5), ()))), '(2 + 3 * sqrt(5))')
Exemplo n.º 2
0
 def test_repr(self):
     from constructible import Constructible
     self.assertEqual(repr(Constructible(2)),
                      'Constructible(Fraction(2, 1), 0, ())')
     self.assertEqual(
         repr(
             Constructible(Constructible(2), Constructible(3),
                           (Constructible(5), ()))), 'Constructible('
         'Constructible(Fraction(2, 1), 0, ()), '
         'Constructible(Fraction(3, 1), 0, ()), '
         '(Constructible(Fraction(5, 1), 0, ()), ())'
         ')')
Exemplo n.º 3
0
    def test_rational_comparison(self):
        ''' test comparison operators on constructible 
        instances representing rationals '''
        from constructible import Constructible
        from operator import eq, ne, gt, lt, ge, le

        for op in (eq, ne, gt, lt, ge, le):
            with self.subTest(op=op):
                for a in [0, 1, -1]:
                    for b in [0, 1, -1]:
                        result = op(Constructible(a), Constructible(b))
                        self.assertEqual(result, op(a, b))
                        self.assertIsInstance(result, bool)
Exemplo n.º 4
0
    def test_rational_binop(self):
        ''' test binary operators on constructible 
        instances representing rationals '''
        from constructible import Constructible
        from fractions import Fraction as F
        from operator import add, mul, sub, truediv

        for op in (add, mul, sub, truediv):
            with self.subTest(op=op):
                for a, b in [(F(1, 2), F(5, 7)), (F(-1, 2), F(12, 25)),
                             (F(4, 3), 7), (17, F(7, 16))]:
                    result = op(Constructible(a), Constructible(b))
                    self.assertEqual(result.a, op(a, b))
                    self.assertFalse(result.b)
                    self.assertFalse(result.field)
Exemplo n.º 5
0
    def test_rational(self):
        '''
        hash of rationals represented as Constructible must be equal to the
        hash of the original value.
        '''
        from constructible import Constructible
        from fractions import Fraction as F

        for x in [0, 1, -1, F(0, 1), F(1, 2), F(-1, 1)]:
            with self.subTest(x=x):
                y = Constructible(x)
                # compare y == x instead of x == y because in Python 2.6 Fraction.__eq__ is broken
                self.assertEqual(y, x,
                                 'precondition for this test: %s==%s' % (y, x))
                self.assertEqual(hash(x), hash(y), 'hash(%s)' % (x, ))
Exemplo n.º 6
0
    def test_rational_unop(self):
        ''' test unary operators on constructible instances 
        representing rationals '''
        from constructible import Constructible
        from fractions import Fraction as F
        from operator import pos, neg

        for op in (pos, neg):
            with self.subTest(op=op):
                for a in [
                        F(1, 2),
                        F(-1, 2),
                        F(12, 25),
                        F(4, 3), 7, 0, -10,
                        F(0)
                ]:
                    result = op(Constructible(a))
                    self.assertEqual(result.a, op(a))
                    self.assertFalse(result.b)
                    self.assertFalse(result.field)