Exemplo n.º 1
0
        /// <summary>
        /// Invokes the code that this plan represents.
        /// </summary>
        /// <param name="executionResults"></param>
        /// <returns></returns>
        public bool Execute(out ResultTuple executionResult, TextWriter executionLog,
                            TextWriter debugLog, out bool preconditionViolated, out Exception exceptionThrown,
                            out bool contractViolated, bool forbidNull, bool useRandoopContracts)
        {
            numTimesExecuted++;
            long startTime = 0;

            Timer.QueryPerformanceCounter(ref startTime);

            // Execute parent plans
            ResultTuple[] results1 = new ResultTuple[parentPlans.Length];
            for (int i = 0; i < parentPlans.Length; i++)
            {
                Plan        plan = parentPlans[i];
                ResultTuple tempResults;
                if (!plan.ExecuteHelper(out tempResults, executionLog, debugLog, out preconditionViolated, out exceptionThrown, out contractViolated, forbidNull, useRandoopContracts, out CanGenerateContractAssertion))
                {
                    executionResult = null;
                    return(false);
                }
                results1[i] = tempResults;
            }

            // Execute.
            if (!transformer.Execute(out executionResult, results1, parameterChoosers, executionLog, debugLog,
                                     out preconditionViolated, out exceptionThrown, out contractViolated, forbidNull, useRandoopContracts, out CanGenerateContractAssertion))
            {
                executionResult = null;
                return(false);
            }

            RecordExecutionTime(startTime);
            return(true);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Invokes the code that this plan represents.
        /// TODO: what's the difference between this and the other Execute method?
        /// </summary>
        /// <param name="executionResults"></param>
        /// <returns></returns>
        public bool ExecuteHelper(out ResultTuple executionResult, TextWriter executionLog,
                                  TextWriter debugLog, out bool preconditionViolated, out Exception exceptionThrown, out bool contractViolated,
                                  bool forbidNull, bool useRandoopContracts, out ContractState contractStates)
        {
            // Execute parent plans
            ResultTuple[] results1 = new ResultTuple[parentPlans.Length];
            for (int i = 0; i < parentPlans.Length; i++)
            {
                Plan        plan = parentPlans[i];
                ResultTuple tempResults;
                if (!plan.ExecuteHelper(out tempResults, executionLog, debugLog, out preconditionViolated, out exceptionThrown, out contractViolated, forbidNull, useRandoopContracts, out contractStates))
                {
                    executionResult = null;
                    return(false);
                }
                results1[i] = tempResults;
            }

            //// Execute
            if (!transformer.Execute(out executionResult, results1, parameterChoosers, executionLog, debugLog,
                                     out preconditionViolated, out exceptionThrown, out contractViolated, forbidNull, useRandoopContracts, out contractStates))
            {
                executionResult = null;
                return(false);
            }

            return(true);
        }
Exemplo n.º 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);
        //}

        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)
        {
            contractViolated     = false;
            preconditionViolated = false;
            contractStates       = new ContractState();

            ArrayList a = new ArrayList();

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                {
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);
                }

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
            }

            exceptionThrown = null;
            ret             = new ResultTuple(this, a);
            executionLog.WriteLine("execute arraylistconstructor type " + a.GetType().ToString());//[email protected] adds
            return(true);
        }
Exemplo n.º 4
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);
        //}

        public override bool Execute(out ResultTuple ret, ResultTuple[] parameters,
                                     Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            contractViolated = false;

            Array array;

            try
            {
                array = Array.CreateInstance(baseType, length);
            }
            catch (Exception e)
            {
                ret             = null;
                exceptionThrown = e;
                return(false);
            }

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                {
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);
                }

                array.SetValue(parameters[pair.planIndex].tuple[pair.resultIndex], i);
            }

            ret = new ResultTuple(this, array);
            executionLog.WriteLine("create array (constructor): type " + array.GetType().ToString()); //[email protected] adds
            exceptionThrown = null;
            return(true);
        }
Exemplo n.º 5
0
        internal void AddPlan(Plan p, ResultTuple resultTuple)
        {
            if (p == null) throw new ArgumentNullException();
            if (resultTuple == null) throw new ArgumentNullException();

            AddplanInternal(p, resultTuple);
        }
Exemplo n.º 6
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);
        }
