capnproto-java-rpc/runtime/src/test/scala/org/capnproto/SerializePackedTest.scala

45 lines
1.8 KiB
Scala
Raw Normal View History

2014-09-26 15:27:58 +00:00
package org.capnproto;
import org.scalatest.FunSuite;
import org.scalatest.Matchers._;
import java.nio.ByteBuffer;
class SerializePackedSuite extends FunSuite {
def expectPacksTo(unpacked : Array[Byte], packed : Array[Byte]) {
// ----
// write
val bytes = new Array[Byte](packed.length);
val writer = new ArrayOutputStream(ByteBuffer.wrap(bytes));
val packedOutputStream = new PackedOutputStream (writer);
packedOutputStream.write(ByteBuffer.wrap(unpacked));
(bytes) should equal (packed);
}
test("SimplePacking") {
expectPacksTo(Array(), Array());
2014-09-26 15:54:04 +00:00
expectPacksTo(Array(0,0,12,0,0,34,0,0), Array(0x24,12,34));
2014-09-26 17:06:54 +00:00
expectPacksTo(Array(1,3,2,4,5,7,6,8), Array(0xff.toByte,1,3,2,4,5,7,6,8,0));
expectPacksTo(Array(0,0,0,0,0,0,0,0, 1,3,2,4,5,7,6,8),
Array(0,0,0xff.toByte,1,3,2,4,5,7,6,8,0));
expectPacksTo(Array(0,0,12,0,0,34,0,0, 1,3,2,4,5,7,6,8),
2014-09-26 17:55:02 +00:00
Array(0x24, 12, 34, 0xff.toByte,1,3,2,4,5,7,6,8,0));
expectPacksTo(Array(1,3,2,4,5,7,6,8, 8,6,7,4,5,2,3,1),
Array(0xff.toByte,1,3,2,4,5,7,6,8,1,8,6,7,4,5,2,3,1));
expectPacksTo(Array(1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1),
Array(0xff.toByte,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8,
0xd6.toByte,2,4,9,5,1));
expectPacksTo(Array(1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8, 0,2,4,0,9,0,5,1),
Array(0xff.toByte,1,2,3,4,5,6,7,8, 3, 1,2,3,4,5,6,7,8, 6,2,4,3,9,0,5,1, 1,2,3,4,5,6,7,8,
0xd6.toByte,2,4,9,5,1));
expectPacksTo(Array(8,0,100,6,0,1,1,2, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0, 0,0,1,0,2,0,3,1),
Array(0xed.toByte,8,100,6,1,1,2, 0,2, 0xd4.toByte,1,2,3,1));
2014-09-26 15:27:58 +00:00
}
}