Skip to content

Commit

Permalink
Test review for rest of Search, apache#259
Browse files Browse the repository at this point in the history
  • Loading branch information
paulirwin committed Jan 25, 2024
1 parent fcbdd92 commit 1f84445
Show file tree
Hide file tree
Showing 41 changed files with 572 additions and 544 deletions.
17 changes: 0 additions & 17 deletions src/Lucene.Net.Tests/Search/TestFieldCacheRewriteMethod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,22 +61,5 @@ public virtual void TestEquals()
Assert.IsFalse(a1.Equals(b));
QueryUtils.Check(a1);
}



#region TestSnapshotDeletionPolicy
// LUCENENET NOTE: Tests in a base class are not pulled into the correct
// context in Visual Studio. This fixes that with the minimum amount of code necessary
// to run them in the correct context without duplicating all of the tests.

/// <summary>
/// test a bunch of random regular expressions </summary>
[Test]
public override void TestRegexps()
{
base.TestRegexps();
}

#endregion
}
}
2 changes: 1 addition & 1 deletion src/Lucene.Net.Tests/Search/TestLiveFieldValues.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;

#if FEATURE_RANDOM_NEXTINT64_NEXTSINGLE
#if !FEATURE_RANDOM_NEXTINT64_NEXTSINGLE
using RandomizedTesting.Generators;
#endif

Expand Down
13 changes: 7 additions & 6 deletions src/Lucene.Net.Tests/Search/TestMinShouldMatch2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Documents;
using Lucene.Net.Index;
using Lucene.Net.Support;
using Lucene.Net.Util;
using NUnit.Framework;
using System.Collections.Generic;
using System.Linq;
using Assert = Lucene.Net.TestFramework.Assert;
using JCG = J2N.Collections.Generic;

Expand Down Expand Up @@ -101,12 +99,15 @@ public override void BeforeClass()
r = DirectoryReader.Open(dir);
atomicReader = GetOnlySegmentReader(r);
searcher = new IndexSearcher(atomicReader);
searcher.Similarity = new DefaultSimilarityAnonymousClass();
searcher.Similarity = DefaultSimilarityAnonymousClass.Default;
}

private sealed class DefaultSimilarityAnonymousClass : DefaultSimilarity
{
public DefaultSimilarityAnonymousClass()
// LUCENENET: making a static readonly instance with private constructor for reduced allocations
public static readonly DefaultSimilarityAnonymousClass Default = new DefaultSimilarityAnonymousClass();

private DefaultSimilarityAnonymousClass()
{
}

Expand Down Expand Up @@ -371,7 +372,7 @@ internal SlowMinShouldMatchScorer(BooleanWeight weight, AtomicReader reader, Ind
if (Debugging.AssertsEnabled) Debugging.Assert(success); // no dups
TermContext context = TermContext.Build(reader.Context, term);
SimWeight w = weight.Similarity.ComputeWeight(1f, searcher.CollectionStatistics("field"), searcher.TermStatistics(term, context));
var dummy = w.GetValueForNormalization(); // ignored
_ = w.GetValueForNormalization(); // ignored
w.Normalize(1F, 1F);
sims[(int)ord] = weight.Similarity.GetSimScorer(w, (AtomicReaderContext)reader.Context);
}
Expand Down Expand Up @@ -428,4 +429,4 @@ public override long GetCost()
}
}
}
}
}
60 changes: 42 additions & 18 deletions src/Lucene.Net.Tests/Search/TestMultiPhraseQuery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
using NUnit.Framework;
using System;
using System.Collections.Generic;
using System.Linq;
using Assert = Lucene.Net.TestFramework.Assert;
using Console = Lucene.Net.Util.SystemConsole;

Expand Down Expand Up @@ -354,7 +353,7 @@ private void Add(string s, string type, RandomIndexWriter writer)
[Test]
public virtual void TestEmptyToString()
{
(new MultiPhraseQuery()).ToString();
_ = new MultiPhraseQuery().ToString();
}

