-
Notifications
You must be signed in to change notification settings - Fork 35
/
f64s_test.go
138 lines (129 loc) · 2.62 KB
/
f64s_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
// Copyright ©2020 The go-hep Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package f64s
import (
"fmt"
"math/rand"
"reflect"
"testing"
)
func panics(t *testing.T, want error) func() {
t.Helper()
return func() {
err := recover()
if err == nil {
t.Fatalf("expected a panic")
}
if got, want := err.(error).Error(), want.Error(); got != want {
t.Fatalf("invalid panic message.\ngot= %v\nwant=%v",
got, want,
)
}
}
}
func TestMap(t *testing.T) {
defer panics(t, errLength)()
_ = Map(make([]float64, 3), make([]float64, 5), nil)
}
func TestTake(t *testing.T) {
for _, tc := range []struct {
dst, src []float64
inds []int
want []float64
panics error
}{
{
dst: nil,
src: []float64{1, 2, 3},
inds: []int{1},
want: []float64{2},
},
{
dst: make([]float64, 1),
src: []float64{1, 2, 3},
inds: []int{1},
want: []float64{2},
},
{
dst: make([]float64, 0),
src: []float64{1, 2, 3},
inds: []int{},
want: []float64{},
},
{
dst: []float64{},
src: []float64{1, 2, 3},
inds: nil,
want: []float64{},
},
{
dst: make([]float64, 2),
src: []float64{1, 2, 3},
inds: []int{1, 2},
want: []float64{2, 3},
},
{
dst: nil,
src: []float64{1, 2, 3},
inds: []int{1, 0},
panics: errSortedIndices,
},
{
dst: nil,
src: []float64{1, 2, 3},
inds: []int{0, 1, 2, 3},
panics: errLength,
},
{
dst: make([]float64, 1),
src: []float64{1, 2, 3},
inds: []int{0, 1},
panics: errLength,
},
{
dst: make([]float64, 4),
src: []float64{1, 2, 3},
inds: []int{0, 1},
panics: errLength,
},
{
dst: nil,
src: []float64{1, 2, 3},
inds: []int{1, 1},
panics: errDuplicateIndices,
},
} {
t.Run("", func(t *testing.T) {
if tc.panics != nil {
defer panics(t, tc.panics)()
}
got := Take(tc.dst, tc.src, tc.inds)
if !reflect.DeepEqual(got, tc.want) {
t.Fatalf("got= %v\nwant=%v", got, tc.want)
}
})
}
}
var takeSink []float64
func BenchmarkTake(b *testing.B) {
for _, size := range []int{2, 4, 8, 128, 1024, 1024 * 1024} {
b.Run(fmt.Sprintf("Len=%d", size), func(b *testing.B) {
src := make([]float64, size)
ind := make([]int, 0, len(src))
rnd := rand.New(rand.NewSource(0))
for i := range src {
src[i] = rnd.Float64()
if rnd.Float64() > 0.5 {
ind = append(ind, i)
}
}
dst := make([]float64, len(ind))
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
takeSink = Take(dst, src, ind)
}
})
}
}