示例#1
0
        public virtual void TestEmptyDocs()
        {
            using Directory dir = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 30));
            using RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwConf);

            // make sure that the fact that documents might be empty is not a problem
            Document emptyDoc = new Document();
            int      numDocs  = Random.NextBoolean() ? 1 : AtLeast(1000);

            for (int i = 0; i < numDocs; ++i)
            {
                iw.AddDocument(emptyDoc);
            }
            iw.Commit();
            using DirectoryReader rd = DirectoryReader.Open(dir);
            for (int i = 0; i < numDocs; ++i)
            {
                Document doc = rd.Document(i);
                Assert.IsNotNull(doc);
                Assert.IsTrue(doc.Fields.Count <= 0);
            }
        }
示例#2
0
        public virtual void TestIncompressible()
        {
            var decompressed = new byte[RandomInts.RandomInt32Between(Random, 20, 256)];

            for (int i = 0; i < decompressed.Length; ++i)
            {
                decompressed[i] = (byte)i;
            }
            Test(decompressed);
        }
示例#3
0
        internal static byte[] RandomArray(int length, int max)
        {
            var arr = new byte[length];

            for (int i = 0; i < arr.Length; ++i)
            {
                arr[i] = (byte)RandomInts.RandomInt32Between(Random, 0, max);
            }
            return(arr);
        }
        /// <inheritdoc/>
        public override string OfCodeUnitsLength(Random random, int minCodeUnits, int maxCodeUnits)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (minCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCodeUnits), minCodeUnits, $"{nameof(minCodeUnits)} must be greater than or equal to 0.");
            }
            if (maxCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCodeUnits), maxCodeUnits, $"{nameof(maxCodeUnits)} must be greater than or equal to 0.");
            }
            if (minCodeUnits > maxCodeUnits)
            {
                throw new ArgumentException($"{nameof(minCodeUnits)} must be less than or equal {nameof(maxCodeUnits)}. {nameof(minCodeUnits)}: {minCodeUnits}, {nameof(maxCodeUnits)}: {maxCodeUnits}");
            }

            int length = RandomNumbers.RandomInt32Between(random, minCodeUnits, maxCodeUnits);

            char[] chars = new char[length];
            for (int i = 0; i < chars.Length;)
            {
                int t = RandomNumbers.RandomInt32Between(random, 0, 4);
                if (t == 0 && i < length - 1)
                {
                    // Make a surrogate pair
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0xd800, 0xdbff); // high
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0xdc00, 0xdfff); // low
                }
                else if (t <= 1)
                {
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0, 0x007f);
                }
                else if (t == 2)
                {
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0x80, 0x07ff);
                }
                else if (t == 3)
                {
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0x800, 0xd7ff);
                }
                else if (t == 4)
                {
                    chars[i++] = (char)RandomNumbers.RandomInt32Between(random, 0xe000, 0xffff);
                }
            }
            return(new string(chars));
        }