[Test]
Expand All @@ -367,7 +366,7 @@ public virtual void TestCustomIDF()

IndexReader reader = writer.GetReader();
IndexSearcher searcher = NewSearcher(reader);
searcher.Similarity = new DefaultSimilarityAnonymousClass(this);
searcher.Similarity = new DefaultSimilarityAnonymousClass();

MultiPhraseQuery query = new MultiPhraseQuery();
query.Add(new Term[] { new Term("body", "this"), new Term("body", "that") });
Expand All @@ -382,13 +381,6 @@ public virtual void TestCustomIDF()

private sealed class DefaultSimilarityAnonymousClass : DefaultSimilarity
{
private readonly TestMultiPhraseQuery outerInstance;

public DefaultSimilarityAnonymousClass(TestMultiPhraseQuery outerInstance)
{
this.outerInstance = outerInstance;
}

public override Explanation IdfExplain(CollectionStatistics collectionStats, TermStatistics[] termStats)
{
return new Explanation(10f, "just a test");
Expand Down Expand Up @@ -462,13 +454,45 @@ private static Token MakeToken(string text, int posIncr)
return t;
}

private static readonly Token[] INCR_0_DOC_TOKENS = new Token[] { MakeToken("x", 1), MakeToken("a", 1), MakeToken("1", 0), MakeToken("m", 1), MakeToken("b", 1), MakeToken("1", 0), MakeToken("n", 1), MakeToken("c", 1), MakeToken("y", 1) };

private static readonly Token[] INCR_0_QUERY_TOKENS_AND = new Token[] { MakeToken("a", 1), MakeToken("1", 0), MakeToken("b", 1), MakeToken("1", 0), MakeToken("c", 1) };

private static readonly Token[][] INCR_0_QUERY_TOKENS_AND_OR_MATCH = new Token[][] { new Token[] { MakeToken("a", 1) }, new Token[] { MakeToken("x", 1), MakeToken("1", 0) }, new Token[] { MakeToken("b", 2) }, new Token[] { MakeToken("x", 2), MakeToken("1", 0) }, new Token[] { MakeToken("c", 3) } };

private static readonly Token[][] INCR_0_QUERY_TOKENS_AND_OR_NO_MATCHN = new Token[][] { new Token[] { MakeToken("x", 1) }, new Token[] { MakeToken("a", 1), MakeToken("1", 0) }, new Token[] { MakeToken("x", 2) }, new Token[] { MakeToken("b", 2), MakeToken("1", 0) }, new Token[] { MakeToken("c", 3) } };
private static readonly Token[] INCR_0_DOC_TOKENS = new Token[]
{
MakeToken("x", 1),
MakeToken("a", 1),
MakeToken("1", 0),
MakeToken("m", 1), // not existing, relying on slop=2
MakeToken("b", 1),
MakeToken("1", 0),
MakeToken("n", 1), // not existing, relying on slop=2
MakeToken("c", 1),
MakeToken("y", 1)
};

private static readonly Token[] INCR_0_QUERY_TOKENS_AND = new Token[]
{
MakeToken("a", 1),
MakeToken("1", 0),
MakeToken("b", 1),
MakeToken("1", 0),
MakeToken("c", 1)
};

private static readonly Token[][] INCR_0_QUERY_TOKENS_AND_OR_MATCH = new Token[][]
{
new[] { MakeToken("a", 1) },
new[] { MakeToken("x", 1), MakeToken("1", 0) },
new[] { MakeToken("b", 2) },
new[] { MakeToken("x", 2), MakeToken("1", 0) },
new[] { MakeToken("c", 3) }
};

private static readonly Token[][] INCR_0_QUERY_TOKENS_AND_OR_NO_MATCHN = new Token[][]
{
new[] { MakeToken("x", 1) },
new[] { MakeToken("a", 1), MakeToken("1", 0) },
new[] { MakeToken("x", 2) },
new[] { MakeToken("b", 2), MakeToken("1", 0) },
new[] { MakeToken("c", 3) }
};

/// <summary>
/// using query parser, MPQ will be created, and will not be strict about having all query terms
Expand Down Expand Up @@ -626,4 +650,4 @@ public virtual void TestNegativeSlop()
}
}
}
}
}
26 changes: 7 additions & 19 deletions src/Lucene.Net.Tests/Search/TestMultiTermConstantScore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,6 @@ public class TestMultiTermConstantScore : BaseTestRangeFilter
internal /*static*/ Directory small;
internal /*static*/ IndexReader reader;

