public override PForDeltaDocIdSet CopyOf(BitArray bs, int length) { PForDeltaDocIdSet.Builder builder = (new PForDeltaDocIdSet.Builder()).SetIndexInterval(TestUtil.NextInt(Random(), 1, 20)); for (int doc = bs.NextSetBit(0); doc != -1; doc = bs.NextSetBit(doc + 1)) { builder.Add(doc); } return(builder.Build()); }
internal virtual void DoRandomSets(int maxSize, int iter, int mode) { BitArray a0 = null; FixedBitSet b0 = null; for (int i = 0; i < iter; i++) { int sz = TestUtil.NextInt(Random(), 2, maxSize); BitArray a = new BitArray(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, true); b.Set(idx); idx = Random().Next(sz); a.Set(idx, false); b.Clear(idx); idx = Random().Next(sz); a.Set(idx, !a.Get(idx)); b.Flip(idx, idx + 1); idx = Random().Next(sz); a.Set(idx, !a.Get(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 = (BitArray)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 = (BitArray)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 = (BitArray)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()); 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); 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; } }
public virtual void TestDataInputOutput() { Random random = Random(); for (int iter = 0; iter < 5 * RANDOM_MULTIPLIER; iter++) { BaseDirectoryWrapper dir = NewFSDirectory(CreateTempDir("testOverflow")); if (dir is MockDirectoryWrapper) { ((MockDirectoryWrapper)dir).Throttling = MockDirectoryWrapper.Throttling_e.NEVER; } int blockBits = TestUtil.NextInt(random, 1, 20); int blockSize = 1 << blockBits; PagedBytes p = new PagedBytes(blockBits); IndexOutput @out = dir.CreateOutput("foo", IOContext.DEFAULT); int numBytes = TestUtil.NextInt(Random(), 2, 10000000); byte[] answer = new byte[numBytes]; Random().NextBytes(answer); int written = 0; while (written < numBytes) { if (Random().Next(10) == 7) { @out.WriteByte(answer[written++]); } else { int chunk = Math.Min(Random().Next(1000), numBytes - written); @out.WriteBytes(answer, written, chunk); written += chunk; } } @out.Dispose(); IndexInput input = dir.OpenInput("foo", IOContext.DEFAULT); DataInput @in = (DataInput)input.Clone(); p.Copy(input, input.Length()); PagedBytes.Reader reader = p.Freeze(random.NextBoolean()); byte[] verify = new byte[numBytes]; int read = 0; while (read < numBytes) { if (Random().Next(10) == 7) { verify[read++] = @in.ReadByte(); } else { int chunk = Math.Min(Random().Next(1000), numBytes - read); @in.ReadBytes(verify, read, chunk); read += chunk; } } Assert.IsTrue(Arrays.Equals(answer, verify)); BytesRef slice = new BytesRef(); for (int iter2 = 0; iter2 < 100; iter2++) { int pos = random.Next(numBytes - 1); int len = random.Next(Math.Min(blockSize + 1, numBytes - pos)); reader.FillSlice(slice, pos, len); for (int byteUpto = 0; byteUpto < len; byteUpto++) { Assert.AreEqual(answer[pos + byteUpto], (byte)slice.Bytes[slice.Offset + byteUpto]); } } input.Dispose(); dir.Dispose(); } }
public virtual void TestDataInputOutput2() { Random random = Random(); for (int iter = 0; iter < 5 * RANDOM_MULTIPLIER; iter++) { int blockBits = TestUtil.NextInt(random, 1, 20); int blockSize = 1 << blockBits; PagedBytes p = new PagedBytes(blockBits); DataOutput @out = p.DataOutput; int numBytes = Random().Next(10000000); byte[] answer = new byte[numBytes]; Random().NextBytes(answer); int written = 0; while (written < numBytes) { if (Random().Next(10) == 7) { @out.WriteByte(answer[written++]); } else { int chunk = Math.Min(Random().Next(1000), numBytes - written); @out.WriteBytes(answer, written, chunk); written += chunk; } } PagedBytes.Reader reader = p.Freeze(random.NextBoolean()); DataInput @in = p.DataInput; byte[] verify = new byte[numBytes]; int read = 0; while (read < numBytes) { if (Random().Next(10) == 7) { verify[read++] = @in.ReadByte(); } else { int chunk = Math.Min(Random().Next(1000), numBytes - read); @in.ReadBytes(verify, read, chunk); read += chunk; } } Assert.IsTrue(Arrays.Equals(answer, verify)); BytesRef slice = new BytesRef(); for (int iter2 = 0; iter2 < 100; iter2++) { int pos = random.Next(numBytes - 1); int len = random.Next(Math.Min(blockSize + 1, numBytes - pos)); reader.FillSlice(slice, pos, len); for (int byteUpto = 0; byteUpto < len; byteUpto++) { Assert.AreEqual(answer[pos + byteUpto], (byte)slice.Bytes[slice.Offset + byteUpto]); } } } }
public override Sorter NewSorter(Entry[] arr) { return(new ArrayTimSorter <Entry>(arr, ArrayUtil.GetNaturalComparer <Entry>(), TestUtil.NextInt(Random(), 0, arr.Length))); }
private void MostlyAscendingStrategy(Entry[] arr, int i) { arr[i] = i == 0 ? new Entry(random.nextInt(6), 0) : new Entry(arr[i - 1].Value + TestUtil.NextInt(random, -8, 10), i); }
private void StrictlyDescendingStrategy(Entry[] arr, int i) { arr[i] = i == 0 ? new Entry(random.nextInt(6), 0) : new Entry(arr[i - 1].Value - TestUtil.NextInt(random, 1, 5), i); }