The Queue<T> class (and Stack<T> too) of the .NET Framework from Microsoft is implemented using an array. While this is a perfectly good approach, I think that a Linked List based implementation could be desired in some situations (specifically when the size of the queue is not fixed).
Since the implementation alone would be rather simple for a post, I’ll show you how to implement Unit Testing with the class using Nunit. Although this is a rather simple class to test I think it will show the basic concepts behind unit testing.
First of all let’s begin with the interface of the class. We will call it LinkedListQueue<T>. We basically need the typical operations of a queue.
The interface (without the implementation) is going to be something similar to this:
The thing about unit testing is to first write the different tests so we know what we want to test and then we have to write the code so the tests are passed. Following this convention, now is the time to write some tests using Nunit. To do so, we have to create a new project of type Class Library in Visual Studio (we could have the tests on the same code but it’s better to have it separately so we don’t bloat the real production code). For more information about how to install and set up Nunit I suggest you visit the site for more information. I called the project LinkedListQueueTest
Once we have the code, we can start writing tests in a special class which will have this modifier:
Inside this class we have to write one method for each test we want to perform. It’s usually a good practice to have one test per method on the class you’re testing. In our case, we will begin testing the Enqueue method. To mark a method as a test, we have to add a modifier to it like this:
And in the body of the method, we have to perform the test we need. Usually the tests have a structure: creating an object on which to test, executing the method, and confirm that the result is the same as we expected. Let’s do a simple code for our test:
I suggest you read the Nunit documentation to see the different assertions you have available.
Now that we wrote our first test, we should start thinking and writing the rest of the tests. To simplify the post, here’s a full class showing some tests that you may have wanted to perform:
To see how our test behave, we have to open the resulting LinkedListQueueTest.dll in the Nunit tester program. As we have written no real code on the queue class, it’s obvious that our tests will fail. So let’s get our hands dirty and start programming our queue.
I will not go into the details because a linked list based queue is really easy to implement, so here’s the final code of the class:
The advantage of unit testing is that now the testing is going to be done automatically. The only thing we have to do is open the testing dll file into Nunit and see how’s the outcome. As we’re really good programmers, using the code I showed you, all the tests will pass :)
This is a really simple example of how things should be done with unit testing using C# and Nunit, but I hope it can get you started and you can grasp the philosophy of it. For more information I really recommend reading the book Pragmatic Unit Testing in C# with NUnit.