#! /usr/bin/env python
import  sys,os,re
from subprocess import call,Popen,PIPE
solDir,outDir,execDir,testDir="solutions","outputs","executables","testcases"
import filecmp

def execute(opts="all"):
    os.system("make clean")
    os.system("make "+opts)
    execs= [f for f in os.listdir(execDir) if not f.startswith(".") and not f.startswith("~")]
    for noext in execs:
        test_dir= os.path.join(testDir,noext)
        out_dir= os.path.join(outDir,noext)
        if not os.path.exists(out_dir):
            os.system("mkdir -p "+out_dir)
        for testfile in os.listdir(test_dir):
            filename= os.path.splitext(testfile)[0]
            out_path= os.path.join(out_dir,filename+".out")
            test_path= os.path.join(test_dir,testfile)
            exec_path= os.path.join(execDir,noext)
            os.system("./"+exec_path+" < "+test_path+" > "+ out_path)

def evaluate():
    execs= [file for file in os.listdir(execDir) if not file.startswith(".") and not file.startswith("~")]
    for noext in execs:
        print "evaluating %s"%(noext)
        sol_dir= os.path.join(solDir,noext)
        out_dir= os.path.join(outDir,noext)
        # total= len(os.listdir(sol_dir))
        counter,passed,flag=0,0,True
        for file in os.listdir(out_dir):
            out_path= os.path.join(out_dir,file)
            sol_path= os.path.join(sol_dir,os.path.splitext(file)[0]+".ans")
            counter+=1
            with open(sol_path,"r") as sol_reader:
                with open(out_path,"r") as out_reader:
                    for line in sol_reader:
                        sol_line= line.strip();
                        out_line= out_reader.readline().strip()
                        if sol_line != out_line:
                            flag= False
                            break
            if flag:
                print "testcase %d: passed"%(counter)
                passed+=1
            else:
                print "testcase %d: failed"%(counter)
        print "result: %d/%d"%(passed,counter)

if __name__=='__main__':
    opts="all"
    if len(sys.argv)>1:
        opts=' '.join(sys.argv[1:])
    if not os.path.exists(outDir):
        os.system("mkdir "+outDir)
    execute(opts)
    evaluate()