Writing simple Odd even Kata in C#

Few days back, I’ve 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 great audience, here is one of the good questions, asked during session by great guy Balwinder Singh.

Q. How do I find Odd even in C# using Test Driven Development approach ?

Answer is very simple we just need to follow same approach as detailing hereunder:

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

Lets start to get the walk through – how we can write this tdd kata?

public static string PrintOddEven(int startNumber, int lastNumber)  
return GetOddEvenWithinRange(startNumber, lastNumber);  

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

public static string PrintSingleOddEven(int number)  
return CheckSingleNumberOddEvenPrimeResult(number);  

Great, we also have one another method, which tell us whether entered number is odd or even 🙂

Now, lets test our first method to find out odds and evens from within provided range of integers:

[Category("OddEven Kata")]  
public class TestOddEven  
[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 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 above we have three test cases with different Series of integers, it will provides all numbers quoting Odd or Even.

You just need to run the code and you will get the desired results 🙂

1 comment on “Writing simple Odd even Kata in C#”

  1. Pingback: Writing simple Odd even Kata in C# - How to Code .NET

Leave A Reply