示例#5
0
        public virtual void TestConcurrentReads()
        {
            using Directory dir = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 30));
            using RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwConf);

            // make sure the readers are properly cloned
            Document doc   = new Document();
            Field    field = new StringField("fld", "", Field.Store.YES);

            doc.Add(field);
            int numDocs = AtLeast(1000);

            for (int i = 0; i < numDocs; ++i)
            {
                field.SetStringValue("" + i);
                iw.AddDocument(doc);
            }
            iw.Commit();

            AtomicReference <Exception> ex = new AtomicReference <Exception>();

            using (DirectoryReader rd = DirectoryReader.Open(dir))
            {
                IndexSearcher     searcher        = new IndexSearcher(rd);
                int               concurrentReads = AtLeast(5);
                int               readsPerThread  = AtLeast(50);
                IList <ThreadJob> readThreads     = new JCG.List <ThreadJob>();

                for (int i = 0; i < concurrentReads; ++i)
                {
                    readThreads.Add(new ThreadAnonymousClass(numDocs, rd, searcher, readsPerThread, ex));
                }
                foreach (ThreadJob thread in readThreads)
                {
                    thread.Start();
                }
                foreach (ThreadJob thread in readThreads)
                {
                    thread.Join();
                }
            } // rd.Dispose();
            if (ex.Value != null)
            {
                ExceptionDispatchInfo.Capture(ex.Value).Throw(); // LUCENENET: Rethrow to preserve stack details from the other thread
            }
        }
        public override string OfCodePointsLength(Random random, int minCodePoints, int maxCodePoints)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }

            int length = RandomNumbers.RandomInt32Between(random, minCodePoints, maxCodePoints);

            int[] codepoints = new int[length];
            while (length > 0)
            {
                codepoints[--length] = all[random.Next(all.Length)];
            }
            return(Character.ToString(codepoints, 0, codepoints.Length)); //new string(codepoints, 0, codepoints.Length);
        }
        /// <summary>
        /// Fuzzify the input value by decreasing it by a few ulps, but never past <paramref name="minValue"/>.
        /// </summary>
        /// <exception cref="ArgumentException"><paramref name="value"/> is less than <paramref name="minValue"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="random"/> is <c>null</c>.</exception>
        public static float FuzzDown(Random random, float value, float minValue)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (value < minValue)
            {
                throw new ArgumentException($"{nameof(value)} must be greater than or equal to {nameof(minValue)}");
            }

            for (int steps = RandomNumbers.RandomInt32Between(random, 1, 10); steps > 0 && value > minValue; steps--)
            {
                value = value.NextAfter(double.NegativeInfinity);
            }
            return(value);
        }
        /// <summary>
        /// Fuzzify the input value by increasing it by a few ulps, but never past <paramref name="maxValue"/>.
        /// </summary>
        /// <exception cref="ArgumentException"><paramref name="value"/> is greater than <paramref name="maxValue"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="random"/> is <c>null</c>.</exception>
        public static float FuzzUp(Random random, float value, float maxValue)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (value > maxValue)
            {
                throw new ArgumentException($"{nameof(value)} must be less than or equal to {nameof(maxValue)}");
            }

            for (int steps = RandomNumbers.RandomInt32Between(random, 1, 10); steps > 0 && value < maxValue; steps--)
            {
                value = value.NextUp();
            }
            return(value);
        }
        /// <inheritdoc/>
        public override string OfCodeUnitsLength(Random random, int minCodeUnits, int maxCodeUnits)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (minCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCodeUnits), minCodeUnits, $"{nameof(minCodeUnits)} must be greater than or equal to 0.");
            }
            if (maxCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCodeUnits), maxCodeUnits, $"{nameof(maxCodeUnits)} must be greater than or equal to 0.");
            }
            if (minCodeUnits > maxCodeUnits)
            {
                throw new ArgumentException($"{nameof(minCodeUnits)} must be less than or equal {nameof(maxCodeUnits)}. {nameof(minCodeUnits)}: {minCodeUnits}, {nameof(maxCodeUnits)}: {maxCodeUnits}");
            }

            int length = RandomNumbers.RandomInt32Between(random, minCodeUnits, maxCodeUnits);
            int block  = random.Next(blockStarts.Length);

            StringBuilder sb = new StringBuilder();

            while (length > 0)
            {
                int cp = RandomNumbers.RandomInt32Between(random, blockStarts[block], blockEnds[block]);
                if (length > Character.CharCount(cp))
                {
                    sb.AppendCodePoint(cp);
                    length -= Character.CharCount(cp);
                }
                else
                {
                    // Padding for blocks that don't fit.
                    sb.AppendCodePoint('a');
                    length--;
                }
            }
            return(sb.ToString());
        }
        public override string OfCodeUnitsLength(Random random, int minCodeUnits, int maxCodeUnits)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (minCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minCodeUnits), $"{nameof(minCodeUnits)} must be greater than or equal to 0.");
            }
            if (maxCodeUnits < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxCodeUnits), $"{nameof(maxCodeUnits)} must be greater than or equal to 0.");
            }
            if (minCodeUnits > maxCodeUnits)
            {
                throw new ArgumentException($"{nameof(minCodeUnits)} must be less than or equal to {nameof(maxCodeUnits)}. {nameof(minCodeUnits)}: {minCodeUnits}, {nameof(maxCodeUnits)}: {maxCodeUnits}");
            }

            int length = RandomNumbers.RandomInt32Between(random, minCodeUnits, maxCodeUnits);

            // Check and cater for odd number of code units if no bmp characters are given.
            if (bmp.Length == 0 && IsOdd(length))
            {
                if (minCodeUnits == maxCodeUnits)
                {
                    throw new ArgumentException("Cannot return an odd number of code units "
                                                + " when surrogate pairs are the only available codepoints.");
                }
                else
                {
                    // length is odd so we move forward or backward to the closest even number.
                    if (length == minCodeUnits)
                    {
                        length++;
                    }
                    else
                    {
                        length--;
                    }
                }
            }

            int[] codepoints = new int[length];
            int   actual     = 0;

            while (length > 0)
            {
                if (length == 1)
                {
                    codepoints[actual] = bmp[random.Next(bmp.Length)];
                }
                else
                {
                    codepoints[actual] = all[random.Next(all.Length)];
                }
                if (Character.IsSupplementaryCodePoint(codepoints[actual]))
                {
                    length -= 2;
                }
                else
                {
                    length -= 1;
                }
                actual++;
            }
            return(Character.ToString(codepoints, 0, actual));
        }
