-
Notifications
You must be signed in to change notification settings - Fork 47
/
CompoundTagTest.java
362 lines (322 loc) · 13.2 KB
/
CompoundTagTest.java
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package net.querz.nbt;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.Map;
import static org.junit.Assert.assertNotEquals;
public class CompoundTagTest extends NBTTestCase {
private CompoundTag createCompoundTag() {
CompoundTag ct = new CompoundTag();
invokeSetValue(ct, new LinkedHashMap<>());
ct.put("b", new ByteTag(Byte.MAX_VALUE));
ct.put("str", new StringTag("foo"));
ct.put("list", new ListTag<>());
ct.getListTag("list").addByte((byte) 123);
return ct;
}
public void testStringConversion() {
CompoundTag ct = createCompoundTag();
assertEquals("{b:127b,str:foo,list:[123b]}", ct.toTagString());
assertEquals("{\"type\":\"CompoundTag\"," +
"\"value\":{" +
"\"b\":{\"type\":\"ByteTag\",\"value\":127}," +
"\"str\":{\"type\":\"StringTag\",\"value\":\"foo\"}," +
"\"list\":{\"type\":\"ListTag\"," +
"\"value\":{\"type\":\"ByteTag\",\"list\":[{\"type\":\"ByteTag\",\"value\":123}]}}}}", ct.toString());
}
public void testEquals() {
CompoundTag ct = createCompoundTag();
CompoundTag ct2 = new CompoundTag();
ct2.putByte("b", Byte.MAX_VALUE);
ct2.putString("str", "foo");
ct2.put("list", new ListTag<>());
ct2.getListTag("list").addByte((byte) 123);
assertTrue(ct.equals(ct2));
ct2.getListTag("list").asByteTagList().get(0).setValue((byte) 124);
assertFalse(ct.equals(ct2));
ct2.remove("str");
assertFalse(ct.equals(ct2));
assertThrowsNoRuntimeException(() -> ct.equals("blah"));
assertFalse(ct.equals("blah"));
assertEquals(ct, ct);
}
public void testHashCode() {
CompoundTag t = new CompoundTag();
for (int i = 0; i < 256; i++) {
t.putByte("key_byte" + i, (byte) i);
t.putShort("key_short" + i, (short) i);
t.putInt("key_int" + i, i);
t.putLong("key_long" + i, i);
t.putFloat("key_float" + i, i * 1.001f);
t.putDouble("key_double" + i, i * 1.001);
t.putString("key_string" + i, "value" + i);
byte[] bArray = new byte[257];
int[] iArray = new int[257];
long[] lArray = new long[257];
for (byte b = -128; b < 127; b++) {
bArray[b + 128] = b;
iArray[b + 128] = b;
lArray[b + 128] = b;
}
bArray[256] = (byte) i;
iArray[256] = i;
lArray[256] = i;
t.putByteArray("key_byte_array" + i, bArray);
t.putIntArray("key_int_array" + i, iArray);
t.putLongArray("key_long_array" + i, lArray);
ListTag<StringTag> l = new ListTag<>();
for (int j = 0; j < 256; j++) {
l.addString("value" + j);
}
l.addString("value" + i);
t.put("key_list" + i, l);
CompoundTag c = new CompoundTag();
c.putString("key_string" + i, "value" + i);
t.put("key_child" + i, c);
}
CompoundTag t2 = new CompoundTag();
for (int i = 0; i < 256; i++) {
t2.putString("key_string" + i, "value" + i);
t2.putDouble("key_double" + i, i * 1.001);
t2.putFloat("key_float" + i, i * 1.001f);
t2.putLong("key_long" + i, i);
t2.putInt("key_int" + i, i);
t2.putShort("key_short" + i, (short) i);
t2.putByte("key_byte" + i, (byte) i);
byte[] bArray = new byte[257];
int[] iArray = new int[257];
long[] lArray = new long[257];
for (byte b = -128; b < 127; b++) {
bArray[b + 128] = b;
iArray[b + 128] = b;
lArray[b + 128] = b;
}
bArray[256] = (byte) i;
iArray[256] = i;
lArray[256] = i;
t2.putByteArray("key_byte_array" + i, bArray);
t2.putIntArray("key_int_array" + i, iArray);
t2.putLongArray("key_long_array" + i, lArray);
ListTag<StringTag> l = new ListTag<>();
for (int j = 0; j < 256; j++) {
l.addString("value" + j);
}
l.addString("value" + i);
t2.put("key_list" + i, l);
CompoundTag c = new CompoundTag();
c.putString("key_string" + i, "value" + i);
t2.put("key_child" + i, c);
}
assertEquals(t, t2);
assertEquals(t.hashCode(), t2.hashCode());
t.getCompoundTag("key_child1").remove("key_string1");
assertNotEquals(t, t2);
assertNotEquals(t.hashCode(), t2.hashCode());
}
public void testClone() {
CompoundTag ct = createCompoundTag();
CompoundTag cl = ct.clone();
assertTrue(ct.equals(cl));
assertFalse(ct == cl);
assertFalse(ct.get("list") == cl.get("list"));
assertFalse(invokeGetValue(ct) == invokeGetValue(cl));
}
public void testClear() {
CompoundTag cclear = new CompoundTag();
cclear.putString("test", "blah");
assertEquals(1, cclear.size());
cclear.clear();
assertEquals(0, cclear.size());
}
public void testSerializeDeserialize() {
CompoundTag ct = createCompoundTag();
byte[] data = serialize(ct);
assertTrue(Arrays.equals(new byte[]{10, 0, 0, 1, 0, 1, 98, 127, 8, 0, 3, 115, 116, 114, 0, 3, 102, 111, 111, 9, 0, 4, 108, 105, 115, 116, 1, 0, 0, 0, 1, 123, 0}, data));
CompoundTag tt = (CompoundTag) deserialize(data);
assertTrue(ct.equals(tt));
}
public void testCasting() {
CompoundTag cc = new CompoundTag();
assertNull(cc.get("b", ByteTag.class));
assertNull(cc.get("b"));
cc.putByte("b", Byte.MAX_VALUE);
assertEquals(new ByteTag(Byte.MAX_VALUE), cc.getByteTag("b"));
assertNull(cc.getByteTag("bb"));
assertEquals(Byte.MAX_VALUE, cc.get("b", ByteTag.class).asByte());
assertThrowsRuntimeException(() -> cc.getShort("b"), ClassCastException.class);
assertEquals(0, cc.getByte("bb"));
assertEquals(true, cc.getBoolean("b"));
cc.putByte("b2", (byte) 0);
assertEquals(false, cc.getBoolean("b2"));
cc.putBoolean("b3", false);
assertEquals(0, cc.getByte("b3"));
cc.putBoolean("b4", true);
assertEquals(1, cc.getByte("b4"));
cc.putShort("s", Short.MAX_VALUE);
assertEquals(new ShortTag(Short.MAX_VALUE), cc.getShortTag("s"));
assertNull(cc.getShortTag("ss"));
assertEquals(Short.MAX_VALUE, cc.get("s", ShortTag.class).asShort());
assertThrowsRuntimeException(() -> cc.getInt("s"), ClassCastException.class);
assertEquals(0, cc.getShort("ss"));
cc.putInt("i", Integer.MAX_VALUE);
assertEquals(new IntTag(Integer.MAX_VALUE), cc.getIntTag("i"));
assertNull(cc.getIntTag("ii"));
assertEquals(Integer.MAX_VALUE, cc.get("i", IntTag.class).asInt());
assertThrowsRuntimeException(() -> cc.getLong("i"), ClassCastException.class);
assertEquals(0, cc.getInt("ii"));
cc.putLong("l", Long.MAX_VALUE);
assertEquals(new LongTag(Long.MAX_VALUE), cc.getLongTag("l"));
assertNull(cc.getLongTag("ll"));
assertEquals(Long.MAX_VALUE, cc.get("l", LongTag.class).asLong());
assertThrowsRuntimeException(() -> cc.getFloat("l"), ClassCastException.class);
assertEquals(0, cc.getLong("ll"));
cc.putFloat("f", Float.MAX_VALUE);
assertEquals(new FloatTag(Float.MAX_VALUE), cc.getFloatTag("f"));
assertNull(cc.getFloatTag("ff"));
assertEquals(Float.MAX_VALUE, cc.get("f", FloatTag.class).asFloat());
assertThrowsRuntimeException(() -> cc.getDouble("f"), ClassCastException.class);
assertEquals(0.0F, cc.getFloat("ff"));
cc.putDouble("d", Double.MAX_VALUE);
assertEquals(new DoubleTag(Double.MAX_VALUE), cc.getDoubleTag("d"));
assertNull(cc.getDoubleTag("dd"));
assertEquals(Double.MAX_VALUE, cc.get("d", DoubleTag.class).asDouble());
assertThrowsRuntimeException(() -> cc.getString("d"), ClassCastException.class);
assertEquals(0.0D, cc.getDouble("dd"));
cc.putString("st", "foo");
assertEquals(new StringTag("foo"), cc.getStringTag("st"));
assertNull(cc.getStringTag("stst"));
assertEquals("foo", cc.get("st", StringTag.class).getValue());
assertThrowsRuntimeException(() -> cc.getByteArray("st"), ClassCastException.class);
assertEquals("", cc.getString("stst"));
cc.putByteArray("ba", new byte[]{Byte.MIN_VALUE, 0, Byte.MAX_VALUE});
assertEquals(new ByteArrayTag(new byte[]{Byte.MIN_VALUE, 0, Byte.MAX_VALUE}), cc.getByteArrayTag("ba"));
assertNull(cc.getByteArrayTag("baba"));
assertTrue(Arrays.equals(new byte[]{Byte.MIN_VALUE, 0, Byte.MAX_VALUE}, cc.get("ba", ByteArrayTag.class).getValue()));
assertThrowsRuntimeException(() -> cc.getIntArray("ba"), ClassCastException.class);
assertTrue(Arrays.equals(new byte[0], cc.getByteArray("baba")));
cc.putIntArray("ia", new int[]{Integer.MIN_VALUE, 0, Integer.MAX_VALUE});
assertEquals(new IntArrayTag(new int[]{Integer.MIN_VALUE, 0, Integer.MAX_VALUE}), cc.getIntArrayTag("ia"));
assertNull(cc.getIntArrayTag("iaia"));
assertTrue(Arrays.equals(new int[]{Integer.MIN_VALUE, 0, Integer.MAX_VALUE}, cc.get("ia", IntArrayTag.class).getValue()));
assertThrowsRuntimeException(() -> cc.getLongArray("ia"), ClassCastException.class);
assertTrue(Arrays.equals(new int[0], cc.getIntArray("iaia")));
cc.putLongArray("la", new long[]{Long.MIN_VALUE, 0, Long.MAX_VALUE});
assertEquals(new LongArrayTag(new long[]{Long.MIN_VALUE, 0, Long.MAX_VALUE}), cc.getLongArrayTag("la"));
assertNull(cc.getLongArrayTag("lala"));
assertTrue(Arrays.equals(new long[]{Long.MIN_VALUE, 0, Long.MAX_VALUE}, cc.get("la", LongArrayTag.class).getValue()));
assertThrowsRuntimeException(() -> cc.getListTag("la"), ClassCastException.class);
assertTrue(Arrays.equals(new long[0], cc.getLongArray("lala")));
cc.put("li", new ListTag<>());
assertEquals(new ListTag<>(), cc.getListTag("li"));
assertNull(cc.getListTag("lili"));
assertThrowsRuntimeException(() -> cc.getCompoundTag("li"), ClassCastException.class);
cc.put("co", new CompoundTag());
assertEquals(new CompoundTag(), cc.getCompoundTag("co"));
assertNull(cc.getCompoundTag("coco"));
assertThrowsRuntimeException(() -> cc.getByte("co"), ClassCastException.class);
}
public void testCompareTo() {
CompoundTag ci = new CompoundTag();
ci.putInt("one", 1);
ci.putInt("two", 2);
CompoundTag co = new CompoundTag();
co.putInt("three", 3);
co.putInt("four", 4);
assertEquals(0, ci.compareTo(co));
co.putInt("five", 5);
assertEquals(-1, ci.compareTo(co));
co.remove("five");
co.remove("four");
assertEquals(1, ci.compareTo(co));
assertEquals(0, ci.compareTo(null));
}
public void testMaxDepth() {
CompoundTag root = new CompoundTag();
CompoundTag rec = root;
for (int i = 0; i < Tag.MAX_DEPTH + 1; i++) {
CompoundTag c = new CompoundTag();
rec.put("c" + i, c);
rec = c;
}
assertThrowsRuntimeException(() -> serialize(root), MaxDepthReachedException.class);
assertThrowsRuntimeException(() -> deserializeFromFile("max_depth_reached.dat"), MaxDepthReachedException.class);
assertThrowsRuntimeException(root::toString, MaxDepthReachedException.class);
assertThrowsRuntimeException(root::toTagString, MaxDepthReachedException.class);
assertThrowsRuntimeException(() -> root.valueToString(-1), IllegalArgumentException.class);
assertThrowsRuntimeException(() -> root.valueToTagString(-1), IllegalArgumentException.class);
}
public void testRecursion() {
CompoundTag recursive = new CompoundTag();
recursive.put("recursive", recursive);
assertThrowsRuntimeException(() -> serialize(recursive), MaxDepthReachedException.class);
assertThrowsRuntimeException(recursive::toString, MaxDepthReachedException.class);
assertThrowsRuntimeException(recursive::toTagString, MaxDepthReachedException.class);
}
public void testEntrySet() {
CompoundTag e = new CompoundTag();
e.putInt("int", 123);
for (Map.Entry<String, Tag> en : e.entrySet()) {
assertThrowsRuntimeException(() -> en.setValue(null), NullPointerException.class);
assertThrowsNoRuntimeException(() -> en.setValue(new IntTag(321)));
}
assertEquals(1, e.size());
assertEquals(321, e.getInt("int"));
}
public void testContains() {
CompoundTag ct = createCompoundTag();
assertTrue(3 == ct.size());
assertTrue(ct.containsKey("b"));
assertTrue(ct.containsKey("str"));
assertTrue(ct.containsKey("list"));
assertFalse(ct.containsKey("invalid"));
assertTrue(ct.containsValue(new StringTag("foo")));
ListTag<ByteTag> l = new ListTag<>();
l.addByte((byte) 123);
assertTrue(ct.containsValue(l));
assertTrue(ct.containsValue(new ByteTag(Byte.MAX_VALUE)));
assertFalse(ct.containsValue(new ByteTag(Byte.MIN_VALUE)));
assertFalse(ct.containsKey("blah"));
}
public void testIterator() {
CompoundTag ct = createCompoundTag();
for (Tag t : ct.values()) {
assertNotNull(t);
}
ct.values().remove(new StringTag("foo"));
assertFalse(ct.containsKey("str"));
assertThrowsRuntimeException(() -> ct.values().add(new StringTag("foo")), UnsupportedOperationException.class);
ct.putString("str", "foo");
for (String k : ct.keySet()) {
assertNotNull(k);
assertTrue(ct.containsKey(k));
}
ct.keySet().remove("str");
assertFalse(ct.containsKey("str"));
assertThrowsRuntimeException(() -> ct.keySet().add("str"), UnsupportedOperationException.class);
ct.putString("str", "foo");
for (Map.Entry<String, Tag> e : ct.entrySet()) {
assertNotNull(e.getKey());
assertNotNull(e.getValue());
assertThrowsRuntimeException(() -> e.setValue(null), NullPointerException.class);
if (e.getKey().equals("str")) {
assertThrowsNoRuntimeException(() -> e.setValue(new StringTag("bar")));
}
}
assertTrue(ct.containsKey("str"));
assertEquals("bar", ct.getString("str"));
for (Map.Entry<String, Tag> e : ct) {
assertNotNull(e.getKey());
assertNotNull(e.getValue());
assertThrowsRuntimeException(() -> e.setValue(null), NullPointerException.class);
if (e.getKey().equals("str")) {
assertThrowsNoRuntimeException(() -> e.setValue(new StringTag("foo")));
}
}
assertTrue(ct.containsKey("str"));
assertEquals("foo", ct.getString("str"));
ct.forEach((k, v) -> {
assertNotNull(k);
assertNotNull(v);
});
assertEquals(3, ct.size());
}
}