internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; OpenBitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = Random().Next(maxSize); BitArray a = new BitArray(sz); OpenBitSet b = new OpenBitSet(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.SafeSet(idx, true); b.FastSet(idx); idx = Random().Next(sz); a.SafeSet(idx, true); b.FastSet((long)idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.FastClear(idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.FastClear((long)idx); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.FastFlip(idx); bool val = b.FlipAndGet(idx); bool val2 = b.FlipAndGet(idx); Assert.IsTrue(val != val2); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.FastFlip((long)idx); val = b.FlipAndGet((long)idx); val2 = b.FlipAndGet((long)idx); Assert.IsTrue(val != val2); val = b.GetAndSet(idx); Assert.IsTrue(val2 == val); Assert.IsTrue(b.Get(idx)); if (!val) { b.FastClear(idx); } Assert.IsTrue(b.Get(idx) == val); } } // test that the various ways of accessing the bits are equivalent DoGet(a, b); DoGetFast(a, b, sz); // test ranges, including possible extension int fromIndex, toIndex; fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); BitArray aa = (BitArray)a.Clone(); // C# BitArray class does not support dynamic sizing. // We have to explicitly change its size using the Length attribute. if (toIndex > aa.Length) { aa.Length = toIndex; } aa.Flip(fromIndex, toIndex); OpenBitSet bb = (OpenBitSet)b.Clone(); bb.Flip(fromIndex, toIndex); DoIterate(aa, bb, mode); // a problem here is from flip or doIterate fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); aa = (BitArray)a.Clone(); if (toIndex > aa.Length) { aa.Length = toIndex; } aa.Clear(fromIndex, toIndex); bb = (OpenBitSet)b.Clone(); bb.Clear(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit DoNextSetBitLong(aa, bb); DoPrevSetBit(aa, bb); DoPrevSetBitLong(aa, bb); fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); aa = (BitArray)a.Clone(); if (toIndex > aa.Length) { aa.Length = toIndex; } aa.Set(fromIndex, toIndex); bb = (OpenBitSet)b.Clone(); bb.Set(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit DoNextSetBitLong(aa, bb); DoPrevSetBit(aa, bb); DoPrevSetBitLong(aa, bb); if (a0 != null) { aa = (BitArray)a.Clone(); BitArray aa0 = (BitArray)a0.Clone(); int largest = Math.Max(a.Length, a0.Length); aa.Length = aa0.Length = largest; // BitWiseEquals needs both arrays to be the same size for succeeding. // We could enlarge the smallest of a and a0, but then the tests below // won't test "UnequalLengths" operations. Assert.AreEqual(aa.BitWiseEquals(aa0), b.Equals(b0)); Assert.AreEqual(a.Cardinality(), b.Cardinality()); BitArray a_and = (BitArray)a.Clone(); a_and = a_and.And_UnequalLengths(a0); BitArray a_or = (BitArray)a.Clone(); a_or = a_or.Or_UnequalLengths(a0); BitArray a_xor = (BitArray)a.Clone(); a_xor = a_xor.Xor_UnequalLengths(a0); BitArray a_andn = (BitArray)a.Clone(); a_andn.AndNot(a0); OpenBitSet b_and = (OpenBitSet)b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0); OpenBitSet b_or = (OpenBitSet)b.Clone(); b_or.Or(b0); OpenBitSet b_xor = (OpenBitSet)b.Clone(); b_xor.Xor(b0); OpenBitSet b_andn = (OpenBitSet)b.Clone(); b_andn.AndNot(b0); DoIterate(a_and, b_and, mode); DoIterate(a_or, b_or, mode); DoIterate(a_xor, b_xor, mode); DoIterate(a_andn, b_andn, 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()); // test non-mutating popcounts Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0)); Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0)); Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0)); Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0)); } a0 = a; b0 = b; } }
internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; Int64BitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = TestUtil.NextInt(Random(), 2, maxSize); BitArray a = new BitArray(sz); Int64BitSet b = new Int64BitSet(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.SafeSet(idx, true); b.Set(idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.Clear(idx); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.Flip(idx, idx + 1); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); 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); BitArray aa = new BitArray(a); aa.Flip(fromIndex, toIndex); Int64BitSet bb = b.Clone(); bb.Flip(fromIndex, toIndex); fromIndex = Random().Next(sz / 2); toIndex = fromIndex + Random().Next(sz - fromIndex); aa = new BitArray(a); 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 = new BitArray(a); 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()); BitArray a_and = new BitArray(a); a_and = a_and.And_UnequalLengths(a0); BitArray a_or = new BitArray(a); a_or = a_or.Or_UnequalLengths(a0); BitArray a_xor = new BitArray(a); a_xor = a_xor.Xor_UnequalLengths(a0); BitArray a_andn = new BitArray(a); a_andn.AndNot(a0); Int64BitSet b_and = b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0); Int64BitSet b_or = b.Clone(); b_or.Or(b0); Int64BitSet b_xor = b.Clone(); b_xor.Xor(b0); Int64BitSet b_andn = b.Clone(); b_andn.AndNot(b0); Assert.AreEqual(a0.Cardinality(), b0.Cardinality()); Assert.AreEqual(a_or.Cardinality(), b_or.Cardinality()); 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; } }
internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; OpenBitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = Random().Next(maxSize); BitArray a = new BitArray(sz); OpenBitSet b = new OpenBitSet(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.SafeSet(idx, true); b.FastSet(idx); idx = Random().Next(sz); a.SafeSet(idx, true); b.FastSet((long)idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.FastClear(idx); idx = Random().Next(sz); a.SafeSet(idx, false); b.FastClear((long)idx); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.FastFlip(idx); bool val = b.FlipAndGet(idx); bool val2 = b.FlipAndGet(idx); Assert.IsTrue(val != val2); idx = Random().Next(sz); a.SafeSet(idx, !a.SafeGet(idx)); b.FastFlip((long)idx); val = b.FlipAndGet((long)idx); val2 = b.FlipAndGet((long)idx); Assert.IsTrue(val != val2); val = b.GetAndSet(idx); Assert.IsTrue(val2 == val); Assert.IsTrue(b.Get(idx)); if (!val) { b.FastClear(idx); } Assert.IsTrue(b.Get(idx) == val); } } // test that the various ways of accessing the bits are equivalent DoGet(a, b); DoGetFast(a, b, sz); // test ranges, including possible extension int fromIndex, toIndex; fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); BitArray aa = (BitArray)a.Clone(); aa.Flip(fromIndex, toIndex); OpenBitSet bb = (OpenBitSet)b.Clone(); bb.Flip(fromIndex, toIndex); DoIterate(aa, bb, mode); // a problem here is from flip or doIterate fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); aa = (BitArray)a.Clone(); aa.Clear(fromIndex, toIndex); bb = (OpenBitSet)b.Clone(); bb.Clear(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from clear() or nextSetBit DoNextSetBitLong(aa, bb); DoPrevSetBit(aa, bb); DoPrevSetBitLong(aa, bb); fromIndex = Random().Next(sz + 80); toIndex = fromIndex + Random().Next((sz >> 1) + 1); aa = (BitArray)a.Clone(); aa.Set(fromIndex, toIndex); bb = (OpenBitSet)b.Clone(); bb.Set(fromIndex, toIndex); DoNextSetBit(aa, bb); // a problem here is from set() or nextSetBit DoNextSetBitLong(aa, bb); DoPrevSetBit(aa, bb); DoPrevSetBitLong(aa, bb); if (a0 != null) { Assert.AreEqual(a.BitWiseEquals(a0), b.Equals(b0)); Assert.AreEqual(a.Cardinality(), b.Cardinality()); BitArray a_and = (BitArray)a.Clone(); a_and = a_and.And(a0); BitArray a_or = (BitArray)a.Clone(); a_or = a_or.Or(a0); BitArray a_xor = (BitArray)a.Clone(); a_xor = a_xor.Xor(a0); BitArray a_andn = (BitArray)a.Clone(); a_andn.AndNot(a0); OpenBitSet b_and = (OpenBitSet)b.Clone(); Assert.AreEqual(b, b_and); b_and.And(b0); OpenBitSet b_or = (OpenBitSet)b.Clone(); b_or.Or(b0); OpenBitSet b_xor = (OpenBitSet)b.Clone(); b_xor.Xor(b0); OpenBitSet b_andn = (OpenBitSet)b.Clone(); b_andn.AndNot(b0); DoIterate(a_and, b_and, mode); DoIterate(a_or, b_or, mode); DoIterate(a_xor, b_xor, mode); DoIterate(a_andn, b_andn, 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()); // test non-mutating popcounts Assert.AreEqual(b_and.Cardinality(), OpenBitSet.IntersectionCount(b, b0)); Assert.AreEqual(b_or.Cardinality(), OpenBitSet.UnionCount(b, b0)); Assert.AreEqual(b_xor.Cardinality(), OpenBitSet.XorCount(b, b0)); Assert.AreEqual(b_andn.Cardinality(), OpenBitSet.AndNotCount(b, b0)); } a0 = a; b0 = b; } }