github.com/tcotav/boggle@v0.0.0-20231023231124-a86497006536/solver/bogglesolver_test.go (about)

     1  package solver
     2  
     3  import (
     4  	"os"
     5  	"path"
     6  	"runtime"
     7  	"testing"
     8  
     9  	"github.com/rs/zerolog/log"
    10  	"github.com/tcotav/boggle/wordlookup"
    11  )
    12  
    13  // https://stackoverflow.com/a/70050794
    14  // allow our tests to run from the root of the project
    15  // get the current executing and add ..
    16  // and find the data files
    17  func init() {
    18  	_, filename, _, _ := runtime.Caller(0)
    19  	dir := path.Join(path.Dir(filename), "..")
    20  	err := os.Chdir(dir)
    21  	if err != nil {
    22  		panic(err)
    23  	}
    24  }
    25  
    26  func TestBoggleSolver(t *testing.T) {
    27  	// todo add more board variations
    28  	boards := [][][]rune{
    29  		{
    30  			{'a', 'n', 't', 'z'},
    31  			{'z', 'z', 'z', 'z'},
    32  			{'z', 'z', 'z', 'z'},
    33  			{'z', 'z', 'z', 'z'},
    34  		},
    35  		{
    36  			{'l', 'z', 'z', 'z'},
    37  			{'z', 'o', 's', 'z'},
    38  			{'z', 't', 'e', 'z'},
    39  			{'z', 'u', 's', 'z'},
    40  		},
    41  	}
    42  	dict := wordlookup.NewDictionary()
    43  	dict.Add("ant")
    44  	dict.Add("lotus")
    45  	dict.Add("lotuses")
    46  	dict.Add("lots")
    47  	dict.Add("lost")
    48  
    49  	solution_count := []int{1, 4}
    50  	for i, board := range boards {
    51  		bs := NewBoggleSolver(board, dict)
    52  		// this returns dupes because we're not checking for them
    53  		// there's a todo
    54  		words := bs.FindWords()
    55  		// TODO remove logging from tests
    56  		log.Info().Msgf("%v", words)
    57  
    58  		// we fail because dupes -- need to remove dupes
    59  		if len(words) != solution_count[i] {
    60  			t.Errorf("Expected %d word, got %d", solution_count[i], len(words))
    61  		}
    62  	}
    63  }