/// <summary>
/// LUCENENET specific
/// Is non-static because NewIndexWriterConfig is no longer static.
/// </summary>
[OneTimeSetUp]
public override void BeforeClass()
{
Expand Down Expand Up @@ -95,7 +91,7 @@ public override void AfterClass()
public static Query Csrq(string f, string l, string h, bool il, bool ih)
{
TermRangeQuery query = TermRangeQuery.NewStringRange(f, l, h, il, ih);
query.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
query.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
if (Verbose)
{
Console.WriteLine("TEST: query=" + query);
Expand All @@ -106,7 +102,7 @@ public static Query Csrq(string f, string l, string h, bool il, bool ih)
public static Query Csrq(string f, string l, string h, bool il, bool ih, MultiTermQuery.RewriteMethod method)
{
TermRangeQuery query = TermRangeQuery.NewStringRange(f, l, h, il, ih);
query.MultiTermRewriteMethod = (method);
query.MultiTermRewriteMethod = method;
if (Verbose)
{
Console.WriteLine("TEST: query=" + query + " method=" + method);
Expand All @@ -119,7 +115,7 @@ public static Query Csrq(string f, string l, string h, bool il, bool ih, MultiTe
public static Query Cspq(Term prefix)
{
PrefixQuery query = new PrefixQuery(prefix);
query.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
query.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
return query;
}

Expand All @@ -128,7 +124,7 @@ public static Query Cspq(Term prefix)
public static Query Cswcq(Term wild)
{
WildcardQuery query = new WildcardQuery(wild);
query.MultiTermRewriteMethod = (MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE);
query.MultiTermRewriteMethod = MultiTermQuery.CONSTANT_SCORE_FILTER_REWRITE;
return query;
}

Expand Down Expand Up @@ -242,7 +238,7 @@ public virtual void TestBoost()
search.Similarity = new DefaultSimilarity();
Query q = Csrq("data", "1", "6", T, T);
q.Boost = 100;
search.Search(q, null, new CollectorAnonymousClass(this));
search.Search(q, null, new CollectorAnonymousClass());

//
// Ensure that boosting works to score one clause of a query higher
Expand Down Expand Up @@ -287,15 +283,7 @@ public virtual void TestBoost()

private sealed class CollectorAnonymousClass : ICollector
{
private readonly TestMultiTermConstantScore outerInstance;

public CollectorAnonymousClass(TestMultiTermConstantScore outerInstance)
{
this.outerInstance = outerInstance;
@base = 0;
}

private int @base;
private int @base = 0;
private Scorer scorer;

public void SetScorer(Scorer scorer)
Expand Down Expand Up @@ -544,4 +532,4 @@ public virtual void TestRangeQueryRand()
assertEquals("max,nul,T,T", 1, result.Length);
}
}
}
}
20 changes: 7 additions & 13 deletions src/Lucene.Net.Tests/Search/TestMultiTermQueryRewrites.cs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ private void CheckBooleanQueryOrder(Query q)
private void CheckDuplicateTerms(MultiTermQuery.RewriteMethod method)
{
MultiTermQuery mtq = TermRangeQuery.NewStringRange("data", "2", "7", true, true);
mtq.MultiTermRewriteMethod = (method);
mtq.MultiTermRewriteMethod = method;
Query q1 = searcher.Rewrite(mtq);
Query q2 = multiSearcher.Rewrite(mtq);
Query q3 = multiSearcherDupls.Rewrite(mtq);
Expand Down Expand Up @@ -184,8 +184,8 @@ private void CheckBooleanQueryBoosts(BooleanQuery bq)

private void CheckBoosts(MultiTermQuery.RewriteMethod method)
{
MultiTermQuery mtq = new MultiTermQueryAnonymousClass(this);
mtq.MultiTermRewriteMethod = (method);
MultiTermQuery mtq = new MultiTermQueryAnonymousClass();
mtq.MultiTermRewriteMethod = method;
Query q1 = searcher.Rewrite(mtq);
Query q2 = multiSearcher.Rewrite(mtq);
Query q3 = multiSearcherDupls.Rewrite(mtq);
Expand All @@ -205,27 +205,21 @@ private void CheckBoosts(MultiTermQuery.RewriteMethod method)

private sealed class MultiTermQueryAnonymousClass : MultiTermQuery
{
private readonly TestMultiTermQueryRewrites outerInstance;

public MultiTermQueryAnonymousClass(TestMultiTermQueryRewrites outerInstance)
public MultiTermQueryAnonymousClass()
: base("data")
{
this.outerInstance = outerInstance;
}

protected override TermsEnum GetTermsEnum(Terms terms, AttributeSource atts)
{
return new TermRangeTermsEnumAnonymousClass(this, terms.GetEnumerator(), new BytesRef("2"), new BytesRef("7"));
return new TermRangeTermsEnumAnonymousClass(terms.GetEnumerator(), new BytesRef("2"), new BytesRef("7"));
}

private sealed class TermRangeTermsEnumAnonymousClass : TermRangeTermsEnum
{
private readonly MultiTermQueryAnonymousClass outerInstance;

public TermRangeTermsEnumAnonymousClass(MultiTermQueryAnonymousClass outerInstance, TermsEnum iterator, BytesRef bref1, BytesRef bref2)
public TermRangeTermsEnumAnonymousClass(TermsEnum iterator, BytesRef bref1, BytesRef bref2)
: base(iterator, bref1, bref2, true, true)
{
this.outerInstance = outerInstance;
boostAtt = Attributes.AddAttribute<IBoostAttribute>();
}

Expand Down Expand Up @@ -305,4 +299,4 @@ public virtual void TestMaxClauseLimitations()
CheckNoMaxClauseLimitation(new MultiTermQuery.TopTermsBoostOnlyBooleanQueryRewrite(1024));
}
}
}
}
9 changes: 3 additions & 6 deletions src/Lucene.Net.Tests/Search/TestMultiThreadTermVectors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@
using Lucene.Net.Diagnostics;
using Lucene.Net.Documents;
using Lucene.Net.Index.Extensions;
using Lucene.Net.Support.Threading;
using NUnit.Framework;
using System;
using System.IO;
using System.Text;
using System.Threading;
using Console = Lucene.Net.Util.SystemConsole;
Expand Down Expand Up @@ -60,7 +58,7 @@ public override void SetUp()
customType.StoreTermVectors = true;
for (int i = 0; i < numDocs; i++)
{
Documents.Document doc = new Documents.Document();
Document doc = new Document();
Field fld = NewField("field", English.Int32ToEnglish(i), customType);
doc.Add(fld);
writer.AddDocument(doc);
Expand Down Expand Up @@ -98,8 +96,7 @@ public virtual void Test()
{
try
{
/// <summary>
/// close the opened reader </summary>
// close the opened reader
reader.Dispose();
}
catch (Exception ioe) when (ioe.IsIOException())
Expand Down Expand Up @@ -240,4 +237,4 @@ private void VerifyVector(TermsEnum vector, int num)
}
}
}
}
}
4 changes: 1 addition & 3 deletions src/Lucene.Net.Tests/Search/TestNot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ namespace Lucene.Net.Search

/// <summary>
/// Similarity unit test.
///
///
/// </summary>
[TestFixture]
public class TestNot : LuceneTestCase
Expand Down Expand Up @@ -62,4 +60,4 @@ public virtual void TestNot_Mem()
store.Dispose();
}
}
}
}
Loading

0 comments on commit 1f84445

Please sign in to comment.