|
| 1 | +package org.msgpack.unpacker; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertTrue; |
| 5 | +import static org.junit.Assert.fail; |
| 6 | + |
| 7 | +import java.math.BigInteger; |
| 8 | +import java.nio.ByteBuffer; |
| 9 | +import java.io.IOException; |
| 10 | +import java.io.EOFException; |
| 11 | +import java.io.DataInputStream; |
| 12 | +import java.io.ByteArrayInputStream; |
| 13 | +import java.io.DataOutputStream; |
| 14 | +import java.io.ByteArrayOutputStream; |
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.HashMap; |
| 17 | +import java.util.Iterator; |
| 18 | +import java.util.List; |
| 19 | +import java.util.Map; |
| 20 | +import java.util.Arrays; |
| 21 | +import java.util.Random; |
| 22 | + |
| 23 | +import org.msgpack.MessagePack; |
| 24 | +import org.msgpack.type.Value; |
| 25 | +import org.msgpack.type.ValueFactory; |
| 26 | +import org.msgpack.packer.BufferPacker; |
| 27 | +import org.msgpack.unpacker.BufferUnpacker; |
| 28 | +import org.msgpack.unpacker.UnpackerIterator; |
| 29 | + |
| 30 | +import org.junit.Test; |
| 31 | + |
| 32 | +public class TestBufferUnpacker { |
| 33 | + @Test |
| 34 | + public void testEachByte() throws Exception { |
| 35 | + List<Value> vs = new ArrayList<Value>(); |
| 36 | + |
| 37 | + BufferPacker pk = new MessagePack().createBufferPacker(); |
| 38 | + for(int i=0; i < 50; i++) { |
| 39 | + Value v = createComplexType(); |
| 40 | + vs.add(v); |
| 41 | + pk.write(v); |
| 42 | + } |
| 43 | + byte[] raw = pk.toByteArray(); |
| 44 | + |
| 45 | + int n = 0; |
| 46 | + BufferUnpacker u = new MessagePack().createBufferUnpacker(); |
| 47 | + UnpackerIterator it = u.iterator(); |
| 48 | + |
| 49 | + for(int i=0; i < raw.length; i++) { |
| 50 | + u.feed(raw, i, 1); |
| 51 | + while(it.hasNext()) { |
| 52 | + Value v = it.next(); |
| 53 | + assertEquals(vs.get(n), v); |
| 54 | + n++; |
| 55 | + } |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + public Value createComplexType() throws Exception { |
| 60 | + Random rand = new Random(System.currentTimeMillis()); |
| 61 | + byte[] b0 = new byte[0]; |
| 62 | + byte[] b1 = new byte[10]; |
| 63 | + rand.nextBytes(b1); |
| 64 | + byte[] b2 = new byte[1024]; |
| 65 | + rand.nextBytes(b2); |
| 66 | + |
| 67 | + Value list = ValueFactory.arrayValue(new Value[] { |
| 68 | + ValueFactory.rawValue(b0), |
| 69 | + ValueFactory.rawValue(b1), |
| 70 | + ValueFactory.rawValue(b2), |
| 71 | + }); |
| 72 | + |
| 73 | + Value map = ValueFactory.mapValue(new Value[] { |
| 74 | + ValueFactory.integerValue(0), ValueFactory.integerValue(Integer.MIN_VALUE), |
| 75 | + ValueFactory.integerValue(rand.nextInt()), ValueFactory.integerValue(Integer.MAX_VALUE), |
| 76 | + ValueFactory.floatValue(rand.nextFloat()), ValueFactory.booleanValue(true), |
| 77 | + ValueFactory.floatValue(rand.nextDouble()), ValueFactory.nilValue(), |
| 78 | + }); |
| 79 | + |
| 80 | + List<Value> values = new ArrayList<Value>(); |
| 81 | + |
| 82 | + for(int i=0; i < 2; i++) { |
| 83 | + values.add(list); |
| 84 | + for(int j=0; j < 100; j++) { |
| 85 | + values.add(map); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + Value complex = ValueFactory.arrayValue(values.toArray(new Value[values.size()])); |
| 90 | + return complex; |
| 91 | + } |
| 92 | +} |
| 93 | + |
0 commit comments