Esempio n. 1
0
        public void TestRandomActions2()
        {
            var actionGenerator = new ActionGenerator();
            var rndIndex        = new System.Random();
            var rndNewNumber    = new System.Random();

            var list = new PartitionableList <int>();

            var part1 = list.CreatePartialView(Condition1);
            var part2 = list.CreatePartialView(Condition2);
            var part3 = list.CreatePartialView(Condition3);

            var maxTimeToRun = TimeSpan.FromSeconds(1);
            var watch        = new System.Diagnostics.Stopwatch();

            watch.Start();
            var  actionStatistics     = new int[6];
            long accumulatedListCount = 0;
            int  maxListCount         = 0;

            int numberOfActionsTested = 0;

            for (; watch.Elapsed < maxTimeToRun; ++numberOfActionsTested)
            {
                var action = actionGenerator.GetNextAction(list);
                ++actionStatistics[(int)action];
                int idx, idx2;
                int newNumber;
                int?oldItem = null;

                switch (action)
                {
                case ListAction.Clear:
                    part1.Clear();
                    Assert.AreEqual(0, part1.Count);

                    idx = rndIndex.Next(5);
                    if (idx == 0)
                    {
                        list.Clear();
                        idx = rndIndex.Next(10);
                        for (int i = 0; i < idx; ++i)
                        {
                            list.Add(rndNewNumber.Next(100)); // add some numbers to the parent list that not neccessarily fullfil the criterion of part1
                        }
                    }

                    break;

                case ListAction.RemoveAt:
                    if (part1.Count > 0)
                    {
                        var oldCount = part1.Count;
                        idx     = rndIndex.Next(part1.Count);
                        oldItem = part1[idx];
                        part1.RemoveAt(idx);
                        Assert.AreEqual(oldCount - 1, part1.Count);
                    }
                    break;

                case ListAction.Add:
                {
                    var oldCount = part1.Count;
                    part1.Add(newNumber = 3 * rndNewNumber.Next(100));
                    Assert.AreEqual(oldCount + 1, part1.Count);
                    Assert.AreEqual(newNumber, part1[part1.Count - 1]);
                }
                break;

                case ListAction.InsertAt:
                {
                    var oldCount = part1.Count;
                    idx = rndIndex.Next(part1.Count + 1);
                    part1.Insert(idx, newNumber = 3 * rndNewNumber.Next(100));
                    Assert.AreEqual(oldCount + 1, part1.Count);
                    Assert.AreEqual(newNumber, part1[idx]);
                }
                break;

                case ListAction.Set:
                    if (part1.Count > 0)
                    {
                        var oldCount = part1.Count;
                        idx        = rndIndex.Next(part1.Count);
                        part1[idx] = (newNumber = 3 * rndNewNumber.Next(100));
                        Assert.AreEqual(oldCount, part1.Count);
                        Assert.AreEqual(newNumber, part1[idx]);
                    }
                    break;

                case ListAction.Move:
                    if (part1.Count > 0)
                    {
                        var oldCount = part1.Count;
                        idx     = rndIndex.Next(part1.Count);
                        idx2    = rndIndex.Next(part1.Count);
                        oldItem = part1[idx];
                        part1.Move(idx, idx2);
                        Assert.AreEqual(oldCount, part1.Count);
                        Assert.AreEqual(oldItem, part1[idx2]);
                    }
                    break;

                default:
                    break;
                }

                accumulatedListCount += list.Count;
                maxListCount          = Math.Max(maxListCount, list.Count);

                bool succ1 = IsOrderingTestSuccessfull(list, part1, Condition1);
                bool succ2 = IsOrderingTestSuccessfull(list, part2, Condition2);
                bool succ3 = IsOrderingTestSuccessfull(list, part3, Condition3);

                if (!succ1)
                {
                }
                if (!succ2)
                {
                }
                if (!succ3)
                {
                }

                Assert.IsTrue(succ1);
                Assert.IsTrue(succ2);
                Assert.IsTrue(succ3);
            }

            double averageListCount = accumulatedListCount / numberOfActionsTested;

            watch.Stop();
        }
