-
Notifications
You must be signed in to change notification settings - Fork 3
/
basic_types.go
277 lines (237 loc) · 8.31 KB
/
basic_types.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package chaos
import (
"time"
"github.com/google/uuid"
)
// Int32 generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func Int32(n int32) int32 {
return singleton.Int32(n)
}
// Int32 generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func (c *Chaos) Int32(n int32) int32 {
if n <= 0 {
return 0
}
r := c.rand()
return r.Int31n(n + 1)
}
// Int32Between generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func Int32Between(min, max int32) int32 {
return singleton.Int32Between(min, max)
}
// Int32Between generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func (c *Chaos) Int32Between(min, max int32) int32 {
if min > max {
min, max = max, min
}
return c.Int32(max-min) + min
}
// Int64 generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func Int64(n int64) int64 {
return singleton.Int64(n)
}
// Int64 generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func (c *Chaos) Int64(n int64) int64 {
if n <= 0 {
return 0
}
r := c.rand()
return r.Int63n(n + 1)
}
// Int64Between generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func Int64Between(min, max int64) int64 {
return singleton.Int64Between(min, max)
}
// Int64Between generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func (c *Chaos) Int64Between(min, max int64) int64 {
if min > max {
min, max = max, min
}
return c.Int64(max-min) + min
}
// Int generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func Int(n int) int {
return singleton.Int(n)
}
// Int generates a deterministic integer between 0 and n (inclusive) based on the provided seed.
// Behavior:
// - If n <= 0, the function returns 0.
// - The generated integer is guaranteed to be within the range [0, n],
// including both 0 and n as possible values.
func (c *Chaos) Int(n int) int {
if n <= 0 {
return 0
}
r := c.rand()
return r.Intn(n + 1)
}
// IntBetween generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func IntBetween(min, max int) int {
return singleton.IntBetween(min, max)
}
// IntBetween generates a deterministic integer between min and max (inclusive) based on the provided seed.
// Behavior:
// - If min > max, the values are swapped.
// - The generated integer is guaranteed to be within the range [min, max],
// including both min and max as possible values.
func (c *Chaos) IntBetween(min, max int) int {
if min > max {
min, max = max, min
}
return c.Int(max-min) + min
}
// Bool generates a deterministic boolean.
func Bool() bool {
return singleton.Bool()
}
// Bool generates a deterministic boolean.
func (c *Chaos) Bool() bool {
return c.Int64(2)%2 == 0
}
// Duration returns a random duration between 0 and n.
func Duration(n time.Duration) time.Duration {
return singleton.Duration(n)
}
// Duration returns a deterministic duration between 0 and n.
func (c *Chaos) Duration(n time.Duration) time.Duration {
return time.Duration(c.Int64(n.Nanoseconds()))
}
// DurationBetween returns a random duration between min and max.
func DurationBetween(min, max time.Duration) time.Duration {
return singleton.DurationBetween(min, max)
}
func (c *Chaos) DurationBetween(min, max time.Duration) time.Duration {
return time.Duration(c.Int64Between(min.Nanoseconds(), max.Nanoseconds()))
}
// Time returns a random time between Unix epoch and 2106-02-07 08:28:16.
func Time() time.Time {
return singleton.Time()
}
// Time returns a deterministic time between Unix epoch and 2106-02-07 08:28:16.
func (c *Chaos) Time() time.Time {
return time.Unix(c.Int64(1<<32), 0)
}
// TimeBetween returns a random time between min and max.
func TimeBetween(min, max time.Time) time.Time {
return singleton.TimeBetween(min, max)
}
// TimeBetween returns a deterministic time between min and max.
func (c *Chaos) TimeBetween(min, max time.Time) time.Time {
minUnix := min.Unix()
maxUnix := max.Unix()
return time.Unix(c.Int64Between(minUnix, maxUnix), 0)
}
// Float32 returns a random float32 between 0 and n.
func Float32(n float32) float32 {
return singleton.Float32(n)
}
// Float32 returns a deterministic float32 between 0 and n.
func (c *Chaos) Float32(n float32) float32 {
r := c.rand()
return r.Float32() * n
}
// Float32Between returns a random float32 between min and max.
func Float32Between(min, max float32) float32 {
return singleton.Float32Between(min, max)
}
// Float32Between returns a deterministic float32 between min and max.
func (c *Chaos) Float32Between(min, max float32) float32 {
return c.Float32(max-min) + min
}
// Float64 returns a random float64 between 0 and n.
func Float64(n float64) float64 {
return singleton.Float64(n)
}
// Float64 returns a deterministic float64 between 0 and n.
func (c *Chaos) Float64(n float64) float64 {
r := c.rand()
return r.Float64() * n
}
// Float64Between returns a random float64 between min and max.
func Float64Between(min, max float64) float64 {
return singleton.Float64Between(min, max)
}
// Float64Between returns a deterministic float64 between min and max.
func (c *Chaos) Float64Between(min, max float64) float64 {
return c.Float64(max-min) + min
}
const alphanumericalChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
// String returns a random string of <length> alphanumerical characters.
func String(length int) string {
return singleton.String(length)
}
// String returns a random string of <length> alphanumerical characters.
func (c *Chaos) String(length int) string {
ret := ""
for i := 0; i < length; i++ {
index := c.Int(len(alphanumericalChars) - 1)
ret += string(alphanumericalChars[index])
}
return ret
}
// IntSlice returns a slice of random numbers between 0 and high included.
// The length of the slice is length.
func IntSlice(high int, length int) []int {
return singleton.IntSlice(high, length)
}
// IntSlice returns a slice of deterministic numbers between 0 and high included.
// The length of the slice is length.
func (c *Chaos) IntSlice(high int, length int) []int {
result := make([]int, length)
for i := 0; i < length; i++ {
result[i] = c.Int(high)
}
return result
}
func UUID() uuid.UUID {
return singleton.UUID()
}
// UUID returns a random UUID.
func (c *Chaos) UUID() uuid.UUID {
rnd := c.rand()
id, err := uuid.NewRandomFromReader(rnd)
if err != nil {
panic(err)
}
return id
}