def gen_sol(testcase_dir, solution_dir): test_files = os.listdir(testcase_dir) for filename in tqdm(test_files, desc="Build solution"): print(filename) inputfile = FileStream(os.path.join(testcase_dir, filename)) dest = open(os.path.join(solution_dir, filename), "w") # lexer = BKITLexer(input) # tokens = CommonTokenStream(lexer) # parser = BKITParser(tokens) # tree = parser.program() # asttree = ASTGeneration().visit(tree) # dest.write(str(asttree)) # dest.close() if type(input) is str: lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) checker = StaticChecker(asttree) try: res = checker.check() #dest.write(str(list(res))) except StaticError as e: dest.write(str(e)) finally: dest.close()
def test(input, expect, num): dest = open("./test/solutions/" + 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 checker = StaticChecker(asttree) try: checker.check() dest.write("") except StaticError as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() print(line) print(expect) return line == expect
def checkStatic(input, expect, num): dest = open("./test/solutions/" + 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 checker = StaticChecker(asttree) try: res = checker.check() # dest.write(str(list(res))) except StaticError as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() if (line != expect): print("Number:", num) print("Input:", input) print("Output:", line) print("Expect:", expect) print( "-------------------------------------------------------------------\n\n" ) return line == expect
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 checkASTGen(input, expect, num): inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() return line == str(expect)
def gen_sol(testcase_dir, solution_dir): test_files = os.listdir(testcase_dir) for filename in tqdm(test_files, desc="Build solution"): print(filename) inputfile = FileStream(os.path.join(testcase_dir,filename)) dest = open(os.path.join(solution_dir, filename),"w") lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close()
def checkParser(input, expect, num): inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) listener = TestParser.createErrorListener() tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) parser.removeErrorListeners() parser.addErrorListener(listener) try: parser.program() dest.write("successful") except SyntaxException as f: dest.write(f.message) except Exception as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() if line != expect: print('\nTest case number: {}\n'.format(num)) print('Input: {}\n'.format(input)) print('Output: {}\n'.format(line)) print('Expect: {}\n'.format(expect)) print('--------------\n'.replace('-', '------')) return line == expect
def test(inputdir, outputdir, num): #print("inutdir = "+inputdir) #print("outputdir = "+outputdir) dest = open(outputdir + "/" + str(num) + ".txt", "w") lexer = BKITLexer(FileStream(inputdir + "/" + str(num) + ".txt")) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close() dest1 = open(outputdir + "/" + str(num) + ".json", "w") dest1.write(asttree.to_json()) dest1.close()
def checkParser(input,expect,num): inputfile = TestUtil.makeSource(input,num) dest = open("./test/solutions/" + str(num) + ".txt","w") lexer = BKITLexer(inputfile) listener = TestParser.createErrorListener() tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) parser.removeErrorListeners() parser.addErrorListener(listener) try: parser.program() dest.write("successful") except SyntaxException as f: dest.write(f.message) except Exception as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt","r") line = dest.read() ##MYPART: check output vs expected print(num,"--Output:",line,"--Expect",expect) return line == expect
def checkASTGen(input, expect, num): print(str(num), '::', input, '\n') inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() print('success' if line == str(expect) else 'fail', 'Expect: ', str(expect), 'Result: ', line) print() return line == str(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
def checkASTGen(input, expect, num): inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() print('\nTest case number: {}\n'.format(num)) print('Input: {}\n'.format(input)) print('Output: {}\n'.format(line)) print('Expect: {}\n'.format(expect)) print('--------------\n'.replace('-', '------')) return line == str(expect)
def test1(inputdir, outputdir, num): dest = open(outputdir + "/" + str(num) + ".txt", "w") try: lexer = BKITLexer(FileStream(inputdir + "/" + str(num) + ".txt")) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) checker = StaticChecker(asttree) res = checker.check() except StaticError as e: dest.write(str(e) + '\n') except: trace = traceback.format_exc() print("Test " + str(num) + " catches unexpected error:" + trace + "\n") finally: dest.close()
def checkASTGen(input, expect, num): testfile.write(""" def test_""" + str(TestAST._TestAST__count) + """(self): input = \"\"\"""" + input + """\"\"\" expect = """) inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) tree = parser.program() asttree = ASTGeneration().visit(tree) dest.write(str(asttree)) dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() testfile.write(line + """ self.assertTrue(TestAST.checkASTGen(input,expect,""" + str(TestAST._TestAST__count) + """))\n""") TestAST.__count += 1 return line == str(expect)
def checkParser(input, expect, num): inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) listener = TestParser.createErrorListener() tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) parser.removeErrorListeners() parser.addErrorListener(listener) try: parser.program() dest.write("successful") except SyntaxException as f: pass dest.write(f.message) except Exception as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() if (line != expect): print('---------------------------') print('Case: ' + str(num)) print("Testcase: " + input) print("Solution: " + line) print("Expect: " + expect) print('---------------------------') return line == expect
def checkParser(input, expect, num): testcase = "./test/testParser.txt" testfile = open(testcase, "a") testfile.write(""" def test_""" + str(num) + """(self): \"\"\"Created automatically\"\"\" input = r\"\"\"""" + input + """\"\"\" expect = r\"\"\"""") inputfile = TestUtil.makeSource(input, num) dest = open("./test/solutions/" + str(num) + ".txt", "w") lexer = BKITLexer(inputfile) listener = TestParser.createErrorListener() tokens = CommonTokenStream(lexer) parser = BKITParser(tokens) parser.removeErrorListeners() parser.addErrorListener(listener) try: parser.program() dest.write("successful") except SyntaxException as f: dest.write(f.message) except Exception as e: dest.write(str(e)) finally: dest.close() dest = open("./test/solutions/" + str(num) + ".txt", "r") line = dest.read() testfile.write(line + """\"\"\" self.assertTrue(TestParser.checkParser(input,expect,""" + str(num) + """))""") testfile.close() return line == expect