Skip to content

Commit e6a7b80

Browse files
nickita-khylkouskiGoogle Java Core Libraries
authored andcommitted
Add a test to check the serialized size of a BloomFilter.
See #8198 RELNOTES=n/a PiperOrigin-RevId: 866662371
1 parent 31df9ad commit e6a7b80

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed

android/guava-tests/test/com/google/common/hash/BloomFilterTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.hash.BloomFilter.toBloomFilter;
2020
import static com.google.common.hash.Funnels.unencodedCharsFunnel;
2121
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth.assertWithMessage;
2223
import static java.nio.charset.StandardCharsets.UTF_8;
2324
import static java.util.concurrent.TimeUnit.SECONDS;
2425
import static org.junit.Assert.assertThrows;
@@ -353,6 +354,45 @@ public void testBitSize() {
353354
}
354355
}
355356

357+
/**
358+
* Tests that bitSize() can be used to predict the serialization size produced by writeTo().
359+
*
360+
* <p>The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
361+
* functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
362+
*/
363+
public void testBitSizeMatchesSerializationSize() throws Exception {
364+
int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
365+
double[] fppValues = {0.01, 0.03, 0.1};
366+
367+
for (int expectedInsertions : expectedInsertionValues) {
368+
for (double fpp : fppValues) {
369+
BloomFilter<String> bf =
370+
BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp);
371+
372+
// Add some elements
373+
for (int i = 0; i < expectedInsertions / 2; i++) {
374+
bf.put("element" + i);
375+
}
376+
377+
// Calculate expected size based on bitSize()
378+
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
379+
// Data: bitSize / 8 bytes
380+
long predictedSize = bf.bitSize() / 8 + 6;
381+
382+
// Serialize and measure actual size
383+
ByteArrayOutputStream out = new ByteArrayOutputStream();
384+
bf.writeTo(out);
385+
int actualSize = out.size();
386+
387+
assertWithMessage(
388+
"Serialization size mismatch for expectedInsertions=%s, fpp=%s",
389+
expectedInsertions, fpp)
390+
.that(actualSize)
391+
.isEqualTo(predictedSize);
392+
}
393+
}
394+
}
395+
356396
public void testApproximateElementCount() {
357397
int numInsertions = 1000;
358398
BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions);

guava-tests/test/com/google/common/hash/BloomFilterTest.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import static com.google.common.hash.BloomFilter.toBloomFilter;
2020
import static com.google.common.hash.Funnels.unencodedCharsFunnel;
2121
import static com.google.common.truth.Truth.assertThat;
22+
import static com.google.common.truth.Truth.assertWithMessage;
2223
import static java.nio.charset.StandardCharsets.UTF_8;
2324
import static java.util.concurrent.TimeUnit.SECONDS;
2425
import static org.junit.Assert.assertThrows;
@@ -355,6 +356,45 @@ public void testBitSize() {
355356
}
356357
}
357358

359+
/**
360+
* Tests that bitSize() can be used to predict the serialization size produced by writeTo().
361+
*
362+
* <p>The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash
363+
* functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes).
364+
*/
365+
public void testBitSizeMatchesSerializationSize() throws Exception {
366+
int[] expectedInsertionValues = {1, 10, 100, 1000, 10000};
367+
double[] fppValues = {0.01, 0.03, 0.1};
368+
369+
for (int expectedInsertions : expectedInsertionValues) {
370+
for (double fpp : fppValues) {
371+
BloomFilter<String> bf =
372+
BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp);
373+
374+
// Add some elements
375+
for (int i = 0; i < expectedInsertions / 2; i++) {
376+
bf.put("element" + i);
377+
}
378+
379+
// Calculate expected size based on bitSize()
380+
// Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes
381+
// Data: bitSize / 8 bytes
382+
long predictedSize = bf.bitSize() / 8 + 6;
383+
384+
// Serialize and measure actual size
385+
ByteArrayOutputStream out = new ByteArrayOutputStream();
386+
bf.writeTo(out);
387+
int actualSize = out.size();
388+
389+
assertWithMessage(
390+
"Serialization size mismatch for expectedInsertions=%s, fpp=%s",
391+
expectedInsertions, fpp)
392+
.that(actualSize)
393+
.isEqualTo(predictedSize);
394+
}
395+
}
396+
}
397+
358398
public void testApproximateElementCount() {
359399
int numInsertions = 1000;
360400
BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions);

0 commit comments

Comments
 (0)