0
|
1 package org.msgpack.unpacker;
|
|
2
|
|
3 import static org.junit.Assert.assertEquals;
|
|
4 import static org.junit.Assert.assertTrue;
|
|
5
|
|
6 import java.util.ArrayList;
|
|
7 import java.util.List;
|
|
8 import java.util.Random;
|
|
9
|
|
10 import org.msgpack.MessagePack;
|
|
11 import org.msgpack.type.Value;
|
|
12 import org.msgpack.type.ValueFactory;
|
|
13 import org.msgpack.packer.BufferPacker;
|
|
14 import org.msgpack.unpacker.BufferUnpacker;
|
|
15 import org.msgpack.unpacker.UnpackerIterator;
|
|
16
|
|
17 import org.junit.Test;
|
|
18
|
|
19 public class TestBufferUnpacker {
|
|
20 @Test
|
|
21 public void testEachByte() throws Exception {
|
|
22 List<Value> vs = new ArrayList<Value>();
|
|
23
|
|
24 BufferPacker pk = new MessagePack().createBufferPacker();
|
|
25 for (int i = 0; i < 50; i++) {
|
|
26 Value v = createComplexType();
|
|
27 vs.add(v);
|
|
28 pk.write(v);
|
|
29 }
|
|
30 byte[] raw = pk.toByteArray();
|
|
31
|
|
32 int n = 0;
|
|
33 BufferUnpacker u = new MessagePack().createBufferUnpacker();
|
|
34 UnpackerIterator it = u.iterator();
|
|
35
|
|
36 for (int i = 0; i < raw.length; i++) {
|
|
37 u.feed(raw, i, 1);
|
|
38 while (it.hasNext()) {
|
|
39 Value v = it.next();
|
|
40 assertEquals(vs.get(n), v);
|
|
41 n++;
|
|
42 }
|
|
43 }
|
|
44 assertEquals(50, n);
|
|
45 }
|
|
46
|
|
47 @Test
|
|
48 public void testElevenBytes() throws Exception {
|
|
49 List<Value> vs = new ArrayList<Value>();
|
|
50
|
|
51 BufferPacker pk = new MessagePack().createBufferPacker();
|
|
52 for (int i = 0; i < 55; i++) {
|
|
53 Value v = createComplexType();
|
|
54 vs.add(v);
|
|
55 pk.write(v);
|
|
56 }
|
|
57 byte[] raw = pk.toByteArray();
|
|
58
|
|
59 int n = 0;
|
|
60 BufferUnpacker u = new MessagePack().createBufferUnpacker();
|
|
61 UnpackerIterator it = u.iterator();
|
|
62
|
|
63 for (int i = 0; i < raw.length; i += 11) {
|
|
64 u.feed(raw, i, 11);
|
|
65 while (it.hasNext()) {
|
|
66 Value v = it.next();
|
|
67 assertEquals(vs.get(n), v);
|
|
68 n++;
|
|
69 }
|
|
70 }
|
|
71 assertEquals(55, n);
|
|
72 }
|
|
73
|
|
74 @Test
|
|
75 public void testEachObject() throws Exception {
|
|
76 BufferUnpacker u = new MessagePack().createBufferUnpacker();
|
|
77 UnpackerIterator it = u.iterator();
|
|
78
|
|
79 for (int i = 0; i < 50; i++) {
|
|
80 Value v = createComplexType();
|
|
81 BufferPacker pk = new MessagePack().createBufferPacker();
|
|
82 pk.write(v);
|
|
83 byte[] raw = pk.toByteArray();
|
|
84 // pk.reset();
|
|
85
|
|
86 u.feed(raw, 0, raw.length);
|
|
87
|
|
88 assertTrue(it.hasNext());
|
|
89 Value ov = it.next();
|
|
90 assertEquals(v, ov);
|
|
91 // assertFalse(it.hasNext());
|
|
92 }
|
|
93 }
|
|
94
|
|
95 public Value createComplexType() throws Exception {
|
|
96 Random rand = new Random(System.currentTimeMillis());
|
|
97 byte[] b0 = new byte[0];
|
|
98 byte[] b1 = new byte[10];
|
|
99 rand.nextBytes(b1);
|
|
100 byte[] b2 = new byte[1024];
|
|
101 rand.nextBytes(b2);
|
|
102
|
|
103 Value list = ValueFactory.createArrayValue(new Value[] {
|
|
104 ValueFactory.createRawValue(b0),
|
|
105 ValueFactory.createRawValue(b1),
|
|
106 ValueFactory.createRawValue(b2), });
|
|
107
|
|
108 Value map = ValueFactory.createMapValue(new Value[] {
|
|
109 ValueFactory.createIntegerValue(0),
|
|
110 ValueFactory.createIntegerValue(Integer.MIN_VALUE),
|
|
111 ValueFactory.createIntegerValue(rand.nextInt()),
|
|
112 ValueFactory.createIntegerValue(Integer.MAX_VALUE),
|
|
113 ValueFactory.createFloatValue(rand.nextFloat()),
|
|
114 ValueFactory.createBooleanValue(true),
|
|
115 ValueFactory.createFloatValue(rand.nextDouble()),
|
|
116 ValueFactory.createNilValue(), });
|
|
117
|
|
118 List<Value> values = new ArrayList<Value>();
|
|
119
|
|
120 for (int i = 0; i < 2; i++) {
|
|
121 values.add(list);
|
|
122 for (int j = 0; j < 100; j++) {
|
|
123 values.add(map);
|
|
124 }
|
|
125 }
|
|
126
|
|
127 Value complex = ValueFactory.createArrayValue(values
|
|
128 .toArray(new Value[values.size()]));
|
|
129 return complex;
|
|
130 }
|
|
131 }
|