-
Notifications
You must be signed in to change notification settings - Fork 27
Expand file tree
/
Copy pathrandom_test.go
More file actions
46 lines (39 loc) · 802 Bytes
/
Copy pathrandom_test.go
File metadata and controls
46 lines (39 loc) · 802 Bytes
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
package strutil
import (
"fmt"
"testing"
)
var randCounter = 0
type dummyRandReader struct {
}
func (r *dummyRandReader) Read(p []byte) (n int, err error) {
for i := range p {
p[i] = byte(randCounter)
randCounter++
}
return len(p), err
}
func TestRandom(t *testing.T) {
oldReader := randReader
randReader = &dummyRandReader{}
tests := []struct {
input string
length int
expected string
}{
{"abcdefghij", 5, "abcde"},
{"abcdefghij", 1, "a"},
{"abc", 6, "abcabc"},
{"", 5, ""},
{"aaa", 5, "aaaaa"},
}
for i, test := range tests {
randCounter = 0
output, _ := Random(test.input, test.length)
Assert(t, test.expected, output, "Test case %d is not successful\n", i)
}
randReader = oldReader
}
func ExampleRandom() {
fmt.Println(Random("abcdefghik", 5))
}