This is an automated email from the ASF dual-hosted git repository.

nightowl888 pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/lucenenet.git

commit 58fa97ebf43cf4fddb2359d471c9bafbeaca8eea
Author: Shad Storhaug <[email protected]>
AuthorDate: Mon Feb 22 06:46:47 2021 +0700

    BUG: Lucene.Net.Tests.Queries.TermsFilterTest::TestSingleFieldEquals(): 
Hash code collision provided in Lucene won't necessarily be a collision in 
.NET. Generate the collision at runtime so the test will pass (the test 
wouldn't have passed if the assert hadn't been commented). (#259)
---
 src/Lucene.Net.Tests.Queries/TermsFilterTest.cs | 43 +++++++++++++++++++++++--
 1 file changed, 40 insertions(+), 3 deletions(-)

diff --git a/src/Lucene.Net.Tests.Queries/TermsFilterTest.cs 
b/src/Lucene.Net.Tests.Queries/TermsFilterTest.cs
index 45b1add..7627f18 100644
--- a/src/Lucene.Net.Tests.Queries/TermsFilterTest.cs
+++ b/src/Lucene.Net.Tests.Queries/TermsFilterTest.cs
@@ -339,13 +339,50 @@ namespace Lucene.Net.Tests.Queries
         [Test]
         public void TestSingleFieldEquals()
         {
-            // Two terms with the same hash code
+            //// Two terms with the same hash code
             //assertEquals("AaAaBB".GetHashCode(), "BBBBBB".GetHashCode());
-            TermsFilter left = TermsFilter(true, new Term("id", "AaAaAa"), new 
Term("id", "AaAaBB"));
-            TermsFilter right = TermsFilter(true, new Term("id", "AaAaAa"), 
new Term("id", "BBBBBB"));
+            //TermsFilter left = TermsFilter(true, new Term("id", "AaAaAa"), 
new Term("id", "AaAaBB"));
+            //TermsFilter right = TermsFilter(true, new Term("id", "AaAaAa"), 
new Term("id", "BBBBBB"));
+            //assertFalse(left.Equals(right));
+
+            // LUCENENET specific - since in .NET the hash code is dependent 
on the underlying
+            // target framework, we need to generate a collision at runtime.
+            GenerateHashCollision(out string theString, out string 
stringWithCollision);
+            assertEquals(theString.GetHashCode(), 
stringWithCollision.GetHashCode());
+            TermsFilter left = TermsFilter(true, new Term("id", "AaAaAa"), new 
Term("id", theString));
+            TermsFilter right = TermsFilter(true, new Term("id", "AaAaAa"), 
new Term("id", stringWithCollision));
             assertFalse(left.Equals(right));
         }
 
+        // LUCENENET specific - since in .NET the hash code is dependent on 
the underlying
+        // target framework, we need to generate a collision at runtime.
+        // Source: https://stackoverflow.com/a/32027473
+        private static void GenerateHashCollision(out string theString, out 
string stringWithCollision)
+        {
+            var words = new Dictionary<int, string>();
+
+            int i = 0;
+            string teststring;
+            while (true)
+            {
+                i++;
+                teststring = i.ToString();
+                try
+                {
+                    words.Add(teststring.GetHashCode(), teststring);
+                }
+                catch (Exception)
+                {
+                    break;
+                }
+            }
+
+            var collisionHash = teststring.GetHashCode();
+
+            theString = teststring;
+            stringWithCollision = words[collisionHash];
+        }
+
         [Test]
         public void TestNoTerms()
         {

Reply via email to