Exemple #1
0
        public override bool Execute(out ResultTuple ret, ResultTuple[] parameters,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out bool preconditionViolated, out Exception exceptionThrown, out bool contractViolated, bool forbidNull, bool useRandoopContracts, out ContractState contractStates)
        {
            long startTime = 0;

            Timer.QueryPerformanceCounter(ref startTime);

            object[] objects = new object[method.GetParameters().Length];
            // Get the actual objects from the results using parameterIndices;
            object receiver = parameters[parameterMap[0].planIndex].tuple[parameterMap[0].resultIndex];

            for (int i = 0; i < method.GetParameters().Length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i + 1];
                objects[i] = parameters[pair.planIndex].tuple[pair.resultIndex];
            }

            contractStates       = new ContractState();
            preconditionViolated = false;
            if (useRandoopContracts)
            {
                try
                {
                    preconditionViolated = new RandoopContractsManager().PreconditionViolated(method, objects);
                }
                catch (InvalidRandoopContractException) { } //precondition is invalid, ignore it and proceed with execution

                if (preconditionViolated)
                {
                    ret              = null;
                    exceptionThrown  = null;
                    contractViolated = false;
                    return(false);
                }
            }

            if (forbidNull)
            {
                foreach (object o in objects)
                {
                    Util.Assert(o != null);
                }
            }

            CodeExecutor.CodeToExecute call;

            object returnValue = null;

            contractViolated = false; //default value of contract violation

            call = delegate() { returnValue = method.Invoke(receiver, objects); };


            bool retval = true;

            executionLog.WriteLine("execute method " + method.Name
                                   + "[" + (timesExecuted - 1).ToString() + "]"); //[email protected] changes
            Logger.Debug("execute method " + method.Name                          //[email protected] adds
                         + "[" + (timesExecuted - 1).ToString() + "]");

            executionLog.Flush();

            //if (timesExecuted != ReturnValue.Count + 1) //[email protected] adds for debug
            //{
            //    Logger.Debug("timeExecute = " + timesExecuted.ToString() +
            //        " but ReturnValue is " + ReturnValue.Count.ToString());
            //}

            timesExecuted++;
            if (!CodeExecutor.ExecuteReflectionCall(call, debugLog, out exceptionThrown))
            {
                //for exns we can ony add the class to faulty classes when its a guideline violation
                if (Util.GuidelineViolation(exceptionThrown.GetType()))
                {
                    PlanManager.numDistinctContractViolPlans++;

                    KeyValuePair <MethodBase, Type> k = new KeyValuePair <MethodBase, Type>(method, exceptionThrown.GetType());
                    if (!exnViolatingMethods.ContainsKey(k))
                    {
                        PlanManager.numContractViolatingPlans++;
                        exnViolatingMethods[k] = true;
                    }

                    //add this class to the faulty classes
                    contractExnViolatingClasses[method.DeclaringType] = true;
                    contractExnViolatingMethods[method] = true;
                }

                ret = null;
                executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString()
                                       + "]: invocationOk is false.");//[email protected] adds

                //string temp = "RANDOOPFAIL"; //[email protected] adds for capture current status
                ReturnValue.Add(null); //[email protected] adds for capture current status

                return(false);
            }
            else
            {
                ret = new ResultTuple(method, receiver, returnValue, objects);

                #region caputre latest execution return value
                ////[email protected] adds to capture return value -- start////
                if (returnValue != null)
                {
                    if ((returnValue.GetType() == typeof(string)) ||
                        (returnValue.GetType() == typeof(bool)) ||
                        (returnValue.GetType() == typeof(byte)) ||
                        (returnValue.GetType() == typeof(short)) ||
                        (returnValue.GetType() == typeof(int)) ||
                        (returnValue.GetType() == typeof(long)) ||
                        (returnValue.GetType() == typeof(float)) ||
                        (returnValue.GetType() == typeof(double)) ||
                        (returnValue.GetType() == typeof(char)))
                    {
                        executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString() + "]: "
                                               + returnValue.ToString().Replace("\n", "\\n").Replace("\r", "\\r"));
                    }
                    else
                    {
                        executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString() + "]: not string or primitive");
                    }

                    //doulbe check to make sure there is no non-deterministic exeuction -- we don't want to regression assertion with that
                    //This is not a sufficient approach because the difference may be inherited from previous constructors or method calls
                    //What was done in Randoop(java): after generating an "entire" test suite, Randoop runs it before outputting it.
                    //If any test fails, Randoop disables each failing assertions.
                    //let the VS plug-in do this functionality
                    if (Execute2(returnValue, objects, receiver, executionLog, debugLog))
                    {
                        ReturnValue.Add(returnValue);
                    }
                    else
                    {
                        ReturnValue.Add(null);
                    }
                }
                else
                {
                    executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString()
                                           + "]: no return value");

                    ReturnValue.Add(null);
                }
                ////[email protected] adds to capture return value -- end////
                #endregion caputre latest execution return value
            }

            //check if the objects in the output tuple violated basic contracts
            if (ret != null)
            {
                CheckContracts(ret, ref contractViolated, ref retval);
                contractStates = new RandoopContractsManager().ValidateAssertionContracts(method, receiver, returnValue);
            }

            if (contractViolated)                              //[email protected] adds
            {
                executionLog.WriteLine("contract violation."); //[email protected] adds
            }
            long endTime = 0;
            Timer.QueryPerformanceCounter(ref endTime);
            executionTimeAccum += endTime - startTime / (double)Timer.PerfTimerFrequency;

            return(retval);
        }
