-
Notifications
You must be signed in to change notification settings - Fork 275
/
random_math_test.go
183 lines (169 loc) · 3.56 KB
/
random_math_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
package base64Captcha
import (
"bytes"
"testing"
)
func TestRandText(t *testing.T) {
type args struct {
size int
sourceChars string
}
tests := []struct {
name string
args args
want string
}{
{"", args{0, "foo"}, ""},
{"", args{1, "aaa"}, "a"},
{"", args{2, "bbb"}, "bb"},
{"", args{3, "bbb"}, "bbb"},
{"", args{3, "b"}, "bbb"},
{"", args{4, "b"}, "bbbb"},
{"", args{4, ""}, ""},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := RandText(tt.args.size, tt.args.sourceChars); got != tt.want {
t.Errorf("RandText() = %v, want %v", got, tt.want)
}
})
}
}
func TestRandom(t *testing.T) {
type args struct {
min int64
max int64
}
tests := []struct {
name string
args args
}{
{"", args{-10, 10}},
{"", args{-1, 5}},
{"", args{0, 15}},
{"", args{10, 14}},
{"", args{10, 10}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := random(tt.args.min, tt.args.max)
// if out of bound then error
if got < float64(tt.args.min) || got > float64(tt.args.max) {
t.Errorf("RandText() = %v, out of range", got)
}
})
}
}
func TestRandDarkAndLightColor(t *testing.T) {
// Test RandColor
for i := 0; i < 100; i++ {
rgbA := RandColor()
if rgbA.R < 0 || rgbA.R > 255 ||
rgbA.B < 0 || rgbA.B > 255 ||
rgbA.G < 0 || rgbA.G > 255 {
t.Errorf("RandText() = %v, out of range", rgbA)
}
}
// Test RandLightColor
for i := 0; i < 100; i++ {
rgbA := RandLightColor()
if rgbA.R < 200 || rgbA.R > 255 ||
rgbA.B < 200 || rgbA.B > 255 ||
rgbA.G < 200 || rgbA.G > 255 {
t.Errorf("RandText() = %v, out of range", rgbA)
}
}
// Test RandDeepColor
for i := 0; i < 100; i++ {
rgbA := RandDeepColor()
if rgbA.R < 0 || rgbA.R > 255 ||
rgbA.B < 0 || rgbA.B > 255 ||
rgbA.G < 0 || rgbA.G > 255 {
t.Errorf("RandText() = %v, out of range", rgbA)
}
}
}
func TestRand(t *testing.T) {
// test rand int Range
type args struct {
from int
to int
}
tests := []struct {
name string
args args
}{
{"", args{-10, 10}},
{"", args{-1, 5}},
{"", args{0, 15}},
{"", args{10, 14}},
{"", args{10, 10}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := randIntRange(tt.args.from, tt.args.to)
// if out of bound then error
if got < tt.args.from || got > tt.args.to {
t.Errorf("RandText() = %v, out of range", got)
}
})
}
// test rand float Range
type fargs struct {
from float64
to float64
}
tests2 := []struct {
name string
arg fargs
}{
{"", fargs{-10.0, 10.1}},
{"", fargs{-1.0, 5.2}},
{"", fargs{0, 15.3}},
{"", fargs{10.1, 14.3}},
{"", fargs{10.5, 10.5}},
}
for _, tt := range tests2 {
t.Run(tt.name, func(t *testing.T) {
got := randFloat64Range(tt.arg.from, tt.arg.to)
// if out of bound then error
if got < tt.arg.from || got > tt.arg.to {
t.Errorf("RandText() = %v, out of range", got)
}
})
}
}
func TestRandomID(t *testing.T) {
id := RandomId()
if len(id) != idLen {
t.Errorf("Wrong length got %d, want %d", len(id), idLen)
}
for _, val := range id {
if !bytes.ContainsRune(idChars, val) {
t.Errorf("got %v, want %v", idChars, val)
}
}
}
func TestRandBytes(t *testing.T) {
type args struct {
n int
}
tests := []struct {
name string
args args
want int
}{
{"", args{5}, 5},
{"", args{0}, 0},
{"", args{1}, 1},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := randBytes(tt.args.n)
// if out of bound then error
if len(got) != tt.want {
t.Errorf("randBytes() = %v, want %v", got, tt.want)
}
})
}
}