Пример #1
0
    def test_infinity(self):
        expressions = [('2^3000', N('^', L(2), L(3000))),
                       ('2^-3000', N('^', L(2), -L(3000)))]
        #               ('2^99999999999', None),
        #               ('2^-99999999999', 0.0)]

        run_expressions(Parser, expressions)
Пример #2
0
    def test_raise_numerics(self):
        l1, l2 = root = tree('2 ^ 3')
        self.assertEqualNodes(raise_numerics(root, (l1, l2, 0)), L(8))

        l1_neg, l2 = root = tree('(-2) ^ 2')
        self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), --L(4))

        l1_neg, l2 = root = tree('(-2) ^ 3')
        self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 0)), ---L(8))

        l1_neg, l2 = root = tree('-((-2) ^ 3)')
        self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), ----L(8))

        l1_neg, l2 = root = tree('-(2 ^ 3)')
        self.assertEqualNodes(raise_numerics(root, (l1_neg, l2, 1)), -L(8))
Пример #3
0
    def test_pow_nested(self):
        # a^b^c = a^(b^c) != (a^b)^c

        a, b, c, d, e = L('a'), L('b'), L('c'), L('d'), L('e')

        expressions = [
            ('a^b^c', N('^', a, N('^', b, c))),
            ('-1^b^c', -N('^', L(1), N('^', b, c))),
            ('ab^c', N('*', a, N('^', b, c))),
            ('a(b)^c', N('*', a, N('^', b, c))),
            ('a(b+c)^(d+e)', N('*', a, N('^', N('+', b, c), N('+', d, e)))),
            ('(a(b+c))^(d+e)', N('^', N('*', a, N('+', b, c)), N('+', d, e))),
        ]

        run_expressions(Parser, expressions)
Пример #4
0
 def test_is_leaf(self):
     self.assertTrue(L(2).is_leaf)
     self.assertFalse(N('+', *self.l[:2]).is_leaf)
Пример #5
0
    def test___lt__(self):
        self.assertTrue(L(1) < L(2))
        self.assertFalse(L(1) < L(1))
        self.assertFalse(L(2) < L(1))

        self.assertTrue(L(2) < N('+', L(1), L(2)))
        self.assertFalse(N('+', L(1), L(2)) < L(1))

        self.assertTrue(N('^', L('a'), L(2)) < N('^', L('a'), L(3)))
        self.assertTrue(N('^', L(2), L('a')) < N('^', L(3), L('a')))
        self.assertTrue(
            N('*', L(2), N('^', L('a'), L('b'))) < N('*', L(3),
                                                     N('^', L('a'), L('b'))))
        self.assertFalse(N('^', L('a'), L(3)) < N('^', L('a'), L(2)))
Пример #6
0
 def setUp(self):
     self.l = [L(1), N('*', L(2), L(3)), L(4), L(5)]
     self.n, self.f = tree('a + b + cd,f')
     (self.a, self.b), self.cd = self.n
     self.c, self.d = self.cd
     self.scope = Scope(self.n)
Пример #7
0
 def test_diagnostic_test_application(self):
     apply_expressions(Parser, [
         ('7p+2p', 1, (L(7) + 2) * 'p'),
         ('7p-3p', 1, (L(7) + -L(3)) * 'p'),
     ])
Пример #8
0
 def test_extract_polynome_properties_power(self):
     power = N('^', L('a'), L(2))
     self.assertEqual(power.extract_polynome_properties(),
                      (L(1), L('a'), L(2)))
Пример #9
0
 def test_is_numeric(self):
     self.assertTrue(L(1).is_numeric())
     self.assertTrue(L(1.5).is_numeric())
     self.assertFalse(L('a').is_numeric())
Пример #10
0
    def test_concat_intermediate(self):
        expressions = [
            ('(3+4)(5+7)', N('*', N('+', L(3), L(4)), N('+', L(5), L(7)))),
            ('(a+b)(c+d)',
             N('*', N('+', L('a'), L('b')), N('+', L('c'), L('d')))),
            ('a+b(c+d)', N('+', L('a'), N('*', L('b'), N('+', L('c'),
                                                         L('d'))))),
            ('abcd', N('*', N('*', N('*', L('a'), L('b')), L('c')), L('d'))),
            ('ab(c)d', N('*', N('*', N('*', L('a'), L('b')), L('c')), L('d'))),
            ('ab*(c)*d', N('*', N('*', N('*', L('a'), L('b')), L('c')),
                           L('d'))),
            ('ab*(c)^d', N('*', N('*', L('a'), L('b')), N('^', L('c'),
                                                          L('d')))),
        ]

        run_expressions(Parser, expressions)
