Exemplo n.º 1
0
def testgen ():
    numcases = globals.getnumcases()
    execorig = globals.getsetting('executable')
    execloc = os.path.join(globals.sandboxdir, execorig.name)
    execfile = open(execloc, 'wb')
    execfile.write(execorig.read())
    execfile.close()
    os.chmod(execloc, 0755)
    print 'Automatically generating {} test cases for problem {} with {}'.format(numcases, globals.getprob(), execorig.name)
    curtest = 0
    for i in range(numcases):
        while True:
            inloc = os.path.join(globals.probdir, 'pcugen{:03d}.in'.format(curtest))
            ansloc = os.path.join(globals.probdir, 'pcugen{:03d}.ans'.format(curtest))
            if not os.path.exists(inloc):
                break
            curtest += 1
        print 'Generating case {:03d}'.format(curtest)
        execresult = helper.runproc('./{} {} {}'.format(execorig.name, curtest, random.randint(0, 2147483647)), globals.sandboxdir, timelim=10.0)
        if execresult[0] != 0:
            print 'Generator executable error'
            break
        infile = open(inloc, 'wb')
        ansfile = open(ansloc, 'wb')
        infile.write(execresult[2])
        ansfile.write(execresult[3])
        infile.close()
        ansfile.close()
        curtest += 1
    log.addmsg('Generated {} tests for problem {} with {}'.format(numcases, globals.getprob(), execorig.name))
Exemplo n.º 2
0
def compile():
    print 'Compiling ' + globals.getsrc()
    lang = settings.langexts[globals.getext()]
    print 'Language detected: ' + lang
    if not helper.copy(os.path.join(globals.working, globals.getsrc()),
                       globals.sandboxdir):
        print 'Source file not found in working directory'
        return False
    compcommand = settings.compcommands[lang]
    if compcommand is None:
        print 'No compilation necessary'
        return True
    else:
        compcommand = pattern.convert(compcommand)
        print 'Compilation command: ' + compcommand
        compresult = helper.runproc(compcommand,
                                    globals.sandboxdir,
                                    timelim=settings.compilelimit)
        if compresult[0] is None:  # Time limit exceeded
            print 'Compile time limit exceeded ({:.2f} seconds)'.format(
                settings.compilelimit)
            return False
        message = compresult[3].strip()
        if compresult[0] != 0:  # Runtime error
            helper.printdesc(message, 'Compile errors', settings.smalldivider,
                             False)
            return False
        print 'Successful compile ({:.2f} seconds)'.format(compresult[1])
        if len(message) > 0:  # Check for warnings
            helper.printdesc(message, 'Compile warnings',
                             settings.smalldivider, False)
        return True
Exemplo n.º 3
0
def compile ():
    print 'Compiling ' + globals.getsrc()
    lang = settings.langexts[globals.getext()]
    print 'Language detected: ' + lang
    if not helper.copy(os.path.join(globals.working, globals.getsrc()), globals.sandboxdir):
        print 'Source file not found in working directory'
        return False
    compcommand = settings.compcommands[lang]
    if compcommand is None:
        print 'No compilation necessary'
        return True
    else:
        compcommand = pattern.convert(compcommand)
        print 'Compilation command: ' + compcommand
        compresult = helper.runproc(compcommand, globals.sandboxdir, timelim=settings.compilelimit)
        if compresult[0] is None: # Time limit exceeded
            print 'Compile time limit exceeded ({:.2f} seconds)'.format(settings.compilelimit)
            return False
        message = compresult[3].strip()
        if compresult[0] != 0: # Runtime error
            helper.printdesc(message, 'Compile errors', settings.smalldivider, False)
            return False
        print 'Successful compile ({:.2f} seconds)'.format(compresult[1])
        if len(message) > 0: # Check for warnings
            helper.printdesc(message, 'Compile warnings', settings.smalldivider, False)
        return True
