public static string GetIdentifiedTest( FileInfo testFile, TestInfo info, LanguageSupport support, out string relativePath) { relativePath = ParaibaPath.GetRelativePath(testFile.FullName, info.BasePath); var ast = support.CodeToXml.GenerateFromFile(testFile.FullName); // テストケース識別用コードの埋め込み CodeTransformer.InsertIntoTestCase(info, ast, support, relativePath); // コード生成 return support.XmlToCode.Generate(ast); }
public static void ReadFile(TestInfo testInfo, FileInfo fileInfo) { using (var fs = new FileStream(fileInfo.FullName, FileMode.Open)) { if (IsTextFile(fs)) { ReadTextFile(testInfo, fs, testInfo.TestCases.Count > 0 ? testInfo.TestCases[0] : null); } else { ReadBinaryFile(testInfo, fs, testInfo.TestCases.Count > 0 ? testInfo.TestCases[0] : null); } } }
public static void ReadFile(TestInfo testInfo, FileInfo fileInfo, TestCase initialTestCase) { using (var fs = new FileStream(fileInfo.FullName, FileMode.Open)) { if (IsTextFile(fs)) { ReadTextFile(testInfo, fs, initialTestCase); } else { ReadBinaryFile(testInfo, fs, initialTestCase); } } }
public static void ReadBinaryFile( TestInfo testInfo, FileStream fs, TestCase initialTestCase) { // TODO: Should be null (but KLEE uses initialTestCase) var testCase = initialTestCase; while (true) { var id = (fs.ReadByte() << 24) + (fs.ReadByte() << 16) + (fs.ReadByte() << 8) + (fs.ReadByte() << 0); var value = fs.ReadByte(); if (value == -1) { return; } switch ((ElementType)(value >> 2)) { case ElementType.Statement: testCase.Statements.Add(id); testCase.StatementConditionDecisions.Add(id); testCase.Paths.Add(id); break; case ElementType.Decision: id = (value & 1) == 0 ? id : -id; testCase.Decisions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.Condition: id = (value & 1) == 0 ? id : -id; testCase.Conditions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.DecisionAndCondition: id = (value & 1) == 0 ? id : -id; testCase.Decisions.Add(id); testCase.Conditions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.TestCase: if (testInfo.TestCases.Count <= id) { throw new InvalidOperationException( "There is contradiction between the coverage data and the source code. Please retry to measure coverage data."); } testCase = testInfo.TestCases[id]; break; } } }
public static void ReadTextFile( TestInfo testInfo, FileStream fs, TestCase initialTestCase) { // TODO: Should be null (but KLEE uses initialTestCase) var testCase = initialTestCase; using (var reader = new StreamReader(fs)) { string line; while (!string.IsNullOrEmpty((line = reader.ReadLine()))) { var items = line.Split(' '); var id = int.Parse(items[0]); var value = int.Parse(items[2]); switch ((ElementType)int.Parse(items[1])) { case ElementType.Statement: testCase.Statements.Add(id); testCase.StatementConditionDecisions.Add(id); testCase.Paths.Add(id); break; case ElementType.Decision: id = (value & 1) == 0 ? id : -id; testCase.Decisions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.Condition: id = (value & 1) == 0 ? id : -id; testCase.Conditions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.DecisionAndCondition: id = (value & 1) == 0 ? id : -id; testCase.Decisions.Add(id); testCase.Conditions.Add(id); testCase.ConditionDecisions.Add(id); testCase.StatementConditionDecisions.Add(id); break; case ElementType.TestCase: if (testInfo.TestCases.Count <= id) { throw new InvalidOperationException( "There is contradiction between the coverage data and the source code. Please retry to measure coverage data."); } testCase = testInfo.TestCases[id]; break; } } } }
private static void AnalyzeTestResultCsv(FileInfo csvFileInfo, TestInfo testInfo) { var passTestLists = new List<string>(); var spriter = new[] { ',' }; using (var reader = csvFileInfo.OpenText()) { foreach (var line in reader.ReadLines()) { passTestLists.AddRange(line.Split(spriter).ToList()); } reader.Close(); } foreach (var passTest in passTestLists) { var testCase = testInfo.TestCases.FirstOrDefault(t => t.Name.EndsWith(passTest)); if (testCase != null) { testCase.Passed = true; } else { Console.Error.WriteLine("[WARNING] the testcase of '" + passTest + "' is not founded."); } } }
private static void AnalyzeTestResult(DirectoryInfo rootDirInfo, TestInfo testInfo) { var fileInfo = rootDirInfo.GetFile(OccfNames.SuccessfulTests); using (var reader = fileInfo.OpenText()) { foreach (var line in reader.ReadLines()) { var testCase = testInfo.TestCases.FirstOrDefault(t => t.Name.EndsWith(line)); if (testCase != null) { testCase.Passed = true; } else { Console.Error.WriteLine("[WARNING] the testcase of '" + line + "' is not founded."); } } } }
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 void WriteInfoFiles(DirectoryInfo rootDir, CoverageInfo covInfo, TestInfo testInfo) { var formatter = new BinaryFormatter(); covInfo.Write(rootDir, formatter); if (testInfo == null) { return; } testInfo.Write(rootDir, formatter); }
private static void WriteTestCodeFiles( DirectoryInfo rootDir, DirectoryInfo testDir, LanguageSupport prof, TestInfo info) { var paths = prof.FilePatterns.SelectMany( pattern => testDir.EnumerateFiles( pattern, SearchOption.AllDirectories)); foreach (var path in paths.ToList()) { var backPath = Path.Combine(rootDir.FullName, path.FullName + OccfNames.BackupSuffix); path.CopyTo(backPath, true); //バックアップファイルの上書き可 var outPath = OccfCodeGenerator.AnalyzeAndWriteIdentifiedTest(prof, info, path, rootDir); Console.WriteLine("wrote:" + outPath); } }
public static void InsertMeasurementCode( DirectoryInfo rootDir, ICollection<FileInfo> fileInfos, DirectoryInfo testDir, DirectoryInfo libDir, LanguageSupport mode, RecordingMode recordingMode) { Contract.Requires<ArgumentException>(rootDir.Exists); Contract.Requires<ArgumentException>(testDir == null || testDir.Exists); Contract.Requires<ArgumentException>(libDir.Exists); Contract.Requires<ArgumentNullException>(mode != null); //root var covInfo = new CoverageInfo(rootDir.FullName, mode.Name, SharingMethod.File); //(o)root or src? var testInfo = testDir != null ? new TestInfo(rootDir.FullName) : null; //root RemoveExistingCoverageDataFiles(rootDir, libDir); mode.RemoveLibraries(libDir); //+src WriteProductionCodeFiles(rootDir, fileInfos, mode, covInfo); if (testInfo != null) { //(o)root or src WriteTestCodeFiles(rootDir, testDir, mode, testInfo); } else { // Initialize test information with empty contents testInfo = new TestInfo(rootDir.FullName); testInfo.TestCases.Add(new TestCase("nothing", "nothing", new CodeRange())); } //root WriteInfoFiles(rootDir, covInfo, testInfo); mode.CopyLibraries(libDir, recordingMode); }
public static string GetIdentifiedTest(FileInfo testFile, TestInfo info, LanguageSupport support) { string relativePath; return GetIdentifiedTest(testFile, info, support, out relativePath); }
public static string AnalyzeAndWriteIdentifiedTest( LanguageSupport support, TestInfo info, FileInfo fullPath, DirectoryInfo outDirPath) { string relativePath; var code = GetIdentifiedTest(fullPath, info, support, out relativePath); return WriteCode(relativePath, outDirPath, code); }
public static void InsertIntoTestCase( TestInfo info, XElement root, LanguageSupport support, string relativePath) { var nodes = support.AstAnalyzer.FindTestCases(root); var trans = support.AstTransformer; foreach (var node in nodes.ToList()) { var id = info.TestCases.Count; var testCase = trans.InsertTestCaseId(node, id, relativePath); info.TestCases.Add(testCase); } // Add import for loggin executed items support.AstTransformer.InsertImport(root); }
public static void ReadJUnitResult(FileInfo resultFile, TestInfo testInfo) { using (var reader = new StreamReader(resultFile.FullName)) { var failedTestIndex = 0; var prefix = GetFailedTestCasePrefix(ref failedTestIndex); while (true) { var line = reader.ReadLine(); if (line == null) { return; } if (!line.StartsWith(prefix)) { continue; } var match = FailedTestLineRegex.Match(line.Substring(prefix.Length)); if (!match.Success) { continue; } var name = match.Groups[2] + "." + match.Groups[1]; // TODO: O(n^2) var testCase = testInfo.TestCases.FirstOrDefault(t => t.Name == name); if (testCase != null) { testCase.Passed = false; } prefix = GetFailedTestCasePrefix(ref failedTestIndex); } } }
public static IEnumerable<LocalizedElement> LocalizeStatements( CoverageInfo covInfo, TestInfo testInfo, string metricsFilePath) { var calcMetricFunc = LoadMetricFunc(metricsFilePath); var maxStatementCount = covInfo.StatementIndexAndTargets.Count(); var currentStatementCount = 1; foreach (var stmt in covInfo.StatementIndexAndTargets) { var id = stmt.Item1; var element = stmt.Item2; var passedTestCases = testInfo.TestCases.Where(t => t.Passed); var executedAndPassedTestCases = passedTestCases.Where(t => t.Statements.Contains(id)); var failedTestCases = testInfo.TestCases.Where(t => !t.Passed); var executedAndFailedTestCases = failedTestCases.Where(t => t.Statements.Contains(id)); var localized = new LocalizedElement { ExecutedAndPassed = executedAndPassedTestCases.Count(), Passed = passedTestCases.Count(), ExecutedAndFailed = executedAndFailedTestCases.Count(), Failed = failedTestCases.Count(), Element = element, }; var metrics = calcMetricFunc( localized.ExecutedAndPassed, localized.Passed, localized.ExecutedAndFailed, localized.Failed).Cast<double>().ToList(); localized.Values = metrics.ToList(); yield return localized; Console.Write("Calculating: " + currentStatementCount + " / " + maxStatementCount + "\r"); currentStatementCount++; } }