예제 #1
0
def test_function_return_value():
    for algo in define_algorithms(
            interface_text="""
                function f(int a) -> int;
                main {
                    var int a, b;
                    read a;
                    call f(a) -> b;
                    write b;
                }
            """,
            sources={
                'c++': """
                    #include <cassert>
                    int f(int a) {
                        assert(a == 1);
                        return 2;
                    }
                """,
                'python': """if True:
                    def f(a):
                        assert a == 1
                        return 2
                """,
                'javascript': """function f(a, b) { return 2; }""",
            },
    ):
        with algo.run() as p:
            assert p.call.f(1) == 2
예제 #2
0
def test_function_with_arguments():
    for algo in define_algorithms(interface_text="""
                function f(int a, int b);
                main {
                    var int a, b;
                    read a, b;
                    call f(a, b);
                    checkpoint;
                }
            """,
                                  sources={
                                      'c++':
                                      """
                    #include <cassert>
                    void f(int a, int b) {
                        assert(a == 1 && b == 2);
                    }
                """,
                                      'python':
                                      """if True:
                    def f(a,b):
                        assert a == 1 and b == 2
                """,
                                  }):
        with algo.run() as p:
            p.call.f(1, 2)
예제 #3
0
def test_callback_no_arguments_cpp():
    for algo in define_algorithms(
            interface_text="""
                callback c() {}
                function test();
                main {
                    call test();
                }
            """,
            sources={
                'c++': """
                    void c();
                    void test() {
                        c();
                        c();
                    }
                """,
                'python': """if True:
                    from skeleton import c
                    
                    def test():
                        c()
                        c()
                """,
            }
    ):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls)
            p.call.test(c=lambda: c())

            assert calls == [
                (c, ()),
                (c, ()),
            ]
예제 #4
0
def test_interface_multiple_callbacks():
    for algo in define_algorithms(
            interface_text="""
                callback cb1() {}
                callback cb2() {}
                function test() -> int;
                main {
                    var int o;
                    call test() -> o;
                    write o;
                }
            """,
            sources={
                'c++': """
                    void cb1();
                    void cb2();
                    
                    int test() {
                        cb1();
                        cb2();
                        cb2();
                        cb1();
                        return 1;
                    }
                """,
                'python': """if True:
                    from skeleton import cb1, cb2
                    def test():
                        cb1()
                        cb2()
                        cb2()
                        cb1()
                        return 1
                """,
            },
    ):
        with algo.run() as p:
            calls = []
            cb1 = callback_mock(calls)
            cb2 = callback_mock(calls)
            assert p.call.test(cb1=cb1, cb2=cb2) == 1
            assert calls == [
                (cb1, ()),
                (cb2, ()),
                (cb2, ()),
                (cb1, ()),
            ]
예제 #5
0
def test_callback_return_value():
    for algo in define_algorithms(
            interface_text="""
                callback c(int a) -> int {
                    write a;
                    flush;
                    var int b;
                    read b;
                    return b;
                }
                function test();
                main {
                    call test();
                    checkpoint;
                }
            """,
            sources={
                'c++': """
                    #include <cassert>
                    int c(int a);
                    void test() {
                        assert(c(1) == 2);
                        assert(c(3) == 4);
                    }
                """,
                'python': """if True:
                    from skeleton import c
                    def test():
                        assert c(1) == 2
                        assert c(3) == 4
                """
            }
    ):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls, [2, 4])
            p.call.test(c=lambda a: c(a))

            assert calls == [
                (c, (1,)),
                (c, (3,)),
            ]
예제 #6
0
def test_function_no_arguments():
    for algo in define_algorithms(
            interface_text="""
                function f();
                main {
                    call f();
                    checkpoint;
                }
            """,
            sources={
                'c++':
                """
                    void f() {
                    }
                """,
                'python':
                """if True:
                    def f():
                        pass
                """
            },
    ):
        with algo.run() as p:
            p.call.f()
예제 #7
0
def test_callback_with_arguments():
    for algo in define_algorithms(
            interface_text="""
                callback c(int a, int b) {
                    write a, b;
                }
                function test();
                main {
                    call test();
                }
            """,
            sources={
                'c++': """
                    void c(int a, int b);
                    void test() {
                        c(1, 2);
                        c(3, 4);
                    }
                """,
                'python': """if True:
                    from skeleton import c
                    def test():
                        c(1, 2)
                        c(3, 4)
                """,
            },
    ):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls)
            p.call.test(c=lambda a, b: c(a, b))

            assert calls == [
                (c, (1, 2)),
                (c, (3, 4)),
            ]
예제 #8
0
def test_interface_no_callbacks():
    for algo in define_algorithms(
            interface_text="""
                function test() -> int;
                main {
                    var int o;
                    call test() -> o;
                    write o;
                }
            """,
            sources={
                'c++': """
                    int test() {
                        return 1;
                    }
                """,
                'python': """if True:
                    def test():
                        return 1
                """,
            },
    ):
        with algo.run() as p:
            assert p.call.test() == 1