def check(soldir, asttree, num): codeGen = CodeGenerator() path = os.path.join(soldir, str(num)) if not os.path.isdir(path): os.mkdir(path) f = open(os.path.join(soldir, str(num) + ".txt"), "w") try: codeGen.gen(asttree, path) subprocess.call("java -jar " + JASMIN_JAR + " " + path + "/MPClass.j", shell=True, stderr=subprocess.STDOUT) cmd = "java -cp ./lib" + os.pathsep + ". MPClass" subprocess.run(cmd, shell=True, stdout=f, timeout=5) except StaticError as e: f.write(str(e)) except subprocess.TimeoutExpired: f.write("Time out\n") except subprocess.CalledProcessError as e: raise RuntimeError( "command '{}' return with error (code {}): {}".format( e.cmd, e.returncode, e.output)) finally: f.close()
def test(input, expect, num): print('----') print('****') print('Test #', num) solpath = "./test/solutions/" dest = open(solpath + str(num) + ".txt", "w") if type(input) is str: # print("Input: ", input) inputfile = TestUtil.makeSource(input, num) lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) else: # print('Input: ', input) inputfile = TestUtil.makeSource(str(input), num) asttree = input codeGen = CodeGenerator() path = solpath + str(num) if not os.path.isdir(path): os.mkdir(path) try: codeGen.gen(asttree, path) subprocess.call("java -jar " + JASMIN_JAR + " " + path + "/MCClass.j", shell=True, stderr=subprocess.STDOUT) f = open(solpath + str(num) + ".txt", "w") subprocess.call("java -cp ./lib" + os.pathsep + ". MCClass", shell=True, stdout=f) f.close() except StaticError as e: print('died') dest.write(str(e)) except subprocess.CalledProcessError as e: print('died') raise RuntimeError( "command '{}' return with error (code {}): {}".format( e.cmd, e.returncode, e.output)) finally: dest.close() dest = open(solpath + str(num) + ".txt", "r") line = dest.read() if line != expect: print('f**k no') else: print('f**k yea') print('Expect: ', expect) print('Result: ', line) print('****') print('----') return line == expect
def test(input, expect, num): solpath = "./test/solutions/" dest = open(solpath + str(num) + ".txt","w") if type(input) is str: inputfile = TestUtil.makeSource(input,num) lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) else: inputfile = TestUtil.makeSource(str(input),num) asttree = input codeGen = CodeGenerator() path = solpath + str(num) if not os.path.isdir(path): os.mkdir(path) try: print("{0}{1}{0}".format('='*30, "Test "+ str(num))) codeGen.gen(asttree, path) subprocess.call("java -jar "+ JASMIN_JAR + " " + path + "/MCClass.j",shell=True,stderr=subprocess.STDOUT) #print("java -jar "+ JASMIN_JAR + " " + path + "/MCClass.j") f = open(solpath + str(num) + ".txt","w") subprocess.call("java -cp ./lib" + os.pathsep + ". MCClass",shell=True, stdout = f) #print("java -cp ./lib" + os.pathsep + ". MCClass") f.close() except StaticError as e: print(f"Error of test {num} is {e}") dest.write(str(e)) except subprocess.CalledProcessError as e: raise RuntimeError("command '{}' return with error (code {}): {}".format(e.cmd, e.returncode, e.output)) finally: dest.close() dest = open(solpath + str(num) + ".txt","r") line = dest.read() print(f"Result: {line}") print("{0}{1}{0}\n".format('='*30, "End test " + str(num))) return line == expect