public void AddBefore(int oldItem, int item) { for (int i = 1; i <= 5; i++) { doublyLinkedListTest.AddLast(i); } doublyLinkedListTest.AddBefore(oldItem, item); Assert.AreEqual(doublyLinkedListTest.Count(), 6, "invalid count"); Assert.IsTrue(doublyLinkedListTest.Contains(item), "Item doesn't exists."); }
public void ListTestAddBefore(int expectedCount, int[] values) { var list = new DoublyLinkedList <int>(); foreach (var item in values) { if (list.Count > 0) { list.AddBefore(list.Tail, item); } else { list.AddNodeToFront(item); } } Assert.Equal(expectedCount, list.Count); if (expectedCount > 0) { Assert.Equal(list.Head.prev, list.Tail); Assert.Equal(list.Tail.next, list.Head); Assert.NotNull(list.Head); Assert.NotNull(list.Tail); if (values.Length > 1) { Assert.Equal(values[1], list.Head.data); } Assert.Equal(values[0], list.Tail.data); } }
public void AddBeforeTheMiddleNodeAndCheckForOrder() { var list = new DoublyLinkedList <int>(); int itemCount = 1000; var middleNode = new DoublyLinkedListNode <int>(0); for (int i = 0; i < itemCount; i++) { if (i == itemCount / 2) { middleNode = list.AddLast(i); } else { list.AddLast(i); } } for (int i = 0; i < itemCount; i++) { list.AddBefore(middleNode, i); } int j = 0; int k = 0; int numberOfElementsInList = 0; foreach (var item in list) { if (j < itemCount / 2) { if (j != item) { Assert.Fail("1"); } } else if (j >= itemCount / 2 && j < itemCount + itemCount / 2) { if (k++ != item) { Assert.Fail("2"); } } else { if (j - itemCount != item) { Assert.Fail("3"); } } j++; numberOfElementsInList++; } Assert.IsTrue(list.Count == 2 * itemCount && numberOfElementsInList == list.Count); }
public void AddBeforeNullNode() { DoublyLinkedList <int> dll = new DoublyLinkedList <int> { 10 }; dll.AddBefore(dll.Head.Next, 20); }
public void InsertBefore() { IDoublyLinkedList <int> list = new DoublyLinkedList <int>(); list.AddAt(0, new DoublyLinkedListNode <int>(1)); list.AddBefore(1, new DoublyLinkedListNode <int>(122)); Assert.AreEqual(list.FindAtIndex(0).Value, 122); }
public void AddBeforeHeadTest() { DoublyLinkedList <int> dll = new DoublyLinkedList <int> { 10 }; dll.AddBefore(dll.Head, 5); Assert.AreEqual(5, dll.Head.Value); Assert.AreEqual(10, dll.Head.Next.Value); Assert.AreEqual(5, dll.Head.Next.Previous.Value); }
void InvalidNode() { // Arrange var list = new DoublyLinkedList <int>(); var anotherList = new DoublyLinkedList <int>(new int[] { 1 }); var node = anotherList.Find(1); // Act Action action = () => list.AddBefore(node, 1); // Assert action.Should().ThrowExactly <InvalidOperationException>(); }
public void AddBeforeTest() { DoublyLinkedList <int> dll = new DoublyLinkedList <int> { 10, 30 }; dll.AddBefore(dll.Head.Next, 20); Assert.AreEqual(20, dll.Head.Next.Value); Assert.AreEqual(10, dll.Head.Next.Previous.Value); Assert.AreEqual(30, dll.Head.Next.Next.Value); Assert.AreEqual(20, dll.Tail.Previous.Value); }
void NullNode() { // Arrange var list = new DoublyLinkedList <int>(); // Act Action action = () => list.AddBefore(null, 1); // Assert action.Must() .Throw <ArgumentNullException>() .EvaluatesTrue(exception => exception.ParamName == "node"); }
public void AddNullItem() { var list = new DoublyLinkedList <string>(); list.AddFirst(null); list.AddLast(null); list.AddAfter(null, null); list.AddBefore(null, null); Assert.AreEqual(list.Count(), 4, "invalid count"); list.Remove(null); list.RemoveFirst(); list.RemoveLast(); Assert.AreEqual(list.Count(), 1, "invalid count"); }
public void AddBeforePUT([PexAssumeUnderTest] IList <int> values, int toAddValue) { PexAssume.IsFalse(values.Contains(toAddValue)); DoublyLinkedList <int> dll = new DoublyLinkedList <int>(values); if (values.Count == 0) { dll.AddBefore(dll.Head, toAddValue); PexAssert.IsFalse(true, "InvalidOperationException was expected"); } dll.AddBefore(dll.Head.Next, toAddValue); if (values.Count == 1) { PexAssert.IsFalse(true, "ArgumentNullException was expected"); } PexAssert.AreEqual(toAddValue, dll.Head.Next.Value); PexAssert.AreEqual(values[0], dll.Head.Next.Previous.Value); PexAssert.AreEqual(values[1], dll.Head.Next.Next.Value); if (values.Count == 2) { PexAssert.AreEqual(toAddValue, dll.Tail.Previous.Value); } }
public void CountTest() { DoublyLinkedList <int> dll = new DoublyLinkedList <int> { 10, 5 }; dll.RemoveFirst(); dll.RemoveLast(); dll.Add(10); dll.AddAfter(dll.Head, 30); dll.AddBefore(dll.Head.Next, 20); Assert.AreEqual(3, dll.Count); }
void NullNode() { // Arrange var list = new DoublyLinkedList <int>(); // Act Action action = () => list.AddBefore(null, 1); // Assert action.Should() .ThrowExactly <ArgumentNullException>() .And .ParamName.Should() .Be("node"); }
public void CountPUT([PexAssumeUnderTest] IList <int> values, int randomPick1) { PexAssume.IsFalse(values.Contains(randomPick1)); PexAssume.IsTrue(values.Count > 1); DoublyLinkedList <int> dll = new DoublyLinkedList <int> (values); dll.RemoveFirst(); dll.RemoveLast(); PexAssert.AreEqual(values.Count - 2, dll.Count); dll.Add(randomPick1); dll.AddAfter(dll.Head, values[0]); dll.AddBefore(dll.Head.Next, values[values.Count - 1]); PexAssert.AreEqual(values.Count + 1, dll.Count); }
void AddItem(IReadOnlyList <int> collection, int after, int item, IReadOnlyList <int> expected) { // Arrange var list = new DoublyLinkedList <int>(collection); var version = list.Version; var node = list.Find(after); // Act list.AddBefore(node, item); // Assert list.Count.Should().Be(expected.Count); list.Version.Should().NotBe(version); list.EnumerateForward().Should().Equal(expected); list.EnumerateReversed().Should().Equal(expected.Reverse()); }
void AddItem(IReadOnlyList <int> collection, int after, int item, IReadOnlyList <int> expected) { // Arrange var list = new DoublyLinkedList <int>(collection); var version = list.Version; var node = list.Find(after); // Act list.AddBefore(node, item); // Assert list.Version.Must() .BeNotEqualTo(version); list.EnumerateForward().Must() .BeEnumerableOf <int>() .BeEqualTo(expected); list.EnumerateReversed().Must() .BeEnumerableOf <int>() .BeEqualTo(expected.Reverse()); }
public void Test() { DoublyLinkedList <int> list1 = new DoublyLinkedList <int>(); Console.WriteLine("Count: " + list1.Count); list1.AddFirst(1); list1.AddFirst(0); list1.AddFirst(-1); list1.AddLast(2); list1.AddLast(4); var node = list1.Find(4); list1.AddBefore(node, 3); Console.WriteLine(list1.ToString()); list1.AddAfter(node, 5); Console.WriteLine(list1.ToString()); Console.WriteLine("List contains 2 - " + (list1.Contains(2) ? "TRUE" : "FALSE")); Console.WriteLine("List contains 6 - " + (list1.Contains(6) ? "TRUE" : "FALSE")); list1.Remove(0); list1.RemoveFirst(); list1.RemoveFirst(); list1.RemoveLast(); Console.WriteLine("Count: " + list1.Count); Console.WriteLine(list1.ToString()); DoublyLinkedList <string> list2 = new DoublyLinkedList <string>(new string[] { "abcde", "abcdefg", "bcd" }); Console.WriteLine(list2.ToString()); }
public void AddBeforeTest() { DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10, 30}; dll.AddBefore(dll.Head.Next, 20); Assert.AreEqual(20, dll.Head.Next.Value); Assert.AreEqual(10, dll.Head.Next.Previous.Value); Assert.AreEqual(30, dll.Head.Next.Next.Value); Assert.AreEqual(20, dll.Tail.Previous.Value); }
/// <summary> /// Merging two lists into a given <see cref="DoublyLinkedList{T}"/>. Used for descending sort. /// </summary> /// <typeparam name="T">The data type of the <see cref="DoublyLinkedList{T}"/>.</typeparam> /// <param name="mergeList">The <see cref="DoublyLinkedList{T}"/> for merging the left and right lists into.</param> /// <param name="left">The left <see cref="DoublyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="right">The right <see cref="DoublyLinkedList{T}"/> containing some of the elements for sorting.</param> /// <param name="nodeAfterEndNode">The <see cref="DoublyLinkedListNode{T}"/> which is the node after the sorted sequence.</param> /// <param name="comparer">The <see cref="IComparable{T}"/> implementation used for comparing the elements.</param> /// <returns>Returns the given <see cref="DoublyLinkedList{T}"/> for the merged elements from the left <see cref="DoublyLinkedList{T}"/> and right <see cref="DoublyLinkedList{T}"/>.</returns> private static void MergeDescending <T>(DoublyLinkedList <T> mergeList, DoublyLinkedList <T> left, DoublyLinkedList <T> right, DoublyLinkedListNode <T> nodeAfterEndNode, IComparer <T> comparer) { bool nodeAfterEndNodeIsNull = nodeAfterEndNode == null; // merging until one of the lists becomes empty while (left.Count > 0 && right.Count > 0) { if (comparer.Compare(left.First.Value, right.First.Value) >= 0) { var node = left.First; left.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } else { var node = right.First; right.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } } // add the remaining elements from the left or the right list while (left.Count > 0) { var node = left.First; left.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } while (right.Count > 0) { var node = right.First; right.RemoveFirst(); if (nodeAfterEndNodeIsNull) // if the end node is the last in the list { mergeList.AddLast(node); // we add at the end of the list } else// if the end node is not the last { mergeList.AddBefore(nodeAfterEndNode, node);// we add before the node which is after the end node } } }
public void AddBeforeEmptyListTest() { DoublyLinkedList <int> dll = new DoublyLinkedList <int>(); dll.AddBefore(dll.Head, 10); }
public void AddBeforePUT([PexAssumeUnderTest]IList<int> values, int toAddValue) { PexAssume.IsFalse(values.Contains(toAddValue)); DoublyLinkedList<int> dll = new DoublyLinkedList<int>(values); if (values.Count == 0) { dll.AddBefore(dll.Head, toAddValue); PexAssert.IsFalse(true, "InvalidOperationException was expected"); } dll.AddBefore(dll.Head.Next, toAddValue); if (values.Count == 1) { PexAssert.IsFalse(true, "ArgumentNullException was expected"); } PexAssert.AreEqual(toAddValue, dll.Head.Next.Value); PexAssert.AreEqual(values[0], dll.Head.Next.Previous.Value); PexAssert.AreEqual(values[1], dll.Head.Next.Next.Value); if(values.Count == 2) PexAssert.AreEqual(toAddValue, dll.Tail.Previous.Value); }
public void CountPUT([PexAssumeUnderTest]IList<int> values, int randomPick1) { PexAssume.IsFalse(values.Contains(randomPick1)); PexAssume.IsTrue(values.Count > 1); DoublyLinkedList<int> dll = new DoublyLinkedList<int> (values); dll.RemoveFirst(); dll.RemoveLast(); PexAssert.AreEqual(values.Count-2, dll.Count); dll.Add(randomPick1); dll.AddAfter(dll.Head, values[0]); dll.AddBefore(dll.Head.Next, values[values.Count-1]); PexAssert.AreEqual(values.Count+1, dll.Count); }
public void AddBeforeHeadTest() { DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10}; dll.AddBefore(dll.Head, 5); Assert.AreEqual(5, dll.Head.Value); Assert.AreEqual(10, dll.Head.Next.Value); Assert.AreEqual(5, dll.Head.Next.Previous.Value); }
public void AddBeforeEmptyListTest() { DoublyLinkedList<int> dll = new DoublyLinkedList<int>(); dll.AddBefore(dll.Head, 10); }
public void CountTest() { DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10, 5}; dll.RemoveFirst(); dll.RemoveLast(); dll.Add(10); dll.AddAfter(dll.Head, 30); dll.AddBefore(dll.Head.Next, 20); Assert.AreEqual(3, dll.Count); }
public void AddBeforeNullNode() { DoublyLinkedList<int> dll = new DoublyLinkedList<int> {10}; dll.AddBefore(dll.Head.Next, 20); }