-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_test.go
70 lines (60 loc) · 1.29 KB
/
utils_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
package adaptivepool
import (
"bufio"
"bytes"
"compress/bzip2"
_ "embed"
"encoding/csv"
"fmt"
"reflect"
"strconv"
"testing"
)
//go:embed stats_test_data.csv.bz2
var statsTestData []byte
func msgFmt(msg string, args ...any) string {
if msg != "" {
msg = fmt.Sprintf(msg+": ", args...)
}
return msg
}
func equal[T comparable](tb testing.TB, expected, got T, msg string,
args ...any) {
tb.Helper()
if expected != got {
tb.Fatalf("%sexpected %v, get %v", msgFmt(msg, args...), expected, got)
}
}
func zero(tb testing.TB, v any, msg string, args ...any) {
tb.Helper()
if v != nil && !reflect.ValueOf(v).IsZero() {
tb.Fatalf("%sunexpected non-zero value: %v", msgFmt(msg, args...), v)
}
}
func parseFloats(ss []string, ret []float64) error {
for i, s := range ss {
f, err := strconv.ParseFloat(s, 64)
if err != nil {
return fmt.Errorf("parse %d-eth float: %w", i, err)
}
ret[i] = f
}
return nil
}
func csvTestDataReader(tb testing.TB) *csv.Reader {
tb.Helper()
r := bufio.NewReader(bzip2.NewReader(bytes.NewReader(statsTestData)))
// discard the CSV header
for {
_, isPrefix, err := r.ReadLine()
zero(tb, err, "ReadLine from test data")
if !isPrefix {
break
}
}
cr := csv.NewReader(r)
cr.Comma = '\t'
cr.FieldsPerRecord = 3
cr.ReuseRecord = true
return cr
}