forked from syncthing/syncthing
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.go
157 lines (142 loc) · 2.62 KB
/
util.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
// Copyright (c) 2014, Suryandaru Triandana <syndtr@gmail.com>
// All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package testutil
import (
"bytes"
"flag"
"math/rand"
"reflect"
"sync"
"github.com/onsi/ginkgo/config"
"github.com/syndtr/goleveldb/leveldb/comparer"
)
var (
runfn = make(map[string][]func())
runmu sync.Mutex
)
func Defer(args ...interface{}) bool {
var (
group string
fn func()
)
for _, arg := range args {
v := reflect.ValueOf(arg)
switch v.Kind() {
case reflect.String:
group = v.String()
case reflect.Func:
r := reflect.ValueOf(&fn).Elem()
r.Set(v)
}
}
if fn != nil {
runmu.Lock()
runfn[group] = append(runfn[group], fn)
runmu.Unlock()
}
return true
}
func RunDefer(groups ...string) bool {
if len(groups) == 0 {
groups = append(groups, "")
}
runmu.Lock()
var runfn_ []func()
for _, group := range groups {
runfn_ = append(runfn_, runfn[group]...)
delete(runfn, group)
}
runmu.Unlock()
for _, fn := range runfn_ {
fn()
}
return runfn_ != nil
}
func RandomSeed() int64 {
if !flag.Parsed() {
panic("random seed not initialized")
}
return config.GinkgoConfig.RandomSeed
}
func NewRand() *rand.Rand {
return rand.New(rand.NewSource(RandomSeed()))
}
var cmp = comparer.DefaultComparer
func BytesSeparator(a, b []byte) []byte {
if bytes.Equal(a, b) {
return b
}
i, n := 0, len(a)
if n > len(b) {
n = len(b)
}
for ; i < n && (a[i] == b[i]); i++ {
}
x := append([]byte{}, a[:i]...)
if i < n {
if c := a[i] + 1; c < b[i] {
return append(x, c)
}
x = append(x, a[i])
i++
}
for ; i < len(a); i++ {
if c := a[i]; c < 0xff {
return append(x, c+1)
} else {
x = append(x, c)
}
}
if len(b) > i && b[i] > 0 {
return append(x, b[i]-1)
}
return append(x, 'x')
}
func BytesAfter(b []byte) []byte {
var x []byte
for _, c := range b {
if c < 0xff {
return append(x, c+1)
} else {
x = append(x, c)
}
}
return append(x, 'x')
}
func RandomIndex(rnd *rand.Rand, n, round int, fn func(i int)) {
if rnd == nil {
rnd = NewRand()
}
for x := 0; x < round; x++ {
fn(rnd.Intn(n))
}
return
}
func ShuffledIndex(rnd *rand.Rand, n, round int, fn func(i int)) {
if rnd == nil {
rnd = NewRand()
}
for x := 0; x < round; x++ {
for _, i := range rnd.Perm(n) {
fn(i)
}
}
return
}
func RandomRange(rnd *rand.Rand, n, round int, fn func(start, limit int)) {
if rnd == nil {
rnd = NewRand()
}
for x := 0; x < round; x++ {
start := rnd.Intn(n)
length := 0
if j := n - start; j > 0 {
length = rnd.Intn(j)
}
fn(start, start+length)
}
return
}