<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Martijn's C# Programming Blog &#187; nunit</title>
	<atom:link href="http://www.dijksterhuis.org/tag/nunit/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.dijksterhuis.org</link>
	<description>Information, news about programming in C#</description>
	<lastBuildDate>Fri, 07 Aug 2009 21:26:47 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Using Linked lists in C# &#8211; Part II</title>
		<link>http://www.dijksterhuis.org/using-linked-lists-in-c-part-ii/</link>
		<comments>http://www.dijksterhuis.org/using-linked-lists-in-c-part-ii/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 07:32:48 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Algorithms]]></category>
		<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[linked list]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=415</guid>
		<description><![CDATA[In a previous post I looked at how to build a very simple linked list in C#.. That class had many problems, including that it exposed quite a bit of its inner workings to the outside world. In this post I will introduce a C# generic LinkedList class that implements the Java LinkedList specification and [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>In <a href="http://www.dijksterhuis.org/using-linked-lists-in-c-part-i/">a previous post</a> I looked at how to build a very simple linked list in C#.. That class had many problems, including that it exposed quite a bit of its inner workings to the outside world. In this post I will introduce a C# generic LinkedList class that implements the Java LinkedList specification and behaves accordingly. I used this opportunity to explore how to apply unit testing with NUnit to effectively test the development process.</em></p>
<p><span id="more-415"></span></p>
<p>First of all, C# already has a generic LinkedList implementation, found in the System.Collections class. If you are looking for that class, the MSDN website <a href="http://msdn.microsoft.com/en-us/library/he2s3bh7.aspx">provides a solid introduction</a>.</p>
<p>After building a basic link list class in a previous post, I decided to extend it. To avoid cloning the C# System.Collections class completely I used the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/LinkedList.html">Java LinkedList specification</a> instead to roll my own. That meant implementing an iterator and methods such as AddFirst, AddLast, Clear, ToArray etc. To be able to support this the internal working of my earlier class had to be reworked substantially. I have included the complete code for reference below.</p>
<p>My main purpose for writing this code was to experiment with using NUnit. NUnit is a testing framework which allows you to create unit tests for your code. As you implement a method you write a small test to ensure that it works correctly. Because you are adding functionality to the class as you write new code the underlying methods might change in unexpected ways. Unit tests help you catch bugs introduced this way.</p>
<p>In earlier posts I commented on how to <a href="http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/">use NUnit if you are coding in Visual Studio C# 2008 Express</a> ; and <a href="http://www.dijksterhuis.org/using-nunit-with-monodevelop/">how to use NUnit if you are building your applications with MonoDevelop</a> so if you are having trouble setting this up please refer to these.</p>
<p><strong>Creating new and interesting bugs</strong></p>
<p>As a late optimization to my LinkedList class I added &#8220;Search Backwards&#8221; logic to the &#8220;Get(n)&#8221; method. </p>
<p>As you can guess, the &#8220;Get(n)&#8221; method returns the n<sup>th</sup> item in the list. To find it the n<sup>th</sup> element we need to step through the list from the start. If we work out that the item is closer the tail of the list we can save some time by stepping backwards through the list instead. Unit testing helped here as I had made a small bit significant error in how I calculated the distance from the tail. Several tests immediately failed as they now received unexpected results.</p>
<p><strong>Creating the Unit Tests</strong></p>
<p>The best place to put your tests is in a separate class file. As can be seen in the code snippet below the first thing we need to do is to include the NUnit.Framework in the code. The first thing to notice about this class is that it has a [TestFixture] attribute – this is how we indicate that the class contains test code. The class has to be public.  As we can run NUnit tests independently of your programs main routine, NUnit will simply load your programs classes and use the [TestFixture] attribute to discover any and all tests classes.</p>
<p>Each method containing a valid test has a [Test] attribute associated with it. Again, NUnit will simply scan the [TestFixture] classes for any method labeled with a [Test] attribute.</p>
<p>In the example below I included a very simple test for the Add() method of my linked list. After adding 2 items to the LinkedList, the total number of items has to be 2. Testing is done through the Assert class, if the Assert test fails, NUnit will flag the test as failed. Some other common Assert methods are: AreEqual(a,b) / AreNotEquels(a,b) / Equals  / IsTrue / IsFalse etc.</p>
<pre class="brush: c#">
using System;
using System.Text;
using NUnit.Framework;

namespace LinkedListJavaStyle
{
    /// &lt;summary&gt;
    /// LinkedList_NUnit is a class implementing a set of unit tests for each of the methods supported by the LinkedList class
    /// &lt;/summary&gt;

    [TestFixture]
    public class LinkedList_NUnit
    {

        [Test]
        public void TestLinkedList_add()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Assert.AreEqual(2, Test.Size());
        }
}
</pre>
<p>The complete LinkedListClass.cs and set of tests (LinkedList_NUnit.cs) are given below: </p>
<p><strong>LinkedList.cs</strong></p>
<pre class="brush: c#">
using System;
using System.Text;
using NUnit.Framework;

namespace LinkedListJavaStyle
{
    /// &lt;summary&gt;
    /// LinkedList_NUnit is a class implementing a set of unit tests for each of the methods supported by the LinkedList class
    /// &lt;/summary&gt;

    [TestFixture]
    public class LinkedList_NUnit
    {

        [Test]
        public void TestLinkedList_add()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Assert.AreEqual(2, Test.Size());
        }
}

The complete LinkedListClass and set of tests are given below:

// LinkedList.cs created with MonoDevelop
// User: martijn at 10:02 AM 12/30/2008

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace LinkedListJavaStyle
{

    /// &lt;summary&gt;
    /// LinkedListElement is the memory structure used by the LinkedList class for storing each list cell.
    /// Every element contains a pointer to the previous and next element and a copy of the data.
    /// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;T&quot;&gt;The same as for LinkedList&lt;/typeparam&gt;

    class LinkedListNode&lt;T&gt;
    {
        public T Data;
        public LinkedListNode&lt;T&gt; Prev;
        public LinkedListNode&lt;T&gt; Next;

        public LinkedListNode(T Data)
        {
            this.Data = Data;
        }
    }

    /// &lt;summary&gt;
    /// Linked List implementation following the model layed out by the Java LinkedList Class. Implements all list operations and permits
    /// all elements (including null). Operations that need to enter the list will travel through the list from beginning to end until
    /// the required offset is found (or from the tail if the distance is shorter).
    ///
    /// The class is implemented as a double linked list.
    /// &lt;/summary&gt;
    /// &lt;typeparam name=&quot;T&quot;&gt;This class accepts any type you would like to use for the generic.&lt;/typeparam&gt;

    class LinkedList&lt;T&gt;
    {

        LinkedListNode&lt;T&gt; Head;
        LinkedListNode&lt;T&gt; Tail;
        int LinkedList_Size;

        /// ---------------------------------------------------------------------------------------
        /// &lt;summary&gt;
        /// Constructor
        /// &lt;/summary&gt;

        public LinkedList()
        {
            Head = null;
            Tail = null;
            LinkedList_Size = 0;
        }

        /// &lt;summary&gt;
        /// Constructs a list containing the elements of the specified collection, in the order that they are
        /// returned by the collections itterator.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;collection&quot;&gt;An array of type T containing the elements to be added to the list&lt;/param&gt;

        public LinkedList(T[] collection)
        {
            Head = null;
            Tail = null;
            LinkedList_Size = 0;
            AddAll_Internal(Tail, collection);
        }

        /// &lt;summary&gt;
        /// Private function which inserts a T element before the offset given in the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;offset&quot;&gt;A LinkedListElement before which the new elements needs to be inserted&lt;/param&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element to be inserted&lt;/param&gt;
        /// &lt;returns&gt;A reference to the newly created LinkedListElement&lt;/returns&gt;

        private LinkedListNode&lt;T&gt; Add_Internal_Before(LinkedListNode&lt;T&gt; offset, T element)
        {
            if (offset == null)
                throw new Exception(&quot;Add_Internal: null is not a valid argument&quot;);

            LinkedListNode&lt;T&gt; Listelement = new LinkedListNode&lt;T&gt;(element);

            Listelement.Prev = offset.Prev;
            Listelement.Next = offset;

            if (offset.Prev != null) offset.Prev.Next = Listelement;
            offset.Prev = Listelement;

            if (offset == Head)
                Head = Listelement;

            LinkedList_Size++;

            return Listelement;
        }

        /// &lt;summary&gt;
        /// Private function which inserts a T element after the offset given in the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;offset&quot;&gt;The LinkedListElement after which we need to insert the new element&lt;/param&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element to be inserted&lt;/param&gt;
        /// &lt;returns&gt;A reference to the newly created LinkListElement&lt;/returns&gt;

        private LinkedListNode&lt;T&gt; Add_Internal_After(LinkedListNode&lt;T&gt; offset, T element)
        {
            if (offset == null)
                throw new Exception(&quot;Add_Internal: null is not a valid argument&quot;);

            LinkedListNode&lt;T&gt; Listelement = new LinkedListNode&lt;T&gt;(element);

            Listelement.Prev = offset;
            Listelement.Next = offset.Next;
            if (offset.Next != null) offset.Next.Prev = Listelement;
            offset.Next = Listelement;

            if (offset == Tail)
                Tail = Listelement;

            LinkedList_Size++;

            return Listelement;
        }

        /// &lt;summary&gt;
        /// Adds the specified element at the specified position in the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;Index&quot;&gt;The index at which to insert the element&lt;/param&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element of T to insert&lt;/param&gt;
        /// &lt;returns&gt;True as per Java implementation&lt;/returns&gt;

        public bool Add(int Index, T element)
        {
            if ((Index == 0) &amp;&amp; (Head == null))
                AddFirst(element);
            else
                Add_Internal_Before(Get_Internal(Index), element);
            return true;
        }

        /// &lt;summary&gt;
        /// Adds the element to the end of the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element of type T to insert&lt;/param&gt;

        public void Add(T element)
        {
            if (Head == null)
                AddFirst(element);
            else
                Add_Internal_After(Tail, element);
        }

        /// &lt;summary&gt;
        /// Adds the element at the beginning of the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element to add to the list&lt;/param&gt;

        public void AddFirst(T element)
        {
            LinkedListNode&lt;T&gt; ListElement = new LinkedListNode&lt;T&gt;(element);
            if (Tail == null)
            {
                Tail = ListElement;
            }
            else
            {
                ListElement.Next = Head;
                Head.Prev = ListElement;
            }
            Head = ListElement;
            LinkedList_Size++;
        }

        /// &lt;summary&gt;
        /// Adds the given element to the end of the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;Element to add to the list&lt;/param&gt;

        public void AddLast(T element)
        {
            LinkedListNode&lt;T&gt; Listelement = new LinkedListNode&lt;T&gt;(element);
            if (Head == null)
            {
                Head = Listelement;
            }
            else
            {
                Listelement.Prev = Tail;
                Tail.Next = Listelement;
            }
            Tail = Listelement;
            LinkedList_Size++;
        }

        /// &lt;summary&gt;
        /// Private function which adds all the elements in the given collection to the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;start&quot;&gt;A LinkedListElement giving the starting point in the list&lt;/param&gt;
        /// &lt;param name=&quot;collection&quot;&gt;Elements to add to the list&lt;/param&gt;

        private void AddAll_Internal(LinkedListNode&lt;T&gt; start, T[] collection)
        {
            // The set of elements is inserted before start, but then needs to follow the
            // newly inserted first element.
            bool First = true;
            foreach (T element in collection)
            {
                if (Head == null)
                {
                    AddFirst(element);
                    start = Head;
                    First = false;
                }
                else
                {
                    if (First)
                    {
                        start = Add_Internal_Before(start, element);
                        First = false;
                    }
                    else
                        start = Add_Internal_After(start, element);
                }
            }
        }

        /// &lt;summary&gt;
        /// Adds all the elements in the collection to the end of the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;collection&quot;&gt;An array of T with all the elements to add&lt;/param&gt;

        public void AddAll(T[] collection)
        {
            // Obtain the element at the given offset
            AddAll_Internal(Tail, collection);
        }

        /// &lt;summary&gt;
        /// Adds all the elements in the collection to the list from the end
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;collection&quot;&gt;An array of T with all the elements to add&lt;/param&gt;

        public void AddAll(int index, T[] collection)
        {
            AddAll_Internal(Get_Internal(index), collection);
        }

        /// &lt;summary&gt;
        /// Empties the list by unlinking all elements.
        /// &lt;/summary&gt;

        public void Clear()
        {
            LinkedListNode&lt;T&gt; Temp;

            while (Head != null)
            {
                Temp = Head;
                Head = Head.Next;

                Temp.Prev = null;
                Temp.Next = null;
            }

            Tail = null;
            LinkedList_Size = 0;
        }

        /// &lt;summary&gt;
        /// Returns a copy of the LinkedList containing all the elements.
        /// &lt;/summary&gt;
        /// &lt;returns&gt;A LinkedList of the same type&lt;/returns&gt;

        public LinkedList&lt;T&gt; Clone()
        {
            LinkedList&lt;T&gt; ClonedCopy = new LinkedList&lt;T&gt;();

            LinkedListNode&lt;T&gt; Iterator = Head;
            while (Iterator != null)
            {
                ClonedCopy.AddLast(Iterator.Data);
                Iterator = Iterator.Next;
            }
            return ClonedCopy;
        }

        // Contains
        // Returns true if the list contains the specified element

        public bool Contains(T needle)
        {
            LinkedListNode&lt;T&gt; Iterator = Head;
            while (Iterator != null)
            {
                // Check for null in the needle
                if ((needle == null)&amp;&amp;(Iterator.Data == null))
                  return true;

                // Check for null in the list
                if (Iterator.Data != null)
                    if (Iterator.Data.Equals(needle))
                        return true;

                // Not found, continue to the next entry
                Iterator = Iterator.Next;
            }
            return false;
        }

        /// &lt;summary&gt;
        /// Returns the number of elements in this list
        /// &lt;/summary&gt;
        /// &lt;returns&gt;the number of elements in the list&lt;/returns&gt;

        public int Size()
        {
            return LinkedList_Size;
        }

        /// &lt;summary&gt;
        /// An internal function which travels the list to find the specified index.
        /// The function travels forward/backward depending on whether the head or
        /// tail is closer to the specified index.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;Index&quot;&gt;The position in the list to find.&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;

        private LinkedListNode&lt;T&gt; Get_Internal(int Index)
        {
            if ((LinkedList_Size == 0) || (Index &lt; 0) || (Index &gt;= LinkedList_Size))
                throw new IndexOutOfRangeException(&quot;LinkedList&lt;T&gt; Invalid Index&quot;);

            LinkedListNode&lt;T&gt; iterator;
            int DistanceFromHead = Index;
            int DistanceFromTail = LinkedList_Size - Index -1;  

            if (DistanceFromHead &lt; DistanceFromTail)
            {
                // We travel forward if the index is closer to the beginning
                iterator = Head;
                while (iterator != null &amp;&amp; DistanceFromHead-- &gt; 0)
                    iterator = iterator.Next;
            }
            else
            {
                // We travel backward if the index is closer to the end
                iterator = Tail;
                while (iterator != null &amp;&amp; DistanceFromTail-- &gt; 0)
                    iterator = iterator.Prev;
            }

            return iterator;
        }

        /// &lt;summary&gt;
        /// Returns the value of the specified element in the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;index&quot;&gt;The index item to return&lt;/param&gt;
        /// &lt;returns&gt;The value of the element specified &lt;/returns&gt;
        /// &lt;throws&gt;IndexOutOfBoundsException if the index is out of range (smaller than 0 or index &gt;= size()&lt;/throws&gt;

        public T Get(int index)
        {
            return Get_Internal(index).Data;
        }

        /// &lt;summary&gt;
        /// Returns the value stored at the head of the list
        /// &lt;/summary&gt;
        /// &lt;returns&gt;The value stored at the head of the list&lt;/returns&gt;

        public T GetFirst()
        {
            if (LinkedList_Size == 0)
                throw new IndexOutOfRangeException(&quot;LinkedList&lt;T&gt; Invalid Index&quot;);
            return Head.Data;
        }

        /// &lt;summary&gt;
        /// Returns the value stored at the tail of the list
        /// &lt;/summary&gt;
        /// &lt;returns&gt;The value stored at the tail of the list&lt;/returns&gt;

        public T GetLast()
        {
            if (LinkedList_Size == 0)
                throw new IndexOutOfRangeException(&quot;LinkedList&lt;T&gt; Invalid Index&quot;);
            return Tail.Data;
        }

        /// &lt;summary&gt;
        /// Search for the given element in the list and return the LinkedListNode
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;Element to search for&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;

        private LinkedListNode&lt;T&gt; IndexOf_Internal(T element)
        {
            int Index = 0;
            LinkedListNode&lt;T&gt; Iterator = Head;
            while (Iterator != null)
            {
                // Check for null in the needle
                if ((element == null) &amp;&amp; (Iterator.Data == null))
                    return Iterator;

                // Check for null in the list
                if (Iterator.Data != null)
                    if (Iterator.Data.Equals(element))
                        return Iterator;

                Index++;
                Iterator = Iterator.Next;
            }
            return null;
        }

        /// &lt;summary&gt;
        /// This routine searches the linked list for the first occurance of the specified element
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;The value to look for, null is also possible&lt;/param&gt;
        /// &lt;returns&gt;The index of the item searched, or -1 if not found&lt;/returns&gt;

        public int IndexOf(T element)
        {
            int Index = 0;
            LinkedListNode&lt;T&gt; Iterator = Head;
            while (Iterator != null)
            {
                // Check for null in the needle
                if ((element == null) &amp;&amp; (Iterator.Data == null))
                    return Index;

                // Check for null in the list
                if (Iterator.Data != null)
                    if (Iterator.Data.Equals(element))
                        return Index;

                Index++;
                Iterator = Iterator.Next;
            }
            return -1;
        }

        /// &lt;summary&gt;
        /// Returns the last occurance of the specified element in the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;The value to search for&lt;/param&gt;
        /// &lt;returns&gt;The index of the element, or -1 if not found&lt;/returns&gt;

        public int LastIndexOf(T element)
        {
            int Index = LinkedList_Size;
            LinkedListNode&lt;T&gt; Iterator = Tail;
            while (Iterator != null)
            {
                Index--;

                // Check for null in the element
                if ((element == null) &amp;&amp; (Iterator.Data == null))
                    return Index;

                // Check for null in the list before checking the content
                if (Iterator.Data != null)
                    if (Iterator.Data.Equals(element))
                        return Index;

                Iterator = Iterator.Prev;
            }
            return -1;
        }

        /// &lt;summary&gt;
        /// An internal function which unlinks a specified link in the list chain.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;A reference to the list item to unlink&lt;/param&gt;

        public void Remove_Internal(LinkedListNode&lt;T&gt; element)
        {
            if (element == null)
                throw new Exception(&quot;LinkedListNode&lt;T&gt; null is not a valid argument&quot;);

            if (element == Head)
                Head = element.Next;

            if (element == Tail)
                Tail = element.Prev;

            if (element.Prev != null)
                element.Prev.Next = element.Next;

            if (element.Next != null)
                element.Next.Prev = element.Prev;

            LinkedList_Size--;
        }

        /// &lt;summary&gt;
        /// This function removes the specified index item from the list
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;Index&quot;&gt;&lt;/param&gt;
        /// &lt;throws&gt;IndexOutOfRangeException if an invalid index is given&lt;/throws&gt;
        /// &lt;returns&gt;&lt;/returns&gt;

        public T Remove(int Index)
        {
            LinkedListNode&lt;T&gt; Element = Get_Internal(Index);

            T Data = Element.Data;

            Remove_Internal(Element);

            return Data;
        }

        /// &lt;summary&gt;
        /// Searches the list for the first occurance of the specified element, and removes it.
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;element&quot;&gt;Element to remove&lt;/param&gt;
        /// &lt;returns&gt;True if the element was found and removed, False if not found&lt;/returns&gt;

        public bool Remove(T element)
        {
            LinkedListNode&lt;T&gt; Node = IndexOf_Internal(element);

            // Find the element offset
            if (Node != null)
            {
                // This is bad -- we walk the list twice -- need to optimize
                Remove_Internal(Node);
                return true;
            }

            return false;
        }

        /// &lt;summary&gt;
        /// This routine removes the first element from the list
        /// &lt;/summary&gt;
        /// &lt;returns&gt;The value of the element removed&lt;/returns&gt;
        /// &lt;throws&gt;IndexOutofRangException if the list is empty&lt;/throws&gt;

        public T RemoveFirst()
        {
            if (LinkedList_Size == 0)
                throw new IndexOutOfRangeException(&quot;LinkedList&lt;T&gt; Empty List&quot;);

            T Data = Head.Data;

            Remove_Internal(Head);

            return Data;
        }

        /// &lt;summary&gt;
        /// Remove the last element of the list
        /// &lt;/summary&gt;
        /// &lt;returns&gt;The value of the element removed&lt;/returns&gt;
        /// &lt;throws&gt;IndexOutofRangException if the list is empty&lt;/throws&gt;

        public T RemoveLast()
        {
            if (LinkedList_Size == 0)
                throw new IndexOutOfRangeException(&quot;LinkedList&lt;T&gt; Empty List&quot;);

            T Data = Tail.Data;

            Remove_Internal(Tail);

            return Data;
        }

        /// &lt;summary&gt;
        /// This routine inserts the given element at the given index
        /// &lt;/summary&gt;
        /// &lt;param name=&quot;Index&quot;&gt;The index to insert the element at&lt;/param&gt;
        /// &lt;param name=&quot;element&quot;&gt;The element to insert&lt;/param&gt;
        /// &lt;returns&gt;&lt;/returns&gt;

        public T Set(int Index, T element)
        {
            LinkedListNode&lt;T&gt; OldListEntry = Get_Internal(Index);

            // Keep a pointer to the old data object
            T Temp = OldListEntry.Data;

            // Swap it with the new data object
            OldListEntry.Data = element;

            // And return the old one
            return Temp;
        }

        /// &lt;summary&gt;
        /// Returns an array containing all the elements in the list in the head-tail order
        /// &lt;/summary&gt;
        /// &lt;returns&gt;An array of T[] containing all the elements in the list&lt;/returns&gt;

        public T[] ToArray()
        {
            LinkedListNode&lt;T&gt; iterator = Head;
            T[] DataArray = new T[LinkedList_Size];
            int Index = 0;

            while (iterator != null)
            {
                DataArray[Index++] = iterator.Data;
                iterator = iterator.Next;
            }

            return DataArray;
        }

        /// &lt;summary&gt;
        /// Returns a list iterator of the elements in this list implementing the IENumerator interface.
        /// &lt;/summary&gt;
        /// &lt;returns&gt;IENumerator &lt;/returns&gt;

        public IEnumerator&lt;T&gt; GetEnumerator()
        {
            return new LinkedListEnumerator(this);
        }

        /// &lt;summary&gt;
        /// The LinkedListEnumerator is an nested class implementing the IENumerator interface. Making it
        /// possible to call foreach() on the list.
        /// &lt;/summary&gt;

        class LinkedListEnumerator : IEnumerator&lt;T&gt;
        {
            LinkedListNode&lt;T&gt; start;
            LinkedListNode&lt;T&gt; current;

            public LinkedListEnumerator(LinkedList&lt;T&gt; list)
            {
                start = list.Head;
                current = null;
            }
            public bool MoveNext()
            {
                if (current == null) current = start;
                else current = current.Next;
                return (current != null);
            }

            public T Current
            {
                get
                {
                    if (current == null)
                        throw new InvalidOperationException();
                    return current.Data;
                }
            }

            object IEnumerator.Current
            {
                get
                {
                    if (current == null)
                        throw new InvalidOperationException();
                    return current.Data;
                }
            }

            public void Dispose() { }
            public void Reset() { current = null; }
        }

    }

}
</pre>
<p><strong>LinkedList_NUnit.cs</strong></p>
<pre class="brush: c#">
using System;
using System.Text;
using NUnit.Framework;

namespace LinkedListJavaStyle
{
    /// &lt;summary&gt;
    /// LinkedList_NUnit is a class implementing a set of unit tests for each of the methods supported by the LinkedList class
    /// &lt;/summary&gt;

    [TestFixture]
    public class LinkedList_NUnit
    {

        [TestX]
        public void TestLinkedList_add()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Assert.AreEqual(2, Test.Size());
        }

        [Test]
        public void TestLinkedList_Add_AtIndex()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Test.Add(&quot;Boom Boom&quot;);
            Test.Add(2, &quot;Much Wiser&quot;);
            Assert.AreEqual(4, Test.Size());
            string Value = Test.Get(2);
            Assert.AreEqual(&quot;Much Wiser&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Add_AtFirst()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Test.AddFirst(&quot;Much Wiser&quot;);
            Assert.AreEqual(3, Test.Size());
            string Value = Test.Get(0);
            Assert.AreEqual(&quot;Much Wiser&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Add_AtLast()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            Test.Add(&quot;Hello World&quot;);
            Test.Add(&quot;The world today&quot;);
            Test.AddLast(&quot;Much Wiser&quot;);
            Assert.AreEqual(3, Test.Size());
            string Value = Test.Get(2);
            Assert.AreEqual(&quot;Much Wiser&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Add_All()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            Assert.AreEqual(3, Test.Size());
            string Value = Test.Get(1);
            Assert.AreEqual(&quot;The World Today&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Add_AllFromIndex()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            string[] Input2 = { &quot;Monty Python&quot;, &quot;Flying Circus&quot; };
            Test.AddAll(Input);
            Assert.AreEqual(3, Test.Size());
            Test.AddAll(1, Input2);
            string Value = Test.Get(2);
            Assert.AreEqual(&quot;Flying Circus&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Clear()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            Test.Clear();
            Assert.AreEqual(0, Test.Size());
        }

        [Test]
        public void TestLinkedList_Clone()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            LinkedList&lt;string&gt; Test2;
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            Test2 = Test.Clone();
            Test.Clear();
            Assert.AreEqual(3, Test2.Size());
            string Value = Test2.Get(1);
            Assert.AreEqual(&quot;The World Today&quot;, Value);
        }

        [Test]
        public void TestLinkedList_Contains()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, null, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            Assert.IsTrue(Test.Contains(&quot;Hello World&quot;));
            Assert.IsFalse(Test.Contains(&quot;Botany Bay&quot;));
            Assert.IsTrue(Test.Contains(null));
        }

        [Test]
        public void TestLinkedList_Get()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            string Value = Test.Get(2);
            Assert.AreEqual(&quot;Much Wiser&quot;, Value);
            Assert.
        }

        [Test]
        public void TestLinkedList_GetFirst()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            string Value = Test.GetFirst();
            Assert.AreEqual(&quot;Hello World&quot;, Value);
        }

        [Test]
        public void TestLinkedList_GetLast()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            string Value = Test.GetLast();
            Assert.AreEqual(&quot;Much Wiser&quot;, Value);
        }

        [Test]
        public void TestLinkedList_IndexOf()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, null, &quot;Much Wiser&quot; };
            Test.AddAll(Input);
            int IndexValue = Test.IndexOf(&quot;The World Today&quot;);
            Assert.AreEqual(1, IndexValue);
            int IndexValue2 = Test.IndexOf(&quot;My Day In the Sun&quot;);
            Assert.AreEqual(-1, IndexValue2);
            int IndexValue3 = Test.IndexOf(null);
            Assert.AreEqual(2, IndexValue3);
        }

        [Test]
        public void TestLinkedList_LastIndexOf()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, null, &quot;Hello World&quot;, null };
            Test.AddAll(Input);
            int IndexValue = Test.LastIndexOf(&quot;Hello World&quot;);
            Assert.AreEqual(3, IndexValue);
            int IndexValue2 = Test.LastIndexOf(&quot;My Day In the Sun&quot;);
            Assert.AreEqual(-1, IndexValue2);
            int IndexValue3 = Test.LastIndexOf(null);
            Assert.AreEqual(4, IndexValue3);
        }

        [Test]
        public void TestLinkedList_listIterator()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            // The array is empty, and we should never enter this part
            foreach (string token in Test)
                Assert.Fail(&quot;Entered empty iterator&quot;);

            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Hello World&quot; };

            Test.AddAll(Input);

            // Match the contents against the string[]
            int offset = 0;
            foreach (string token in Test)
                Assert.AreEqual(token, Input[offset++]);
        }

        [Test]
        public void TestLinkedList_remove()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot; };
            Test.AddAll(Input);
            Test.Remove(1);
            // There are only 2 elements left
            Assert.AreEqual(Test.Size(), 2);
            // Before the removal this was Position 2
            int IndexValue1 = Test.IndexOf(&quot;Its going to be a fine day&quot;);
            Assert.AreEqual(IndexValue1, 1);
        }

        [Test]
        public void TestLinkedList_RemoveObject()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot; };
            Test.AddAll(Input);
            Assert.AreEqual(Test.Remove(&quot;Its going to be a fine day&quot;), true);
            // There are only 2 elements left
            Assert.AreEqual(Test.Size(), 2);
            // We should not be able to find this element
            Assert.AreEqual(Test.IndexOf(&quot;Its going to be a fine day&quot;), -1);
        }

        [Test]
        public void TestLinkedList_removeFirst()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot; };
            Test.AddAll(Input);
            Test.RemoveFirst();
            // There are only 2 elements left
            Assert.AreEqual(Test.Size(), 2);
            // Before the removal this was Position 1
            int IndexValue1 = Test.IndexOf(&quot;The World Today&quot;);
            Assert.AreEqual(IndexValue1, 0);
        }

        [Test]
        public void TestLinkedList_removeLast()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot; };
            Test.AddAll(Input);

            // We should not be able to find this now
            int IndexValue1 = Test.IndexOf(&quot;Its going to be a fine day&quot;);
            Assert.AreEqual(IndexValue1, 2);

            // Remove the last entry
            Test.RemoveLast();

            // There are only 2 elements left
            Assert.AreEqual(Test.Size(), 2);

            // We should not be able to find this now
            int IndexValue2 = Test.IndexOf(&quot;Its going to be a fine day&quot;);
            Assert.AreEqual(IndexValue2, -1);
        }

        [Test]
        public void TestLinkedList_Set()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot; };
            Test.AddAll(Input);

            // Replace position #1 with a new object
            string OldValue = Test.Set(1, &quot;Sunday Sports&quot;);
            Assert.AreEqual(OldValue, &quot;The World Today&quot;);

            int IndexValue2 = Test.IndexOf(&quot;Sunday Sports&quot;);
            Assert.AreEqual(IndexValue2, 1);
        }

        [Test]
        public void TestLinkedList_ToArray()
        {
            LinkedList&lt;string&gt; Test = new LinkedList&lt;string&gt;();
            string[] Input = { &quot;Hello World&quot;, &quot;The World Today&quot;, &quot;Its going to be a fine day&quot;, null };
            Test.AddAll(Input);

            string[] Copy = Test.ToArray();

            int Index = 0;
            foreach (string element in Input)
                Assert.AreEqual(element, Copy[Index++]);
        }

    }

}
</pre>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/using-linked-lists-in-c-part-ii/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using NUnit with MonoDevelop</title>
		<link>http://www.dijksterhuis.org/using-nunit-with-monodevelop/</link>
		<comments>http://www.dijksterhuis.org/using-nunit-with-monodevelop/#comments</comments>
		<pubDate>Tue, 30 Dec 2008 04:29:04 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Beginner]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[monodevelop]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=403</guid>
		<description><![CDATA[Unit testing with NUnit is deeply integrated into Mono. The Mono code itself uses NUnit extensively to test its own functionality. In this post we look at how you can enable unit testing for your own code under MonoDevelop. In an earlier post I looked at how to install NUnit for Visual Studio 2008 Express, [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><em>Unit testing with NUnit is deeply integrated into Mono. The Mono code itself uses NUnit extensively to test its own functionality. In this post we look at how you can enable unit testing for your own code under MonoDevelop. In an earlier post I looked at<a href="http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/"> how to install NUnit for Visual Studio 2008 Express</a>, in this post we do the same for MonoDevelop.</em></p>
<p><span id="more-403"></span></p>
<p><strong>Creating your first NUnit test</strong></p>
<p>We need to create a new solution to test if this works and we need to add a reference to NUnit to be able to use the code.  This is the only &#8220;difficult&#8221; bit, the MonoDevelop documentation is sparse at best on how to include external assemblies. A dedicated menu option would have been nice.</p>
<p>To be able to add an assembly or package to an existing project you need to right click the References item on the Solution panel and then click &#8220;Edit References&#8221;. Click the &#8220;NUnit.Core&#8221; and &#8220;NUnit.Framework&#8221; selections to include them into your solution.</p>
<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2008/12/screenshot.png"><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/12/screenshot-500x387.png" alt="Including NUnit as an External Assembly to MonoDevelop" title="Including NUnit as an External Assembly to MonoDevelop" width="500" height="387" class="alignnone size-medium wp-image-404" /></a></p>
<p><strong>Testing the NUnit Demo code</strong></p>
<p>Below I have included a little bit of demo code that tests if this works properly. The code is mostly borrowed from the NUnit website demo, I have just for the sake of it included a class containing “Main” so that the code actually compiles and runs.</p>
<pre class="brush: c#">
using System;
using NUnit.Framework;

namespace bank
{
    public class Account
    {
        private float balance;
        public void Deposit(float amount)
        {
            balance += amount;
        }

        public void Withdraw(float amount)
        {
            balance -= amount;
        }

        public void TransferFunds(Account destination, float amount)
        {
        }

        public float Balance
        {
            get { return balance; }
        }
    }
}

namespace bank
{
    [TestFixture]
    public class AccountTest
    {
        [Test]
        public void TransferFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Account destination = new Account();
            destination.Deposit(150.00F);

            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);

        }

        [Test]
        public void DepositFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Assert.AreEqual(200.00F, source.Balance);
        }

    }
}

