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 2016/10/23 13:01:53 UTC

[07/50] [abbrv] lucenenet git commit: Added missing test Core.Util.TestMathUtil

Added missing test Core.Util.TestMathUtil


Project: http://git-wip-us.apache.org/repos/asf/lucenenet/repo
Commit: http://git-wip-us.apache.org/repos/asf/lucenenet/commit/aff0579e
Tree: http://git-wip-us.apache.org/repos/asf/lucenenet/tree/aff0579e
Diff: http://git-wip-us.apache.org/repos/asf/lucenenet/diff/aff0579e

Branch: refs/heads/master
Commit: aff0579eee93b9868dd5699b5bedd98998dc66d7
Parents: 4b7b267
Author: Shad Storhaug <sh...@shadstorhaug.com>
Authored: Sun Oct 16 05:08:59 2016 +0700
Committer: Shad Storhaug <sh...@shadstorhaug.com>
Committed: Thu Oct 20 18:20:51 2016 +0700

----------------------------------------------------------------------
 src/Lucene.Net.Core/Util/MathUtil.cs           |  5 +--
 src/Lucene.Net.Tests/Lucene.Net.Tests.csproj   |  1 +
 src/Lucene.Net.Tests/core/Util/TestMathUtil.cs | 36 ++++++++++++++-------
 3 files changed, 28 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/lucenenet/blob/aff0579e/src/Lucene.Net.Core/Util/MathUtil.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Core/Util/MathUtil.cs b/src/Lucene.Net.Core/Util/MathUtil.cs
index 87d0aa0..4ea0a6d 100644
--- a/src/Lucene.Net.Core/Util/MathUtil.cs
+++ b/src/Lucene.Net.Core/Util/MathUtil.cs
@@ -68,8 +68,9 @@ namespace Lucene.Net.Util
         // see http://en.wikipedia.org/wiki/Binary_GCD_algorithm#Iterative_version_in_C.2B.2B_using_ctz_.28count_trailing_zeros.29
         public static long Gcd(long a, long b)
         {
-            a = Math.Abs(a);
-            b = Math.Abs(b);
+            // LUCENENET: Math.Abs and BigInteger.Abs get an OverflowException, so we resort to this.
+            a = a < 0 ? -a : a;
+            b = b < 0 ? -b : b;
             if (a == 0)
             {
                 return b;

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/aff0579e/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
index c0ec82b..8cf8890 100644
--- a/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
+++ b/src/Lucene.Net.Tests/Lucene.Net.Tests.csproj
@@ -592,6 +592,7 @@
     <Compile Include="core\Util\TestLongBitSet.cs">
       <SubType>Code</SubType>
     </Compile>
+    <Compile Include="core\Util\TestMathUtil.cs" />
     <Compile Include="core\Util\TestMergedIterator.cs">
       <SubType>Code</SubType>
     </Compile>

http://git-wip-us.apache.org/repos/asf/lucenenet/blob/aff0579e/src/Lucene.Net.Tests/core/Util/TestMathUtil.cs
----------------------------------------------------------------------
diff --git a/src/Lucene.Net.Tests/core/Util/TestMathUtil.cs b/src/Lucene.Net.Tests/core/Util/TestMathUtil.cs
index 8cf5f1d..f2272d3 100644
--- a/src/Lucene.Net.Tests/core/Util/TestMathUtil.cs
+++ b/src/Lucene.Net.Tests/core/Util/TestMathUtil.cs
@@ -1,12 +1,11 @@
-using System;
-using System.Diagnostics;
 using Lucene.Net.Randomized.Generators;
 using Lucene.Net.Support;
 using NUnit.Framework;
+using System;
+using System.Linq;
 
 namespace Lucene.Net.Util
 {
-
     /*
      * Licensed to the Apache Software Foundation (ASF) under one or more
      * contributor license agreements.  See the NOTICE file distributed with
@@ -60,11 +59,16 @@ namespace Lucene.Net.Util
         }
 
         // slow version used for testing
-        internal static long Gcd(long l1, long l2)
+        private static bool TryGetGcd(long a, long b, out long result)
         {
-            System.Numerics.BigInteger gcd = System.Numerics.BigInteger.ValueOf(l1).gcd(System.Numerics.BigInteger.ValueOf(l2));
-            Debug.Assert(gcd.BitCount() <= 64);
-            return (long)gcd;
+            result = 0;
+            var c = System.Numerics.BigInteger.GreatestCommonDivisor(a, b);
+            if (c <= long.MaxValue && c >= long.MinValue)
+            {
+                result = (long)c;
+                return true;
+            }
+            return false; // would overflow
         }
 
         [Test]
@@ -76,12 +80,20 @@ namespace Lucene.Net.Util
                 long l1 = RandomLong();
                 long l2 = RandomLong();
                 long gcd = MathUtil.Gcd(l1, l2);
-                long actualGcd = Gcd(l1, l2);
-                Assert.AreEqual(actualGcd, gcd);
-                if (gcd != 0)
+                long actualGcd;
+                if (TryGetGcd(l1, l2, out actualGcd))
+                {
+                    Assert.AreEqual(actualGcd, gcd);
+                    if (gcd != 0)
+                    {
+                        Assert.AreEqual(l1, (l1 / gcd) * gcd);
+                        Assert.AreEqual(l2, (l2 / gcd) * gcd);
+                    }
+                }
+                else
                 {
-                    Assert.AreEqual(l1, (l1 / gcd) * gcd);
-                    Assert.AreEqual(l2, (l2 / gcd) * gcd);
+                    // GCD cast to long would fail, try again
+                    i--;
                 }
             }
         }