Ejemplo n.º 1
0
        public override bool Execute(out ResultTuple ret, ResultTuple[] results,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            contractViolated = false;

            Util.Assert(parameterMap.Length == 2);

            object receiver = results[parameterMap[0].planIndex].tuple[parameterMap[0].resultIndex];
            object val      = results[parameterMap[1].planIndex].tuple[parameterMap[1].resultIndex];

            if (!this.coverageInfo.IsStatic)
            {
                Util.Assert(receiver != null);
            }

            if (forbidNull)
            {
                Util.Assert(val != null);
            }

            CodeExecutor.CodeToExecute call = delegate() { ffield.SetValue(receiver, val); };

            executionLog.WriteLine("set field " + ffield.Name);
            executionLog.Flush();

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

            ret = new ResultTuple(ffield, receiver);
            return(true);
        }
Ejemplo n.º 2
0
        public override bool Execute(out ResultTuple ret, ResultTuple[] results,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            contractViolated = false;

            this.timesExecuted++;
            long startTime = 0;

            Timer.QueryPerformanceCounter(ref startTime);

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

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

            object newObject = null;

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

            executionLog.WriteLine("execute constructor " + this.fconstructor.DeclaringType);
            executionLog.Flush();

            bool retval = true;

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

                if (exceptionThrown is AccessViolationException)
                {
                    //Console.WriteLine("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.fconstructor, exceptionThrown.GetType());
                    if (!exnViolatingMethods.ContainsKey(k))
                    {
                        PlanManager.numContractViolatingPlans++;
                        exnViolatingMethods[k] = true;
                    }

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


                return(false);
            }
            else
            {
                ret = new ResultTuple(fconstructor, 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[fconstructor] = true;

                        bool newcontractViolation = false;
                        PlanManager.numDistinctContractViolPlans++;

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

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

                            equalsViolatingMethods[fconstructor] = true;
                        }

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

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

                        retval = false;
                    }
                }
            }

            long endTime = 0;

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

            return(retval);
        }
Ejemplo n.º 3
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);
        }
Ejemplo n.º 4
0
        public bool Execute2(object retValOldRun, object[] objectsOld, object receiverOld,
                             TextWriter executionLog, TextWriter debugLog)
        {
            //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];
            //}

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

            object[]    objects  = objectsOld;
            object      receiver = receiverOld;
            ResultTuple ret;
            Exception   exceptionThrown = new Exception();

            CodeExecutor.CodeToExecute call;

            object returnValue = null;

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

            //bool retval = true;

            executionLog.WriteLine("execute method " + method.Name
                                   + "[" + (timesExecuted - 1).ToString() + "] the second time");
            Logger.Debug("execute method " + method.Name
                         + "[" + (timesExecuted - 1).ToString() + "] the second time");

            executionLog.Flush();


            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 the sceond time --- shouldn't happen?");

                //ReturnValue.Add(null);

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

                ////[email protected] adds to capture return value -- start////
                if (returnValue != null)
                {
                    if ((returnValue.GetType() == typeof(System.String)) ||
                        (returnValue.GetType() == typeof(System.Boolean)) ||
                        (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() + "] the second time: "
                                               + returnValue.ToString().Replace("\n", "\\n").Replace("\r", "\\r"));
                    }
                    else
                    {
                        executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString() + "] the second time: not primitive or string");
                    }

                    //ReturnValue.Add(returnValue);
                    Type typeOfReturnVal = returnValue.GetType();
                    if (typeOfReturnVal == typeof(bool) || typeOfReturnVal == typeof(byte) || typeOfReturnVal == typeof(short) ||
                        typeOfReturnVal == typeof(int) || typeOfReturnVal == typeof(long) || typeOfReturnVal == typeof(float) ||
                        typeOfReturnVal == typeof(double) || typeOfReturnVal == typeof(char) || typeOfReturnVal == typeof(string))
                    {
                        if (returnValue.Equals(retValOldRun))
                        {
                            return(true);
                        }
                        else
                        {
                            return(false);
                        }
                    }

                    return(true); //other types just assert NOTNULL -- should be ok
                }
                else
                {
                    executionLog.WriteLine("return value [" + (timesExecuted - 1).ToString()
                                           + "]: no return value the second time -- shouldn't happen?");

                    //ReturnValue.Add(null);
                    return(false);
                }
                ////[email protected] adds to capture return value -- end////
            }

            //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[method] = true;

            //            PlanManager.numDistinctContractViolPlans++;

            //            bool newcontractViolation = false;

            //            if (toStrViol)
            //            {

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

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

            //                equalsViolatingMethods[method] = true;
            //            }

            //            if (newcontractViolation)
            //                PlanManager.numContractViolatingPlans++;

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

            //            retval = false;
            //        }

            //    }
            //}

            //if (contractViolated)
            //    executionLog.WriteLine("contract violation.");

            //return retval;
        }
Ejemplo n.º 5
0
        public override bool Execute(out ResultTuple ret, ResultTuple[] parameters,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            this.timesExecuted++;
            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];
            }

            // FIXME This should be true! It currently isn't.
            //if (!this.coverageInfo.methodInfo.IsStatic)
            //    Util.Assert(receiver != null);

            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 " + this.method.Name);
            executionLog.Flush();

            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>(this.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;
                return(false);
            }
            else
            {
                ret = new ResultTuple(method, receiver, returnValue, 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[method] = true;

                        PlanManager.numDistinctContractViolPlans++;

                        bool newcontractViolation = false;

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

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

                            equalsViolatingMethods[method] = true;
                        }

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

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

                        retval = false;
                    }
                }
            }



            long endTime = 0;

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

            return(retval);
        }