@Test public void testSerialization() throws Exception { final int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; VSizeColumnarInts ints = VSizeColumnarInts.fromArray(array); ByteArrayOutputStream baos = new ByteArrayOutputStream(); ints.writeTo(Channels.newChannel(baos), null); final byte[] bytes = baos.toByteArray(); Assert.assertEquals(ints.getSerializedSize(), bytes.length); VSizeColumnarInts deserialized = VSizeColumnarInts.readFromByteBuffer(ByteBuffer.wrap(bytes)); Assert.assertEquals(1, deserialized.getNumBytes()); Assert.assertEquals(array.length, deserialized.size()); for (int i = 0; i < array.length; i++) { Assert.assertEquals(array[i], deserialized.get(i)); } } }
private void checkSerializedSizeAndData() throws Exception { int maxValue = vals.length == 0 ? 0 : Ints.max(vals); VSizeColumnarIntsSerializer writer = new VSizeColumnarIntsSerializer(segmentWriteOutMedium, maxValue); VSizeColumnarInts intsFromList = VSizeColumnarInts.fromIndexedInts(new ArrayBasedIndexedInts(vals), maxValue); writer.open(); for (int val : vals) { writer.addValue(val); } long writtenLength = writer.getSerializedSize(); WriteOutBytes writeOutBytes = segmentWriteOutMedium.makeWriteOutBytes(); writer.writeTo(writeOutBytes, null); assertEquals(writtenLength, intsFromList.getSerializedSize()); // read from ByteBuffer and check values VSizeColumnarInts intsFromByteBuffer = VSizeColumnarInts.readFromByteBuffer( ByteBuffer.wrap(IOUtils.toByteArray(writeOutBytes.asInputStream())) ); assertEquals(vals.length, intsFromByteBuffer.size()); for (int i = 0; i < vals.length; ++i) { assertEquals(vals[i], intsFromByteBuffer.get(i)); } }