|
| 1 | +package org.msgpack.io; |
| 2 | + |
| 3 | +import java.math.BigInteger; |
| 4 | +import java.nio.ByteBuffer; |
| 5 | +import java.io.IOException; |
| 6 | +import java.io.DataOutputStream; |
| 7 | +import java.io.ByteArrayOutputStream; |
| 8 | +import java.util.ArrayList; |
| 9 | +import java.util.HashMap; |
| 10 | +import java.util.Iterator; |
| 11 | +import java.util.List; |
| 12 | +import java.util.Map; |
| 13 | +import java.util.Arrays; |
| 14 | + |
| 15 | +import org.junit.Test; |
| 16 | +import org.msgpack.MessagePack; |
| 17 | +import org.msgpack.value.Value; |
| 18 | + |
| 19 | +import junit.framework.TestCase; |
| 20 | + |
| 21 | +public class TestLinkedBufferOutput extends TestCase { |
| 22 | + @Test |
| 23 | + public void testGetSize() throws IOException { |
| 24 | + LinkedBufferOutput o = new LinkedBufferOutput(10); |
| 25 | + for(int i=0; i < 21; i++) { |
| 26 | + o.writeByte((byte)1); |
| 27 | + assertEquals(i+1, o.getSize()); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + @Test |
| 32 | + public void testWritePrimitives() throws IOException { |
| 33 | + ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
| 34 | + DataOutputStream o1 = new DataOutputStream(bo); |
| 35 | + LinkedBufferOutput o2 = new LinkedBufferOutput(10); |
| 36 | + o1.writeByte((byte)2); |
| 37 | + o2.writeByte((byte)2); |
| 38 | + o1.writeShort((short)2); |
| 39 | + o2.writeShort((short)2); |
| 40 | + o1.writeInt((int)2); |
| 41 | + o2.writeInt((int)2); |
| 42 | + o1.writeLong(2L); |
| 43 | + o2.writeLong(2L); |
| 44 | + o1.writeFloat(1.1f); |
| 45 | + o2.writeFloat(1.1f); |
| 46 | + o1.writeDouble(1.1); |
| 47 | + o2.writeDouble(1.1); |
| 48 | + byte[] b1 = bo.toByteArray(); |
| 49 | + byte[] b2 = o2.toByteArray(); |
| 50 | + assertEquals(b1.length, b2.length); |
| 51 | + assertTrue(Arrays.equals(b1, b2)); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void testByteAndWritePrimitives() throws IOException { |
| 56 | + ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
| 57 | + DataOutputStream o1 = new DataOutputStream(bo); |
| 58 | + LinkedBufferOutput o2 = new LinkedBufferOutput(10); |
| 59 | + o1.writeByte((byte)9); |
| 60 | + o1.writeByte((byte)2); |
| 61 | + o2.writeByteAndByte((byte)9, (byte)2); |
| 62 | + o1.writeByte((byte)9); |
| 63 | + o1.writeShort((short)2); |
| 64 | + o2.writeByteAndShort((byte)9, (short)2); |
| 65 | + o1.writeByte((byte)9); |
| 66 | + o1.writeInt((int)2); |
| 67 | + o2.writeByteAndInt((byte)9, (int)2); |
| 68 | + o1.writeByte((byte)9); |
| 69 | + o1.writeLong(2L); |
| 70 | + o2.writeByteAndLong((byte)9, 2L); |
| 71 | + o1.writeByte((byte)9); |
| 72 | + o1.writeFloat(1.1f); |
| 73 | + o2.writeByteAndFloat((byte)9, 1.1f); |
| 74 | + o1.writeByte((byte)9); |
| 75 | + o1.writeDouble(1.1); |
| 76 | + o2.writeByteAndDouble((byte)9, 1.1); |
| 77 | + byte[] b1 = bo.toByteArray(); |
| 78 | + byte[] b2 = o2.toByteArray(); |
| 79 | + assertEquals(b1.length, b2.length); |
| 80 | + assertTrue(Arrays.equals(b1, b2)); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testWrite() throws IOException { |
| 85 | + ByteArrayOutputStream bo = new ByteArrayOutputStream(); |
| 86 | + DataOutputStream o1 = new DataOutputStream(bo); |
| 87 | + LinkedBufferOutput o2 = new LinkedBufferOutput(10); |
| 88 | + byte[] raw = new byte[9]; |
| 89 | + raw[0] = (byte)1; |
| 90 | + raw[7] = (byte)1; |
| 91 | + for(int i=0; i < 11; i++) { |
| 92 | + o1.write(raw, 0, raw.length); |
| 93 | + o2.write(raw, 0, raw.length); |
| 94 | + } |
| 95 | + byte[] b1 = bo.toByteArray(); |
| 96 | + byte[] b2 = o2.toByteArray(); |
| 97 | + assertEquals(b1.length, b2.length); |
| 98 | + assertTrue(Arrays.equals(b1, b2)); |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments