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)
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)
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)
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)
def test_constant_reduction(self): e = expr_parse('1 + 2') f = pattern.transform(arithmetic.constant_reduction, e) self.assertIn(expr_parse('3'), f)
def test_double_negation_reduction(self): e = expr_parse('--a') f = pattern.transform(arithmetic.double_negation_reduction, e) self.assertIn(expr_parse('a'), f)
def test_negation(self): e = expr_parse('a - b') f = pattern.transform(arithmetic.negation, e) self.assertIn(expr_parse('a + -b'), f)
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)