Beispiel #1
0
        public void TestMutabilification()
        {
            // Make a single block mutable
            FVList <int> v = new FVList <int>(0, 1);
            FWList <int> w = v.ToFWList();

            ExpectList(w, 0, 1);
            w[0] = 2;
            ExpectList(w, 2, 1);
            ExpectList(v, 0, 1);

            // Make another block, make the front block mutable, then the block-of-2
            v.Push(-1);
            w    = v.ToFWList();
            w[0] = 3;
            ExpectList(w, 3, 0, 1);
            Assert.That(w.WithoutFirst(1) == v.WithoutFirst(1));
            w[1] = 2;
            ExpectList(w, 3, 2, 1);
            Assert.That(w.WithoutFirst(1) != v.WithoutFirst(1));

            // Now for a more complicated case: create a long immutable chain by
            // using a nasty access pattern, add a mutable block in front, then
            // make some of the immutable blocks mutable. This will cause several
            // immutable blocks to be consolidated into one mutable block,
            // shortening the chain.
            v = new FVList <int>(6);
            v = v.Add(-1).Tail.Add(5).Add(-1).Tail.Add(4).Add(-1).Tail.Add(3);
            v = v.Add(-1).Tail.Add(2).Add(-1).Tail.Add(1).Add(-1).Tail.Add(0);
            ExpectList(v, 0, 1, 2, 3, 4, 5, 6);
            // At this point, every block in the chain has only one item (it's
            // a linked list!) and the capacity of each block is 2.
            Assert.AreEqual(7, v.BlockChainLength);

            w = v.ToFWList();
            w.AddRange(new int[] { 5, 4, 3, 2, 1 });
            Assert.AreEqual(w.Count, 12);
            ExpectList(w, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5, 6);
            // Indices:   0  1  2  3  4  5  6  7  8  9  10 11
            // Blocks:    block A   | B   | C| D| E| F| G| H
            Assert.AreEqual(8, w.BlockChainLength);
            Assert.AreEqual(4, w.LocalCount);

            w[8] = -3;
            ExpectList(w, 5, 4, 3, 2, 1, 0, 1, 2, -3, 4, 5, 6);
            // Blocks:    block A   | block I       | F| G| H
            Assert.AreEqual(5, w.BlockChainLength);
        }
Beispiel #2
0
        private void TestTransform(int count, int[] expect, int commonTailLength, params XfAction[] actions)
        {
            FVList <int> list = new FVList <int>();

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

            int          counter = 0;
            FVList <int> result  =
                list.Transform(delegate(int i, ref int item) {
                if (i >= 0)
                {
                    Assert.AreEqual(list[i], item);
                }
                item *= 10;
                return(actions[counter++]);
            });

            Assert.AreEqual(counter, actions.Length);

            ExpectList(result.ToRVList(), expect);

            Assert.That(result.WithoutFirst(result.Count - commonTailLength)
                        == list.WithoutFirst(list.Count - commonTailLength));
        }
Beispiel #3
0
        public void TestEmptyListOperations()
        {
            FVList <int> a = new FVList <int>();
            FVList <int> b = new FVList <int>();

            a.AddRange(b);
            a.InsertRange(0, b);
            a.RemoveRange(0, 0);
            Assert.That(!a.Remove(0));
            Assert.That(a.IsEmpty);
            Assert.That(a.WithoutFirst(0).IsEmpty);

            a.Add(1);
            Assert.That(a.WithoutFirst(1).IsEmpty);

            b.AddRange(a);
            ExpectList(b, 1);
            b.RemoveAt(0);
            Assert.That(b.IsEmpty);
            b.InsertRange(0, a);
            ExpectList(b, 1);
            b.RemoveRange(0, 1);
            Assert.That(b.IsEmpty);
            b.Insert(0, a[0]);
            ExpectList(b, 1);
            b.Remove(a.First);
            Assert.That(b.IsEmpty);

            AssertThrows <InvalidOperationException>(delegate() { a.PreviousIn(b); });
        }
Beispiel #4
0
        public void TestToArray()
        {
            FVList <int> list = new FVList <int>();

            int[] array = list.ToArray();
            Assert.AreEqual(array.Length, 0);

            array = list.Add(1).ToArray();
            ExpectList(array, 1);

            array = list.Add(2).ToArray();
            ExpectList(array, 2, 1);

            array = list.Add(3).ToArray();
            ExpectList(array, 3, 2, 1);

            array = list.AddRange(new int[] { 8, 7, 6, 5, 4 }).ToArray();
            ExpectList(array, 8, 7, 6, 5, 4, 3, 2, 1);
        }
Beispiel #5
0
        public void RandomTest()
        {
            int          seed = Environment.TickCount;
            Random       r = new Random(seed);
            int          action, index, iteration = 0, i = -1;
            FWList <int> wlist = new FWList <int>();
            List <int>   list  = new List <int>();

            // Perform a series of random operations on FWList:
            // - Calling the setter
            // - RemoveAt
            // - Add to front
            // - Insert
            // - Making part of the list immutable
            try {
                for (iteration = 0; iteration < 100; iteration++)
                {
                    action = r.Next(5);
                    int range = list.Count + (action >= 2 ? 1 : 0);
                    if (range == 0)
                    {
                        iteration--; continue;
                    }
                    index = r.Next(range);

                    switch (action)
                    {
                    case 0:
                        list.RemoveAt(index);
                        wlist.RemoveAt(index);
                        break;

                    case 1:
                        list[index]  = iteration;
                        wlist[index] = iteration;
                        break;

                    case 2:
                        list.Insert(0, iteration);
                        wlist.Add(iteration);
                        break;

                    case 3:
                        list.Insert(index, iteration);
                        wlist.Insert(index, iteration);
                        break;

                    case 4:
                        FVList <int> v = wlist.WithoutFirst(index);
                        if (r.Next(2) == 0)
                        {
                            v.Add(index);
                        }
                        break;
                    }
                    Assert.AreEqual(list.Count, wlist.Count);
                    for (i = 0; i < list.Count; i++)
                    {
                        Assert.AreEqual(list[i], wlist[i]);
                    }
                }
            } catch (Exception e) {
                Console.WriteLine("{0} with seed {1} at iteration {2}, i={3}", e.GetType().Name, seed, iteration, i);
                throw;
            }
        }
