Example #1
0
        public void TestStandardOperations()
        {
            List <int>  list  = new List <int>();
            BList <int> blist = NewList();

            // Ensure standard operations work for various list sizes
            int next = 0, item;

            for (int size = 5; size <= 125; size *= 5)
            {
                while (blist.Count < size)
                {
                    AddToBoth(blist, list, next += 2, -1);
                    Assert.AreEqual(list.Count, blist.Count);
                }

                ExpectList(blist, list, _r.Next(2) == 0);

                // Add one in the middle
                int at = blist.FindLowerBound(size);
                blist.Do(AListOperation.Add, size);
                list.Insert(at, size);

                // Remove in different ways
                item = _r.Next(size * 2);
                Assert.AreEqual(list.Remove(item), blist.Remove(item));
                item = _r.Next(size * 2);
                Assert.AreEqual(list.Remove(item), blist.Do(AListOperation.Remove, item) == -1);

                // IndexOf/Contains
                item = _r.Next(size * 2);
                Assert.AreEqual(list.IndexOf(item), blist.IndexOf(item));
                item = _r.Next(size * 2);
                Assert.AreEqual(list.Contains(item), blist.Contains(item));

                ExpectList(blist, list, _r.Next(2) == 0);

                if (_testExceptions)
                {
                    AssertThrows <KeyAlreadyExistsException>(() => blist.Do(AListOperation.AddOrThrow, blist.First));
                }

                // Replace an item with itself (with ints, this command can never
                // do anything, but we'll try it in for completeness). Also,
                // try no-op AddOrReplace and AddIfNotPresent operations to verify
                // that they do nothing.
                Assert.AreEqual(0, blist.Do(AListOperation.ReplaceIfPresent, _r.Next(size)));
                Assert.AreEqual(0, blist.Do(AListOperation.AddOrReplace, blist.Last));
                Assert.AreEqual(0, blist.Do(AListOperation.AddIfNotPresent, blist.First));

                // Also do an AddOrReplace and AddIfNotPresent operation with an
                // item that does not already exist.
                blist.Do(AListOperation.AddOrReplace, next + 3);                 // end of the list
                blist.Do(AListOperation.AddIfNotPresent, next + 1);
                blist.Do(AListOperation.Add, next + 1);
                list.Add(next + 1);
                list.Add(next + 1);
                list.Add(next + 3);

                ExpectList(blist, list, _r.Next(2) == 0);

                Assert.AreEqual(2, blist.RemoveAll(next + 1));
                Assert.IsTrue(list.Remove(next + 1));
                Assert.IsTrue(list.Remove(next + 1));
            }
        }