Esempio n. 1
0
        /// <summary>
        /// Note that Explore can silently fail (exits with 1), if a second chance AV happens
        /// </summary>
        /// <param name="timer"></param>
        /// <param name="planManager"></param>
        /// <param name="methodWeigthing"></param>
        /// <param name="forbidNull"></param>
        /// <param name="mutateFields"></param>
        public void Explore(ITimer timer, PlanManager planManager, MethodWeighing methodWeigthing,
                            bool forbidNull, bool mutateFields, bool fairOpt)
        {
            // Start timing test generation.
            Timer.QueryPerformanceCounter(ref TimeTracking.generationStartTime);

            //exercise the fair option
            if (fairOpt)
            {
                Logger.Debug("Using the fair option ....");

                //fairOptLog = new StreamWriter("Randoop.fairopt.log");

                //compute the initial working set and the active methods for each class
                InitializeActiveMemberAndDependency(planManager);

                for (; ;)
                {
                    //field setting
                    if (mutateFields && actions.fieldList.Count > 0 && Enviroment.Random.Next(2) == 0)
                    {
                        NewFieldSettingPlan(actions.RandomField(), planManager, forbidNull);
                    }

                    //If the active set is recomputed from scratch
                    //InitializeActiveMemberAndDependency(planManager);


                    //copy the keys
                    Dictionary <Type, bool> .KeyCollection keys = new Dictionary <Type, bool> .KeyCollection(activeTypes);

                    List <Type> activeList = new List <Type>();
                    foreach (Type t in keys)
                    {
                        activeList.Add(t);
                    }

                    RandoopPrintFairStats("Randoop.RandomExplore::Explore", "Starting a round of the fair workset algorithm with " + activeTypes.Count + "activeTypes");

                    foreach (Type t in activeList)
                    {
                        //pick up an active method of the type

                        MemberInfo method;

                        try
                        {
                            method = actions.RandomActiveMethodOrConstructor(activeMembers, t);
                        }
                        catch (Exception e)
                        {
                            Logger.Error("Exception raised from RandomActiveMethodorConstructor is {0}", e.ToString());
                            continue;
                        }

                        //Logger.Debug("Picked up method {0}::{1}", m.DeclaringType,m.Name );
                        int prevBuilderPlanCnt = planManager.builderPlans.NumPlans;

                        //Randoop step
                        ExploreMemberInternal(timer, planManager, forbidNull, method);

                        //need to know if the object creation was succesful, since we know the type
                        //we do it indirectly by looking at the builder plans
                        if (planManager.builderPlans.NumPlans > prevBuilderPlanCnt)
                        {
                            Type retType;
                            if (method is MethodInfo)
                            {
                                retType = (method as MethodInfo).ReturnType;
                            }
                            else if (method is ConstructorInfo)
                            {
                                retType = (method as ConstructorInfo).DeclaringType;
                            }
                            else
                            {
                                throw new RandoopBug("Constructor or Method expected");
                            }

                            //use the return type of the method/constructor to incrementally activate more methods/classes
                            try
                            {
                                UpdateActiveMethodsAndClasses(retType);
                            }
                            catch (Exception e) // had a nasty bug in the updateActiveMethodsAndClasses, so using try-catch
                            {
                                Logger.Error("Exception raised from UpdateActiveMethodsAndClasses is {0}", e.ToString());
                            }
                        }

                        if (timer.TimeToStop())
                        {
                            //get the size of # of active members
                            int size = 0;
                            foreach (KeyValuePair <Type, List <MemberInfo> > kv in activeMembers)
                            {
                                List <MemberInfo> l = kv.Value;
                                size = size + l.Count;
                            }

                            //stats printing
                            Logger.Debug("Finishing the fair exploration...printing activity stats");
                            Logger.Debug("Stats: <#ActiveTypes, #ActiveMembers, #BuilderPlans, #typesWithObjs> = <{0},{1},{2},{3}>",
                                         activeTypes.Count, size, planManager.builderPlans.NumPlans, typesWithObjects.Count);
                            //fairOptLog.Close();

                            return;
                        }
                    }
                }
            }
            else
            {
                #region Old code for the Randoop exploration
                for (; ;)
                {
                    //field setting
                    if (mutateFields && this.actions.fieldList.Count > 0 && Common.Enviroment.Random.Next(2) == 0)
                    {
                        NewFieldSettingPlan(this.actions.RandomField(), planManager, forbidNull);
                    }

                    MemberInfo m;
                    if (methodWeigthing == MethodWeighing.RoundRobin)
                    {
                        m = actions.RandomMethodOrConstructorRoundRobin();
                    }
                    else if (methodWeigthing == MethodWeighing.Uniform)
                    {
                        m = actions.RandomMethodOrConstructorUniform();
                    }
                    else
                    {
                        throw new RandoopBug("Unrecognized method weighting option.");
                    }


                    ExploreMemberInternal(timer, planManager, forbidNull, m);

                    if (timer.TimeToStop())
                    {
                        // this computation gives us the activity stats for the non-fair option
                        InitializeActiveMemberAndDependency(planManager);
                        //get the size of # of active members
                        int size = 0;
                        foreach (KeyValuePair <Type, List <MemberInfo> > kv in activeMembers)
                        {
                            List <MemberInfo> l = kv.Value;
                            size = size + l.Count;
                        }

                        //stats printing
                        Logger.Debug("Finishing the non-fair exploration...printing activity stats");
                        Logger.Debug("Stats: <#ActiveTypes, #ActiveMembers, #BuilderPlans, #typesWithObjs> = <{0},{1},{2},{3}>",
                                     activeTypes.Count, size, planManager.builderPlans.NumPlans, typesWithObjects.Count);
                        //fairOptLog.Close();

                        return;
                    }
                }
                #endregion
            }
        }