From 8e5ecafa44738765ca04004b8bf96e03e4e94e56 Mon Sep 17 00:00:00 2001 From: Shad Storhaug Date: Sat, 23 Oct 2021 16:36:58 +0700 Subject: [PATCH] BUG: Lucene.Net.Search.FuzzyTermsEnum: Compare using Lucene.Net.Util.NumericUtils.SingleToSortableInt32() to prevent test failures on x86 .NET Framework. This fixes Lucene.Net.Search.TestBooleanQuery.TestBS2DisjunctionNextVsAdvance(), Lucene.Net.Search.TestFuzzyQuery.TestTieBreaker(), and Lucene.Net.Sandbox.Queries.TestSlowFuzzyQuery.TestTieBreaker(). See #269. --- .../Queries/TestSlowFuzzyQuery.cs | 3 --- src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs | 3 --- src/Lucene.Net/Search/FuzzyTermsEnum.cs | 14 +++++++++----- 3 files changed, 9 insertions(+), 11 deletions(-) diff --git a/src/Lucene.Net.Tests.Sandbox/Queries/TestSlowFuzzyQuery.cs b/src/Lucene.Net.Tests.Sandbox/Queries/TestSlowFuzzyQuery.cs index 087c9d5cfd..cec1ccdbd1 100644 --- a/src/Lucene.Net.Tests.Sandbox/Queries/TestSlowFuzzyQuery.cs +++ b/src/Lucene.Net.Tests.Sandbox/Queries/TestSlowFuzzyQuery.cs @@ -334,9 +334,6 @@ public void TestFuzzinessLong() * is not implemented correctly, there will be problems! */ [Test] -#if NETFRAMEWORK - [AwaitsFix(BugUrl = "https://github.com/apache/lucenenet/issues/269")] // LUCENENET TODO: this test fails on x86 on .NET Framework in Release mode only -#endif public void TestTieBreaker() { Directory directory = NewDirectory(); diff --git a/src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs b/src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs index 069c8e033c..b9fc33848b 100644 --- a/src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs +++ b/src/Lucene.Net.Tests/Search/TestFuzzyQuery.cs @@ -249,9 +249,6 @@ public virtual void Test2() /// is not implemented correctly, there will be problems! /// [Test] -#if NETFRAMEWORK - [AwaitsFix(BugUrl = "https://github.com/apache/lucenenet/issues/269")] // LUCENENET TODO: this test fails on x86 on .NET Framework in Release mode only -#endif public virtual void TestTieBreaker() { Directory directory = NewDirectory(); diff --git a/src/Lucene.Net/Search/FuzzyTermsEnum.cs b/src/Lucene.Net/Search/FuzzyTermsEnum.cs index f0f3ee161a..58db0c1cf3 100644 --- a/src/Lucene.Net/Search/FuzzyTermsEnum.cs +++ b/src/Lucene.Net/Search/FuzzyTermsEnum.cs @@ -220,6 +220,7 @@ protected virtual void SetEnum(TermsEnum actualEnum) /// Fired when the max non-competitive boost has changed. This is the hook to /// swap in a smarter actualEnum /// + private void BottomChanged(BytesRef lastTerm, bool init) { int oldMaxEdits = m_maxEdits; @@ -229,7 +230,11 @@ private void BottomChanged(BytesRef lastTerm, bool init) // as long as the max non-competitive boost is >= the max boost // for some edit distance, keep dropping the max edit distance. - while (m_maxEdits > 0 && (termAfter ? bottom >= CalculateMaxBoost(m_maxEdits) : bottom > CalculateMaxBoost(m_maxEdits))) + + // LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled + while (m_maxEdits > 0 && (termAfter ? + NumericUtils.SingleToSortableInt32(bottom) >= NumericUtils.SingleToSortableInt32(CalculateMaxBoost(m_maxEdits)) : + NumericUtils.SingleToSortableInt32(bottom) > NumericUtils.SingleToSortableInt32(CalculateMaxBoost(m_maxEdits)))) { m_maxEdits--; } @@ -380,9 +385,6 @@ public AutomatonFuzzyTermsEnum(FuzzyTermsEnum outerInstance, TermsEnum tenum, Co /// /// Finds the smallest Lev(n) DFA that accepts the term. -#if NETFRAMEWORK - [MethodImpl(MethodImplOptions.NoOptimization)] // LUCENENET specific: comparing float equality fails in x86 on .NET Framework with optimizations enabled, and is causing the TestTokenLengthOpt test to fail -#endif protected override AcceptStatus Accept(BytesRef term) { //System.out.println("AFTE.accept term=" + term); @@ -415,7 +417,9 @@ protected override AcceptStatus Accept(BytesRef term) { int codePointCount = UnicodeUtil.CodePointCount(term); float similarity = 1.0f - ((float)ed / (float)(Math.Min(codePointCount, outerInstance.m_termLength))); - if (similarity > outerInstance.m_minSimilarity) + + // LUCENENET specific - compare bits rather than using equality operators to prevent these comparisons from failing in x86 in .NET Framework with optimizations enabled + if (NumericUtils.SingleToSortableInt32(similarity) > NumericUtils.SingleToSortableInt32(outerInstance.m_minSimilarity)) { boostAtt.Boost = (similarity - outerInstance.m_minSimilarity) * outerInstance.m_scaleFactor; //System.out.println(" yes");