HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

Comparing DoublyLinkedList<T> implementation performance with other BCL (.NET) classes

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
doublylinkedlistbclcomparingwithnetperformanceclassesimplementationother

Problem

I have implemented generic doubly-linked list class, which supports IEnumerable,IEnumerator interfaces.

DoublyLinkedList is fully compatible with standart BCL classes.

Targets:

  • class can be used as a standard replacement for Stack, Queue and List.



  • class itself is immutable, so you can-not change the containing value of the element.



  • class methods raises exceptions which conforms for common considerations.



  • class method's lock statement was used for locking current active element.



  • class Push, Pop, Peek operations is breaking the current state of the enumerated object.



How can we compare the performance for that class compared to the BCL classes and other implementation?

Will you provide the C# code to compare to the BCL classes like Stack, Querty, List?

Unit tests (code coverage - 100%):

```
[TestClass]
public class DoublyLinkedListUnitTest
{
[TestMethod]
public void TestValueMethod()
{
DoublyLinkedList dll = new DoublyLinkedList();

int i1 = 1;
int i2 = 2;
int i3 = 3;

Assert.AreEqual(((IDoublyLinkedList)dll).Index, -1);
try
{
dll.Peek();
}
catch (Exception ex)
{
Assert.AreEqual(typeof(InvalidOperationException), ex.GetType());
}

using (IEnumerator dllEnumerator = dll.GetEnumerator())
{
Assert.AreEqual(dll.MoveLast(), false);
Assert.AreEqual(dll.MovePrevious(), false);
Assert.AreEqual(dllEnumerator.MoveNext(), false);
Assert.AreEqual(dllEnumerator.Current, default(int));
Assert.AreEqual(dll.Count, 0);
Assert.AreEqual(dll.CurrentIndex, -1);

try
{
dll.Pop();
}
catch (Exception ex)
{
Assert.AreEqual(typeof(InvalidOperationException), ex.GetType());
}

dll.Add(i1);
dll.MovePrevious();

Solution

More granular unit tests as per my comment above.

[TestClass]
    public sealed class DoublyLinkedListUnitTest
    {
        private IDoublyLinkedList valueDll;

        [TestInitialize]
        public void TestEmptyValueDllInitialize()
        {
            this.valueDll = new DoublyLinkedList();
        }

        [TestMethod]
        public void TestEmptyValueDllIndex()
        {
            Assert.AreEqual(-1, this.valueDll.Index);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestEmptyValueDllPeek()
        {
            var intValue = this.valueDll.Peek();
        }

        [TestMethod]
        public void TestEmptyValueDllMoveLast()
        {
            Assert.AreEqual(false, this.valueDll.MoveLast());
        }

        [TestMethod]
        public void TestEmptyValueDllMovePrevious()
        {
            Assert.AreEqual(false, this.valueDll.MovePrevious());
        }

        [TestMethod]
        public void TestEmptyValueDllEnumeratorMoveNext()
        {
            using (var dllEnumerator = this.valueDll.GetEnumerator())
            {
                Assert.AreEqual(false, dllEnumerator.MoveNext());
            }
        }

        [TestMethod]
        public void TestEmptyValueDllEnumeratorCurrent()
        {
            using (var dllEnumerator = this.valueDll.GetEnumerator())
            {
                Assert.AreEqual(default(int), dllEnumerator.Current);
            }
        }

        [TestMethod]
        public void TestEmptyValueDllCount()
        {
            Assert.AreEqual(0, this.valueDll.Count);
        }

        [TestMethod]
        public void TestEmptyValueDllCurrentIndex()
        {
            Assert.AreEqual(-1, this.valueDll.CurrentIndex);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestEmptyValueDllPop()
        {
            var intValue = this.valueDll.Pop();
        }

        [TestMethod]
        public void TestEmptyValueDllAdd()
        {
            const int I1 = 1;

            this.valueDll.Add(I1);
            this.TestEmptyValueDllIndex();
            Assert.AreEqual(1, this.valueDll.Count);
            Assert.AreEqual(0, this.valueDll.CurrentIndex);

            var i1 = this.valueDll.Peek(); // Technically not a "unit" test since we are relying on Peek() to work too.

            Assert.AreEqual(I1, i1);
        }

        //// Add more unit tests here.
    }
}

Code Snippets

[TestClass]
    public sealed class DoublyLinkedListUnitTest
    {
        private IDoublyLinkedList<int> valueDll;

        [TestInitialize]
        public void TestEmptyValueDllInitialize()
        {
            this.valueDll = new DoublyLinkedList<int>();
        }

        [TestMethod]
        public void TestEmptyValueDllIndex()
        {
            Assert.AreEqual(-1, this.valueDll.Index);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestEmptyValueDllPeek()
        {
            var intValue = this.valueDll.Peek();
        }

        [TestMethod]
        public void TestEmptyValueDllMoveLast()
        {
            Assert.AreEqual(false, this.valueDll.MoveLast());
        }

        [TestMethod]
        public void TestEmptyValueDllMovePrevious()
        {
            Assert.AreEqual(false, this.valueDll.MovePrevious());
        }

        [TestMethod]
        public void TestEmptyValueDllEnumeratorMoveNext()
        {
            using (var dllEnumerator = this.valueDll.GetEnumerator())
            {
                Assert.AreEqual(false, dllEnumerator.MoveNext());
            }
        }

        [TestMethod]
        public void TestEmptyValueDllEnumeratorCurrent()
        {
            using (var dllEnumerator = this.valueDll.GetEnumerator())
            {
                Assert.AreEqual(default(int), dllEnumerator.Current);
            }
        }

        [TestMethod]
        public void TestEmptyValueDllCount()
        {
            Assert.AreEqual(0, this.valueDll.Count);
        }

        [TestMethod]
        public void TestEmptyValueDllCurrentIndex()
        {
            Assert.AreEqual(-1, this.valueDll.CurrentIndex);
        }

        [TestMethod]
        [ExpectedException(typeof(InvalidOperationException))]
        public void TestEmptyValueDllPop()
        {
            var intValue = this.valueDll.Pop();
        }

        [TestMethod]
        public void TestEmptyValueDllAdd()
        {
            const int I1 = 1;

            this.valueDll.Add(I1);
            this.TestEmptyValueDllIndex();
            Assert.AreEqual(1, this.valueDll.Count);
            Assert.AreEqual(0, this.valueDll.CurrentIndex);

            var i1 = this.valueDll.Peek(); // Technically not a "unit" test since we are relying on Peek() to work too.

            Assert.AreEqual(I1, i1);
        }

        //// Add more unit tests here.
    }
}

Context

StackExchange Code Review Q#4911, answer score: 3

Revisions (0)

No revisions yet.