示例#11
0
 public static long NextInt64(Random random, long minValue, long maxValue)
 {
     return(RandomInts.RandomInt64Between(random, minValue, maxValue)); // LUCENENET: Moved general random data generation to RandomizedTesting.Generators
 }
示例#12
0
        public virtual void TestEncodeDecode()
        {
            int   iterations = RandomInts.RandomInt32Between(Random, 1, 1000);
            float AcceptableOverheadRatio = (float)Random.NextDouble();

            int[] values = new int[(iterations - 1) * Lucene41PostingsFormat.BLOCK_SIZE + ForUtil.MAX_DATA_SIZE];
            for (int i = 0; i < iterations; ++i)
            {
                int bpv = Random.Next(32);
                if (bpv == 0)
                {
                    int value = RandomInts.RandomInt32Between(Random, 0, int.MaxValue);
                    for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j)
                    {
                        values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = value;
                    }
                }
                else
                {
                    for (int j = 0; j < Lucene41PostingsFormat.BLOCK_SIZE; ++j)
                    {
                        values[i * Lucene41PostingsFormat.BLOCK_SIZE + j] = RandomInts.RandomInt32Between(Random, 0, (int)PackedInt32s.MaxValue(bpv));
                    }
                }
            }

            Directory d = new RAMDirectory();
            long      endPointer;

            {
                // encode
                IndexOutput @out    = d.CreateOutput("test.bin", IOContext.DEFAULT);
                ForUtil     forUtil = new ForUtil(AcceptableOverheadRatio, @out);

                for (int i = 0; i < iterations; ++i)
                {
                    forUtil.WriteBlock(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, values.Length), new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], @out);
                }
                endPointer = @out.Position; // LUCENENET specific: Renamed from getFilePointer() to match FileStream
                @out.Dispose();
            }

            {
                // decode
                IndexInput @in     = d.OpenInput("test.bin", IOContext.READ_ONCE);
                ForUtil    forUtil = new ForUtil(@in);
                for (int i = 0; i < iterations; ++i)
                {
                    if (Random.NextBoolean())
                    {
                        forUtil.SkipBlock(@in);
                        continue;
                    }
                    int[] restored = new int[Lucene41.ForUtil.MAX_DATA_SIZE];
                    forUtil.ReadBlock(@in, new byte[Lucene41.ForUtil.MAX_ENCODED_SIZE], restored);
                    Assert.AreEqual(Arrays.CopyOfRange(values, i * Lucene41PostingsFormat.BLOCK_SIZE, (i + 1) * Lucene41PostingsFormat.BLOCK_SIZE), Arrays.CopyOf(restored, Lucene41PostingsFormat.BLOCK_SIZE));
                }
                assertEquals(endPointer, @in.Position); // LUCENENET specific: Renamed from getFilePointer() to match FileStream
                @in.Dispose();
            }
        }
        /// <summary>
        /// Returns a random string that will have a random UTF-8 representation length between
        /// <paramref name="minUtf8Length"/> and <paramref name="maxUtf8Length"/>.
        /// </summary>
        /// <param name="random">A <see cref="Random"/> instance.</param>
        /// <param name="minUtf8Length">Minimum UTF-8 representation length (inclusive).</param>
        /// <param name="maxUtf8Length">Maximum UTF-8 representation length (inclusive).</param>
        /// <returns>A random string that will have a random UTF-8 representation length between
        /// <paramref name="minUtf8Length"/> and <paramref name="maxUtf8Length"/>.</returns>
        /// <exception cref="ArgumentException"><paramref name="minUtf8Length"/> is greater than <paramref name="maxUtf8Length"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="minUtf8Length"/> or <paramref name="maxUtf8Length"/> is less than 0.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="random"/> is <c>null</c>.</exception>
        public string OfUtf8Length(Random random, int minUtf8Length, int maxUtf8Length)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (minUtf8Length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(minUtf8Length), minUtf8Length, $"{nameof(minUtf8Length)} must be greater than or equal to 0.");
            }
            if (maxUtf8Length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(maxUtf8Length), maxUtf8Length, $"{nameof(maxUtf8Length)} must be greater than or equal to 0.");
            }
            if (minUtf8Length > maxUtf8Length)
            {
                throw new ArgumentException($"{nameof(minUtf8Length)} must be less than or equal {nameof(maxUtf8Length)}. {nameof(minUtf8Length)}: {minUtf8Length}, {nameof(maxUtf8Length)}: {maxUtf8Length}");
            }

            int length = RandomNumbers.RandomInt32Between(random, minUtf8Length, maxUtf8Length);

            char[] buffer = new char[length * 3];
            int    bytes  = length;
            int    i      = 0;

            for (; i < buffer.Length && bytes != 0; i++)
            {
                int t;
                if (bytes >= 4)
                {
                    t = random.Next(5);
                }
                else if (bytes >= 3)
                {
                    t = random.Next(4);
                }
                else if (bytes >= 2)
                {
                    t = random.Next(2);
                }
                else
                {
                    t = 0;
                }
                if (t == 0)
                {
                    buffer[i] = (char)RandomNumbers.RandomInt32Between(random, 0, 0x7f);
                    bytes--;
                }
                else if (1 == t)
                {
                    buffer[i] = (char)RandomNumbers.RandomInt32Between(random, 0x80, 0x7ff);
                    bytes    -= 2;
                }
                else if (2 == t)
                {
                    buffer[i] = (char)RandomNumbers.RandomInt32Between(random, 0x800, 0xd7ff);
                    bytes    -= 3;
                }
                else if (3 == t)
                {
                    buffer[i] = (char)RandomNumbers.RandomInt32Between(random, 0xe000, 0xffff);
                    bytes    -= 3;
                }
                else if (4 == t)
                {
                    // Make a surrogate pair
                    buffer[i++] = (char)RandomNumbers.RandomInt32Between(random, 0xd800, 0xdbff); // high
                    buffer[i]   = (char)RandomNumbers.RandomInt32Between(random, 0xdc00, 0xdfff); // low
                    bytes      -= 4;
                }
            }
            return(new string(buffer, 0, i));
        }
