-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathint_test.go
133 lines (118 loc) · 2.36 KB
/
int_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
package ipv4
import (
"net"
"testing"
)
func TestFromNetIPError(t *testing.T) {
ip6 := net.ParseIP("2001:4860:0:2001::68")
val, err := FromNetIP(ip6)
if err == nil || val != 0 {
t.Errorf("Expected error parsing IPv6 string")
}
}
func TestFromDotsError(t *testing.T) {
val, err := FromDots("2001:4860:0:2001::68")
if err == nil || val != 0 {
t.Errorf("Expected error parsing IPv6 string")
}
}
func TestToNetIP(t *testing.T) {
n := ToNetIP(3232235777)
ip := net.ParseIP("192.168.1.1")
if !n.Equal(ip) {
t.Errorf("ToNetIP(3232235777) = %v, want %v", n, ip)
}
}
func TestFromDotsErrors(t *testing.T) {
cases := []string{
"....",
"0",
"0.",
"0.0",
"0.0.",
"0.0.0",
"0.0.0.",
"0.0.0.0.",
"0..",
"0..0",
"0.0.0..",
"999.1.1.1",
"1.1.1.11111",
}
for _, c := range cases {
if _, err := FromDots(c); err == nil {
t.Errorf("Case %q did not error", c)
}
}
}
func TestEndian(t *testing.T) {
val, err := FromNetIP(net.ParseIP("0.0.0.1"))
if err != nil {
t.Fatalf("unable to parse 0.0.0.1")
}
if val != 1 {
t.Fatalf("endian problem parsing 0.0.0.1")
}
}
func TestRoundTrip(t *testing.T) {
cases := []string{
"0.0.0.0",
"127.0.0.0",
"128.0.0.0",
"0.127.0.0",
"0.128.0.0",
"127.127.127.127",
"128.128.128.128",
"255.255.255.255",
}
for _, c := range cases {
raw, err := FromDots(c)
if err != nil {
t.Errorf("Case %q didn't parse: %s", c, err)
continue
}
if ToDots(raw) != c {
t.Errorf("Case %q roundtrip produced %q", c, ToDots(raw))
}
}
}
func TestSortUnique(t *testing.T) {
case1 := []uint32{1, 1, 1, 1, 1, 1, 1}
SortUniqueUint32(case1)
if len(case1) != 1 && case1[0] != 1 {
t.Errorf("Dedup failed")
}
case2 := []uint32{9, 8, 7, 6, 5, 4, 3, 2, 1, 0}
SortUniqueUint32(case2)
for i := 0; i < 10; i++ {
if case2[i] != uint32(i) {
t.Fatalf("Sort order failed")
}
}
}
var tempOut uint32
func BenchmarkFromDots(b *testing.B) {
var val uint32
for i := 0; i < b.N; i++ {
val, _ = FromDots("255.129.128.127")
}
tempOut = val
}
var tempString string
func BenchmarkToDots(b *testing.B) {
var tmp string
ip, _ := FromDots("1.19.159.255")
for i := 0; i < b.N; i++ {
tmp = ToDots(ip)
}
tempString = tmp
}
var tempUint32 uint32
func BenchmarkFromNetIP(b *testing.B) {
var tmp uint32
ip := net.ParseIP("128.128.128.128")
for i := 0; i < b.N; i++ {
tmp, _ = FromNetIP(ip)
}
tempUint32 = tmp
}