~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-sudoku/sudoku/test/BoardTest.hx

  • Committer: edA-qa mort-ora-y
  • Date: 2010-02-16 05:36:32 UTC
  • Revision ID: eda-qa@disemia.com-20100216053632-60lt7fndfi3fgblw
first

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* <license>
 
2
 * This file is part of the dis-Emi-A HaXe Library. Copyright © edA-qa mort-ora-y
 
3
 * For full copyright and license information please refer to doc/license.txt.
 
4
 * </license> 
 
5
 */
 
6
package sudoku.test;
 
7
 
 
8
import sudoku.Board;
 
9
import sudoku.GConst;
 
10
 
 
11
class BoardTest extends haxe.unit.TestCaseX
 
12
{
 
13
        static var sampleBoard = [
 
14
                [1,2,3, 4,5,6, 7,8,9],
 
15
                [4,5,6, 7,8,9, 1,2,3],
 
16
                [7,8,9, 1,2,3, 4,5,6],
 
17
                
 
18
                [2,3,1, 5,6,4, 8,9,7],
 
19
                [5,6,4, 8,9,7, 2,3,1],
 
20
                [8,9,7, 2,3,1, 5,6,4],
 
21
                
 
22
                [3,1,2, 6,4,5, 9,7,8],
 
23
                [6,4,5, 9,7,8, 3,1,2],
 
24
                [9,7,8, 3,1,2, 6,4,5]
 
25
        ];
 
26
        
 
27
        public function testValid()
 
28
        {
 
29
                var m = mathx.Matrix.fromArrayMat(sampleBoard);
 
30
                var s = new Board( m );
 
31
                assertTrue( s.isValid() );
 
32
                assertTrue( s.isComplete() );
 
33
                
 
34
                //check that simple validity checks are working
 
35
                s.set( 3, 7, 5 );
 
36
                assertFalse( s.isValid() );
 
37
                
 
38
                s.set( 3, 7, sudoku.GConst.cellUnset );
 
39
                assertTrue( s.isValid() );
 
40
                assertFalse( s.isComplete() );
 
41
                
 
42
                //check the the grouping check is working
 
43
                m.swapCols( 2, 3 );
 
44
                s = new Board( m );
 
45
                assertFalse( s.isValid() );
 
46
        }
 
47
        
 
48
        public function testCreate()
 
49
        {
 
50
                //track steps to solution
 
51
                var solveCounts = new Array<Int>();
 
52
                for( i in 0...10 )
 
53
                        solveCounts[i] = 0;
 
54
                        
 
55
                var s = new Board();
 
56
                for( i in 0...10 )      //do a few to be sure
 
57
                {
 
58
                        var c = s.fillRandom();
 
59
                        solveCounts[c]++;
 
60
                        assertTrue( s.isValid() );
 
61
                        assertTrue( s.isComplete() );
 
62
                }
 
63
                
 
64
                //trace( solveCounts );
 
65
        }
 
66
        
 
67
        public function testSolve()
 
68
        {       
 
69
                var s = new Board();
 
70
                for( i in 1...(GConst.boardSize-2) )
 
71
                {
 
72
                        s.fillRandom();
 
73
                        s.removeCount( i*i );
 
74
                        assertFalse( s.isComplete() );
 
75
                        assertFalse( s.findSolution() == -1 );
 
76
                        assertTrue( s.isValid() );
 
77
                }
 
78
        }
 
79
}