示例#1
0
def define_algorithms(interface_text, sources):
    for language, source in sources.items():
        with Algorithm.load(
                source_text=source,
                language=language,
                interface_text=interface_text,
        ) as impl:
            yield impl
示例#2
0
def test_sandbox_smoke():
    with Algorithm.load(
            interface_text=interface_text,
            language="python",
            source_text="""if True:
                def test():
                    return 3
            """,
    ) as algo:
        with algo.run() as p:
            assert p.call.test() == 3
示例#3
0
def test_multiple_reads():
    with Algorithm.load(
            interface_text="""
                function f(int a, int b) -> int;
                main {
                    var int a, b, c;
                    read a;
                    read b;
                    call f(a, b) -> c;
                    write c;
                }
            """,
            language="javascript",
            source_text="function f(a, b) { return 1; }"
    ) as algo:
        with algo.run() as process:
            assert process.call.f(2, 5) == 1
示例#4
0
    def evaluate(self, *, prepared_problem_dir, submission_dir):
        script_globals = {}
        with open(self.script_path) as f:
            script = compile(f.read(), self.script_path, mode="exec")

        context = ProblemEvaluationContext(prepared_problem_dir)
        script_globals["context"] = context

        eval_stdout = StringIO()
        with redirect_stdout(eval_stdout):
            exec(script, script_globals)
            data = script_globals[self.function_name](
                Algorithm(submission_dir))

        return Evaluation(
            stdout=eval_stdout.getvalue().splitlines(),
            data=data,
        )
示例#5
0
def test_sandbox_error():
    with Algorithm.load(
            interface_text="""
                function test();
                main {
                    call test();
                    checkpoint;
                }
            """,
            source_text="""
                #include <cstdlib>
                void test() {
                    exit(0);
                }
            """,
            language="c++",
    ) as algo:
        with pytest.raises(SandboxError):
            with algo.run() as p:
                p.call.test()
示例#6
0
def test_get_time_memory_usage():
    with Algorithm.load(
            interface_text="""
                function test(int i) -> int;
                main {
                    var int i1, i2, o1, o2;
                    read i1;
                    call test(i1) -> o1;
                    write o1;
                    flush;
                    read i2;
                    call test(i2) -> o2;
                    write o2;
                }
            """,
            language="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:
            with p.section() as section1:
                p.call.test(1)
            info1 = p.sandbox.get_info()
            with p.section() as section2:
                p.call.test(2)
            info2 = p.sandbox.get_info()

    assert 0 < section1.time_usage == info1.time_usage < 0.5
    assert 0 < section2.time_usage < 0.5

    assert section1.time_usage + section2.time_usage == pytest.approx(info2.time_usage)

    assert 1024 * 1024 < info1.memory_usage < 2 * 1024 * 1024
    assert info2.memory_usage == 0
示例#7
0
 def algorithms(self):
     root, dirs, files = next(
         os.walk(os.path.join(self.evaluation_dir, "algorithms")))
     return {name: Algorithm(os.path.join(root, name)) for name in dirs}
示例#8
0
 def submission(self):
     return Algorithm(os.path.join(self.evaluation_dir, "submission"))
示例#9
0
def java_algorithm(interface, java_source):
    return Algorithm.load(
        interface_text=interface,
        language="java",
        source_text=java_source,
    )
示例#10
0
def test_valid_types(language, source_text):
    with Algorithm.load(
            interface_text="""
                var int i;
                var int[] ia;
                var int[][] iaa;
            
                function get_i() -> int;
                function get_ia(int j) -> int;
                function get_iaa(int j, int k) -> int;
            
                init {
                    read i;
                    alloc ia, iaa : i;
                    for(j : i) {
                        read ia[j];
                        alloc iaa[j] : ia[j];
                        for(k : ia[j]) {
                            read iaa[j][k];
                        }
                    }
                }
            
                main {
                    var int o;
                    var int[] oa;
                    var int[][] oaa;
            
                    call get_i() -> o;
                    alloc oa, oaa : i;
                    for(j : i) {
                        call get_ia(j) -> oa[j];
                        alloc oaa[j] : ia[j];
                        for(k : ia[j]) {
                            call get_iaa(j, k) -> oaa[j][k];
                        }
                    }
            
                    write o;
                    for(j : i) {
                        write oa[j];
                        for(k : ia[j]) {
                            write oaa[j][k];
                        }
                    }
                }
            """,
            language=language,
            source_text=source_text,
    ) as algo:
        iaa = [
            [1, 2, 3],
            [],
            [4, 5],
        ]
        ia = [len(x) for x in iaa]
        i = len(ia)

        with algo.run(global_variables=dict(iaa=iaa, ia=ia, i=i)) as p:
            assert i == p.call.get_i()
            for j in range(i):
                assert ia[j] == p.call.get_ia(j)
                for k in range(ia[j]):
                    assert iaa[j][k] == p.call.get_iaa(j, k)
示例#11
0
def javascript_algorithm(source):
    return Algorithm.load(
        interface_text=interface_text,
        language="javascript",
        source_text=source,
    )
示例#12
0
def cpp_algorithm(source):
    return Algorithm.load(
        interface_text=protocol_text,
        language="c++",
        source_text=source,
    )