-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse_test.go
277 lines (209 loc) · 4.31 KB
/
parse_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package mapparser
import (
"encoding/json"
"fmt"
"os"
"strconv"
"testing"
"github.com/minetest-go/types"
)
func validateMapblock(t *testing.T, mapblock *types.MapBlock) {
if mapblock == nil {
t.Error("no data")
return
}
if mapblock.ContentId == nil {
t.Error("contentid is nil")
return
}
if mapblock.Param1 == nil {
t.Error("param1 is nil")
return
}
if mapblock.Param2 == nil {
t.Error("param2 is nil")
return
}
if len(mapblock.ContentId) != 4096 {
t.Error("invalid contentid size")
}
if len(mapblock.Param1) != 4096 {
t.Error("invalid param1 size")
}
if len(mapblock.Param2) != 4096 {
t.Error("invalid param2 size")
}
for _, nodeid := range mapblock.ContentId {
nodename := mapblock.BlockMapping[nodeid]
if nodename == "" {
t.Errorf("Nodename not found for id: %d", nodeid)
}
}
/*
for i, param1 := range mapblock.Mapdata.Param1 {
if param1 > 15 {
t.Error(fmt.Sprintf("Invalid param1: %d @ %d", param1, i))
}
}
*/
}
func TestParse(t *testing.T) {
data, err := os.ReadFile("testdata/0.0.0")
if err != nil {
t.Error(err)
}
mapblock, err := Parse(data)
validateMapblock(t, mapblock)
if err != nil {
t.Error(err)
}
if mapblock.IsEmpty() {
t.Error("mapblock is empty")
}
if mapblock.Version != 28 {
t.Error("wrong mapblock version: " + strconv.Itoa(int(mapblock.Version)))
}
if !mapblock.Underground {
t.Error("Underground flag")
}
if len(mapblock.ContentId) != 4096 {
t.Error("Mapdata length wrong")
}
if len(mapblock.Param2) != 4096 {
t.Error("Mapdata length wrong")
}
if len(mapblock.Param1) != 4096 {
t.Error("Mapdata length wrong")
}
pairs := mapblock.Fields[0]
if pairs["owner"] != "pipo" {
t.Error(pairs["owner"])
}
if mapblock.GetNodeId(types.NewPos(0, 0, 0)) != 0 {
t.Error("nodeid mismatch")
}
if mapblock.GetNodeName(types.NewPos(0, 0, 0)) != "travelnet:travelnet" {
t.Error("nodename mismatch")
}
if mapblock.GetParam2(types.NewPos(0, 0, 0)) != 0 {
t.Error("param2 mismatch")
}
json, err := json.Marshal(mapblock)
if err != nil {
t.Error(err)
}
fmt.Println(string(json))
}
func TestParseError(t *testing.T) {
data, err := Parse([]byte{})
if data != nil {
t.Error("data is set")
}
if err == nil {
t.Error("error expected")
}
if err != ErrNoData {
t.Error("wrong error")
}
}
func TestParseError2(t *testing.T) {
data, err := Parse([]byte{24})
if data == nil {
t.Error("data is not set")
return
}
if data.Version != 24 {
t.Error("mapblock version wrong")
}
if err == nil {
t.Error("error expected")
}
if err != ErrMapblockVersion {
t.Error("wrong error")
}
}
func TestParseZstd(t *testing.T) {
data, err := os.ReadFile("testdata/zstd-block.bin")
if err != nil {
t.Error(err)
}
mapblock, err := Parse(data)
if err != nil {
t.Error(err)
}
fmt.Println(mapblock)
validateMapblock(t, mapblock)
}
func TestParse2(t *testing.T) {
data, err := os.ReadFile("testdata/11.0.2")
if err != nil {
t.Error(err)
}
mapblock, err := Parse(data)
if err != nil {
t.Error(err)
}
validateMapblock(t, mapblock)
if mapblock.IsEmpty() {
t.Error("mapblock empty")
}
for id, name := range mapblock.BlockMapping {
fmt.Printf("%d = %s\n", id, name)
}
}
func TestParse3(t *testing.T) {
data, err := os.ReadFile("testdata/0.1.0")
if err != nil {
t.Error(err)
}
mapblock, err := Parse(data)
if err != nil {
t.Error(err)
}
validateMapblock(t, mapblock)
}
func TestParseMetadata(t *testing.T) {
data, err := os.ReadFile("testdata/mb-with-metadata.bin")
if err != nil {
t.Error(err)
}
mb, err := Parse(data)
if err != nil {
t.Error(err)
}
str, err := json.MarshalIndent(mb, "", " ")
if err != nil {
t.Error(err)
}
fmt.Println(string(str))
}
func TestParseNetworkBlock(t *testing.T) {
data, err := os.ReadFile("testdata/network-blockdata.bin")
if err != nil {
t.Error(err)
}
mb, err := ParseNetwork(28, data)
if err != nil {
t.Error(err)
}
str, err := json.MarshalIndent(mb, "", " ")
if err != nil {
t.Error(err)
}
fmt.Println(string(str))
}
func TestParseNetworkBlock2(t *testing.T) {
data, err := os.ReadFile("testdata/mapblock3516192727.bin")
if err != nil {
t.Error(err)
}
mb, err := Parse(data)
if err != nil {
t.Error(err)
}
str, err := json.MarshalIndent(mb, "", " ")
if err != nil {
t.Error(err)
}
fmt.Println(string(str))
}