private static bool Analyze(DirectoryInfo rootDir, FileInfo covDataFile)
        {
            var formatter    = new BinaryFormatter();
            var covInfoFile  = FileUtil.GetCoverageInfo(rootDir);
            var testInfoFile = FileUtil.GetTestInfo(rootDir);
            var covInfo      = CoverageInfo.Read(covInfoFile, formatter);
            var testInfo     = TestInfo.Read(testInfoFile, formatter);

            testInfo.InitializeForStoringData(true);
            CoverageDataReader.ReadFile(testInfo, covDataFile);

            foreach (var testCase in testInfo.TestCases)
            {
                Console.WriteLine(
                    "**** " + testCase.RelativePath + ": " + testCase.Name + " ****");
                var stmts = testCase.Paths.Select(i => covInfo.Targets[i]);
                foreach (var stmt in stmts)
                {
                    Console.WriteLine(stmt.RelativePath + ": " + stmt.Position.SmartLineString);
                }
                Console.WriteLine();
                Console.WriteLine();
            }

            return(true);
        }
        private static void Localize(
            DirectoryInfo rootDir, FileInfo resultFile,
            FileInfo recordFile, string metricsFilePath, DirectoryInfo csvDir)
        {
            var covInfoFile  = FileUtil.GetCoverageInfo(rootDir);
            var testInfoFile = FileUtil.GetTestInfo(rootDir);
            var covInfo      = CoverageInfo.Read(covInfoFile);
            var testInfo     = TestInfo.Read(testInfoFile);

            testInfo.InitializeForStoringData(true);
            ReadJUnitResult(resultFile, testInfo);
            CoverageDataReader.ReadFile(testInfo, recordFile);

            var localizeStatements = LocalizeStatements(covInfo, testInfo, metricsFilePath).ToList();

            ShowLocalizeStatements(localizeStatements,
                                   new Dictionary <FileInfo, Dictionary <int, int> >(),
                                   metricsFilePath);

            if (csvDir != null)
            {
                LocalizeStatementsCsv(csvDir, localizeStatements,
                                      new Dictionary <FileInfo, Dictionary <int, int> >());
            }
        }