示例#14
0
        public virtual void TestBigDocuments()
        {
            // "big" as "much bigger than the chunk size"
            // for this test we force a FS dir
            // we can't just use newFSDirectory, because this test doesn't really index anything.
            // so if we get NRTCachingDir+SimpleText, we make massive stored fields and OOM (LUCENE-4484)
            using Directory dir = new MockDirectoryWrapper(Random, new MMapDirectory(CreateTempDir("testBigDocuments")));
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            //iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 30));
            // LUCENENET specific - Reduced amount to keep the total
            // Nightly test time under 1 hour
            iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 15));
            using RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwConf);
            if (dir is MockDirectoryWrapper mockDirectoryWrapper)
            {
                mockDirectoryWrapper.Throttling = Throttling.NEVER;
            }

            Document emptyDoc = new Document(); // emptyDoc
            Document bigDoc1  = new Document(); // lot of small fields
            Document bigDoc2  = new Document(); // 1 very big field

            Field idField = new StringField("id", "", Field.Store.NO);

            emptyDoc.Add(idField);
            bigDoc1.Add(idField);
            bigDoc2.Add(idField);

            FieldType onlyStored = new FieldType(StringField.TYPE_STORED);

            onlyStored.IsIndexed = false;

            Field smallField = new Field("fld", RandomByteArray(Random.Next(10), 256), onlyStored);
            //int numFields = RandomInts.RandomInt32Between(Random, 500000, 1000000);
            // LUCENENET specific - Reduced amount to keep the total
            // Nightly test time under 1 hour
            int numFields = RandomInts.RandomInt32Between(Random, 250000, 500000);

            for (int i = 0; i < numFields; ++i)
            {
                bigDoc1.Add(smallField);
            }

            //Field bigField = new Field("fld", RandomByteArray(RandomInts.RandomInt32Between(Random, 1000000, 5000000), 2), onlyStored);
            // LUCENENET specific - Reduced amount to keep the total
            // Nightly test time under 1 hour
            Field bigField = new Field("fld", RandomByteArray(RandomInts.RandomInt32Between(Random, 500000, 2500000), 2), onlyStored);

            bigDoc2.Add(bigField);

            int numDocs = AtLeast(5);

            Document[] docs = new Document[numDocs];
            for (int i = 0; i < numDocs; ++i)
            {
                docs[i] = RandomPicks.RandomFrom(Random, new Document[] { emptyDoc, bigDoc1, bigDoc2 });
            }
            for (int i = 0; i < numDocs; ++i)
            {
                idField.SetStringValue("" + i);
                iw.AddDocument(docs[i]);
                if (Random.Next(numDocs) == 0)
                {
                    iw.Commit();
                }
            }
            iw.Commit();
            iw.ForceMerge(1); // look at what happens when big docs are merged
            using DirectoryReader rd = DirectoryReader.Open(dir);
            IndexSearcher searcher = new IndexSearcher(rd);

            for (int i = 0; i < numDocs; ++i)
            {
                Query   query   = new TermQuery(new Term("id", "" + i));
                TopDocs topDocs = searcher.Search(query, 1);
                Assert.AreEqual(1, topDocs.TotalHits, "" + i);
                Document doc = rd.Document(topDocs.ScoreDocs[0].Doc);
                Assert.IsNotNull(doc);
                IIndexableField[] fieldValues = doc.GetFields("fld");
                Assert.AreEqual(docs[i].GetFields("fld").Length, fieldValues.Length);
                if (fieldValues.Length > 0)
                {
                    Assert.AreEqual(docs[i].GetFields("fld")[0].GetBinaryValue(), fieldValues[0].GetBinaryValue());
                }
            }
        }
