This source file includes following definitions.
- bytes
- toList
- assertEqualBytes
- assertWriteVarint
- testWriteVarint
- assertWriteLittleEndian32
- assertWriteLittleEndian64
- testWriteLittleEndian
- testEncodeZigZag
- testWriteWholeMessage
- testWriteWholePackedFieldsMessage
- testWriteMessageWithNegativeEnumValue
package com.google.protobuf;
import protobuf_unittest.UnittestProto.SparseEnumMessage;
import protobuf_unittest.UnittestProto.TestAllTypes;
import protobuf_unittest.UnittestProto.TestPackedTypes;
import protobuf_unittest.UnittestProto.TestSparseEnum;
import junit.framework.TestCase;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
public class CodedOutputStreamTest extends TestCase {
private byte[] bytes(int... bytesAsInts) {
byte[] bytes = new byte[bytesAsInts.length];
for (int i = 0; i < bytesAsInts.length; i++) {
bytes[i] = (byte) bytesAsInts[i];
}
return bytes;
}
private List<Byte> toList(byte[] bytes) {
List<Byte> result = new ArrayList<Byte>();
for (byte b : bytes) {
result.add(b);
}
return result;
}
private void assertEqualBytes(byte[] a, byte[] b) {
assertEquals(toList(a), toList(b));
}
private void assertWriteVarint(byte[] data, long value) throws Exception {
if ((value >>> 32) == 0) {
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
output.writeRawVarint32((int) value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
assertEquals(data.length,
CodedOutputStream.computeRawVarint32Size((int) value));
}
{
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
output.writeRawVarint64(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
assertEquals(data.length,
CodedOutputStream.computeRawVarint64Size(value));
}
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
if ((value >>> 32) == 0) {
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output =
CodedOutputStream.newInstance(rawOutput, blockSize);
output.writeRawVarint32((int) value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
}
{
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output =
CodedOutputStream.newInstance(rawOutput, blockSize);
output.writeRawVarint64(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
}
}
}
public void testWriteVarint() throws Exception {
assertWriteVarint(bytes(0x00), 0);
assertWriteVarint(bytes(0x01), 1);
assertWriteVarint(bytes(0x7f), 127);
assertWriteVarint(bytes(0xa2, 0x74), (0x22 << 0) | (0x74 << 7));
assertWriteVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x0b),
(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
(0x0bL << 28));
assertWriteVarint(bytes(0xbe, 0xf7, 0x92, 0x84, 0x1b),
(0x3e << 0) | (0x77 << 7) | (0x12 << 14) | (0x04 << 21) |
(0x1bL << 28));
assertWriteVarint(
bytes(0x80, 0xe6, 0xeb, 0x9c, 0xc3, 0xc9, 0xa4, 0x49),
(0x00 << 0) | (0x66 << 7) | (0x6b << 14) | (0x1c << 21) |
(0x43L << 28) | (0x49L << 35) | (0x24L << 42) | (0x49L << 49));
assertWriteVarint(
bytes(0x9b, 0xa8, 0xf9, 0xc2, 0xbb, 0xd6, 0x80, 0x85, 0xa6, 0x01),
(0x1b << 0) | (0x28 << 7) | (0x79 << 14) | (0x42 << 21) |
(0x3bL << 28) | (0x56L << 35) | (0x00L << 42) |
(0x05L << 49) | (0x26L << 56) | (0x01L << 63));
}
private void assertWriteLittleEndian32(byte[] data, int value)
throws Exception {
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
output.writeRawLittleEndian32(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
rawOutput = new ByteArrayOutputStream();
output = CodedOutputStream.newInstance(rawOutput, blockSize);
output.writeRawLittleEndian32(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
}
}
private void assertWriteLittleEndian64(byte[] data, long value)
throws Exception {
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output = CodedOutputStream.newInstance(rawOutput);
output.writeRawLittleEndian64(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
for (int blockSize = 1; blockSize <= 16; blockSize *= 2) {
rawOutput = new ByteArrayOutputStream();
output = CodedOutputStream.newInstance(rawOutput, blockSize);
output.writeRawLittleEndian64(value);
output.flush();
assertEqualBytes(data, rawOutput.toByteArray());
}
}
public void testWriteLittleEndian() throws Exception {
assertWriteLittleEndian32(bytes(0x78, 0x56, 0x34, 0x12), 0x12345678);
assertWriteLittleEndian32(bytes(0xf0, 0xde, 0xbc, 0x9a), 0x9abcdef0);
assertWriteLittleEndian64(
bytes(0xf0, 0xde, 0xbc, 0x9a, 0x78, 0x56, 0x34, 0x12),
0x123456789abcdef0L);
assertWriteLittleEndian64(
bytes(0x78, 0x56, 0x34, 0x12, 0xf0, 0xde, 0xbc, 0x9a),
0x9abcdef012345678L);
}
public void testEncodeZigZag() throws Exception {
assertEquals(0, CodedOutputStream.encodeZigZag32( 0));
assertEquals(1, CodedOutputStream.encodeZigZag32(-1));
assertEquals(2, CodedOutputStream.encodeZigZag32( 1));
assertEquals(3, CodedOutputStream.encodeZigZag32(-2));
assertEquals(0x7FFFFFFE, CodedOutputStream.encodeZigZag32(0x3FFFFFFF));
assertEquals(0x7FFFFFFF, CodedOutputStream.encodeZigZag32(0xC0000000));
assertEquals(0xFFFFFFFE, CodedOutputStream.encodeZigZag32(0x7FFFFFFF));
assertEquals(0xFFFFFFFF, CodedOutputStream.encodeZigZag32(0x80000000));
assertEquals(0, CodedOutputStream.encodeZigZag64( 0));
assertEquals(1, CodedOutputStream.encodeZigZag64(-1));
assertEquals(2, CodedOutputStream.encodeZigZag64( 1));
assertEquals(3, CodedOutputStream.encodeZigZag64(-2));
assertEquals(0x000000007FFFFFFEL,
CodedOutputStream.encodeZigZag64(0x000000003FFFFFFFL));
assertEquals(0x000000007FFFFFFFL,
CodedOutputStream.encodeZigZag64(0xFFFFFFFFC0000000L));
assertEquals(0x00000000FFFFFFFEL,
CodedOutputStream.encodeZigZag64(0x000000007FFFFFFFL));
assertEquals(0x00000000FFFFFFFFL,
CodedOutputStream.encodeZigZag64(0xFFFFFFFF80000000L));
assertEquals(0xFFFFFFFFFFFFFFFEL,
CodedOutputStream.encodeZigZag64(0x7FFFFFFFFFFFFFFFL));
assertEquals(0xFFFFFFFFFFFFFFFFL,
CodedOutputStream.encodeZigZag64(0x8000000000000000L));
assertEquals(0,
CodedOutputStream.encodeZigZag32(CodedInputStream.decodeZigZag32(0)));
assertEquals(1,
CodedOutputStream.encodeZigZag32(CodedInputStream.decodeZigZag32(1)));
assertEquals(-1,
CodedOutputStream.encodeZigZag32(CodedInputStream.decodeZigZag32(-1)));
assertEquals(14927,
CodedOutputStream.encodeZigZag32(CodedInputStream.decodeZigZag32(14927)));
assertEquals(-3612,
CodedOutputStream.encodeZigZag32(CodedInputStream.decodeZigZag32(-3612)));
assertEquals(0,
CodedOutputStream.encodeZigZag64(CodedInputStream.decodeZigZag64(0)));
assertEquals(1,
CodedOutputStream.encodeZigZag64(CodedInputStream.decodeZigZag64(1)));
assertEquals(-1,
CodedOutputStream.encodeZigZag64(CodedInputStream.decodeZigZag64(-1)));
assertEquals(14927,
CodedOutputStream.encodeZigZag64(CodedInputStream.decodeZigZag64(14927)));
assertEquals(-3612,
CodedOutputStream.encodeZigZag64(CodedInputStream.decodeZigZag64(-3612)));
assertEquals(856912304801416L,
CodedOutputStream.encodeZigZag64(
CodedInputStream.decodeZigZag64(
856912304801416L)));
assertEquals(-75123905439571256L,
CodedOutputStream.encodeZigZag64(
CodedInputStream.decodeZigZag64(
-75123905439571256L)));
}
public void testWriteWholeMessage() throws Exception {
TestAllTypes message = TestUtil.getAllSet();
byte[] rawBytes = message.toByteArray();
assertEqualBytes(TestUtil.getGoldenMessage().toByteArray(), rawBytes);
for (int blockSize = 1; blockSize < 256; blockSize *= 2) {
ByteArrayOutputStream rawOutput = new ByteArrayOutputStream();
CodedOutputStream output =
CodedOutputStream.newInstance(rawOutput, blockSize);
message.writeTo(output);
output.flush();
assertEqualBytes(rawBytes, rawOutput.toByteArray());
}
}
public void testWriteWholePackedFieldsMessage() throws Exception {
TestPackedTypes message = TestUtil.getPackedSet();
byte[] rawBytes = message.toByteArray();
assertEqualBytes(TestUtil.getGoldenPackedFieldsMessage().toByteArray(),
rawBytes);
}
public void testWriteMessageWithNegativeEnumValue() throws Exception {
SparseEnumMessage message = SparseEnumMessage.newBuilder()
.setSparseEnum(TestSparseEnum.SPARSE_E) .build();
assertTrue(message.getSparseEnum().getNumber() < 0);
byte[] rawBytes = message.toByteArray();
SparseEnumMessage message2 = SparseEnumMessage.parseFrom(rawBytes);
assertEquals(TestSparseEnum.SPARSE_E, message2.getSparseEnum());
}
}