Exemplo n.º 7
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)
        {
            contractViolated = false;

            //List<int> a = new List<int>();
            Type  genericListType = typeof(List <>).MakeGenericType(baseType);
            IList a = (IList)Activator.CreateInstance(genericListType);

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                {
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);
                }

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
                //a.Add((int)(parameters[pair.planIndex].tuple[pair.resultIndex]));
            }

            exceptionThrown = null;
            ret             = new ResultTuple(this, a);
            executionLog.WriteLine("execute listconstructor type " + a.GetType().ToString());//[email protected] adds
            return(true);
        }
Exemplo n.º 8
0
        private void CheckContracts(ResultTuple ret, ref bool contractViolated, ref bool retval)
        {
            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;
                }
            }
        }
Exemplo n.º 9
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;
     exceptionThrown  = null;
     ret = new ResultTuple(ftype, new object[] { null });
     return(true);
 }
Exemplo n.º 10
0
 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();
     exceptionThrown      = null;
     ret = new ResultTuple(ftype, new object[] { null });
     executionLog.WriteLine("execute dummy action ");//[email protected] adds
     return(true);
 }
Exemplo n.º 11
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;

            //if (forbidNull)
            //    Util.Assert(fvalue != null);

            exceptionThrown = null;
            ret             = new ResultTuple(ftype, new object[] { fvalue });
            executionLog.WriteLine("execute primitive value type " + ftype.ToString()); //[email protected] adds
            return(true);
        }
Exemplo n.º 12
0
        internal void AddPlan(Plan p, ResultTuple resultTuple)
        {
            if (p == null)
            {
                throw new ArgumentNullException();
            }
            if (resultTuple == null)
            {
                throw new ArgumentNullException();
            }

            AddplanInternal(p, resultTuple);
        }
Exemplo n.º 13
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)
        {
            contractViolated = false;

            ArrayList a = new ArrayList();

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                {
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);
                }

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
            }

            exceptionThrown = null;
            ret             = new ResultTuple(this, a);
            return(true);
        }
Exemplo n.º 14
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;
     exceptionThrown = null;
     ret = new ResultTuple(ftype, new object[] { null });
     return true;
 }
Exemplo n.º 15
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);
        }
Exemplo n.º 16
0
 /// <summary>
 /// Returns false when the execution fails due to exception
 /// Returns true even when some contracts are violated
 /// </summary>
 /// <param name="ret"></param>
 /// <param name="results"></param>
 /// <param name="parameterMap"></param>
 /// <param name="writer"></param>
 /// <param name="exceptionThrown"></param>
 /// <param name="contractViolated"></param>
 /// <param name="forbidNull"></param>
 /// <returns></returns>
 public abstract bool Execute(out ResultTuple ret,
                              ResultTuple[] results, Plan.ParameterChooser[] parameterMap, TextWriter executionLog,
                              TextWriter writer, out Exception exceptionThrown, out bool contractViolated, bool forbidNull);
Exemplo n.º 17
0
        // resultTuple can be null.
        private bool AddplanInternal(Plan p, ResultTuple resultTuple)
        {
            totalAttemptedAdditions++;
            bool addSuccessful = false;

            if (planSet.ContainsKey(p))
            {
                totalFailedAdditions++;

                return false;
            }
            else
            {
                planSet[p] = true;
                NumPlans++;

                Dictionary<Type, bool> processedTypes = new Dictionary<Type, bool>();

                for (int i = 0; i < p.transformer.TupleTypes.Length; i++)
                {
                    if (!p.IsActiveTupleElement(i))
                    {
                        continue;
                    }

                    Type t = p.transformer.TupleTypes[i];

                    Collection<Plan> l = null;
                    if (plansByType.ContainsKey(t))
                    {
                        l = plansByType[t];
                    }
                    else
                    {
                        l = new Collection<Plan>();

                        // Always add 0 to the list of plans for a type.
                        bool success;
                        Plan pl = DefaultPlan(t, out success);
                        if (success)
                        {
                            l.Add(pl);
                            typeMap.AddTypeWithPlans(t);
                        }
                        plansByType[t] = l;
                    }

                    //we don't allow chars because the it can create unreadable chars
                    if (/*t.IsValueType && */t.IsPrimitive && !t.ToString().StartsWith("System.Char"))
                    {
                        if (p.transformer is PrimitiveValueTransformer)
                        {
                            l.Add(p);
                            typeMap.AddTypeWithPlans(t);
                        }
                        else
                        {
                            if (resultTuple != null)
                            {
                                object pValue = resultTuple.tuple[i];
                                Util.Assert(pValue != null && pValue.GetType().Equals(t));

                                bool alreadyInDB = false;
                                foreach (Plan existingPlan in l)
                                {
                                    Util.Assert(existingPlan.transformer is PrimitiveValueTransformer);
                                    object existingPlanValue = (existingPlan.transformer as PrimitiveValueTransformer).fvalue;
                                    Util.Assert(existingPlanValue != null
                                            && existingPlanValue.GetType().Equals(pValue.GetType()));
                                    if (pValue.Equals(existingPlanValue))
                                        alreadyInDB = true;
                                }
                                if (!alreadyInDB)
                                {
                                    Plan constantPlan = Plan.Constant(t, pValue);
                                    l.Add(constantPlan);
                                    typeMap.AddTypeWithPlans(t);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (processedTypes.ContainsKey(t))
                            continue;

                        processedTypes[t] = true;
                        bool success;
                        Plan pl = DefaultPlan(t, out success);
                        // Util.Assert(l.Contains(p) && success ? p.Equals(pl) : true);
                        typeMap.AddTypeWithPlans(t);
                        l.Add(p);
                    }
                }

                addSuccessful = true;
            }

            return addSuccessful;
        }
Exemplo n.º 18
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);
        }
Exemplo n.º 19
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;
        }
