Exemple #1
0
def test_interface_one_callback():
    for algo in define_algorithms(interface_text="""
                function test() callbacks {
                    procedure cb();
                }
                main {
                    call o = test();
                    write o;
                }
            """,
                                  sources={
                                      'C++':
                                      """
                    int test(void cb()) {
                        cb();
                        cb();
                        return 1;
                    }
                """,
                                      'Python':
                                      """if True:
                    def test(cb):
                        cb()
                        cb()
                        return 1
                """,
                                  }):
        with algo.run() as p:
            calls = []
            cb = callback_mock(calls)
            assert p.functions.test(callbacks=[cb]) == 1
            assert calls == [
                (cb, ()),
                (cb, ()),
            ]
Exemple #2
0
def test_callback_return_value():
    for algo in define_algorithms(interface_text="""
                procedure test() callbacks {
                    function c(a);
                }
                main {
                    call test();
                }
            """,
                                  sources={
                                      'C++':
                                      """
                    #include <cassert>
                    void test(int c(int a)) {
                        assert(c(1) == 2);
                        assert(c(3) == 4);
                    }
                """,
                                      'Python':
                                      """if True:
                    def test(c):
                        assert c(1) == 2
                        assert c(3) == 4
                """
                                  }):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls, [2, 4])
            p.procedures.test(callbacks=[lambda a: c(a)])

            assert calls == [
                (c, (1, )),
                (c, (3, )),
            ]
Exemple #3
0
def test_callback_no_arguments():
    for algo in define_algorithms(interface_text="""
                procedure test() callbacks {
                    procedure c();
                }
                
                main {
                    call test();
                }
            """,
                                  sources={
                                      'C++':
                                      """
                    void test(void c()) {
                        c();
                        c();
                    }
                """,
                                      'Python':
                                      """if True:                    
                    def test(c):
                        c()
                        c()
                """,
                                  }):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls)
            p.procedures.test(callbacks=[lambda: c()])

            assert calls == [
                (c, ()),
                (c, ()),
            ]
Exemple #4
0
def test_interface_no_callbacks():
    for algo in define_algorithms(
            interface_text="""
                function test();
                main {
                    call o = test();
                    write o;
                }
            """,
            sources={
                'C++':
                """
                    int test() {
                        return 1;
                    }
                """,
                'Python':
                """if True:
                    def test():
                        return 1
                """,
            },
    ):
        with algo.run() as p:
            assert p.functions.test() == 1
Exemple #5
0
def test_method_return_value():
    for algo in define_algorithms(
            interface_text="""
                function f(a);
                main {
                    read a;
                    call b = f(a);
                    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.functions.f(1) == 2
Exemple #6
0
def test_method_no_arguments():
    for algo in define_algorithms(
            interface_text="""
                procedure f();
                main {
                    call f();
                    checkpoint;
                }
            """,
            sources={
                'C++':
                """
                    void f() {
                    }
                """,
                'Python':
                """if True:
                    def f():
                        pass
                """
            },
    ):
        with algo.run() as p:
            p.procedures.f()
            p.checkpoint()
Exemple #7
0
def test_method_with_arguments():
    for algo in define_algorithms(interface_text="""
                procedure f(a, b);
                main {
                    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.procedures.f(1, 2)
            p.checkpoint()
def test_constant():
    for algo in define_algorithms(interface_text="""
                const c = 2;
                procedure p();
                main {
                    for i to c {
                        call p();
                        checkpoint;
                    }
                }
            """,
                                  sources={
                                      'C++': "void p() {}",
                                      'Python': "def p(): pass",
                                  }):
        with algo.run() as p:
            for i in range(2):
                p.procedures.p()
                p.checkpoint()
Exemple #9
0
def test_callback_with_arguments():
    for algo in define_algorithms(
            interface_text="""
                procedure test() callbacks {
                    procedure c(a, b);
                }
                
                main {
                    call test() callbacks {
                        procedure c(a, b) {
                            write a;
                            write b;
                        }
                    }
                }
            """,
            sources={
                'C++':
                """
                    void test(void c(int a, int b)) {
                        c(1, 2);
                        c(3, 4);
                    }
                """,
                'Python':
                """if True:
                    def test(c):
                        c(1, 2)
                        c(3, 4)
                """,
            },
    ):
        with algo.run() as p:
            calls = []
            c = callback_mock(calls)
            p.procedures.test(callbacks=[lambda a, b: c(a, b)])

            assert calls == [
                (c, (1, 2)),
                (c, (3, 4)),
            ]