Just Code‎ > ‎

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

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


Adding a few test cases for the Node type aswell.

module NodeTests

open DataStructure
open NUnit.Framework
open FsUnit

[<TestFixture>]
type NodeFixture() =
    let nStart = new Node(new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0) )
    let nGoal = new Node(new SodukoProblem(1, 2, 3, 4, 5, 6, 7, 8, 9) )
    let valueInMiddleNode = new Node(new SodukoProblem(0, 0, 0, 0, 5, 0, 0, 0, 0) )
    let threeValuesNode = new Node(new SodukoProblem(0, 4, 0, 0, 5, 0, 3, 0, 0) )
    let almostGoalNode = new Node(new SodukoProblem(1, 4, 2, 6, 5, 7, 3, 9, 0) )

    [<Test>]
    member this.
        ``Based on known states of the Node isGoal should be true``() =

       nStart.isGoal |> should equal false
       nGoal.isGoal |> should equal true

    [<Test>]
    member this.
        ``Should create 8 child nodes based on a node with one value``() =
        
        let children = nStart.getChildren
        let randomChildren = valueInMiddleNode.getChildren

        children |> should haveLength 8
        randomChildren |> should haveLength 8

    [<Test>]
    member this.
        ``Should create some child nodes based on a node with some values``() =
        
        let someChildren = threeValuesNode.getChildren
        someChildren |> should haveLength 6

    [<Test>]
    member this.
        ``Should create one child node when all but one slots are filled``() =
        
        let oneChild = almostGoalNode.getChildren
        oneChild |> should haveLength 1

    [<Test>]
    member this.
        ``Should create goal child node when all but one slots are filled``() =
        
        let goalChild = almostGoalNode.getChildren
        goalChild.Head.isGoal |> should equal true
    
    [<Test>]
    member this.
        ``Two nodes should only be equal if they have the same values in the same spots``() =
        
        let first = new Node(new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0) )
        let second = new Node(new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0) )
        first.equalTo second |> should equal true
        first.equalTo first |> should equal true

        let first = new Node(new SodukoProblem(1, 2, 3, 4, 5, 6, 7, 0, 0) )
        let second = new Node(new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0) )
        first.equalTo second |> should equal false

        let first = new Node(new SodukoProblem(0, 1, 0, 0, 0, 0, 0, 0, 0) )
        let second = new Node(new SodukoProblem(1, 0, 0, 0, 0, 0, 0, 0, 0) )
        first.equalTo second |> should equal false
Comments