-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathproperty_test.go
More file actions
85 lines (74 loc) · 1.67 KB
/
property_test.go
File metadata and controls
85 lines (74 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package multielo
import (
"fmt"
"math/rand"
"reflect"
"testing"
"testing/quick"
"time"
)
type matchInput struct {
Count uint8
Seed int64
}
// Generate implements quick.Generator to create bounded random inputs.
func (matchInput) Generate(r *rand.Rand, size int) reflect.Value {
return reflect.ValueOf(matchInput{
Count: uint8(2 + r.Intn(5)), // 2..6 players
Seed: r.Int63(),
})
}
func TestELOConservationProperty(t *testing.T) {
config := DefaultConfig()
config.MaxMatches = 1000
prop := func(mi matchInput) bool {
l := NewLeagueWithConfig(config)
n := int(mi.Count)
names := make([]string, n)
for i := 0; i < n; i++ {
names[i] = fmt.Sprintf("p%d", i)
if err := l.AddPlayer(names[i]); err != nil {
return false
}
}
r := rand.New(rand.NewSource(mi.Seed))
perm := r.Perm(n)
results := make([]*MatchResult, n)
for pos, idx := range perm {
p, _ := l.GetPlayer(names[idx])
results[pos] = &MatchResult{Position: pos + 1, Player: p}
}
preSum := 0
for _, name := range names {
elo, _ := l.GetPlayerELO(name)
preSum += elo
}
if err := l.AddMatch(results, time.Now()); err != nil {
return false
}
postSum := 0
minELO := config.MaxELO
maxELO := config.MinELO
for _, name := range names {
elo, _ := l.GetPlayerELO(name)
postSum += elo
if elo < minELO {
minELO = elo
}
if elo > maxELO {
maxELO = elo
}
}
sumDiff := postSum - preSum
if sumDiff < -1 || sumDiff > 1 {
return false
}
if minELO < config.MinELO || maxELO > config.MaxELO {
return false
}
return true
}
if err := quick.Check(prop, &quick.Config{MaxCount: 50}); err != nil {
t.Fatalf("property failed: %v", err)
}
}