/* * Copyright 2009 Wishnu Prasetya. * * This file is part of T2. * T2 is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License (GPL) as published by the * Free Software Foundation; either version 3 of the License, or any * later version. * * T2 is distributed in the hope that it will be useful, but WITHOUT ANY * WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * for more details. * * A copy of the GNU General Public License can be found in T2 distribution. * If it is missing, see http://www.gnu.org/licenses. */ package Sequenic.T2ext.CoverageEngine; import Sequenic.T2.Pool; import Sequenic.T2.TrFile; import Sequenic.T2.Seq.*; import java.lang.reflect.Method; import java.util.*; import Sequenic.T2ext.Analyzer.*; import Sequenic.T2ext.Instrumenter.*; /** * An abstract class for engines to generate new test-traces. The * global work flow is as follows: * * 1. We run T2 to generate N number of T2 test-traces; save those traces. * * 2. We use T2TraceFileCoverageAnalyzer to calculate the coverage of those * N traces. This involves reloading those traces from the save-file and * rerunning the traces. * * 3. Based on the coverage report obtained in 2, determine the set of * prime-paths which are still left uncovered. For each of those uncovered * path, call this engine (step 4). * * 4. The above 3 steps are before running this engine. Now, pass a target * path (a path that is still uncovered), the coverage report obtained * in step-2, as well as the used T2-coverage-analyzer to this engine. * * 5. Execute this engine to generate one or more test-traces that will generate * executions that more closely cover the target path. Hopefully, at least * one will completely cover it. Empty is returned if the engine fails to * generate a better trace. * * @author Wishnu Prasetya * */ abstract public class BaseEngine { /** * The target path to cover. */ LinkedList targetpath ; /** * This is to hold the T2-coverage report we received. We may need this e.g. * to obtain the test sequence that most closely cover the target path. */ public T2TraceFileCoverageResult covReport ; /** * The Cov-analyzer that was used to produce the above report. We need this * because it holds the used object pool and set of class invariants. */ public T2TraceFileCoverageAnalyzer t2covAnalyzer ; /** * Derived field. This is the CFG to which this targetpath belongs. */ public CFG CFG ; boolean debug = false ; public BaseEngine() { } public BaseEngine(T2TraceFileCoverageResult covReport_, T2TraceFileCoverageAnalyzer t2covAnalyzer_, LinkedList targetpath_){ covReport = covReport_ ; t2covAnalyzer = t2covAnalyzer_ ; targetpath = targetpath_ ; CFG = findCFG() ; } /** * Find the CFG to which the target path belongs to. */ private CFG findCFG() { for (Sequenic.T2ext.Instrumenter.CFG C : t2covAnalyzer.getCOVanalyzer().getCFGs().cfgs ) for (LinkedList tpath : C.getTargetPaths()) if (tpath == targetpath) return C ; return null ; } /** * This will hold the set of newly generated traces. */ public LinkedList> newTraces = new LinkedList>() ; /** * Generate new traces (in order to try to cover the target path). * Empty if the engine fails to produce a better trace. * Each pair will be tupled with its distance to the target-path. */ public abstract LinkedList> generateTraces() ; /** * Just a short-cut to execute a given T2-test-trace. It will return * the TraceExecInfo. Currently no error reporting will be produced. * This can be done later by the replay tool. */ public TraceExecInfo executeT2Trace(Trace tr) { return tr.exec(t2covAnalyzer.getC(), t2covAnalyzer.getPool(), t2covAnalyzer.getClassinvs(), tr.diverging, false, ReportersPool.NULLreporter) ; } /** * Just to test the generated traces... They will be executed, and the * coverage result will be printed. */ public CoverageResult testGeneratedTraces() { System.out.println("Path to cover: ") ; for (Node N : targetpath) { System.out.print(N.getId() + " --> ") ; } System.out.print("EXIT ") ; System.out.println("in method " + CFG.getMethodName() + "\n") ; Sensor.ejectObservations(); for (Pair tau : newTraces) executeT2Trace(tau.fst) ; CFGBunchFile CFGs = t2covAnalyzer.getCOVanalyzer().getCFGs() ; CoverageAnalyzer analz = new CoverageAnalyzer(CFGs) ; //analz.debug = true ; CoverageResult result = analz.calculateMethodCoverage(CFG) ; result.simplePrint(Sensor.getObservedPaths()) ; return result; } }