namespace UnitTestDemo
{
    public class MyAccountingSoftware
    {

        public static void Main()
        {
            bank.Account DemoAccount = new bank.Account();
            DemoAccount.Deposit(1000.00F);
            DemoAccount.Withdraw(500.50F);
            Console.WriteLine(&quot;Our account balance is {0}&quot;, DemoAccount.Balance);
        }

    }
}
</pre>
<p>In the above code the actual unit testing is done by the “AccountTest” class, for simplicity we keep this in the same file but usually you would keep a separate directory with all of your unit tests, separate from the actual code.Note that because the “TransferFunds” function in the code above is poorly implemented (eg. its empty) the Unit test will hopefully (!!) fail. The second unit test in DepositFunds should succeed.</p>
<p><strong>How to run NUnit tests from within MonoDevelop</strong></p>
<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2008/12/screenshot-1.png"><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/12/screenshot-1-500x290.png" alt="Running NUnit from within MonoDevelop" title="Running NUnit from within MonoDevelop" width="500" height="290" class="alignnone size-medium wp-image-407" /></a></p>
<p>MonoDevelop allows you to run the NUnit tests directly in the GUI environment. Select &#8220;View > Unit Tests&#8221; to see an overview of all the defined tests in the left panel. Right click on a test , or a group of tests and select &#8220;Run Test&#8221;. Click on <em>&#8220;View > NUnit Test Results&#8221;</em> to see which tests passed and which failed.</p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/using-nunit-with-monodevelop/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Setting up NUnit for C# Unit Testing with Visual Studio C# Express 2008</title>
		<link>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/</link>
		<comments>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/#comments</comments>
		<pubDate>Thu, 27 Nov 2008 04:50:43 +0000</pubDate>
		<dc:creator>Martijn</dc:creator>
				<category><![CDATA[Intermediate]]></category>
		<category><![CDATA[Learn C#]]></category>
		<category><![CDATA[nunit]]></category>
		<category><![CDATA[unit testing]]></category>
		<category><![CDATA[visual studio express C# 2008]]></category>

		<guid isPermaLink="false">http://www.dijksterhuis.org/?p=292</guid>
		<description><![CDATA[
Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order.  I am currently writing some code that needs [...]<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></description>
			<content:encoded><![CDATA[<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/unittesting.gif" alt="" title="Unit Testing with Visual Studio Express 2008" width="500" height="278" class="alignnone size-full wp-image-302" /></p>
<p><em>Unit testing helps you verify that each individual part of your code is working as expected and keeps doing that as you change your software. You do this by adding small bits of testing code and have the unit testing frame work execute them in order.  I am currently writing some code that needs to have a basic unit testing framework and wanted to install the NUnit framework. This post is about how you can install NUnit, use it and run it with / install it for Visual Studio C# 2008 Express. I will leave the nuts and bolts of unit testing to a future post but this post should get you up and running.<br />
</em></p>
<p><span id="more-292"></span></p>
<p>Installing NUnit itself is straightforward and only requires a few steps:</p>
<ul>
<li>Download the NUnit framework from the <a href="http://www.nunit.org/index.php?p=download">NUnit website</a> (download <a href="http://prdownloads.sourceforge.net/nunit/NUnit-2.4.8-net-2.0.msi?download">NUnit-2.4.8-net-2.0.msi</a>)
<li>Install the software on your Windows XP / Vista machine
</ul>
<p><strong>Creating your first NUnit test</strong></p>
<p>Now we need to create a new project to test if this works, and we need to add a reference to NUnit to be able to use the code.</p>
<p>Click &#8220;Project > Add Reference&#8230;&#8221;</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit1.jpg" alt="" title="Adding NUnit To Visual Studio Express 2008" width="400" height="291" class="alignnone size-full wp-image-293" /></p>
<p>Select &#8220;Browse&#8221; and navigate to &#8220;C:\Program Files\NUnit 2.4.8\bin&#8221; and select the <strong>nunit.framework.dll</strong>.</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit2.gif" alt="" title="Adding the NUnit Framework DLL to Visual Studio C# Express 2008" width="400" height="300" class="alignnone size-full wp-image-294" /></p>
<p>This allows you to use NUnit.Framework in your programs. </p>
<p><strong>Testing the NUnit Demo code</strong></p>
<p>Below I have included a little bit of demo code that tests if this works properly. The code is mostly borrowed from the NUnit website demo, I have just for the sake of it included a class containing &#8220;Main&#8221; so that the code actually compiles and runs.</p>
<pre class="brush: c#">
using System;
using NUnit.Framework;

namespace bank
{
    public class Account
    {
        private float balance;
        public void Deposit(float amount)
        {
            balance += amount;
        }

        public void Withdraw(float amount)
        {
            balance -= amount;
        }

        public void TransferFunds(Account destination, float amount)
        {
        }

        public float Balance
        {
            get { return balance; }
        }
    }
}

namespace bank
{
    [TestFixture]
    public class AccountTest
    {
        [Test]
        public void TransferFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Account destination = new Account();
            destination.Deposit(150.00F);

            source.TransferFunds(destination, 100.00F);
            Assert.AreEqual(250.00F, destination.Balance);
            Assert.AreEqual(100.00F, source.Balance);

        }

        [Test]
        public void DepositFunds()
        {
            Account source = new Account();
            source.Deposit(200.00F);
            Assert.AreEqual(200.00F, source.Balance);
        }

    }
}

namespace UnitTestDemo
{
    public class MyAccountingSoftware
    {

        public static void Main()
        {
            bank.Account DemoAccount = new bank.Account();
            DemoAccount.Deposit(1000.00F);
            DemoAccount.Withdraw(500.50F);
            Console.WriteLine(&quot;Our account balance is {0}&quot;, DemoAccount.Balance);
        }

    }
}
</pre>
<p>In the above code the actual unit testing is done by the &#8220;AccountTest&#8221; class, for simplicity we keep this in the same file but usually you would keep a separate directory with all of your unit tests, separate from the actual code. </p>
<p>Note that because the &#8220;<strong>WithDraw</strong>&#8221; function in the code above is poorly implemented (eg. its empty) the Unit test will hopefully (!!) fail. The second unit test in <strong>DepositFunds</strong> should succeed.</p>
<p><strong>How to run the NUnit GUI runner from within Visual Studio C# Express 2008</strong></p>
<p>We can run NUnit from the Menu option it installed under [Start/Programs/NUnit] but it is more convenient to do so from Visual Studio Express itself. </p>
<p>Visual Studio C# Express allows you to define outside &#8220;Tools&#8221; which can be run from the menu. This is exactly what we would like to do with the NUnit GUI Runner. The GUI Runner is a tool which loads our program and executes all the unit tests it finds and reports the results in a seperate window.</p>
<p>Click on &#8220;Tools > External Tools&#8230;&#8221; to add NUnit&#8217;s GUI Runner.</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit3.gif" alt="" title="Adding NUnit to the VisuaL Studio Express Tools Menu" width="500" height="191" class="alignnone size-full wp-image-296" /></p>
<p>The following definition allows us to run the tests in our current project:</p>
<p><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit4.gif" alt="" title="Starting NUnit from the Visual Studio Tools Menu" width="460" height="486" class="alignnone size-full wp-image-299" /></p>
<p>To run the unit test, simply click &#8220;NUnit&#8221; from the Tools Menu.</p>
<p><a href="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit5.gif"><img src="http://www.dijksterhuis.org/wp-content/uploads/2008/11/nunit5-300x243.gif" alt="" title="Running NUnit with our Visual Studio C# project" width="300" height="243" class="alignnone size-medium wp-image-300" /></a></p>
<p>[Click the picture for a larger image] </p>
<p>If everything went correctly you should see NUnit run 2 tests, one of which will fail. </p>
<p>This is a post from <a href="http://www.dijksterhuis.org">Martijn's C# Coding Blog</a>. </p>
]]></content:encoded>
			<wfw:commentRss>http://www.dijksterhuis.org/setting-up-nunit-for-c-unit-testing-with-visual-studio-c-express-2008/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