示例#15
0
        public virtual void TestWriteReadMerge()
        {
            // get another codec, other than the default: so we are merging segments across different codecs
            Codec otherCodec;

            if ("SimpleText".Equals(Codec.Default.Name, StringComparison.Ordinal))
            {
                otherCodec = new Lucene46Codec();
            }
            else
            {
                otherCodec = new SimpleTextCodec();
            }
            using Directory dir = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 30));
            RandomIndexWriter iw = new RandomIndexWriter(Random, dir, (IndexWriterConfig)iwConf.Clone());

            try
            {
                int docCount = AtLeast(200);
                var data     = new byte[docCount][][];
                for (int i = 0; i < docCount; ++i)
                {
                    int fieldCount = Rarely() ? RandomInts.RandomInt32Between(Random, 1, 500) : RandomInts.RandomInt32Between(Random, 1, 5);
                    data[i] = new byte[fieldCount][];
                    for (int j = 0; j < fieldCount; ++j)
                    {
                        int length = Rarely() ? Random.Next(1000) : Random.Next(10);
                        int max    = Rarely() ? 256 : 2;
                        data[i][j] = RandomByteArray(length, max);
                    }
                }

                FieldType type = new FieldType(StringField.TYPE_STORED);
                type.IsIndexed = false;
                type.Freeze();
                Int32Field id = new Int32Field("id", 0, Field.Store.YES);
                for (int i = 0; i < data.Length; ++i)
                {
                    Document doc = new Document();
                    doc.Add(id);
                    id.SetInt32Value(i);
                    for (int j = 0; j < data[i].Length; ++j)
                    {
                        Field f = new Field("bytes" + j, data[i][j], type);
                        doc.Add(f);
                    }
                    iw.IndexWriter.AddDocument(doc);
                    if (Random.NextBoolean() && (i % (data.Length / 10) == 0))
                    {
                        iw.IndexWriter.Dispose();
                        // test merging against a non-compressing codec
                        if (iwConf.Codec == otherCodec)
                        {
                            iwConf.SetCodec(Codec.Default);
                        }
                        else
                        {
                            iwConf.SetCodec(otherCodec);
                        }
                        iw = new RandomIndexWriter(Random, dir, (IndexWriterConfig)iwConf.Clone());
                    }
                }

                for (int i = 0; i < 10; ++i)
                {
                    int min = Random.Next(data.Length);
                    int max = min + Random.Next(20);
                    iw.DeleteDocuments(NumericRangeQuery.NewInt32Range("id", min, max, true, false));
                }

                iw.ForceMerge(2); // force merges with deletions

                iw.Commit();

                using (DirectoryReader ir = DirectoryReader.Open(dir))
                {
                    Assert.IsTrue(ir.NumDocs > 0);
                    int numDocs = 0;
                    for (int i = 0; i < ir.MaxDoc; ++i)
                    {
                        Document doc = ir.Document(i);
                        if (doc is null)
                        {
                            continue;
                        }
                        ++numDocs;
                        int docId = (int)doc.GetField("id").GetInt32Value();
                        Assert.AreEqual(data[docId].Length + 1, doc.Fields.Count);
                        for (int j = 0; j < data[docId].Length; ++j)
                        {
                            var      arr     = data[docId][j];
                            BytesRef arr2Ref = doc.GetBinaryValue("bytes" + j);
                            var      arr2    = Arrays.CopyOfRange(arr2Ref.Bytes, arr2Ref.Offset, arr2Ref.Offset + arr2Ref.Length);
                            Assert.AreEqual(arr, arr2);
                        }
                    }
                    Assert.IsTrue(ir.NumDocs <= numDocs);
                } // ir.Dispose();

                iw.DeleteAll();
                iw.Commit();
                iw.ForceMerge(1);
            }
            finally
            {
                iw.Dispose();
            }
        }
        /// <summary>
        /// A random <see cref="double"/> between <paramref name="minValue"/> (inclusive) and <paramref name="maxValue"/>
        /// (inclusive). If you wish to have an exclusive range,
        /// use <see cref="MathExtensions.NextAfter(float, double)"/> to adjust the range.
        /// <para/>
        /// The code was inspired by GeoTestUtil from Apache Lucene.
        /// </summary>
        /// <param name="random">The <see cref="Random"/> instance.</param>
        /// <param name="minValue">Left range boundary, inclusive. May be <see cref="double.NegativeInfinity"/>, but not <see cref="double.NaN"/>.</param>
        /// <param name="maxValue">Right range boundary, inclusive. May be <see cref="double.PositiveInfinity"/>, but not <see cref="double.NaN"/>.</param>
        /// <returns>A random <see cref="double"/> between <paramref name="minValue"/> (inclusive) and <paramref name="maxValue"/>
        /// (inclusive).</returns>
        /// <exception cref="ArgumentException"><paramref name="minValue"/> is greater than <paramref name="maxValue"/>.</exception>
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="minValue"/> or <paramref name="maxValue"/> is <see cref="double.NaN"/>.</exception>
        /// <exception cref="ArgumentNullException"><paramref name="random"/> is <c>null</c>.</exception>
        public static double RandomDoubleBetween(Random random, double minValue, double maxValue)
        {
            if (random is null)
            {
                throw new ArgumentNullException(nameof(random));
            }
            if (maxValue < minValue)
            {
                throw new ArgumentException($"{nameof(minValue)} must be greater than or equal to {nameof(maxValue)}: {minValue}, {maxValue}");
            }
            if (double.IsNaN(minValue))
            {
                throw new ArgumentOutOfRangeException(nameof(minValue), $"{nameof(minValue)} must not be {nameof(double.NaN)}");
            }
            if (double.IsNaN(maxValue))
            {
                throw new ArgumentOutOfRangeException(nameof(maxValue), $"{nameof(maxValue)} must not be {nameof(double.NaN)}");
            }

            bool hasZero = minValue <= 0 && maxValue >= 0;

            int pick = random.Next(
                EvilRangeLeft +
                EvilRangeRight +
                EvilVeryCloseRangeEnds +
                (hasZero ? EvilZeroOrNear : 0) +
                EvilSimpleProportion +
                EvilRandomRepresentationBits);

            // Exact range ends
            pick -= EvilRangeLeft;
            if (pick < 0 || minValue == maxValue)
            {
                return(minValue);
            }

            pick -= EvilRangeRight;
            if (pick < 0)
            {
                return(maxValue);
            }

            // If we're dealing with infinities, adjust them to discrete values.
            Debug.Assert(minValue != maxValue);
            if (double.IsInfinity(minValue))
            {
                minValue = minValue.NextUp();
            }
            if (double.IsInfinity(maxValue))
            {
                maxValue = maxValue.NextAfter(double.NegativeInfinity);
            }

            // Numbers "very" close to range ends. "very" means a few floating point
            // representation steps (ulps) away.
            pick -= EvilVeryCloseRangeEnds;
            if (pick < 0)
            {
                if (random.NextBoolean())
                {
                    return(FuzzUp(random, minValue, maxValue));
                }
                else
                {
                    return(FuzzDown(random, maxValue, minValue));
                }
            }

            // Zero or near-zero values, if within the range.
            if (hasZero)
            {
                pick -= EvilZeroOrNear;
                if (pick < 0)
                {
                    int v = random.Next(4);
                    if (v == 0)
                    {
                        return(0d);
                    }
                    else if (v == 1)
                    {
                        return(-0.0d);
                    }
                    else if (v == 2)
                    {
                        return(FuzzDown(random, 0d, minValue));
                    }
                    else if (v == 3)
                    {
                        return(FuzzUp(random, 0d, maxValue));
                    }
                }
            }

            // Simple proportional selection.
            pick -= EvilSimpleProportion;
            if (pick < 0)
            {
                return(minValue + (maxValue - minValue) * random.NextDouble());
            }

            // Random representation space selection. This will be heavily biased
            // and overselect from the set of tiny values, if they're allowed.
            pick -= EvilRandomRepresentationBits;
            if (pick < 0)
            {
                long from = ToSortable(minValue);
                long to   = ToSortable(maxValue);
                return(FromSortable(RandomNumbers.RandomInt64Between(random, from, to)));
            }

            throw new Exception("Unreachable.");
        }
