Beispiel #1
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);
        }
        ////[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);
        }
Beispiel #3
0
            private void HandleMethod(Plan p, Collection <int> path)
            {
                MethodCall mct      = p.transformer as MethodCall;
                string     freshvar = null;

                if (!mct.method.ReturnType.Equals(typeof(void)))
                {
                    OutPortId newObjPort = new OutPortId(path, 1);
                    freshvar = nameManager.foobar.Get(newObjPort);
                }


                Collection <string> arguments = new Collection <string>();

                for (int i = 0; i < p.parameterChoosers.Length; i++)
                {
                    if (i == 0 && mct.method.IsStatic)
                    {
                        arguments.Add(null);
                        continue;
                    }
                    Plan.ParameterChooser c = p.parameterChoosers[i];
                    path.Add(c.planIndex);
                    OutPortId paramPort = new OutPortId(path, c.resultIndex);
                    path.RemoveAt(path.Count - 1);
                    arguments.Add(nameManager.foobar.Get(paramPort));
                }
                b.Add(p.transformer.ToCSharpCode(new ReadOnlyCollection <string>(arguments), freshvar));
            }
Beispiel #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 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);
        }
Beispiel #5
0
            private void MapOutputPortsMethod(Plan p, Collection <int> path, string freshvar)
            {
                // Map receiver.
                Plan.ParameterChooser c           = p.parameterChoosers[0];
                OutPortId             pOutputPort = new OutPortId(path, 0);

                path.Add(c.planIndex);
                OutPortId recPort = new OutPortId(path, c.resultIndex);

                path.RemoveAt(path.Count - 1);
                foobar.set(pOutputPort, foobar.Get(recPort));

                // Map return value.
                MethodCall mct = p.transformer as MethodCall;

                if (!mct.method.ReturnType.Equals(typeof(void)))
                {
                    OutPortId newObjPort = new OutPortId(path, 1);
                    foobar.set(newObjPort, freshvar);
                }

                // Map mutated parameters.
                // Start from 1 because already handled receiver.
                for (int i = 1; i < p.parameterChoosers.Length; i++)
                {
                    c           = p.parameterChoosers[i];
                    pOutputPort = new OutPortId(path, i + 1); // Add 1 because of retval.
                    path.Add(c.planIndex);
                    OutPortId paramPort = new OutPortId(path, c.resultIndex);
                    path.RemoveAt(path.Count - 1);
                    foobar.set(pOutputPort, foobar.Get(paramPort));
                }
            }
Beispiel #6
0
            private void MapOutputPortsFieldSetter(Plan p, Collection <int> path, string freshvar)
            {
                // Map receiver.
                Plan.ParameterChooser c           = p.parameterChoosers[0];
                OutPortId             pOutputPort = new OutPortId(path, 0);

                path.Add(c.planIndex);
                OutPortId recPort = new OutPortId(path, c.resultIndex);

                path.RemoveAt(path.Count - 1);
                foobar.set(pOutputPort, foobar.Get(recPort));
            }
Beispiel #7
0
            private void HandleField(Plan p, Collection <int> path)
            {
                Collection <string> args = new Collection <string>();

                for (int i = 0; i < p.parameterChoosers.Length; i++)
                {
                    Plan.ParameterChooser c = p.parameterChoosers[i];
                    path.Add(c.planIndex);
                    OutPortId paramPort = new OutPortId(path, c.resultIndex);
                    path.RemoveAt(path.Count - 1);
                    args.Add(nameManager.foobar.Get(paramPort));
                }
                b.Add(p.transformer.ToCSharpCode(new ReadOnlyCollection <string>(args), "dummy"));
            }
Beispiel #8
0
            private void HandleArray(Plan p, Collection <int> path)
            {
                OutPortId newObjPort = new OutPortId(path, 0);
                string    newObjName = nameManager.foobar.Get(newObjPort);

                Collection <string> args = new Collection <string>();

                for (int i = 0; i < p.parameterChoosers.Length; i++)
                {
                    Plan.ParameterChooser c = p.parameterChoosers[i];
                    path.Add(c.planIndex);
                    OutPortId paramPort = new OutPortId(path, c.resultIndex);
                    path.RemoveAt(path.Count - 1);
                    args.Add(nameManager.foobar.Get(paramPort));
                }
                b.Add(p.transformer.ToCSharpCode(new ReadOnlyCollection <string>(args), nameManager.foobar.Get(newObjPort), useRandoopContracts, p.CanGenerateContractAssertion));
            }
