harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From regi...@apache.org
Subject svn commit: r956455 - in /harmony/enhanced/java/trunk/classlib/modules/math/src: main/java/java/math/BigInteger.java test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java
Date Mon, 21 Jun 2010 06:47:22 GMT
Author: regisxu
Date: Mon Jun 21 04:24:35 2010
New Revision: 956455

URL: http://svn.apache.org/viewvc?rev=956455&view=rev
Log:
Simplify BigInteger.modPow, pow 0 should be always 1.

Modified:
    harmony/enhanced/java/trunk/classlib/modules/math/src/main/java/java/math/BigInteger.java
    harmony/enhanced/java/trunk/classlib/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java

Modified: harmony/enhanced/java/trunk/classlib/modules/math/src/main/java/java/math/BigInteger.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/math/src/main/java/java/math/BigInteger.java?rev=956455&r1=956454&r2=956455&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/math/src/main/java/java/math/BigInteger.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/math/src/main/java/java/math/BigInteger.java
Mon Jun 21 04:24:35 2010
@@ -1344,8 +1344,8 @@ public class BigInteger extends Number i
         if (m.isOne() | (exponent.sign > 0 & base.sign == 0)) {
             return BigInteger.ZERO;
         }
-        if (base.sign == 0 && exponent.sign == 0) {
-            return BigInteger.ONE;
+        if (exponent.sign == 0) {
+            return BigInteger.ONE.mod(m);
         }
         if (exponent.sign < 0) {
             base = modInverse(m);

Modified: harmony/enhanced/java/trunk/classlib/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java
URL: http://svn.apache.org/viewvc/harmony/enhanced/java/trunk/classlib/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java?rev=956455&r1=956454&r2=956455&view=diff
==============================================================================
--- harmony/enhanced/java/trunk/classlib/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java
(original)
+++ harmony/enhanced/java/trunk/classlib/modules/math/src/test/java/org/apache/harmony/tests/java/math/BigIntegerModPowTest.java
Mon Jun 21 04:24:35 2010
@@ -47,6 +47,13 @@ public class BigIntegerModPowTest extend
 		} catch (ArithmeticException e) {
 			assertEquals("Improper exception message", "BigInteger: modulus not positive", e.getMessage());
 		}
+
+        try {
+            BigInteger.ZERO.modPow(new BigInteger("-1"), new BigInteger("10"));
+            fail("ArithmeticException has not been caught");
+        } catch (ArithmeticException e) {
+            // expected
+        }
 	}
 
 	/**
@@ -95,6 +102,29 @@ public class BigIntegerModPowTest extend
 		assertEquals("incorrect sign", 1, result.signum());
 	}
 
+    public void testModPowZeroExp() {
+        BigInteger exp = new BigInteger("0");
+        BigInteger[] base = new BigInteger[] {new BigInteger("-1"), new BigInteger("0"),
new BigInteger("1")};
+        BigInteger[] mod = new BigInteger[] {new BigInteger("2"), new BigInteger("10"), new
BigInteger("2147483648")};
+
+        for (int i = 0; i < base.length; ++i) {
+            for (int j = 0; j < mod.length; ++j) {
+                assertEquals(base[i] + " modePow(" + exp + ", " + mod[j]
+                        + ") should be " + BigInteger.ONE, BigInteger.ONE,
+                        base[i].modPow(exp, mod[j]));
+            }
+        }
+
+        mod = new BigInteger[] {new BigInteger("1")};
+        for (int i = 0; i < base.length; ++i) {
+            for (int j = 0; j < mod.length; ++j) {
+                assertEquals(base[i] + " modePow(" + exp + ", " + mod[j]
+                        + ") should be " + BigInteger.ZERO, BigInteger.ZERO,
+                        base[i].modPow(exp, mod[j]));
+            }
+        }
+    }
+
 	/**
 	 * modInverse: non-positive modulus
 	 */



Mime
View raw message