Skip to content

test equivalence of packing blocks and single values #645

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 1 commit into from
Jan 28, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/test/java/org/apache/datasketches/theta/BitPackingTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public void packUnpackBits() {
input[i] = value & mask;
value += Util.INVERSE_GOLDEN_U64;
}

byte[] bytes = new byte[8 * Long.BYTES];
int bitOffset = 0;
int bufOffset = 0;
Expand All @@ -57,6 +58,7 @@ public void packUnpackBits() {
bufOffset += (bitOffset + bits) >>> 3;
bitOffset = (bitOffset + bits) & 7;
}

for (int i = 0; i < 8; ++i) {
assertEquals(output[i], input[i]);
}
Expand All @@ -76,6 +78,7 @@ public void packUnpackBlocks() {
input[i] = value & mask;
value += Util.INVERSE_GOLDEN_U64;
}

byte[] bytes = new byte[8 * Long.BYTES];
BitPacking.packBitsBlock8(input, 0, bytes, 0, bits);
if (enablePrinting) { hexDump(bytes); }
Expand All @@ -91,6 +94,68 @@ public void packUnpackBlocks() {
}
}

@Test
public void packBitsUnpackBlocks() {
long value = 0; // arbitrary starting value
for (int n = 0; n < 10000; n++) {
for (int bits = 1; bits <= 63; bits++) {
final long mask = (1 << bits) - 1;
long[] input = new long[8];
for (int i = 0; i < 8; ++i) {
input[i] = value & mask;
value += Util.INVERSE_GOLDEN_U64;
}

byte[] bytes = new byte[8 * Long.BYTES];
int bitOffset = 0;
int bufOffset = 0;
for (int i = 0; i < 8; ++i) {
BitPacking.packBits(input[i], bits, bytes, bufOffset, bitOffset);
bufOffset += (bitOffset + bits) >>> 3;
bitOffset = (bitOffset + bits) & 7;
}

long[] output = new long[8];
BitPacking.unpackBitsBlock8(output, 0, bytes, 0, bits);

for (int i = 0; i < 8; ++i) {
assertEquals(output[i], input[i]);
}
}
}
}

@Test
public void packBlocksUnpackBits() {
long value = 123L; // arbitrary starting value
for (int n = 0; n < 10000; n++) {
for (int bits = 1; bits <= 63; bits++) {
final long mask = (1 << bits) - 1;
long[] input = new long[8];
for (int i = 0; i < 8; ++i) {
input[i] = value & mask;
value += Util.INVERSE_GOLDEN_U64;
}

byte[] bytes = new byte[8 * Long.BYTES];
BitPacking.packBitsBlock8(input, 0, bytes, 0, bits);

long[] output = new long[8];
int bitOffset = 0;
int bufOffset = 0;
for (int i = 0; i < 8; ++i) {
BitPacking.unpackBits(output, i, bits, bytes, bufOffset, bitOffset);
bufOffset += (bitOffset + bits) >>> 3;
bitOffset = (bitOffset + bits) & 7;
}

for (int i = 0; i < 8; ++i) {
assertEquals(output[i], input[i]);
}
}
}
}

void hexDump(byte[] bytes) {
for (int i = 0; i < bytes.length; i++) {
System.out.print(String.format("%02x ", bytes[i]));
Expand Down
Loading