Beispiel #9
0
            private void MapOutputPortsConstructor(Plan p, Collection <int> path, string freshvar)
            {
                // Map new object.
                OutPortId newObjPort = new OutPortId(path, 0);

                foobar.set(newObjPort, freshvar);

                // Map mutated parameters.
                for (int i = 0; i < p.parameterChoosers.Length; i++)
                {
                    Plan.ParameterChooser c           = p.parameterChoosers[i];
                    OutPortId             pOutputPort = new OutPortId(path, i + 1);
                    path.Add(c.planIndex);
                    OutPortId paramPort = new OutPortId(path, c.resultIndex);
                    path.RemoveAt(path.Count - 1);
                    foobar.set(pOutputPort, foobar.Get(paramPort));
                }
            }
        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);
        }
Beispiel #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;

            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);
        }
Beispiel #12
0
        /// <summary>
        /// Selects random plans (from planDB) that yield results specified
        /// in typesDesired. Only consider plans for which filter returns true.
        /// </summary>
        private bool RandomPlans(out RandomPlansResult result, Type[] typesDesired, PlanFilter filter,
            PlanDataBase planDB, bool forbidNull, MethodInfo method)
        {
            List<Plan> plans = new List<Plan>();
            Plan.ParameterChooser[] parameterMap = new Plan.ParameterChooser[typesDesired.Length];

            for (int i = 0; i < typesDesired.Length; i++)
            {
                Type typeDesired = typesDesired[i];

                if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList)))
                {
                    int randArrayLength = Common.Enviroment.Random.Next(this.arrayMaxSize + 1);

                    Transformer t;
                    Type baseType = null;
                    if (typeDesired.IsArray)
                    {
                        baseType = typeDesired.GetElementType();
                        t = ArrayBuilderTransformer.Get(baseType, randArrayLength);
                    }
                    else
                    {
                        baseType = typeof(object);
                        t = ArrayListBuilderTransformer.Get(typeof(object), randArrayLength);
                    }

                    RandomPlansResult arrayResult;

                    PlanFilter f;

                    if (forbidNull)
                    {
                        f = delegate(Plan p, int x)
                        {
                            // Non-null heuristic
                            if (baseType.IsValueType && (p.transformer is PrimitiveValueTransformer)
                                && (p.transformer as PrimitiveValueTransformer).IsNull)
                                return false;

                            return true;
                        };
                    }
                    else
                        f = delegate(Plan dontCareP, int dontCareInt) { return true; };

                    if (!RandomPlans(out arrayResult, t.ParameterTypes, f, planDB, forbidNull, null))
                    {
                        //Util.Warning("\tNo array plans for type " + typeDesired.Name + " created");
                        result = null;
                        return false;
                    }

                    plans.Add(new Plan(t, arrayResult.fplans, arrayResult.fparameterChoosers));
                    parameterMap[i] = new Plan.ParameterChooser(i, 0);

                }
                else
                {
                    // Types for which default plans are not created
                    // Don't want to create a plan for a value type which is just a type defn.
                    if (typeDesired.IsValueType && typeDesired.IsGenericTypeDefinition)
                    {
                        result = null; return false;
                    }

                    //Array.createInstance can't create arrays of these types
                    if (typeDesired.Name.Equals("TypedReference") ||
                        typeDesired.Name.Equals("ArgIterator") ||
                        typeDesired.Name.Equals("ByRef") ||
                        typeDesired.Name.Equals("RuntimeArgumentHandle"))
                    {
                        result = null;
                        return false;
                    }

                    if (typeDesired.IsPointer)
                    {
                        result = null;
                        return false;
                    }

                    Plan chosenplan;
                    if (i == 0 && method != null && method.IsStatic)
                    {
                        chosenplan = new Plan(DummyTransformer.Get(typeDesired), new Plan[0], new Randoop.Plan.ParameterChooser[0]);
                    }
                    else
                    {

                        chosenplan = planDB.RandomPlan(typeDesired);
                        if (chosenplan == null)
                        {
                            // No plans for type typeDesired created.
                            result = null;
                            return false;
                        }

                        if (!filter(chosenplan, i))
                        {
                            result = null;
                            return false;
                        }

                        // If plan represents "null" return false.
                        if (forbidNull && (chosenplan.transformer is PrimitiveValueTransformer)
                                       && (chosenplan.transformer as PrimitiveValueTransformer).IsNull)
                        {
                            result = null;
                            return false;
                        }

                    }
                    plans.Add(chosenplan);

                    Collection<int> possibleResultIndices = PossibleResultTupleIndices(chosenplan, typeDesired);
                    if (possibleResultIndices.Count == 0)
                    {
                        result = null;
                        return false;
                    }

                    parameterMap[i] =
                        new Plan.ParameterChooser(i, possibleResultIndices[Common.Enviroment.Random.Next(possibleResultIndices.Count)]);

                }
            }

            result = new RandomPlansResult(plans.ToArray(), parameterMap);
            return true;
        }
