def test_try_to_save_to_file_and_delete_afterwards(self): judge.Toolkit.save(self.src_script, self.script) judge.Toolkit.save(self.src_txt, self.txt) self.judge = judge.Judge(self.src_script, self.src_txt) self.assertIsNotNone(self.judge.script) judge.Toolkit.delete(self.src_script) judge.Toolkit.delete(self.src_txt) with self.assertRaises(FileNotFoundError): self.judge = judge.Judge(self.src_script, self.src_txt)
def __init__(self, parameters, output_path, log_path): self.parameters = parameters content = self.parameters["content"] self.judge = judge.Judge(content) self.output_path = output_path self.log_file_path = log_path self.fail_files = []
def test_adjudication(self): blue_red = (((10, 10), (9, 9)), ((11, 10), (10, 9)), ((12, 10), (11, 9)), ((13, 10), (12, 9)), ((13, 11), (13, 9)), ((12, 11), (14, 9)), ((11, 11), (14, 10)), ((10, 11), (14, 11)), ((10, 12), (14, 12)), ((11, 12), (14, 13)), ((12, 12), (14, 14)), ((13, 12), (14, 15)), ((13, 13), (13, 15)), ((12, 13), (12, 15)), ((11, 13), (11, 15)), ((10, 13), (10, 15)), ((10, 14), (9, 15)), ((11, 14), (9, 14)), ((12, 14), (9, 13)), ((13, 14), (9, 12))) J = judge.Judge(pos_blue=blue_red[0][0], pos_red=blue_red[0][1]) J.world.save('game.state', player=BLUE) self.assertEqual(J.adjudicate('game.state', new_move=None), None) winner = None for (blue, red) in blue_red[1:]: for pos in (blue, red): player = BLUE if pos == blue else RED J.world.save('game.state', player=player) P = tron.World('game.state') P.move_player(pos) P.save('game.state') winner = J.adjudicate('game.state', new_move=player) if winner: break if winner: break else: self.fail("Winning condition not detected") self.assertEqual(winner, RED)
def closeJudge(self, s, st): ee = map(lambda x: [x], s.envs) ae = map(lambda x: [x], s.args) map(lambda x: self.envClosure(x), ee) map(lambda x: self.envClosure(x), ae) es = [[]] for e in ee: es2 = [] for ss in es: for ei in e: es2.append(ss + [ei]) es = es2 aas = [[]] for e in ae: as2 = [] for ss in aas: for ei in e: as2.append(ss + [ei]) aas = as2 flag = False for e in es: for a in aas: r = judge.Judge(None) r.nt = s.nt r.envs = e r.args = a r.defn = s.defn if r not in st: st.append(r) flag = True return flag
def judge(self, href, data, keyword): true_urls = [] ju = judge.Judge() for i in range(len(href)): flow = ju.judge(data[i], keyword) if flow == True: true_urls.append(href[i]) return true_urls
def test_runs_a_program(self): judge.Toolkit.save(self.src_script, self.script) judge.Toolkit.save(self.src_txt, self.txt) self.judge = judge.Judge(self.src_script, self.src_txt) output, run_time = self.judge.run() self.assertEqual(output, self.txt) self.assertGreater(run_time, 0) judge.Toolkit.delete(self.src_script) judge.Toolkit.delete(self.src_txt)
def test_try_to_use_an_inexistent_config_file(self): self.assertIsNone(judge.Toolkit.load_config('randomconfig')) judge.Toolkit.save(self.src_script, self.script) judge.Toolkit.save(self.src_txt, self.txt) self.judge = judge.Judge(self.src_script, self.src_txt) self.judge.set_config('randomconfig') with self.assertRaises(RuntimeError): self.judge.run() judge.Toolkit.delete(self.src_script) judge.Toolkit.delete(self.src_txt)
def test_raises_error_when_running_an_invalid_script(self): txt = "hi" src_txt = "error.txt" script = "#! ruby\ntext = gets.chomp\nputs text.upcas\n" src_script = "error.rb" judge.Toolkit.save(src_script, script) judge.Toolkit.save(src_txt, txt) self.judge = judge.Judge(src_script, src_txt) output, how_long = self.judge.run() self.assertEqual(-1, how_long) judge.Toolkit.delete(src_script) judge.Toolkit.delete(src_txt)
def run(params): pipe = params['src'] inlet = params['txt'] bot = judge.Judge(pipe, inlet) if (bot.raw_config is None) and ('JOEDGEBOT_HOME' in os.environ): bot.set_config(os.environ['JOEDGEBOT_HOME']) outlet, elapsed = bot.run() if type(outlet) is type(b''): outlet = outlet.decode('utf-8') return outlet
def test_fetches_a_program(self): txt = "2" src_txt = "double.txt" script = "puts(gets.chomp.to_i*2)" src_script = "double.rb" judge.Toolkit.save(src_script, script) judge.Toolkit.save(src_txt, txt) self.judge = judge.Judge(src_script, src_txt) output, run_time = self.judge.run() self.assertEqual(output, "4") self.assertGreater(run_time, 0) judge.Toolkit.delete(src_script) judge.Toolkit.delete(src_txt)
def test_minmaxflood_i(self): for seed in xrange(101, 103): random.seed(seed) # TODO: The basic strategy test loop can be factored out into a separate module J = judge.Judge() J.world.save('game.state', player=BLUE) self.assertEqual(J.world.pos_opponent[1], J.world.pos_player[1]) # Same axial self.assertEqual( abs(J.world.pos_opponent[0] - J.world.pos_player[0]), 15) # Opposite sides self.assertEqual(J.adjudicate('game.state', new_move=None), None) print("Blue starts at (%d,%d)" % J.world.pos_player) print("Red starts at (%d,%d)" % J.world.pos_opponent) winner = None player = RED while (winner == None): if player == BLUE: player = RED # Red plays with the naive random-move strategy S = tron.Strategy() else: player = BLUE # Blue plays with the strategy under test S = minmaxflood_i.Strategy() S.time_limit = 20.0 shutil.copyfile('game.state', 'game.state.bak') J.world.save('game.state', player=player) W = tron.World('game.state') start_time = time.time() S.move(W) shutil.copyfile('game.state', 'game.state.bak') W.save('game.state') # self.assertLess(time.time() - start_time, MAX_MOVE_TIME) winner = J.adjudicate('game.state', new_move=player) self.assertNotEqual(winner, None) if winner == BLUE: result = "Blue wins!" else: result = "Red wins!" world_map = viz.WorldMap() world_map.plot_trace(J.trace_blue, J.trace_red) # world_map.plot_points(J.world.empty_space(),'g') world_map.save(result, str(seed) + '.png') # TODO: Add a unit test for the number of liberties at the north and the south pole (it was calculated incorrectly)
def test_run_a_lua_script(self): script = """ line = io.read("*line") io.write(line, "\\n") """ txt = "hello joe\n" script_file = "test.lua" txt_file = "test.txt" judge.Toolkit.save(script_file, script) judge.Toolkit.save(txt_file, txt) self.judge = judge.Judge(script_file, txt_file) output, how_long = self.judge.run() self.assertEqual(output, "hello joe") judge.Toolkit.delete(script_file) judge.Toolkit.delete(txt_file)
def test_run_a_golang_program(self): script = """ package main import "fmt" func main() { fmt.Println("hello joe!") } """ txt = "asdf" script_file = "test.go" txt_file = "test.txt" judge.Toolkit.save(script_file, script) judge.Toolkit.save(txt_file, txt) self.judge = judge.Judge(script_file, txt_file) output, how_long = self.judge.run() self.assertEqual(output, "hello joe!") judge.Toolkit.delete(script_file) judge.Toolkit.delete(txt_file)
def test_poles(self): """ Verify that behaviour at the poles are as expected. All points in the (ant)arctic circles should be treated as adjacent, and (x,0) should be the same point for all x, and likewise for (x,29). """ blue_red = ( ((15, 1), (15, 28)), ( (25, 0), (25, 29) ), # Both players step into the poles. The system should handle the adjacency # of the unusual coordinates correctly. ( (5, 1), (5, 28) ), # Both players step out of the poles at completely different longitudes. Again, # the system should handle the adjacency correctly despite the unusual # coordinates. ( (5, 0), (5, 29) ) # This should generate an exception, since both players have already visited # the poles. ) J = judge.Judge(pos_blue=blue_red[0][0], pos_red=blue_red[0][1]) J.world.save('game.state', player=BLUE) self.assertEqual(J.adjudicate('game.state', new_move=None), None) winner = None move = 0 for (blue, red) in blue_red[1:]: move += 1 for pos in (blue, red): player = BLUE if pos == blue else RED J.world.save('game.state', player=player) P = tron.World('game.state') P.move_player(pos) P.save('game.state') if move == 3: self.assertRaises(judge.StateFileException, J.adjudicate, 'game.state', new_move=player) else: J.adjudicate('game.state', new_move=player)
def test_basic_strategy(self): for seed in xrange(100, 110): random.seed(seed) J = judge.Judge() J.world.save('game.state', player=BLUE) self.assertEqual(J.adjudicate('game.state', new_move=None), None) winner = None player = BLUE while (winner == None): player = RED if player == BLUE else BLUE shutil.copyfile('game.state', 'game.state.bak') J.world.save('game.state', player=player) W = tron.World('game.state') S = tron.Strategy() S.move(W) shutil.copyfile('game.state', 'game.state.bak') W.save('game.state') winner = J.adjudicate('game.state', new_move=player) self.assertNotEqual(winner, None)
def test_prospect(self): random.seed(1006) J = judge.Judge() J.world.save('game.state', player=BLUE) self.assertEqual(J.adjudicate('game.state', new_move=None), None) winner = None player = BLUE for turn in xrange(0, 30): player = RED if player == BLUE else BLUE shutil.copyfile('game.state', 'game.state.bak') J.world.save('game.state', player=player) W = tron.World('game.state') S = tron.Strategy() S.move(W) shutil.copyfile('game.state', 'game.state.bak') W.save('game.state') winner = J.adjudicate('game.state', new_move=player) if winner: break (player_domain, opponent_domain) = J.world.prospect(plies=40)
def invertArg(self, arg, envr, pm): arg = self.resolveOrDont(arg) if not isinstance(arg, judge.Judge) or arg.nt: raise ProofError("Argument to " + self.command + " must be a positive judgement, found: " + arg.shortString()) #if any of the parts of arg are not atomic, we will need to use proxy variables proxy = judge.Judge(None) pMatches = [] proxy.defn = arg.defn proxy.nt = False trackFresh = set() def proxify(x): if util.isAtomic(x): return x else: fresh = envr.fresh(x.type(pm.world.symList)) trackFresh.add(fresh) pMatches.append(x.isSuperType(fresh, pm.world.symList)) return fresh proxy.envs = map(proxify, arg.envs) proxy.args = map(proxify, arg.args) #find the cases that match syntactically cases = [] matches2 = [] #matches formals to fresh variables matches = [] #matches fresh variables to actuals for c in proxy.defn.cases: m2 = symbol.SeqMatch(None) for source in set(c.getConclusions()[0].vars()): for v in source.vars(): if util.interestingRoot(v): m2.addIdMatch(v, envr.fresh(v)) m = proxy.unify(c.getConclusions()[0].substMatch([m2]), pm.world.symList) if m: cases.append(c) m.substMatch(pMatches) matches.append(m) matches2.append(m2) #if no cases, then we have a contradiction and the result is false if not cases: return None #use fresh variables for fv for c,m in zip(cases, matches2): for f in c.free: m.addIdMatch(f, envr.fresh(f)) #find the premises of those cases, subst variables deduced from conc prems = map(lambda (c,m,m2): map(lambda p: p.substMatch([m2]).substMatch([m]), c.premises), zip(cases, matches, matches2)) for (p, m, m2) in zip(prems, matches, matches2): for source in set(m.dom()): #account for equalities between actuals and formals of the conclusion/arg if not util.isAtomic(source): pe = judge.Equality(None) pe.lhs = m.match(source) pe.rhs = source.substMatch([m2]).substMatch([m]) pe.nt = False p.insert(0, pe) #account for where refinements of actuals would otherwise be lost elif not source.substMatch([m]).isSuperType(source, pm.world.symList): #revert the premises to using the more refined variable for i in range(len(p)): p[i] = p[i].substMatch([symbol.IdMatch(m.match(source), source.substMatch([m2]))]) #add an equality which captures the refinement pe = judge.Equality(None) pe.lhs = m.match(source) pe.rhs = source.substMatch([m2]) pe.nt = False p.insert(0, pe) return prems,cases
def test_try_to_open_inexistent_file(self): judge.Toolkit.delete('randomfile') with self.assertRaises(FileNotFoundError): self.judge = judge.Judge('randomscript', 'randominlet')
def submitCode(): submission = request.get_json() output = judge.Judge().executeCode(submission['code'], submission['language']) return output
def run(script, text): joe = judge.Judge(script, text) output, elapsed = joe.run() return output, elapsed
#!/usr/bin/env python #python import logsend import readfile import configure import judge configure = configure.Configure() first = configure.readoption judge = judge.Judge(filename) filename = 'aa.txt' readfile.readfile(filename)
def RunAndJudgeWithoutSubtask(submission_id, submission_info, data_config, testdata_path, code_path, exe_path, language_config): testcases_count = data_config['data']['testcasesCount'] result = { 'time_usage': 0, 'memory_usage': 0, 'status': static.name_to_id['Running'], 'score': 0, 'cases': [{ 'status': static.name_to_id['Waiting'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': 0 } for i in range(testcases_count)] } all_statuses = [] UpdateInfo(submission_id, result) for id in range(1, testcases_count + 1): result['cases'][id - 1]['status'] = static.name_to_id['Running'] UpdateInfo(submission_id, result) input_rel_path = '%s%d%s' % (data_config['data']['prefix'], id, data_config['data']['inputSuffix']) output_rel_path = '%s%d%s' % (data_config['data']['prefix'], id, data_config['data']['outputSuffix']) input_path = os.path.join(testdata_path, input_rel_path) output_path = os.path.join(testdata_path, output_rel_path) run_result = Run( data_config=data_config, exe_path=exe_path, input_rel_path=modules.EscapeFilename(input_rel_path), output_rel_path=modules.EscapeFilename(output_rel_path), input_path=modules.EscapeFilename(input_path), output_path=modules.EscapeFilename(output_path), language_config=language_config) print(run_result) full_score = 100.0 / testcases_count run_result['full_score'] = full_score result['cases'][id - 1] = run_result if run_result['status'] != static.name_to_id['Accepted']: result['cases'][id - 1]['score'] = 0 else: judge_result = judge.Judge( data_config=data_config, input_path=modules.EscapeFilename(input_path), output_path=modules.EscapeFilename(output_path)) result['cases'][id - 1]['status'] = judge_result['status'] result['cases'][ id - 1]['score'] = judge_result['score'] * full_score / 100.0 result['cases'][id - 1]['judger_message'] = judge_result.get( 'judger_message') result['time_usage'] += run_result['time_usage'] result['memory_usage'] = max(result['memory_usage'], run_result['memory_usage']) result['score'] += result['cases'][id - 1]['score'] all_statuses.append(result['cases'][id - 1]['status']) # UpdateInfo(submission_id,result) # 下一个测试点开始时会调用 UpdateInfo # 故省略本次 result['status'] = min(all_statuses) UpdateInfo(submission_id, result)
def RunAndJudgeWithSubtask(submission_id, submission_info, data_config, testdata_path, code_path, exe_path, language_config): result = { 'subtask': True, 'time_usage': 0, 'memory_usage': 0, 'status': static.name_to_id['Running'], 'score': 0, 'subtasks': [{ 'name': subtask.get('name', ''), 'status': static.name_to_id['Waiting'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': subtask['score'], 'cases': [{ 'status': static.name_to_id['Waiting'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': subtask['score'] } for i in range(subtask['testcasesCount'])] } for subtask in data_config['data']['subtasks']] } subtask_statuses = [] for _subtask_id, subtask in enumerate(data_config['data']['subtasks']): subtask_id = _subtask_id + 1 print('Running on subtask %d...' % subtask_id) skip_this_subtask = 0 for reliance in subtask.get('rely', []): if result['subtasks'][reliance - 1]['status'] not in [ static.name_to_id['Accepted'], static.name_to_id['Partially Accepted'] ]: skip_this_subtask = 1 break if skip_this_subtask: result['subtasks'][subtask_id - 1] = { 'name': subtask.get('name', ''), 'status': static.name_to_id['Skipped'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': subtask['score'], 'cases': [{ 'status': static.name_to_id['Skipped'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': subtask['score'] } for i in range(subtask['testcasesCount'])] } UpdateInfo(submission_id, result) continue result['subtasks'][subtask_id - 1]['status'] = static.name_to_id['Running'] UpdateInfo(submission_id, result) subtask_result = { 'name': subtask.get('name', ''), 'status': static.name_to_id['Running'], 'time_usage': 0, 'memory_usage': 0, 'score': subtask['score'], 'full_score': subtask['score'], 'cases': [{ 'status': static.name_to_id['Waiting'], 'time_usage': 0, 'memory_usage': 0, 'score': 0, 'full_score': subtask['score'] } for i in range(subtask['testcasesCount'])] } testcases_count = subtask['testcasesCount'] cases_statuses = [] for id in range(1, testcases_count + 1): result['subtasks'][subtask_id - 1]['cases'][ id - 1]['status'] = static.name_to_id['Running'] UpdateInfo(submission_id, result) input_rel_path = '%s%d%s' % (subtask['prefix'], id, data_config['data']['inputSuffix']) output_rel_path = '%s%d%s' % (subtask['prefix'], id, data_config['data']['outputSuffix']) input_path = os.path.join(testdata_path, input_rel_path) output_path = os.path.join(testdata_path, output_rel_path) case_result = Run( data_config=data_config, exe_path=exe_path, input_rel_path=modules.EscapeFilename(input_rel_path), output_rel_path=modules.EscapeFilename(output_rel_path), input_path=modules.EscapeFilename(input_path), output_path=modules.EscapeFilename(output_path), language_config=language_config) case_result['score'] = subtask['score'] case_result['full_score'] = subtask['score'] # print(run_result) if case_result['status'] != static.name_to_id['Accepted']: case_result['score'] = 0 else: judge_result = judge.Judge( data_config=data_config, input_path=modules.EscapeFilename(input_path), output_path=modules.EscapeFilename(output_path)) case_result['status'] = judge_result['status'] case_result['score'] = judge_result['score'] * case_result[ 'full_score'] / 100.0 case_result['judger_message'] = judge_result.get( 'judger_message') cases_statuses.append(case_result['status']) result['time_usage'] += case_result['time_usage'] result['memory_usage'] = max(case_result['memory_usage'], result['memory_usage']) subtask_result['time_usage'] += case_result['time_usage'] subtask_result['memory_usage'] = max( case_result['memory_usage'], subtask_result['memory_usage']) subtask_result['cases'][id - 1] = case_result subtask_result['score'] = min(subtask_result['score'], case_result['score']) if case_result['status'] not in [ static.name_to_id['Accepted'], static.name_to_id['Partially Accepted'] ]: for i in range(id + 1, testcases_count + 1): subtask_result['cases'][ i - 1]['status'] = static.name_to_id['Skipped'] result['subtasks'][subtask_id - 1] = subtask_result break result['subtasks'][subtask_id - 1] = subtask_result UpdateInfo(submission_id, result) subtask_result['status'] = min(cases_statuses) subtask_statuses.append(subtask_result['status']) result['score'] += subtask_result['score'] UpdateInfo(submission_id, result) result['status'] = min(subtask_statuses) UpdateInfo(submission_id, result)
__author__ = 'OL' import pylab import player_baseClass, judge player_a, player_b = player_baseClass.Player(), player_baseClass.Player() judge = judge.Judge() pl = list() for i in range(100): player_a.think() player_b.think() judge.receive(player_a.move, player_b.move) result = judge.rulling() pl.append(result) #print(result) pylab.plot(pl,'ro') pylab.show()