예제 #1
0
        public virtual void SimpleObserverTests1()
        {
            AList alist = NewList();
            var   tob   = new AListTestObserver <int, T>();

            alist.AddObserver(tob);
            Add(alist, 1, 0);
            Add(alist, 0, 0);
            Add(alist, 3, 2);
            Add(alist, 2, 2);
            Assert.AreEqual(4, tob.CheckPointCount);
            alist.RemoveAt(1);
            alist.RemoveRange(0, 2);
            Assert.AreEqual(6, tob.CheckPointCount);
        }
예제 #2
0
        public virtual void ObserveRemoveSection()
        {
            List <T> list;
            AList    alist = NewList(200, out list);

            var tob = new AListTestObserver <int, T>();
            var idx = new AListIndexer <int, T>();

            alist.AddObserver(tob);
            alist.AddObserver(idx);

            for (int iteration = 0; alist.Count > 10; iteration++)
            {
                int  start  = _r.Next(alist.Count - 5);
                int  amount = _r.Next(Math.Min(alist.Count - start, 40));
                bool remove = _r.Next(3) != 0;

                AList sec;
                if (remove)
                {
                    sec = RemoveSection(alist, start, amount);
                    list.RemoveRange(start, amount);
                }
                else
                {
                    sec = CopySection(alist, start, amount);
                }

                ExpectList(alist, list, true);
                tob.CheckPoint();
                idx.VerifyCorrectness();
                Assert.AreEqual(alist.Count, idx.ItemCount);

                for (int i = 0; i < list.Count; i++)
                {
                    Assert.AreEqual(i, idx.IndexOfAny(list[i]));
                }
                for (int i = 0; i < sec.Count; i++)
                {
                    Assert.AreEqual(remove ? -1 : start + i, idx.IndexOfAny(sec[i]));
                }
            }

            alist.RemoveObserver(tob);
            alist.RemoveObserver(idx);
        }
예제 #3
0
        public virtual void SimpleObserverTests2()
        {
            // Note: derived class must test observers with replacement and other
            //       functions that this class does not have access to.
            int       index;
            List <T>  list;
            const int InitialCount = 50;
            AList     alist        = NewList(InitialCount, out list);

            var tob = new AListTestObserver <int, T>();
            var idx = new AListIndexer <int, T>();

            alist.AddObserver(tob);
            alist.AddObserver(idx);
            Assert.AreEqual(0, tob.CheckPointCount);
            idx.VerifyCorrectness();

            for (int item = -50; item < 0; item++)
            {
                // Add
                index = Add(alist, item, item + 50);
                T item2 = alist[index];

                Assert.AreEqual(item + 51, tob.CheckPointCount);
                Assert.AreEqual(alist.Count, idx.ItemCount);
                Assert.AreEqual(index, idx.IndexOfAny(item2));
            }

            idx.VerifyCorrectness();

            // Modifying a clone must have no effect.
            //var clone = alist.Clone();
            //index = Add(clone, 999, clone.Count);
            //Assert.AreEqual(alist.Count, tob.CheckPointCount + InitialCount);
            //Assert.AreEqual(-1, idx.IndexOfAny(clone[index]));
            //idx.VerifyCorrectness();

            while (alist.Count > 2)
            {
                // RemoveAt
                int at   = _r.Next(alist.Count);
                T   item = alist[at];
                Assert.AreEqual(at, idx.IndexOfAny(item));
                alist.RemoveAt(at);
                Assert.AreEqual(-1, idx.IndexOfAny(item));
                Assert.AreEqual(alist.Count, idx.ItemCount);

                // RemoveRange
                at   = _r.Next(alist.Count);
                item = alist[at];
                int amount = _r.Next(1, Math.Min(9, alist.Count - at));
                alist.RemoveRange(at, amount);
                Assert.AreEqual(-1, idx.IndexOfAny(item));
                Assert.AreEqual(alist.Count, idx.ItemCount);

                idx.VerifyCorrectness();
            }

            tob.CheckPoint();
            alist.Clear();
            Assert.AreEqual(0, idx.ItemCount);
            Assert.AreEqual(-1, idx.IndexOfAny(default(T)));
        }