comparison src/test/java/org/msgpack/io/TestLinkedBufferOutput.java @ 0:cb825acd883a

first commit
author sugi
date Sat, 18 Oct 2014 15:06:15 +0900
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:cb825acd883a
1 package org.msgpack.io;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertArrayEquals;
5
6 import java.io.IOException;
7 import java.io.DataOutputStream;
8 import java.io.ByteArrayOutputStream;
9
10 import org.junit.Test;
11
12
13 public class TestLinkedBufferOutput {
14 @Test
15 public void testGetSize() throws IOException {
16 LinkedBufferOutput o = new LinkedBufferOutput(10);
17 for(int i=0; i < 21; i++) {
18 o.writeByte((byte)1);
19 assertEquals(i+1, o.getSize());
20 }
21 }
22
23 @Test
24 public void testWritePrimitives() throws IOException {
25 ByteArrayOutputStream bo = new ByteArrayOutputStream();
26 DataOutputStream o1 = new DataOutputStream(bo);
27 LinkedBufferOutput o2 = new LinkedBufferOutput(10);
28 o1.writeByte((byte)2);
29 o2.writeByte((byte)2);
30 o1.writeShort((short)2);
31 o2.writeShort((short)2);
32 o1.writeInt(2);
33 o2.writeInt(2);
34 o1.writeLong(2L);
35 o2.writeLong(2L);
36 o1.writeFloat(1.1f);
37 o2.writeFloat(1.1f);
38 o1.writeDouble(1.1);
39 o2.writeDouble(1.1);
40 byte[] b1 = bo.toByteArray();
41 byte[] b2 = o2.toByteArray();
42 assertEquals(b1.length, b2.length);
43 assertArrayEquals(b1, b2);
44 }
45
46 @Test
47 public void testWriteByteAndPrimitives() throws IOException {
48 ByteArrayOutputStream bo = new ByteArrayOutputStream();
49 DataOutputStream o1 = new DataOutputStream(bo);
50 LinkedBufferOutput o2 = new LinkedBufferOutput(10);
51 o1.writeByte((byte)9);
52 o1.writeByte((byte)2);
53 o2.writeByteAndByte((byte)9, (byte)2);
54 o1.writeByte((byte)9);
55 o1.writeShort((short)2);
56 o2.writeByteAndShort((byte)9, (short)2);
57 o1.writeByte((byte)9);
58 o1.writeInt(2);
59 o2.writeByteAndInt((byte)9, 2);
60 o1.writeByte((byte)9);
61 o1.writeLong(2L);
62 o2.writeByteAndLong((byte)9, 2L);
63 o1.writeByte((byte)9);
64 o1.writeFloat(1.1f);
65 o2.writeByteAndFloat((byte)9, 1.1f);
66 o1.writeByte((byte)9);
67 o1.writeDouble(1.1);
68 o2.writeByteAndDouble((byte)9, 1.1);
69 byte[] b1 = bo.toByteArray();
70 byte[] b2 = o2.toByteArray();
71 assertEquals(b1.length, b2.length);
72 assertArrayEquals(b1, b2);
73 }
74
75 @Test
76 public void testWrite() throws IOException {
77 ByteArrayOutputStream bo = new ByteArrayOutputStream();
78 DataOutputStream o1 = new DataOutputStream(bo);
79 LinkedBufferOutput o2 = new LinkedBufferOutput(10);
80 byte[] raw = new byte[9];
81 raw[0] = (byte)1;
82 raw[7] = (byte)1;
83 for(int i=0; i < 11; i++) {
84 o1.write(raw, 0, raw.length);
85 o2.write(raw, 0, raw.length);
86 }
87 byte[] b1 = bo.toByteArray();
88 byte[] b2 = o2.toByteArray();
89 assertEquals(b1.length, b2.length);
90 assertArrayEquals(b1, b2);
91 }
92 }
93