Exemplo n.º 4
0
def run():
    if not globals.commands['comp']():
        return False
    print settings.meddivider
    stdio = globals.getmode() == 'stdio'
    print 'Executing problem {} in {} I/O mode'.format(globals.getprob(),
                                                       globals.getmode())
    files = os.listdir(globals.probdir)
    cases = []
    for file in files:
        if file.endswith('.in'):
            cases.append(file[:-3])
    print '{} test inputs found'.format(len(cases))
    sandboxin = os.path.join(globals.sandboxdir,
                             pattern.convert(settings.inputfile))
    sandboxout = os.path.join(globals.sandboxdir,
                              pattern.convert(settings.outputfile))
    execcommand = pattern.convert(
        settings.execcommands[settings.langexts[globals.getext()]])
    langlim = settings.timeconstraints[settings.langexts[globals.getext()]]
    outcomes = collections.OrderedDict([('Correct', 0), ('Runtime Error', 0),
                                        ('Time Limit Exceeded', 0),
                                        ('Presentation Error', 0),
                                        ('Wrong Answer', 0),
                                        ('No Output Produced', 0),
                                        ('No Answer File', 0)])
    print 'Time limit per input: {:.2f} seconds'.format(langlim)
    for case in cases:
        print settings.meddivider
        print 'Test input ' + case
        helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin)
        input = helper.read(sandboxin)
        execresult = helper.runproc(execcommand,
                                    globals.sandboxdir,
                                    stdin=(input if stdio else None),
                                    timelim=langlim)
        output = execresult[2] if stdio else helper.read(sandboxout)
        ans = helper.read(os.path.join(globals.probdir, case + '.ans'))
        wronganswer = False
        if execresult[0] is None:  # Time limit exceeded
            print 'Execution time limit exceeded'
            outcomes['Time Limit Exceeded'] += 1
        else:
            print 'Execution time: {:.2f} seconds'.format(execresult[1])
            if execresult[0] != 0:
                print 'Runtime error: exit code ' + repr(execresult[0])
                outcomes['Runtime Error'] += 1
            elif output is None or len(output) == 0:
                print 'Program did not produce output'
                outcomes['No Output Produced'] += 1
            elif ans is None:
                print 'No answer file provided'
                outcomes['No Answer File'] += 1
            elif ans == output or \
                 (''.join(ans.split()) == ''.join(output.split())
                  and settings.acceptformatting):
                print 'Correct'
                outcomes['Correct'] += 1
            elif ''.join(ans.split()) == ''.join(output.split()):
                print 'Presentation Error (extra/missing whitespace)'
                outcomes['Presentation Error'] += 1
                wronganswer = True
            else:
                print 'Wrong Answer'
                outcomes['Wrong Answer'] += 1
                wronganswer = True
        if settings.printinput:
            helper.printdesc(input, 'Input', settings.smalldivider)
        if settings.printoutput:
            helper.printdesc(output, 'Output', settings.smalldivider)
        if wronganswer and settings.printexpected:
            helper.printdesc(ans, 'Expected Output', settings.smalldivider)
        if settings.printstdout and not stdio:
            helper.printdesc(execresult[2], 'Stdout', settings.smalldivider)
        if settings.printstderr:
            helper.printdesc(execresult[3], 'Stderr', settings.smalldivider)
        helper.write(os.path.join(globals.probdir, case + '.out'), output)
        if not stdio:
            helper.write(os.path.join(globals.probdir, case + '.stdout'),
                         execresult[2])
        helper.write(os.path.join(globals.probdir, case + '.stderr'),
                     execresult[3])
    if settings.printsummary:
        print settings.meddivider
        print 'Results Summary'
        for outcome in outcomes:
            print '{:2d} | {:<20}'.format(outcomes[outcome], outcome)
        print 'Total Score: {:.1f}'.format(
            (100.0 * outcomes['Correct']) / max(1, len(cases)))
    log.addmsg('Executed problem {} in {} I/O mode with score {:.1f}'.format(
        globals.getprob(), globals.getmode(),
        (100.0 * outcomes['Correct']) / max(1, len(cases))))
    return True
Exemplo n.º 5
0
Arquivo: run.py Projeto: ertemplin/pcu
def run ():
    if not globals.commands['comp']():
        return False
    print settings.meddivider
    stdio = globals.getmode() == 'stdio'
    print 'Executing problem {} in {} I/O mode'.format(globals.getprob(), globals.getmode())
    files = os.listdir(globals.probdir)
    cases = []
    for file in files:
        if file.endswith('.in'):
            cases.append(file[:-3])
    print '{} test inputs found'.format(len(cases))
    sandboxin = os.path.join(globals.sandboxdir, pattern.convert(settings.inputfile))
    sandboxout = os.path.join(globals.sandboxdir, pattern.convert(settings.outputfile))
    execcommand = pattern.convert(settings.execcommands[settings.langexts[globals.getext()]])
    langlim = settings.timeconstraints[settings.langexts[globals.getext()]]
    outcomes = collections.OrderedDict([
        ('Correct', 0),
        ('Runtime Error', 0),
        ('Time Limit Exceeded', 0),
        ('Presentation Error', 0),
        ('Wrong Answer', 0),
        ('No Output Produced', 0),
        ('No Answer File', 0)
    ])
    print 'Time limit per input: {:.2f} seconds'.format(langlim)
    for case in cases:
        print settings.meddivider
        print 'Test input ' + case
        helper.copy(os.path.join(globals.probdir, case + '.in'), sandboxin)
        input = helper.read(sandboxin)
        execresult = helper.runproc(execcommand, globals.sandboxdir, stdin=(input if stdio else None), timelim=langlim)
        output = execresult[2] if stdio else helper.read(sandboxout)
        ans = helper.read(os.path.join(globals.probdir, case + '.ans'))
        wronganswer = False
        if execresult[0] is None: # Time limit exceeded
            print 'Execution time limit exceeded'
            outcomes['Time Limit Exceeded'] += 1
        else:
            print 'Execution time: {:.2f} seconds'.format(execresult[1])
            if execresult[0] != 0:
                print 'Runtime error: exit code ' + repr(execresult[0])
                outcomes['Runtime Error'] += 1
            elif output is None or len(output) == 0:
                print 'Program did not produce output'
                outcomes['No Output Produced'] += 1
            elif ans is None:
                print 'No answer file provided'
                outcomes['No Answer File'] += 1
            elif ans == output:
                print 'Correct'
                outcomes['Correct'] += 1
            elif ''.join(ans.split()) == ''.join(output.split()):
                print 'Presentation Error (extra/missing whitespace)'
                outcomes['Presentation Error'] += 1
                wronganswer = True
            else:
                print 'Wrong Answer'
                outcomes['Wrong Answer'] += 1
                wronganswer = True
        if settings.printinput:
            helper.printdesc(input, 'Input', settings.smalldivider)
        if settings.printoutput:
            helper.printdesc(output, 'Output', settings.smalldivider)
        if wronganswer and settings.printexpected:
            helper.printdesc(ans, 'Expected Output', settings.smalldivider)
        if settings.printstdout and not stdio:
            helper.printdesc(execresult[2], 'Stdout', settings.smalldivider)
        if settings.printstderr:
            helper.printdesc(execresult[3], 'Stderr', settings.smalldivider)
        helper.write(os.path.join(globals.probdir, case + '.out'), output)
        if not stdio:
            helper.write(os.path.join(globals.probdir, case + '.stdout'), execresult[2])
        helper.write(os.path.join(globals.probdir, case + '.stderr'), execresult[3])
    if settings.printsummary:
        print settings.meddivider
        print 'Results Summary'
        for outcome in outcomes:
            print '{:2d} | {:<20}'.format(outcomes[outcome], outcome)
        print 'Total Score: {:.1f}'.format((100.0 * outcomes['Correct']) / len(cases)); 
    return True