View Ravichandran J.V.'s profile on LinkedIn RJV 's Blog: Sudoku puzzle and solution unit test Tweet @ravichandranjv

Translate

Monday, 15 October 2012

Sudoku puzzle and solution unit test

Using "async" and "await" and NUnit 2.6.2's new async support, I have embarked on the fascinating task of creating a Sudoku set of solutions.

This is the first step - to get the basic logic right, based on whatever type of input (in this context, it is a random selection of values), to achieve a working solution. Lots of re-factoring to the domain methods to follow with asynchronous events adding to the beauty of the solution! Rerun the IsSolutionGenerated test in a loop (with, if necessary, tweaks) and you will get the complete solution (though, mind yourself, not the complete game).

Many would want to use a Main() or a print values method but that is the tendency that you have to curb, if you wish to get into TDD. Why do you want to "see" the results when you do not know if the code is behaving properly? If at all you want to see the output, use the Console.Write...method(s) in your unit tests and view it in the Text Output tab of the NUnit GUI.

To start off, here is a unit test class for a single set of 9 unique values solution. The domain class can be accessed here


   [TestFixture]
    public class SudokuGridTests
    {
        SudokuGrid ob = new SudokuGrid();
        [SetUp]
        public void InitializeGame()
        {

        }
        [Test]
        public void CheckIfGridEmpty()
        {
            ob.StartGame();
            Assert.True(ob.IsGridEmpty());
        }
        [Test]
        public async void IsSolutionGenerated()
        {
            Assert.True(await ob.GenerateSolution());
        }
    }

No comments: