Skip to content

Commit

Permalink
Merge pull request #645 from apache/packing_equivalence
Browse files Browse the repository at this point in the history
test equivalence of packing blocks and single values
  • Loading branch information
AlexanderSaydakov authored Jan 28, 2025
2 parents 186fb22 + 2f2e5e5 commit 5753fbd
Showing 1 changed file with 65 additions and 0 deletions.
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

0 comments on commit 5753fbd

Please sign in to comment.