Chapter 2. Test-Driven Development in .NET—By Example

In this chapter, we’ll demonstrate how to implement a Stack using Test-Driven Development (TDD). We have found that the best way to understand TDD is to see it practiced and follow along step by step. The following are the steps we used to build a Stack using this method.

The Task

The task is to implement an unbounded Stack, which is a data structure in which access is restricted to the most recently inserted item.

Note

An unbounded Stack doesn’t have to be presized, and you can insert an unlimited number of elements onto it.

The operations include Push, Pop, Top, and IsEmpty. The Push function inserts an element onto the top of the Stack. The Pop function removes the topmost element and returns it; the Top operation returns the topmost element but does not remove it from the Stack. The IsEmpty function returns true when there are no elements on the Stack. Figure 2-1 shows the Push operation in action, Figure 2-2 shows the Pop operation, and Figure 2-3 shows the Top operation.

Push operation

Figure 2-1. Push operation

Pop operation

Figure 2-2. Pop operation

Top operation

Figure 2-3. Top operation

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset