@Test
public void testWrapAsByteBuffer() {
SerializationTestType randomInt = Util.randomRecord(SerializationTestTypeFactory.INT);
DataOutputSerializer serializer = new DataOutputSerializer(randomInt.length());
MemorySegment segment = new MemorySegment(new byte[randomInt.length()]);
try {
// empty buffer, read buffer should be empty
ByteBuffer wrapper = serializer.wrapAsByteBuffer();
Assert.assertEquals(0, wrapper.position());
Assert.assertEquals(0, wrapper.limit());
// write to data output, read buffer should still be empty
randomInt.write(serializer);
Assert.assertEquals(0, wrapper.position());
Assert.assertEquals(0, wrapper.limit());
// get updated read buffer, read buffer should contain written data
wrapper = serializer.wrapAsByteBuffer();
Assert.assertEquals(0, wrapper.position());
Assert.assertEquals(randomInt.length(), wrapper.limit());
// clear data output, read buffer should still contain written data
serializer.clear();
Assert.assertEquals(0, wrapper.position());
Assert.assertEquals(randomInt.length(), wrapper.limit());
// get updated read buffer, should be empty
wrapper = serializer.wrapAsByteBuffer();
Assert.assertEquals(0, wrapper.position());
Assert.assertEquals(0, wrapper.limit());
// write to data output and read back to memory
randomInt.write(serializer);
wrapper = serializer.wrapAsByteBuffer();
segment.put(0, wrapper, randomInt.length());
Assert.assertEquals(randomInt.length(), wrapper.position());
Assert.assertEquals(randomInt.length(), wrapper.limit());
} catch (IOException e) {
e.printStackTrace();