Exemplo n.º 1
0
        public void TestNextLowerItem()
        {
            var alist = new SparseAList <int>(_maxLeafSize, _maxInnerSize);
            int?i;

            for (i = 0; i < 100; i++)
            {
                alist.Add(i.Value);
                alist.InsertSpace(alist.Count, 1000);
            }
            i = int.MaxValue;
            Assert.AreEqual(alist.NextLowerItem(ref i), 99);
            Assert.AreEqual(i.Value, 99099);
            i = 99100;
            Assert.AreEqual(alist.NextLowerItem(ref i), 99);
            Assert.AreEqual(i.Value, 99099);
            Assert.AreEqual(alist.NextLowerItem(ref i), 98);
            Assert.AreEqual(i.Value, 98098);
            i = 55555;
            Assert.AreEqual(alist.NextLowerItem(ref i), 55);
            Assert.AreEqual(i.Value, 55055);
            i = 1002;
            Assert.AreEqual(alist.NextLowerItem(ref i), 1);
            Assert.AreEqual(i.Value, 1001);
            Assert.AreEqual(alist.NextLowerItem(ref i), 0);
            Assert.AreEqual(i.Value, 0);
            i = 1;
            Assert.AreEqual(alist.NextLowerItem(ref i), 0);
            Assert.AreEqual(i.Value, 0);
            Assert.AreEqual(alist.NextLowerItem(ref i), default(int));
            Assert.AreEqual(i.HasValue, false);
            i = int.MinValue;
            Assert.AreEqual(alist.NextLowerItem(ref i), default(int));
            Assert.AreEqual(i.HasValue, false);

            // Place a value at the edge of integer space
            alist.Clear();
            alist.InsertSpace(0, int.MaxValue - 1);
            alist.Add(777);
            i = int.MinValue;
            Assert.AreEqual(alist.NextLowerItem(ref i), default(int));
            Assert.AreEqual(i.HasValue, false);
            i = int.MaxValue;
            Assert.AreEqual(alist.NextLowerItem(ref i), default(int));
            Assert.AreEqual(i.HasValue, false);
            i = null;
            Assert.AreEqual(alist.NextLowerItem(ref i), 777);
            Assert.AreEqual(i.Value, int.MaxValue);
        }
Exemplo n.º 2
0
        public void TestSwap()
        {
            List <int>        list1, list2;
            SparseAList <int> alist1 = NewList(10, out list1);
            SparseAList <int> alist2 = NewList(100, out list2);

            // Can't Swap with a frozen list
            SparseAList <int> frozen = alist1.Clone();

            frozen.Freeze();
            if (_testExceptions)
            {
                AssertThrows <ReadOnlyException>(() => alist1.Swap(frozen));
            }

            // Swap, and ensure that ListChanging and NodeObserver are swapped.
            alist1.ListChanging += (sender, args) => Assert.Fail();
            alist1.AddObserver(new AListTestObserver <int, int>());
            alist1.Swap(alist2);
            Assert.AreEqual(0, alist1.ObserverCount);
            Assert.AreEqual(1, alist2.ObserverCount);

            list2.Add(999);
            alist1.Add(999);
            ExpectList(alist1, list2, false);
            ExpectList(alist2, list1, true);
        }
Exemplo n.º 3
0
        SparseAList <int> NewList(int start, int count, ListChangingHandler <int> observer, out List <int> list)
        {
            var alist = new SparseAList <int>(_maxLeafSize, _maxInnerSize);

            list = new List <int>();
            for (int i = 0; i < count; i++)
            {
                if (_r.Next(2) == 0)
                {
                    alist.Add(start + i);
                    list.Add(start + i);
                }
                else
                {
                    alist.InsertSpace(i);
                    list.Add(0);
                }
            }
            if (observer != null)
            {
                alist.ListChanging += observer;
            }
            return(alist);
        }