Пример #11
0
    def test_concat_easy(self):
        expressions = [
            ('xy', N('*', L('x'), L('y'))),
            ('2x', N('*', L(2), L('x'))),
            ('x4', N('*', L('x'), L(4))),
            ('3 4', N('*', L(3), L(4))),
            ('(x)4', N('*', L('x'), L(4))),
            ('(3+4)2', N('*', N('+', L(3), L(4)), L(2))),
        ]

        run_expressions(Parser, expressions)
Пример #12
0
    def test_basic_on_exp(self):
        expressions = [('4', L(4)),
                       ('3+4', L(3) + L(4)), ('3-4', L(3) + -L(4)),
                       ('3/4', L(3) / L(4)), ('-4', -L(4)),
                       ('3^4', N('^', L(3), L(4))), ('(2)', L(2))]

        run_expressions(Parser, expressions)
Пример #13
0
 def test_constructor(self):
     assert ParserWrapper(Parser).run(['1+4']) \
             == N('+', L(1), L(4))
Пример #14
0
    def test_add_numerics(self):
        l1, l2 = root = tree('1 + 2')
        self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), 3)

        (l1, a), l2 = root = tree('1 + a + 2')
        self.assertEqual(add_numerics(root, (Scope(root), l1, l2)), L(3) + a)
Пример #15
0
 def test_is_identifier(self):
     self.assertTrue(L('a').is_identifier())
     self.assertFalse(L(1).is_identifier())
Пример #16
0
 def test_is_float(self):
     self.assertTrue(L(1.5).is_float())
     self.assertFalse(L(1).is_float())
     self.assertFalse(L('a').is_float())
Пример #17
0
 def test_negation(self):
     run_expressions(Parser, [
         ('-9', -L(9)),
         ('--9', --L(9)),
         ('a--9', L('a') + -(-L(9))),
     ])
Пример #18
0
 def test_extract_polynome_properties_identifier(self):
     self.assertEqual(
         L('a').extract_polynome_properties(), (L(1), L('a'), L(1)))
Пример #19
0
 def test_diagnostic_test_parser(self):
     run_expressions(Parser, [
         ('6*5^2', L(6) * L(5)**2),
         ('-5*(-3)^2', -(L(5) * (-L(3))**2)),
         ('7p-3p', L(7) * 'p' + -(L(3) * 'p')),
         ('-5a*-6', -(L(5) * 'a' * -L(6))),
         ('3a-8--5-2a', L(3) * 'a' + -L(8) + --(L(5)) + -(L(2) * 'a')),
     ])
Пример #20
0
 def test_extract_polynome_properties_coefficient_exponent_int(self):
     times = N('*', L(3), N('^', L('a'), L(2)))
     self.assertEqual(times.extract_polynome_properties(),
                      (L(3), L('a'), L(2)))
Пример #21
0
 def test_diagnostic_test(self):
     run_expressions(Parser, [
         ('5(a-2b)', L(5) * (L('a') + -(L(2) * 'b'))),
         ('-(3a+6b)', -(L(3) * L('a') + L(6) * 'b')),
         ('18-(a-12)', L(18) + -(L('a') + -L(12))),
         ('-p-q+5(p-q)-3q-2(p-q)',
             -L('p') + -L('q') + L(5) * (L('p') + -L('q')) + -(L(3) * 'q') \
             + -(L(2) * (L('p') + -L('q')))
         ),
         ('(2+3/7)^4',
             N('^', N('+', L(2), N('/', L(3), L(7))), L(4))
         ),
         ('x^3*x^2*x',
             N('*',
               N('*',
                 N('^', L('x'), L(3)),
                 N('^', L('x'), L(2))),
               L('x')
             )
         ),
         ('-x^3*-2x^5',
             -(L('x') ** L(3) * -L(2) * L('x') ** L(5))
         ),
         ('(7x^2y^3)^2/(7x^2y^3)',
             N('/',
               N('^',
                 N('*',
                   N('*', L(7), N('^', L('x'), L(2))),
                   N('^', L('y'), L(3))
                 ),
                 L(2)),
               N('*',
                 N('*', L(7), N('^', L('x'), L(2))),
                 N('^', L('y'), L(3))
               )
             )
         ),
         ])