def check_board_consistency(board,row_ids,cell_ids,tname): d = {} #list,count for row in board: for cell in row: if cell: if cell[0] in d: d[cell[0]][1]+=1 if cell[2] is not d[cell[0]][0]: res_code(tname, "badboard", "Ship "+cell[0]+" has different list ids") return False else: d[cell[0]] = [cell[2],1] for k,v in d.items(): if v[0][0] != v[1]: res_code(tname, "badboard", "Ship "+k+" has wrong count") return False if len({id(d[k][0]) for k in d}) != len(d): res_code(tname, "badboard", "Repeated length list ids on board") return False if row_ids != [id(row) for row in board]: res_code(tname, "badboard", "Row list ids changed during execution") return False for row,idlist, in zip(board,cell_ids): for cell,cid in zip(row,idlist): if(cell and cid and (id(cell) != cid)): res_code(tname, "badboard", "Cell tuples replaced during execution") return False return True
def test_set(name, testlist, modulename="geo_tweet_tools",timeout=3, runner=import_runner, comparemethod=None): correct = 0 #answers = [] for i,(fname,args,kwargs,ans) in enumerate(testlist): tname = name + "_" + str(i) try: res, retval = mp_test2(runner,[modulename,fname,args,kwargs],{"tname":tname}, timeout=timeout) #ans=retval #answers.append((fname,args,kwargs,ans)) #continue if res=="skip": continue if res: res_code(tname, res, retval) continue if allclose(retval,ans): # correct result #with no floats correct+=1 continue else: #if isinstance(ans, Sequence): # long_sequence_compare(tname, ans, retval, contextpreview=3) #else: res_code(tname, "wrong",diff_str("Wrong result",ans,retval)) continue except at.Error as e: res_code(tname, e.code, e.message) except Exception as e: res_code(tname, "testingFailed", e) continue #print (name+'=',answers) res_code(name, str(correct))
def set_summary(name, correct, total): output = str(correct) + ' passed tests out of ' + str( total) + " in test set named '" + name + "'." if stdout.isatty(): if correct == total: output = '\033[32;1;7m' + output + '\033[0m' else: output = '\033[35;1;7m' + output + '\033[0m' res_code(name, str(correct), output)
def test_sets(name, data, moddefaults): defaults = moddefaults.copy() defaults.update(data.defaults) def getarg(key): try: return val[key] except KeyError: return defaults[key] correct = 0 total = 0 for key, val in data.testcases.items(): tname = '_'.join([name, str(key)]) total += 1 try: runner = getarg('runner') timeout = getarg('timeout') modulename = getarg('modulename') fname = getarg('fname') args = getarg('args') ans = getarg('ans') comparemethod = getarg('comparemethod') kwargs = getarg('kwargs') options = getarg('options') code, res = mp_test(runner, [modulename, fname, args, kwargs, options], {"tname": tname}, timeout=timeout) if code: res_code(tname, code, res) continue if any(comparemethod(a, res) for a in ans): correct += 1 continue else: res_code( tname, "wrong", diff_str("Wrong result, input: " + str(args), ans[0], res)) except at.Error as e: res_code(tname, e.code, e.message) continue except Exception as e: res_code(tname, "testingFailed", e.__repr__()) continue output = str(correct) + ' passed tests out of ' + str(total) res_code(name, str(correct), output)
def filelist(folder): required = ["README", "ex8_2nd_order.py", "ex8_iterators.py" ] try: val=filelist_test(folder, required, format='zip') except: res_code("zipfile",output="Testing zip file failed...") print('@@@@@@@@@@ fail @@@@@@@@@@@') return val if val==1: print('@@@@@@@@@@ pass @@@@@@@@@@@') if val==-1: print('@@@@@@@@@@ fail @@@@@@@@@@@') return val
def test_rpsls_setseries(script="ex2_rpsls_wrapper.py", timeout=3, name="RPSLSsetseries"): correct = 0 #d = {} for i in range(len(setlist.l2)): ans = setlist.r2[i] tname = name+"_"+str(i) try: res, output = sp_test([sys.executable,script,str(i+len(setlist.l))], timeout=timeout, input=setlist.msetinput(i)+"1\n", universal_newlines=True) if res: res_code(tname, res, output) continue if isinstance(output,str): output = output.encode() #d[i] = output #continue if output==ans: # correct result correct+=1 continue else: long_sequence_compare(tname, ans, output) continue except Exception as e: res_code(tname, "testingFailed", e) continue res_code(name, str(correct))
def test_rpsls_game(script="ex2_rpsls_game_wrapper.py", timeout=3, name="RPSLSgame"): correct = 0 for i in range(len(gamelist.l)): for w in (0,1): ans = gamelist.d[(i,w)] tname = name+"_"+str(i)+"_"+str(w) try: res, output = sp_test([sys.executable,script,str(i),str(w)], timeout=timeout, input=gamelist.game_input_str(i,1-w), universal_newlines=True) if res: res_code(tname, res, output) continue if isinstance(output,str): output = output.encode() if output==ans: # correct result correct+=1 continue else: long_sequence_compare(tname, ans, output) continue except Exception as e: res_code(tname, "testingFailed", e) continue res_code(name, str(correct))
def import_runner_chk_board(modulename, fname, args=[], kwargs={},tname=''): #print(modulename, fname, args, kwargs,tname) replay = kwargs.pop("_replay",[]) recount = kwargs.pop("_recount",False) #print(replay) board = args[0] if args else kwargs["board"] row_ids = [id(row) for row in board] cell_ids = [[id(cell) if cell else None for cell in row] for row in board] board_cp = copy.deepcopy(board) if replay: replay_board(board_cp,replay) if recount: recount_board(board_cp) retval,res = import_runner(modulename, fname, args, kwargs, check_input=False) if retval: res_code(tname, retval, res) return ("skip",res) if not (check_boards_equal(board_cp,board,tname)): return ("skip", board) if not (check_board_consistency(board,row_ids,cell_ids,tname)): return ("skip", board) return (None,res)
def test_set(name, testlist, modulename="battleship",timeout=3, runner=import_runner, comparemethod=None): correct = 0 #answers = [] for i,(fname,args,kwargs,ans) in enumerate(testlist): tname = name + "_" + str(i) try: res, inner = mp_test(runner,[modulename,fname,args,kwargs],{"tname":tname}, timeout=timeout) #ans=retval #answers.append((fname,args,kwargs,ans)) #continue if res: res_code(tname, res, retval) continue lres,retval = inner if lres=="skip": continue if lres: res_code(tname, lres, retval) continue if ans and comparemethod=="new_board": if check_empty_board(retval,ans[0],ans[1],tname): correct+=1 continue if retval==ans: # correct result #with no floats correct+=1 continue else: #if isinstance(ans, Sequence): # long_sequence_compare(tname, ans, retval, contextpreview=3) #else: res_code(tname, "wrong",diff_str("Wrong result",ans,retval)) continue except FakeException as e: res_code(tname, "testingFailed", e) continue #print (name+'=',answers) res_code(name, str(correct))
def test_set(name, testlist, modulename="ex4",timeout=3, rtol=1.e-5, atol=1.e-5, geteps=False, runner=import_runner): correct = 0 #answers = [] for i,(fname,args,kwargs,ans) in enumerate(testlist): tname = name + "_" + str(i) try: res, (lres,retval) = mp_test(runner,[modulename,fname,args,kwargs], timeout=timeout) #ans=retval #answers.append((fname,args,kwargs,ans)) #continue if res: res_code(tname, res, retval) continue if lres: res_code(tname, lres, retval) continue #if retval==ans: # correct result #with no floats if geteps: atol += kwargs["epsilon"] if "epsilon" in kwargs else 1E-5 if allclose(ans, retval, rtol=rtol, atol=atol): # correct result #deep float compare? correct+=1 continue else: if isinstance(ans, Sequence): long_sequence_compare(tname, ans, retval, contextpreview=3) else: res_code(tname, "wrong", "\n".join( ["Wrong result:", "expected: "+str(ans), "actual: "+str(retval)])) continue except Exception as e: res_code(tname, "testingFailed", e) continue #print (name+'=',answers) res_code(name, str(correct))
def check_empty_board(board,width,height,tname): if not isinstance(board, list): res_code(tname, "wrong", diff_str("Wrong type",list,type(board))) return False if len(board)!=height: res_code(tname, "wrong", diff_str("Wrong height",height,len(board))) return False exp_row = [None]*width id_set = {id(row) for row in board} if len(id_set)!=height: res_code(tname, "wrong", diff_str("Number of different row lists wrong",height,len(id_set))) return False for i,row in enumerate(board): if row != exp_row: res_code(tname, "wrong", diff_str("Row "+str(i)+" wrong",exp_row,row)) return False return True
def test_ex3(modulename="ex3",timeout=3): for name,(testlist) in rtset.items(): correct = 0 #answers = [] for i,(fname,args,kwargs,ans) in enumerate(testlist): tname = name + "_" + str(i) try: res, (lres,retval) = mp_test(import_runner,[modulename,fname,args,kwargs], timeout=timeout) #ans=retval #answers.append((fname,args,kwargs,ans)) #continue if res: res_code(tname, res, retval) continue if lres: res_code(tname, lres, retval) continue #if retval==ans: # correct result #with no floats if allclose(ans, retval): # correct result #deep float compare? correct+=1 continue else: if isinstance(ans, Sequence): long_sequence_compare(tname, ans, retval, contextpreview=3) else: res_code(tname, "wrong", "\n".join( ["Wrong result:", "expected: "+str(ans), "actual: "+str(retval)])) continue except Exception as e: res_code(tname, "testingFailed", e) continue #print (name+'=',answers) res_code(name, str(correct))
def check_boards_equal(exp,act,tname): if len(exp)!=len(act): res_code(tname, "wrong", diff_str("Different board heights",len(exp),len(act))) return False id_set = {id(row) for row in act} if len(id_set)!=len(act): res_code(tname, "wrong", diff_str("Number of different row lists wrong",len(act),len(id_set))) return False for i,(erow,arow) in enumerate(zip(exp,act)): if erow != arow: res_code(tname, "wrong", diff_str("Row "+str(i)+" different",erow,arow)) return False return True
def test_rpsls_illegal(script="ex2_rpsls_game_wrapper.py", timeout=3, name="RPSLSillegal"): ans = gamelist.ill try: res, output = sp_test([sys.executable,script]+gamelist.illargs, timeout=timeout, input="0\n-5\n6\n12345678901234567890\n-987654321\n-987654321\n1234567890\n6\n-5\n0\n2\n-987654321\n1234567890\n6\n-5\n0\n0\n-5\n6\n1234567890\n-9876543211234567890\n2\n", universal_newlines=True) if res: res_code(name, res, output) else: if isinstance(output,str): output = output.encode() if output==ans: # correct result res_code(name, "correct") else: long_sequence_compare(name, ans, output) except Exception as e: res_code(tname, "testingFailed", e)
def test_squares(script="ex2_square_wrapper.py", timeout=3, name="Squares"): correct = 0 for i in sqset: ans = get_sq(i) try: res, output = sp_test([sys.executable,script,str(i)], timeout=timeout, input=str(i), universal_newlines=True) if res: res_code(name+str(i), res, output) continue if isinstance(output,str): output = output.encode() if output==ans: # correct result correct+=1 continue else: long_sequence_compare(name+str(i), ans, output) continue except Exception as e: res_code(name+str(i), "testingFailed", e) continue res_code(name, str(correct))
#!/usr/bin/env python3 from autotest import filelist_test, res_code from sys import argv required = [ "README", "ex7.py", ] try: filelist_test(argv[1], required, format='zip') except: res_code("zipfile", output="Testing zip file failed...") exit(-1)
#!/usr/bin/env python3 from autotest import filelist_test,res_code from sys import argv required = ["README", "oneliners.py", ] try: filelist_test(argv[1], required) except: res_code("tarfile",output="Testing tar file failed...") exit(-1)
def test_sets(name, data, moddefaults, tests=None, dryrun=False): defaults = moddefaults.copy() defaults.update(data.defaults) def getarg(key): try: return val[key] except KeyError: return defaults[key] correct = 0 total = 0 for key, val in data.testcases.items(): tname = '_'.join([name, str(key)]) if tests and tname not in tests: continue total += 1 try: if dryrun: print(test_info(tname, getarg)) continue runner = getarg('runner') timeout = getarg('timeout') modulename = getarg('modulename') fname = getarg('fname') args = getarg('args') ans = getarg('ans') comparemethod = getarg('comparemethod') kwargs = getarg('kwargs') options = getarg('options') teststr = test_info(tname, getarg) code, res = mp_test(runner, [modulename, fname, args, kwargs, options], {"tname": tname}, timeout=timeout) if code: print(teststr) announce_failure(tname) res_code(tname, code, res) continue if any(comparemethod(a, res) for a in ans): correct += 1 continue else: print(teststr) announce_failure(tname) res_code( tname, "wrong", diff_str("Wrong result, input: " + str(args), ans[0], res)) except at.Error as e: print(teststr) announce_failure(tname) res_code(tname, e.code, e.message) continue except Exception as e: announce_failure(tname) res_code(tname, "testingFailed", e.__repr__()) continue if not dryrun: set_summary(name, correct, total)