Extract method LinkedListNode.GetNodeAtIndex out of Tape.LoadState
authorMichael Welch <michaelgwelch@gmail.com>
Thu, 23 Feb 2012 19:51:03 +0000 (13:51 -0600)
committerMichael Welch <michaelgwelch@gmail.com>
Thu, 23 Feb 2012 19:51:03 +0000 (13:51 -0600)
Did the extract method. This simplified LoadState. Added unit tests for the new method.

src.net/BrainmessCore/LinkedListNode.cs
src.net/BrainmessCore/Tape.cs
src.net/BrainmessCoreTests/LinkedListNodeTests.cs

index 3e8811d..349d8c9 100644 (file)
@@ -52,6 +52,23 @@ namespace Welch.Brainmess
             return index;
 
         }
+
+        public static LinkedListNode<T> GetNodeAtIndex<T>(this LinkedList<T> list, int index)
+        {
+            if (list == null) throw new ArgumentNullException("list");
+            if (index < 0) throw new ArgumentOutOfRangeException("index");
+
+            var currentNode = list.First;
+
+            // Find the linked list node that corresponds to the specified position.
+            while (index > 0 && currentNode != null)
+            {
+                currentNode = currentNode.Next;
+                index--;
+            }
+            if (currentNode == null) throw new ArgumentOutOfRangeException("index");
+            return currentNode;
+        }
     }
 }
 
index bb88e93..3c331f4 100644 (file)
@@ -45,17 +45,7 @@ namespace Welch.Brainmess
             if (position > array.Length - 1) throw new ArgumentOutOfRangeException("position");
 
             var list = new LinkedList<int>(array);
-            int counter = position;
-            var currentNode = list.First;
-            
-            // Find the linked list node that corresponds to the specified position.
-            while(counter > 0)
-            {
-                Debug.Assert(currentNode != null); // This is used to remove Resharper warning. Our checks above gaurantee this will never throw.
-                currentNode = currentNode.Next;
-                counter--;
-            }
-
+            var currentNode = list.GetNodeAtIndex(position);
             return new Tape(currentNode);
         }
 
index d981236..a107e16 100644 (file)
@@ -67,7 +67,7 @@ namespace Welch.Brainmess
         {
             try
             {
-                LinkedListNode<int> node = GetNull();
+                LinkedListNode<int> node = Identity.ValueOf<LinkedListNode<int>>(null);
                 node.MoveForward();
                 Assert.Fail("Expected ArgumentNullException");
 
@@ -77,11 +77,6 @@ namespace Welch.Brainmess
             }
         }
 
-        private static LinkedListNode<int> GetNull()
-        {
-            return null;
-        }
-
         [Test]
         public void MoveBackward_WithNullNode_ShouldThrow()
         {
@@ -221,11 +216,88 @@ namespace Welch.Brainmess
             {
                 // Act
                 node.IndexOf();
+
+                // Assert
                 Assert.Fail("Expected ArgumentException");
             }
             catch (ArgumentException)
             { }
         }
+
+        [TestMethod]
+        public void GetNodeAtIndex_IfListIsNull_ShouldThrowArgumentNullException()
+        {
+            // Arrange
+            LinkedList<string> list = Identity.ValueOf<LinkedList<string>>(null);
+
+            try
+            {
+                // Act
+                list.GetNodeAtIndex(5);
+
+                // Assert
+                Assert.Fail("Expected ArgumentNullException");
+            }
+            catch (ArgumentNullException)
+            {
+            }
+            
+        }
+
+        [TestMethod]
+        public void GetNodeAtIndex_IfIndexIsNegative_ShouldThrowArgumentOutOfRangeException()
+        {
+            // Arrange
+            LinkedList<string> strings = new LinkedList<string>();
+
+            try
+            {
+                // Act
+                strings.GetNodeAtIndex(-1);
+
+                // Assert
+                Assert.Fail("Expected ArgumentOutOfRange Exception");
+            }
+            catch (ArgumentOutOfRangeException)
+            {
+
+            }
+
+        }
+
+        [TestMethod]
+        public void GetNodeAtIndex_IfIndexEqualsLength_ShouldThrowArgumentOutOfRangeException()
+        {
+            // Arrange
+            LinkedList<int> nums = new LinkedList<int>(new[] { 1, 3 });
+
+            try
+            {
+                // Act
+                nums.GetNodeAtIndex(2);
+
+                // Assert
+                Assert.Fail("Expected ArgumentOutOfRangeException");
+            }
+            catch (ArgumentOutOfRangeException)
+            {
+            }
+        }
+
+        [TestMethod]
+        public void GetNodeAtIndex_WithIndexEqualToOne_ShouldReturnSecondNode()
+        {
+            // Arrange
+            LinkedList<int> nums = new LinkedList<int>(new[] { 1, 3, 5 });
+
+            // Act
+            var node = nums.GetNodeAtIndex(1);
+
+            // Assert
+            Assert.AreEqual(3, node.Value);
+        }
+
+
     }
     // ReSharper restore InconsistentNaming
 }