コード例 #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);
            }
        }
コード例 #2
0
        public void TestClearSpace()
        {
            for (int iter = 0; iter < 10; iter++)
            {
                int i1 = MathEx.Square(_r.Next(50)) + 1;                      // e.g. 100
                int i0 = _r.Next(i1);                                         // e.g. 50
                int i2 = i1 + MathEx.Square(_r.Next(50)) + 1;                 // e.g. 100
                int i3 = i2 + _r.Next(2500);                                  // e.g. 1000

                SparseAList <int> list = new SparseAList <int>();
                list.ClearSpace(0, i1);
                Assert.AreEqual(i1, list.Count);
                Assert.AreEqual(0, list.GetRealItemCount());
                Assert.AreEqual(0, list[i1 - 1]);
                if (_testExceptions)
                {
                    Assert.Throws <ArgumentOutOfRangeException>(() => { var _ = list[i1]; });
                    Assert.Throws <ArgumentOutOfRangeException>(() => { list.ClearSpace(0, -1); });
                    Assert.Throws <ArgumentOutOfRangeException>(() => { list.ClearSpace(-1, 10); });
                }
                list.ClearSpace(i0, i2 - i0);
                Assert.AreEqual(i2, list.Count);
                Assert.AreEqual(0, list.GetRealItemCount());
                for (int i = i0; i < i2; i++)
                {
                    list[i] = i;
                }
                list.ClearSpace(i1, i3 - i1);
                Assert.AreEqual(i3, list.Count);
                Assert.AreEqual(i1 - i0, list.GetRealItemCount());
                list.ClearSpace(i0 + 1, i1 - (i0 + 1));
                Assert.AreEqual(i3, list.Count);
                Assert.AreEqual(1, list.GetRealItemCount());
                list.ClearSpace(0, i0 + 1);
                Assert.AreEqual(i3, list.Count);
                Assert.AreEqual(0, list.GetRealItemCount());
            }
        }