示例#1
0
 public void AddEnumConstantsToPlanDB(Collection <Type> types)
 {
     foreach (Type t in types)
     {
         if (t.IsEnum)
         {
             Array array = Enum.GetValues(t);
             for (int i = 0; i < array.Length; i++)
             {
                 AddPlan(Plan.Constant(t, array.GetValue(i)));
             }
         }
     }
 }
示例#2
0
        /// <summary>
        /// 0 for primitive types, null for reference types.
        /// for enum types/value types, the first member is returned
        ///
        /// The returned plan is valid if success is true
        /// </summary>
        public static Plan DefaultPlan(Type t, out bool success)
        {
            success = true;

            if (t.Equals(typeof(void)))
            {
                return(Plan.DummyVoidPlan);
            }


            if (t.IsEnum)
            {
                Array array = Enum.GetValues(t);
                return(Plan.Constant(t, array.GetValue(0) as ValueType));
            }
            //special care for chars as the default value is ^@ unreadable in the output
            if (t.Name.Equals("Char"))
            {
                return(Plan.Constant(t, '0'));
            }
            if (t.IsValueType)
            {
                try
                {
                    Array array = Array.CreateInstance(t, 1);
                    return(Plan.Constant(t, array.GetValue(0) as ValueType));
                }
                catch (Exception)
                {
                    success = false;
                    //the plan does not even make sense
                    return(Plan.DummyVoidPlan);
                }
            }
            else if (t.IsPointer)
            {
                // Cannot handle pointers.
                success = false;
                return(Plan.DummyVoidPlan);
            }
            else
            {
                return(Plan.Constant(t, null));
            }
        }
示例#3
0
        // Add some non-zero constants to the mix.
        public void AddConstantsToTDB(RandoopConfiguration config)
        {
            foreach (SimpleTypeValues vs in config.simpleTypeValues)
            {
                Type type = Type.GetType(vs.simpleType);

                if (type == null)
                {
                    throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                }

                foreach (FileName fn in vs.fileNames)
                {
                    string fileName = fn.fileName;
                    if (!File.Exists(fileName))
                    {
                        throw new Common.RandoopBareExceptions.InvalidUserParamsException("Configuration file does not exist: " + fileName);
                    }

                    if (type.Equals(typeof(sbyte)))
                    {
                        SByteReader r = new SByteReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(sbyte), o));
                        }
                    }
                    else if (type.Equals(typeof(byte)))
                    {
                        ByteReader r = new ByteReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(byte), o));
                        }
                    }
                    else if (type.Equals(typeof(short)))
                    {
                        ShortReader r = new ShortReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(short), o));
                        }
                    }
                    else if (type.Equals(typeof(ushort)))
                    {
                        UshortReader r = new UshortReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(ushort), o));
                        }
                    }
                    else if (type.Equals(typeof(int)))
                    {
                        IntReader r = new IntReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(int), o));
                        }
                    }
                    else if (type.Equals(typeof(uint)))
                    {
                        UintReader r = new UintReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(uint), o));
                        }
                    }
                    else if (type.Equals(typeof(long)))
                    {
                        LongReader r = new LongReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(long), o));
                        }
                    }
                    else if (type.Equals(typeof(ulong)))
                    {
                        UlongReader r = new UlongReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(ulong), o));
                        }
                    }
                    else if (type.Equals(typeof(char)))
                    {
                        CharReader r = new CharReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(char), o));
                        }
                    }
                    else if (type.Equals(typeof(float)))
                    {
                        FloatReader r = new FloatReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(float), o));
                        }
                    }
                    else if (type.Equals(typeof(double)))
                    {
                        DoubleReader r = new DoubleReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(double), o));
                        }
                    }
                    else if (type.Equals(typeof(bool)))
                    {
                        BoolReader r = new BoolReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(bool), o));
                        }
                    }
                    else if (type.Equals(typeof(decimal)))
                    {
                        DecimalReader r = new DecimalReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(decimal), o));
                        }
                    }
                    else
                    {
                        if (!type.Equals(typeof(string)))
                        {
                            throw new Common.RandoopBareExceptions.InternalError("invalid simple type in XML config file.");
                        }
                        Common.StringReader r = new Common.StringReader();
                        foreach (object o in r.Read(fileName))
                        {
                            this.AddPlan(Plan.Constant(typeof(string), o));
                        }
                    }
                }
            }
        }
示例#4
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);
        }