Example #1
0
        /// <summary>
        /// This method is used in a DFS exploration of nondet choice. It will pop off bool
        /// choices that are 1 until it reaches a 0 that will then be changed to a 1. The
        /// NextNondetChoiceIndex will be reset ready for replay.
        /// </summary>
        public bool BacktrackNondetChoices()
        {
            if (this.NondetChoices is null)
            {
                return(false);
            }

            Debug.Assert(this.NextNondetChoiceIndex == this.NondetChoices.Count, "this.NextNondetChoiceIndex != this.NondetChoices.Count");

            this.NextNondetChoiceIndex = 0;

            while (this.NondetChoices.Count > 0)
            {
                NonDetChoice choice = this.NondetChoices[this.NondetChoices.Count - 1];
                Debug.Assert(choice.IsBoolChoice, "DFS DPOR only supports bool choices.");
                if (choice.Choice == 0)
                {
                    choice.Choice = 1;
                    this.NondetChoices[this.NondetChoices.Count - 1] = choice;
                    return(true);
                }

                Debug.Assert(choice.Choice == 1, "Unexpected choice value.");
                this.NondetChoices.RemoveAt(this.NondetChoices.Count - 1);
            }

            return(false);
        }
Example #2
0
        /// <summary>
        /// Get a nondet choice. This may replay a nondet choice or make (and record) a new nondet choice.
        /// </summary>
        public int MakeOrReplayNondetChoice(bool isBoolChoice, IRandomNumberGenerator rand)
        {
            Debug.Assert(rand != null || isBoolChoice,
                         "A DFS DPOR exploration of int nondeterminstic choices " +
                         "is not currently supported because this won't scale.");

            if (this.NondetChoices is null)
            {
                this.NondetChoices = new List <NonDetChoice>();
            }

            if (this.NextNondetChoiceIndex < this.NondetChoices.Count)
            {
                // Replay:
                NonDetChoice choice = this.NondetChoices[this.NextNondetChoiceIndex];
                ++this.NextNondetChoiceIndex;
                Debug.Assert(choice.IsBoolChoice == isBoolChoice, "choice.IsBoolChoice != isBoolChoice");
                return(choice.Choice);
            }

            // Adding a choice.
            Debug.Assert(this.NextNondetChoiceIndex == this.NondetChoices.Count, "this.NextNondetChoiceIndex != this.NondetChoices.Count");
            NonDetChoice ndc = new NonDetChoice
            {
                IsBoolChoice = isBoolChoice,
                Choice       = rand is null ? 0 : (isBoolChoice ? rand.Next(2) : rand.Next())
            };

            this.NondetChoices.Add(ndc);
            ++this.NextNondetChoiceIndex;
            return(ndc.Choice);
        }