forked from brianvoe/gofakeit
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc_test.go
263 lines (217 loc) · 5.01 KB
/
misc_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
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
package gofakeit
import (
"fmt"
"reflect"
"sort"
"testing"
"github.com/brianvoe/gofakeit/v6/data"
)
func ExampleBool() {
Seed(11)
fmt.Println(Bool())
// Output: true
}
func ExampleFaker_Bool() {
f := New(11)
fmt.Println(f.Bool())
// Output: true
}
func BenchmarkBool(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
Bool()
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.Bool()
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.Bool()
}
})
}
func TestUUID(t *testing.T) {
id := UUID()
if len(id) != 36 {
t.Error("unique length does not equal requested length")
}
}
func ExampleUUID() {
Seed(11)
fmt.Println(UUID())
// Output: 590c1440-9888-45b0-bd51-a817ee07c3f2
}
func ExampleFaker_UUID() {
f := New(11)
fmt.Println(f.UUID())
// Output: 590c1440-9888-45b0-bd51-a817ee07c3f2
}
func BenchmarkUUID(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
UUID()
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.UUID()
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.UUID()
}
})
}
func TestShuffleAnySlice(t *testing.T) {
ShuffleAnySlice(nil) // Should do nothing
ShuffleAnySlice("b") // Should do nothing
ShuffleAnySlice([]string{"b"}) // If single value should do nothing
a := []string{"a", "b", "c", "d", "e", "f", "g", "h"}
b := make([]string, len(a))
copy(b, a)
ShuffleAnySlice(a)
if equalSliceString(a, b) {
t.Errorf("shuffle strings resulted in the same permutation, the odds are slim")
}
n := []int{1, 2, 3, 4, 5, 6, 7, 8, 9, 0}
m := make([]int, len(n))
copy(m, n)
ShuffleAnySlice(n)
if equalSliceInt(n, m) {
t.Errorf("shuffle ints resulted in the same permutation, the odds are slim")
}
i := []interface{}{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
ii := make([]interface{}, len(i))
copy(ii, i)
ShuffleAnySlice(i)
if equalSliceInterface(i, ii) {
t.Errorf("shuffle interface resulted in the same permutation, the odds are slim")
}
}
func ExampleShuffleAnySlice() {
Seed(11)
strings := []string{"happy", "times", "for", "everyone", "have", "a", "good", "day"}
ShuffleAnySlice(strings)
fmt.Println(strings)
ints := []int{52, 854, 941, 74125, 8413, 777, 89416, 841657}
ShuffleAnySlice(ints)
fmt.Println(ints)
// Output:
// [good everyone have for times a day happy]
// [777 74125 941 854 89416 52 8413 841657]
}
func ExampleFaker_ShuffleAnySlice() {
f := New(11)
strings := []string{"happy", "times", "for", "everyone", "have", "a", "good", "day"}
f.ShuffleAnySlice(strings)
fmt.Println(strings)
ints := []int{52, 854, 941, 74125, 8413, 777, 89416, 841657}
f.ShuffleAnySlice(ints)
fmt.Println(ints)
// Output:
// [good everyone have for times a day happy]
// [777 74125 941 854 89416 52 8413 841657]
}
func BenchmarkShuffleAnySlice(b *testing.B) {
b.Run("package", func(b *testing.B) {
a := []interface{}{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
for i := 0; i < b.N; i++ {
ShuffleAnySlice(a)
}
})
b.Run("Faker math", func(b *testing.B) {
a := []interface{}{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
f := New(0)
for i := 0; i < b.N; i++ {
f.ShuffleAnySlice(a)
}
})
b.Run("Faker crypto", func(b *testing.B) {
a := []interface{}{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.ShuffleAnySlice(a)
}
})
}
func ExampleFlipACoin() {
Seed(11)
fmt.Println(FlipACoin())
// Output: Heads
}
func ExampleFaker_FlipACoin() {
f := New(11)
fmt.Println(f.FlipACoin())
// Output: Heads
}
func TestFlipACoin(t *testing.T) {
for i := 0; i < 100; i++ {
FlipACoin()
}
}
func BenchmarkFlipACoin(b *testing.B) {
b.Run("package", func(b *testing.B) {
for i := 0; i < b.N; i++ {
FlipACoin()
}
})
b.Run("Faker math", func(b *testing.B) {
f := New(0)
for i := 0; i < b.N; i++ {
f.FlipACoin()
}
})
b.Run("Faker crypto", func(b *testing.B) {
f := NewCrypto()
for i := 0; i < b.N; i++ {
f.FlipACoin()
}
})
}
func TestRandomMapKey(t *testing.T) {
mStr := map[string]int{
"a": 1,
"b": 2,
"c": 3,
}
for i := 0; i < 100; i++ {
key := RandomMapKey(mStr)
if _, ok := mStr[key.(string)]; !ok {
t.Errorf("key %s not found in map", key)
}
}
mInt := map[int]string{
1: "a",
2: "b",
3: "c",
}
for i := 0; i < 100; i++ {
f := New(11)
key := f.RandomMapKey(mInt)
if _, ok := mInt[key.(int)]; !ok {
t.Errorf("key %d not found in map", key)
}
}
}
func TestCategories(t *testing.T) {
var got, expected []string
for k := range Categories() {
got = append(got, k)
}
for k := range data.Data {
expected = append(expected, k)
}
sort.Strings(got)
sort.Strings(expected)
if !reflect.DeepEqual(got, expected) {
t.Error("Type arrays are not the same.\nExpected: ", expected, "\nGot: ", got)
}
}