示例#17
0
        //
        // Delegates to RandomNumbers.
        //

        //      /**
        //       * A random integer from 0..max (inclusive).
        //       */
        //      @Deprecated
        //public static int randomInt(int max)
        //      {
        //          return RandomNumbers.randomIntBetween(getRandom(), 0, max);
        //      }

        //      /**
        //       * A random long from 0..max (inclusive).
        //       */
        //      @Deprecated
        //public static long randomLong(long max)
        //      {
        //          return RandomNumbers.randomLongBetween(getRandom(), 0, max);
        //      }

        /**
         * A random integer from <code>min</code> to <code>max</code> (inclusive).
         *
         * @see #scaledRandomIntBetween(int, int)
         */
        public static int RandomInt32Between(int min, int max)
        {
            return(RandomNumbers.RandomInt32Between(Random, min, max));
        }
示例#18
0
 public static int NextInt32(Random random, int minValue, int maxValue)
 {
     return(RandomInts.RandomInt32Between(random, minValue, maxValue)); // LUCENENET: Moved general random data generation to RandomizedTesting.Generators
 }
示例#19
0
 /**
  * A random long from <code>min</code> to <code>max</code> (inclusive).
  */
 public static long RandomInt64Between(long min, long max)
 {
     return(RandomNumbers.RandomInt64Between(Random, min, max));
 }
