Esempio n. 1
0
 public static ArrayBuilderTransformer Get(Type arrayBaseType, int arrayLength)
 {
     ArrayBuilderTransformer t;
     BaseTypeAndLengthPair p = new BaseTypeAndLengthPair(arrayBaseType, arrayLength);
     cachedTransformers.TryGetValue(p, out t);
     if (t == null)
         cachedTransformers[p] = t = new ArrayBuilderTransformer(arrayBaseType, arrayLength);
     return t;
 }
Esempio n. 2
0
        public override bool Equals(object obj)
        {
            ArrayBuilderTransformer t = obj as ArrayBuilderTransformer;

            if (t == null)
            {
                return(false);
            }
            return(this.baseType.Equals(t.baseType) && this.length == t.length);
        }
Esempio n. 3
0
        public static ArrayBuilderTransformer Get(Type arrayBaseType, int arrayLength)
        {
            ArrayBuilderTransformer t;
            BaseTypeAndLengthPair   p = new BaseTypeAndLengthPair(arrayBaseType, arrayLength);

            cachedTransformers.TryGetValue(p, out t);
            if (t == null)
            {
                cachedTransformers[p] = t = new ArrayBuilderTransformer(arrayBaseType, arrayLength);
            }
            return(t);
        }
Esempio n. 4
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);
        }
Esempio n. 5
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);
        }