Just Code‎ > ‎

F# - Sudoku Solver using search trees Step Three: Adding Unit Tests

posted Jan 29, 2012, 12:52 PM by Peter Henell   [ updated Feb 19, 2012, 2:16 AM ]


Before we start modifying the code we should have some basic unit tests so that we know what we are breaking.

I am using FsUnit as my framework for writing these unit tests and TestDriven.Net to run them.

Starting with the unit tests for the SodukoProblem type.
module SodukoProblemTests

open DataStructure
open NUnit.Framework
open FsUnit

[<TestFixture>]
type SodukoProblemFixture() =

    [<Test>]
    member this.
        ``Should Find Spots For Each Slot Of The List based on the previous step``() =
        let step1 = new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0)
        let step2 = SodukoProblem.FromList (step1.findSpotFor 2)
        let step3 = SodukoProblem.FromList (step2.findSpotFor 3)
        let step4 = SodukoProblem.FromList (step3.findSpotFor 4)
        let step5 = SodukoProblem.FromList (step4.findSpotFor 5)
        let step6 = SodukoProblem.FromList (step5.findSpotFor 6)
        let step7 = SodukoProblem.FromList (step6.findSpotFor 7)
        let step8 = SodukoProblem.FromList (step7.findSpotFor 8)
        let step9 = SodukoProblem.FromList (step8.findSpotFor 9)

        step1.toList |> should equal [1; 0; 0; 0; 0; 0; 0; 0; 0]
        step2.toList |> should equal [1; 2; 0; 0; 0; 0; 0; 0; 0]
        step3.toList |> should equal [1; 2; 3; 0; 0; 0; 0; 0; 0]
        step4.toList |> should equal [1; 2; 3; 4; 0; 0; 0; 0; 0]
        step5.toList |> should equal [1; 2; 3; 4; 5; 0; 0; 0; 0]
        step6.toList |> should equal [1; 2; 3; 4; 5; 6; 0; 0; 0]
        step7.toList |> should equal [1; 2; 3; 4; 5; 6; 7; 0; 0]
        step8.toList |> should equal [1; 2; 3; 4; 5; 6; 7; 8; 0]
        step9.toList |> should equal [1; 2; 3; 4; 5; 6; 7; 8; 9]


    [<Test>]
    member this.
        ``Should find spots on the list when there is one spot taken in the middle of the list``() = 
        let step = new SodukoProblem(0, 0, 0, 0, 5, 0, 0, 0, 0)
        let step2 = SodukoProblem.FromList (step.findSpotFor 1)
        step2.toList |> should equal [1; 0; 0; 0; 5; 0; 0; 0; 0]

    [<Test>]
    member this.
        ``Should find spots for a number that already exists (Should not care about the number itself)``() = 
        let step = new SodukoProblem(0, 0, 0, 0, 5, 0, 0, 0, 0)
        let step2 = SodukoProblem.FromList (step.findSpotFor 5)

        step2.toList |> should equal [5; 0; 0; 0; 5; 0; 0; 0; 0]

    [<Test>]
    member this.
        ``Should find a spot for a number when there are no numbers in the list``() = 
        let step = new SodukoProblem(0, 0, 0, 0, 0, 0, 0, 0, 0)
        let step2 = SodukoProblem.FromList (step.findSpotFor 5)

        step2.toList |> should equal [5; 0; 0; 0; 0; 0; 0; 0; 0]

    [<Test>]
    member this.
        ``Should create empty list when trying to find a slot for a number when the list is full``() = 
        let step = new SodukoProblem(5, 5, 5, 5, 5, 5, 5, 5, 5)
        let step2 = (step.findSpotFor 5)

        step2 |> should haveLength 0

    [<Test>]
    member this.
        ``Should throw exception when trying to create a SodukoProblem from an empty list``() = 
        let step = new SodukoProblem(5, 5, 5, 5, 5, 5, 5, 5, 5)
        let step2 = (step.findSpotFor 5)        
        
        (fun () ->
            SodukoProblem.FromList step2 |> ignore )
            |> should throw typeof<System.ArgumentException>
Comments