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))
def make (): print 'Creating {} source file for problem {}'.format(settings.langexts[globals.getext()], globals.getprob()) template = helper.read(os.path.join(globals.templatedir, globals.getmode() + '.' + globals.getext())) if template is None: print '{}.{} template file not found (add it to templates folder)'.format(globals.getmode(), globals.getext()) return False helper.write(os.path.join(globals.working, globals.getsrc()), pattern.convert(template)) print 'Source file created: ' + globals.getsrc() log.addmsg('Creating {} source file for problem {}'.format(settings.langexts[globals.getext()], globals.getprob())) return True
def make(): print 'Creating {} source file for problem {}'.format( settings.langexts[globals.getext()], globals.getprob()) template = helper.read( os.path.join(globals.templatedir, globals.getmode() + '.' + globals.getext())) if template is None: print '{}.{} template file not found (add it to templates folder)'.format( globals.getmode(), globals.getext()) return False helper.write(os.path.join(globals.working, globals.getsrc()), pattern.convert(template)) print 'Source file created: ' + globals.getsrc() log.addmsg('Creating {} source file for problem {}'.format( settings.langexts[globals.getext()], globals.getprob())) return True
def setfile (ext): file = globals.probsettings['filename'] type = exttotype[ext] testid = globals.gettestid() print 'Setting test case {} {} for problem {}'.format(testid, type, globals.getprob()) input = '' if file is None: print 'Enter {} below (Ctrl-{} when finished)'.format(type, 'Z' if helper.iswindows() else 'D') print settings.smalldivider input = sys.stdin.read() print settings.smalldivider else: print 'Retrieving from ' + file.name input = file.read() helper.write(os.path.join(globals.probdir, testid + ext), input) print 'Set {} successfully'.format(exttotype[ext]) log.addmsg('Set test case {} {} for problem {}'.format(testid, type, globals.getprob())) return True
def setfile(ext): file = globals.probsettings['filename'] type = exttotype[ext] testid = globals.gettestid() print 'Setting test case {} {} for problem {}'.format( testid, type, globals.getprob()) input = '' if file is None: print 'Enter {} below (Ctrl-{} when finished)'.format( type, 'Z' if helper.iswindows() else 'D') print settings.smalldivider input = sys.stdin.read() print settings.smalldivider else: print 'Retrieving from ' + file.name input = file.read() helper.write(os.path.join(globals.probdir, testid + ext), input) print 'Set {} successfully'.format(exttotype[ext]) log.addmsg('Set test case {} {} for problem {}'.format( testid, type, globals.getprob())) return True
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
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