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);
        }
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 TestRemoveNode()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

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

            // Act
            bool removeFromEmptyList           = list.Remove(new ReadCopyUpdateListNode <int>(0));
            ReadCopyUpdateListNode <int> node0 = list.AddFirst(0);
            ReadCopyUpdateListNode <int> node1 = list.AddAfter(node0, 1);
            ReadCopyUpdateListNode <int> node2 = list.AddAfter(node1, 2);
            ReadCopyUpdateListNode <int> node3 = list.AddAfter(node2, 3);
            ReadCopyUpdateListNode <int> node4 = list.AddAfter(node3, 4);

            list.AddAfter(node4, 5);

            bool removed3      = list.Remove(node3);
            bool removed3Again = list.Remove(node3);
            bool removed72     = list.Remove(new ReadCopyUpdateListNode <int>(72));
            bool removed0      = list.Remove(node0);

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

            // Act
            ReadCopyUpdateListNode <int> first = list.AddLast(0);
            ReadCopyUpdateListNode <int> node  = list.AddLast(1);

            // Assert
            Assert.IsNotNull(node);
            Assert.AreEqual(1, node.Value);
            Assert.IsNotNull(first);
            Assert.AreEqual(node, first.Next);
            Assert.AreEqual(node, list.Last);
            Assert.IsTrue(list.Contains(1));
        }
        public void TestAddFirst()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Act
            ReadCopyUpdateListNode <int> last = list.AddFirst(0); // Will be last when next node added
            ReadCopyUpdateListNode <int> node = list.AddFirst(1);

            // Assert
            Assert.IsNotNull(node);
            Assert.AreEqual(1, node.Value);
            Assert.IsNotNull(last);
            Assert.AreEqual(node, list.First);
            Assert.AreEqual(last, node.Next);
            Assert.IsTrue(list.Contains(0));
            Assert.IsTrue(list.Contains(1));
        }
        public void TestLast()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Assume
            Assert.IsNull(list.Last);

            // Act
            ReadCopyUpdateListNode <int> last = list.AddFirst(1); // Add in reverse order

            list.AddFirst(0);

            // Assert
            Assert.IsNotNull(list.Last);
            Assert.AreEqual(last, list.Last);
            Assert.AreEqual(1, list.Last.Value);
        }
        public void TestAddAfter()
        {
            // Arrange
            ReadCopyUpdateList <int> list = new ReadCopyUpdateList <int>();

            // Assume
            AssertThrow <ArgumentNullException>(() => list.AddAfter(null, 4));

            // Act
            ReadCopyUpdateListNode <int> last    = list.AddFirst(0); // Will be last when next node added
            ReadCopyUpdateListNode <int> first   = list.AddFirst(1);
            ReadCopyUpdateListNode <int> newLast = list.AddAfter(last, 2);
            ReadCopyUpdateListNode <int> mid     = list.AddAfter(first, 3);

            // Assert
            Assert.AreEqual(first, list.First);
            Assert.AreEqual(mid, first.Next);
            Assert.AreEqual(last, mid.Next);
            Assert.AreEqual(newLast, last.Next);
        }