-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathword_test.go
More file actions
220 lines (196 loc) · 5.01 KB
/
word_test.go
File metadata and controls
220 lines (196 loc) · 5.01 KB
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
package randomizer_test
import (
"testing"
"github.com/colduction/randomizer"
)
var (
benchWordString string
benchWordBytes []byte
)
func hasAdjacentDuplicate(b []byte) bool {
if len(b) < 2 {
return false
}
last := b[0]
for i := 1; i < len(b); i++ {
if b[i] == last {
return true
}
last = b[i]
}
return false
}
func allInAlphabet(b []byte, allow [256]bool) bool {
for _, c := range b {
if !allow[c] {
return false
}
}
return true
}
func makeAlphabet(chars string) [256]bool {
var allow [256]bool
for i := 0; i < len(chars); i++ {
allow[chars[i]] = true
}
return allow
}
func TestWordZeroLength(t *testing.T) {
if got := randomizer.Word.Decimal(0); got != "" {
t.Fatalf("Decimal(0) = %q, want empty string", got)
}
if got := randomizer.Word.Hex(0, false); got != "" {
t.Fatalf("Hex(0,false) = %q, want empty string", got)
}
if got := randomizer.Word.Octal(0); got != "" {
t.Fatalf("Octal(0) = %q, want empty string", got)
}
if got := randomizer.Word.DecimalBytes(0); got != nil {
t.Fatalf("DecimalBytes(0) = %v, want nil", got)
}
if got := randomizer.Word.HexBytes(0, false); got != nil {
t.Fatalf("HexBytes(0,false) = %v, want nil", got)
}
if got := randomizer.Word.OctalBytes(0); got != nil {
t.Fatalf("OctalBytes(0) = %v, want nil", got)
}
}
func TestWordDecimalOutput(t *testing.T) {
const n = 4096
allow := makeAlphabet("0123456789")
s := randomizer.Word.Decimal(n)
if len(s) != n {
t.Fatalf("Decimal length = %d, want %d", len(s), n)
}
sb := []byte(s)
if !allInAlphabet(sb, allow) {
t.Fatal("Decimal produced invalid character")
}
if hasAdjacentDuplicate(sb) {
t.Fatal("Decimal produced adjacent duplicate character")
}
b := randomizer.Word.DecimalBytes(n)
if len(b) != n {
t.Fatalf("DecimalBytes length = %d, want %d", len(b), n)
}
if !allInAlphabet(b, allow) {
t.Fatal("DecimalBytes produced invalid character")
}
if hasAdjacentDuplicate(b) {
t.Fatal("DecimalBytes produced adjacent duplicate character")
}
}
func TestWordHexOutput(t *testing.T) {
const n = 4096
allowLower := makeAlphabet("0123456789abcdef")
allowUpper := makeAlphabet("0123456789ABCDEF")
sLower := randomizer.Word.Hex(n, false)
if len(sLower) != n {
t.Fatalf("Hex length = %d, want %d", len(sLower), n)
}
sbLower := []byte(sLower)
if !allInAlphabet(sbLower, allowLower) {
t.Fatal("Hex lower produced invalid character")
}
if hasAdjacentDuplicate(sbLower) {
t.Fatal("Hex lower produced adjacent duplicate character")
}
sUpper := randomizer.Word.Hex(n, true)
if len(sUpper) != n {
t.Fatalf("Hex uppercase length = %d, want %d", len(sUpper), n)
}
sbUpper := []byte(sUpper)
if !allInAlphabet(sbUpper, allowUpper) {
t.Fatal("Hex uppercase produced invalid character")
}
if hasAdjacentDuplicate(sbUpper) {
t.Fatal("Hex uppercase produced adjacent duplicate character")
}
bLower := randomizer.Word.HexBytes(n, false)
if len(bLower) != n {
t.Fatalf("HexBytes lower length = %d, want %d", len(bLower), n)
}
if !allInAlphabet(bLower, allowLower) {
t.Fatal("HexBytes lower produced invalid character")
}
if hasAdjacentDuplicate(bLower) {
t.Fatal("HexBytes lower produced adjacent duplicate character")
}
bUpper := randomizer.Word.HexBytes(n, true)
if len(bUpper) != n {
t.Fatalf("HexBytes upper length = %d, want %d", len(bUpper), n)
}
if !allInAlphabet(bUpper, allowUpper) {
t.Fatal("HexBytes upper produced invalid character")
}
if hasAdjacentDuplicate(bUpper) {
t.Fatal("HexBytes upper produced adjacent duplicate character")
}
}
func TestWordOctalOutput(t *testing.T) {
const n = 4096
allow := makeAlphabet("01234567")
s := randomizer.Word.Octal(n)
if len(s) != n {
t.Fatalf("Octal length = %d, want %d", len(s), n)
}
sb := []byte(s)
if !allInAlphabet(sb, allow) {
t.Fatal("Octal produced invalid character")
}
if hasAdjacentDuplicate(sb) {
t.Fatal("Octal produced adjacent duplicate character")
}
b := randomizer.Word.OctalBytes(n)
if len(b) != n {
t.Fatalf("OctalBytes length = %d, want %d", len(b), n)
}
if !allInAlphabet(b, allow) {
t.Fatal("OctalBytes produced invalid character")
}
if hasAdjacentDuplicate(b) {
t.Fatal("OctalBytes produced adjacent duplicate character")
}
}
func BenchmarkWordDecimal(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordString = randomizer.Word.Decimal(n)
}
}
func BenchmarkWordDecimalBytes(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordBytes = randomizer.Word.DecimalBytes(n)
}
}
func BenchmarkWordHex(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordString = randomizer.Word.Hex(n, false)
}
}
func BenchmarkWordHexBytes(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordBytes = randomizer.Word.HexBytes(n, false)
}
}
func BenchmarkWordOctal(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordString = randomizer.Word.Octal(n)
}
}
func BenchmarkWordOctalBytes(b *testing.B) {
const n = 256
b.ReportAllocs()
for b.Loop() {
benchWordBytes = randomizer.Word.OctalBytes(n)
}
}