Esempio n. 2
0
        public void TestRandomActions1()
        {
            var actionGenerator = new ActionGenerator();
            var rndIndex        = new System.Random();
            var rndNewNumber    = new System.Random();

            var list = new PartitionableList <int>();

            var part1 = list.CreatePartialView(Condition1);
            var part2 = list.CreatePartialView(Condition2);
            var part3 = list.CreatePartialView(Condition3);

            var maxTimeToRun = TimeSpan.FromSeconds(1);
            var watch        = new System.Diagnostics.Stopwatch();

            watch.Start();
            var  actionStatistics     = new int[Enum.GetValues(typeof(ListAction)).Length];
            long accumulatedListCount = 0;
            int  maxListCount         = 0;

            int numberOfActionsTested = 0;

            for (; watch.Elapsed < maxTimeToRun; ++numberOfActionsTested)
            {
                var action = actionGenerator.GetNextAction(list);
                ++actionStatistics[(int)action];
                int idx;
                int idx2;
                int newNumber;
                int?oldItem = null;

                switch (action)
                {
                case ListAction.Clear:
                    list.Clear();
                    Assert.AreEqual(0, list.Count);
                    break;

                case ListAction.RemoveAt:
                    if (list.Count > 0)
                    {
                        var oldCount = list.Count;
                        idx     = rndIndex.Next(list.Count);
                        oldItem = list[idx];
                        list.RemoveAt(idx);
                        Assert.AreEqual(oldCount - 1, list.Count);
                    }
                    break;

                case ListAction.Add:
                {
                    var oldCount = list.Count;
                    list.Add(newNumber = rndNewNumber.Next(100));
                    Assert.AreEqual(oldCount + 1, list.Count);
                    Assert.AreEqual(newNumber, list[list.Count - 1]);
                }
                break;

                case ListAction.InsertAt:
                {
                    var oldCount = list.Count;
                    idx = rndIndex.Next(list.Count + 1);
                    list.Insert(idx, newNumber = rndNewNumber.Next(100));
                    Assert.AreEqual(oldCount + 1, list.Count);
                    Assert.AreEqual(newNumber, list[idx]);
                }
                break;

                case ListAction.Set:
                    if (list.Count > 0)
                    {
                        var oldCount = list.Count;
                        idx       = rndIndex.Next(list.Count);
                        oldItem   = list[idx];
                        list[idx] = (newNumber = rndNewNumber.Next(100));
                        Assert.AreEqual(oldCount, list.Count);
                        Assert.AreEqual(newNumber, list[idx]);
                    }
                    break;

                case ListAction.Move:
                    if (list.Count > 0)
                    {
                        var oldCount = list.Count;
                        idx     = rndIndex.Next(list.Count);
                        idx2    = rndIndex.Next(list.Count);
                        oldItem = list[idx];
                        list.Move(idx, idx2);
                        Assert.AreEqual(oldCount, list.Count);
                        Assert.AreEqual(oldItem, list[idx2]);
                    }
                    break;

                default:
                    break;
                }

                accumulatedListCount += list.Count;
                maxListCount          = Math.Max(maxListCount, list.Count);

                bool succ1 = IsOrderingTestSuccessfull(list, part1, Condition1);
                bool succ2 = IsOrderingTestSuccessfull(list, part2, Condition2);
                bool succ3 = IsOrderingTestSuccessfull(list, part3, Condition3);

                if (!succ1)
                {
                }
                if (!succ2)
                {
                }
                if (!succ3)
                {
                }

                Assert.IsTrue(succ1);
                Assert.IsTrue(succ2);
                Assert.IsTrue(succ3);
            }

            double averageListCount = accumulatedListCount / numberOfActionsTested;

            watch.Stop();
        }