-
-
Notifications
You must be signed in to change notification settings - Fork 263
/
misc_test.go
203 lines (164 loc) · 3.87 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
package gofakeit
import (
"fmt"
"reflect"
"sort"
"testing"
"github.com/brianvoe/gofakeit/v7/data"
)
func ExampleBool() {
Seed(11)
fmt.Println(Bool())
// Output: false
}
func ExampleFaker_Bool() {
f := New(11)
fmt.Println(f.Bool())
// Output: false
}
func BenchmarkBool(b *testing.B) {
for i := 0; i < b.N; i++ {
Bool()
}
}
func TestUUID(t *testing.T) {
id := UUID()
if len(id) != 36 {
t.Error(id)
t.Error("unique length does not equal requested length")
}
// Checking for race conditions, need to run --race
for i := 0; i < 10000; i++ {
go func() {
_ = UUID()
}()
}
}
func ExampleUUID() {
Seed(11)
fmt.Println(UUID())
// Output: b412b5fb-33a4-498e-9503-21c6b7e01dcf
}
func ExampleFaker_UUID() {
f := New(11)
fmt.Println(f.UUID())
// Output: b412b5fb-33a4-498e-9503-21c6b7e01dcf
}
func BenchmarkUUID(b *testing.B) {
for i := 0; i < b.N; i++ {
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 := []any{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
ii := make([]any, 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: [for day happy everyone good times a have]
// [854 52 74125 941 777 8413 841657 89416]
}
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: [for day happy everyone good times a have]
// [854 52 74125 941 777 8413 841657 89416]
}
func BenchmarkShuffleAnySlice(b *testing.B) {
a := []any{"a", 1, "c", 3, []string{"a", "b", "c"}, -555, []byte{1, 5}, "h"}
for i := 0; i < b.N; i++ {
ShuffleAnySlice(a)
}
}
func ExampleFlipACoin() {
Seed(11)
fmt.Println(FlipACoin())
// Output: Tails
}
func ExampleFaker_FlipACoin() {
f := New(11)
fmt.Println(f.FlipACoin())
// Output: Tails
}
func TestFlipACoin(t *testing.T) {
for i := 0; i < 100; i++ {
FlipACoin()
}
}
func BenchmarkFlipACoin(b *testing.B) {
for i := 0; i < b.N; i++ {
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)
}
}