Skip to content

Commit

Permalink
Add unit test for negative values in ByteBufferStreamInput::readVLong
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisHegarty committed Oct 28, 2024
1 parent 78ccd2a commit 33d0dbf
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,28 @@ protected StreamInput getStreamInput(BytesReference bytesReference) throws IOExc
final BytesRef bytesRef = bytesReference.toBytesRef();
return new ByteBufferStreamInput(ByteBuffer.wrap(bytesRef.bytes, bytesRef.offset, bytesRef.length));
}

public void testReadVLongNegative() throws IOException {
for (int i = 0; i < 1024; i++) {
long write = randomNegativeLong();
BytesStreamOutput out = new BytesStreamOutput();
out.writeVLongNoCheck(write);
long read = getStreamInput(out.bytes()).readVLong();
assertEquals(write, read);
}
}

public void testReadVLongBounds() throws IOException {
long write = Long.MAX_VALUE;
BytesStreamOutput out = new BytesStreamOutput();
out.writeVLongNoCheck(write);
long read = getStreamInput(out.bytes()).readVLong();
assertEquals(write, read);

write = Long.MIN_VALUE;
out = new BytesStreamOutput();
out.writeVLongNoCheck(write);
read = getStreamInput(out.bytes()).readVLong();
assertEquals(write, read);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1026,6 +1026,13 @@ public static IntStream randomInts(long streamSize) {
return random().ints(streamSize);
}

/**
* @return a <code>long</code> between <code>Long.MIN_VALUE</code> and <code>-1</code> (inclusive) chosen uniformly at random.
*/
public static long randomNegativeLong() {
return randomLong() & Long.MIN_VALUE;
}

/**
* @return a <code>long</code> between <code>0</code> and <code>Long.MAX_VALUE</code> (inclusive) chosen uniformly at random.
*/
Expand Down

0 comments on commit 33d0dbf

Please sign in to comment.