-
Notifications
You must be signed in to change notification settings - Fork 1
/
ring_test.go
68 lines (61 loc) · 1.47 KB
/
ring_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
67
68
package o
import (
"sort"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestForcePush(t *testing.T) {
r := NewRing(1)
r.Push()
assert.Equal(t, uint(0), r.ForcePush())
}
func TestPushAndShift(t *testing.T) {
tests := []struct {
name string
cap uint
turns uint
}{
{"mask/3", 16, 3},
{"basic/3", 19, 3},
}
for _, elt := range tests {
test := elt
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ring := NewRing(test.cap)
for i := uint(0); i < test.cap*test.turns; i++ {
pushed, err := ring.Push()
require.NoError(t, err)
require.True(t, pushed < test.cap)
shifted, err := ring.Shift()
require.NoError(t, err)
require.Equal(t, pushed, shifted, "on attempt %d", i)
}
})
}
}
func TestSlices(t *testing.T) {
tests := []struct {
name string
slice Slice
len uint
}{
{"[]int", sort.IntSlice([]int{1, 2, 3, 4}), 4},
{"[]float64", sort.Float64Slice([]float64{1.0, 2.0, 3.0}), 3},
{"[]string", sort.StringSlice([]string{"hi", "there", "farts", "yup", "strings"}), 5},
}
for _, elt := range tests {
test := elt
t.Run(test.name, func(t *testing.T) {
t.Parallel()
ring := NewRingForSlice(test.slice)
assert.Equal(t, ring.capacity(), test.len)
})
}
}
// TestErrors is silly but improves coverage metrics.
func TestErrors(t *testing.T) {
assert.Equal(t, ErrEmpty.Error(), "reading from an empty ring")
assert.Equal(t, ErrFull.Error(), "inserting into a full ring")
}