gsmiller commented on code in PR #17019: URL: https://github.com/apache/lucene/pull/17019#discussion_r4688700349
########## lucene/codecs/src/test/org/apache/lucene/codecs/bloom/TestFuzzySet.java: ########## @@ -0,0 +1,148 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.lucene.codecs.bloom; + +import org.apache.lucene.tests.util.LuceneTestCase; +import org.apache.lucene.util.BytesRef; + +/** Tests for {@link FuzzySet} sizing. */ +public class TestFuzzySet extends LuceneTestCase { + + private static final float TARGET_FPP = 0.1023f; + + /** A value count small enough that no sizing arithmetic can overflow. */ + private static final int SAFE_VALUE_COUNT = 1_000_000; + + private static double observedFalsePositiveRate(FuzzySet set) { + for (int i = 0; i < 1000; i++) { + set.addValue(new BytesRef("present-" + i)); + } + int maybes = 0; + for (int i = 0; i < 1000; i++) { + if (set.contains(new BytesRef("absent-" + i)) == FuzzySet.ContainsResult.MAYBE) { + maybes++; + } + } + return (double) maybes / 1000; + } + + private static void assertRejectsAbsentValues(String desc, FuzzySet set) { + double fpp = observedFalsePositiveRate(set); + assertTrue( + desc + " answered MAYBE for " + (fpp * 100) + "% of absent values; set=" + set, fpp < 0.5); + } + + private static void assertNotSmallerThanSafeBaseline(String desc, FuzzySet set) { + FuzzySet baseline = FuzzySet.createOptimalSet(SAFE_VALUE_COUNT, TARGET_FPP); + assertTrue( + desc + + " allocated " + + set.ramBytesUsed() + + " bytes, less than the " + + baseline.ramBytesUsed() + + " bytes used for only " + + SAFE_VALUE_COUNT + + " values; set=" + + set, + set.ramBytesUsed() >= baseline.ramBytesUsed()); + } + + // + // Test cases checking for integer overflow issues when sizing the bitset. + // + + public void testCreateOptimalSetDoesNotOverflowWhenDoubling() { + int valueCount = 226_505_854; + FuzzySet set = FuzzySet.createOptimalSet(valueCount, TARGET_FPP); + assertNotSmallerThanSafeBaseline("createOptimalSet(" + valueCount + ")", set); + assertRejectsAbsentValues("createOptimalSet(" + valueCount + ")", set); + } + + public void testCreateOptimalSetDoesNotOverflowWhenCastSaturates() { + int valueCount = Integer.MAX_VALUE; + FuzzySet set = FuzzySet.createOptimalSet(valueCount, TARGET_FPP); + assertNotSmallerThanSafeBaseline("createOptimalSet(" + valueCount + ")", set); + assertRejectsAbsentValues("createOptimalSet(" + valueCount + ")", set); + } + + public void testCreateOptimalSetSizeIsMonotonic() { Review Comment: Yeah, I'll drop this test case. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
