Ejemplo n.º 1
0
    def test_empty(self):
        l = lithium.Lithium()
        with open("empty.txt", "w"):
            pass

        class Interesting(DummyInteresting):
            inter = False

            def interesting(sub, conditionArgs, tempPrefix):
                return sub.inter

        l.conditionScript = Interesting()
        l.strategy = lithium.Minimize()
        l.testcase = lithium.TestcaseLine()
        l.testcase.readTestcase("empty.txt")
        with self.assertLogs("lithium") as logs:
            self.assertEqual(l.run(), 1)
        self.assertIn(
            "INFO:lithium:Lithium result: the original testcase is not 'interesting'!",
            logs.output)
        Interesting.inter = True
        with self.assertLogs("lithium") as logs:
            self.assertEqual(l.run(), 0)
        self.assertIn(
            "INFO:lithium:The file has 0 lines so there's nothing for Lithium to try to remove!",
            logs.output)
Ejemplo n.º 2
0
    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))
Ejemplo n.º 3
0
    def test_minimize(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:
                    return b"o\n" in f.read()
        l = lithium.Lithium()
        l.conditionScript = Interesting()
        l.strategy = lithium.Minimize()
        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"x\n\nx\nx\no\nx\nx\nx\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")