Skip to content

Commit

Permalink
Test review Core J-S, #259 (#914)
Browse files Browse the repository at this point in the history
* Formatting and remove extraneous comment in Search/Payloads, #259

* Array formatting in Search/Similarities to match upstream

* Search/Spans formatting cleanup, use singletons for parameterless/captureless anonymous classes

* Code cleanup in Search/B-TestB tests

* Search/TestC code cleanup and allocation improvements

* Search/TestD-TestE code cleanup

* Test review TestF-TestL, #259

* Test review for rest of Search, #259

* Test review of Store, #259

* Finish J-S test cleanup

* Fix .NET FX build failure due to missing import

* Use CompareToOrdinal instead of string.Compare

* Change Array.Empty<T> to Arrays.Empty<T> until #916 is done in a separate PR

* Null-safe disposal of resources in TestSpansAdvanced

* Make TestSpansAdvanced.AssertHits static to match upstream Java code

* Use Arrays.Empty<T> in TestBoolean2 instead of Array.Empty<T>

* Revert static instance of MockScorer in TestCachingCollector

* Revert static instances of NoOpCollector in TestCachingCollector

* Fix seealso cref to CheckHits.CheckNoMatchExplanations

* Fix ticks math in RandomGen.LuceneDate

* Fix XML doc comment paragraph tag in TestFieldCacheRangeFilter

* Revert static instance of AnalyzerAnonymousClass in TestPhraseQuery

* Comment out TestScorerPerf.terms to prevent unused warning

* Remove use of Convert.ToInt32 which boxes in CheckHits

* Specify culture for Convert.ToInt32

* Fix spelling of TransactionalThreadInterrupt class

* Add back redundant override of TestRegexps in case some test runners don't report failures correctly

* Remove unused ConcurrentDictionaryWrapper type
  • Loading branch information
paulirwin committed Mar 10, 2024
1 parent b1476ae commit e08a342
Show file tree
Hide file tree
Showing 123 changed files with 1,414 additions and 1,728 deletions.
2 changes: 2 additions & 0 deletions Lucene.Net.sln.DotSettings
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<wpf:ResourceDictionary xml:space="preserve" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:ss="urn:shemas-jetbrains-com:settings-storage-xaml" xmlns:wpf="http://schemas.microsoft.com/winfx/2006/xaml/presentation">
<s:Boolean x:Key="/Default/UserDictionary/Words/=Coord/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>
13 changes: 6 additions & 7 deletions src/Lucene.Net.TestFramework/Search/CheckHits.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Text;
using JCG = J2N.Collections.Generic;
using Assert = Lucene.Net.TestFramework.Assert;
Expand Down Expand Up @@ -59,13 +58,13 @@ public static void CheckNoMatchExplanations(Query q, string defaultFieldName, In
JCG.SortedSet<int> ignore = new JCG.SortedSet<int>();
for (int i = 0; i < results.Length; i++)
{
ignore.Add(Convert.ToInt32(results[i], CultureInfo.InvariantCulture));
ignore.Add(results[i]);
}

int maxDoc = searcher.IndexReader.MaxDoc;
for (int doc = 0; doc < maxDoc; doc++)
{
if (ignore.Contains(Convert.ToInt32(doc, CultureInfo.InvariantCulture)))
if (ignore.Contains(doc))
{
continue;
}
Expand Down Expand Up @@ -133,13 +132,13 @@ public static void DoCheckHits(Random random, Query query, string defaultFieldNa
JCG.SortedSet<int> correct = new JCG.SortedSet<int>();
for (int i = 0; i < results.Length; i++)
{
correct.Add(Convert.ToInt32(results[i], CultureInfo.InvariantCulture));
correct.Add(results[i]);
}

JCG.SortedSet<int> actual = new JCG.SortedSet<int>();
for (int i = 0; i < hits.Length; i++)
{
actual.Add(Convert.ToInt32(hits[i].Doc, CultureInfo.InvariantCulture));
actual.Add(hits[i].Doc);
}

Assert.AreEqual(correct, actual, aggressive: false, () => query.ToString(defaultFieldName));
Expand Down Expand Up @@ -423,7 +422,7 @@ public virtual void SetScorer(Scorer scorer)

public virtual void Collect(int doc)
{
bag.Add(Convert.ToInt32(doc + @base, CultureInfo.InvariantCulture));
bag.Add(doc + @base);
}

public virtual void SetNextReader(AtomicReaderContext context)
Expand Down Expand Up @@ -539,4 +538,4 @@ public virtual void SetNextReader(AtomicReaderContext context)

public virtual bool AcceptsDocsOutOfOrder => true;
}
}
}
19 changes: 5 additions & 14 deletions src/Lucene.Net.Tests/Search/BaseTestRangeFilter.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using J2N.Text;
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using NUnit.Framework;
Expand Down Expand Up @@ -68,7 +69,7 @@ internal TestIndex(Random random, int minR, int maxR, bool allowNegativeRandomIn
internal static TestIndex signedIndexDir;
internal static TestIndex unsignedIndexDir;

internal static int minId = 0;
internal const int minId = 0;
internal static int maxId;

internal static readonly int intLength = Convert.ToString(int.MaxValue).Length;
Expand Down Expand Up @@ -96,11 +97,6 @@ public static string Pad(int n)
return b.ToString();
}

/// <summary>
/// LUCENENET specific
/// Is non-static because <see cref="Build(Random, TestIndex)"/> is no
/// longer static.
/// </summary>
[OneTimeSetUp]
public override void BeforeClass() // LUCENENET specific: renamed from BeforeClassBaseTestRangeFilter() so we can override to control the order of execution
{
Expand All @@ -127,12 +123,7 @@ public static string Pad(int n)
base.AfterClass();
}

/// <summary>
/// LUCENENET specific
/// Passed in because NewStringField and NewIndexWriterConfig are no
/// longer static.
/// </summary>
private IndexReader Build(Random random, TestIndex index)
private static IndexReader Build(Random random, TestIndex index)
{
/* build an index */

Expand Down Expand Up @@ -208,8 +199,8 @@ public virtual void TestPad()
string bb = Pad(b);
string label = a + ":" + aa + " vs " + b + ":" + bb;
Assert.AreEqual(aa.Length, bb.Length, "i=" + i + ": length of " + label);
Assert.IsTrue(System.String.Compare(aa, bb, System.StringComparison.Ordinal) < 0, "i=" + i + ": compare less than " + label);
Assert.IsTrue(aa.CompareToOrdinal(bb) < 0, "i=" + i + ": compare less than " + label);
}
}
}
}
}
12 changes: 5 additions & 7 deletions src/Lucene.Net.Tests/Search/FuzzyTermOnShortTermsTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,12 @@ public static Analyzer Analyzer
}
}

/// <summary>
/// LUCENENET specific
/// Is non-static because NewIndexWriterConfig is no longer static.
/// </summary>
public Directory GetDirectory(Analyzer analyzer, string[] vals)
public static Directory GetDirectory(Analyzer analyzer, string[] vals)
{
Directory directory = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random, directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer).SetMaxBufferedDocs(TestUtil.NextInt32(Random, 100, 1000)).SetMergePolicy(NewLogMergePolicy()));
RandomIndexWriter writer = new RandomIndexWriter(Random, directory,
NewIndexWriterConfig(TEST_VERSION_CURRENT, analyzer)
.SetMaxBufferedDocs(TestUtil.NextInt32(Random, 100, 1000)).SetMergePolicy(NewLogMergePolicy()));

foreach (string s in vals)
{
Expand All @@ -110,4 +108,4 @@ public Directory GetDirectory(Analyzer analyzer, string[] vals)
return directory;
}
}
}
}
3 changes: 1 addition & 2 deletions src/Lucene.Net.Tests/Search/JustCompileSearch.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Lucene.Net.Util;
using System;

namespace Lucene.Net.Search
{
Expand Down Expand Up @@ -330,4 +329,4 @@ public override Scorer GetScorer(AtomicReaderContext context, IBits acceptDocs)
}
}
}
}
}
3 changes: 2 additions & 1 deletion src/Lucene.Net.Tests/Search/MultiCollectorTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ public class MultiCollectorTest : LuceneTestCase
{
private class DummyCollector : ICollector
{
// LUCENENET: .NET conventions are to use properties instead of fields where feasible
internal bool AcceptsDocsOutOfOrderCalled { get; private set; } = false;
internal bool CollectCalled { get; private set; } = false;
internal bool SetNextReaderCalled { get; private set; } = false;
Expand Down Expand Up @@ -115,4 +116,4 @@ public virtual void TestCollector()
}
}
}
}
}
4 changes: 2 additions & 2 deletions src/Lucene.Net.Tests/Search/Payloads/PayloadHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ public virtual IndexSearcher SetUp(Random random, Similarity similarity, int num
PayloadAnalyzer analyzer = new PayloadAnalyzer(this);

// TODO randomize this
IndexWriter writer = new IndexWriter(directory, (new IndexWriterConfig(LuceneTestCase.TEST_VERSION_CURRENT, analyzer)).SetSimilarity(similarity));
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LuceneTestCase.TEST_VERSION_CURRENT, analyzer).SetSimilarity(similarity));
// writer.infoStream = System.out;
for (int i = 0; i < numDocs; i++)
{
Expand All @@ -156,4 +156,4 @@ public virtual void TearDown()
Reader.Dispose();
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,15 @@ public class TestPayloadExplanations : TestExplanations
public override void SetUp()
{
base.SetUp();
searcher.Similarity = new DefaultSimilarityAnonymousClass(this);
searcher.Similarity = DefaultSimilarityAnonymousClass.Default;
}

private sealed class DefaultSimilarityAnonymousClass : DefaultSimilarity
{
private readonly TestPayloadExplanations outerInstance;
public static readonly DefaultSimilarityAnonymousClass Default = new DefaultSimilarityAnonymousClass();

public DefaultSimilarityAnonymousClass(TestPayloadExplanations outerInstance)
private DefaultSimilarityAnonymousClass()
{
this.outerInstance = outerInstance;
}

public override float ScorePayload(int doc, int start, int end, BytesRef payload)
Expand Down Expand Up @@ -118,4 +117,4 @@ public virtual void TestPT5()

// TODO: test the payloadnear query too!
}
}
}
10 changes: 4 additions & 6 deletions src/Lucene.Net.Tests/Search/Payloads/TestPayloadNearQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,17 +114,15 @@ private PayloadNearQuery NewPhraseQuery(string fieldName, string phrase, bool in
return new PayloadNearQuery(clauses, 0, inOrder, function);
}

/// <summary>
/// LUCENENET specific
/// Is non-static because NewIndexWriterConfig is no longer static.
/// </summary>
[OneTimeSetUp]
public override void BeforeClass()
{
base.BeforeClass();

directory = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random, directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()).SetSimilarity(similarity));
RandomIndexWriter writer = new RandomIndexWriter(Random, directory,
NewIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
.SetSimilarity(similarity));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++)
{
Expand Down Expand Up @@ -396,4 +394,4 @@ public override Explanation IdfExplain(CollectionStatistics collectionStats, Ter
}
}
}
}
}
33 changes: 24 additions & 9 deletions src/Lucene.Net.Tests/Search/Payloads/TestPayloadTermQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,18 @@ public class TestPayloadTermQuery : LuceneTestCase
{
private static IndexSearcher searcher;
private static IndexReader reader;
private static readonly Similarity similarity = new BoostingSimilarity();
private static readonly Similarity similarity = BoostingSimilarity.Default; // LUCENENET: using static instance
private static readonly byte[] payloadField = { 1 };
private static readonly byte[] payloadMultiField1 = { 2 };
private static readonly byte[] payloadMultiField2 = { 4 };
protected internal static Directory directory;

private class PayloadAnalyzer : Analyzer
{
internal PayloadAnalyzer()
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
public static readonly PayloadAnalyzer Default = new PayloadAnalyzer();

private PayloadAnalyzer()
: base(PER_FIELD_REUSE_STRATEGY)
{
}
Expand Down Expand Up @@ -118,17 +121,15 @@ public override void Reset()
}
}

