Added tests for binary search and strings comparition
This commit is contained in:
parent
99ecba369c
commit
1c132c0642
@ -20,6 +20,12 @@ Usage of ./gordle:
|
||||
Word size (default 5)
|
||||
```
|
||||
|
||||
## Testing instructions
|
||||
|
||||
```bash
|
||||
go test # [-cover]
|
||||
```
|
||||
|
||||
## Dictionaries
|
||||
|
||||
To run game you also need dictionary files. By default gordle searches for them in `./dictionary/<n>.txt` file, where `<n>` - number of characters, specified by `-n` flag
|
||||
|
77
gordle_test.go
Normal file
77
gordle_test.go
Normal file
@ -0,0 +1,77 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bufio"
|
||||
"fmt"
|
||||
"os"
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestCompareStrings(t *testing.T) {
|
||||
for _, tt := range []struct {
|
||||
name string
|
||||
input string
|
||||
reference string
|
||||
expected []CharacterStatus
|
||||
}{
|
||||
{
|
||||
name: "Same_strigs",
|
||||
input: "TEST",
|
||||
reference: "TEST",
|
||||
expected: []CharacterStatus{RIGHT, RIGHT, RIGHT, RIGHT},
|
||||
},
|
||||
{
|
||||
name: "Shuffeled",
|
||||
input: "STTE",
|
||||
reference: "TEST",
|
||||
expected: []CharacterStatus{CONTAINS, CONTAINS, CONTAINS, CONTAINS},
|
||||
},
|
||||
{
|
||||
name: "Different",
|
||||
input: "OVAL",
|
||||
reference: "TEST",
|
||||
expected: []CharacterStatus{WRONG, WRONG, WRONG, WRONG},
|
||||
},
|
||||
{
|
||||
name: "Complex",
|
||||
input: "TSAE",
|
||||
reference: "TEST",
|
||||
expected: []CharacterStatus{RIGHT, CONTAINS, WRONG, CONTAINS},
|
||||
},
|
||||
} {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
res := CompareStrings(tt.input, tt.reference)
|
||||
|
||||
if !reflect.DeepEqual(tt.expected, res) {
|
||||
t.Errorf("Comparition error: \n Expected %v \n Got %v", tt.expected, res)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBinSerach(t *testing.T) {
|
||||
file, err := os.Open("./dictionary/23.txt")
|
||||
|
||||
if err != nil {
|
||||
t.Fatal("Could not open sample dictionary")
|
||||
}
|
||||
|
||||
defer file.Close()
|
||||
|
||||
scanner := bufio.NewScanner(file)
|
||||
|
||||
arr := []string{}
|
||||
|
||||
for scanner.Scan() {
|
||||
arr = append(arr, scanner.Text())
|
||||
}
|
||||
|
||||
for pos, el := range arr {
|
||||
t.Run("Test/"+fmt.Sprint(pos), func(t *testing.T) {
|
||||
if !BinSearch(arr, el) {
|
||||
t.Errorf("Could not find %d string \"%s\"", pos, el)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user