You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucenenet.apache.org by ni...@apache.org on 2019/08/07 22:25:58 UTC

[lucenenet] 01/42: BUG: Lucene.Net.Suggest.Suggest.FileDictionary - Fixed conversion of string to number to be culture insensitive (it caused the tests in FileDictionaryTest to fail randomly)

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 7f69810240e32cfc73bcbf53a63b8a8fbf2ade1b
Author: Shad Storhaug <sh...@shadstorhaug.com>
AuthorDate: Sun Jul 28 17:19:13 2019 +0700

    BUG: Lucene.Net.Suggest.Suggest.FileDictionary - Fixed conversion of string to number to be culture insensitive (it caused the tests in FileDictionaryTest to fail randomly)
---
 src/Lucene.Net.Suggest/Suggest/FileDictionary.cs   | 21 ++++++++++++++-------
 .../Suggest/FileDictionaryTest.cs                  | 22 ++++++++++++----------
 2 files changed, 26 insertions(+), 17 deletions(-)

diff --git a/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs b/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs
index 0928676..cdcb9dc 100644
--- a/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs
+++ b/src/Lucene.Net.Suggest/Suggest/FileDictionary.cs
@@ -2,6 +2,7 @@
 using Lucene.Net.Util;
 using System;
 using System.Collections.Generic;
+using System.Globalization;
 using System.IO;
 using System.Text;
 
@@ -255,14 +256,20 @@ namespace Lucene.Net.Search.Suggest
 
             internal void ReadWeight(string weight)
             {
-                // keep reading floats for bw compat
-                try
+                // LUCENENET specific - don't use exception, use TryParse
+                if (!long.TryParse(weight, NumberStyles.Integer, CultureInfo.InvariantCulture, out curWeight))
                 {
-                    curWeight = Convert.ToInt64(weight);
-                }
-                catch (FormatException)
-                {
-                    curWeight = (long)Convert.ToDouble(weight);
+                    try
+                    {
+                        // keep reading floats for bw compat
+                        curWeight = (long)double.Parse(weight, NumberStyles.Float, CultureInfo.InvariantCulture);
+                    }
+                    catch (FormatException e)
+                    {
+                        // LUCENENET TODO: This is just so we can see what string and what culture was being tested when parsing failed,
+                        // to try to reproduce the conditions of the failure.
+                        throw new FormatException($"Weight '{weight}' could not be parsed to long or double in culture '{CultureInfo.CurrentCulture.Name}'.", e);
+                    }
                 }
             }
 
diff --git a/src/Lucene.Net.Tests.Suggest/Suggest/FileDictionaryTest.cs b/src/Lucene.Net.Tests.Suggest/Suggest/FileDictionaryTest.cs
index cbdf5f8..74ec357 100644
--- a/src/Lucene.Net.Tests.Suggest/Suggest/FileDictionaryTest.cs
+++ b/src/Lucene.Net.Tests.Suggest/Suggest/FileDictionaryTest.cs
@@ -32,24 +32,26 @@ namespace Lucene.Net.Search.Suggest
             List<string> entryValues = new List<string>();
             StringBuilder sb = new StringBuilder();
             string term = TestUtil.RandomSimpleString(Random(), 1, 300);
-            sb.append(term);
+            sb.Append(term);
             entryValues.Add(term);
             if (hasWeight)
             {
-                sb.append(fieldDelimiter);
+                sb.Append(fieldDelimiter);
                 long weight = TestUtil.NextLong(Random(), long.MinValue, long.MaxValue);
-                sb.append(weight);
-                entryValues.Add(weight.ToString());
+                // LUCENENET: We need to explicitly use invariant culture here,
+                // as that is what is expected in Java
+                sb.Append(weight.ToString(CultureInfo.InvariantCulture));
+                entryValues.Add(weight.ToString(CultureInfo.InvariantCulture));
             }
             if (hasPayload)
             {
-                sb.append(fieldDelimiter);
+                sb.Append(fieldDelimiter);
                 string payload = TestUtil.RandomSimpleString(Random(), 1, 300);
-                sb.append(payload);
+                sb.Append(payload);
                 entryValues.Add(payload);
             }
             sb.append("\n");
-            return new KeyValuePair<List<string>, string>(entryValues, sb.toString());
+            return new KeyValuePair<List<string>, string>(entryValues, sb.ToString());
         }
 
         private KeyValuePair<List<List<string>>, string> generateFileInput(int count, string fieldDelimiter, bool hasWeights, bool hasPayloads)
@@ -65,9 +67,9 @@ namespace Lucene.Net.Search.Suggest
                 }
                 KeyValuePair<List<string>, string> entrySet = GenerateFileEntry(fieldDelimiter, (!hasPayloads && hasWeights) ? Random().nextBoolean() : hasWeights, hasPayload);
                 entries.Add(entrySet.Key);
-                sb.append(entrySet.Value);
+                sb.Append(entrySet.Value);
             }
-            return new KeyValuePair<List<List<string>>, string>(entries, sb.toString());
+            return new KeyValuePair<List<List<string>>, string>(entries, sb.ToString());
         }
 
         [Test]
@@ -198,7 +200,7 @@ namespace Lucene.Net.Search.Suggest
                 List<string> entry = entries[count];
                 assertTrue(entry.size() >= 2); // at least term and weight
                 assertEquals(entry[0], term.Utf8ToString());
-                assertEquals(long.Parse(entry[1]), inputIter.Weight);
+                assertEquals(long.Parse(entry[1], CultureInfo.InvariantCulture), inputIter.Weight);
                 if (entry.size() == 3)
                 {
                     assertEquals(entry[2], inputIter.Payload.Utf8ToString());