Exemple #2
0
 public void SetUp()
 {
     contractsManager = new RandoopContractsManager();
 }
Exemple #3
0
        ////[email protected] adds for capture return value for regression assertion
        //public override string ToCSharpCode(ReadOnlyCollection<string> arguments, string newValueName, string return_val)
        //{
        //    return ToCSharpCode(arguments, newValueName);
        //}


        //TODO Diana: check for precondition violations if useRandoopContracts = true
        //TODO Diana: compute canGenerateContractAssertions
        public override bool Execute(out ResultTuple ret, ResultTuple[] results,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out bool preconditionViolated, out Exception exceptionThrown, out bool contractViolated, bool forbidNull, bool useRandoopContracts, out ContractState contractStates)
        {
            contractViolated     = false;
            preconditionViolated = false;
            contractStates       = new ContractState();

            long startTime = 0;

            Timer.QueryPerformanceCounter(ref startTime);

            object[] objects = new object[constructor.GetParameters().Length];
            // Get the actual objects from the results using parameterIndices;
            for (int i = 0; i < constructor.GetParameters().Length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];
                objects[i] = results[pair.planIndex].tuple[pair.resultIndex];
            }

            preconditionViolated = false;
            if (useRandoopContracts)
            {
                try
                {
                    preconditionViolated = new RandoopContractsManager().PreconditionViolated(constructor, objects);
                }
                catch (InvalidRandoopContractException) { } //precondition is invalid, ignore it and proceed with execution

                if (preconditionViolated)
                {
                    ret              = null;
                    exceptionThrown  = null;
                    contractViolated = false;
                    return(false);
                }
            }

            if (forbidNull)
            {
                foreach (object o in objects)
                {
                    Util.Assert(o != null);
                }
            }

            object newObject = null;

            CodeExecutor.CodeToExecute call =
                delegate() { newObject = constructor.Invoke(objects); };

            executionLog.WriteLine("execute constructor " + this.constructor.DeclaringType);
            debugLog.WriteLine("execute constructor " + this.constructor.DeclaringType); //[email protected] adds
            executionLog.Flush();

            timesExecuted++;
            bool retval = true;

            if (!CodeExecutor.ExecuteReflectionCall(call, debugLog, out exceptionThrown))
            {
                ret = null;


                if (exceptionThrown is AccessViolationException)
                {
                    //Logger.Debug("SECOND CHANCE AV!" + this.ToString());
                    //Logging.LogLine(Logging.GENERAL, "SECOND CHANCE AV!" + this.ToString());
                }

                //for exns we can ony add the class to faulty classes when its a guideline violation
                if (Util.GuidelineViolation(exceptionThrown.GetType()))
                {
                    PlanManager.numDistinctContractViolPlans++;

                    KeyValuePair <MethodBase, Type> k = new KeyValuePair <MethodBase, Type>(this.constructor, exceptionThrown.GetType());
                    if (!exnViolatingMethods.ContainsKey(k))
                    {
                        PlanManager.numContractViolatingPlans++;
                        exnViolatingMethods[k] = true;
                    }

                    //add this class to the faulty classes
                    contractExnViolatingClasses[constructor.GetType()] = true;
                    contractExnViolatingMethods[constructor]           = true;
                }

                executionLog.WriteLine("execution failure."); //[email protected] adds
                return(false);
            }
            else
            {
                ret = new ResultTuple(constructor, newObject, objects);
            }


            //check if the objects in the output tuple violated basic contracts
            if (ret != null)
            {
                foreach (object o in ret.tuple)
                {
                    if (o == null)
                    {
                        continue;
                    }

                    bool toStrViol, hashCodeViol, equalsViol;
                    int  count;
                    if (Util.ViolatesContracts(o, out count, out toStrViol, out hashCodeViol, out equalsViol))
                    {
                        contractViolated = true;
                        contractExnViolatingMethods[constructor] = true;

                        bool newcontractViolation = false;
                        PlanManager.numDistinctContractViolPlans++;

                        if (toStrViol)
                        {
                            if (!toStrViolatingMethods.ContainsKey(constructor))
                            {
                                newcontractViolation = true;
                            }
                            toStrViolatingMethods[constructor] = true;
                        }
                        if (hashCodeViol)
                        {
                            if (!hashCodeViolatingMethods.ContainsKey(constructor))
                            {
                                newcontractViolation = true;
                            }

                            hashCodeViolatingMethods[constructor] = true;
                        }
                        if (equalsViol)
                        {
                            if (!equalsViolatingMethods.ContainsKey(constructor))
                            {
                                newcontractViolation = true;
                            }

                            equalsViolatingMethods[constructor] = true;
                        }

                        if (newcontractViolation)
                        {
                            PlanManager.numContractViolatingPlans++;
                        }

                        //add this class to the faulty classes
                        contractExnViolatingClasses[constructor.DeclaringType] = true;

                        retval = false;
                    }
                }

                contractStates = new RandoopContractsManager().ValidateAssertionContracts(constructor, newObject);
            }

            long endTime = 0;

            Timer.QueryPerformanceCounter(ref endTime);
            executionTimeAccum += ((double)(endTime - startTime)) / ((double)(Timer.PerfTimerFrequency));

            if (contractViolated)                              //[email protected] adds
            {
                executionLog.WriteLine("contract violation."); //[email protected] adds
            }
            return(retval);
        }