Accept Merge Request #3 add tests : (tests -> master)

Merge Request: add tests
Created By: @virusdefender
Accepted By: @virusdefender
URL: https://coding.net/u/virusdefender/p/judger/git/merge/3
This commit is contained in:
virusdefender 2016-01-23 18:19:40 +08:00
commit 018d2b2d73
33 changed files with 215 additions and 6 deletions

3
.gitignore vendored
View File

@ -2,7 +2,6 @@
build/
a.out
limit
in
out
runner
CMakeLists.txt
main

10
.travis.yml Normal file
View File

@ -0,0 +1,10 @@
language: python
python:
- "2.7"
sudo: required
before_install:
- sudo apt-get -qq update
- sudo apt-get install libseccomp-dev
install:
- sudo python setup.py install
script: sudo python tests/test.py

9
README.md Normal file
View File

@ -0,0 +1,9 @@
[![Build Status](https://travis-ci.org/QingdaoU/Judger.svg?branch=master)](https://travis-ci.org/QingdaoU/Judger)
```
sudo python setup.py install
touch in out
gcc test.c -o test
sudo python demo.py
```

4
readme
View File

@ -1,4 +0,0 @@
sudo python setup.py install
touch in out
gcc test.c -o test
python demo.py

15
tests/1/Main.c Normal file
View File

@ -0,0 +1,15 @@
/*
*
*/
#include <stdio.h>
#include <string.h>
int main()
{
char input[100];
scanf("%s", input);
printf("%s\n", input);
printf("Hello world");
return 0;
}

1
tests/1/in Normal file
View File

@ -0,0 +1 @@
1234567890

2
tests/1/out Normal file
View File

@ -0,0 +1,2 @@
1234567890
Hello world

1
tests/1/result Normal file
View File

@ -0,0 +1 @@
{"flag": 0, "signal": 0}

36
tests/2/Main.c Normal file
View File

@ -0,0 +1,36 @@
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
// 1k
int small_size = 1024;
// 150m
int big_size = 150 * 1024 * 1024;
int *s = NULL, *b = NULL;
s = (int *)malloc(small_size);
if(s){
printf("malloc small size succedeed\n");
}
else{
printf("malloc small size failed\n");
return -1;
}
b = (int *)malloc(big_size);
if(b){
printf("malloc big size succedeed\n");
}
else{
printf("malloc big size failed\n");
return -2;
}
return 0;
}

0
tests/2/in Normal file
View File

2
tests/2/out Normal file
View File

@ -0,0 +1,2 @@
malloc small size succedeed
malloc big size succedeed

1
tests/2/result Normal file
View File

@ -0,0 +1 @@
{"flag": 0, "signal": 0}

21
tests/3/Main.c Normal file
View File

@ -0,0 +1,21 @@
/*
* clone
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
pid_t pid;
printf("fork test\n");
pid = fork();
if(pid < 0){
printf("fork failed\n");
}
else {
printf("fork succeeded\n");
}
return 0;
}

0
tests/3/in Normal file
View File

0
tests/3/out Normal file
View File

1
tests/3/result Normal file
View File

@ -0,0 +1 @@
{"flag": 4, "signal": 31}

14
tests/4/Main.c Normal file
View File

@ -0,0 +1,14 @@
/*
* exec
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main(int arg, char **args) {
char *argv[] = {"/bin/echo", "1234567890", NULL};
char *envp[] = {NULL};
printf("execve test\n");
execve("/bin/echo", argv, envp);
}

0
tests/4/in Normal file
View File

0
tests/4/out Normal file
View File

1
tests/4/result Normal file
View File

@ -0,0 +1 @@
{"flag": 4, "signal": 31}

26
tests/5/Main.c Normal file
View File

@ -0,0 +1,26 @@
/*
* fork
*/
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
__attribute((constructor))
void myinit(void)
{
if(fork() < 0) {
printf("fork failed\n");
}
else {
printf("fork succeeded\n");
}
}
int main(void)
{
return 0;
}

0
tests/5/in Normal file
View File

0
tests/5/out Normal file
View File

1
tests/5/result Normal file
View File

@ -0,0 +1 @@
{"flag": 4, "signal": 31}

17
tests/6/Main.c Normal file
View File

@ -0,0 +1,17 @@
/*
* write file
*/
#include <stdio.h>
#include <stdlib.h>
int main()
{
FILE * fp;
fp = fopen ("/dev/null", "w");
fprintf(fp, "test");
fclose(fp);
printf("write file succeesed");
return 0;
}

0
tests/6/in Normal file
View File

0
tests/6/out Normal file
View File

1
tests/6/result Normal file
View File

@ -0,0 +1 @@
{"flag": 4, "signal": 31}

13
tests/7/Main.c Normal file
View File

@ -0,0 +1,13 @@
/*
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main()
{
system("/bin/echo test");
return 0;
}

0
tests/7/in Normal file
View File

0
tests/7/out Normal file
View File

1
tests/7/result Normal file
View File

@ -0,0 +1 @@
{"flag": 4, "signal": 31}

41
tests/test.py Normal file
View File

@ -0,0 +1,41 @@
# coding=utf-8
import os
import json
import judger
import shutil
from unittest import TestCase, main
class JudgerTest(TestCase):
def setUp(self):
self.tmp_path = "/tmp/judger_test"
def compile_src(self, src_path, language, exe_path):
if language == "c":
return os.system("gcc %s -o %s" % (src_path, exe_path))
elif language == "cpp":
return os.system("g++ %s -o %s" % (src_path, exe_path))
elif language == "java":
pass
else:
raise ValueError("Invalid language")
def test_run(self):
shutil.rmtree(self.tmp_path, ignore_errors=True)
os.mkdir(self.tmp_path)
for i in range(1, 8):
test_dir = os.path.join(os.path.dirname(os.path.abspath(__file__)), str(i))
exe_path = os.path.join("/tmp/judger_test", str(i))
self.assertEqual(self.compile_src(os.path.join(test_dir, "Main.c"), "c", exe_path), 0)
run_result = judger.run(path=exe_path,
in_file=os.path.join(test_dir, "in"),
out_file=os.path.join(self.tmp_path, str(i) + ".out"),
max_cpu_time=2000, max_memory=200000000)
result = json.loads(open(os.path.join(test_dir, "result")).read())
self.assertEqual(result["flag"], run_result["flag"])
self.assertEqual(result["signal"], run_result["signal"])
self.assertEqual(open(os.path.join(test_dir, "out")).read(), open(os.path.join(self.tmp_path, str(i) + ".out")).read())
if __name__ == "__main__":
main()