harmony-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ge...@apache.org
Subject svn commit: r387239 [18/21] - in /incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math: ./ Harmony/ doc/ doc/images/ make/ src/ src/common/ src/common/javasrc/ src/common/javasrc/java/ src/common/javasrc/java/applet/ src/common/javasrc/ja...
Date Mon, 20 Mar 2006 16:31:33 GMT
Added: incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConstructorsTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConstructorsTest.java?rev=387239&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConstructorsTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConstructorsTest.java Mon Mar 20 08:31:09 2006
@@ -0,0 +1,772 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/**
+ * @author Elena Semukhina
+ * @version $Revision: 1.6.2.3 $
+ */
+
+package java.math;
+
+import junit.framework.TestCase;
+import java.math.BigInteger;
+import java.util.Random;
+
+/**
+ * Class:   java.math.BigInteger
+ * Constructors: BigInteger(byte[] a), BigInteger(int sign, byte[] a), 
+ *               BigInteger(String val, int radix)
+ */
+public class BigIntegerConstructorsTest extends TestCase {
+    /**
+     * Create a number from an array of bytes.
+     * Verify an exception thrown if an array is zero bytes long
+     */
+    public void testConstructorBytesException() {
+        byte aBytes[] = {};
+        try {
+            BigInteger aNumber = new BigInteger(aBytes);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("zero length array"));
+        }
+    }
+    
+    /**
+     * Create a positive number from an array of bytes.
+     * The number fits in an array of integers.
+     */
+    public void testConstructorBytesPositive1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
+        byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from an array of bytes.
+     * The number fits in an integer.
+     */
+    public void testConstructorBytesPositive2() {
+        byte aBytes[] = {12, 56, 100};
+        byte rBytes[] = {12, 56, 100};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from an array of bytes.
+     * The number of bytes is 4.
+     */
+    public void testConstructorBytesPositive3() {
+        byte aBytes[] = {127, 56, 100, -1};
+        byte rBytes[] = {127, 56, 100, -1};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from an array of bytes.
+     * The number of bytes is multiple of 4.
+     */
+    public void testConstructorBytesPositive() {
+        byte aBytes[] = {127, 56, 100, -1, 14, 75, -24, -100};
+        byte rBytes[] = {127, 56, 100, -1, 14, 75, -24, -100};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a negative number from an array of bytes.
+     * The number fits in an array of integers.
+     */
+    public void testConstructorBytesNegative1() {
+        byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
+        byte rBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from an array of bytes.
+     * The number fits in an integer.
+     */
+    public void testConstructorBytesNegative2() {
+        byte aBytes[] = {-12, 56, 100};
+        byte rBytes[] = {-12, 56, 100};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from an array of bytes.
+     * The number of bytes is 4.
+     */
+    public void testConstructorBytesNegative3() {
+        byte aBytes[] = {-128, -12, 56, 100};
+        byte rBytes[] = {-128, -12, 56, 100};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+    
+    /**
+     * Create a negative number from an array of bytes.
+     * The number of bytes is multiple of 4.
+     */
+    public void testConstructorBytesNegative4() {
+        byte aBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78};
+        byte rBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a zero number from an array of zero bytes.
+     */
+    public void testConstructorBytesZero() {
+        byte aBytes[] = {0, 0, 0, -0, +0, 0, -0};
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a number from a sign and an array of bytes.
+     * Verify an exception thrown if a sign has improper value.
+     */
+    public void testConstructorSignBytesException1() {
+        byte aBytes[] = {123, 45, -3, -76};
+        int aSign = 3;
+        try {
+            BigInteger aNumber = new BigInteger(aSign, aBytes);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("sign must be -1, 0, or 1"));
+        }
+    }
+    
+    /**
+     * Create a number from a sign and an array of bytes.
+     * Verify an exception thrown if the array contains non-zero bytes while the sign is 0. 
+     */
+    public void testConstructorSignBytesException2() {
+        byte aBytes[] = {123, 45, -3, -76};
+        int aSign = 0;
+        try {
+            BigInteger aNumber = new BigInteger(aSign, aBytes);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("zero sign with non-zero magnitude"));
+        }
+    }
+
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number fits in an array of integers.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesPositive1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        int aSign = 1;
+        byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+    
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number fits in an array of integers.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesPositive2() {
+        byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        int aSign = 1;
+        byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number fits in an integer.
+     */
+    public void testConstructorSignBytesPositive3() {
+        byte aBytes[] = {-12, 56, 100};
+        int aSign = 1;
+        byte rBytes[] = {0, -12, 56, 100};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number of bytes is 4.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesPositive4() {
+        byte aBytes[] = {127, 56, 100, -2};
+        int aSign = 1;
+        byte rBytes[] = {127, 56, 100, -2};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number of bytes is 4.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesPositive5() {
+        byte aBytes[] = {-127, 56, 100, -2};
+        int aSign = 1;
+        byte rBytes[] = {0, -127, 56, 100, -2};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+    
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number of bytes is multiple of 4.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesPositive6() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        int aSign = 1;
+        byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a sign and an array of bytes.
+     * The number of bytes is multiple of 4.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesPositive7() {
+        byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        int aSign = 1;
+        byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+    
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number fits in an array of integers.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesNegative1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        int aSign = -1;
+        byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 15};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+    
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number fits in an array of integers.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesNegative2() {
+        byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15};
+        int aSign = -1;
+        byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 15};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number fits in an integer.
+     */
+    public void testConstructorSignBytesNegative3() {
+        byte aBytes[] = {-12, 56, 100};
+        int aSign = -1;
+        byte rBytes[] = {-1, 11, -57, -100};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number of bytes is 4.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesNegative4() {
+        byte aBytes[] = {127, 56, 100, -2};
+        int aSign = -1;
+        byte rBytes[] = {-128, -57, -101, 2};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number of bytes is 4.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesNegative5() {
+        byte aBytes[] = {-127, 56, 100, -2};
+        int aSign = -1;
+        byte rBytes[] = {-1, 126, -57, -101, 2};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+    
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number of bytes is multiple of 4.
+     * The most significant byte is positive.
+     */
+    public void testConstructorSignBytesNegative6() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        int aSign = -1;
+        byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a negative number from a sign and an array of bytes.
+     * The number of bytes is multiple of 4.
+     * The most significant byte is negative.
+     */
+    public void testConstructorSignBytesNegative7() {
+        byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101};
+        int aSign = -1;
+        byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero bytes.
+     * The sign is -1.
+     */
+    public void testConstructorSignBytesZero1() {
+        byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
+        int aSign = -1;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero bytes.
+     * The sign is 0.
+     */
+    public void testConstructorSignBytesZero2() {
+        byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
+        int aSign = 0;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero bytes.
+     * The sign is 1.
+     */
+    public void testConstructorSignBytesZero3() {
+        byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000};
+        int aSign = 1;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero length.
+     * The sign is -1.
+     */
+    public void testConstructorSignBytesZeroNull1() {
+        byte aBytes[] = {};
+        int aSign = -1;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero length.
+     * The sign is 0.
+     */
+    public void testConstructorSignBytesZeroNull2() {
+        byte aBytes[] = {};
+        int aSign = 0;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a zero number from a sign and an array of zero length.
+     * The sign is 1.
+     */
+    public void testConstructorSignBytesZeroNull3() {
+        byte aBytes[] = {};
+        int aSign = 1;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a number from a string value and radix.
+     * Verify an exception thrown if a radix is out of range
+     */
+    public void testConstructorStringException1() {
+        String value = "9234853876401";
+        int radix = 45;
+        try {
+            BigInteger aNumber = new BigInteger(value, radix);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("radix is out of range"));
+        }
+    }
+    
+    /**
+     * Create a number from a string value and radix.
+     * Verify an exception thrown if the string starts with a space.
+     */
+    public void testConstructorStringException2() {
+        String value = "   9234853876401";
+        int radix = 10;
+        try {
+            BigInteger aNumber = new BigInteger(value, radix);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+        }
+    }
+    
+    /**
+     * Create a number from a string value and radix.
+     * Verify an exception thrown if the string contains improper characters.
+     */
+    public void testConstructorStringException3() {
+        String value = "92348$*#78987";
+        int radix = 34;
+        try {
+            BigInteger aNumber = new BigInteger(value, radix);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+        }
+    }
+    
+    /**
+     * Create a number from a string value and radix.
+     * Verify an exception thrown if some digits are greater than radix.
+     */
+    public void testConstructorStringException4() {
+        String value = "98zv765hdsaiy";
+        int radix = 20;
+        try {
+            BigInteger aNumber = new BigInteger(value, radix);
+            fail("NumberFormatException has not been caught");
+        } catch (NumberFormatException e) {
+        }
+    }
+
+    /**
+     * Create a positive number from a string value and radix 2.
+     */
+    public void testConstructorStringRadix2() {
+        String value = "10101010101010101";
+        int radix = 2;
+        byte rBytes[] = {1, 85, 85};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+    
+    /**
+     * Create a positive number from a string value and radix 8.
+     */
+    public void testConstructorStringRadix8() {
+        String value = "76356237071623450";
+        int radix = 8;
+        byte rBytes[] = {7, -50, -28, -8, -25, 39, 40};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a string value and radix 10.
+     */
+    public void testConstructorStringRadix10() {
+        String value = "987328901348934898";
+        int radix = 10;
+        byte rBytes[] = {13, -77, -78, 103, -103, 97, 68, -14};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a string value and radix 16.
+     */
+    public void testConstructorStringRadix16() {
+        String value = "fe2340a8b5ce790";
+        int radix = 16;
+        byte rBytes[] = {15, -30, 52, 10, -117, 92, -25, -112};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a positive number from a string value and radix 36.
+     */
+    public void testConstructorStringRadix36() {
+        String value = "skdjgocvhdjfkl20jndjkf347ejg457";
+        int radix = 36;
+        byte rBytes[] = {0, -12, -116, 112, -105, 12, -36, 66, 108, 66, -20, -37, -15, 108, -7, 52, -99, -109, -8, -45, -5};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * Create a negative number from a string value and radix 10.
+     */
+    public void testConstructorStringRadix10Negative() {
+        String value = "-234871376037";
+        int radix = 36;
+        byte rBytes[] = {-4, 48, 71, 62, -76, 93, -105, 13};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * Create a zero number from a string value and radix 36.
+     */
+    public void testConstructorStringRadix10Zero() {
+        String value = "-00000000000000";
+        int radix = 10;
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(value, radix);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+
+    /**
+     * Create a random number of 75 bits length.
+     */
+    public void testConstructorRandom() {
+        int bitLen = 75;
+        Random rnd = new Random();
+        BigInteger aNumber = new BigInteger(bitLen, rnd);
+        assertTrue("incorrect bitLength", aNumber.bitLength() <= bitLen);
+    }
+
+    /**
+     * Create a prime number of 25 bits length.
+     */
+    public void testConstructorPrime() {
+        int bitLen = 25;
+        Random rnd = new Random();
+        BigInteger aNumber = new BigInteger(bitLen, 80, rnd);
+        assertTrue("incorrect bitLength", aNumber.bitLength() == bitLen);
+    }
+
+    /**
+     * Create a prime number of 2 bits length.
+     */
+    public void testConstructorPrime2() {
+        int bitLen = 2;
+        Random rnd = new Random();
+        BigInteger aNumber = new BigInteger(bitLen, 80, rnd);
+        assertTrue("incorrect bitLength", aNumber.bitLength() == bitLen);
+        int num = aNumber.intValue();
+        assertTrue("incorrect value", num == 2 || num == 3);
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConvertTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConvertTest.java?rev=387239&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConvertTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerConvertTest.java Mon Mar 20 08:31:09 2006
@@ -0,0 +1,792 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/**
+ * @author Elena Semukhina
+ * @version $Revision: 1.8.2.2 $
+ */
+
+package java.math;
+
+import junit.framework.TestCase;
+import java.math.BigInteger;
+
+/**
+ * Class:   java.math.BigInteger
+ * Methods: intValue, longValue, toByteArray(), valueOf(long val),
+ * floatValue(), doubleValue()
+ */
+public class BigIntegerConvertTest extends TestCase {
+    /**
+     * Return the double value of ZERO. 
+     */
+    public void testDoubleValueZero() {
+        String a = "0";
+        double result = 0.0;
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The number's length is less than 64 bits.
+     */
+    public void testDoubleValuePositive1() {
+        String a = "27467238945";
+        double result = 2.7467238945E10;
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The number's bit length is inside [63, 1024].
+     */
+    public void testDoubleValuePositive2() {
+        String a = "2746723894572364578265426346273456972";
+        double result = 2.7467238945723645E36;
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a negative number to a double value. 
+     * The number's bit length is less than 64 bits.
+     */
+    public void testDoubleValueNegative1() {
+        String a = "-27467238945";
+        double result = -2.7467238945E10;
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a negative number to a double value. 
+     * The number's bit length is inside [63, 1024].
+     */
+    public void testDoubleValueNegative2() {
+        String a = "-2746723894572364578265426346273456972";
+        double result = -2.7467238945723645E36;
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 1.
+     */
+    public void testDoubleValuePosRounded1() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = 1;
+        double result = 1.54747264387948E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a double value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 0
+     * but some of dropped bits are 1s.
+     */
+    public void testDoubleValuePosRounded2() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5};
+        int aSign = 1;
+        double result = 1.547472643879479E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+        /**
+     * Convert a positive number to a double value. 
+     * Rounding is NOT needed.
+     */
+    public void testDoubleValuePosNotRounded() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5};
+        int aSign = 1;
+        double result = 1.5474726438794828E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a double value. 
+     * Rounding is needed.
+     */
+    public void testDoubleValueNegRounded1() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = -1;
+        double result = -1.54747264387948E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a double value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 0
+     * but some of dropped bits are 1s.
+     */
+    public void testDoubleValueNegRounded2() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 36, 23, 1, -3, -5};
+        int aSign = -1;
+        double result = -1.547472643879479E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * Rounding is NOT needed.
+     */
+    public void testDoubleValueNegNotRounded() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, -128, 23, 1, -3, -5};
+        int aSign = -1;
+        double result = -1.5474726438794828E26;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a double value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The rounding bit is 0.
+     * The result is Double.MAX_VALUE.
+     */
+    public void testDoubleValuePosMaxValue() {
+        byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+           };
+        int aSign = 1;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == Double.MAX_VALUE);
+    }
+
+    /**
+     * Convert a negative number to a double value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The result is -Double.MAX_VALUE.
+     */
+    public void testDoubleValueNegMaxValue() {
+        byte[] a = {0, -1, -1, -1, -1, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
+            -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
+           };
+        int aSign = -1;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == -Double.MAX_VALUE);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The rounding bit is 1.
+     * The result is Double.POSITIVE_INFINITY.
+     */
+    public void testDoubleValuePositiveInfinity1() {
+        byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+           };
+        int aSign = 1;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == Double.POSITIVE_INFINITY);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The number's bit length is greater than 1024.
+     */
+    public void testDoubleValuePositiveInfinity2() {
+        String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == Double.POSITIVE_INFINITY);
+    }
+
+    /**
+     * Convert a negative number to a double value. 
+     * The number's bit length is greater than 1024.
+     */
+    public void testDoubleValueNegativeInfinity1() {
+        String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
+        double aNumber = new BigInteger(a).doubleValue();
+        assertTrue(aNumber == Double.NEGATIVE_INFINITY);
+    }
+
+    /**
+     * Convert a negative number to a double value. 
+     * The exponent is 1023 and the mantissa is all 0s.
+     * The rounding bit is 0.
+     * The result is Double.NEGATIVE_INFINITY.
+     */
+    public void testDoubleValueNegativeInfinity2() {
+        byte[] a = {-1, -1, -1, -1, -1, -1, -1, -8, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+           };
+        int aSign = -1;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == Double.NEGATIVE_INFINITY);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The exponent is 1023 and the mantissa is all 0s
+     * but the 54th bit (implicit) is 1.
+     */
+    public void testDoubleValuePosMantissaIsZero() {
+        byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+           };
+        int aSign = 1;
+        double result = 8.98846567431158E307;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The exponent is 1023 and the mantissa is all 0s
+     * but the 54th bit (implicit) is 1.
+     */
+    public void testDoubleValueNegMantissaIsZero() {
+        byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+               0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 
+           };
+        int aSign = -1;
+        double aNumber = new BigInteger(aSign, a).doubleValue();
+        assertTrue(aNumber == -8.98846567431158E307);
+    }
+
+    /**
+     * Return the float value of ZERO. 
+     */
+    public void testFloatValueZero() {
+        String a = "0";
+        float result = 0.0f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * The number's length is less than 32 bits.
+     */
+    public void testFloatValuePositive1() {
+        String a = "27467238";
+        float result = 2.7467238E7f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * The number's bit length is inside [32, 127].
+     */
+    public void testFloatValuePositive2() {
+        String a = "27467238945723645782";
+        float result = 2.7467239E19f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a negative number to a float value. 
+     * The number's bit length is less than 32 bits.
+     */
+    public void testFloatValueNegative1() {
+        String a = "-27467238";
+        float result = -2.7467238E7f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a negative number to a doufloatble value. 
+     * The number's bit length is inside [63, 1024].
+     */
+    public void testFloatValueNegative2() {
+        String a = "-27467238945723645782";
+        float result = -2.7467239E19f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 1.
+     */
+    public void testFloatValuePosRounded1() {
+        byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = 1;
+        float result = 1.5475195E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a float value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 0
+     * but some of dropped bits are 1s.
+     */
+    public void testFloatValuePosRounded2() {
+        byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = 1;
+        float result = 1.5474728E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+        /**
+     * Convert a positive number to a float value. 
+     * Rounding is NOT needed.
+     */
+    public void testFloatValuePosNotRounded() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = 1;
+        float result = 1.5474726E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a float value. 
+     * Rounding is needed.
+     */
+    public void testFloatValueNegRounded1() {
+        byte[] a = {-128, 1, -1, -4, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = -1;
+        float result = -1.5475195E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a float value. 
+     * Rounding is needed.
+     * The rounding bit is 1 and the next bit to the left is 0
+     * but some of dropped bits are 1s.
+     */
+    public void testFloatValueNegRounded2() {
+        byte[] a = {-128, 1, 2, -128, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = -1;
+        float result = -1.5474728E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * Rounding is NOT needed.
+     */
+    public void testFloatValueNegNotRounded() {
+        byte[] a = {-128, 1, 2, 3, 4, 5, 60, 23, 1, -3, -5};
+        int aSign = -1;
+        float result = -1.5474726E26f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a positive number to a float value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The rounding bit is 0.
+     * The result is Float.MAX_VALUE.
+     */
+    public void testFloatValuePosMaxValue() {
+        byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+        int aSign = 1;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == Float.MAX_VALUE);
+    }
+
+    /**
+     * Convert a negative number to a float value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The rounding bit is 0.
+     * The result is -Float.MAX_VALUE.
+     */
+    public void testFloatValueNegMaxValue() {
+        byte[] a = {0, -1, -1, -1, 0, -1, -1, -8, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+        int aSign = -1;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == -Float.MAX_VALUE);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * The exponent is 1023 and the mantissa is all 1s.
+     * The rounding bit is 1.
+     * The result is Float.POSITIVE_INFINITY.
+     */
+    public void testFloatValuePositiveInfinity1() {
+        byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+        int aSign = 1;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == Float.POSITIVE_INFINITY);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * The number's bit length is greater than 127.
+     */
+    public void testFloatValuePositiveInfinity2() {
+        String a = "2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == Float.POSITIVE_INFINITY);
+    }
+
+    /**
+     * Convert a negative number to a float value. 
+     * The number's bit length is greater than 127.
+     */
+    public void testFloatValueNegativeInfinity1() {
+        String a = "-2746723894572364578265426346273456972283746872364768676747462342342342342342342342323423423423423423426767456345745293762384756238475634563456845634568934568347586346578648576478568456457634875673845678456786587345873645767456834756745763457863485768475678465783456702897830296720476846578634576384567845678346573465786457863";
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == Float.NEGATIVE_INFINITY);
+    }
+
+    /**
+     * Convert a negative number to a float value. 
+     * The exponent is 1023 and the mantissa is all 0s.
+     * The rounding bit is 0.
+     * The result is Float.NEGATIVE_INFINITY.
+     */
+    public void testFloatValueNegativeInfinity2() {
+        byte[] a = {0, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
+        int aSign = -1;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == Float.NEGATIVE_INFINITY);
+    }
+
+    /**
+     * Convert a positive number to a float value. 
+     * The exponent is 1023 and the mantissa is all 0s
+     * but the 54th bit (implicit) is 1.
+     */
+    public void testFloatValuePosMantissaIsZero() {
+        byte[] a = {-128, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+        int aSign = 1;
+        float result = 1.7014118E38f;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive number to a double value. 
+     * The exponent is 1023 and the mantissa is all 0s
+     * but the 54th bit (implicit) is 1.
+     */
+    public void testFloatValueNegMantissaIsZero() {
+        byte[] a = {1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+        int aSign = -1;
+        float aNumber = new BigInteger(aSign, a).floatValue();
+        assertTrue(aNumber == Float.NEGATIVE_INFINITY);
+    }
+
+    /**
+     * Convert a negative number to a float value. 
+     * The number's bit length is less than 32 bits.
+     */
+    public void testFloatValueBug2482() {
+        String a = "2147483649";
+        float result = 2.14748365E9f;
+        float aNumber = new BigInteger(a).floatValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a positive BigInteger to an integer value. 
+     * The low digit is positive
+     */
+    public void testIntValuePositive1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3};
+        int resInt = 1496144643;
+        int aNumber = new BigInteger(aBytes).intValue();
+        assertTrue(aNumber == resInt);
+    }
+
+    /**
+     * Convert a positive BigInteger to an integer value. 
+     * The low digit is positive
+     */
+    public void testIntValuePositive2() {
+        byte aBytes[] = {12, 56, 100};
+        int resInt = 800868;
+        int aNumber = new BigInteger(aBytes).intValue();
+        assertTrue(aNumber == resInt);
+    }
+
+    /**
+     * Convert a positive BigInteger to an integer value. 
+     * The low digit is negative.
+     */
+    public void testIntValuePositive3() {
+        byte aBytes[] = {56, 13, 78, -12, -5, 56, 100};
+        int sign = 1;
+        int resInt = -184862620;
+        int aNumber = new BigInteger(sign, aBytes).intValue();
+        assertTrue(aNumber == resInt);
+    }
+
+    /**
+     * Convert a negative BigInteger to an integer value.
+     * The low digit is negative.
+     */
+    public void testIntValueNegative1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, -128, 45, 91, 3};
+        int sign = -1;
+        int resInt = 2144511229;
+        int aNumber = new BigInteger(sign, aBytes).intValue();
+        assertTrue(aNumber == resInt);
+    }
+
+    /**
+     * Convert a negative BigInteger to an integer value.
+     * The low digit is negative.
+     */
+    public void testIntValueNegative2() {
+        byte aBytes[] = {-12, 56, 100};
+        int result = -771996;
+        int aNumber = new BigInteger(aBytes).intValue();
+        assertTrue(aNumber == result);
+    }
+    
+    /**
+     * Convert a negative BigInteger to an integer value. 
+     * The low digit is positive.
+     */
+    public void testIntValueNegative3() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 127, 45, 91, 3};
+        int sign = -1;
+        int resInt = -2133678851;
+        int aNumber = new BigInteger(sign, aBytes).intValue();
+        assertTrue(aNumber == resInt);
+    }
+
+    /**
+     * Convert a BigInteger to a positive long value
+     * The BigInteger is longer than int.
+     */
+    public void testLongValuePositive1() {
+        byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, 120, -34, -12, 45, 98};
+        long result = 3268209772258930018L;
+        long aNumber = new BigInteger(aBytes).longValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a number to a positive long value
+     * The number fits in a long.
+     */
+    public void testLongValuePositive2() {
+        byte aBytes[] = {12, 56, 100, 18, -105, 34, -18, 45};
+        long result = 880563758158769709L;
+        long aNumber = new BigInteger(aBytes).longValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a number to a negative long value
+     * The BigInteger is longer than int.
+     */
+    public void testLongValueNegative1() {
+        byte aBytes[] = {12, -1, 100, -2, -76, -128, 45, 91, 3};
+        long result = -43630045168837885L;
+        long aNumber = new BigInteger(aBytes).longValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * Convert a number to a negative long value
+     * The number fits in a long.
+     */
+    public void testLongValueNegative2() {
+        byte aBytes[] = {-12, 56, 100, 45, -101, 45, 98};
+        long result = -3315696807498398L;
+        long aNumber = new BigInteger(aBytes).longValue();
+        assertTrue(aNumber == result);
+    }
+
+    /**
+     * valueOf (long val): convert Integer.MAX_VALUE to a BigInteger.
+     */
+    public void testValueOfIntegerMax() {
+        long longVal = Integer.MAX_VALUE;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {127, -1, -1, -1};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * valueOf (long val): convert Integer.MIN_VALUE to a BigInteger.
+     */
+    public void testValueOfIntegerMin() {
+        long longVal = Integer.MIN_VALUE;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {-128, 0, 0, 0};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * valueOf (long val): convert Long.MAX_VALUE to a BigInteger.
+     */
+    public void testValueOfLongMax() {
+        long longVal = Long.MAX_VALUE;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {127, -1, -1, -1, -1, -1, -1, -1};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * valueOf (long val): convert Long.MIN_VALUE to a BigInteger.
+     */
+    public void testValueOfLongMin() {
+        long longVal = Long.MIN_VALUE;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {-128, 0, 0, 0, 0, 0, 0, 0};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * valueOf (long val): convert a positive long value to a BigInteger.
+     */
+    public void testValueOfLongPositive1() {
+        long longVal = 268209772258930018L;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {3, -72, -33, 93, -24, -56, 45, 98};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+
+    /**
+     * valueOf (long val): convert a positive long value to a BigInteger.
+     * The long value fits in integer.
+     */
+    public void testValueOfLongPositive2() {
+        long longVal = 58930018L;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {3, -125, 51, 98};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 1);
+    }
+    
+    /**
+     * valueOf (long val): convert a negative long value to a BigInteger.
+     */
+    public void testValueOfLongNegative1() {
+        long longVal = -268209772258930018L;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {-4, 71, 32, -94, 23, 55, -46, -98};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+
+    /**
+     * valueOf (long val): convert a negative long value to a BigInteger.
+     * The long value fits in integer.
+     */
+    public void testValueOfLongNegative2() {
+        long longVal = -58930018L;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {-4, 124, -52, -98};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == -1);
+    }
+    /**
+     * valueOf (long val): convert a zero long value to a BigInteger.
+     */
+    public void testValueOfLongZero() {
+        long longVal = 0L;
+        BigInteger aNumber = BigInteger.valueOf(longVal);
+        byte rBytes[] = {0};
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = aNumber.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", aNumber.signum() == 0);
+    }
+}

Added: incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerDivideTest.java
URL: http://svn.apache.org/viewcvs/incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerDivideTest.java?rev=387239&view=auto
==============================================================================
--- incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerDivideTest.java (added)
+++ incubator/harmony/enhanced/classlib/trunk/modules/regex-beans-math/test/common/unit/java/math/BigIntegerDivideTest.java Mon Mar 20 08:31:09 2006
@@ -0,0 +1,670 @@
+/*
+ *  Copyright 2005 The Apache Software Foundation or its licensors, as applicable.
+ *
+ *  Licensed under the Apache License, Version 2.0 (the "License");
+ *  you may not use this file except in compliance with the License.
+ *  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing, software
+ *  distributed under the License is distributed on an "AS IS" BASIS,
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ *  See the License for the specific language governing permissions and
+ *  limitations under the License.
+ */
+/**
+ * @author Elena Semukhina
+ * @version $Revision: 1.7.2.3 $
+ */
+
+package java.math;
+
+import junit.framework.TestCase;
+import java.math.BigInteger;
+
+/**
+ * Class:   java.math.BigInteger
+ * Methods: divide, remainder, mod, and divideAndRemainder 
+ */
+public class BigIntegerDivideTest extends TestCase {
+    /**
+     * Divide by zero
+     */
+    public void testCase1() {
+        byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
+        byte bBytes[] = {0};
+        int aSign = 1;
+        int bSign = 0;        
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        try {
+            BigInteger result = aNumber.divide(bNumber);
+            fail("ArithmeticException has not been caught");
+        } catch (ArithmeticException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("division by zero"));
+        }
+    }
+
+    /**
+     * Divide by ZERO
+     */
+    public void testCase2() {
+        byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
+        int aSign = 1;
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = BigInteger.ZERO;
+        try {
+            BigInteger result = aNumber.divide(bNumber);
+            fail("ArithmeticException has not been caught");
+        } catch (ArithmeticException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("division by zero"));
+        }
+    }
+
+    /**
+     * Divide two equal positive numbers
+     */
+    public void testCase3() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {1};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide two equal in absolute value numbers of different signes.
+     */
+    public void testCase4() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[] = {-1};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Divide two numbers of different length and different signs.
+     * The second is longer.
+     */
+    public void testCase5() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 1, 2, 3, 4, 5};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 0);
+    }
+
+    /**
+     * Divide two positive numbers of the same length.
+     * The second is greater.
+     */
+    public void testCase6() {
+        byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127};
+        byte bBytes[] = {15, 100, 56, 7, 98, -1, 39, -128, 127};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 0);
+    }
+
+    /**
+     * Divide two positive numbers.
+     */
+    public void testCase7() {
+        byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9};
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {23, 115, 11, 78, 35, -11};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide a positive number by a negative one.
+     */
+    public void testCase8() {
+        byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9};
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {-24, -116, -12, -79, -36, 11};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Divide a negative number by a positive one.
+     */
+    public void testCase9() {
+        byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9};
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[] = {-24, -116, -12, -79, -36, 11};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Divide two negative numbers.
+     */
+    public void testCase10() {
+        byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9};
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = -1;
+        int bSign = -1;        
+        byte rBytes[] = {23, 115, 11, 78, 35, -11};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide zero by a negative number.
+     */
+    public void testCase11() {
+        byte aBytes[] = {0};
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = 0;
+        int bSign = -1;        
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 0);
+    }
+    
+    /**
+     * Divide ZERO by a negative number.
+     */
+    public void testCase12() {
+        byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int bSign = -1;        
+        byte rBytes[] = {0};
+        BigInteger aNumber = BigInteger.ZERO;
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 0);
+    }
+
+    /**
+     * Divide a positive number by ONE.
+     */
+    public void testCase13() {
+        byte aBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        int aSign = 1;        
+        byte rBytes[] = {15, 48, -29, 7, 98, -1, 39, -128};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = BigInteger.ONE;
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide ONE by ONE.
+     */
+    public void testCase14() {
+        byte rBytes[] = {1};
+        BigInteger aNumber = BigInteger.ONE;
+        BigInteger bNumber = BigInteger.ONE;
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+    
+    /**
+     * Verifies the case when borrow != 0 in the private divide method.
+     */
+    public void testDivisionKnuth1() {
+        byte aBytes[] = {-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7};
+        byte bBytes[] = {-3, -3, -3, -3};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {0, -5, -12, -33, -96, -36, -105, -56, 92, 15, 48, -109};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Verifies the case when the divisor is already normalized.
+     */
+    public void testDivisionKnuthIsNormalized() {
+        byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
+        byte bBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1};
+        int aSign = -1;
+        int bSign = -1;        
+        byte rBytes[] = {0, -9, -8, -7, -6, -5, -4, -3};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Verifies the case when the first digits of the dividend
+     * and divisor equal.
+     */
+    public void testDivisionKnuthFirstDigitsEqual() {
+        byte aBytes[] = {2, -3, -4, -5, -1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
+        byte bBytes[] = {2, -3, -4, -5, -1, -1, -1, -1};
+        int aSign = -1;
+        int bSign = -1;        
+        byte rBytes[] = {0, -1, -1, -1, -1, -2, -88, -60, 41};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide the number of one digit by the number of one digit 
+     */
+    public void testDivisionKnuthOneDigitByOneDigit() {
+        byte aBytes[] = {113, -83, 123, -5};
+        byte bBytes[] = {2, -3, -4, -5};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {-37};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Divide the number of multi digits by the number of one digit 
+     */
+    public void testDivisionKnuthMultiDigitsByOneDigit() {
+        byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29};
+        byte bBytes[] = {2, -3, -4, -5};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {-38, 2, 7, 30, 109, -43};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.divide(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Remainder of division by zero
+     */
+    public void testCase15() {
+        byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
+        byte bBytes[] = {0};
+        int aSign = 1;
+        int bSign = 0;        
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        try {
+            BigInteger result = aNumber.remainder(bNumber);
+            fail("ArithmeticException has not been caught");
+        } catch (ArithmeticException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("division by zero"));
+        }
+    }
+
+    /**
+     * Remainder of division of equal numbers
+     */
+    public void testCase16() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {0};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 0);
+    }
+
+    /**
+     * Remainder of division of two positive numbers
+     */
+    public void testCase17() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {12, -21, 73, 56, 27};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Remainder of division of two negative numbers
+     */
+    public void testCase18() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = -1;
+        int bSign = -1;        
+        byte rBytes[] = {-13, 20, -74, -57, -27};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Remainder of division of two numbers of different signs.
+     * The first is positive.
+     */
+    public void testCase19() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {12, -21, 73, 56, 27};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+    
+    /**
+     * Remainder of division of two numbers of different signs.
+     * The first is negative.
+     */
+    public void testCase20() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[] = {-13, 20, -74, -57, -27};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == -1);
+    }
+
+    /**
+     * Tests the step D6 from the Knuth algorithm
+     */
+    public void testRemainderKnuth1() {
+        byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1};
+        byte bBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {1, 2, 3, 4, 5, 6, 7, 7, 18, -89};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide the number of one digit by the number of one digit 
+     */
+    public void testRemainderKnuthOneDigitByOneDigit() {
+        byte aBytes[] = {113, -83, 123, -5};
+        byte bBytes[] = {2, -3, -4, -50};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {2, -9, -14, 53};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * Divide the number of multi digits by the number of one digit 
+     */
+    public void testRemainderKnuthMultiDigitsByOneDigit() {
+        byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29};
+        byte bBytes[] = {2, -3, -4, -50};
+        int aSign = 1;
+        int bSign = -1;        
+        byte rBytes[] = {2, -37, -60, 59};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.remainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * divideAndRemainder of two numbers of different signs.
+     * The first is negative.
+     */
+    public void testCase21() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[][] = {
+                {-5, 94, -115, -74, -85, 84},
+                {-13, 20, -74, -57, -27}
+        };
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result[] = aNumber.divideAndRemainder(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result[0].toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            if (resBytes[i] != rBytes[0][i]) {
+                fail("Incorrect quotation");
+            }
+        }
+        assertTrue(result[0].signum() == -1);
+        resBytes = result[1].toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            if (resBytes[i] != rBytes[1][i]) {
+                fail("Incorrect remainder");
+            }
+            assertTrue(result[1].signum() == -1);
+        }
+    }
+    
+    /**
+     * mod when modulus is negative
+     */
+    public void testCase22() {
+        byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
+        byte bBytes[] = {1, 30, 40, 56, -1, 45};
+        int aSign = 1;
+        int bSign = -1;        
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        try {
+            BigInteger result = aNumber.mod(bNumber);
+            fail("ArithmeticException has not been caught");
+        } catch (ArithmeticException e) {
+            assertTrue("Improper exception message", e.getMessage().equals("modulus is non-positive"));
+        }
+    }
+
+    /**
+     * mod when a divisor is positive
+     */
+    public void testCase23() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = 1;
+        int bSign = 1;        
+        byte rBytes[] = {12, -21, 73, 56, 27};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.mod(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+
+    /**
+     * mod when a divisor is negative
+     */
+    public void testCase24() {
+        byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75};
+        byte bBytes[] = {27, -15, 65, 39, 100};
+        int aSign = -1;
+        int bSign = 1;        
+        byte rBytes[] = {15, 5, -9, -17, 73};
+        BigInteger aNumber = new BigInteger(aSign, aBytes);
+        BigInteger bNumber = new BigInteger(bSign, bBytes);
+        BigInteger result = aNumber.mod(bNumber);
+        byte resBytes[] = new byte[rBytes.length];
+        resBytes = result.toByteArray();
+        for(int i = 0; i < resBytes.length; i++) {
+            assertTrue(resBytes[i] == rBytes[i]);
+        }
+        assertTrue("incorrect sign", result.signum() == 1);
+    }
+}



Mime
View raw message