def test_embed_in_true(self, nrcv): prog = Conditional(Number(0), [ FunctionDefinition("foo1", Function([], [])), FunctionDefinition("foo2", Function([], [Number(1)])), Conditional(Number(0), [], []) ], None) assert nrcv(prog) == set(["foo1"])
def test_embed_in_false(self, nrcv): prog = Conditional(Number(0), None, [ FunctionDefinition("foo3", Function([], [])), FunctionDefinition("foo4", Function([], [Number(3)])), Conditional(Number(0), [], []) ]) assert nrcv(prog) == set(["foo3"])
def test_embedded_in_bad_if(self, nrcv): prog = Conditional(Number(0), [ FunctionDefinition("foo1", Function([], [])), FunctionDefinition("foo2", Function([], [Number(1)])), Conditional(Number(0), [], []) ], [ FunctionDefinition("foo3", Function([], [])), FunctionDefinition("foo4", Function([], [Number(3)])), Conditional(Number(0), [], []) ]) assert nrcv(prog) == set(["foo1", "foo3"])
def test_embedded(self, nrcv): prog = FunctionDefinition("foo1", Function([], [ Conditional(Number(0), [], []), FunctionDefinition("foo2", Function([], [ FunctionDefinition("foo3", Function([], [])), ])), FunctionDefinition("foo4", Function([], [ FunctionDefinition("foo5", Function([], [])), Conditional(Number(0), [], []) ])), FunctionDefinition("foo6", Function([], [])) ])) assert nrcv(prog) == set(["foo3", "foo4", "foo5", "foo6"])
def test_embedded(self, nrcv): prog = Conditional(Number(0), [ Conditional(Number(0), [], []), FunctionDefinition("foo1", Function([], [])), FunctionDefinition("foo2", Function([], [Number(1)])), Number(2), ], [ Conditional(Number(0), [], []), FunctionDefinition("foo3", Function([], [])), FunctionDefinition("foo4", Function([], [Number(3)])), Number(4), ]) assert nrcv(prog) == set(["foo1", "foo3"])
def my_tests(): reader = Print(Read('n')) print_binary = Print( BinaryOperation( Reference('x'), '/', UnaryOperation('!', BinaryOperation(Reference('y'), '%', Number(2))))) conditional = Conditional( BinaryOperation(BinaryOperation(Reference('x'), '*', Number(2)), '<', UnaryOperation('-', Reference('y'))), [ print_binary, UnaryOperation('-', UnaryOperation('!', Reference('s'))) ], []) definition_max3 = FunctionDefinition( 'max3', Function(["a", "b", "c"], [ Conditional(BinaryOperation(Reference("a"), ">", Reference("b")), [ Conditional( BinaryOperation(Reference("a"), ">=", Reference("c")), [Reference("a")], [Reference("c")]) ], [ Conditional( BinaryOperation(Reference("b"), "<", Reference("c")), [Reference("c")], [Reference("b")]) ]) ])) fun_call = Print(FunctionCall(definition_max3, [Number(1), Number(2)])) definition_log2 = FunctionDefinition( "log2", Function(["k", "n", "step"], [ Conditional( BinaryOperation( BinaryOperation(Reference("k"), "*", Number(2)), "<=", Reference("n")), [ FunctionCall(Reference("log_2_def"), [ BinaryOperation(Reference("k"), "*", Number(2)), Reference("n"), BinaryOperation(Reference("step"), "+", Number(1)) ]) ], [Reference("step")]) ])) empty_func = FunctionDefinition("emptyFunc", Function([], [])) printer = PrettyPrinter() printer.visit(reader) printer.visit(conditional) printer.visit(print_binary) printer.visit(definition_max3) printer.visit(fun_call) printer.visit(definition_log2) printer.visit(empty_func)
def test_smoke2(self, pcv): prog2 = Conditional(BinaryOperation(Number(4), "=", Number(5)), [ Print(Number(123)), ], [ Number(456), ]) assert pcv(prog2) == False
def test_smoke1(self, pcv): prog1 = Conditional(BinaryOperation(Number(4), "=", Number(5)), [ Number(123), ], [ Number(456), ]) assert pcv(prog1) == True
def test_embed_in_last(self, nrcv): def func_def(name): return FunctionCall(FunctionDefinition(name, Function([], [])), []) def comb_func_def(name1, name2): return Conditional(Number(0), [func_def(name1)], [func_def(name2)]) prog = Conditional(Number(0), [comb_func_def("foo1", "foo2")], [comb_func_def("foo3", "foo4")]) assert nrcv(prog) == set(["foo1", "foo2", "foo3", "foo4"])
def visitConditional(self, cond): condition = cond.condition.visit(self) if_true = None if_false = None if cond.if_true: if_true = self.visitBody(cond.if_true) if cond.if_false: if_false = self.visitBody(cond.if_false) return Conditional(condition, if_true, if_false)
def visitConditional(self, cond): cond_without_const = cond.condition.accept(self) if_true_without_const = [elem.accept(self) for elem in cond.if_true ] if cond.if_true != None else None if_false_without_const = [elem.accept(self) for elem in cond.if_false ] if cond.if_false != None else None conditional_without_const = Conditional(cond_without_const, if_true_without_const, if_false_without_const) return conditional_without_const
def test_smoke(self, nrcv): prog = ( Conditional(Number(1), [ FunctionDefinition("foo", Function([], [ Number(123), Conditional(Number(1), [ Number(2), ]) ])), FunctionDefinition("bar", Function([], [ Number(123) ])), FunctionDefinition("baz", Function([], [ ])), FunctionDefinition("foobar", Function([], [ Conditional(Number(1), [ Number(2), ]), Number(123) ])), ]) ) assert nrcv(prog) == set(["foo", "baz"])
def test(): f1 = Conditional(FunctionCall(Reference('x'), [Number(22)]), [Conditional(BinaryOperation(Number(0), '-', Number(6)), [],[Conditional(Number(0), [Conditional(UnaryOperation('-', Number(20)), [],[FunctionDefinition('foobar', Function(['ab', 'cd'], [ Print(BinaryOperation(UnaryOperation('-', Number(120)), '*', BinaryOperation(UnaryOperation('-', Number(20)), '+', Reference('z')))), Read('x') ]))])],[])])],[Conditional(Number(0), [Conditional(Number(0), [],[Read('xxx')])],[])]) f = FunctionDefinition('foo', Function(['a', 'b'], [ FunctionDefinition('bar', Function(['c', 'd'], [ Read('c') ])), Conditional(Number(6), [Conditional(Number(5), [Read('x')])],[f1]) ])) pr = PrettyPrinter() pr.visit(f)
def test_conditional_unpure_true_empty(self, pcv): assert pcv(Conditional(Number(0), [Number(10), Print(Number(0)), Number(20)], [])) == False
def test_conditional_non_empty(self, pcv): assert pcv(Conditional(Number(0), [Number(10), Number(20)], [Number(30), Number(40)])) == True
def test_conditional_none(self, pcv): assert pcv(Conditional(Number(0), None, None)) == True
def test_conditional_empty(self, pcv): assert pcv(Conditional(Number(0), [], [])) == True
def test_embedded(self, nrcv): bad = Conditional(Number(0), [], []) def func_def(name): return FunctionDefinition(name, Function([], [])) prog = UnaryOperation("-", func_def("foo1")) assert nrcv(prog) == set(["foo1"])
def test_good_expr_with_bad_embed(self, nrcv_good): assert nrcv_good(UnaryOperation("-", Conditional(Number(0), [], [])))
def test_embedded_and_bad_rhs(self, nrcv): bad = Conditional(Number(0), [], []) def func_def(name): return FunctionDefinition(name, Function([], [])) prog = BinaryOperation(func_def("foo1"), "+", bad) assert nrcv(prog) == set(["foo1"])
def test_good_with_bad_both(self, nrcv_good): assert nrcv_good(BinaryOperation(Conditional(Number(0), [], []), "+", Conditional(Number(0), [], [])))
def test_embed(self, nrcv): bad = Conditional(Number(0), [], []) def func_def(name): return FunctionDefinition(name, Function([], [])) prog = FunctionCall(func_def("foo0"), [Number(1), func_def("foo1"), bad, func_def("foo2"), Number(3)]) assert nrcv(prog) == set(["foo0", "foo1", "foo2"])
def test_bad_with_cond(self, nrcv_good): assert not nrcv_good(FunctionCall(Reference("hello"), [Number(1), Conditional(Number(0), [], []), Number(3)]))
def test_conditional_unpure_cond_empty(self, pcv): assert pcv(Conditional(Print(Number(0)), [], [])) == False
def test_conditional_unpure_false_empty(self, pcv): assert pcv(Conditional(Number(0), [], [Number(30), Print(Number(0)), Number(40)])) == False
def test_bad_not_last(self, nrcv_good): bad = Conditional(Number(0), [], []) assert nrcv_good(Conditional(Number(0), [bad, Number(1)], [bad, Number(2)]))
def test_conditional_unpure_cond_non_empty(self, pcv): assert pcv(Conditional(Print(Number(0)), [Number(10), Number(20)], [Number(30), Number(0), Number(40)])) == False
def comb_func_def(name1, name2): return Conditional(Number(0), [func_def(name1)], [func_def(name2)])
def test_function_call_unpure_expr(self, pcv): cond = Conditional(Number(1), [Print(Number(1)), Reference("foo")]) assert pcv(FunctionCall(cond, [])) == False
def test_good_with_bad_embed(self, nrcv_good): assert nrcv_good(Print(Conditional(Number(0), [], [Number(10)])))