예제 #1
0
 def test_zero_reduction(self):
     e = expr_parse('a * 0')
     f = pattern.transform(arithmetic.zero_reduction_multiplication, e)
     self.assertIn(expr_parse('0'), f)
     e = expr_parse('0 / a')
     f = pattern.transform(arithmetic.zero_reduction_division, e)
     self.assertIn(expr_parse('0'), f)
예제 #2
0
 def test_identity_reduction(self):
     e = expr_parse('0 + a')
     f = pattern.transform(arithmetic.identity_reduction_addition, e)
     self.assertIn(expr_parse('a'), f)
     e = expr_parse('1 * a')
     f = pattern.transform(arithmetic.identity_reduction_multiplication, e)
     self.assertIn(expr_parse('a'), f)
     e = expr_parse('a / 1')
     f = pattern.transform(arithmetic.identity_reduction_division, e)
     self.assertIn(expr_parse('a'), f)
예제 #3
0
 def test_distributivity_distribute(self):
     for e in ['(a + b) * c', '(b + a) * c', 'c * (a + b)', 'c * (b + a)']:
         f = pattern.transform(
             arithmetic.distributivity_distribute_multiplication,
             expr_parse(e))
         self.assertIn(expr_parse('a * c + b * c'), f)
     e = expr_parse('-(a + b)')
     f = pattern.transform(
         arithmetic.distributivity_distribute_unary_subtraction_addition, e)
     self.assertIn(expr_parse('-a - b'), f)
예제 #4
0
 def test_associativity(self):
     for e in ['(a + b) + c', '(b + a) + c', 'c + (a + b)', 'c + (b + a)']:
         f = pattern.transform(arithmetic.associativity_addition,
                               expr_parse(e))
         self.assertIn(expr_parse('a + (b + c)'), f)
         self.assertIn(expr_parse('b + (a + c)'), f)
     for e in ['(a * b) * c', '(b * a) * c', 'c * (a * b)', 'c * (b * a)']:
         f = pattern.transform(arithmetic.associativity_multiplication,
                               expr_parse(e))
         self.assertIn(expr_parse('a * (b * c)'), f)
         self.assertIn(expr_parse('b * (a * c)'), f)
예제 #5
0
 def test_constant_reduction(self):
     e = expr_parse('1 + 2')
     f = pattern.transform(arithmetic.constant_reduction, e)
     self.assertIn(expr_parse('3'), f)
예제 #6
0
 def test_double_negation_reduction(self):
     e = expr_parse('--a')
     f = pattern.transform(arithmetic.double_negation_reduction, e)
     self.assertIn(expr_parse('a'), f)
예제 #7
0
 def test_negation(self):
     e = expr_parse('a - b')
     f = pattern.transform(arithmetic.negation, e)
     self.assertIn(expr_parse('a + -b'), f)
예제 #8
0
 def test_distributivity_collect(self):
     e = expr_parse('c * a + b * c')
     f = pattern.transform(arithmetic.distributivity_collect_multiplication,
                           e)
     self.assertIn(expr_parse('(a + b) * c'), f)