public void TestObjectEnumeration()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();
            IEnumerable listEnum          = list;

            list.AddValues(0, 1, 2, 3, 4);
            IEnumerator enumerator = listEnum.GetEnumerator();

            // Act
            enumerator.Reset();
            int    count = 0;
            object current;

            AssertThrow <InvalidOperationException>(() => current = enumerator.Current); // Can't access current before move next
            while (enumerator.MoveNext())
            {
                current = enumerator.Current;
                Assert.AreEqual(count, current);
                count++;
            }
            AssertThrow <InvalidOperationException>(() => current = enumerator.Current); // Can't access current after end of enum
            // ReSharper disable ImplicitlyCapturedClosure
            AssertThrow <InvalidOperationException>(() => enumerator.MoveNext());
            // ReSharper restore ImplicitlyCapturedClosure

            // Assert
            Assert.AreEqual(5, count);
        }
Example #2
0
        public void TestConcurrentReadAndUpdate()
        {
            rculist = new ReadCopyUpdateList <int>();
            rculist.AddValues(1, 2, 3, 4);
            rcunode3 = rculist.Find(3);
            Assert.IsNotNull(rcunode3);

            Parallel.Invoke(
                () =>
            {
                // Reader thread
                ReadCopyUpdateListNode <int> node = rculist.First;
                int count = 0;
                while (node != null)
                {
                    node = node.Next;
                    count++;
                }
                Debug.WriteLine("Read completed - count was: " + count);
            },
                () =>
            {
                // Updater thread
                rculist.Remove(rcunode3);
                Debug.WriteLine("Node 3 removed");
            });
        }
        public void TestTypedEnumeration()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            list.AddValues(0, 1, 2, 3, 4);
            IEnumerator <int> enumerator = list.GetEnumerator();

            // Act
            enumerator.Reset();
            int count = 0;
            int current;

            AssertThrow <InvalidOperationException>(() => current = enumerator.Current); // Can't access current before move next
            while (enumerator.MoveNext())
            {
                current = enumerator.Current;
                Assert.AreEqual(count, current);
                count++;
            }
            AssertThrow <InvalidOperationException>(() => current = enumerator.Current); // Can't access current after end of enum
            AssertThrow <InvalidOperationException>(() => enumerator.MoveNext());

            // Assert
            Assert.AreEqual(5, count);
        }
        public void TestFindFirst()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Assume
            AssertThrow <ArgumentNullException>(() => list.FindFirst(null));

            // Act
            list.AddValues(0, 1, 2, 3, 4, 3, 5);
            ReadCopyUpdateListNode <int> first3 = list.FindFirst(n => n == 3);
            bool removedFirst3 = list.Remove(3);
            ReadCopyUpdateListNode <int> second3 = list.FindFirst(n => n == 3);
            ReadCopyUpdateListNode <int> first72 = list.FindFirst(n => n == 72);

            // Assert
            Assert.IsNotNull(first3);
            Assert.AreEqual(3, first3.Value);
            Assert.IsNotNull(first3.Next);
            Assert.AreEqual(4, first3.Next.Value);
            Assert.IsTrue(removedFirst3);
            Assert.IsNotNull(second3);
            Assert.AreEqual(3, second3.Value);
            Assert.IsNotNull(second3.Next);
            Assert.AreEqual(5, second3.Next.Value);
            Assert.IsNull(first72);
        }
        public void TestClear()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Act
            list.AddValues(0, 1, 2, 3, 4);
            bool contains3 = list.Contains(3);

            list.Clear();
            bool contains3AfterClear = list.Contains(3);

            // Assert
            Assert.IsTrue(contains3);
            Assert.IsFalse(contains3AfterClear);
        }
        public void TestRemoveValue()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Act
            list.AddValues(0, 1, 2, 3, 4, 5);
            bool removed3      = list.Remove(3);
            bool removed3Again = list.Remove(3);
            bool removed72     = list.Remove(72);

            // Assert
            Assert.IsTrue(removed3);
            Assert.IsFalse(removed3Again);
            Assert.IsFalse(removed72);
        }
        public void TestDisposeTypedEnumeration()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            list.AddValues(0, 1, 2, 3, 4);
            int count = 0;

            // Act
            using (IEnumerator <int> enumerator = list.GetEnumerator())
            {
                enumerator.Reset();
                while (enumerator.MoveNext())
                {
                    int current = enumerator.Current;
                    Assert.AreEqual(count, current);
                    count++;
                }
            }

            // Assert
            Assert.AreEqual(5, count);
        }