Beispiel #13
0
        /// <summary>
        /// Selects random plans (from planDB) that yield results specified
        /// in typesDesired. Only consider plans for which filter returns true.
        /// </summary>
        private bool RandomPlans(out RandomPlansResult result, Type[] typesDesired, PlanFilter filter,
            PlanDataBase planDB, bool forbidNull, MethodInfo method)
        {
            List<Plan> plans = new List<Plan>();
            Plan.ParameterChooser[] parameterMap = new Plan.ParameterChooser[typesDesired.Length];                  

            for (int i = 0; i < typesDesired.Length; i++)
            {
                Type typeDesired = typesDesired[i];

                //if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList))) //[email protected] changes as below to add List input generation
                if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList)) || (!string.IsNullOrEmpty(typeDesired.FullName) && typeDesired.FullName.Contains("System.Collections.Generic.List")))
                {
                    int randArrayLength = Common.Enviroment.Random.Next(this.arrayMaxSize + 1);

                    Transformer t;
                    Type baseType = null;
                    if (typeDesired.IsArray)
                    {
                        baseType = typeDesired.GetElementType();
                        t = ArrayBuilderTransformer.Get(baseType, randArrayLength);
                    }
                    else //[email protected] addes -- start
                    {
                        if (typeDesired.FullName.Contains("System.Collections.Generic.List"))
                        {
                            //string fullType = typeDesired.ToString();
                            //string elementType = fullType.Substring(fullType.IndexOf('[') + 1, fullType.IndexOf(']') - fullType.IndexOf('[') - 1);
                            //baseType = System.Type.GetType(elementType);

                            baseType = typeDesired.GetProperty("Item").PropertyType;
                            
                            if(baseType == null)
                                baseType = typeof(object);

                            t = ListBuilderTransformer.Get(baseType, randArrayLength);
                             
                        }
                        //[email protected] addes -- end
                        else
                        {
                            baseType = typeof(object);
                            t = ArrayListBuilderTransformer.Get(typeof(object), randArrayLength);
                        }
                    }

                    RandomPlansResult arrayResult;

                    PlanFilter f;

                    if (forbidNull)
                    {
                        f = delegate(Plan p, int x)
                        {
                            // Non-null heuristic
                            if (baseType.IsValueType && (p.transformer is PrimitiveValueTransformer)
                                && (p.transformer as PrimitiveValueTransformer).IsNull)
                                return false;

                            return true;
                        };
                    }
                    else
                        f = delegate(Plan dontCareP, int dontCareInt) { return true; };

                    if (!RandomPlans(out arrayResult, t.ParameterTypes, f, planDB, forbidNull, null))
                    {
                        //Util.Warning("\tNo array plans for type " + typeDesired.Name + " created");
                        result = null;
                        return false;
                    }

                    plans.Add(new Plan(t, arrayResult.fplans, arrayResult.fparameterChoosers));
                    parameterMap[i] = new Plan.ParameterChooser(i, 0);

                }
                else
                {
                    // Types for which default plans are not created
                    // Don't want to create a plan for a value type which is just a type defn.
                    if (typeDesired.IsValueType && typeDesired.IsGenericTypeDefinition)
                    {
                        result = null; return false;
                    }

                    //Array.createInstance can't create arrays of these types
                    if (typeDesired.Name.Equals("TypedReference") ||
                        typeDesired.Name.Equals("ArgIterator") ||
                        typeDesired.Name.Equals("ByRef") ||
                        typeDesired.Name.Equals("RuntimeArgumentHandle"))
                    {
                        result = null;
                        return false;
                    }

                    if (typeDesired.IsPointer)
                    {
                        result = null;
                        return false;
                    }

                    Plan chosenplan;
                    if (i == 0 && method != null && method.IsStatic)
                    {
                        chosenplan = new Plan(DummyTransformer.Get(typeDesired), new Plan[0], new Randoop.Plan.ParameterChooser[0]);
                    }
                    else
                    {

                        chosenplan = planDB.RandomPlan(typeDesired);                        

                        if (chosenplan == null)
                        {
                            // No plans for type typeDesired created.
                            result = null;
                            return false;
                        }

                        //if ((method != null) && method.Name.Contains("heapSort") && !(chosenplan.transformer is PrimitiveValueTransformer)) //[email protected]
                        //    Console.WriteLine("debug.");

                        if (!filter(chosenplan, i))
                        {
                            result = null;
                            return false;
                        }

                        // If plan represents "null" return false.
                        if (forbidNull && (chosenplan.transformer is PrimitiveValueTransformer)
                                       && (chosenplan.transformer as PrimitiveValueTransformer).IsNull)
                        {
                            result = null;
                            return false;
                        }

                    }
                    plans.Add(chosenplan);

                    Collection<int> possibleResultIndices = PossibleResultTupleIndices(chosenplan, typeDesired);
                    if (possibleResultIndices.Count == 0)
                    {
                        result = null;
                        return false;
                    }

                    parameterMap[i] =
                        new Plan.ParameterChooser(i, possibleResultIndices[Common.Enviroment.Random.Next(possibleResultIndices.Count)]);

                }
            }

            result = new RandomPlansResult(plans.ToArray(), parameterMap);
            return true;
        }
