-
Notifications
You must be signed in to change notification settings - Fork 0
/
sw_test.go
190 lines (161 loc) · 3.37 KB
/
sw_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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
package slidingwindow
import (
"reflect"
"testing"
"unsafe"
)
func TestCreate(t *testing.T) {
w := Window{}
if err := w.Create(10, 3); err != nil {
t.Error("Create(10, 3) should not fail")
}
if err := w.Create(3, 10); err == nil {
t.Error("Create(3, 10) should fail")
}
}
func TestAdd(t *testing.T) {
w := Window{}
_ = w.Create(4, 2)
if s := w.Slice(); len(s) != 0 {
t.Fail()
}
// Check first append
w.Add(1)
if s := w.Slice(); len(s) != 1 || s[0] != 1 {
t.Errorf("%v\n", s)
}
// Check window moving
w.Add(2)
if s := w.Slice(); len(s) != 2 || s[0] != 1 || s[1] != 2 {
t.Errorf("%v\n", s)
}
w.Add(3)
if s := w.Slice(); len(s) != 2 || s[0] != 2 || s[1] != 3 {
t.Errorf("%v\n", s)
}
w.Add(4)
if s := w.Slice(); len(s) != 2 || s[0] != 3 || s[1] != 4 {
t.Errorf("%v\n", s)
}
// Check going over capacity
if getSliceHeader(&w.base).Cap != 4 {
t.Fail()
}
w.Add(5)
if s := w.Slice(); len(s) != 2 || s[0] != 4 || s[1] != 5 {
t.Errorf("%v\n", w.base)
}
if getSliceHeader(&w.base).Cap != 4 {
t.Errorf("%v\n", w.base)
t.Errorf("Capacity was extended, shouldn't be")
}
// And the next after rollover
w.Add(6)
if s := w.Slice(); len(s) != 2 || s[0] != 5 || s[1] != 6 {
t.Errorf("%v\n", s)
}
}
func TestClear(t *testing.T) {
w := Window{}
_ = w.Create(4, 2)
w.Add(1)
w.Add(2)
w.Add(3)
if s := w.Slice(); len(s) != 2 {
t.Errorf("%v\n", s)
}
w.Clear()
if s := w.Slice(); len(s) != 0 {
t.Errorf("%v should be empty\n", s)
}
}
func TestRemove(t *testing.T) {
w := Window{}
_ = w.Create(4, 2)
w.Remove()
if s := w.Slice(); len(s) != 0 {
t.Errorf("Empty-1 should be empty but is %v\n", s)
}
w.Add(1)
w.Add(2)
w.Add(3)
if s := w.Slice(); len(s) != 2 {
t.Errorf("%v\n", s)
}
w.Remove()
if s := w.Slice(); len(s) != 1 || s[0] != 2 {
t.Errorf("%v should be [2]\n", s)
}
}
func TestLoad(t *testing.T) {
w := Window{}
_ = w.Create(4, 2)
w.Add(1)
w.Add(2)
w.Add(3)
w.Load([]float64{})
if s := w.Slice(); len(s) != 2 || s[1] != 3 {
t.Errorf("%v should be [2, 3]\n", s)
}
w.Load([]float64{4, 5})
if s := w.Slice(); len(s) != 2 || s[0] != 4 || s[1] != 5 {
t.Errorf("%v should be [4, 5]\n", s)
}
w.Load([]float64{5, 6, 7})
if s := w.Slice(); len(s) != 2 || s[0] != 6 || s[1] != 7 {
t.Errorf("%v should be [6, 7]\n", s)
}
}
func BenchmarkSlice(b *testing.B) {
w := Window{}
_ = w.Create(2, 10)
w.Load([]float64{42, 43, 44})
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Slice()
}
}
func benchAdd(i, j int, b *testing.B) {
w := Window{}
_ = w.Create(i, j)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Add(42)
}
}
func BenchmarkAdd100X10(b *testing.B) {
benchAdd(100, 10, b)
}
func BenchmarkAdd100X50(b *testing.B) {
benchAdd(100, 50, b)
}
func BenchmarkAdd100X80(b *testing.B) {
benchAdd(100, 80, b)
}
func BenchmarkAdd1000X20(b *testing.B) {
benchAdd(1000, 20, b)
}
func BenchmarkAdd100000X20(b *testing.B) {
benchAdd(100000, 20, b)
}
func BenchmarkAdd100000X200(b *testing.B) {
benchAdd(100000, 200, b)
}
func benchLoad(i, j int, b *testing.B) {
ds := make([]float64, 10)
w := Window{}
_ = w.Create(i, j)
b.ResetTimer()
for i := 0; i < b.N; i++ {
w.Load(ds)
}
}
func BenchmarkLoadNormal(b *testing.B) {
benchLoad(100, 10000, b)
}
func BenchmarkLoadRollover(b *testing.B) {
benchLoad(15, 20, b)
}
func getSliceHeader(x *[]float64) *reflect.SliceHeader {
return (*reflect.SliceHeader)(unsafe.Pointer(x))
}