Exemplo n.º 20
0
        // resultTuple can be null.
        private bool AddplanInternal(Plan p, ResultTuple resultTuple)
        {
            totalAttemptedAdditions++;
            bool addSuccessful = false;

            if (planSet.ContainsKey(p))
            {
                totalFailedAdditions++;

                return(false);
            }
            else
            {
                planSet[p] = true;
                NumPlans++;

                Dictionary <Type, bool> processedTypes = new Dictionary <Type, bool>();

                for (int i = 0; i < p.transformer.TupleTypes.Length; i++)
                {
                    if (!p.IsActiveTupleElement(i))
                    {
                        continue;
                    }

                    Type t = p.transformer.TupleTypes[i];

                    Collection <Plan> l = null;
                    if (plansByType.ContainsKey(t))
                    {
                        l = plansByType[t];
                    }
                    else
                    {
                        l = new Collection <Plan>();

                        // Always add 0 to the list of plans for a type.
                        bool success;
                        Plan pl = DefaultPlan(t, out success);
                        if (success)
                        {
                            l.Add(pl);
                            typeMap.AddTypeWithPlans(t);
                        }
                        plansByType[t] = l;
                    }

                    //we don't allow chars because the it can create unreadable chars
                    if (/*t.IsValueType && */ t.IsPrimitive && !t.ToString().StartsWith("System.Char"))
                    {
                        if (p.transformer is PrimitiveValueTransformer)
                        {
                            l.Add(p);
                            typeMap.AddTypeWithPlans(t);
                        }
                        else
                        {
                            if (resultTuple != null)
                            {
                                object pValue = resultTuple.tuple[i];
                                Util.Assert(pValue != null && pValue.GetType().Equals(t));

                                bool alreadyInDB = false;
                                foreach (Plan existingPlan in l)
                                {
                                    Util.Assert(existingPlan.transformer is PrimitiveValueTransformer);
                                    object existingPlanValue = (existingPlan.transformer as PrimitiveValueTransformer).fvalue;
                                    Util.Assert(existingPlanValue != null &&
                                                existingPlanValue.GetType().Equals(pValue.GetType()));
                                    if (pValue.Equals(existingPlanValue))
                                    {
                                        alreadyInDB = true;
                                    }
                                }
                                if (!alreadyInDB)
                                {
                                    Plan constantPlan = Plan.Constant(t, pValue);
                                    l.Add(constantPlan);
                                    typeMap.AddTypeWithPlans(t);
                                }
                            }
                        }
                    }
                    else
                    {
                        if (processedTypes.ContainsKey(t))
                        {
                            continue;
                        }

                        processedTypes[t] = true;
                        bool success;
                        Plan pl = DefaultPlan(t, out success);
                        // Util.Assert(l.Contains(p) && success ? p.Equals(pl) : true);
                        typeMap.AddTypeWithPlans(t);
                        l.Add(p);
                    }
                }

                addSuccessful = true;
            }

            return(addSuccessful);
        }
