From d42c1760797dc475693b759ec7086c5f15cdf676 Mon Sep 17 00:00:00 2001 From: Neil Murphy Date: Sun, 19 May 2024 10:37:14 +0100 Subject: [PATCH] adding test cases, a Makefile so that the tests can be run recursively within subdirectories, README update --- .vscode/settings.json | 8 +++++-- Makefile | 3 +++ README.md | 8 +++++++ utils/helpers.go | 2 +- utils/helpers_test.go | 55 +++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 73 insertions(+), 3 deletions(-) create mode 100644 Makefile create mode 100644 utils/helpers_test.go diff --git a/.vscode/settings.json b/.vscode/settings.json index 009b8cc..c3af523 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -1,10 +1,14 @@ { + "[go]": { + "editor.tabSize": 4, + "editor.insertSpaces": true + }, "[json]": { "editor.tabSize": 2, "editor.insertSpaces": true }, - "[go]": { + "[makefile]": { "editor.tabSize": 4, - "editor.insertSpaces": true + "editor.insertSpaces": false } } diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..e402175 --- /dev/null +++ b/Makefile @@ -0,0 +1,3 @@ +test: + @echo "Running tests recursively..." + go test ./... diff --git a/README.md b/README.md index 52d6024..c255b35 100644 --- a/README.md +++ b/README.md @@ -37,6 +37,14 @@ A screenshot of the results: ![results](screenshots/results.png) +## Testing + +Run all tests (including tests in subdirectories): + +```bash +make test +``` + ## Acknowledgements A big thanks to [u/Disciplined2021](https://www.reddit.com/user/Disciplined2021/) for the initial work compiling the list of duels and results. diff --git a/utils/helpers.go b/utils/helpers.go index 8d7021e..680b82d 100644 --- a/utils/helpers.go +++ b/utils/helpers.go @@ -7,7 +7,7 @@ import ( "os" ) -// calculateExpectedScore calculates the expected score for a participant +// calculates the expected score for a duelist func CalculateExpectedScore(ratingA, ratingB float64) float64 { return 1 / (1 + math.Pow(10, (ratingB-ratingA)/400)) } diff --git a/utils/helpers_test.go b/utils/helpers_test.go new file mode 100644 index 0000000..856ca59 --- /dev/null +++ b/utils/helpers_test.go @@ -0,0 +1,55 @@ +package utils + +import ( + "math" + "testing" +) + +func TestGetUniqueNames(t *testing.T) { + duels := []Duel{ + {Duelist: "Alice", Versus: "Bob", MultiDuelists: []string{"Charlie", "David"}}, + {Duelist: "Bob", Versus: "Alice", MultiDuelists: []string{"Charlie", "David"}}, + {Duelist: "Charlie", Versus: "David", MultiDuelists: []string{"Alice", "Bob"}}, + } + expected := []string{"Alice", "Bob", "Charlie", "David"} + names := GetUniqueNames(duels) + if len(names) != len(expected) { + t.Errorf("got %v; expected %v", names, expected) + } + for i, name := range names { + if name != expected[i] { + t.Errorf("got %v; expected %v", name, expected[i]) + } + } +} + +func TestUpdateRatings(t *testing.T) { + duelistNames := []string{"Alice", "Bob", "Charlie", "David"} + duelists := make(map[string]*Duelist) + for _, name := range duelistNames { + duelists[name] = &Duelist{Name: name, Rating: 1000} + } + + duels := []Duel{ + {Duelist: "Alice", Versus: "Bob", Winner: "Alice"}, + {Duelist: "Charlie", Versus: "David", Winner: "David"}, + {Duelist: "Bob", Versus: "Alice", Winner: "Bob"}, + } + + for _, duel := range duels { + UpdateRatings(duelists, duel) + } + + expected := map[string]float64{ + "Alice": 998.53, + "Bob": 1001.47, + "Charlie": 984, + "David": 1016, + } + + for name, rating := range expected { + if (math.Round(duelists[name].Rating * 100) / 100) != rating { + t.Errorf("got %v; expected %v", duelists[name].Rating, rating) + } + } +}