Пример #1
0
        public void TestStablePoint()
        {
            var f = StableBloomFilter.NewDefaultStableBloomFilter(10000, 0.1);

            for (int i = 0; i < 1000000; i++)
            {
                f.TestAndAdd(Encoding.ASCII.GetBytes(i.ToString()));
            }

            var zeros = 0;

            for (uint i = 0; i < f.M; i++)
            {
                if (f.cells.Get(i) == 0)
                {
                    zeros++;
                }
            }

            var actual   = Math.Round((double)((double)zeros / (double)f.M), 1, MidpointRounding.AwayFromZero);
            var expected = Math.Round(f.StablePoint(), 1, MidpointRounding.AwayFromZero);

            Assert.AreEqual(expected, actual);

            // A classic Bloom filter is a special case of SBF where P is 0 and max is 1.
            // It doesn't have a stable point.
            var bf          = StableBloomFilter.NewUnstableBloomFilter(1000, 0.1);
            var stablePoint = bf.StablePoint();

            if (stablePoint != 0)
            {
                Assert.Fail(string.Format("Expected stable point 0, got {0}", stablePoint));
            }
        }
Пример #2
0
 public void Testinitialize()
 {
     n    = 100000;
     f    = StableBloomFilter.NewUnstableBloomFilter(n, 0.1);
     data = new byte[n][];
     for (int i = 0; i < n; i++)
     {
         data[i] = Encoding.ASCII.GetBytes(i.ToString());
     }
 }
Пример #3
0
        public void TestNewUnstableBloomFilter()
        {
            var f = StableBloomFilter.NewUnstableBloomFilter(100, 0.1);
            var k = ProbabilisticDataStructures.Utils.OptimalK(0.1);

            Assert.AreEqual(k, f.K());
            Assert.AreEqual(100u, f.M);
            Assert.AreEqual(0u, f.P());
            Assert.AreEqual(1u, f.Max);
        }
Пример #4
0
        public void TestStableFalsePositiveRate()
        {
            var f   = StableBloomFilter.NewDefaultStableBloomFilter(1000, 0.01);
            var fps = Math.Round(f.FalsePositiveRate(), 2, MidpointRounding.AwayFromZero);

            Assert.IsFalse(fps > 0.01);

            // Classic Bloom filters have an unbound rate of false positives. Once they
            // become full, every query returns a false positive.
            var bf = StableBloomFilter.NewUnstableBloomFilter(1000, 0.01);

            fps = bf.FalsePositiveRate();
            Assert.AreEqual(1.0, fps);
        }
Пример #5
0
        public void TestStableReset()
        {
            var f = StableBloomFilter.NewDefaultStableBloomFilter(1000, 0.01);

            for (int i = 0; i < 1000; i++)
            {
                f.TestAndAdd(Encoding.ASCII.GetBytes(i.ToString()));
            }

            var resetF = f.Reset();

            Assert.AreSame(f, resetF, "Returned StableBloomFilter should be the same instance");

            for (uint i = 0; i < f.M; i++)
            {
                var cell = f.cells.Get(i);
                if (cell != 0)
                {
                    Assert.Fail(string.Format("Expected zero cell, got {0}", cell));
                }
            }
        }
Пример #6
0
        public void TestStableTestAndAdd()
        {
            var f = StableBloomFilter.NewDefaultStableBloomFilter(10000, 0.01);

            // 'a' is not in the filter.
            if (f.Test(A_BYTES))
            {
                Assert.Fail("'a' should not be a member");
            }

            var addedF = f.Add(A_BYTES);

            Assert.AreSame(f, addedF, "Returned StableBloomFilter should be the same instance");

            // 'a' is now in the filter.
            if (!f.Test(A_BYTES))
            {
                Assert.Fail("'a' should be a member");
            }

            // 'a' is still in the filter.
            if (!f.TestAndAdd(A_BYTES))
            {
                Assert.Fail("'a' should be a member");
            }

            // 'b' is not in the filter.
            if (f.TestAndAdd(B_BYTES))
            {
                Assert.Fail("'b' should not be a member");
            }

            // 'a' is still in the filter.
            if (!f.Test(A_BYTES))
            {
                Assert.Fail("'a' should be a member");
            }

            // 'b' is now in the filter.
            if (!f.Test(B_BYTES))
            {
                Assert.Fail("'b' should be a member");
            }

            // 'c' is not in the filter.
            if (f.Test(C_BYTES))
            {
                Assert.Fail("'c' should not be a member");
            }

            for (int i = 0; i < 1000000; i++)
            {
                f.TestAndAdd(Encoding.ASCII.GetBytes(i.ToString()));
            }

            // 'a' should have been evicted
            if (f.Test(A_BYTES))
            {
                Assert.Fail("'a' should not be a member");
            }
        }
Пример #7
0
        public void TestStableK()
        {
            var f = new StableBloomFilter(100, 1, 0.01);

            Assert.AreEqual(3u, f.K());
        }
Пример #8
0
        public void TestStableCells()
        {
            var f = new StableBloomFilter(100, 1, 0.1);

            Assert.AreEqual(100u, f.Cells());
        }
Пример #9
0
 public void TestCleanup()
 {
     f    = null;
     n    = 0;
     data = null;
 }