Beispiel #6
0
        public void SimpleTests()
        {
            // In this simple test, I only add and remove items from the front
            // of a FVList, but forking is also tested.

            FVList <int> list = new FVList <int>();

            Assert.That(list.IsEmpty);

            // Adding to VListBlockOfTwo
            list = new FVList <int>(10, 20);
            ExpectList(list, 10, 20);

            list = new FVList <int>();
            list.Add(1);
            Assert.That(!list.IsEmpty);
            list.Add(2);
            ExpectList(list, 2, 1);

            // A fork in VListBlockOfTwo. Note that list2 will use two VListBlocks
            // here but list will only use one.
            FVList <int> list2 = list.WithoutFirst(1);

            list2.Add(3);
            ExpectList(list, 2, 1);
            ExpectList(list2, 3, 1);

            // Try doubling list2
            list2.AddRange(list2);
            ExpectList(list2, 3, 1, 3, 1);

            // list now uses two arrays
            list.Add(4);
            ExpectList(list, 4, 2, 1);

            // Try doubling list using a different overload of AddRange()
            list.AddRange((IList <int>)list);
            ExpectList(list, 4, 2, 1, 4, 2, 1);
            list = list.WithoutFirst(3);
            ExpectList(list, 4, 2, 1);

            // Remove(), Pop()
            Assert.That(list2.Remove(3));
            ExpectList(list2, 1, 3, 1);
            Assert.That(!list2.Remove(0));
            Assert.AreEqual(1, list2.Pop());
            Assert.That(list2.Remove(3));
            ExpectList(list2, 1);
            Assert.AreEqual(1, list2.Pop());
            ExpectList(list2);
            AssertThrows <Exception>(delegate() { list2.Pop(); });

            // Add many, SubList(). This will fill 3 arrays (sizes 8, 4, 2) and use
            // 1 element of a size-16 array. Oh, and test the enumerator.
            for (int i = 5; i <= 16; i++)
            {
                list.Add(i);
            }
            ExpectList(list, 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 2, 1);
            list2 = list.WithoutFirst(6);
            ExpectListByEnumerator(list2, 10, 9, 8, 7, 6, 5, 4, 2, 1);
            AssertThrows <IndexOutOfRangeException>(delegate() { int i = list[-1]; });
            AssertThrows <IndexOutOfRangeException>(delegate() { int i = list[15]; });

            // IndexOf, contains
            Assert.That(list.Contains(11));
            Assert.That(!list2.Contains(11));
            Assert.That(list[list.IndexOf(2)] == 2);
            Assert.That(list[list.IndexOf(1)] == 1);
            Assert.That(list[list.IndexOf(15)] == 15);
            Assert.That(list.IndexOf(3) == -1);

            // PreviousIn(), this[], Front
            FVList <int> list3 = list2;

            Assert.AreEqual(11, (list3 = list3.PreviousIn(list))[0]);
            Assert.AreEqual(12, (list3 = list3.PreviousIn(list))[0]);
            Assert.AreEqual(13, (list3 = list3.PreviousIn(list))[0]);
            Assert.AreEqual(14, (list3 = list3.PreviousIn(list)).First);
            Assert.AreEqual(15, (list3 = list3.PreviousIn(list)).First);
            Assert.AreEqual(16, (list3 = list3.PreviousIn(list)).First);
            AssertThrows <Exception>(delegate() { list3.PreviousIn(list); });

            // Tail
            Assert.AreEqual(10, (list3 = list3.WithoutFirst(6))[0]);
            Assert.AreEqual(9, (list3 = list3.Tail)[0]);
            Assert.AreEqual(8, (list3 = list3.Tail)[0]);
            Assert.AreEqual(7, (list3 = list3.Tail).First);
            Assert.AreEqual(6, (list3 = list3.Tail).First);
            Assert.AreEqual(5, (list3 = list3.Tail).First);
            Assert.AreEqual(4, (list3 = list3.Tail)[0]);
            Assert.AreEqual(2, (list3 = list3.Tail)[0]);
            Assert.AreEqual(1, (list3 = list3.Tail)[0]);
            Assert.That((list3 = list3.Tail).IsEmpty);

            // list2 is still the same
            ExpectList(list2, 10, 9, 8, 7, 6, 5, 4, 2, 1);

            // ==, !=, Equals(), AddRange(a, b)
            Assert.That(!list2.Equals("hello"));
            list3 = list2;
            Assert.That(list3.Equals(list2));
            Assert.That(list3 == list2);
            // This AddRange forks the list. List2 end up with block sizes 8 (3
            // used), 8 (3 used), 4, 2.
            list2.AddRange(list2, list2.WithoutFirst(3));
            ExpectList(list2, 10, 9, 8, 10, 9, 8, 7, 6, 5, 4, 2, 1);
            Assert.That(list3 != list2);

            // List3 is a sublist of list, but list2 no longer is
            Assert.That(list3.PreviousIn(list).First == 11);
            AssertThrows <InvalidOperationException>(delegate() { list2.PreviousIn(list); });

            list2 = list2.WithoutFirst(3);
            Assert.That(list3 == list2);
        }