(gamma) Correctly decode zero-length sequences

This commit is contained in:
Viktor Lofgren 2024-06-24 13:10:56 +02:00
parent 23759a7243
commit fff2ce5721
2 changed files with 11 additions and 5 deletions

View File

@ -76,10 +76,6 @@ public class BitReader {
/** Read bits until a 1 is encountered */
public int takeWhileZero() {
if (bitPosition <= 0) {
readNext();
}
int result = 0;
do {
@ -118,7 +114,7 @@ public class BitReader {
bitPosition = 64;
}
else if (remainingCapacity >= 4) {
currentValue = underlying.getInt() & 0xFFFFFFFFL;
currentValue = underlying.getInt() & 0xFFFF_FFFFL;
bitPosition = 32;
}
else if (remainingCapacity >= 2) {

View File

@ -1,5 +1,6 @@
package nu.marginalia.sequence;
import it.unimi.dsi.fastutil.ints.IntList;
import nu.marginalia.sequence.io.BitReader;
import nu.marginalia.sequence.io.BitWriter;
import org.junit.jupiter.api.Test;
@ -10,6 +11,15 @@ import static org.junit.jupiter.api.Assertions.*;
class BitReaderTest {
@Test
void emptySequence() {
var writer = new BitWriter(ByteBuffer.allocate(1024));
var buffer = writer.finish();
assertEquals(IntList.of(), new GammaCodedSequence(buffer).values());
}
@Test
void getBit() {
var writer = new BitWriter(ByteBuffer.allocate(1024));