def test_a_add_b(self): code = ''' #include <stdio.h> int main() { int a, b; scanf("%d %d", &a, &b); printf("%d\\n", a + b); return 0; } ''' compile_it(code) in_data = '1 2' with open('input.txt', 'w') as fw: fw.write(in_data) jl = JudgeLight('./a.out', input_file_path='input.txt', output_file_path='output.txt') jl.run() with open('output.txt') as fr: self.assertEqual(fr.read().strip(), '3') clear_it() os.remove('input.txt') os.remove('output.txt')
def test_output_size_limit(self): code = ''' # include <stdio.h> int main() { for (int i = 0; i < 1000000; i++) { printf("Hello World!\\n"); } return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', output_size_limit=10240, output_file_path='output.txt') stats = jl.run() self.assertEqual(stats['re_flag'], 1) with open('output.txt') as fr: l = len(fr.read()) self.assertTrue(l == 10240) clear_it() os.remove('output.txt')
def test_re(self): code = ''' #include <stdio.h> int main() { int a = 0 / 0; } ''' compile_it(code) jl = JudgeLight('./a.out') stats = jl.run() self.assertEqual(stats['re_flag'], 1) clear_it()
def test_signum(self): code = ''' #include <stdio.h> int main() { return 1; } ''' compile_it(code) jl = JudgeLight('./a.out') stats = jl.run() self.assertEqual(stats['signum'], 1) clear_it()
def test_cpu_time_limit(self): code = ''' int main() { while (1); } ''' compile_it(code) jl = JudgeLight('./a.out', time_limit=1000) stats = jl.run() self.assertEqual(stats['re_flag'], 1) # CPU 时间限制只能精确到秒,因此这里放宽限制 self.assertTrue(1000 <= stats['time_used'] <= 3000) clear_it()
def test_real_time_limit(self): code = ''' # include <unistd.h> int main() { sleep(10); } ''' compile_it(code) jl = JudgeLight('./a.out', real_time_limit=1000) stats = jl.run() self.assertEqual(stats['re_flag'], 1) self.assertTrue(1000 <= stats['real_time_used'] <= 1500) clear_it()
def test_fork(self): # nopass code = ''' #include <unistd.h> int main() { int pid = fork(); return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default') stats = jl.run() self.assertEqual(stats['re_flag'], 1) self.assertTrue(stats['re_syscall'] != 0 and stats['re_syscall'] != -1) clear_it()
def test_new(self): # pass code = ''' #include <iostream> using namespace std; int main() { int *a = new int; *a = 2; return *a; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default') stats = jl.run() self.assertEqual(stats['re_flag'], 0) clear_it()
def test_malloc(self): # pass code = ''' #include <stdio.h> #include <stdlib.h> int main() { int *a = (int *) malloc(sizeof(int)); *a = 1; return *a; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default') stats = jl.run() self.assertEqual(stats['re_flag'], 0) clear_it()
def test_openat(self): # nopass code = ''' #include <fcntl.h> int main() { openat(0, "~/JudgeLight/tests/input.txt", O_CREAT | O_RDWR); return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default') stats = jl.run() self.assertEqual(stats['re_flag'], 1) self.assertTrue(stats['re_syscall'] != 0 and stats['re_syscall'] != -1) clear_it()
def test_exec(self): # nopass code = ''' #include <unistd.h> int main() { char *const args[] = {"echo", "Hello", NULL}; execvp("echo", args); return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default') stats = jl.run() self.assertEqual(stats['re_flag'], 1) self.assertTrue(stats['re_syscall'] != 0 and stats['re_syscall'] != -1) clear_it()
def test_system(self): # nopass code = ''' #include <stdlib.h> int main() { system("echo Hello World"); } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default', output_file_path='output.txt') stats = jl.run() self.assertEqual(stats['re_flag'], 1) with open('output.txt') as fr: self.assertEqual(fr.read().strip(), '') clear_it() os.remove('output.txt')
def test_hello(self): # pass code = ''' #include <stdio.h> int main() { printf("Hello World!\\n"); return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', allow_system_calls_rule='default', output_file_path='output.txt') stats = jl.run() self.assertEqual(stats['re_flag'], 0) with open('output.txt') as fr: self.assertEqual(fr.read().strip(), 'Hello World!') clear_it() os.remove('output.txt')
def test_memory_limit(self): code = ''' # include <stdio.h> # include <stdlib.h> # define MAX 2000000 int *a[MAX]; int main(){ for (int i = 0; i < MAX; i++) { a[i] = (int *) malloc(sizeof(int)); } for (int i = 0; i < MAX; i++) { *a[i] = i; } int ans = 0; for (int i = 0; i < MAX; i++) { ans ^= *a[i]; } printf("%d\\n", ans); return 0; } ''' compile_it(code) jl = JudgeLight('./a.out', output_file_path='output.txt', memory_limit=4096, trace=False) stats = jl.run() self.assertEqual(stats['re_flag'], 1) clear_it() os.remove('output.txt')