def test_minimize_balanced(self): class Interesting(DummyInteresting): def interesting(sub, conditionArgs, tempPrefix): # pylint: disable=no-self-argument # pylint: disable=missing-return-doc,missing-return-type-doc with open("a.txt", "rb") as f: data = f.read() if b"o\n" in data: a, b = data.split(b"o\n") return (a.count(b"{") == b.count(b"}")) and \ (a.count(b"(") == b.count(b")")) and \ (a.count(b"[") == b.count(b"]")) return False l = lithium.Lithium() l.conditionScript = Interesting() l.strategy = lithium.MinimizeBalancedPairs() for testcaseType in (lithium.TestcaseChar, lithium.TestcaseLine, lithium.TestcaseSymbol): log.info("Trying with testcase type %s:", testcaseType.__name__) with open("a.txt", "wb") as f: f.write(b"[\n[\nxxx{\no\n}\n]\n]\n") l.testcase = testcaseType() l.testcase.readTestcase("a.txt") self.assertEqual(l.run(), 0) with open("a.txt", "rb") as f: self.assertEqual(f.read(), b"o\n")
def run(args, classpath, java_file, together_java_files): """ Main entry of the file minimization. @param args args parsed by ArgumentParser, used to running debugged tool @param classpath the necessary classpath to compile the given java_file @param java_file the java file that need to be minimized @param together_java_files files that need to run with the given java_file to trigger tool crash """ print("====== Minimizing file {} ======".format(java_file)) FileMinimization.preprocess(java_file) l = lithium.Lithium() l.conditionArgs = args l.conditionScript = FileInterestingJudger(java_file, together_java_files, classpath) l.testcase = lithium.TestcaseLine() l.testcase.readTestcase(java_file) # First round of reduction by main minimization algorithm print("====== Performing main minimization algorithm ======") l.strategy = lithium.Minimize() l.run() print("------ main minimization algorithm done ------") # Second round of reduction, focus on reducing balanced pairs print("====== Minimizing balanced pairs ======") l.strategy = lithium.MinimizeBalancedPairs() l.run() print("------ Minimizing balanced pairs done ------") # Third round ofreduction, reducing surrounding pairs print("====== Minimizing surrounding pairs ======") l.strategy = lithium.MinimizeSurroundingPairs() l.run() print("------ Minimizing surrounding pairs done ------") # Final round of reduction, repeat the main minimization algorithm print("====== Performing main minimization algorithm ======") l.strategy = lithium.Minimize() l.run() print("------ main minimization algorithm done ------") print("------ file {} has been minimized ------".format(java_file))