Beispiel #14
0
        /// <summary>
        /// Selects random plans (from planDB) that yield results specified
        /// in typesDesired. Only consider plans for which filter returns true.
        /// </summary>
        private bool RandomPlans(out RandomPlansResult result, Type[] typesDesired, PlanFilter filter,
                                 PlanDataBase planDB, bool forbidNull, MethodInfo method)
        {
            List <Plan> plans = new List <Plan>();

            Plan.ParameterChooser[] parameterMap = new Plan.ParameterChooser[typesDesired.Length];

            for (int i = 0; i < typesDesired.Length; i++)
            {
                Type typeDesired = typesDesired[i];

                //if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList))) //[email protected] changes as below to add List input generation
                if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList)) || (!string.IsNullOrEmpty(typeDesired.FullName) && typeDesired.FullName.Contains("System.Collections.Generic.List")))
                {
                    int randArrayLength = Common.Enviroment.Random.Next(this.arrayMaxSize + 1);

                    Transformer t;
                    Type        baseType = null;
                    if (typeDesired.IsArray)
                    {
                        baseType = typeDesired.GetElementType();
                        t        = ArrayBuilderTransformer.Get(baseType, randArrayLength);
                    }
                    else //[email protected] addes -- start
                    {
                        if (typeDesired.FullName.Contains("System.Collections.Generic.List"))
                        {
                            //string fullType = typeDesired.ToString();
                            //string elementType = fullType.Substring(fullType.IndexOf('[') + 1, fullType.IndexOf(']') - fullType.IndexOf('[') - 1);
                            //baseType = System.Type.GetType(elementType);

                            baseType = typeDesired.GetProperty("Item").PropertyType;

                            if (baseType == null)
                            {
                                baseType = typeof(object);
                            }

                            t = ListBuilderTransformer.Get(baseType, randArrayLength);
                        }
                        //[email protected] addes -- end
                        else
                        {
                            baseType = typeof(object);
                            t        = ArrayListBuilderTransformer.Get(typeof(object), randArrayLength);
                        }
                    }

                    RandomPlansResult arrayResult;

                    PlanFilter f;

                    if (forbidNull)
                    {
                        f = delegate(Plan p, int x)
                        {
                            // Non-null heuristic
                            if (baseType.IsValueType && (p.transformer is PrimitiveValueTransformer) &&
                                (p.transformer as PrimitiveValueTransformer).IsNull)
                            {
                                return(false);
                            }

                            return(true);
                        };
                    }
                    else
                    {
                        f = delegate(Plan dontCareP, int dontCareInt) { return(true); }
                    };

                    if (!RandomPlans(out arrayResult, t.ParameterTypes, f, planDB, forbidNull, null))
                    {
                        //Util.Warning("\tNo array plans for type " + typeDesired.Name + " created");
                        result = null;
                        return(false);
                    }

                    plans.Add(new Plan(t, arrayResult.fplans, arrayResult.fparameterChoosers));
                    parameterMap[i] = new Plan.ParameterChooser(i, 0);
                }
                else
                {
                    // Types for which default plans are not created
                    // Don't want to create a plan for a value type which is just a type defn.
                    if (typeDesired.IsValueType && typeDesired.IsGenericTypeDefinition)
                    {
                        result = null; return(false);
                    }

                    //Array.createInstance can't create arrays of these types
                    if (typeDesired.Name.Equals("TypedReference") ||
                        typeDesired.Name.Equals("ArgIterator") ||
                        typeDesired.Name.Equals("ByRef") ||
                        typeDesired.Name.Equals("RuntimeArgumentHandle"))
                    {
                        result = null;
                        return(false);
                    }

                    if (typeDesired.IsPointer)
                    {
                        result = null;
                        return(false);
                    }

                    Plan chosenplan;
                    if (i == 0 && method != null && method.IsStatic)
                    {
                        chosenplan = new Plan(DummyTransformer.Get(typeDesired), new Plan[0], new Randoop.Plan.ParameterChooser[0]);
                    }
                    else
                    {
                        chosenplan = planDB.RandomPlan(typeDesired);

                        if (chosenplan == null)
                        {
                            // No plans for type typeDesired created.
                            result = null;
                            return(false);
                        }

                        //if ((method != null) && method.Name.Contains("heapSort") && !(chosenplan.transformer is PrimitiveValueTransformer)) //[email protected]
                        //    Logger.Debug("debug.");

                        if (!filter(chosenplan, i))
                        {
                            result = null;
                            return(false);
                        }

                        // If plan represents "null" return false.
                        if (forbidNull && (chosenplan.transformer is PrimitiveValueTransformer) &&
                            (chosenplan.transformer as PrimitiveValueTransformer).IsNull)
                        {
                            result = null;
                            return(false);
                        }
                    }
                    plans.Add(chosenplan);

                    Collection <int> possibleResultIndices = PossibleResultTupleIndices(chosenplan, typeDesired);
                    if (possibleResultIndices.Count == 0)
                    {
                        result = null;
                        return(false);
                    }

                    parameterMap[i] =
                        new Plan.ParameterChooser(i, possibleResultIndices[Enviroment.Random.Next(possibleResultIndices.Count)]);
                }
            }

            result = new RandomPlansResult(plans.ToArray(), parameterMap);
            return(true);
        }