Exemplo n.º 21
0
        /// <summary>
        /// Invokes the code that this plan represents.
        /// </summary>
        /// <param name="executionResults"></param>
        /// <returns></returns>
        public bool Execute(out ResultTuple executionResult, TextWriter executionLog,
            TextWriter debugLog, out Exception exceptionThrown,
            out bool contractViolated, bool forbidNull, bool monkey)
        {
            this.numTimesExecuted++;
            long startTime = 0;
            Timer.QueryPerformanceCounter(ref startTime);

            // Execute parent plans
            ResultTuple[] results1 = new ResultTuple[parentPlans.Length];
            for (int i = 0; i < parentPlans.Length; i++)
            {
                Plan plan = parentPlans[i];
                ResultTuple tempResults;
                if (!plan.ExecuteHelper(out tempResults, executionLog, debugLog, out exceptionThrown, out contractViolated, forbidNull))
                {
                    executionResult = null;
                    return false;
                }
                results1[i] = tempResults;
            }

            // Execute.
            if (!transformer.Execute(out executionResult, results1, parameterChoosers, executionLog, debugLog,
                out exceptionThrown, out contractViolated, forbidNull))
            {
                executionResult = null;
                return false;
            }

            RecordExecutionTime(startTime);
            return true;
        }
Exemplo n.º 22
0
        /// <summary>
        /// Invokes the code that this plan represents.
        /// TODO: what's the difference between this and the other Execute method?
        /// </summary>
        /// <param name="executionResults"></param>
        /// <returns></returns>
        public bool ExecuteHelper(out ResultTuple executionResult, TextWriter executionLog,
            TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated,
            bool forbidNull)
        {


            // Execute parent plans
            ResultTuple[] results1 = new ResultTuple[parentPlans.Length];
            for (int i = 0; i < parentPlans.Length; i++)
            {
                Plan plan = parentPlans[i];
                ResultTuple tempResults;
                if (!plan.ExecuteHelper(out tempResults, executionLog, debugLog, out exceptionThrown, out contractViolated, forbidNull))
                {
                    executionResult = null;
                    return false;
                }
                results1[i] = tempResults;
            }

            //// Execute
            if (!transformer.Execute(out executionResult, results1, parameterChoosers, executionLog, debugLog,
                out exceptionThrown, out contractViolated, forbidNull))
            {
                executionResult = null;
                return false;
            }

            return true;
        }
Exemplo n.º 23
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 " + this.method.Name
                + "[" + (this.timesExecuted - 1).ToString() + "] the second time"); 
            Console.WriteLine("execute method " + this.method.Name 
                + "[" + (this.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>(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;
                executionLog.WriteLine("return value [" + (this.timesExecuted - 1).ToString()
                    + "]: invocationOk is false the sceond time --- shouldn't happen?");

                //this.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 [" + (this.timesExecuted - 1).ToString() + "] the second time: "
                        + returnValue.ToString().Replace("\n", "\\n").Replace("\r", "\\r"));
                    else
                        executionLog.WriteLine("return value [" + (this.timesExecuted - 1).ToString() + "] the second time: not primitive or string");

                    //this.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 [" + (this.timesExecuted - 1).ToString()
                        + "]: no return value the second time -- shouldn't happen?");

                    //this.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;
        }
Exemplo n.º 24
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);
        //}


        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);
            debugLog.WriteLine("set field " + ffield.Name); //[email protected] adds
            executionLog.Flush();

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

            ret = new ResultTuple(ffield, receiver);
            return true;
        }
Exemplo n.º 25
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)
        {
            contractViolated = false;

            ArrayList a = new ArrayList();

            for (int i = 0; i < length; i++)
            {

                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
            }

            exceptionThrown = null;
            ret = new ResultTuple(this, a);
            return true;
        }
Exemplo n.º 26
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);
        }
Exemplo n.º 27
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;
        }
Exemplo n.º 28
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;
        }
Exemplo n.º 29
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;

            //if (forbidNull)
            //    Util.Assert(fvalue != null);

            exceptionThrown = null;
            ret = new ResultTuple(ftype, new object[] { fvalue });
            executionLog.WriteLine("execute primitive value type " + ftype.ToString()); //[email protected] adds
            return true;
        }
Exemplo n.º 30
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);
        //}

        public override bool Execute(out ResultTuple ret, ResultTuple[] parameters,
            Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            contractViolated = false;

            ArrayList a = new ArrayList();

            for (int i = 0; i < length; i++)
            {

                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
            }

            exceptionThrown = null;
            ret = new ResultTuple(this, a);
            executionLog.WriteLine("execute arraylistconstructor type " + a.GetType().ToString());//[email protected] adds
            return true;
        }