/// <summary>
/// LUCENENET specific
/// Is non-static because NewIndexWriterConfig is no longer static.
/// </summary>
[OneTimeSetUp]
public override void BeforeClass()
{
base.BeforeClass();

directory = NewDirectory();
RandomIndexWriter writer = new RandomIndexWriter(Random, directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()).SetSimilarity(similarity).SetMergePolicy(NewLogMergePolicy()));
RandomIndexWriter writer = new RandomIndexWriter(Random, directory,
NewIndexWriterConfig(TEST_VERSION_CURRENT, PayloadAnalyzer.Default) // LUCENENET: Using static instance of PayloadAnalyzer
.SetSimilarity(similarity).SetMergePolicy(NewLogMergePolicy()));
//writer.infoStream = System.out;
for (int i = 0; i < 1000; i++)
{
Expand Down Expand Up @@ -251,7 +252,7 @@ public virtual void TestIgnoreSpanScorer()

IndexReader reader = DirectoryReader.Open(directory);
IndexSearcher theSearcher = NewSearcher(reader);
theSearcher.Similarity = new FullSimilarity();
theSearcher.Similarity = FullSimilarity.Default; // LUCENENET: using static instance of FullSimilarity
TopDocs hits = searcher.Search(query, null, 100);
Assert.IsTrue(hits != null, "hits is null and it shouldn't be");
Assert.IsTrue(hits.TotalHits == 100, "hits Size: " + hits.TotalHits + " is not: " + 100);
Expand Down Expand Up @@ -320,6 +321,13 @@ public virtual void TestNoPayload()

internal class BoostingSimilarity : DefaultSimilarity
{
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
public static readonly BoostingSimilarity Default = new BoostingSimilarity();

private BoostingSimilarity()
{
}

public override float QueryNorm(float sumOfSquaredWeights)
{
return 1;
Expand Down Expand Up @@ -363,11 +371,18 @@ public override float Tf(float freq)

internal class FullSimilarity : DefaultSimilarity
{
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
public static readonly FullSimilarity Default = new FullSimilarity();

private FullSimilarity()
{
}

public virtual float ScorePayload(int docId, string fieldName, sbyte[] payload, int offset, int length)
{
//we know it is size 4 here, so ignore the offset/length
return payload[offset];
}
}
}
}
}
42 changes: 35 additions & 7 deletions src/Lucene.Net.Tests/Search/Similarities/TestSimilarityBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,41 @@ public class TestSimilarityBase : LuceneTestCase

/// <summary>
/// The DFR basic models to test. </summary>
internal static BasicModel[] BASIC_MODELS = new BasicModel[] { new BasicModelBE(), new BasicModelD(), new BasicModelG(), new BasicModelIF(), new BasicModelIn(), new BasicModelIne(), new BasicModelP() };
internal static BasicModel[] BASIC_MODELS =
{
new BasicModelBE(), new BasicModelD(), new BasicModelG(),
new BasicModelIF(), new BasicModelIn(), new BasicModelIne(),
new BasicModelP()
};

