|
19 | 19 | import static com.google.common.hash.BloomFilter.toBloomFilter; |
20 | 20 | import static com.google.common.hash.Funnels.unencodedCharsFunnel; |
21 | 21 | import static com.google.common.truth.Truth.assertThat; |
| 22 | +import static com.google.common.truth.Truth.assertWithMessage; |
22 | 23 | import static java.nio.charset.StandardCharsets.UTF_8; |
23 | 24 | import static java.util.concurrent.TimeUnit.SECONDS; |
24 | 25 | import static org.junit.Assert.assertThrows; |
@@ -353,6 +354,45 @@ public void testBitSize() { |
353 | 354 | } |
354 | 355 | } |
355 | 356 |
|
| 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 | + |
356 | 396 | public void testApproximateElementCount() { |
357 | 397 | int numInsertions = 1000; |
358 | 398 | BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), numInsertions); |
|
0 commit comments