public void PartitionCreation(out IList <int> mainList, out IList <int> evenList, out IList <int> oddList)
        {
            const int max  = 10;
            var       list = new PartitionableList <int>();

            mainList = list;

            oddList  = list.CreatePartialView(x => 0 != (x % 2));
            evenList = list.CreatePartialView(x => 0 == (x % 2));

            for (int i = 0; i < max; ++i)
            {
                list.Add(i);
            }

            Assert.AreEqual(max, list.Count, "Count of the list unexpected");
            for (int i = 0; i < max; ++i)
            {
                Assert.AreEqual(i, list[i], string.Format("items unequal at index {0}", i));
            }

            Assert.AreEqual(max / 2, evenList.Count, "Count of the even list unexpected");

            for (int i = 0; i < max / 2; ++i)
            {
                Assert.AreEqual(i * 2, evenList[i], string.Format("items of even list unequal at index {0}", i));
            }

            Assert.AreEqual(max / 2, oddList.Count, "Count of the odd list unexpected");

            for (int i = 0; i < max / 2; ++i)
            {
                Assert.AreEqual(i * 2 + 1, oddList[i], string.Format("items of odd list unequal at index {0}", i));
            }
        }
        public void TestSet2()
        {
            var list = new PartitionableList <int>();
            var part = list.CreatePartialView(x => 0 == x % 2);

            list.Add(44);
            Assert.AreEqual(1, part.Count);
            Assert.AreEqual(44, part[0]);

            list[0] = 33;
            Assert.AreEqual(0, part.Count);
        }
        public void TestSet3()
        {
            var list = new PartitionableList <int>();
            var part = list.CreatePartialView(x => 0 == x % 2);

            list.Add(64);
            list.Add(66);
            Assert.AreEqual(2, part.Count);

            list[0] = 51;
            Assert.AreEqual(1, part.Count);
            Assert.AreEqual(66, part[0]);
        }
        public void TestMove1()
        {
            var list = new PartitionableList <int>();
            var part = list.CreatePartialView(x => 0 == x % 2);

            list.Add(81);
            list.Add(67);
            list.Add(46);
            list.Add(49);

            Assert.AreEqual(1, part.Count);

            list.Move(0, 2);
            Assert.AreEqual(1, part.Count);
            Assert.AreEqual(46, part[0]);
        }
        public void TestMove2()
        {
            var list = new PartitionableList <int>();
            var part = list.CreatePartialView(x => 0 == x % 2);

            list.Add(24);
            list.Add(74);
            list.Add(84);

            Assert.AreEqual(3, part.Count);

            list.Move(1, 2);
            Assert.AreEqual(3, part.Count);
            Assert.AreEqual(24, part[0]);
            Assert.AreEqual(84, part[1]);
            Assert.AreEqual(74, part[2]);
        }
        public void TestSet4()
        {
            var list = new PartitionableList <int>();
            var part = list.CreatePartialView(x => 0 == x % 3);

            list.Add(31);
            list.Add(42);
            list.Add(30);

            Assert.AreEqual(2, part.Count);

            list[0] = 15;
            Assert.AreEqual(3, part.Count);
            Assert.AreEqual(15, part[0]);
            Assert.AreEqual(42, part[1]);
            Assert.AreEqual(30, part[2]);
        }
        public void TestPartialViewDelayed()
        {
            const int max  = 10;
            var       list = new PartitionableList <int>();

            for (int i = 0; i < max; ++i)
            {
                list.Add(i);
            }
            Assert.AreEqual(list.Count, max, "Count of the list unexpected");

            var pw = list.CreatePartialView(x => x % 2 == 0);

            Assert.AreEqual(pw.Count, max / 2, "Count of the partial view unexpected");

            for (int i = 0; i < max / 2; ++i)
            {
                Assert.AreEqual(i * 2, pw[i], string.Format("items of partial view unequal at index {0}", i));
            }
        }
        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();
        }
        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();
        }