Skip to content

Commit

Permalink
refine buffer varint tests
Browse files Browse the repository at this point in the history
  • Loading branch information
chaokunyang committed Oct 4, 2023
1 parent c89b841 commit 8fb845f
Show file tree
Hide file tree
Showing 2 changed files with 140 additions and 140 deletions.
8 changes: 4 additions & 4 deletions java/fury-core/src/main/java/io/fury/memory/MemoryBuffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -1211,8 +1211,8 @@ public int writePositiveVarInt(int v) {
}

/**
* For implementation efficiency, this method needs at most 8 bytes for writing 5 bytes
* using long to avoid using two memory operations.
* For implementation efficiency, this method needs at most 8 bytes for writing 5 bytes using long
* to avoid using two memory operations.
*/
public int unsafeWriteVarInt(int v) {
// Ensure negatives close to zero is encode in little bytes.
Expand All @@ -1227,8 +1227,8 @@ public int readVarInt() {
}

/**
* For implementation efficiency, this method needs at most 8 bytes for writing 5 bytes
* using long to avoid using two memory operations.
* For implementation efficiency, this method needs at most 8 bytes for writing 5 bytes using long
* to avoid using two memory operations.
*/
public int unsafeWritePositiveVarInt(int v) {
// The encoding algorithm are based on kryo UnsafeMemoryOutput.writeVarInt
Expand Down
272 changes: 136 additions & 136 deletions java/fury-core/src/test/java/io/fury/memory/MemoryBufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -275,44 +275,48 @@ private void checkPositiveVarInt(MemoryBuffer buf, int value) {

@Test
public void testWriteVarInt() {
for (int i = 0; i < 32; i++) {
MemoryBuffer buf = MemoryUtils.buffer(8);
for (int j = 0; j < i; j++) {
buf.writeByte((byte) 1); // make address unaligned.
buf.readByte();
}
checkVarInt(buf, 1, 1);
checkVarInt(buf, 1 << 5, 1);
checkVarInt(buf, 1 << 6, 2);
checkVarInt(buf, 1 << 7, 2);
checkVarInt(buf, 1 << 12, 2);
checkVarInt(buf, 1 << 13, 3);
checkVarInt(buf, 1 << 14, 3);
checkVarInt(buf, 1 << 19, 3);
checkVarInt(buf, 1 << 20, 4);
checkVarInt(buf, 1 << 26, 4);
checkVarInt(buf, 1 << 27, 5);
checkVarInt(buf, 1 << 28, 5);
checkVarInt(buf, Integer.MAX_VALUE, 5);
for (int i = 0; i < 5; i++) {
checkVarInt(buf(i), 1, 1);
checkVarInt(buf(i), 1 << 5, 1);
checkVarInt(buf(i), 1 << 6, 2);
checkVarInt(buf(i), 1 << 7, 2);
checkVarInt(buf(i), 1 << 12, 2);
checkVarInt(buf(i), 1 << 13, 3);
checkVarInt(buf(i), 1 << 14, 3);
checkVarInt(buf(i), 1 << 19, 3);
checkVarInt(buf(i), 1 << 20, 4);
checkVarInt(buf(i), 1 << 26, 4);
checkVarInt(buf(i), 1 << 27, 5);
checkVarInt(buf(i), 1 << 28, 5);
checkVarInt(buf(i), Integer.MAX_VALUE, 5);

checkVarInt(buf, -1, 1);
checkVarInt(buf, -1 << 6, 1);
checkVarInt(buf, -1 << 7, 2);
checkVarInt(buf, -1 << 13, 2);
checkVarInt(buf, -1 << 14, 3);
checkVarInt(buf, -1 << 20, 3);
checkVarInt(buf, -1 << 21, 4);
checkVarInt(buf, -1 << 27, 4);
checkVarInt(buf, -1 << 28, 5);
checkVarInt(buf, Byte.MIN_VALUE, 2);
checkVarInt(buf, Byte.MAX_VALUE, 2);
checkVarInt(buf, Short.MAX_VALUE, 3);
checkVarInt(buf, Short.MIN_VALUE, 3);
checkVarInt(buf, Integer.MAX_VALUE, 5);
checkVarInt(buf, Integer.MIN_VALUE, 5);
checkVarInt(buf(i), -1, 1);
checkVarInt(buf(i), -1 << 6, 1);
checkVarInt(buf(i), -1 << 7, 2);
checkVarInt(buf(i), -1 << 13, 2);
checkVarInt(buf(i), -1 << 14, 3);
checkVarInt(buf(i), -1 << 20, 3);
checkVarInt(buf(i), -1 << 21, 4);
checkVarInt(buf(i), -1 << 27, 4);
checkVarInt(buf(i), -1 << 28, 5);
checkVarInt(buf(i), Byte.MIN_VALUE, 2);
checkVarInt(buf(i), Byte.MAX_VALUE, 2);
checkVarInt(buf(i), Short.MAX_VALUE, 3);
checkVarInt(buf(i), Short.MIN_VALUE, 3);
checkVarInt(buf(i), Integer.MAX_VALUE, 5);
checkVarInt(buf(i), Integer.MIN_VALUE, 5);
}
}

private MemoryBuffer buf(int numUnaligned) {
MemoryBuffer buf = MemoryUtils.buffer(1);
for (int j = 0; j < numUnaligned; j++) {
buf.writeByte((byte) 1); // make address unaligned.
buf.readByte();
}
return buf;
}

private void checkVarInt(MemoryBuffer buf, int value, int bytesWritten) {
int readerIndex = buf.readerIndex();
assertEquals(buf.writerIndex(), readerIndex);
Expand All @@ -329,60 +333,58 @@ private void checkVarInt(MemoryBuffer buf, int value, int bytesWritten) {
public void testWriteVarLong() {
MemoryBuffer buf = MemoryUtils.buffer(8);
checkVarLong(buf, -1, 1);
for (int i = 0; i < 32; i++) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < i; j++) {
buf.writeByte(1);
buf.readByte();
checkVarLong(buf, -1, 1);
checkVarLong(buf, 1, 1);
checkVarLong(buf, 1L << 6, 2);
checkVarLong(buf, 1L << 7, 2);
checkVarLong(buf, -(2 << 5), 1);
checkVarLong(buf, -(2 << 6), 2);
checkVarLong(buf, 1L << 13, 3);
checkVarLong(buf, 1L << 14, 3);
checkVarLong(buf, -(2 << 12), 2);
checkVarLong(buf, -(2 << 13), 3);
checkVarLong(buf, 1L << 19, 3);
checkVarLong(buf, 1L << 20, 4);
checkVarLong(buf, 1L << 21, 4);
checkVarLong(buf, -(2 << 19), 3);
checkVarLong(buf, -(2 << 20), 4);
checkVarLong(buf, 1L << 26, 4);
checkVarLong(buf, 1L << 27, 5);
checkVarLong(buf, 1L << 28, 5);
checkVarLong(buf, -(2 << 26), 4);
checkVarLong(buf, -(2 << 27), 5);
checkVarLong(buf, 1L << 30, 5);
checkVarLong(buf, -(2L << 29), 5);
checkVarLong(buf, 1L << 30, 5);
checkVarLong(buf, -(2L << 30), 5);
checkVarLong(buf, 1L << 32, 5);
checkVarLong(buf, -(2L << 31), 5);
checkVarLong(buf, 1L << 34, 6);
checkVarLong(buf, -(2L << 33), 5);
checkVarLong(buf, 1L << 35, 6);
checkVarLong(buf, -(2L << 34), 6);
checkVarLong(buf, 1L << 41, 7);
checkVarLong(buf, -(2L << 40), 6);
checkVarLong(buf, 1L << 42, 7);
checkVarLong(buf, -(2L << 41), 7);
checkVarLong(buf, 1L << 48, 8);
checkVarLong(buf, -(2L << 47), 7);
checkVarLong(buf, -(2L << 48), 8);
checkVarLong(buf, 1L << 49, 8);
checkVarLong(buf, -(2L << 48), 8);
checkVarLong(buf, -(2L << 54), 8);
checkVarLong(buf, 1L << 54, 8);
checkVarLong(buf, 1L << 55, 9);
checkVarLong(buf, 1L << 56, 9);
checkVarLong(buf, -(2L << 55), 9);
checkVarLong(buf, 1L << 62, 9);
checkVarLong(buf, -(2L << 62), 9);
checkVarLong(buf, 1L << 63 - 1, 9);
checkVarLong(buf, -(2L << 62), 9);
checkVarLong(buf, Long.MAX_VALUE, 9);
checkVarLong(buf, Long.MIN_VALUE, 9);
checkVarLong(buf(i), -1, 1);
checkVarLong(buf(i), 1, 1);
checkVarLong(buf(i), 1L << 6, 2);
checkVarLong(buf(i), 1L << 7, 2);
checkVarLong(buf(i), -(2 << 5), 1);
checkVarLong(buf(i), -(2 << 6), 2);
checkVarLong(buf(i), 1L << 13, 3);
checkVarLong(buf(i), 1L << 14, 3);
checkVarLong(buf(i), -(2 << 12), 2);
checkVarLong(buf(i), -(2 << 13), 3);
checkVarLong(buf(i), 1L << 19, 3);
checkVarLong(buf(i), 1L << 20, 4);
checkVarLong(buf(i), 1L << 21, 4);
checkVarLong(buf(i), -(2 << 19), 3);
checkVarLong(buf(i), -(2 << 20), 4);
checkVarLong(buf(i), 1L << 26, 4);
checkVarLong(buf(i), 1L << 27, 5);
checkVarLong(buf(i), 1L << 28, 5);
checkVarLong(buf(i), -(2 << 26), 4);
checkVarLong(buf(i), -(2 << 27), 5);
checkVarLong(buf(i), 1L << 30, 5);
checkVarLong(buf(i), -(2L << 29), 5);
checkVarLong(buf(i), 1L << 30, 5);
checkVarLong(buf(i), -(2L << 30), 5);
checkVarLong(buf(i), 1L << 32, 5);
checkVarLong(buf(i), -(2L << 31), 5);
checkVarLong(buf(i), 1L << 34, 6);
checkVarLong(buf(i), -(2L << 33), 5);
checkVarLong(buf(i), 1L << 35, 6);
checkVarLong(buf(i), -(2L << 34), 6);
checkVarLong(buf(i), 1L << 41, 7);
checkVarLong(buf(i), -(2L << 40), 6);
checkVarLong(buf(i), 1L << 42, 7);
checkVarLong(buf(i), -(2L << 41), 7);
checkVarLong(buf(i), 1L << 48, 8);
checkVarLong(buf(i), -(2L << 47), 7);
checkVarLong(buf(i), -(2L << 48), 8);
checkVarLong(buf(i), 1L << 49, 8);
checkVarLong(buf(i), -(2L << 48), 8);
checkVarLong(buf(i), -(2L << 54), 8);
checkVarLong(buf(i), 1L << 54, 8);
checkVarLong(buf(i), 1L << 55, 9);
checkVarLong(buf(i), 1L << 56, 9);
checkVarLong(buf(i), -(2L << 55), 9);
checkVarLong(buf(i), 1L << 62, 9);
checkVarLong(buf(i), -(2L << 62), 9);
checkVarLong(buf(i), 1L << 63 - 1, 9);
checkVarLong(buf(i), -(2L << 62), 9);
checkVarLong(buf(i), Long.MAX_VALUE, 9);
checkVarLong(buf(i), Long.MIN_VALUE, 9);
}
}
}
Expand All @@ -403,56 +405,54 @@ private void checkVarLong(MemoryBuffer buf, long value, int bytesWritten) {
public void testWritePositiveVarLong() {
MemoryBuffer buf = MemoryUtils.buffer(8);
checkPositiveVarint64(buf, -1, 9);
for (int i = 0; i < 32; i++) {
for (int i = 0; i < 9; i++) {
for (int j = 0; j < i; j++) {
buf.writeByte(1);
buf.readByte();
checkPositiveVarint64(buf, -1, 9);
checkPositiveVarint64(buf, 1, 1);
checkPositiveVarint64(buf, 1L << 6, 1);
checkPositiveVarint64(buf, 1L << 7, 2);
checkPositiveVarint64(buf, -(2 << 5), 9);
checkPositiveVarint64(buf, -(2 << 6), 9);
checkPositiveVarint64(buf, 1L << 13, 2);
checkPositiveVarint64(buf, 1L << 14, 3);
checkPositiveVarint64(buf, -(2 << 12), 9);
checkPositiveVarint64(buf, -(2 << 13), 9);
checkPositiveVarint64(buf, 1L << 20, 3);
checkPositiveVarint64(buf, 1L << 21, 4);
checkPositiveVarint64(buf, -(2 << 19), 9);
checkPositiveVarint64(buf, -(2 << 20), 9);
checkPositiveVarint64(buf, 1L << 27, 4);
checkPositiveVarint64(buf, 1L << 28, 5);
checkPositiveVarint64(buf, -(2 << 26), 9);
checkPositiveVarint64(buf, -(2 << 27), 9);
checkPositiveVarint64(buf, 1L << 30, 5);
checkPositiveVarint64(buf, -(2L << 29), 9);
checkPositiveVarint64(buf, 1L << 30, 5);
checkPositiveVarint64(buf, -(2L << 30), 9);
checkPositiveVarint64(buf, 1L << 32, 5);
checkPositiveVarint64(buf, -(2L << 31), 9);
checkPositiveVarint64(buf, 1L << 34, 5);
checkPositiveVarint64(buf, -(2L << 33), 9);
checkPositiveVarint64(buf, 1L << 35, 6);
checkPositiveVarint64(buf, -(2L << 34), 9);
checkPositiveVarint64(buf, 1L << 41, 6);
checkPositiveVarint64(buf, -(2L << 40), 9);
checkPositiveVarint64(buf, 1L << 42, 7);
checkPositiveVarint64(buf, -(2L << 41), 9);
checkPositiveVarint64(buf, 1L << 48, 7);
checkPositiveVarint64(buf, -(2L << 47), 9);
checkPositiveVarint64(buf, 1L << 49, 8);
checkPositiveVarint64(buf, -(2L << 48), 9);
checkPositiveVarint64(buf, 1L << 55, 8);
checkPositiveVarint64(buf, -(2L << 54), 9);
checkPositiveVarint64(buf, 1L << 56, 9);
checkPositiveVarint64(buf, -(2L << 55), 9);
checkPositiveVarint64(buf, 1L << 62, 9);
checkPositiveVarint64(buf, -(2L << 62), 9);
checkPositiveVarint64(buf, 1L << 63 - 1, 9);
checkPositiveVarint64(buf, -(2L << 62), 9);
checkPositiveVarint64(buf, Long.MAX_VALUE, 9);
checkPositiveVarint64(buf, Long.MIN_VALUE, 9);
checkPositiveVarint64(buf(i), -1, 9);
checkPositiveVarint64(buf(i), 1, 1);
checkPositiveVarint64(buf(i), 1L << 6, 1);
checkPositiveVarint64(buf(i), 1L << 7, 2);
checkPositiveVarint64(buf(i), -(2 << 5), 9);
checkPositiveVarint64(buf(i), -(2 << 6), 9);
checkPositiveVarint64(buf(i), 1L << 13, 2);
checkPositiveVarint64(buf(i), 1L << 14, 3);
checkPositiveVarint64(buf(i), -(2 << 12), 9);
checkPositiveVarint64(buf(i), -(2 << 13), 9);
checkPositiveVarint64(buf(i), 1L << 20, 3);
checkPositiveVarint64(buf(i), 1L << 21, 4);
checkPositiveVarint64(buf(i), -(2 << 19), 9);
checkPositiveVarint64(buf(i), -(2 << 20), 9);
checkPositiveVarint64(buf(i), 1L << 27, 4);
checkPositiveVarint64(buf(i), 1L << 28, 5);
checkPositiveVarint64(buf(i), -(2 << 26), 9);
checkPositiveVarint64(buf(i), -(2 << 27), 9);
checkPositiveVarint64(buf(i), 1L << 30, 5);
checkPositiveVarint64(buf(i), -(2L << 29), 9);
checkPositiveVarint64(buf(i), 1L << 30, 5);
checkPositiveVarint64(buf(i), -(2L << 30), 9);
checkPositiveVarint64(buf(i), 1L << 32, 5);
checkPositiveVarint64(buf(i), -(2L << 31), 9);
checkPositiveVarint64(buf(i), 1L << 34, 5);
checkPositiveVarint64(buf(i), -(2L << 33), 9);
checkPositiveVarint64(buf(i), 1L << 35, 6);
checkPositiveVarint64(buf(i), -(2L << 34), 9);
checkPositiveVarint64(buf(i), 1L << 41, 6);
checkPositiveVarint64(buf(i), -(2L << 40), 9);
checkPositiveVarint64(buf(i), 1L << 42, 7);
checkPositiveVarint64(buf(i), -(2L << 41), 9);
checkPositiveVarint64(buf(i), 1L << 48, 7);
checkPositiveVarint64(buf(i), -(2L << 47), 9);
checkPositiveVarint64(buf(i), 1L << 49, 8);
checkPositiveVarint64(buf(i), -(2L << 48), 9);
checkPositiveVarint64(buf(i), 1L << 55, 8);
checkPositiveVarint64(buf(i), -(2L << 54), 9);
checkPositiveVarint64(buf(i), 1L << 56, 9);
checkPositiveVarint64(buf(i), -(2L << 55), 9);
checkPositiveVarint64(buf(i), 1L << 62, 9);
checkPositiveVarint64(buf(i), -(2L << 62), 9);
checkPositiveVarint64(buf(i), 1L << 63 - 1, 9);
checkPositiveVarint64(buf(i), -(2L << 62), 9);
checkPositiveVarint64(buf(i), Long.MAX_VALUE, 9);
checkPositiveVarint64(buf(i), Long.MIN_VALUE, 9);
}
}
}
Expand Down

0 comments on commit 8fb845f

Please sign in to comment.