-
Notifications
You must be signed in to change notification settings - Fork 0
/
ipconv_test.go
91 lines (79 loc) · 1.6 KB
/
ipconv_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
package ipconv
import (
"strings"
"testing"
)
func Test_Ipconv_V42Long(t *testing.T) {
t.Parallel()
cases := []struct {
uint32
string
}{
{0, "0.0.0.0"},
{1, "0.0.0.1"},
{16777216, "1.0.0.0"},
{185273099, "11.11.11.11"},
{4294967295, "255.255.255.255"},
}
for _, tc := range cases {
t.Run(tc.string, func(t *testing.T) {
if tc.uint32 != V42Long(tc.string) {
t.Errorf("%s => %d", tc.string, tc.uint32)
}
})
}
}
func Benchmark_Ipconv_V42Long(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
V42Long("255.255.255.255")
}
}
func Test_Ipconv_V42Long_Panic(t *testing.T) {
t.Parallel()
ips := []string{"111.111.111.111.", "1.1.1", "a.a.a.a", "256.255.255.255", "1.1.1.1.1"}
for _, ip := range ips {
t.Run(ip, func(t *testing.T) {
defer func() {
if r := recover(); r != nil {
err, ok := r.(ipv4Error)
if !ok {
t.Error("error in recover should be ipv4Error")
}
if !strings.Contains(err.Error(), ip) {
t.Errorf("error should contain ip %s", ip)
}
}
}()
V42Long(ip)
})
}
}
func Test_Ipconv_Long2V4(t *testing.T) {
t.Parallel()
cases := []struct {
uint32
string
}{
{0, "0.0.0.0"},
{1, "0.0.0.1"},
{16777216, "1.0.0.0"},
{185273099, "11.11.11.11"},
{4294967295, "255.255.255.255"},
}
for _, tc := range cases {
t.Run(tc.string, func(t *testing.T) {
if tc.string != Long2V4(tc.uint32) {
t.Errorf("%d => %s", tc.uint32, tc.string)
}
})
}
}
func Benchmark_Ipconv_Long2V4(b *testing.B) {
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
Long2V4(4294967295)
}
}