-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil_test.go
76 lines (63 loc) · 1.4 KB
/
util_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
package consultant_test
import (
"math/rand"
"testing"
"time"
"github.com/myENA/consultant/v2"
)
func TestLocalAddress(t *testing.T) {
t.Run("ip", func(t *testing.T) {
if _, err := consultant.LocalAddressIP(); err != nil {
t.Logf("Error fetching RFC-1918 addr: %s", err)
t.Fail()
}
})
t.Run("str", func(t *testing.T) {
if _, err := consultant.LocalAddress(); err != nil {
t.Logf("Error matching RFC-1918 addr: %s", err)
t.Fail()
}
})
}
func testRandStrCheckAlphabet(t *testing.T, in string) bool {
var a, A, N bool
for _, c := range in {
if 'a' <= c && c <= 'z' {
a = true
} else if 'A' <= c && c <= 'Z' {
A = true
} else if '0' <= c && c <= '9' {
N = true
}
}
if !a {
t.Logf("LazyRandomString \"%s\" does not contain [a-z]", in)
}
if !A {
t.Logf("LazyRandomString \"%s\" does not contain [A-Z]", in)
}
if !N {
t.Logf("LazyRandomString \"%s\" does not contain [0-9]", in)
}
return a && A && N
}
func TestRandomString(t *testing.T) {
t.Run("works", func(t *testing.T) {
var (
val string
ok bool
)
rand.Seed(time.Now().UnixNano())
values := make(map[string]struct{})
for i := 1; i < 1000; i++ {
val = consultant.LazyRandomString(12)
testRandStrCheckAlphabet(t, val)
if _, ok = values[val]; ok {
t.Logf("Found duplicate after only \"%d\" iterations!", i)
t.FailNow()
} else {
values[val] = struct{}{}
}
}
})
}