Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#3.5

Writing Simple Odd Even Kata in C#

4.22/5 (4 votes)
20 Aug 2015CPOL2 min read 15.6K   26  
Writing simple odd even Kata in C#

Few days ago, I delivered a talk on ‘Test Driven Development’. More detail: Test Driven Development.

Just after session, I’ve received many queries and lot of responses from a great audience, here is one of the good questions, asked during session by a great guy Balwinder Singh.

Q. How Do I Find Odd Even in C# Using Test Driven Development Approach?

The answer is very simple, we just need to follow the same approach as detailed below.

Please note that I am not going to copy/paste the entire code – the reason being I do not want to steal your time. :) I have attached the complete code as well, you can get the same one from Github:
Odd even katas by Gaurav Kumar Arora

Let's start to get the walk through – how we can write this tdd kata?

C#
public static string PrintOddEven(int startNumber, int lastNumber)  
{  
return GetOddEvenWithinRange(startNumber, lastNumber);  
}

In the above snippet, we have a simple method which is accepting two parameters, startNumber and lastNumber, it's obvious that we are defining the limit of our numeric series to find-out the odd-even.

C#
public static string PrintSingleOddEven(int number)  
{  
return CheckSingleNumberOddEvenPrimeResult(number);  
}

Great, we also have one other method, which tells us whether the entered number is odd or even. :)

Now, let's test our first method to find out odds and evens from within the provided range of integers:

C#
[TestFixture]  
[Category("OddEven Kata")]  
public class TestOddEven  
{  
[Test]  
[TestCase(1, 50)]  
[TestCase(1, 100)]  
[TestCase(5, 150)]  
public void CanPrintOddEven(int startRange, int endRange)  
{  
var result = OddEven.PrintOddEven(startRange, endRange);  
Assert.NotNull(result, string.Format("{0}", result));  
}

In the above, we are using NUnit framework to create tests, I have another post describing all about NUnit framework (if you want to read all about Nunit).

So, in the above, we have three test cases with different series of integers, it will provide all numbers quoting Odd or Even.

You just need to run the code and you will get the desired results. :)

The post Writing simple Odd even Kata in C# appeared first on Gaurav-Arora.com.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)