Skip to content

Commit

Permalink
adding test cases, a Makefile so that the tests can be run recursivel…
Browse files Browse the repository at this point in the history
…y within subdirectories, README update
  • Loading branch information
barrymun committed May 19, 2024
1 parent 96ab275 commit d42c176
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 3 deletions.
8 changes: 6 additions & 2 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -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
}
}
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
test:
@echo "Running tests recursively..."
go test ./...
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion utils/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
Expand Down
55 changes: 55 additions & 0 deletions utils/helpers_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

0 comments on commit d42c176

Please sign in to comment.