예제 #1
0
        private static void Test(bool isCaseSensitive)
        {
            var comparer = isCaseSensitive
                ? StringComparer.Ordinal
                : StringComparer.OrdinalIgnoreCase;
            var strings     = GenerateStrings(2000).Skip(500).Take(1000).ToSet(comparer);
            var testStrings = GenerateStrings(100000);

            for (var d = 0.1; d >= 0.0001; d /= 10)
            {
                var filter = new BloomFilter(strings.Count, d, isCaseSensitive);
                filter.AddRange(strings);

                var correctCount   = 0.0;
                var incorrectCount = 0.0;
                foreach (var test in testStrings)
                {
                    var actualContains = strings.Contains(test);
                    var filterContains = filter.ProbablyContains(test);

                    if (!filterContains)
                    {
                        // if the filter says no, then it can't be in the real set.
                        Assert.False(actualContains);
                    }

                    if (actualContains == filterContains)
                    {
                        correctCount++;
                    }
                    else
                    {
                        incorrectCount++;
                    }
                }

                var falsePositivePercentage = incorrectCount / (correctCount + incorrectCount);
                Assert.True(
                    falsePositivePercentage < (d * 1.5),
                    string.Format("falsePositivePercentage={0}, d={1}", falsePositivePercentage, d)
                    );
            }
        }