Judger/tests/testcase/base.py
2016-08-25 09:40:45 +08:00

45 lines
1.3 KiB
Python

# coding=utf-8
import os
import shutil
from unittest import TestCase
class RunResult(object):
cpu_time_limited = 1
real_time_limit_exceeded = 2
memory_limit_exceeded = 3
runtime_error = 4
system_error = 5
class BaseTestCase(TestCase):
def init_workspace(self, language):
base_workspace = "/tmp"
workspace = os.path.join(base_workspace, language)
shutil.rmtree(workspace, ignore_errors=True)
os.system("mkdir -p " + workspace)
return workspace
def rand_str(self):
return ''.join(map(lambda xx:(hex(ord(xx))[2:]), os.urandom(16)))
def _compile(self, src_name):
path = os.path.dirname(os.path.abspath(__file__))
exe_path = os.path.join(self.workspace, src_name.split("/")[-1].split(".")[0])
cmd = "gcc {0} -g -O0 -o {1}".format(os.path.join(path, src_name), exe_path)
if os.system(cmd):
raise AssertionError("compile error, cmd: {0}".format(cmd))
return exe_path
def make_input(self, content):
path = os.path.join(self.workspace, self.rand_str())
with open(path, "w") as f:
f.write(content)
return path
def output_path(self):
return os.path.join(self.workspace, self.rand_str())
def output_content(self, path):
with open(path, "r") as f:
return f.read()