From a6da17e90ca398b050787fc83cfa358036352cbe Mon Sep 17 00:00:00 2001 From: Paul Irwin Date: Tue, 23 Jan 2024 08:14:10 -0700 Subject: [PATCH] Search/TestD-TestE code cleanup --- Lucene.Net.sln.DotSettings | 2 + src/Lucene.Net.Tests/Search/TestDateFilter.cs | 7 +-- .../Search/TestDisjunctionMaxQuery.cs | 8 +-- src/Lucene.Net.Tests/Search/TestDocBoost.cs | 17 +++---- src/Lucene.Net.Tests/Search/TestDocIdSet.cs | 50 ++++--------------- .../Search/TestDocTermOrdsRangeFilter.cs | 5 +- .../Search/TestDocTermOrdsRewriteMethod.cs | 11 ++-- .../Search/TestDocValuesScoring.cs | 11 ++-- .../Search/TestEarlyTermination.cs | 17 ++----- .../Search/TestElevationComparator.cs | 10 ++-- .../Search/TestExplanations.cs | 15 +++--- 11 files changed, 51 insertions(+), 102 deletions(-) create mode 100644 Lucene.Net.sln.DotSettings diff --git a/Lucene.Net.sln.DotSettings b/Lucene.Net.sln.DotSettings new file mode 100644 index 0000000000..464613e85d --- /dev/null +++ b/Lucene.Net.sln.DotSettings @@ -0,0 +1,2 @@ + + True \ No newline at end of file diff --git a/src/Lucene.Net.Tests/Search/TestDateFilter.cs b/src/Lucene.Net.Tests/Search/TestDateFilter.cs index 2d44e6c20f..fb183d1574 100644 --- a/src/Lucene.Net.Tests/Search/TestDateFilter.cs +++ b/src/Lucene.Net.Tests/Search/TestDateFilter.cs @@ -1,7 +1,6 @@ using Lucene.Net.Attributes; using Lucene.Net.Documents; using NUnit.Framework; -using System; using Assert = Lucene.Net.TestFramework.Assert; namespace Lucene.Net.Search @@ -34,13 +33,10 @@ namespace Lucene.Net.Search /// /// DateFilter JUnit tests. - /// - /// /// [TestFixture] public class TestDateFilter : LuceneTestCase { - /// [OneTimeSetUp] public override void BeforeClass() // LUCENENET specific - renamed from TestBefore() to ensure calling order vs base class { @@ -106,7 +102,6 @@ public void Test() // noop, required for the before and after tests to run } - /// [OneTimeTearDown] public override void AfterClass() // LUCENENET specific - renamed from TestAfter() to ensure calling order vs base class { @@ -166,4 +161,4 @@ public override void AfterClass() // LUCENENET specific - renamed from TestAfter base.AfterClass(); } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDisjunctionMaxQuery.cs b/src/Lucene.Net.Tests/Search/TestDisjunctionMaxQuery.cs index b1c6961176..db7fb026e8 100644 --- a/src/Lucene.Net.Tests/Search/TestDisjunctionMaxQuery.cs +++ b/src/Lucene.Net.Tests/Search/TestDisjunctionMaxQuery.cs @@ -56,7 +56,7 @@ public class TestDisjunctionMaxQuery : LuceneTestCase { /// /// threshold for comparing floats - public static readonly float SCORE_COMP_THRESH = 0.0000f; + public const float SCORE_COMP_THRESH = 0.0000f; /// /// Similarity to eliminate tf, idf and lengthNorm effects to isolate test @@ -190,7 +190,7 @@ public virtual void TestSkipToFirsttimeMiss() Assert.IsTrue(s.TopReaderContext is AtomicReaderContext); Weight dw = s.CreateNormalizedWeight(dq); AtomicReaderContext context = (AtomicReaderContext)s.TopReaderContext; - Scorer ds = dw.GetScorer(context, (context.AtomicReader).LiveDocs); + Scorer ds = dw.GetScorer(context, context.AtomicReader.LiveDocs); bool skipOk = ds.Advance(3) != DocIdSetIterator.NO_MORE_DOCS; if (skipOk) { @@ -208,7 +208,7 @@ public virtual void TestSkipToFirsttimeHit() QueryUtils.Check(Random, dq, s); Weight dw = s.CreateNormalizedWeight(dq); AtomicReaderContext context = (AtomicReaderContext)s.TopReaderContext; - Scorer ds = dw.GetScorer(context, (context.AtomicReader).LiveDocs); + Scorer ds = dw.GetScorer(context, context.AtomicReader.LiveDocs); Assert.IsTrue(ds.Advance(3) != DocIdSetIterator.NO_MORE_DOCS, "firsttime skipTo found no match"); Assert.AreEqual("d4", r.Document(ds.DocID).Get("id"), "found wrong docid"); } @@ -568,4 +568,4 @@ protected internal virtual void PrintHits(string test, ScoreDoc[] h, IndexSearch } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDocBoost.cs b/src/Lucene.Net.Tests/Search/TestDocBoost.cs index 52bdfde733..cbef0616c6 100644 --- a/src/Lucene.Net.Tests/Search/TestDocBoost.cs +++ b/src/Lucene.Net.Tests/Search/TestDocBoost.cs @@ -33,8 +33,6 @@ namespace Lucene.Net.Search /// /// Document boost unit test. - /// - /// /// [TestFixture] public class TestDocBoost : LuceneTestCase @@ -49,8 +47,8 @@ public virtual void TestDocBoost_Mem() Field f2 = NewTextField("field", "word", Field.Store.YES); f2.Boost = 2.0f; - Documents.Document d1 = new Documents.Document(); - Documents.Document d2 = new Documents.Document(); + Document d1 = new Document(); + Document d2 = new Document(); d1.Add(f1); // boost = 1 d2.Add(f2); // boost = 2 @@ -64,7 +62,9 @@ public virtual void TestDocBoost_Mem() float[] scores = new float[4]; IndexSearcher searcher = NewSearcher(reader); - searcher.Search(new TermQuery(new Term("field", "word")), new CollectorAnonymousClass(this, scores)); + searcher.Search( + new TermQuery(new Term("field", "word")), + new CollectorAnonymousClass(scores)); float lastScore = 0.0f; @@ -84,13 +84,10 @@ public virtual void TestDocBoost_Mem() private sealed class CollectorAnonymousClass : ICollector { - private readonly TestDocBoost outerInstance; - private readonly float[] scores; - public CollectorAnonymousClass(TestDocBoost outerInstance, float[] scores) + public CollectorAnonymousClass(float[] scores) { - this.outerInstance = outerInstance; this.scores = scores; @base = 0; } @@ -116,4 +113,4 @@ public void SetNextReader(AtomicReaderContext context) public bool AcceptsDocsOutOfOrder => true; } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDocIdSet.cs b/src/Lucene.Net.Tests/Search/TestDocIdSet.cs index 7c956eaf5f..d4d38affba 100644 --- a/src/Lucene.Net.Tests/Search/TestDocIdSet.cs +++ b/src/Lucene.Net.Tests/Search/TestDocIdSet.cs @@ -42,9 +42,9 @@ public class TestDocIdSet : LuceneTestCase public virtual void TestFilteredDocIdSet() { const int maxdoc = 10; - DocIdSet innerSet = new DocIdSetAnonymousClass(this, maxdoc); + DocIdSet innerSet = new DocIdSetAnonymousClass(maxdoc); - DocIdSet filteredSet = new FilteredDocIdSetAnonymousClass(this, innerSet); + DocIdSet filteredSet = new FilteredDocIdSetAnonymousClass(innerSet); DocIdSetIterator iter = filteredSet.GetIterator(); IList list = new JCG.List(); @@ -77,13 +77,10 @@ public virtual void TestFilteredDocIdSet() private sealed class DocIdSetAnonymousClass : DocIdSet { - private readonly TestDocIdSet outerInstance; - private readonly int maxdoc; - public DocIdSetAnonymousClass(TestDocIdSet outerInstance, int maxdoc) + public DocIdSetAnonymousClass(int maxdoc) { - this.outerInstance = outerInstance; this.maxdoc = maxdoc; } @@ -126,12 +123,9 @@ public override long GetCost() private sealed class FilteredDocIdSetAnonymousClass : FilteredDocIdSet { - private readonly TestDocIdSet outerInstance; - - public FilteredDocIdSetAnonymousClass(TestDocIdSet outerInstance, DocIdSet innerSet) + public FilteredDocIdSetAnonymousClass(DocIdSet innerSet) : base(innerSet) { - this.outerInstance = outerInstance; } protected override bool Match(int docid) @@ -158,7 +152,7 @@ public virtual void TestNullDocIdSet() Assert.AreEqual(1, searcher.Search(new MatchAllDocsQuery(), 10).TotalHits); // Now search w/ a Filter which returns a null DocIdSet - Filter f = new FilterAnonymousClass(this); + Filter f = new FilterAnonymousClass(); Assert.AreEqual(0, searcher.Search(new MatchAllDocsQuery(), f, 10).TotalHits); reader.Dispose(); @@ -167,13 +161,6 @@ public virtual void TestNullDocIdSet() private sealed class FilterAnonymousClass : Filter { - private readonly TestDocIdSet outerInstance; - - public FilterAnonymousClass(TestDocIdSet outerInstance) - { - this.outerInstance = outerInstance; - } - public override DocIdSet GetDocIdSet(AtomicReaderContext context, IBits acceptDocs) { return null; @@ -196,7 +183,7 @@ public virtual void TestNullIteratorFilteredDocIdSet() Assert.AreEqual(1, searcher.Search(new MatchAllDocsQuery(), 10).TotalHits); // Now search w/ a Filter which returns a null DocIdSet - Filter f = new FilterAnonymousClass2(this); + Filter f = new FilterAnonymousClass2(); Assert.AreEqual(0, searcher.Search(new MatchAllDocsQuery(), f, 10).TotalHits); reader.Dispose(); @@ -205,28 +192,14 @@ public virtual void TestNullIteratorFilteredDocIdSet() private sealed class FilterAnonymousClass2 : Filter { - private readonly TestDocIdSet outerInstance; - - public FilterAnonymousClass2(TestDocIdSet outerInstance) - { - this.outerInstance = outerInstance; - } - public override DocIdSet GetDocIdSet(AtomicReaderContext context, IBits acceptDocs) { - DocIdSet innerNullIteratorSet = new DocIdSetAnonymousClass2(this); - return new FilteredDocIdSetAnonymousClass2(this, innerNullIteratorSet); + DocIdSet innerNullIteratorSet = new DocIdSetAnonymousClass2(); + return new FilteredDocIdSetAnonymousClass2(innerNullIteratorSet); } private sealed class DocIdSetAnonymousClass2 : DocIdSet { - private readonly FilterAnonymousClass2 outerInstance; - - public DocIdSetAnonymousClass2(FilterAnonymousClass2 outerInstance) - { - this.outerInstance = outerInstance; - } - public override DocIdSetIterator GetIterator() { return null; @@ -235,12 +208,9 @@ public override DocIdSetIterator GetIterator() private sealed class FilteredDocIdSetAnonymousClass2 : FilteredDocIdSet { - private readonly FilterAnonymousClass2 outerInstance; - - public FilteredDocIdSetAnonymousClass2(FilterAnonymousClass2 outerInstance, DocIdSet innerNullIteratorSet) + public FilteredDocIdSetAnonymousClass2(DocIdSet innerNullIteratorSet) : base(innerNullIteratorSet) { - this.outerInstance = outerInstance; } protected override bool Match(int docid) @@ -250,4 +220,4 @@ protected override bool Match(int docid) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDocTermOrdsRangeFilter.cs b/src/Lucene.Net.Tests/Search/TestDocTermOrdsRangeFilter.cs index c9203cb3f6..d59a1bc54c 100644 --- a/src/Lucene.Net.Tests/Search/TestDocTermOrdsRangeFilter.cs +++ b/src/Lucene.Net.Tests/Search/TestDocTermOrdsRangeFilter.cs @@ -3,7 +3,6 @@ using NUnit.Framework; using RandomizedTesting.Generators; using System; -using System.Collections.Generic; using JCG = J2N.Collections.Generic; using Console = Lucene.Net.Util.SystemConsole; @@ -140,7 +139,7 @@ protected internal virtual void AssertSame(BytesRef lowerVal, BytesRef upperVal, { Query docValues = new ConstantScoreQuery(DocTermOrdsRangeFilter.NewBytesRefRange(fieldName, lowerVal, upperVal, includeLower, includeUpper)); MultiTermQuery inverted = new TermRangeQuery(fieldName, lowerVal, upperVal, includeLower, includeUpper); - inverted.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE); + inverted.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE; TopDocs invertedDocs = searcher1.Search(inverted, 25); TopDocs docValuesDocs = searcher2.Search(docValues, 25); @@ -148,4 +147,4 @@ protected internal virtual void AssertSame(BytesRef lowerVal, BytesRef upperVal, CheckHits.CheckEqual(inverted, invertedDocs.ScoreDocs, docValuesDocs.ScoreDocs); } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDocTermOrdsRewriteMethod.cs b/src/Lucene.Net.Tests/Search/TestDocTermOrdsRewriteMethod.cs index c7e102f562..4bd5265719 100644 --- a/src/Lucene.Net.Tests/Search/TestDocTermOrdsRewriteMethod.cs +++ b/src/Lucene.Net.Tests/Search/TestDocTermOrdsRewriteMethod.cs @@ -4,7 +4,6 @@ using NUnit.Framework; using RandomizedTesting.Generators; using System; -using System.Collections.Generic; using JCG = J2N.Collections.Generic; using Assert = Lucene.Net.TestFramework.Assert; using Console = Lucene.Net.Util.SystemConsole; @@ -139,7 +138,7 @@ public virtual void TestRegexps() protected internal virtual void AssertSame(string regexp) { RegexpQuery docValues = new RegexpQuery(new Term(fieldName, regexp), RegExpSyntax.NONE); - docValues.MultiTermRewriteMethod = (new DocTermOrdsRewriteMethod()); + docValues.MultiTermRewriteMethod = new DocTermOrdsRewriteMethod(); RegexpQuery inverted = new RegexpQuery(new Term(fieldName, regexp), RegExpSyntax.NONE); TopDocs invertedDocs = searcher1.Search(inverted, 25); @@ -157,12 +156,12 @@ public virtual void TestEquals() Assert.AreEqual(a1, a2); Assert.IsFalse(a1.Equals(b)); - a1.MultiTermRewriteMethod = (new DocTermOrdsRewriteMethod()); - a2.MultiTermRewriteMethod = (new DocTermOrdsRewriteMethod()); - b.MultiTermRewriteMethod = (new DocTermOrdsRewriteMethod()); + a1.MultiTermRewriteMethod = new DocTermOrdsRewriteMethod(); + a2.MultiTermRewriteMethod = new DocTermOrdsRewriteMethod(); + b.MultiTermRewriteMethod = new DocTermOrdsRewriteMethod(); Assert.AreEqual(a1, a2); Assert.IsFalse(a1.Equals(b)); QueryUtils.Check(a1); } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestDocValuesScoring.cs b/src/Lucene.Net.Tests/Search/TestDocValuesScoring.cs index c42afbf0c5..2aaa6eeea4 100644 --- a/src/Lucene.Net.Tests/Search/TestDocValuesScoring.cs +++ b/src/Lucene.Net.Tests/Search/TestDocValuesScoring.cs @@ -77,7 +77,7 @@ public virtual void TestSimple() Similarity @base = searcher1.Similarity; // boosting IndexSearcher searcher2 = NewSearcher(ir, false); - searcher2.Similarity = new PerFieldSimilarityWrapperAnonymousClass(this, field, @base); + searcher2.Similarity = new PerFieldSimilarityWrapperAnonymousClass(@base); // in this case, we searched on field "foo". first document should have 2x the score. TermQuery tq = new TermQuery(new Term("foo", "quick")); @@ -123,15 +123,10 @@ public virtual void TestSimple() private sealed class PerFieldSimilarityWrapperAnonymousClass : PerFieldSimilarityWrapper { - private readonly TestDocValuesScoring outerInstance; - - private Field field; private Similarity @base; - public PerFieldSimilarityWrapperAnonymousClass(TestDocValuesScoring outerInstance, Field field, Similarity @base) + public PerFieldSimilarityWrapperAnonymousClass(Similarity @base) { - this.outerInstance = outerInstance; - this.field = field; this.@base = @base; fooSim = new BoostingSimilarity(@base, "foo_boost"); } @@ -230,4 +225,4 @@ public override Explanation Explain(int doc, Explanation freq) } } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestEarlyTermination.cs b/src/Lucene.Net.Tests/Search/TestEarlyTermination.cs index fee4d47f33..5f711568a4 100644 --- a/src/Lucene.Net.Tests/Search/TestEarlyTermination.cs +++ b/src/Lucene.Net.Tests/Search/TestEarlyTermination.cs @@ -68,7 +68,7 @@ public virtual void TestEarlyTermination_Mem() for (int i = 0; i < iters; ++i) { IndexSearcher searcher = NewSearcher(reader); - ICollector collector = new CollectorAnonymousClass(this); + ICollector collector = new CollectorAnonymousClass(); searcher.Search(new MatchAllDocsQuery(), collector); } @@ -77,17 +77,8 @@ public virtual void TestEarlyTermination_Mem() private sealed class CollectorAnonymousClass : ICollector { - private readonly TestEarlyTermination outerInstance; - - public CollectorAnonymousClass(TestEarlyTermination outerInstance) - { - this.outerInstance = outerInstance; - outOfOrder = Random.NextBoolean(); - collectionTerminated = true; - } - - internal readonly bool outOfOrder; - internal bool collectionTerminated; + internal readonly bool outOfOrder = Random.NextBoolean(); + internal bool collectionTerminated = true; public void SetScorer(Scorer scorer) { @@ -119,4 +110,4 @@ public void SetNextReader(AtomicReaderContext context) public bool AcceptsDocsOutOfOrder => outOfOrder; } } -} \ No newline at end of file +} diff --git a/src/Lucene.Net.Tests/Search/TestElevationComparator.cs b/src/Lucene.Net.Tests/Search/TestElevationComparator.cs index 1cd2aab82f..e7dedc6895 100644 --- a/src/Lucene.Net.Tests/Search/TestElevationComparator.cs +++ b/src/Lucene.Net.Tests/Search/TestElevationComparator.cs @@ -73,8 +73,10 @@ private void RunTest(IndexSearcher searcher, bool reversed) newq.Add(query, Occur.SHOULD); newq.Add(GetElevatedQuery(new string[] { "id", "a", "id", "x" }), Occur.SHOULD); - Sort sort = new Sort(new SortField("id", new ElevationComparerSource(priority), false), new SortField(null, SortFieldType.SCORE, reversed) - ); + Sort sort = new Sort( + new SortField("id", new ElevationComparerSource(priority), false), + new SortField(null, SortFieldType.SCORE, reversed) + ); TopDocsCollector topCollector = TopFieldCollector.Create(sort, 50, false, true, true, true); searcher.Search(newq, null, topCollector); @@ -156,13 +158,11 @@ private sealed class FieldComparerAnonymousClass : FieldComparer - /// LUCENENET specific - /// Is non-static because NewIndexWriterConfig, NewTextField and - /// NewStringField are no longer static. - /// [OneTimeSetUp] public override void BeforeClass() // LUCENENET specific - renamed from BeforeClassTestExplanations() to ensure calling order { @@ -100,7 +95,13 @@ public override void BeforeClass() // LUCENENET specific - renamed from BeforeCl searcher = NewSearcher(reader); } - protected internal static readonly string[] docFields = new string[] { "w1 w2 w3 w4 w5", "w1 w3 w2 w3 zz", "w1 xx w2 yy w3", "w1 w3 xx w2 yy w3 zz" }; + protected internal static readonly string[] docFields = new string[] + { + "w1 w2 w3 w4 w5", + "w1 w3 w2 w3 zz", + "w1 xx w2 yy w3", + "w1 w3 xx w2 yy w3 zz" + }; /// /// check the expDocNrs first, then check the query (and the explanations) @@ -269,4 +270,4 @@ public virtual void TestNoop() /* NOOP */ } } -} \ No newline at end of file +}