diff --git a/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java b/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java index 80e80e7860b0..1a020b30e99a 100644 --- a/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java +++ b/android/guava-tests/test/com/google/common/hash/BloomFilterTest.java @@ -19,6 +19,7 @@ import static com.google.common.hash.BloomFilter.toBloomFilter; import static com.google.common.hash.Funnels.unencodedCharsFunnel; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertThrows; @@ -353,6 +354,45 @@ public void testBitSize() { } } + /** + * Tests that bitSize() can be used to predict the serialization size produced by writeTo(). + * + *

The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash + * functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes). + */ + public void testBitSizeMatchesSerializationSize() throws Exception { + int[] expectedInsertionValues = {1, 10, 100, 1000, 10000}; + double[] fppValues = {0.01, 0.03, 0.1}; + + for (int expectedInsertions : expectedInsertionValues) { + for (double fpp : fppValues) { + BloomFilter bf = + BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp); + + // Add some elements + for (int i = 0; i < expectedInsertions / 2; i++) { + bf.put("element" + i); + } + + // Calculate expected size based on bitSize() + // Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes + // Data: bitSize / 8 bytes + long predictedSize = bf.bitSize() / 8 + 6; + + // Serialize and measure actual size + ByteArrayOutputStream out = new ByteArrayOutputStream(); + bf.writeTo(out); + int actualSize = out.size(); + + assertWithMessage( + "Serialization size mismatch for expectedInsertions=%s, fpp=%s", + expectedInsertions, fpp) + .that(actualSize) + .isEqualTo(predictedSize); + } + } + } + public void testApproximateElementCount() { int numInsertions = 1000; BloomFilter bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions); diff --git a/guava-tests/test/com/google/common/hash/BloomFilterTest.java b/guava-tests/test/com/google/common/hash/BloomFilterTest.java index 665612d6db34..b7cadcf79fe5 100644 --- a/guava-tests/test/com/google/common/hash/BloomFilterTest.java +++ b/guava-tests/test/com/google/common/hash/BloomFilterTest.java @@ -19,6 +19,7 @@ import static com.google.common.hash.BloomFilter.toBloomFilter; import static com.google.common.hash.Funnels.unencodedCharsFunnel; import static com.google.common.truth.Truth.assertThat; +import static com.google.common.truth.Truth.assertWithMessage; import static java.nio.charset.StandardCharsets.UTF_8; import static java.util.concurrent.TimeUnit.SECONDS; import static org.junit.Assert.assertThrows; @@ -355,6 +356,45 @@ public void testBitSize() { } } + /** + * Tests that bitSize() can be used to predict the serialization size produced by writeTo(). + * + *

The serialization format consists of a 6-byte header (1 byte strategy, 1 byte hash + * functions, 4 bytes array length) followed by the bit array data (bitSize / 8 bytes). + */ + public void testBitSizeMatchesSerializationSize() throws Exception { + int[] expectedInsertionValues = {1, 10, 100, 1000, 10000}; + double[] fppValues = {0.01, 0.03, 0.1}; + + for (int expectedInsertions : expectedInsertionValues) { + for (double fpp : fppValues) { + BloomFilter bf = + BloomFilter.create(Funnels.unencodedCharsFunnel(), expectedInsertions, fpp); + + // Add some elements + for (int i = 0; i < expectedInsertions / 2; i++) { + bf.put("element" + i); + } + + // Calculate expected size based on bitSize() + // Header: 1 byte (strategy) + 1 byte (hash functions) + 4 bytes (array length) = 6 bytes + // Data: bitSize / 8 bytes + long predictedSize = bf.bitSize() / 8 + 6; + + // Serialize and measure actual size + ByteArrayOutputStream out = new ByteArrayOutputStream(); + bf.writeTo(out); + int actualSize = out.size(); + + assertWithMessage( + "Serialization size mismatch for expectedInsertions=%s, fpp=%s", + expectedInsertions, fpp) + .that(actualSize) + .isEqualTo(predictedSize); + } + } + } + public void testApproximateElementCount() { int numInsertions = 1000; BloomFilter bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions);