From 1c132c06426aed53286b6d14da9e193cf5828c7e Mon Sep 17 00:00:00 2001 From: dm1sh Date: Fri, 8 Jul 2022 01:54:07 +0300 Subject: [PATCH] Added tests for binary search and strings comparition --- README.md | 6 ++++ gordle_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 gordle_test.go diff --git a/README.md b/README.md index 6861dff..7e3e0b8 100644 --- a/README.md +++ b/README.md @@ -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/.txt` file, where `` - number of characters, specified by `-n` flag diff --git a/gordle_test.go b/gordle_test.go new file mode 100644 index 0000000..70dab08 --- /dev/null +++ b/gordle_test.go @@ -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) + } + }) + } +}