def test_get_time_memory_usage(): with define_algorithm(interface_text=""" function test(i); main { read i1; call o1 = test(i1); write o1; read i2; call o2 = test(i2); write o2; } """, language_name="C++", source_text=""" int test(int i) { char x[1024 * 1024]; for(int j = 0; j < 100 * 1000 * 1000; j++) { i = x[j%1024] = j^i^x[j%1024]; } return i; } """) as algo: with algo.run() as p: # info0 = p.sandbox.get_info() with p.section() as section1: p.functions.test(1) # info1 = p.get_info() with p.section() as section2: p.functions.test(2) # info2 = p.get_info() assert 0 < section1.time_usage < 0.5 assert 0 < section2.time_usage < 0.5
def my_algo(): return define_algorithm( interface_text=""" procedure fast(a); procedure slow(a); main { read i; call fast(i); checkpoint; read j; call slow(j); checkpoint; } """, language_name="Python", source_text="""if True: def fast(x): pass def slow(x): for i in range(1000000): pass """, )
def test_if_calls(): with define_algorithm( interface_text=""" function f1(); function f2(); main { read condition; if condition { call res = f1(); write res; } call res2 = f2(); write res2; } """, language_name="C++", source_text=""" int f1() {return 1;} int f2() {return 2;} """, ) as algo: with algo.run() as p: assert p.functions.f1() == 1 assert p.functions.f2() == 2 with algo.run() as p: assert p.functions.f2() == 2
def test_multiple_call_function_args(): with define_algorithm( interface_text=""" function f(x); main { read x; call a = f(x); call b = f(x); write a, b; } """, language_name="C++", source_text=""" #include <cassert> int i = 0; int f(int x) { assert(x == 2); return i++; } """, ) as algo: with algo.run() as p: for i in range(2): assert p.functions.f(2) == i
def test_sandbox_smoke(): with define_algorithm( interface_text=interface_text, language_name="Python", source_text="""if True: def test(): return 3 """, ) as algo: with algo.run() as p: assert p.functions.test() == 3
def test_io_garbage(): with define_algorithm( interface_text=INTERFACE_TEXT, language_name="C++", source_text=r""" #include <cstdio> void p() { for(;;) printf("garbage\n"); } """, ) as algo: with pytest.raises(AlgorithmRuntimeError) as exc_info: with algo.run() as p: p.procedures.p() p.checkpoint() assert "sent invalid data" in exc_info.value.message
def test_timeout(): with define_algorithm( interface_text=INTERFACE_TEXT, language_name="C++", source_text=""" void p() { for(;;); } """, ) as algo: with pytest.raises(AlgorithmRuntimeError) as exc_info: with algo.run() as p: p.procedures.p() p.checkpoint() assert "stopped sending data" in exc_info.value.message assert "timeout expired" in exc_info.value.message
def test_switch(): with define_algorithm( interface_text=interface_text, language_name="C++", source_text=""" int f1() {return 1;} int f2() {return 2;} int f3() {return 3;} """, ) as algo: with algo.run() as p: assert p.functions.f1() == 1 with algo.run() as p: assert p.functions.f2() == 2 with algo.run() as p: assert p.functions.f3() == 3
def test_multiple_call_function_no_args(): with define_algorithm( interface_text=""" function f(); main { call a = f(); call b = f(); write a, b; } """, language_name="C++", source_text="int i = 0; int f() { return i++; }", ) as algo: with algo.run() as p: for i in range(2): assert p.functions.f() == i
def test_multiple_call_procedure_no_args(): with define_algorithm( interface_text=""" procedure p(); main { call p(); call p(); checkpoint; } """, language_name="C++", source_text="void p() {}", ) as algo: with algo.run() as p: for i in range(2): p.procedures.p() p.checkpoint()
def test_multiple_function_return_value(): with define_algorithm( interface_text=""" function sum(a, b); main { for i to 10 { read x, y; call ans = sum(x, y); write ans; } } """, language_name="C++", source_text=""" int sum(int a, int b) {return a + b;} """, ) as algo: with algo.run() as p: for i in range(10): assert p.functions.sum(i, i) == 2 * i
def my_algo(): return define_algorithm( interface_text=""" procedure p1(a); procedure p2(a); procedure p3(a); main { read i1; call p1(i1); checkpoint; read i2; call p2(i2); checkpoint; read i3; call p3(i3); checkpoint; } """, language_name="C++", source_text=""" char *p; void p1(int x) { int N = 100000000; // 100Mb p = new char[N]; for(int i = 0; i < N; i++) { p[i] = i; } } void p2(int x) { delete p; } void p3(int x) { } """, )
def test_multiple_call_procedure_args(): with define_algorithm( interface_text=""" procedure p(x); main { read x; call p(x); call p(x); checkpoint; } """, language_name="C++", source_text=""" #include <cassert> void p(int x) { assert(x == 1); } """, ) as algo: with algo.run() as p: for i in range(2): p.procedures.p(1) p.checkpoint()
def java_algorithm(interface, java_source): return define_algorithm( interface_text=interface, language_name="Java", source_text=java_source, )
def cpp_algorithm(source): return define_algorithm( interface_text=protocol_text, language_name="C++", source_text=source, )
def javascript_algorithm(source): return define_algorithm( interface_text=interface_text, language_name="java", source_text=source, )