Skip to content

Commit e08a342

Browse files
authored
Test review Core J-S, #259 (#914)
* 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
1 parent b1476ae commit e08a342

File tree

123 files changed

+1414
-1728
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

123 files changed

+1414
-1728
lines changed

Lucene.Net.sln.DotSettings

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
<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">
2+
<s:Boolean x:Key="/Default/UserDictionary/Words/=Coord/@EntryIndexedValue">True</s:Boolean></wpf:ResourceDictionary>

src/Lucene.Net.TestFramework/Search/CheckHits.cs

+6-7
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Collections.Generic;
55
using System.Diagnostics;
66
using System.Globalization;
7-
using System.IO;
87
using System.Text;
98
using JCG = J2N.Collections.Generic;
109
using Assert = Lucene.Net.TestFramework.Assert;
@@ -59,13 +58,13 @@ public static void CheckNoMatchExplanations(Query q, string defaultFieldName, In
5958
JCG.SortedSet<int> ignore = new JCG.SortedSet<int>();
6059
for (int i = 0; i < results.Length; i++)
6160
{
62-
ignore.Add(Convert.ToInt32(results[i], CultureInfo.InvariantCulture));
61+
ignore.Add(results[i]);
6362
}
6463

6564
int maxDoc = searcher.IndexReader.MaxDoc;
6665
for (int doc = 0; doc < maxDoc; doc++)
6766
{
68-
if (ignore.Contains(Convert.ToInt32(doc, CultureInfo.InvariantCulture)))
67+
if (ignore.Contains(doc))
6968
{
7069
continue;
7170
}
@@ -133,13 +132,13 @@ public static void DoCheckHits(Random random, Query query, string defaultFieldNa
133132
JCG.SortedSet<int> correct = new JCG.SortedSet<int>();
134133
for (int i = 0; i < results.Length; i++)
135134
{
136-
correct.Add(Convert.ToInt32(results[i], CultureInfo.InvariantCulture));
135+
correct.Add(results[i]);
137136
}
138137

139138
JCG.SortedSet<int> actual = new JCG.SortedSet<int>();
140139
for (int i = 0; i < hits.Length; i++)
141140
{
142-
actual.Add(Convert.ToInt32(hits[i].Doc, CultureInfo.InvariantCulture));
141+
actual.Add(hits[i].Doc);
143142
}
144143

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

424423
public virtual void Collect(int doc)
425424
{
426-
bag.Add(Convert.ToInt32(doc + @base, CultureInfo.InvariantCulture));
425+
bag.Add(doc + @base);
427426
}
428427

429428
public virtual void SetNextReader(AtomicReaderContext context)
@@ -539,4 +538,4 @@ public virtual void SetNextReader(AtomicReaderContext context)
539538

540539
public virtual bool AcceptsDocsOutOfOrder => true;
541540
}
542-
}
541+
}

src/Lucene.Net.Tests/Search/BaseTestRangeFilter.cs

+5-14
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using J2N.Text;
12
using Lucene.Net.Documents;
23
using Lucene.Net.Index.Extensions;
34
using NUnit.Framework;
@@ -68,7 +69,7 @@ internal TestIndex(Random random, int minR, int maxR, bool allowNegativeRandomIn
6869
internal static TestIndex signedIndexDir;
6970
internal static TestIndex unsignedIndexDir;
7071

71-
internal static int minId = 0;
72+
internal const int minId = 0;
7273
internal static int maxId;
7374

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

99-
/// <summary>
100-
/// LUCENENET specific
101-
/// Is non-static because <see cref="Build(Random, TestIndex)"/> is no
102-
/// longer static.
103-
/// </summary>
104100
[OneTimeSetUp]
105101
public override void BeforeClass() // LUCENENET specific: renamed from BeforeClassBaseTestRangeFilter() so we can override to control the order of execution
106102
{
@@ -127,12 +123,7 @@ public override void AfterClass() // LUCENENET specific: renamed from AfterClass
127123
base.AfterClass();
128124
}
129125

130-
/// <summary>
131-
/// LUCENENET specific
132-
/// Passed in because NewStringField and NewIndexWriterConfig are no
133-
/// longer static.
134-
/// </summary>
135-
private IndexReader Build(Random random, TestIndex index)
126+
private static IndexReader Build(Random random, TestIndex index)
136127
{
137128
/* build an index */
138129

@@ -208,8 +199,8 @@ public virtual void TestPad()
208199
string bb = Pad(b);
209200
string label = a + ":" + aa + " vs " + b + ":" + bb;
210201
Assert.AreEqual(aa.Length, bb.Length, "i=" + i + ": length of " + label);
211-
Assert.IsTrue(System.String.Compare(aa, bb, System.StringComparison.Ordinal) < 0, "i=" + i + ": compare less than " + label);
202+
Assert.IsTrue(aa.CompareToOrdinal(bb) < 0, "i=" + i + ": compare less than " + label);
212203
}
213204
}
214205
}
215-
}
206+
}

src/Lucene.Net.Tests/Search/FuzzyTermOnShortTermsTest.cs

+5-7
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,12 @@ public static Analyzer Analyzer
9191
}
9292
}
9393

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

103101
foreach (string s in vals)
104102
{
@@ -110,4 +108,4 @@ public Directory GetDirectory(Analyzer analyzer, string[] vals)
110108
return directory;
111109
}
112110
}
113-
}
111+
}

src/Lucene.Net.Tests/Search/JustCompileSearch.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using Lucene.Net.Util;
2-
using System;
32

43
namespace Lucene.Net.Search
54
{
@@ -330,4 +329,4 @@ public override Scorer GetScorer(AtomicReaderContext context, IBits acceptDocs)
330329
}
331330
}
332331
}
333-
}
332+
}

src/Lucene.Net.Tests/Search/MultiCollectorTest.cs

+2-1
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ public class MultiCollectorTest : LuceneTestCase
2929
{
3030
private class DummyCollector : ICollector
3131
{
32+
// LUCENENET: .NET conventions are to use properties instead of fields where feasible
3233
internal bool AcceptsDocsOutOfOrderCalled { get; private set; } = false;
3334
internal bool CollectCalled { get; private set; } = false;
3435
internal bool SetNextReaderCalled { get; private set; } = false;
@@ -115,4 +116,4 @@ public virtual void TestCollector()
115116
}
116117
}
117118
}
118-
}
119+
}

src/Lucene.Net.Tests/Search/Payloads/PayloadHelper.cs

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ public virtual IndexSearcher SetUp(Random random, Similarity similarity, int num
132132
PayloadAnalyzer analyzer = new PayloadAnalyzer(this);
133133

134134
// TODO randomize this
135-
IndexWriter writer = new IndexWriter(directory, (new IndexWriterConfig(LuceneTestCase.TEST_VERSION_CURRENT, analyzer)).SetSimilarity(similarity));
135+
IndexWriter writer = new IndexWriter(directory, new IndexWriterConfig(LuceneTestCase.TEST_VERSION_CURRENT, analyzer).SetSimilarity(similarity));
136136
// writer.infoStream = System.out;
137137
for (int i = 0; i < numDocs; i++)
138138
{
@@ -156,4 +156,4 @@ public virtual void TearDown()
156156
Reader.Dispose();
157157
}
158158
}
159-
}
159+
}

src/Lucene.Net.Tests/Search/Payloads/TestPayloadExplanations.cs

+4-5
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,15 @@ public class TestPayloadExplanations : TestExplanations
4141
public override void SetUp()
4242
{
4343
base.SetUp();
44-
searcher.Similarity = new DefaultSimilarityAnonymousClass(this);
44+
searcher.Similarity = DefaultSimilarityAnonymousClass.Default;
4545
}
4646

4747
private sealed class DefaultSimilarityAnonymousClass : DefaultSimilarity
4848
{
49-
private readonly TestPayloadExplanations outerInstance;
49+
public static readonly DefaultSimilarityAnonymousClass Default = new DefaultSimilarityAnonymousClass();
5050

51-
public DefaultSimilarityAnonymousClass(TestPayloadExplanations outerInstance)
51+
private DefaultSimilarityAnonymousClass()
5252
{
53-
this.outerInstance = outerInstance;
5453
}
5554

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

119118
// TODO: test the payloadnear query too!
120119
}
121-
}
120+
}

src/Lucene.Net.Tests/Search/Payloads/TestPayloadNearQuery.cs

+4-6
Original file line numberDiff line numberDiff line change
@@ -114,17 +114,15 @@ private PayloadNearQuery NewPhraseQuery(string fieldName, string phrase, bool in
114114
return new PayloadNearQuery(clauses, 0, inOrder, function);
115115
}
116116

117-
/// <summary>
118-
/// LUCENENET specific
119-
/// Is non-static because NewIndexWriterConfig is no longer static.
120-
/// </summary>
121117
[OneTimeSetUp]
122118
public override void BeforeClass()
123119
{
124120
base.BeforeClass();
125121

126122
directory = NewDirectory();
127-
RandomIndexWriter writer = new RandomIndexWriter(Random, directory, NewIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer()).SetSimilarity(similarity));
123+
RandomIndexWriter writer = new RandomIndexWriter(Random, directory,
124+
NewIndexWriterConfig(TEST_VERSION_CURRENT, new PayloadAnalyzer())
125+
.SetSimilarity(similarity));
128126
//writer.infoStream = System.out;
129127
for (int i = 0; i < 1000; i++)
130128
{
@@ -396,4 +394,4 @@ public override Explanation IdfExplain(CollectionStatistics collectionStats, Ter
396394
}
397395
}
398396
}
399-
}
397+
}

src/Lucene.Net.Tests/Search/Payloads/TestPayloadTermQuery.cs

+24-9
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,18 @@ public class TestPayloadTermQuery : LuceneTestCase
4848
{
4949
private static IndexSearcher searcher;
5050
private static IndexReader reader;
51-
private static readonly Similarity similarity = new BoostingSimilarity();
51+
private static readonly Similarity similarity = BoostingSimilarity.Default; // LUCENENET: using static instance
5252
private static readonly byte[] payloadField = { 1 };
5353
private static readonly byte[] payloadMultiField1 = { 2 };
5454
private static readonly byte[] payloadMultiField2 = { 4 };
5555
protected internal static Directory directory;
5656

5757
private class PayloadAnalyzer : Analyzer
5858
{
59-
internal PayloadAnalyzer()
59+
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
60+
public static readonly PayloadAnalyzer Default = new PayloadAnalyzer();
61+
62+
private PayloadAnalyzer()
6063
: base(PER_FIELD_REUSE_STRATEGY)
6164
{
6265
}
@@ -118,17 +121,15 @@ public override void Reset()
118121
}
119122
}
120123

121-
/// <summary>
122-
/// LUCENENET specific
123-
/// Is non-static because NewIndexWriterConfig is no longer static.
124-
/// </summary>
125124
[OneTimeSetUp]
126125
public override void BeforeClass()
127126
{
128127
base.BeforeClass();
129128

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

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

321322
internal class BoostingSimilarity : DefaultSimilarity
322323
{
324+
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
325+
public static readonly BoostingSimilarity Default = new BoostingSimilarity();
326+
327+
private BoostingSimilarity()
328+
{
329+
}
330+
323331
public override float QueryNorm(float sumOfSquaredWeights)
324332
{
325333
return 1;
@@ -363,11 +371,18 @@ public override float Tf(float freq)
363371

364372
internal class FullSimilarity : DefaultSimilarity
365373
{
374+
// LUCENENET: static singleton instance and private ctor to avoid unnecessary allocations
375+
public static readonly FullSimilarity Default = new FullSimilarity();
376+
377+
private FullSimilarity()
378+
{
379+
}
380+
366381
public virtual float ScorePayload(int docId, string fieldName, sbyte[] payload, int offset, int length)
367382
{
368383
//we know it is size 4 here, so ignore the offset/length
369384
return payload[offset];
370385
}
371386
}
372387
}
373-
}
388+
}

src/Lucene.Net.Tests/Search/Similarities/TestSimilarityBase.cs

+35-7
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,41 @@ public class TestSimilarityBase : LuceneTestCase
7575

7676
/// <summary>
7777
/// The DFR basic models to test. </summary>
78-
internal static BasicModel[] BASIC_MODELS = new BasicModel[] { new BasicModelBE(), new BasicModelD(), new BasicModelG(), new BasicModelIF(), new BasicModelIn(), new BasicModelIne(), new BasicModelP() };
78+
internal static BasicModel[] BASIC_MODELS =
79+
{
80+
new BasicModelBE(), new BasicModelD(), new BasicModelG(),
81+
new BasicModelIF(), new BasicModelIn(), new BasicModelIne(),
82+
new BasicModelP()
83+
};
7984

8085
/// <summary>
8186
/// The DFR aftereffects to test. </summary>
82-
internal static AfterEffect[] AFTER_EFFECTS = new AfterEffect[] { new AfterEffectB(), new AfterEffectL(), new AfterEffect.NoAfterEffect() };
87+
internal static AfterEffect[] AFTER_EFFECTS =
88+
{
89+
new AfterEffectB(), new AfterEffectL(), new AfterEffect.NoAfterEffect()
90+
};
8391

8492
/// <summary>
8593
/// The DFR normalizations to test. </summary>
86-
internal static Normalization[] NORMALIZATIONS = new Normalization[] { new NormalizationH1(), new NormalizationH2(), new NormalizationH3(), new NormalizationZ(), new Normalization.NoNormalization() };
94+
internal static Normalization[] NORMALIZATIONS =
95+
{
96+
new NormalizationH1(), new NormalizationH2(), new NormalizationH3(),
97+
new NormalizationZ(), new Normalization.NoNormalization()
98+
};
8799

88100
/// <summary>
89101
/// The distributions for IB. </summary>
90-
internal static Distribution[] DISTRIBUTIONS = new Distribution[] { new DistributionLL(), new DistributionSPL() };
102+
internal static Distribution[] DISTRIBUTIONS =
103+
{
104+
new DistributionLL(), new DistributionSPL()
105+
};
91106

92107
/// <summary>
93108
/// Lambdas for IB. </summary>
94-
internal static Lambda[] LAMBDAS = new Lambda[] { new LambdaDF(), new LambdaTTF() };
109+
internal static Lambda[] LAMBDAS =
110+
{
111+
new LambdaDF(), new LambdaTTF()
112+
};
95113

96114
private IndexSearcher searcher;
97115
private Directory dir;
@@ -587,7 +605,17 @@ private void CorrectnessTestCore(SimilarityBase sim, float gold)
587605

588606
/// <summary>
589607
/// The "collection" for the integration tests. </summary>
590-
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 ." };
608+
internal string[] docs = new string[]
609+
{
610+
"Tiger, tiger burning bright In the forest of the night What immortal hand or eye Could frame thy fearful symmetry ?",
611+
"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 ?",
612+
"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 ?",
613+
"What the hammer? What the chain ? In what furnace was thy brain ? What the anvil ? And what dread grasp Dare its deadly terrors clasp ?",
614+
"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 ?",
615+
"Tiger, tiger burning bright In the forest of the night What immortal hand or eye Dare frame thy fearful symmetry ?",
616+
"Cruelty has a human heart And jealousy a human face Terror the human form divine And Secrecy the human dress .",
617+
"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 ."
618+
};
591619

592620
/// <summary>
593621
/// Tests whether all similarities return three documents for the query word
@@ -649,4 +677,4 @@ public virtual void TestDiscountOverlapsBoost()
649677
Assert.AreEqual(expected.ComputeNorm(state), actual.ComputeNorm(state));
650678
}
651679
}
652-
}
680+
}

0 commit comments

Comments
 (0)