Beispiel #15
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);
        }
Beispiel #16
0
        /// <summary>
        /// Selects random plans (from planDB) that yield results specified
        /// in typesDesired. Only consider plans for which filter returns true.
        /// </summary>
        private bool RandomPlans(out RandomPlansResult result, Type[] typesDesired, PlanFilter filter,
                                 PlanDataBase planDB, bool forbidNull, MethodInfo method)
        {
            List <Plan> plans = new List <Plan>();

            Plan.ParameterChooser[] parameterMap = new Plan.ParameterChooser[typesDesired.Length];

            for (int i = 0; i < typesDesired.Length; i++)
            {
                Type typeDesired = typesDesired[i];

                if (typeDesired.IsArray || typeDesired.Equals(typeof(ArrayList)))
                {
                    int randArrayLength = Common.Enviroment.Random.Next(this.arrayMaxSize + 1);

                    Transformer t;
                    Type        baseType = null;
                    if (typeDesired.IsArray)
                    {
                        baseType = typeDesired.GetElementType();
                        t        = ArrayBuilderTransformer.Get(baseType, randArrayLength);
                    }
                    else
                    {
                        baseType = typeof(object);
                        t        = ArrayListBuilderTransformer.Get(typeof(object), randArrayLength);
                    }

                    RandomPlansResult arrayResult;

                    PlanFilter f;

                    if (forbidNull)
                    {
                        f = delegate(Plan p, int x)
                        {
                            // Non-null heuristic
                            if (baseType.IsValueType && (p.transformer is PrimitiveValueTransformer) &&
                                (p.transformer as PrimitiveValueTransformer).IsNull)
                            {
                                return(false);
                            }

                            return(true);
                        };
                    }
                    else
                    {
                        f = delegate(Plan dontCareP, int dontCareInt) { return(true); }
                    };

                    if (!RandomPlans(out arrayResult, t.ParameterTypes, f, planDB, forbidNull, null))
                    {
                        //Util.Warning("\tNo array plans for type " + typeDesired.Name + " created");
                        result = null;
                        return(false);
                    }

                    plans.Add(new Plan(t, arrayResult.fplans, arrayResult.fparameterChoosers));
                    parameterMap[i] = new Plan.ParameterChooser(i, 0);
                }
                else
                {
                    // Types for which default plans are not created
                    // Don't want to create a plan for a value type which is just a type defn.
                    if (typeDesired.IsValueType && typeDesired.IsGenericTypeDefinition)
                    {
                        result = null; return(false);
                    }

                    //Array.createInstance can't create arrays of these types
                    if (typeDesired.Name.Equals("TypedReference") ||
                        typeDesired.Name.Equals("ArgIterator") ||
                        typeDesired.Name.Equals("ByRef") ||
                        typeDesired.Name.Equals("RuntimeArgumentHandle"))
                    {
                        result = null;
                        return(false);
                    }

                    if (typeDesired.IsPointer)
                    {
                        result = null;
                        return(false);
                    }

                    Plan chosenplan;
                    if (i == 0 && method != null && method.IsStatic)
                    {
                        chosenplan = new Plan(DummyTransformer.Get(typeDesired), new Plan[0], new Randoop.Plan.ParameterChooser[0]);
                    }
                    else
                    {
                        chosenplan = planDB.RandomPlan(typeDesired);
                        if (chosenplan == null)
                        {
                            // No plans for type typeDesired created.
                            result = null;
                            return(false);
                        }

                        if (!filter(chosenplan, i))
                        {
                            result = null;
                            return(false);
                        }

                        // If plan represents "null" return false.
                        if (forbidNull && (chosenplan.transformer is PrimitiveValueTransformer) &&
                            (chosenplan.transformer as PrimitiveValueTransformer).IsNull)
                        {
                            result = null;
                            return(false);
                        }
                    }
                    plans.Add(chosenplan);

                    Collection <int> possibleResultIndices = PossibleResultTupleIndices(chosenplan, typeDesired);
                    if (possibleResultIndices.Count == 0)
                    {
                        result = null;
                        return(false);
                    }

                    parameterMap[i] =
                        new Plan.ParameterChooser(i, possibleResultIndices[Common.Enviroment.Random.Next(possibleResultIndices.Count)]);
                }
            }

            result = new RandomPlansResult(plans.ToArray(), parameterMap);
            return(true);
        }
Beispiel #17
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);
        }