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
278
279
280
281
|
package nbt
import (
"bytes"
"fmt"
"io"
"testing"
)
func bigtest() []byte {
// Generated with go-bindata using the bigtest NBT file.
return []byte{
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xed, 0x54,
0xcf, 0x4f, 0x1a, 0x41, 0x14, 0x7e, 0xc2, 0x02, 0xcb, 0x96, 0x82, 0xb1,
0xc4, 0x10, 0x63, 0xcc, 0xab, 0xb5, 0x84, 0xa5, 0xdb, 0xcd, 0x42, 0x11,
0x89, 0xb1, 0x88, 0x16, 0x2c, 0x9a, 0x0d, 0x1a, 0xd8, 0xa8, 0x31, 0x86,
0xb8, 0x2b, 0xc3, 0x82, 0x2e, 0xbb, 0x66, 0x77, 0xb0, 0xf1, 0xd4, 0x4b,
0x7b, 0x6c, 0x7a, 0xeb, 0x3f, 0xd3, 0x23, 0x7f, 0x43, 0xcf, 0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x72, 0xbc, 0x9e, 0xdf, 0x58, 0x5d, 0x4b, 0x97,
0xae, 0x92, 0x17, 0xb9, 0x44, 0xd0, 0x80, 0xc8, 0xfa, 0x3e, 0xbf, 0xb3,
0xdc, 0x54, 0xcb, 0x07, 0x75, 0x6e, 0xa3, 0xb6, 0x76, 0x59, 0x92, 0x93,
0xa9, 0xdc, 0x51, 0x50, 0x99, 0x6b, 0xcc, 0x35, 0xe6, 0x1a, 0xff, 0x57,
0x23, 0x08, 0x42, 0xcb, 0xe9, 0x1b, 0xd6, 0x78, 0xc2, 0xec, 0xfe, 0xfc,
0x7a, 0xfb, 0x7d, 0x78, 0xd3, 0x84, 0xdf, 0xd4, 0xf2, 0xa4, 0xfb, 0x08,
0x06, 0x00, 0x00,
}
}
func byteArrayTestSeries(n int) byte { return byte((n*n*255 + n*7) % 100) }
func getKey(t *testing.T, comp TagCompound, key string, tt TagType) (tag Tag, ok bool) {
tag, ok = comp[key]
if !ok {
t.Errorf("Missing key %#v.", key)
}
if tag.Type != tt {
t.Errorf("Value for key %#v has wrong TagType. Want: %s, have: %s", key, tt, tag.Type)
ok = false
}
return
}
func tagPayloadCompare(t *testing.T, name string, tag Tag, _want interface{}) {
switch tag.Type {
case TAG_Byte:
want := _want.(byte)
have := tag.Payload.(byte)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_Short:
want := _want.(int16)
have := tag.Payload.(int16)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_Int:
want := _want.(int32)
have := tag.Payload.(int32)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_Long:
want := _want.(int64)
have := tag.Payload.(int64)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_Float:
want := _want.(float32)
have := tag.Payload.(float32)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_Double:
want := _want.(float64)
have := tag.Payload.(float64)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
case TAG_String:
want := _want.(string)
have := tag.Payload.(string)
if want != have {
t.Errorf("Wrong payload for %s. Want: %#v, have: %#v", name, want, have)
}
default:
panic(fmt.Errorf("tagPayloadCompare doesn't know how to compare payloads for TagType %s!", tag.Type))
}
}
func nestedCompoundHelper(t *testing.T, comp TagCompound, id, name string, value float32) {
if tag, ok := getKey(t, comp, id, TAG_Compound); ok {
nested := tag.Payload.(TagCompound)
if tag, ok := getKey(t, nested, "name", TAG_String); ok {
tagPayloadCompare(t, "name", tag, name)
}
if tag, ok := getKey(t, nested, "value", TAG_Float); ok {
tagPayloadCompare(t, "value", tag, value)
}
}
}
func TestBigtest(t *testing.T) {
testBigtest(bytes.NewReader(bigtest()), t)
}
func testBigtest(r io.Reader, t *testing.T) {
root, name, err := ReadGzipdNamedTag(r)
if err != nil {
t.Fatalf("Could not read NBT data: %s", err)
}
if name != "Level" {
t.Errorf("Wrong name. Want \"Level\", have %#v", name)
}
if root.Type != TAG_Compound {
t.Fatalf("Root tag must be a TAG_Compound, have %s", root.Type)
}
rootcomp := root.Payload.(TagCompound)
if tag, ok := getKey(t, rootcomp, "shortTest", TAG_Short); ok {
tagPayloadCompare(t, "shortTest", tag, int16(32767))
}
if tag, ok := getKey(t, rootcomp, "longTest", TAG_Long); ok {
tagPayloadCompare(t, "longTest", tag, int64(9223372036854775807))
}
if tag, ok := getKey(t, rootcomp, "floatTest", TAG_Float); ok {
tagPayloadCompare(t, "floatTest", tag, float32(0.49823147))
}
if tag, ok := getKey(t, rootcomp, "stringTest", TAG_String); ok {
tagPayloadCompare(t, "stringTest", tag, "HELLO WORLD THIS IS A TEST STRING ÅÄÖ!")
}
if tag, ok := getKey(t, rootcomp, "intTest", TAG_Int); ok {
tagPayloadCompare(t, "intTest", tag, int32(2147483647))
}
if tag, ok := getKey(t, rootcomp, "byteTest", TAG_Byte); ok {
tagPayloadCompare(t, "byteTest", tag, byte(127))
}
if tag, ok := getKey(t, rootcomp, "doubleTest", TAG_Double); ok {
tagPayloadCompare(t, "doubleTest", tag, float64(0.4931287132182315))
}
if tag, ok := getKey(t, rootcomp, "listTest (compound)", TAG_List); ok {
l := tag.Payload.(TagList)
if l.Type != TAG_Compound {
t.Errorf("\"listTest (compound)\" is list of type %s, expected list of type TAG_Compound", l.Type)
} else if len(l.Elems) != 2 {
t.Errorf("\"listTest (compound)\" has length %d, expected 2", len(l.Elems))
} else {
for i, elem := range l.Elems {
comp := elem.(TagCompound)
if tag, ok := getKey(t, comp, "name", TAG_String); ok {
tagPayloadCompare(t, fmt.Sprintf("listTest (compound) [%d] name", i), tag, fmt.Sprintf("Compound tag #%d", i))
}
if tag, ok := getKey(t, comp, "created-on", TAG_Long); ok {
tagPayloadCompare(t, fmt.Sprintf("listTest (compound) [%d] created-on", i), tag, int64(1264099775885))
}
}
}
}
if tag, ok := getKey(t, rootcomp, "listTest (long)", TAG_List); ok {
l := tag.Payload.(TagList)
if l.Type != TAG_Long {
t.Errorf("\"listTest (long)\" is list of type %s, expected list of type TAG_Long", l.Type)
} else if len(l.Elems) != 5 {
t.Errorf("\"listTest (long)\" has length %d, expected 5", len(l.Elems))
} else {
for i, want := range []int64{11, 12, 13, 14, 15} {
if have := l.Elems[i].(int64); have != want {
t.Errorf("listTest (long) [%d] = %d, expected %d", i, have, want)
}
}
}
}
if tag, ok := getKey(t, rootcomp, "nested compound test", TAG_Compound); ok {
comp := tag.Payload.(TagCompound)
nestedCompoundHelper(t, comp, "ham", "Hampus", 0.75)
nestedCompoundHelper(t, comp, "egg", "Eggbert", 0.5)
}
if tag, ok := getKey(t, rootcomp, "byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))", TAG_Byte_Array); ok {
data := tag.Payload.([]byte)
if len(data) != 1000 {
t.Errorf("byteArrayTest data has length %d, expected 1000", len(data))
} else {
for n := 0; n < 1000; n++ {
want := byteArrayTestSeries(n)
if data[n] != want {
t.Errorf("Wrong byteArrayTest data at index %d: 0x%02x", n, data[n])
break
}
}
}
}
}
func makeNested(name string, value float32) Tag {
comp := make(TagCompound)
comp["name"] = NewStringTag(name)
comp["value"] = NewFloatTag(value)
return Tag{TAG_Compound, comp}
}
func TestCreateBigtest(t *testing.T) {
rootcomp := make(TagCompound)
rootcomp["shortTest"] = NewShortTag(32767)
rootcomp["longTest"] = NewLongTag(9223372036854775807)
rootcomp["floatTest"] = NewFloatTag(0.49823147)
rootcomp["stringTest"] = NewStringTag("HELLO WORLD THIS IS A TEST STRING ÅÄÖ!")
rootcomp["intTest"] = NewIntTag(2147483647)
rootcomp["byteTest"] = NewByteTag(127)
rootcomp["doubleTest"] = NewDoubleTag(0.4931287132182315)
comp := make(TagCompound)
comp["ham"] = makeNested("Hampus", 0.75)
comp["egg"] = makeNested("Eggbert", 0.5)
rootcomp["nested compound test"] = Tag{TAG_Compound, comp}
rootcomp["listTest (long)"] = NewListTag(TAG_Long, []int64{11, 12, 13, 14, 15})
listcomp := make([]interface{}, 2)
for i := 0; i < 2; i++ {
comp := make(TagCompound)
comp["name"] = NewStringTag(fmt.Sprintf("Compound tag #%d", i))
comp["created-on"] = NewLongTag(1264099775885)
listcomp[i] = comp
}
rootcomp["listTest (compound)"] = NewListTag(TAG_Compound, listcomp)
data := make([]byte, 1000)
for n := 0; n < 1000; n++ {
data[n] = byteArrayTestSeries(n)
}
rootcomp["byteArrayTest (the first 1000 values of (n*n*255+n*7)%100, starting with n=0 (0, 62, 34, 16, 8, ...))"] = NewByteArrayTag(data)
tag := Tag{TAG_Compound, rootcomp}
buf := new(bytes.Buffer)
if err := WriteGzipdNamedTag(buf, "Level", tag); err != nil {
t.Fatalf("Could not write NBT data: %s", err)
}
testBigtest(buf, t)
}
|