-
Notifications
You must be signed in to change notification settings - Fork 81
Expand file tree
/
Copy pathsupport.go
More file actions
63 lines (52 loc) · 1.04 KB
/
support.go
File metadata and controls
63 lines (52 loc) · 1.04 KB
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
package test
import (
"runtime/debug"
"testing"
crand "crypto/rand"
"encoding/binary"
"encoding/hex"
mrand "math/rand"
trand "github.com/timtadh/data-structures/rand"
)
var rand *mrand.Rand
func init() {
seed := make([]byte, 8)
if _, err := crand.Read(seed); err == nil {
rand = trand.ThreadSafeRand(int64(binary.BigEndian.Uint64(seed)))
} else {
panic(err)
}
}
type T testing.T
func (t *T) Assert(ok bool, msg string, vars ...interface{}) {
if !ok {
t.Log("\n" + string(debug.Stack()))
t.Fatalf(msg, vars...)
}
}
func (t *T) AssertNil(errors ...error) {
any := false
for _, err := range errors {
if err != nil {
any = true
t.Log("\n" + string(debug.Stack()))
t.Error(err)
}
}
if any {
t.Fatal("assert failed")
}
}
func RandSlice(length int) []byte {
slice := make([]byte, length)
if _, err := crand.Read(slice); err != nil {
panic(err)
}
return slice
}
func RandHex(length int) string {
return hex.EncodeToString(RandSlice(length / 2))
}
func RandStr(length int) string {
return string(RandSlice(length))
}