~eda-qa/dhlib/main

« back to all changes in this revision

Viewing changes to app-words/words/test/BoardTest.mhx

  • 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 words.test;
 
7
 
 
8
class BoardTest extends haxe.unit.TestCaseX
 
9
{
 
10
        static var testBoard = [
 
11
                [ "f", "t", "co" ],
 
12
                [ "s", "a", "i" ],
 
13
                [ "a", "t", "n" ]
 
14
                ];
 
15
                
 
16
        //second value is the "Score"
 
17
        static var foundWords = [
 
18
                ["fantastico", 1], //full permutation, with double letter
 
19
                ["a", 0], //x2 must be found twice, single letter
 
20
                ["ant", 0],
 
21
                ["ants", 0], //prefix also exists "ant"
 
22
                ["sat", 0], //x3 multiple path lookup
 
23
                ["coin", 1] //double letter
 
24
                ];
 
25
                
 
26
        static var unfoundWords = [
 
27
                ["fantastic",   0], //invalid double letter split
 
28
                ["fin", 0], //incorrect permutation
 
29
                ["cost", 0], //incorrect permutation on double
 
30
                ["cat", 0], //incorrect split as prefix
 
31
                ["sats", 0], //double use of a single letter
 
32
                ];
 
33
                
 
34
        private function getAnagram()
 
35
        {
 
36
                var m = mathx.Matrix.fromArrayMat( testBoard );
 
37
                var enc = new words.Encoder_EN();       
 
38
                var hash = HashUtil.merge(
 
39
                        HashUtil.fromArray( foundWords ),
 
40
                        HashUtil.fromArray( unfoundWords )
 
41
                        );
 
42
                var dict = words.Dictionary.withHash( hash, enc );
 
43
                
 
44
                return words.Anagram.withMatrix( m, dict );
 
45
        }
 
46
        
 
47
        public function testSearch() 
 
48
        {
 
49
                var ana = getAnagram();
 
50
                
 
51
                while( ana.step( 1000 ) )
 
52
                { }     //it has to end sometime...
 
53
                        
 
54
                for( fw in foundWords )
 
55
                        assertEquals( fw[1], ana.found.get( fw[0] ) );
 
56
                        
 
57
                //we can't verify the multiple counts now since Anagram stores in a Hash
 
58
                        
 
59
                for( uw in unfoundWords )
 
60
                        assertFalse( ana.found.exists( uw[0] ) );
 
61
        }
 
62
        
 
63
        public function testBoardClass()
 
64
        {
 
65
                var board = words.WordBoard.withMatrix(
 
66
                        mathx.Matrix.fromArrayMat( testBoard )
 
67
                        );
 
68
                        
 
69
                for( fw in foundWords )
 
70
                        assertTrue( board.contains( fw[0] ) );
 
71
                for( uw in unfoundWords )
 
72
                        assertFalse( board.contains( uw[0] ) );
 
73
        }
 
74
        
 
75
#if false
 
76
        public function testSearchPerf()
 
77
        {
 
78
                var start = haxe.Timer.stamp();
 
79
                for( i in 0...100 )
 
80
                {
 
81
                        var ana = getAnagram();
 
82
                        while( ana.step( 1000 ) )
 
83
                        { }     //it has to end sometime...
 
84
                }
 
85
                var end = haxe.Timer.stamp();
 
86
                
 
87
                trace( "Lookup: " + (end-start) );
 
88
                assertTrue( true );
 
89
        }
 
90
#end
 
91
 
 
92
        public function testEncoderEN()
 
93
        {
 
94
                var enc = new words.Encoder_EN();
 
95
                for( fw  in foundWords )
 
96
                        assertEquals( fw[0], enc.decode( enc.encode( fw[0] ) ) );
 
97
        }
 
98
}