# -*- coding: utf-8 -*- #### ΜΗ ΠΕΙΡΑΖΕΤΕ ΑΥΤΟ ΤΟ ΑΡΧΕΙΟ #### #### #### Πρόγραμμα ελέγχου του προγράμματος που γράφουν οι φοιτητές. Μη πειράζετε αυτό το πρόγραμμα. #### Γράφετε το πρόγραμμά σας στο αρχείο user.py και μόνο εκεί. #### import re import sys import os import subprocess # Το πρόγραμμα του χρήστη βρίσκεται στο "userfilename" userfilename="user.py" # Αν ο χρήστης έχει δώσει ένα όρισμα στην κλήση του προγράμματος τότε αυτό είναι το όνομα # του προγράμματος του χρήστη που θέλουμε να ελεγχθεί και όχι απαραίτητα το 'user.py' if len(sys.argv)>1: userfilename=sys.argv[1] # Τα προγράμματα ελέγχου που γράφονται αυτόματα έχουν το παρακάτω πρόθεμα testerfilename="__testcode__" # Αυτή η συνάρτηση φτιάχνει το πρόγραμμα python που ελέγχει το πρόγραμμα του χρήστη στην είσοδο inp και # έξοδο outp. # firstline είναι ο αριθμός της πρώτης γραμμής του κώδικα του χρήστη και lastline ο αριθμός της τελευταίας # fname είναι το όνομα του αρχείου του χρήστη def maketestprog(inp, outp, firstline, lastline, fname): f = open(fname, 'w+') print >> f, "# -*- coding: utf-8 -*-" print >> f, "#### Checking file: {uf}".format(uf=userfilename) print >> f, "import sys, math" print >> f, "LLL={LLL}".format(LLL=inp) print >> f, "# start user code" for i in range(firstline, lastline+1): print >> f, lines[i] print >> f, "# end user code" print >>f, """ def fnc1(x): global a, b, eps if not hasattr(fnc1, "counter"): fnc1.counter = 0 fnc1.maxtimes = (int)(0.1+math.ceil(math.log((b-a)/eps, 2.0)+2)) if fnc1.counterLLL[3]: print \"ERROR on input {LLL}\\nOutput produced is \", uuu, \"\\nShould be {uuu}\" sys.exit(1) else: print "OK" sys.exit(0) """.format(LLL=inp, uuu=outp) return fname # Στις λίστες inputs, outputs βρίσκονται κάποια inputs και τα αντίστοιχα σωστά outputs inputs = [ [0, -1., -0.8, 1e-6], [0, -1., -0.8, 1e-8], [1, -1., -0.5, 1e-6], [1, -1., -0.5, 1e-8], [2, -0.5, 1, 1e-6], [2, -0.5, 1, 1e-8], ] outputs = [ -0.94564927392359, -0.94564927392359, -0.91898735511547, -0.91898735511547, 0.0, 0.0, ] # lines = open(userfilename).readlines() for i,l in enumerate(lines): if re.match('####START', l): #print "START found at line %d, [%s]"% (i, l) firstline = i+1 if re.match('####STOP', l): #print "STOP found at line %d, [%s]"% (i, l) lastline = i-1 errors=0 for i,inp in enumerate(inputs): print print "-------------------Case No %d------------------"% i filename = maketestprog(inputs[i], outputs[i], firstline, lastline, testerfilename+str(i)+".py") exit_code = subprocess.call(["python", filename]) if exit_code == 0: print "---Case No %d: OK"% i else: errors += 1 print "---Case No %d: ERROR"% i print if errors>0: print "****** The program has run in error in some cases." sys.exit(1) else: print "****** The program has run correctly in all cases." sys.exit(0)