-
Notifications
You must be signed in to change notification settings - Fork 5
/
iterator_test.go
70 lines (61 loc) · 1.62 KB
/
iterator_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
package iprange
import (
"fmt"
"net"
"testing"
)
func TestName(t *testing.T) {
testCases := []struct {
input string
want string
}{
{input: "1.1.1.1", want: "1.1.1.1"},
{input: "1.1.1.2/30", want: "1.1.1.3"},
{input: "1.1.1.0-255", want: "1.1.1.200"},
{input: "1.1-2.0-1.4", want: "1.2.0.4"},
{input: "1.1.1.1-1.1.2.1", want: "1.1.2.0"},
{input: "2001::59:63", want: "2001::59:63"},
{input: "2001::59:63/126", want: "2001::59:62"},
{input: "2001::59:63-f2", want: "2001::59:f0"},
{input: "2001::59-60:63-f2", want: "2001::60:f0"},
{input: "2001::59:63-2001::59:f2", want: "2001::59:f0"},
}
for _, v := range testCases {
t.Logf("Test %s", v.input)
it, startIp, err := NewIter(v.input)
if err != nil {
t.Fatal(err)
}
t.Logf("Size %d", it.TotalNum())
index := it.TotalNum() - 1
if index > 2 {
index -= 2
}
index2 := it.TotalNum() - 1
if index2 > 3 {
index2 -= 3
}
// 根据序号,计算得到IP
t.Logf("GetByIndex [%d]: %v", index, it.GetIpByIndex(index))
t.Logf("GetByIndex2 [%d]: %v", index2, it.GetIpByIndex(index2))
// 迭代
it.GetIpByIndex(0) // rest index
i := 0
for itn := startIp; it.HasNext() && i <= 3; itn = it.Next() {
t.Log(itn)
i++
}
// 包含判断
if !it.Contains(net.ParseIP(v.want)) {
t.Error(fmt.Sprintf("[ERR] %s Contains %s?", v.input, v.want), it.Contains(net.ParseIP(v.want)))
} else {
t.Log(fmt.Sprintf("%s Contains %s?", v.input, v.want), it.Contains(net.ParseIP(v.want)))
}
}
// 简单的获取IP序列
ipSet, err := GenIpSet("1.1.1.1/30")
if err != nil {
t.Fatal(err)
}
t.Logf("1.1.1.1/30 GenIpSet is %s", ipSet)
}