Пример #1
0
        public virtual void TestAgainstBitSet()
        {
            int numBits = TestUtil.NextInt32(Random, 100, 1 << 20);

            // test various random sets with various load factors
            foreach (float percentSet in new float[] { 0f, 0.0001f, (float)Random.NextDouble() / 2, 0.9f, 1f })
            {
                BitSet set  = RandomSet(numBits, percentSet);
                T      copy = CopyOf(set, numBits);
                AssertEquals(numBits, set, copy);
            }
            // test one doc
            BitSet set_ = new BitSet(numBits);

            set_.Set(0); // 0 first
            T copy_ = CopyOf(set_, numBits);

            AssertEquals(numBits, set_, copy_);
            set_.Clear(0);
            set_.Set(Random.Next(numBits));
            copy_ = CopyOf(set_, numBits); // then random index
            AssertEquals(numBits, set_, copy_);
            // test regular increments
            for (int inc = 2; inc < 1000; inc += TestUtil.NextInt32(Random, 1, 100))
            {
                set_ = new BitSet(numBits);
                for (int d = Random.Next(10); d < numBits; d += inc)
                {
                    set_.Set(d);
                }
                copy_ = CopyOf(set_, numBits);
                AssertEquals(numBits, set_, copy_);
            }
        }
Пример #2
0
        public virtual void TestCompact()
        {
            BytesRef @ref = new BytesRef();
            int      num  = AtLeast(2);

            for (int j = 0; j < num; j++)
            {
                int       numEntries = 0;
                const int size       = 797;
                BitSet    bits       = new BitSet(size);
                for (int i = 0; i < size; i++)
                {
                    string str;
                    do
                    {
                        str = TestUtil.RandomRealisticUnicodeString(Random, 1000);
                    } while (str.Length == 0);
                    @ref.CopyChars(str);
                    int key = hash.Add(@ref);
                    if (key < 0)
                    {
                        Assert.IsTrue(bits.Get((-key) - 1));
                    }
                    else
                    {
                        Assert.IsFalse(bits.Get(key));
                        bits.Set(key);
                        numEntries++;
                    }
                }
                Assert.AreEqual(hash.Count, bits.Cardinality);
                Assert.AreEqual(numEntries, bits.Cardinality);
                Assert.AreEqual(numEntries, hash.Count);
                int[] compact = hash.Compact();
                Assert.IsTrue(numEntries < compact.Length);
                for (int i = 0; i < numEntries; i++)
                {
                    bits.Clear(compact[i]);
                }
                Assert.AreEqual(0, bits.Cardinality);
                hash.Clear();
                Assert.AreEqual(0, hash.Count);
                hash.Reinit();
            }
        }
Пример #3
0
        internal virtual void DoRandomSets(int maxSize, int iter, int mode)
        {
            BitSet      a0 = null;
            FixedBitSet b0 = null;

            for (int i = 0; i < iter; i++)
            {
                int         sz = TestUtil.NextInt32(Random, 2, maxSize);
                BitSet      a  = new BitSet(sz);
                FixedBitSet b  = new FixedBitSet(sz);

                // test the various ways of setting bits
                if (sz > 0)
                {
                    int nOper = Random.Next(sz);
                    for (int j = 0; j < nOper; j++)
                    {
                        int idx;

                        idx = Random.Next(sz);
                        a.Set(idx);
                        b.Set(idx);

                        idx = Random.Next(sz);
                        a.Clear(idx);
                        b.Clear(idx);

                        idx = Random.Next(sz);
                        a.Flip(idx, idx + 1);
                        b.Flip(idx, idx + 1);

                        idx = Random.Next(sz);
                        a.Flip(idx, idx + 1);
                        b.Flip(idx, idx + 1);

                        bool val2 = b.Get(idx);
                        bool val  = b.GetAndSet(idx);
                        Assert.IsTrue(val2 == val);
                        Assert.IsTrue(b.Get(idx));

                        if (!val)
                        {
                            b.Clear(idx);
                        }
                        Assert.IsTrue(b.Get(idx) == val);
                    }
                }

                // test that the various ways of accessing the bits are equivalent
                DoGet(a, b);

                // test ranges, including possible extension
                int fromIndex, toIndex;
                fromIndex = Random.Next(sz / 2);
                toIndex   = fromIndex + Random.Next(sz - fromIndex);
                BitSet aa = (BitSet)a.Clone();
                aa.Flip(fromIndex, toIndex);
                FixedBitSet bb = b.Clone();
                bb.Flip(fromIndex, toIndex);

                DoIterate(aa, bb, mode); // a problem here is from flip or doIterate

                fromIndex = Random.Next(sz / 2);
                toIndex   = fromIndex + Random.Next(sz - fromIndex);
                aa        = (BitSet)a.Clone();
                aa.Clear(fromIndex, toIndex);
                bb = b.Clone();
                bb.Clear(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit

                DoPrevSetBit(aa, bb);

                fromIndex = Random.Next(sz / 2);
                toIndex   = fromIndex + Random.Next(sz - fromIndex);
                aa        = (BitSet)a.Clone();
                aa.Set(fromIndex, toIndex);
                bb = b.Clone();
                bb.Set(fromIndex, toIndex);

                DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit

                DoPrevSetBit(aa, bb);

                if (b0 != null && b0.Length <= b.Length)
                {
                    Assert.AreEqual(a.Cardinality, b.Cardinality);

                    BitSet a_and = (BitSet)a.Clone();
                    a_and.And(a0);
                    BitSet a_or = (BitSet)a.Clone();
                    a_or.Or(a0);
                    BitSet a_xor = (BitSet)a.Clone();
                    a_xor.Xor(a0);
                    BitSet a_andn = (BitSet)a.Clone();
                    a_andn.AndNot(a0);

                    FixedBitSet b_and = b.Clone();
                    Assert.AreEqual(b, b_and);
                    b_and.And(b0);
                    FixedBitSet b_or = b.Clone();
                    b_or.Or(b0);
                    FixedBitSet b_xor = b.Clone();
                    b_xor.Xor(b0);
                    FixedBitSet b_andn = b.Clone();
                    b_andn.AndNot(b0);

                    Assert.AreEqual(a0.Cardinality, b0.Cardinality);
                    Assert.AreEqual(a_or.Cardinality, b_or.Cardinality);

                    DoIterate(a_and, b_and, mode);
                    DoIterate(a_or, b_or, mode);
                    DoIterate(a_andn, b_andn, mode);
                    DoIterate(a_xor, b_xor, mode);

                    Assert.AreEqual(a_and.Cardinality, b_and.Cardinality);
                    Assert.AreEqual(a_or.Cardinality, b_or.Cardinality);
                    Assert.AreEqual(a_xor.Cardinality, b_xor.Cardinality);
                    Assert.AreEqual(a_andn.Cardinality, b_andn.Cardinality);
                }

                a0 = a;
                b0 = b;
            }
        }