Example #1
0
        public void TestEmptyListOperations()
        {
            WList <int> a = new WList <int>();
            WList <int> b = new WList <int>();

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

            a.Add(1);
            Assert.That(a.WithoutLast(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.Last);
            Assert.That(b.IsEmpty);
        }
Example #2
0
        public void TestMutabilification()
        {
            // Make a single block mutable
            VList <int> v = new VList <int>(1, 0);
            WList <int> w = v.ToWList();

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

            // Make another block, make the front block mutable, then the block-of-2
            v.Push(-1);
            w    = v.ToWList();
            w[2] = 3;
            ExpectList(w, 1, 0, 3);
            Assert.That(w.WithoutLast(1) == v.WithoutLast(1));
            w[1] = 2;
            ExpectList(w, 1, 2, 3);
            Assert.That(w.WithoutLast(1) != v.WithoutLast(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 VList <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, 6, 5, 4, 3, 2, 1, 0);
            // 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.ToWList();
            w.AddRange(new int[] { 1, 2, 3, 4, 5 });
            Assert.AreEqual(w.Count, 12);
            ExpectList(w, 6, 5, 4, 3, 2, 1, 0, 1, 2, 3, 4, 5);
            // Indices:   0  1  2  3  4  5  6  7  8  9  10 11
            // Blocks:    H| G| F| E| D| C|  B  | block A (front of chain)
            Assert.AreEqual(8, w.BlockChainLength);
            Assert.AreEqual(4, w.LocalCount);

            w[3] = -3;
            ExpectList(w, 6, 5, 4, -3, 2, 1, 0, 1, 2, 3, 4, 5);
            // Indices:   0  1  2   3  4  5  6  7  8  9  10 11
            // Blocks:    H| G| F|  block I      | block A (front of chain)
            Assert.AreEqual(5, w.BlockChainLength);
        }
Example #3
0
        public void TestFork()
        {
            WList <int> A = new WList <int>();

            A.AddRange(new int[] { 1, 2, 3 });
            WList <int> B = A.Clone();

            A.Push(4);
            ExpectList(B, 1, 2, 3);
            ExpectList(A, 1, 2, 3, 4);
            B.Push(-4);
            ExpectList(B, 1, 2, 3, -4);

            Assert.That(A.WithoutLast(2) == B.WithoutLast(2));
        }
Example #4
0
        public void SimpleTests()
        {
            // Tests simple adds and removes from the front of the list. It
            // makes part of its tail immutable, but doesn't make it mutable
            // again. Also, we test operations that don't modify the list.

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

            Assert.That(list.IsEmpty);

            // create VListBlockOfTwo
            list = new WList <int>(10, 20);
            ExpectList(list, 10, 20);

            // Add()
            list.Clear();
            list.Add(1);
            Assert.That(!list.IsEmpty);
            list.Add(2);
            Assert.AreEqual(1, list.BlockChainLength);
            list.Add(3);
            Assert.AreEqual(2, list.BlockChainLength);

            ExpectList(list, 1, 2, 3);
            VList <int> snap = list.ToVList();

            ExpectList(snap, 1, 2, 3);

            // AddRange(), Push(), Pop()
            list.Push(4);
            list.AddRange(new int[] { 5, 6 });
            ExpectList(list, 1, 2, 3, 4, 5, 6);
            Assert.AreEqual(list.Pop(), 6);
            ExpectList(list, 1, 2, 3, 4, 5);
            list.RemoveRange(3, 2);
            ExpectList(list, 1, 2, 3);

            // Double the list
            list.AddRange(list);
            ExpectList(list, 1, 2, 3, 1, 2, 3);
            list.RemoveRange(3, 3);

            // Fill a third block
            list.AddRange(new int[] { 4, 5, 6, 7, 8, 9 });
            list.AddRange(new int[] { 10, 11, 12, 13, 14 });
            ExpectList(list, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14);

            // Remove(), enumerator
            list.Remove(14);
            list.Remove(13);
            list.Remove(12);
            list.Remove(11);
            ExpectListByEnumerator(list, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10);

            // IndexOutOfRangeException
            AssertThrows <IndexOutOfRangeException>(delegate() { int i = list[-1]; });
            AssertThrows <IndexOutOfRangeException>(delegate() { int i = list[10]; });
            AssertThrows <IndexOutOfRangeException>(delegate() { list.Insert(-1, -1); });
            AssertThrows <IndexOutOfRangeException>(delegate() { list.Insert(list.Count + 1, -1); });
            AssertThrows <IndexOutOfRangeException>(delegate() { list.RemoveAt(-1); });
            AssertThrows <IndexOutOfRangeException>(delegate() { list.RemoveAt(list.Count); });

            // Front, Contains, IndexOf
            Assert.That(list.Last == 10);
            Assert.That(list.Contains(9));
            Assert.That(list[list.IndexOf(2)] == 2);
            Assert.That(list[list.IndexOf(9)] == 9);
            Assert.That(list[list.IndexOf(7)] == 7);
            Assert.That(list.IndexOf(-1) == -1);

            // snap is still the same
            ExpectList(snap, 1, 2, 3);
        }