Click here to Skip to main content
15,885,141 members
Articles / Programming Languages / C# 3.5

Writing Simple Odd Even Kata in C#

Rate me:
Please Sign up or sign in to vote.
4.22/5 (4 votes)
20 Aug 2015CPOL2 min read 15K   26   3   10
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)


Written By
Chief Technology Officer
India India
Learning never ends.

Comments and Discussions

 
QuestionWhat's the purpose of the test CanPrintOddEven? Pin
John Brett21-Aug-15 6:14
John Brett21-Aug-15 6:14 
AnswerRe: What's the purpose of the test CanPrintOddEven? Pin
Gaurav Aroraa23-Aug-15 21:55
professionalGaurav Aroraa23-Aug-15 21:55 
QuestionWhat is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject21-Aug-15 5:39
professionalfredatcodeproject21-Aug-15 5:39 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa23-Aug-15 21:47
professionalGaurav Aroraa23-Aug-15 21:47 
GeneralRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject23-Aug-15 23:41
professionalfredatcodeproject23-Aug-15 23:41 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa23-Aug-15 23:46
professionalGaurav Aroraa23-Aug-15 23:46 
GeneralRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
fredatcodeproject28-Aug-15 1:14
professionalfredatcodeproject28-Aug-15 1:14 
AnswerRe: What is the pluspoint of Kata TDD compared to MS TESTS ? Pin
Gaurav Aroraa29-Aug-15 11:01
professionalGaurav Aroraa29-Aug-15 11:01 
QuestionQuestion/observation about method calls Pin
Slacker00721-Aug-15 3:51
professionalSlacker00721-Aug-15 3:51 
AnswerRe: Question/observation about method calls Pin
Gaurav Aroraa21-Aug-15 4:15
professionalGaurav Aroraa21-Aug-15 4:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.