-
Notifications
You must be signed in to change notification settings - Fork 48
/
diff_test.go
180 lines (152 loc) · 3.95 KB
/
diff_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
177
178
179
180
package osm
import (
"encoding/xml"
"os"
"reflect"
"testing"
)
func TestDiff_MarshalXML(t *testing.T) {
data := []byte(`<osm>
<action type="delete">
<old>
<node id="1896619025" lat="0" lon="0" user="" uid="0" visible="true" version="2" changeset="0" timestamp="0001-01-01T00:00:00Z"></node>
</old>
<new>
<node id="1896619025" lat="0" lon="0" user="" uid="0" visible="false" version="3" changeset="0" timestamp="0001-01-01T00:00:00Z"></node>
</new>
</action>
<action type="create">
<node id="1911156719" lat="0" lon="0" user="" uid="0" visible="false" version="1" changeset="0" timestamp="0001-01-01T00:00:00Z"></node>
</action>
</osm>`)
diff := &Diff{}
err := xml.Unmarshal(data, &diff)
if err != nil {
t.Errorf("unmarshal error: %v", err)
}
if l := len(diff.Actions); l != 2 {
t.Errorf("incorrect num of actions: %v", l)
}
marshalled, err := xml.MarshalIndent(diff, "", " ")
if err != nil {
t.Errorf("marshal error: %v", err)
}
if !reflect.DeepEqual(marshalled, data) {
t.Errorf("incorrect marshal")
t.Logf("%v", string(marshalled))
t.Logf("%v", string(data))
}
// specifics
diff = &Diff{}
_, err = xml.Marshal(diff)
if err != nil {
t.Errorf("unable to marshal: %v", err)
}
// create
diff.Actions = append(diff.Actions, Action{
Type: ActionCreate,
OSM: &OSM{Nodes: Nodes{{ID: 1}}},
})
_, err = xml.Marshal(diff)
if err != nil {
t.Errorf("unable to marshal: %v", err)
}
// modify
diff.Actions = append(diff.Actions, Action{
Type: ActionModify,
Old: &OSM{Nodes: Nodes{{ID: 1}}},
New: &OSM{Nodes: Nodes{{ID: 1}}},
})
_, err = xml.Marshal(diff)
if err != nil {
t.Errorf("unable to marshal: %v", err)
}
}
func TestDiff(t *testing.T) {
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
t.Fatalf("unable to read file: %v", err)
}
diff := &Diff{}
err = xml.Unmarshal(data, &diff)
if err != nil {
t.Errorf("unable to unmarshal: %v", err)
}
if l := len(diff.Actions); l != 1094 {
t.Fatalf("incorrect number of actions, got %d", l)
}
// create way
if at := diff.Actions[1075].Type; at != ActionCreate {
t.Errorf("not a create action, %v", at)
}
way := diff.Actions[1075].Ways[0]
if id := way.ID; id != 180669361 {
t.Errorf("incorrect way id, got %v", id)
}
// modify relation
if at := diff.Actions[1088].Type; at != ActionModify {
t.Errorf("not a modify action, %v", at)
}
oldRelation := diff.Actions[1088].Old.Relations[0]
newRelation := diff.Actions[1088].New.Relations[0]
if oldRelation.ID != newRelation.ID {
t.Errorf("modify diff is not correct")
t.Logf("old: %v", oldRelation)
t.Logf("new: %v", newRelation)
}
// delete node
if at := diff.Actions[44].Type; at != ActionDelete {
t.Fatalf("not a delete action, %v", at)
}
oldNode := diff.Actions[44].Old.Nodes[0]
newNode := diff.Actions[44].New.Nodes[0]
if oldNode.ID != newNode.ID {
t.Errorf("delete diff is not correct")
t.Logf("old: %v", oldNode)
t.Logf("new: %v", newNode)
}
if newNode.Visible {
t.Errorf("new node must not be visible")
t.Logf("old: %v", oldNode)
t.Logf("new: %v", newNode)
}
// should marshal the unmarshalled data
_, err = xml.Marshal(diff)
if err != nil {
t.Errorf("unable to marshal: %v", err)
}
}
func BenchmarkDiff_Marshal(b *testing.B) {
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}
diff := &Diff{}
err = xml.Unmarshal(data, &diff)
if err != nil {
b.Fatalf("unmarshal error: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
_, err := xml.Marshal(diff)
if err != nil {
b.Fatalf("marshal error: %v", err)
}
}
}
func BenchmarkDiff_Unmarshal(b *testing.B) {
data, err := os.ReadFile("testdata/annotated_diff.xml")
if err != nil {
b.Fatalf("unable to read file: %v", err)
}
b.ReportAllocs()
b.ResetTimer()
for i := 0; i < b.N; i++ {
diff := &Diff{}
err := xml.Unmarshal(data, &diff)
if err != nil {
b.Fatalf("unmarshal error: %v", err)
}
}
}