Newer
Older
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
package net
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestToUint32(t *testing.T) {
tests := []struct {
name string
val uint64
expected uint32
}{
{
name: "IP: 172.24.5.1",
val: 2887255297,
expected: 2887255297,
},
{
name: "bigger than IPv4 address",
val: 2887255295 + 17179869184,
expected: 2887255295,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
ip := IP{
lower: test.val,
}
assert.Equal(t, test.expected, ip.ToUint32())
})
}
}
func TestCompare(t *testing.T) {
tests := []struct {
name string
ip *IP
other *IP
expected int
}{
{
name: "equal",
ip: &IP{
lower: 100,
higher: 200,
},
other: &IP{
lower: 100,
higher: 200,
},
expected: 0,
},
{
name: "greater higher word",
ip: &IP{
lower: 123,
higher: 200,
},
other: &IP{
lower: 456,
higher: 100,
},
expected: 1,
},
{
name: "lesser higher word",
ip: &IP{
lower: 123,
higher: 100,
},
other: &IP{
lower: 456,
higher: 200,
},
expected: -1,
},
{
name: "equal higher word but lesser lower word",
ip: &IP{
lower: 456,
higher: 100,
},
other: &IP{
lower: 123,
higher: 100,
},
expected: 1,
},
{
name: "equal higher word but lesser lower word",
ip: &IP{
lower: 123,
higher: 100,
},
other: &IP{
lower: 456,
higher: 100,
},
expected: -1,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
assert.Equal(t, test.expected, test.ip.Compare(test.other))
})
}
}