-
Notifications
You must be signed in to change notification settings - Fork 125
Adds support for FlexInt and FlexUInt in WriteBuffer #609
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -28,6 +28,10 @@ | |
| import org.junit.After; | ||
| import org.junit.Before; | ||
| import org.junit.Test; | ||
| import org.junit.jupiter.api.Assertions; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.params.ParameterizedTest; | ||
| import org.junit.jupiter.params.provider.CsvSource; | ||
|
|
||
| public class WriteBufferTest | ||
| { | ||
|
|
@@ -39,6 +43,7 @@ public class WriteBufferTest | |
| private ByteArrayOutputStream out; | ||
|
|
||
| @Before | ||
| @BeforeEach | ||
| public void setup() | ||
| { | ||
| buf = new WriteBuffer(ALLOCATOR); | ||
|
|
@@ -1300,4 +1305,124 @@ public void testVarUIntLength9() { | |
| int length = varUIntLength(0x7FFFFFFFFFFFFFFCL); | ||
| assertEquals(9, length); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| " 0, 00000001", | ||
| " 1, 00000011", | ||
| " 2, 00000101", | ||
| " 3, 00000111", | ||
| " 4, 00001001", | ||
| " 5, 00001011", | ||
| " 14, 00011101", | ||
| " 63, 01111111", | ||
| " 64, 00000010 00000001", | ||
| " 729, 01100110 00001011", | ||
| " 8191, 11111110 01111111", | ||
| " 8192, 00000100 00000000 00000001", | ||
| " 1048575, 11111100 11111111 01111111", | ||
| " 1048576, 00001000 00000000 00000000 00000001", | ||
| " 134217727, 11111000 11111111 11111111 01111111", | ||
| " 134217728, 00010000 00000000 00000000 00000000 00000001", | ||
| " 17179869184, 00100000 00000000 00000000 00000000 00000000 00000001", | ||
| " 2199023255552, 01000000 00000000 00000000 00000000 00000000 00000000 00000001", | ||
| " 281474976710655, 11000000 11111111 11111111 11111111 11111111 11111111 01111111", | ||
| " 281474976710656, 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001", | ||
| " 36028797018963967, 10000000 11111111 11111111 11111111 11111111 11111111 11111111 01111111", | ||
| " 36028797018963968, 00000000 00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000001", | ||
| " 4611686018427387903, 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 01111111", | ||
| " 4611686018427387904, 00000000 00000010 00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000001", | ||
| // Long.MAX_VALUE | ||
| " 9223372036854775807, 00000000 11111110 11111111 11111111 11111111 11111111 11111111 11111111 11111111 00000001", | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this (and its negative counterpart below) are the limits for |
||
| " -1, 11111111", | ||
| " -2, 11111101", | ||
| " -3, 11111011", | ||
| " -14, 11100101", | ||
| " -64, 10000001", | ||
| " -65, 11111110 11111110", | ||
| " -729, 10011110 11110100", | ||
| " -8192, 00000010 10000000", | ||
| " -8193, 11111100 11111111 11111110", | ||
| " -1048576, 00000100 00000000 10000000", | ||
| " -1048577, 11111000 11111111 11111111 11111110", | ||
| " -134217728, 00001000 00000000 00000000 10000000", | ||
| " -134217729, 11110000 11111111 11111111 11111111 11111110", | ||
| " -17179869184, 00010000 00000000 00000000 00000000 10000000", | ||
| " -17179869185, 11100000 11111111 11111111 11111111 11111111 11111110", | ||
| " -281474976710656, 01000000 00000000 00000000 00000000 00000000 00000000 10000000", | ||
| " -281474976710657, 10000000 11111111 11111111 11111111 11111111 11111111 11111111 11111110", | ||
| " -36028797018963968, 10000000 00000000 00000000 00000000 00000000 00000000 00000000 10000000", | ||
| " -36028797018963969, 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110", | ||
| "-4611686018427387904, 00000000 00000001 00000000 00000000 00000000 00000000 00000000 00000000 10000000", | ||
| "-4611686018427387905, 00000000 11111110 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111110", | ||
| // Long.MIN_VALUE | ||
| "-9223372036854775808, 00000000 00000010 00000000 00000000 00000000 00000000 00000000 00000000 00000000 11111110", | ||
|
|
||
| }) | ||
| public void testWriteFlexInt(long value, String expectedBits) { | ||
| int numBytes = buf.writeFlexInt(value); | ||
| String actualBits = byteArrayToBitString(bytes()); | ||
| Assertions.assertEquals(expectedBits, actualBits); | ||
| Assertions.assertEquals((expectedBits.length() + 1)/9, numBytes); | ||
| } | ||
|
|
||
| @ParameterizedTest | ||
| @CsvSource({ | ||
| " 0, 00000001", | ||
| " 1, 00000011", | ||
| " 2, 00000101", | ||
| " 3, 00000111", | ||
| " 4, 00001001", | ||
| " 5, 00001011", | ||
| " 14, 00011101", | ||
| " 63, 01111111", | ||
| " 64, 10000001", | ||
| " 127, 11111111", | ||
| " 128, 00000010 00000010", | ||
| " 729, 01100110 00001011", | ||
| " 16383, 11111110 11111111", | ||
| " 16384, 00000100 00000000 00000010", | ||
| " 2097151, 11111100 11111111 11111111", | ||
| " 2097152, 00001000 00000000 00000000 00000010", | ||
| " 268435455, 11111000 11111111 11111111 11111111", | ||
| " 268435456, 00010000 00000000 00000000 00000000 00000010", | ||
| " 34359738368, 00100000 00000000 00000000 00000000 00000000 00000010", | ||
| " 4398046511104, 01000000 00000000 00000000 00000000 00000000 00000000 00000010", | ||
| " 562949953421311, 11000000 11111111 11111111 11111111 11111111 11111111 11111111", | ||
| " 562949953421312, 10000000 00000000 00000000 00000000 00000000 00000000 00000000 00000010", | ||
| " 72057594037927935, 10000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111", | ||
| " 72057594037927936, 00000000 00000001 00000000 00000000 00000000 00000000 00000000 00000000 00000010", | ||
| // Long.MAX_VALUE | ||
| "9223372036854775807, 00000000 11111111 11111111 11111111 11111111 11111111 11111111 11111111 11111111", | ||
| }) | ||
| public void testWriteFlexUInt(long value, String expectedBits) { | ||
| int numBytes = buf.writeFlexUInt(value); | ||
| String actualBits = byteArrayToBitString(bytes()); | ||
| Assertions.assertEquals(expectedBits, actualBits); | ||
| Assertions.assertEquals((expectedBits.length() + 1)/9, numBytes); | ||
| } | ||
|
|
||
| @Test | ||
| public void testWriteFlexUIntForNegativeNumber() { | ||
| Assertions.assertThrows(IllegalArgumentException.class, () -> buf.writeFlexUInt(-1)); | ||
| } | ||
|
|
||
| /** | ||
| * Converts a byte array to a string of bits, such as "00110110 10001001". | ||
| * The purpose of this method is to make it easier to read and write test assertions. | ||
| */ | ||
| private static String byteArrayToBitString(byte[] bytes) { | ||
| StringBuilder s = new StringBuilder(); | ||
| for (byte aByte : bytes) { | ||
| for (int bit = 7; bit >= 0; bit--) { | ||
| if (((0x01 << bit) & aByte) != 0) { | ||
| s.append("1"); | ||
| } else { | ||
| s.append("0"); | ||
| } | ||
| } | ||
| s.append(" "); | ||
| } | ||
| return s.toString().trim(); | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As an optimization, we could go into the fallback loop branch if
remaining() < numBytes, then in the short branches we can write directly into the buffer rather than repetitively callingwriteByte, which requires some redundant logic. See e.g.writeUInt64vswriteUInt64Slow.