示例#20
0
        private void DoTest(DocValuesType type)
        {
            Directory         d        = NewDirectory();
            IndexWriterConfig iwConfig = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));
            int   nDocs = AtLeast(50);
            Field id    = new NumericDocValuesField("id", 0);
            Field f;

            switch (type)
            {
            case DocValuesType.BINARY:
                f = new BinaryDocValuesField("dv", new BytesRef());
                break;

            case DocValuesType.SORTED:
                f = new SortedDocValuesField("dv", new BytesRef());
                break;

            case DocValuesType.NUMERIC:
                f = new NumericDocValuesField("dv", 0);
                break;

            default:
                throw AssertionError.Create();
            }
            Document document = new Document();

            document.Add(id);
            document.Add(f);

            object[] vals = new object[nDocs];

            RandomIndexWriter iw = new RandomIndexWriter(Random, d, iwConfig);

            for (int i = 0; i < nDocs; ++i)
            {
                id.SetInt64Value(i);
                switch (type)
                {
                case DocValuesType.SORTED:
                case DocValuesType.BINARY:
                    do
                    {
                        vals[i] = TestUtil.RandomSimpleString(Random, 20);
                    } while (((string)vals[i]).Length == 0);
                    f.SetBytesValue(new BytesRef((string)vals[i]));
                    break;

                case DocValuesType.NUMERIC:
                    int bitsPerValue = RandomInts.RandomInt32Between(Random, 1, 31);     // keep it an int
                    vals[i] = (long)Random.Next((int)PackedInt32s.MaxValue(bitsPerValue));
                    f.SetInt64Value((long)vals[i]);
                    break;
                }
                iw.AddDocument(document);
                if (Random.NextBoolean() && i % 10 == 9)
                {
                    iw.Commit();
                }
            }
            iw.Dispose();

            DirectoryReader rd = DirectoryReader.Open(d);

            foreach (AtomicReaderContext leave in rd.Leaves)
            {
                FunctionValues ids = (new Int64FieldSource("id")).GetValues(null, leave);
                ValueSource    vs;
                switch (type)
                {
                case DocValuesType.BINARY:
                case DocValuesType.SORTED:
                    vs = new BytesRefFieldSource("dv");
                    break;

                case DocValuesType.NUMERIC:
                    vs = new Int64FieldSource("dv");
                    break;

                default:
                    throw AssertionError.Create();
                }
                FunctionValues values = vs.GetValues(null, leave);
                BytesRef       bytes  = new BytesRef();
                for (int i = 0; i < leave.AtomicReader.MaxDoc; ++i)
                {
                    assertTrue(values.Exists(i));
                    if (vs is BytesRefFieldSource)
                    {
                        assertTrue(values.ObjectVal(i) is string);
                    }
                    else if (vs is Int64FieldSource)
                    {
                        assertTrue(values.ObjectVal(i) is J2N.Numerics.Int64);
                        assertTrue(values.BytesVal(i, bytes));
                    }
                    else
                    {
                        throw AssertionError.Create();
                    }

                    object expected = vals[ids.Int32Val(i)];
                    switch (type)
                    {
                    case DocValuesType.SORTED:
                        values.OrdVal(i);     // no exception
                        assertTrue(values.NumOrd >= 1);
                        goto case DocValuesType.BINARY;

                    case DocValuesType.BINARY:
                        assertEquals(expected, values.ObjectVal(i));
                        assertEquals(expected, values.StrVal(i));
                        assertEquals(expected, values.ObjectVal(i));
                        assertEquals(expected, values.StrVal(i));
                        assertTrue(values.BytesVal(i, bytes));
                        assertEquals(new BytesRef((string)expected), bytes);
                        break;

                    case DocValuesType.NUMERIC:
                        assertEquals(Convert.ToInt64(expected, CultureInfo.InvariantCulture), values.Int64Val(i));
                        break;
                    }
                }
            }
            rd.Dispose();
            d.Dispose();
        }