Exemplo n.º 3
0
        private static void VerifyMeasureAndLocalize(
            string testTargetNames, string inDirPath, string expDirPath, DirectoryInfo outDir)
        {
            var ret = Inserter.Run(
                new[] {
                "-r",
                outDir.FullName,
                "-t",
                outDir.GetDirectory("test").FullName,
            });

            Assert.That(ret, Is.True);

            // jarとdllファイルが存在するか
            var jar = outDir.GetFile("CoverageWriter.jar");

            Assert.That(jar.Exists, Is.True);

            var covInfoFile  = outDir.GetFile(OccfNames.CoverageInfo);
            var testInfoFile = outDir.GetFile(OccfNames.TestInfo);
            var recordFile   = outDir.GetFile(OccfNames.Record);
            var resultFile   = outDir.GetFile("testresult.txt");

            var targets = Directory.EnumerateFiles(
                expDirPath, "*.java",
                SearchOption.AllDirectories)
                          .Concat(
                Directory.EnumerateFiles(
                    expDirPath, OccfNames.BackupSuffix,
                    SearchOption.AllDirectories))
                          .Concat(new[] { covInfoFile.FullName, testInfoFile.FullName });

            foreach (var target in targets)
            {
                AssertEqualFiles(target, expDirPath, inDirPath);
            }

            Compile(outDir.FullName);
            RunTest(outDir.FullName, testTargetNames);

            var covInfo  = CoverageInfo.Read(covInfoFile);
            var testInfo = TestInfo.Read(testInfoFile);

            testInfo.InitializeForStoringData(true);
            BugLocalizer.ReadJUnitResult(resultFile, testInfo);
            CoverageDataReader.ReadFile(testInfo, recordFile);

            var localizeStatements = BugLocalizer.LocalizeStatements(covInfo, testInfo,
                                                                     "metrics/BugLocalization.py");

            Assert.That(localizeStatements, Is.Not.Null);
        }
        private static TestInfo AnalyzeKleeTestFiles(DirectoryInfo testDirInfo)
        {
            var files    = testDirInfo.EnumerateFiles("*.ktest");
            var testInfo = new TestInfo(testDirInfo.FullName);

            testInfo.InitializeForStoringData(false);
            foreach (var file in files)
            {
                var relativePath = ParaibaPath.GetRelativePath(file.FullName, testDirInfo.FullName);
                var testCase     = new TestCase(relativePath, file.FullName, new CodeRange());
                testInfo.TestCases.Add(testCase);
                testCase.InitializeForStoringData(false);
                var dataPath = file.FullName + OccfNames.Record;
                CoverageDataReader.ReadFile(testInfo, new FileInfo(dataPath), testCase);
            }
            return(testInfo);
        }
        private static bool Detect(DirectoryInfo rootDir, FileInfo covDataFile, string criterion)
        {
            var formatter    = new BinaryFormatter();
            var testInfoPath = FileUtil.GetTestInfo(rootDir);
            var testInfo     = TestInfo.Read(testInfoPath, formatter);

            testInfo.InitializeForStoringData(true);
            CoverageDataReader.ReadFile(testInfo, covDataFile);
            var testCases = testInfo.TestCases;

            Func <TestCase, TestCase, bool> isDuplicated;

            switch (criterion)
            {
            case "statement":
                isDuplicated = (tc, tc2) => tc.Statements.IsSubsetOf(tc2.Statements);
                break;

            case "decision":
            case "branch":
                isDuplicated = (tc, tc2) => tc.Decisions.IsSubsetOf(tc2.Decisions);
                break;

            case "condition":
                isDuplicated = (tc, tc2) => tc.Conditions.IsSubsetOf(tc2.Conditions);
                break;

            case "branch/condition":
            case "decision/condition":
            case "condition/branch":
            case "condition/decision":
                isDuplicated =
                    (tc, tc2) => tc.ConditionDecisions.IsSubsetOf(tc2.ConditionDecisions);
                break;

            case "subpath":
                isDuplicated = (tc, tc2) => tc.Paths.IsSubSequence(tc2.Paths);
                break;

            case "path":
                isDuplicated = (tc, tc2) => tc.Paths.SequenceEqual(tc2.Paths);
                break;

            default:
                throw new ArgumentException('"' + criterion + '"' + " is not supported.");
            }

            foreach (var tc in testCases)
            {
                var dups = testInfo.TestCases
                           .Where(tc2 => tc2 != tc && isDuplicated(tc, tc2));
                var tcStr = Environment.NewLine + tc.Name +
                            "(" + tc.RelativePath + ":" + tc.Position.SmartLineString + ")" +
                            " is duplicated with:" + Environment.NewLine;

                foreach (var dup in dups)
                {
                    var dupStr = dup.Name +
                                 "(" + dup.RelativePath + ":" + dup.Position.SmartLineString
                                 + ")";
                    Console.WriteLine(tcStr + "  " + dupStr);
                    tcStr = "";
                }
            }
            return(true);
        }
        private static bool Analyze(DirectoryInfo rootDir, FileInfo covDataFile, bool detail)
        {
            // カバレッジ情報(母数)の取得
            var formatter   = new BinaryFormatter();
            var covInfoPath = FileUtil.GetCoverageInfo(rootDir);
            var covInfo     = CoverageInfo.Read(covInfoPath, formatter);

            CoverageDataReader.ReadFile(covInfo, covDataFile);

            var tags = ReconstructTags(covInfo);

            foreach (var tag in tags /*.Where(t => !t.Contains("class "))*/)
            {
                Console.WriteLine(tag);
                {
                    var checkedLine    = new HashSet <Tuple <string, int> >();
                    var executedAndNot = covInfo.StatementTargets
                                         .Where(e => e.Tag.StartsWith(tag))
                                         .Where(e => checkedLine.Add(Tuple.Create(e.RelativePath, e.Position.StartLine)))
                                         .Halve(e => e.State == CoverageState.Done);
                    var nExe = executedAndNot.Item1.Count;
                    var nAll = nExe + executedAndNot.Item2.Count;
                    Console.WriteLine(
                        "Line Coverage: " + nExe * (100.0 / nAll) + "% : " + nExe + " / "
                        + nAll);
                    if (detail)
                    {
                        foreach (var element in executedAndNot.Item2)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var executedAndNot = covInfo.StatementTargets
                                         .Where(e => e.Tag.StartsWith(tag))
                                         .Halve(e => e.State == CoverageState.Done);
                    var nExe = executedAndNot.Item1.Count;
                    var nAll = nExe + executedAndNot.Item2.Count;
                    Console.WriteLine(
                        "Statement Coverage: " + nExe * (100.0 / nAll) + "% : " + nExe + " / "
                        + nAll);
                    if (detail)
                    {
                        foreach (var element in executedAndNot.Item2)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var executedAndNot = covInfo.BranchTargets
                                         .Where(e => e.Tag.StartsWith(tag))
                                         .Halve(e => e.State == CoverageState.Done);
                    var nExe = executedAndNot.Item1.Count;
                    var nAll = nExe + executedAndNot.Item2.Count;
                    Console.WriteLine(
                        "Branch Coverage: " + nExe * (100.0 / nAll) + "% : " + nExe + " / "
                        + nAll);
                    if (detail)
                    {
                        foreach (var element in executedAndNot.Item2)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var nExe        = 0;
                    var nAll        = 0;
                    var notExecuted = new List <ICoverageElement>();
                    var targets     = covInfo.BranchConditionTargets.Where(
                        e => e.Tag.StartsWith(tag))
                                      .SelectMany(
                        e =>
                        e.Targets.Count > 0
                                                                                                        ? e.Targets : Enumerable.Repeat((ICoverageElement)e, 1));
                    foreach (var t in targets)
                    {
                        nAll += 2;
                        if (t.State == CoverageState.Done)
                        {
                            nExe += 2;
                        }
                        else
                        {
                            notExecuted.Add(t);
                            if (t.State != CoverageState.None)
                            {
                                nExe++;
                            }
                        }
                    }
                    Console.WriteLine(
                        "Branch Coverage in detail: " + nExe * (100.0 / nAll) + "% : " + nExe
                        + " / " + nAll);
                    if (detail)
                    {
                        foreach (var element in notExecuted)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var executedAndNot = covInfo.BranchConditionTargets
                                         .Where(e => e.Tag.StartsWith(tag))
                                         .Halve(e => e.StateChildrenOrParent == CoverageState.Done);
                    var nExe = executedAndNot.Item1.Count;
                    var nAll = nExe + executedAndNot.Item2.Count;
                    Console.WriteLine(
                        "Condition Coverage: " + nExe * (100.0 / nAll) + "% : " + nExe + " / "
                        + nAll);
                    if (detail)
                    {
                        foreach (var element in executedAndNot.Item2)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var nExe        = 0;
                    var nAll        = 0;
                    var notExecuted = new List <ICoverageElement>();
                    foreach (
                        var t in covInfo.BranchConditionTargets.Where(e => e.Tag.StartsWith(tag))
                        )
                    {
                        if (t.Targets.Count == 0)
                        {
                            nAll += 2;
                            if (t.State == CoverageState.Done)
                            {
                                nExe += 2;
                            }
                            else
                            {
                                notExecuted.Add(t);
                                if (t.State != CoverageState.None)
                                {
                                    nExe++;
                                }
                            }
                        }
                        else
                        {
                            foreach (var t2 in t.Targets)
                            {
                                nAll += 2;
                                if (t2.State == CoverageState.Done)
                                {
                                    nExe += 2;
                                }
                                else
                                {
                                    notExecuted.Add(t2);
                                    if (t2.State != CoverageState.None)
                                    {
                                        nExe++;
                                    }
                                }
                            }
                        }
                    }
                    Console.WriteLine(
                        "Condition Coverage in detail: " + nExe * (100.0 / nAll) + "% : " + nExe
                        + " / "
                        + nAll);
                    if (detail)
                    {
                        foreach (var element in notExecuted)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                {
                    var executedAndNot = covInfo.BranchConditionTargets
                                         .Where(e => e.Tag.StartsWith(tag))
                                         .Halve(e => e.State == CoverageState.Done);
                    var nExe = executedAndNot.Item1.Count;
                    var nAll = nExe + executedAndNot.Item2.Count;
                    Console.WriteLine(
                        "Branch/Condition Coverage: " + nExe * (100.0 / nAll) + "% : " + nExe
                        + " / " + nAll);
                    if (detail)
                    {
                        foreach (var element in executedAndNot.Item2)
                        {
                            Console.Write(element.Position.SmartPositionString);
                        }
                    }
                }
                Console.WriteLine();
            }
            return(true);
        }