-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathrandom_test.go
178 lines (150 loc) · 3.55 KB
/
random_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
package random
import (
"math"
"strings"
"testing"
)
type char struct {
charset string
length uint64
}
type byteint struct {
bytearray []byte
result uint64
}
type intrange struct {
start uint64
end uint64
}
func TestUint64Range(t *testing.T) {
var tests = []intrange{
{0, 10},
{111, 112},
{0, 1000000},
{9223372036854775800, 9223372036854775808},
}
_, err := Uint64Range(10, 0)
if err == nil {
t.Error("Start greater than end should produce an error.")
}
for _, test := range tests {
i, _ := Uint64Range(test.start, test.end)
if i < test.start || i > test.end {
t.Error("Expected integer within range of", test.start, "and", test.end, "got", i)
}
}
}
func TestChars(t *testing.T) {
var chars = []char{
{"abcdefghijklmnopqrstuvwxyz", 5},
{"ABCDEFGHIJKLMNOPQRSTUVWXYZ", 20},
{"0123456789abcdef", 15},
}
_, err := Chars("", 15)
if err == nil {
t.Error("An empty character set should produce an error.")
}
_, err = Chars("ABCDEF", 0)
if err == nil {
t.Error("N = 0 should produce an error.")
}
for _, char := range chars {
s, _ := Chars(char.charset, char.length)
if uint64(len(s)) != char.length {
t.Error("Expected string of length", char.length, "and got string of length", len(s))
}
for _, c := range s {
if !strings.Contains(char.charset, string(c)) {
t.Error("Expected a character in", char.charset, "got", c)
}
}
}
}
func TestUint8(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Uint8()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < 0 || n > math.MaxUint8 {
t.Error("Expected integer within range of", 0, "and", math.MaxUint8, "got", n)
}
}
}
func TestInt8(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Int8()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < math.MinInt8 || n > math.MaxInt8 {
t.Error("Expected integer within range of", math.MinInt8, "and", math.MaxInt8, "got", n)
}
}
}
func TestUint16(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Uint16()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < 0 || n > math.MaxUint16 {
t.Error("Expected integer within range of", 0, "and", math.MaxUint16, "got", n)
}
}
}
func TestInt16(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Int16()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < math.MinInt16 || n > math.MaxInt16 {
t.Error("Expected integer within range of", math.MinInt16, "and", math.MaxInt16, "got", n)
}
}
}
func TestUint32(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Uint32()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < 0 || n > math.MaxUint32 {
t.Error("Expected integer within range of", 0, "and", math.MaxUint32, "got", n)
}
}
}
func TestInt32(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Int32()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < math.MinInt32 || n > math.MaxInt32 {
t.Error("Expected integer within range of", math.MinInt32, "and", math.MaxInt32, "got", n)
}
}
}
func TestUint64(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Uint64()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < 0 || n > math.MaxUint64 {
t.Error("Expected integer within range of", 0, "and 18446744073709551615 got", n)
}
}
}
func TestInt64(t *testing.T) {
for i := 0; i < 100; i++ {
n, err := Int64()
if err != nil {
t.Error("Unexpected Error:", err)
}
if n < math.MinInt64 || n > math.MaxInt64 {
t.Error("Expected integer within range of", math.MinInt64, "and", math.MaxInt64, "got", n)
}
}
}