-
Notifications
You must be signed in to change notification settings - Fork 2
/
fielder_test.go
66 lines (61 loc) · 1.58 KB
/
fielder_test.go
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
package main
import (
"fmt"
"strconv"
"strings"
"testing"
"time"
)
func Test_PeriodicEligibility_checkEligible(t *testing.T) {
words := strings.Split("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", "")
notFound := map[string]struct{}{}
for _, w := range words {
notFound[w] = struct{}{}
}
pe := newPeriodicEligibility(NewRng("hello"), words, 60*time.Second)
t.Run("only some words show up for short period", func(t *testing.T) {
for i := 0; i < 100; i++ {
for p := 0; p < 30; p++ {
word := pe.getEligibleWord(time.Duration(p) * time.Second)
delete(notFound, word)
}
if len(notFound) == 0 {
break
}
}
if len(notFound) != 0 {
t.Errorf("expected some words to be not found, got none")
}
})
t.Run("all eligible words show up with full period", func(t *testing.T) {
for i := 0; i < 100; i++ {
for p := 0; p < 60; p++ {
word := pe.getEligibleWord(time.Duration(p) * time.Second)
delete(notFound, word)
}
if len(notFound) == 0 {
break
}
}
if len(notFound) > 0 {
t.Errorf("expected all words to be found, got %v", notFound)
}
})
}
func BenchmarkPeriodicEligibility(b *testing.B) {
for _, card := range []int{10, 50, 200} {
var words []string
for i := 0; i < card; i++ {
words = append(words, strconv.Itoa(i))
}
period := 61 * time.Second
pe := newPeriodicEligibility(NewRng("hello"), words, period)
for p := 0; p < 61; p += 10 {
b.Run(fmt.Sprintf("card_%02d_p_%02d", card, p), func(b *testing.B) {
for i := 0; i < b.N; i++ {
pe.getEligibleWord(time.Duration(p) * time.Second)
}
})
}
}
}