-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmattress_test.go
95 lines (83 loc) · 1.58 KB
/
mattress_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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package airmat
import (
"testing"
"github.com/stretchr/testify/assert"
)
const size = 10
func TestNewMattress(t *testing.T) {
type S struct {
F int
}
t.Run("simple", func(t *testing.T) {
m := NewMattress[int](size)
assert.Len(t, m.Slice, size)
})
t.Run("struct", func(t *testing.T) {
m := NewMattress[S](size)
assert.Len(t, m.Slice, size)
m.Slice[1].F = 42
assert.Equal(t, m.Slice[1].F, 42)
})
t.Run("struct_ptr", func(t *testing.T) {
m := NewMattress[*S](size)
assert.Len(t, m.Slice, size)
// TODO: automate?
if m.Slice[1] == nil {
m.Slice[1] = &S{}
}
m.Slice[1].F = 42
assert.Equal(t, m.Slice[1].F, 42)
})
}
func TestMattress_Grow(t *testing.T) {
tests := []struct {
name string
m *Mattress[string]
len int
cap int
}{
{
name: "nil",
m: &Mattress[string]{},
len: size,
cap: size,
},
{
name: "equal",
m: NewMattress[string](size),
len: size,
cap: size,
},
{
name: "grow_one",
m: NewMattress[string](size),
len: size + 1,
cap: size * 2, // might be flaky assert
},
{
name: "grow_twice",
m: NewMattress[string](size),
len: size * 2,
cap: size * 2, // might be flaky assert
},
{
name: "shrink_one",
m: NewMattress[string](size),
len: size - 1,
cap: size,
},
{
name: "shrink_twice",
m: NewMattress[string](size),
len: size / 2,
cap: size,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
tt.m.SetSize(tt.len)
assert.Equal(t, tt.len, len(tt.m.Slice))
assert.Equal(t, tt.cap, cap(tt.m.Slice))
})
}
}