Exemplo n.º 31
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)
        {
            contractViolated = false;

            //List<int> a = new List<int>();
            Type genericListType = typeof(List<>).MakeGenericType(baseType);
            IList a = (IList)Activator.CreateInstance(genericListType);             

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);

                a.Add(parameters[pair.planIndex].tuple[pair.resultIndex]);
                //a.Add((int)(parameters[pair.planIndex].tuple[pair.resultIndex]));
            }

            exceptionThrown = null;
            ret = new ResultTuple(this, a);
            executionLog.WriteLine("execute listconstructor type " + a.GetType().ToString());//[email protected] adds
            return true;
        }
Exemplo n.º 32
0
 /// <summary>
 /// Returns false when the execution fails due to exception
 /// Returns true even when some contracts are violated
 /// </summary>
 /// <param name="ret"></param>
 /// <param name="results"></param>
 /// <param name="parameterMap"></param>
 /// <param name="writer"></param>
 /// <param name="exceptionThrown"></param>
 /// <param name="contractViolated"></param>
 /// <param name="forbidNull"></param>
 /// <returns></returns>
 public abstract bool Execute(out ResultTuple ret,
                              ResultTuple[] results, Plan.ParameterChooser[] parameterMap, TextWriter executionLog,
                              TextWriter writer, out bool preconditionViolated, out Exception exceptionThrown, out bool contractViolated, bool forbidNull, bool useRandoopContractsout, out ContractState contractStates);
Exemplo n.º 33
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);
        //}

        public override bool Execute(out ResultTuple ret, ResultTuple[] parameters,
            Plan.ParameterChooser[] parameterMap, TextWriter executionLog, TextWriter debugLog, out Exception exceptionThrown, out bool contractViolated, bool forbidNull)
        {
            contractViolated = false;

            Array array;

            try
            {
                array = Array.CreateInstance(baseType, length);
            }
            catch (Exception e)
            {
                ret = null;
                exceptionThrown = e;
                return false;
            }

            for (int i = 0; i < length; i++)
            {
                Plan.ParameterChooser pair = parameterMap[i];

                if (forbidNull)
                    Util.Assert(parameters[pair.planIndex].tuple[pair.resultIndex] != null);

                array.SetValue(parameters[pair.planIndex].tuple[pair.resultIndex], i);
            }

            ret = new ResultTuple(this, array);
            executionLog.WriteLine("create array (constructor): type " + array.GetType().ToString()); //[email protected] adds
            exceptionThrown = null;
            return true;
        }
Exemplo n.º 34
0
 /// <summary>
 /// Returns false when the execution fails due to exception
 /// Returns true even when some contracts are violated
 /// </summary>
 /// <param name="ret"></param>
 /// <param name="results"></param>
 /// <param name="parameterMap"></param>
 /// <param name="writer"></param>
 /// <param name="exceptionThrown"></param>
 /// <param name="contractViolated"></param>
 /// <param name="forbidNull"></param>
 /// <returns></returns>
 public abstract bool Execute(out ResultTuple ret,
     ResultTuple[] results, Plan.ParameterChooser[] parameterMap, TextWriter executionLog,
     TextWriter writer, out Exception exceptionThrown, out bool contractViolated, bool forbidNull);
Exemplo n.º 35
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 
                + "[" + (this.timesExecuted-1).ToString() + "]"); //[email protected] changes
            Console.WriteLine("execute method " + this.method.Name //[email protected] adds
                + "[" + (this.timesExecuted - 1).ToString() + "]");

            executionLog.Flush();

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

            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;
                executionLog.WriteLine("return value [" + (this.timesExecuted-1).ToString() 
                    + "]: invocationOk is false.");//[email protected] adds

                //string temp = "RANDOOPFAIL"; //[email protected] adds for capture current status
                this.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(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 [" + (this.timesExecuted-1).ToString() + "]: " 
                        + returnValue.ToString().Replace("\n", "\\n").Replace("\r", "\\r"));
                    else
                        executionLog.WriteLine("return value [" + (this.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))
                        this.ReturnValue.Add(returnValue);
                    else
                        this.ReturnValue.Add(null);
                }
                else
                {
                    executionLog.WriteLine("return value [" + (this.timesExecuted-1).ToString()
                        +"]: no return value");

                    this.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)
            {
                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)  //[email protected] adds
                executionLog.WriteLine("contract violation."); //[email protected] adds

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

            return retval;
        }