示例#1
0
        public void TestPrependAppend()
        {
            List <int> list  = new List <int>();
            var        alist = NewList();

            List <int>[]        lists  = new List <int> [13];
            SparseAList <int>[] alists = new SparseAList <int>[]
            {
                NewList(0, 0, out lists[0]),
                NewList(2, 1, out lists[1]),
                NewList(6, 5, out lists[2]),
                NewList(10, 5, out lists[3]),
                NewList(15, 11, out lists[4]),
                NewList(30, 11, out lists[5]),
                NewList(30, 20, out lists[6]),
                NewList(60, 20, out lists[7]),
                NewList(50, 32, out lists[8]),
                NewList(100, 32, out lists[9]),
                NewList(80, 53, out lists[10]),
                NewList(150, 53, out lists[11]),
                NewList(150, 100, out lists[12]),
            };
            Assert.AreEqual(alists.Length, lists.Length);

            // So, let's just do a random series of Append and Prepend operations,
            // clearing the list occasionally so that both list sizes vary a lot,
            // which will cause the code paths to vary (important because there
            // are several different ways these operations can be done).
            for (int trial = 0; trial < 20; trial++)
            {
                if (trial % 4 == 0)
                {
                    alist.Clear();
                    list.Clear();
                }
                int whirl = _r.Next(alists.Length);
                SparseAList <int> other = alists[whirl];
                bool append             = _r.Next(2) == 0;

                int ric = alist.GetRealItemCount(), otherRic = other.GetRealItemCount(), oldTH = alist.TreeHeight;
                if (append)
                {
                    alist.Append(other);
                    list.AddRange(lists[whirl]);
                }
                else
                {
                    alist.Prepend(other);
                    list.InsertRange(0, lists[whirl]);
                }
                Assert.That(other.GetImmutableCount() == other.Count || other.TreeHeight <= 1);
                Assert.That(alist.GetRealItemCount() == ric + otherRic);
                Assert.That(alist.GetImmutableCount() >= other.GetImmutableCount() || oldTH == 1);
            }
        }