示例#21
0
        public virtual void TestReadSkip()
        {
            using Directory dir = NewDirectory();
            IndexWriterConfig iwConf = NewIndexWriterConfig(TEST_VERSION_CURRENT, new MockAnalyzer(Random));

            iwConf.SetMaxBufferedDocs(RandomInts.RandomInt32Between(Random, 2, 30));
            using RandomIndexWriter iw = new RandomIndexWriter(Random, dir, iwConf);
            FieldType ft = new FieldType();

            ft.IsStored = true;
            ft.Freeze();

            string @string = TestUtil.RandomSimpleString(Random, 50);
            var    bytes   = @string.GetBytes(Encoding.UTF8);
            long   l       = Random.NextBoolean() ? Random.Next(42) : Random.NextInt64();
            int    i       = Random.NextBoolean() ? Random.Next(42) : Random.Next();
            float  f       = Random.NextSingle();
            double d       = Random.NextDouble();

            Field[] fields = new Field[]
            {
                new Field("bytes", bytes, ft),
                new Field("string", @string, ft),
                new Int64Field("long", l, Field.Store.YES),
                new Int32Field("int", i, Field.Store.YES),
                new SingleField("float", f, Field.Store.YES),
                new DoubleField("double", d, Field.Store.YES)
            };

            for (int k = 0; k < 100; ++k)
            {
                Document doc = new Document();
                foreach (Field fld in fields)
                {
                    doc.Add(fld);
                }
                iw.IndexWriter.AddDocument(doc);
            }
            iw.Commit();

            using DirectoryReader reader = DirectoryReader.Open(dir);
            int docID = Random.Next(100);

            foreach (Field fld in fields)
            {
                string   fldName = fld.Name;
                Document sDoc    = reader.Document(docID, new JCG.HashSet <string> {
                    fldName
                });
                IIndexableField sField = sDoc.GetField(fldName);
                if (typeof(Field) == fld.GetType())
                {
                    Assert.AreEqual(fld.GetBinaryValue(), sField.GetBinaryValue());
                    Assert.AreEqual(fld.GetStringValue(), sField.GetStringValue());
                }
                else
                {
#pragma warning disable 612, 618
                    Assert.AreEqual(fld.GetNumericValue(), sField.GetNumericValue());
#pragma warning restore 612, 618
                }
            }
        }