/// <summary>
/// The DFR aftereffects to test. </summary>
internal static AfterEffect[] AFTER_EFFECTS = new AfterEffect[] { new AfterEffectB(), new AfterEffectL(), new AfterEffect.NoAfterEffect() };
internal static AfterEffect[] AFTER_EFFECTS =
{
new AfterEffectB(), new AfterEffectL(), new AfterEffect.NoAfterEffect()
};

/// <summary>
/// The DFR normalizations to test. </summary>
internal static Normalization[] NORMALIZATIONS = new Normalization[] { new NormalizationH1(), new NormalizationH2(), new NormalizationH3(), new NormalizationZ(), new Normalization.NoNormalization() };
internal static Normalization[] NORMALIZATIONS =
{
new NormalizationH1(), new NormalizationH2(), new NormalizationH3(),
new NormalizationZ(), new Normalization.NoNormalization()
};

/// <summary>
/// The distributions for IB. </summary>
internal static Distribution[] DISTRIBUTIONS = new Distribution[] { new DistributionLL(), new DistributionSPL() };
internal static Distribution[] DISTRIBUTIONS =
{
new DistributionLL(), new DistributionSPL()
};

/// <summary>
/// Lambdas for IB. </summary>
internal static Lambda[] LAMBDAS = new Lambda[] { new LambdaDF(), new LambdaTTF() };
internal static Lambda[] LAMBDAS =
{
new LambdaDF(), new LambdaTTF()
};

private IndexSearcher searcher;
private Directory dir;
Expand Down Expand Up @@ -587,7 +605,17 @@ private void CorrectnessTestCore(SimilarityBase sim, float gold)

/// <summary>
/// The "collection" for the integration tests. </summary>
internal string[] docs = new string[] { "Tiger, tiger burning bright In the forest of the night What immortal hand or eye Could frame thy fearful symmetry ?", "In what distant depths or skies Burnt the fire of thine eyes ? On what wings dare he aspire ? What the hands the seize the fire ?", "And what shoulder and what art Could twist the sinews of thy heart ? And when thy heart began to beat What dread hand ? And what dread feet ?", "What the hammer? What the chain ? In what furnace was thy brain ? What the anvil ? And what dread grasp Dare its deadly terrors clasp ?", "And when the stars threw down their spears And water'd heaven with their tear Did he smile his work to see ? Did he, who made the lamb, made thee ?", "Tiger, tiger burning bright In the forest of the night What immortal hand or eye Dare frame thy fearful symmetry ?", "Cruelty has a human heart And jealousy a human face Terror the human form divine And Secrecy the human dress .", "The human dress is forg'd iron The human form a fiery forge The human face a furnace seal'd The human heart its fiery gorge ." };
internal string[] docs = new string[]
{
"Tiger, tiger burning bright In the forest of the night What immortal hand or eye Could frame thy fearful symmetry ?",
"In what distant depths or skies Burnt the fire of thine eyes ? On what wings dare he aspire ? What the hands the seize the fire ?",
"And what shoulder and what art Could twist the sinews of thy heart ? And when thy heart began to beat What dread hand ? And what dread feet ?",
"What the hammer? What the chain ? In what furnace was thy brain ? What the anvil ? And what dread grasp Dare its deadly terrors clasp ?",
"And when the stars threw down their spears And water'd heaven with their tear Did he smile his work to see ? Did he, who made the lamb, made thee ?",
"Tiger, tiger burning bright In the forest of the night What immortal hand or eye Dare frame thy fearful symmetry ?",
"Cruelty has a human heart And jealousy a human face Terror the human form divine And Secrecy the human dress .",
"The human dress is forg'd iron The human form a fiery forge The human face a furnace seal'd The human heart its fiery gorge ."
};

/// <summary>
/// Tests whether all similarities return three documents for the query word
Expand Down Expand Up @@ -649,4 +677,4 @@ public virtual void TestDiscountOverlapsBoost()
Assert.AreEqual(expected.ComputeNorm(state), actual.ComputeNorm(state));
}
}
}
}

0 comments on commit e08a342

Please sign in to comment.