def eval_with_loop(self, elts): new_elts = [] for elt in elts: elt = self.replace_loopvars_as_constants(copy.deepcopy(elt)) elt = PyBasicConversions().visit(elt) elt = ConstantFold().visit(elt) new_elts.append(elt.value) return tuple(new_elts)
def test_recursive_fold(self): tree = C.Assign( C.SymbolRef("c"), C.Add(C.Add(C.Constant(2), C.Constant(-2)), C.SymbolRef("b"))) tree = ConstantFold().visit(tree) self.assertEqual( str(tree), str(C.Assign(C.SymbolRef("c"), C.SymbolRef("b"))))
def test_no_folding(self): trees = [ C.Add(C.SymbolRef("a"), C.SymbolRef("b")), C.Sub(C.SymbolRef("a"), C.SymbolRef("b")), C.Mul(C.SymbolRef("a"), C.SymbolRef("b")), C.Div(C.SymbolRef("a"), C.SymbolRef("b")), ] for tree in trees: new_tree = ConstantFold().visit(tree) self.assertEqual(tree, new_tree)
def test_div_constant(self): tree = C.Div(C.Constant(20), C.Constant(10)) tree = ConstantFold().visit(tree) self.assertEqual(tree, C.Constant(2))
def test_sub_constant(self): tree = C.Sub(C.Constant(20), C.Constant(10)) tree = ConstantFold().visit(tree) self.assertEqual(tree, C.Constant(10))
def test_mul_constant(self): tree = C.Mul(C.Constant(20), C.Constant(10)) tree = ConstantFold().visit(tree) self.assertEqual(tree, C.Constant(200))
def test_add_constants(self): tree = C.Add(C.Constant(20), C.Constant(10)) tree = ConstantFold().visit(tree) self.assertEqual(tree, C.Constant(30))