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 authored Oct 29, 2024
1 parent d0f71fc commit c9a9894
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,52 @@ 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);
}

public void testReadVIntNegative() throws IOException {
for (int i = 0; i < 1024; i++) {
int write = randomNegativeInt();
BytesStreamOutput out = new BytesStreamOutput();
out.writeVInt(write);
int read = getStreamInput(out.bytes()).readVInt();
assertEquals(write, read);
}
}

public void testReadVIntBounds() throws IOException {
int write = Integer.MAX_VALUE;
BytesStreamOutput out = new BytesStreamOutput();
out.writeVInt(write);
long read = getStreamInput(out.bytes()).readVInt();
assertEquals(write, read);

write = Integer.MIN_VALUE;
out = new BytesStreamOutput();
out.writeVInt(write);
read = getStreamInput(out.bytes()).readVInt();
assertEquals(write, read);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1032,6 +1032,13 @@ public static long randomNonNegativeLong() {
return randomLong() & Long.MAX_VALUE;
}

/**
* @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 an <code>int</code> between <code>0</code> and <code>Integer.MAX_VALUE</code> (inclusive) chosen uniformly at random.
*/
Expand Down

0 comments on commit c9a9894

Please sign in to comment.