181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/* 281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Licensed to the Apache Software Foundation (ASF) under one or more 381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * contributor license agreements. See the NOTICE file distributed with 481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * this work for additional information regarding copyright ownership. 581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The ASF licenses this file to You under the Apache License, Version 2.0 681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * (the "License"); you may not use this file except in compliance with 781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * the License. You may obtain a copy of the License at 881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * 981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * http://www.apache.org/licenses/LICENSE-2.0 1081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * 1181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Unless required by applicable law or agreed to in writing, software 1281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS, 1381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * See the License for the specific language governing permissions and 1581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * limitations under the License. 1681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 1781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/** 1881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * @author Elena Semukhina 1981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 2081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 2181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespackage org.apache.harmony.tests.java.math; 2281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 2381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughesimport junit.framework.TestCase; 2481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughesimport java.math.BigInteger; 2581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughesimport java.util.Random; 2681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 2781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/** 2881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Class: java.math.BigInteger 2981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Constructors: BigInteger(byte[] a), BigInteger(int sign, byte[] a), 3081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * BigInteger(String val, int radix) 3181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespublic class BigIntegerConstructorsTest extends TestCase { 3381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 3481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from an array of bytes. 3581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if an array is zero bytes long 3681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesException() { 3881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {}; 3981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 4081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(aBytes); 4181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 4281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 4381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 4481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 4581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 4681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 4781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from an array of bytes. 4881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 4981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 5081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesPositive1() { 5181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 5281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 5381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 5481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 5581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 5681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 5781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 5881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 5981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 6081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 6181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 6281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 6381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from an array of bytes. 6481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an integer. 6581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 6681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesPositive2() { 6781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100}; 6881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, 56, 100}; 6981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 7081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 7181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 7281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 7381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 7481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 7581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 7681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 7781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 7881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 7981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from an array of bytes. 8081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 8181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 8281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesPositive3() { 8381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {127, 56, 100, -1}; 8481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {127, 56, 100, -1}; 8581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 8681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 8781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 8881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 8981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 9081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 9181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 9281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 9381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 9481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 9581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from an array of bytes. 9681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 9781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 9881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesPositive() { 9981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {127, 56, 100, -1, 14, 75, -24, -100}; 10081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {127, 56, 100, -1, 14, 75, -24, -100}; 10181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 10281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 10381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 10481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 10581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 10681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 10781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 10881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 10981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 11081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 11181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from an array of bytes. 11281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 11381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 11481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesNegative1() { 11581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 11681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 11781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 11881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 11981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 12081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 12181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 12281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 12381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 12481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 12581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 12681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 12781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from an array of bytes. 12881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an integer. 12981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 13081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesNegative2() { 13181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100}; 13281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-12, 56, 100}; 13381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 13481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 13581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 13681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 13781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 13881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 13981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 14081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 14181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 14281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 14381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from an array of bytes. 14481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 14581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 14681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesNegative3() { 14781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-128, -12, 56, 100}; 14881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-128, -12, 56, 100}; 14981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 15081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 15181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 15281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 15381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 15481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 15581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 15681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 15781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 15881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 15981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from an array of bytes. 16081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 16181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 16281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesNegative4() { 16381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78}; 16481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-128, -12, 56, 100, -13, 56, 93, -78}; 16581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 16681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 16781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 16881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 16981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 17081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 17181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 17281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 17381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 17481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 17581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from an array of zero bytes. 17681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 17781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorBytesZero() { 17881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {0, 0, 0, -0, +0, 0, -0}; 17981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 18081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 18181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 18281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 18381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 18481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 18581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 18681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 18781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 18881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 18981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 19081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a sign and an array of bytes. 19181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if a sign has improper value. 19281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 19381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesException1() { 19481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {123, 45, -3, -76}; 19581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 3; 19681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 19781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(aSign, aBytes); 19881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 19981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 20081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 20181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 20281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 20381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 20481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a sign and an array of bytes. 20581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if the array contains non-zero bytes while the sign is 0. 20681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 20781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesException2() { 20881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {123, 45, -3, -76}; 20981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 0; 21081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 21181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(aSign, aBytes); 21281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 21381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 21481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("Improper exception message", "signum-magnitude mismatch", e.getMessage()); 21581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 21681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 21781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 21881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 21981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 22081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 22181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 22281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 22381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive1() { 22481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 22581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 22681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 22781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 22881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 22981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 23081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 23181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 23281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 23381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 23481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 23581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 23681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 23781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 23881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 23981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 24081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 24181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive2() { 24281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 24381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 24481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 24581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 24681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 24781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 24881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 24981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 25081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 25181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 25281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 25381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 25481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 25581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 25681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an integer. 25781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 25881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive3() { 25981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100}; 26081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 26181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -12, 56, 100}; 26281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 26381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 26481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 26581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 26681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 26781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 26881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 26981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 27081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 27181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 27281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 27381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 27481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 27581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 27681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive4() { 27781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {127, 56, 100, -2}; 27881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 27981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {127, 56, 100, -2}; 28081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 28181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 28281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 28381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 28481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 28581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 28681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 28781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 28881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 28981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 29081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 29181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 29281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 29381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 29481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive5() { 29581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 56, 100, -2}; 29681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 29781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -127, 56, 100, -2}; 29881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 29981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 30081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 30181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 30281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 30381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 30481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 30581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 30681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 30781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 30881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 30981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 31081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 31181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 31281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive6() { 31381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 31481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 31581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 31681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 31781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 31881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 31981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 32081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 32181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 32281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 32381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 32481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 32581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 32681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a sign and an array of bytes. 32781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 32881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 32981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 33081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesPositive7() { 33181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 33281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 33381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 33481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 33581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 33681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 33781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 33881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 33981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 34081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 34181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 34281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 34381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 34481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 34581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 34681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 34781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 34881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative1() { 34981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 35081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 35181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 15}; 35281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 35381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 35481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 35581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 35681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 35781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 35881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 35981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 36081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 36181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 36281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 36381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an array of integers. 36481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 36581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 36681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative2() { 36781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15}; 36881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 36981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 15}; 37081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 37181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 37281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 37381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 37481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 37581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 37681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 37781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 37881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 37981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 38081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 38181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number fits in an integer. 38281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 38381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative3() { 38481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100}; 38581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 38681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-1, 11, -57, -100}; 38781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 38881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 38981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 39081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 39181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 39281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 39381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 39481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 39581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 39681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 39781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 39881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 39981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 40081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 40181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative4() { 40281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {127, 56, 100, -2}; 40381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 40481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-128, -57, -101, 2}; 40581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 40681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 40781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 40881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 40981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 41081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 41181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 41281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 41381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 41481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 41581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 41681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is 4. 41781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 41881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 41981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative5() { 42081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 56, 100, -2}; 42181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 42281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-1, 126, -57, -101, 2}; 42381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 42481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 42581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 42681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 42781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 42881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 42981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 43081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 43181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 43281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 43381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 43481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 43581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is positive. 43681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 43781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative6() { 43881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 43981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 44081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101}; 44181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 44281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 44381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 44481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 44581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 44681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 44781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 44881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 44981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 45081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 45181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a sign and an array of bytes. 45281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The number of bytes is multiple of 4. 45381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The most significant byte is negative. 45481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 45581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesNegative7() { 45681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 23, -101}; 45781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 45881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-1, 11, -57, -101, 1, 75, -90, -46, -92, -4, 14, -24, 101}; 45981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 46081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 46181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 46281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 46381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 46481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 46581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 46681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 46781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 46881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 46981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero bytes. 47081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is -1. 47181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 47281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZero1() { 47381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000}; 47481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 47581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 47681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 47781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 47881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 47981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 48081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 48181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 48281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 48381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 48481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 48581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 48681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero bytes. 48781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is 0. 48881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 48981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZero2() { 49081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000}; 49181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 0; 49281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 49381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 49481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 49581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 49681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 49781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 49881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 49981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 50081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 50181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 50281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 50381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero bytes. 50481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is 1. 50581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 50681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZero3() { 50781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-0, 0, +0, 0, 0, 00, 000}; 50881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 50981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 51081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 51181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 51281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 51381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 51481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 51581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 51681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 51781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 51881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 51981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 52081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero length. 52181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is -1. 52281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 52381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZeroNull1() { 52481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {}; 52581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 52681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 52781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 52881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 52981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 53081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 53181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 53281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 53381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 53481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 53581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 53681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 53781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero length. 53881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is 0. 53981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 54081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZeroNull2() { 54181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {}; 54281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 0; 54381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 54481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 54581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 54681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 54781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 54881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 54981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 55081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 55181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 55281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 55381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 55481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a sign and an array of zero length. 55581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The sign is 1. 55681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 55781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorSignBytesZeroNull3() { 55881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {}; 55981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 56081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 56181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 56281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 56381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 56481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 56581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 56681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 56781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 56881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 56981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 57081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 57181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a string value and radix. 57281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if a radix is out of range 57381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 57481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringException1() { 57581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "9234853876401"; 57681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 45; 57781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 57881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(value, radix); 57981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 58081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 58181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 58281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 58381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 58481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 58581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a string value and radix. 58681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if the string starts with a space. 58781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 58881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringException2() { 58981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = " 9234853876401"; 59081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 10; 59181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 59281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(value, radix); 59381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 59481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 59581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 59681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 59781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 59881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 59981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a string value and radix. 60081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if the string contains improper characters. 60181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 60281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringException3() { 60381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "92348$*#78987"; 60481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 34; 60581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 60681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(value, radix); 60781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 60881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 60981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 61081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 61181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 61281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 61381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a number from a string value and radix. 61481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verify an exception thrown if some digits are greater than radix. 61581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 61681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringException4() { 61781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "98zv765hdsaiy"; 61881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 20; 61981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 62081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes new BigInteger(value, radix); 62181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("NumberFormatException has not been caught"); 62281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (NumberFormatException e) { 62381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 62481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 62581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 62681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 62781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a string value and radix 2. 62881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 62981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix2() { 63081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "10101010101010101"; 63181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 2; 63281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 85, 85}; 63381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 63481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 63581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 63681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 63781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 63881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 63981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 64081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 64181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 64281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 64381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a string value and radix 8. 64481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 64581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix8() { 64681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "76356237071623450"; 64781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 8; 64881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {7, -50, -28, -8, -25, 39, 40}; 64981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 65081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 65181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 65281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 65381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 65481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 65581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 65681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 65781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 65881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 65981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a string value and radix 10. 66081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 66181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix10() { 66281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "987328901348934898"; 66381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 10; 66481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {13, -77, -78, 103, -103, 97, 68, -14}; 66581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 66681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 66781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 66881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 66981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 67081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 67181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 67281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 67381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 67481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 67581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a string value and radix 16. 67681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 67781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix16() { 67881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "fe2340a8b5ce790"; 67981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 16; 68081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {15, -30, 52, 10, -117, 92, -25, -112}; 68181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 68281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 68381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 68481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 68581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 68681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 68781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 68881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 68981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 69081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 69181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a positive number from a string value and radix 36. 69281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 69381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix36() { 69481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "skdjgocvhdjfkl20jndjkf347ejg457"; 69581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 36; 69681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -12, -116, 112, -105, 12, -36, 66, 108, 66, -20, -37, -15, 108, -7, 52, -99, -109, -8, -45, -5}; 69781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 69881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 69981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 70081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 70181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 70281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 70381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, aNumber.signum()); 70481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 70581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 70681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 70781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a negative number from a string value and radix 10. 70881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 70981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix10Negative() { 71081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "-234871376037"; 71181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 36; 71281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-4, 48, 71, 62, -76, 93, -105, 13}; 71381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 71481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 71581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 71681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 71781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 71881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 71981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, aNumber.signum()); 72081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 72181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 72281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 72381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a zero number from a string value and radix 36. 72481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 72581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorStringRadix10Zero() { 72681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes String value = "-00000000000000"; 72781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int radix = 10; 72881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 72981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(value, radix); 73081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 73181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = aNumber.toByteArray(); 73281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 73381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 73481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 73581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, aNumber.signum()); 73681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 73781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 73881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 73981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Create a random number of 75 bits length. 74081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 74181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testConstructorRandom() { 74281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bitLen = 75; 74381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes Random rnd = new Random(); 74481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(bitLen, rnd); 74581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue("incorrect bitLength", aNumber.bitLength() <= bitLen); 74681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 74781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 748b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes public void testConstructorPrime() { 749b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes for (int rep = 0; rep < 2048; ++rep) { 750b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes Random rnd = new Random(); 751b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes BigInteger b; 752b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes int bits; 753b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 754b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes // Create a 128-bit prime number. 755b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 128; 756b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, 10, rnd); 757b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertEquals(b.toString(), bits, b.bitLength()); 758b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 759b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes // Create a prime number of 25 bits length. 760b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 25; 761b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, 10, rnd); 762b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertEquals(b.toString(), bits, b.bitLength()); 763b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 764b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes // Create a prime number of 18 bits length. 765b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 18; 766b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, 10, rnd); 767b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertEquals(b.toString(), bits, b.bitLength()); 768b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 7693e0dd60f27f63195548bbc87669bd361fd1f1bc2Elliott Hughes // On Android, anything less than 16 bits used to be at least 16 bits 7703e0dd60f27f63195548bbc87669bd361fd1f1bc2Elliott Hughes // because that's how OpenSSL behaves, but we recently fixed this... 771b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 2; 772b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, 10, rnd); 7733e0dd60f27f63195548bbc87669bd361fd1f1bc2Elliott Hughes assertEquals(b.toString(), bits, b.bitLength()); 774b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 7753e0dd60f27f63195548bbc87669bd361fd1f1bc2Elliott Hughes // The 2-arg constructor has never used OpenSSL. 776b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 2; 777b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, rnd); 778b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertTrue(b.toString(), b.bitLength() <= bits); 779b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertTrue(b.toString(), b.intValue() <= 3); 780b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes 781b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes bits = 16; 782b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes b = new BigInteger(bits, rnd); 783b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes assertTrue(b.toString(), b.bitLength() <= bits); 784b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes } 785b5ccf74bdea2d417d9e4820945fe3ba1f636a0d2Elliott Hughes } 78681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes} 787