-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmerge_test.go
176 lines (148 loc) · 3.77 KB
/
merge_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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package mergeips_test
import (
"bufio"
"compress/gzip"
"fmt"
"net"
"os"
"path/filepath"
"regexp"
"testing"
"github.com/Djarvur/go-mergeips"
"github.com/Djarvur/go-mergeips/ipnet"
"github.com/go-test/deep"
)
const testMergeDataPath = "./testdata"
var testFileName = regexp.MustCompile(`(.*/)?([^/]+)\.in\.gz$`)
type testMergeRow struct {
path string
name string
in []*net.IPNet
expected []*net.IPNet
}
var testMergeData = loadFiles(testMergeDataPath, testFileName)
func TestMergeByRepeat(t *testing.T) {
data := testMergeDataCopy(testMergeData)
for _, row := range data {
merged := ipnet.MergeByRepeat(ipnet.DedupSorted(ipnet.Sort(row.in)))
if diff := deep.Equal(merged, row.expected); diff != nil {
t.Errorf("%s%s: got %v, expected %v: %v", row.path, row.name, merged, row.expected, diff)
}
}
}
func TestMerge(t *testing.T) {
data := testMergeDataCopy(testMergeData)
for _, row := range data {
merged := mergeips.Merge(row.in)
if diff := deep.Equal(merged, row.expected); diff != nil {
t.Errorf("%s%s: got %v, expected %v: %v", row.path, row.name, merged, row.expected, diff)
}
}
}
func TestCompare(t *testing.T) {
data1 := testMergeDataCopy(testMergeData)
data2 := testMergeDataCopy(testMergeData)
for ri := range data1 {
merged1 := mergeips.Merge(data1[ri].in)
merged2 := ipnet.MergeByRepeat(data2[ri].in)
if diff := deep.Equal(merged1, merged2); diff != nil {
t.Errorf("%s%s: %v != %v: %v", data1[ri].path, data1[ri].name, merged1, merged2, diff)
}
}
}
func BenchmarkMergeByRepeat(b *testing.B) {
data := testMergeDataCopy(testMergeData)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, row := range data {
ipnet.MergeByRepeat(row.in)
}
}
}
func BenchmarkMerge(b *testing.B) {
data := testMergeDataCopy(testMergeData)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for _, row := range data {
mergeips.Merge(row.in)
}
}
}
//////////////////////////////////
func testMergeDataCopy(in []testMergeRow) []testMergeRow {
out := make([]testMergeRow, 0, len(in))
for _, row := range in {
newRow := testMergeRow{
path: row.path,
name: row.name,
in: make([]*net.IPNet, 0, len(row.in)),
expected: make([]*net.IPNet, 0, len(row.expected)),
}
for _, n := range row.in {
newRow.in = append(newRow.in, &net.IPNet{IP: n.IP, Mask: n.Mask})
}
for _, n := range row.expected {
newRow.expected = append(newRow.expected, &net.IPNet{IP: n.IP, Mask: n.Mask})
}
out = append(out, newRow)
}
return out
}
func loadFiles(path string, name *regexp.Regexp) []testMergeRow {
files := listFiles(path, name)
data := make([]testMergeRow, 0, len(files))
for _, f := range files {
out := scanFile(f.path, f.name, ".out.gz")
fmt.Printf("out1: %v\n", out)
out = ipnet.MergeByRepeat(out)
fmt.Printf("out2: %v\n", out)
data = append(
data,
testMergeRow{
path: f.path,
name: f.name,
in: scanFile(f.path, f.name, ".in.gz"),
expected: ipnet.MergeByRepeat(scanFile(f.path, f.name, ".out.gz")),
},
)
}
return data
}
func scanFile(p string, n string, s string) []*net.IPNet {
f, err := os.Open(p + n + s)
if err != nil {
panic(err)
}
defer f.Close()
gzr, err := gzip.NewReader(f)
if err != nil {
panic(err)
}
res, err := mergeips.Scan(bufio.NewScanner(gzr))
if err != nil {
panic(err)
}
return res
}
type fileSpec struct {
name string
path string
}
func listFiles(p string, r *regexp.Regexp) (res []fileSpec) {
err := filepath.Walk(
p,
func(fName string, fInfo os.FileInfo, errInner error) error {
if fInfo.Mode()&os.ModeType != 0 {
return nil
}
if fields := r.FindStringSubmatch(fName); fields != nil {
res = append(res, fileSpec{path: fields[1], name: fields[2]})
}
return nil
},
)
if err != nil {
panic(err)
}
return res
}