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 Hughes 2681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes/** 2781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Class: java.math.BigInteger 2881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Method: add 2981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespublic class BigIntegerAddTest extends TestCase { 3181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 3281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two positive numbers of the same length 3381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase1() { 3581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; 3681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 3781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 3881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 3981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {11, 22, 33, 44, 55, 66, 77, 11, 22, 33}; 4081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 4181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 4281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 4381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 4481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 4581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 4681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 4781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 4881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 4981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 5081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 5181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 5281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two negative numbers of the same length 5381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 5481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase2() { 5581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3}; 5681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 5781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 5881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 5981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-12, -23, -34, -45, -56, -67, -78, -12, -23, -33}; 6081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 6181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 6281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 6381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 6481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 6581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 6681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 6781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 6881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 6981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 7081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 7181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 7281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of the same length. 7381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is positive and the second is negative. 7481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is greater in absolute value. 7581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 7681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase3() { 7781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {3, 4, 5, 6, 7, 8, 9}; 7881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; 7981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {2, 2, 2, 2, 2, 2, 2}; 8081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 8181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 8281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 8381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 8481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 8581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 8681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 8781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 8881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 8981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 9081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 9181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 9281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 9381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 9481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of the same length. 9581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is negative and the second is positive. 9681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is greater in absolute value. 9781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 9881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase4() { 9981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {3, 4, 5, 6, 7, 8, 9}; 10081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; 10181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-3, -3, -3, -3, -3, -3, -2}; 10281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 10381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 10481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 10581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 10681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 10781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 10881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 10981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 11081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 11181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 11281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 11381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 11481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 11581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 11681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of the same length. 11781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is positive and the second is negative. 11881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is less in absolute value. 11981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 12081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase5() { 12181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 12281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {3, 4, 5, 6, 7, 8, 9}; 12381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-3, -3, -3, -3, -3, -3, -2}; 12481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 12581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 12681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 12781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 12881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 12981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 13081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 13181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 13281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 13381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 13481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 13581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 13681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 13781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 13881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of the same length. 13981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is negative and the second is positive. 14081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first one is less in absolute value. 14181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 14281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase6() { 14381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 14481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {3, 4, 5, 6, 7, 8, 9}; 14581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {2, 2, 2, 2, 2, 2, 2}; 14681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 14781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 14881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 14981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 15081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 15181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 15281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 15381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 15481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 15581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 15681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 15781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 15881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 15981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 16081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two positive numbers of different length. 16181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is longer. 16281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 16381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase7() { 16481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 16581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 16681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 16781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 16881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; 16981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 17081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 17181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 17281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 17381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 17481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 17581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 17681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 17781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 17881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 17981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 18081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 18181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two positive numbers of different length. 18281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is longer. 18381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 18481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase8() { 18581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 18681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 18781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 15, 26, 37, 41, 52, 63, 74, 15, 26, 37}; 18881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aBytes); 18981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bBytes); 19081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 19181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 19281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 19381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 19481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 19581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 19681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 19781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 19881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 19981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 20081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two negative numbers of different length. 20181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is longer. 20281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 20381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase9() { 20481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 20581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 20681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 20781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 20881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; 20981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 21081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 21181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 21281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 21381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 21481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 21581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 21681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 21781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 21881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 21981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 22081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 22181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two negative numbers of different length. 22281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is longer. 22381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 22481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase10() { 22581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 22681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 22781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 22881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 22981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-2, -3, -4, -5, -16, -27, -38, -42, -53, -64, -75, -16, -27, -37}; 23081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 23181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 23281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 23381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 23481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 23581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 23681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 23781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 23881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 23981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 24081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 24181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 24281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of different length and sign. 24381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is positive. 24481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is longer. 24581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 24681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase11() { 24781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 24881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 24981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 25081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 25181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; 25281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 25381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 25481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 25581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 25681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 25781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 25881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 25981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 26081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 26181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 26281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 26381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 26481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of different length and sign. 26581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is positive. 26681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is longer. 26781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 26881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase12() { 26981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 27081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 27181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 27281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 27381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; 27481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 27581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 27681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 27781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 27881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 27981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 28081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 28181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 28281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 28381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 28481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 28581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 28681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of different length and sign. 28781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is negative. 28881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is longer. 28981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 29081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase13() { 29181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 29281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 29381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 29481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 29581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23}; 29681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 29781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 29881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 29981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 30081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.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, result.signum()); 30581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 30681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 30781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 30881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers of different length and sign. 30981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is negative. 31081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is longer. 31181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 31281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase14() { 31381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 31481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 4, 5, 6, 7}; 31581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 31681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 31781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23}; 31881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 31981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 32081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 32181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 32281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 32381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 32481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 32581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 32681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 32781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 32881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 32981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 33081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two equal numbers of different signs 33181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 33281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase15() { 33381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 33481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; 33581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 33681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 33781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 33881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 33981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 34081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 34181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 34281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 34381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 34481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 34581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 34681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 34781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 34881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 34981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add zero to a number 35081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 35181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase16() { 35281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 35381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {0}; 35481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 35581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 35681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 35781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 35881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 35981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 36081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 36181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 36281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 36381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 36481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 36581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 36681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 36781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 36881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 36981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add a number to zero 37081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 37181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase17() { 37281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {0}; 37381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; 37481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 37581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 37681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 37781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 37881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 37981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 38081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 38181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 38281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 38381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 38481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 38581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 38681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 38781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 38881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 38981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add zero to zero 39081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 39181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase18() { 39281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {0}; 39381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {0}; 39481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 39581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 39681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 39781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 39881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 39981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 40081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 40181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 40281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 40381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 40481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 40581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 40681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 40781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 40881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 40981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add ZERO to a number 41081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 41181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase19() { 41281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 41381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 41481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 41581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 41681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ZERO; 41781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 41881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 41981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 42081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 42181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 42281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 42381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 42481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 42581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 42681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 42781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add a number to zero 42881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 42981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase20() { 43081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 2, 3, 4, 5, 6, 7}; 43181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 43281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 43381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = BigInteger.ZERO; 43481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 43581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 43681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 43781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 43881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 43981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 44081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 44181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 44281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 44381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 44481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 44581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add ZERO to ZERO 44681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 44781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase21() { 44881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 44981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = BigInteger.ZERO; 45081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ZERO; 45181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 45281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 45381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 45481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 45581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 45681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 45781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 45881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 45981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 46081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 46181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add ONE to ONE 46281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 46381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase22() { 46481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {2}; 46581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = BigInteger.ONE; 46681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ONE; 46781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 46881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 46981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 47081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 47181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 47281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 47381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 47481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 47581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 47681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 47781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Add two numbers so that carry is 1 47881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 47981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase23() { 48081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1}; 48181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1}; 48281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 48381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 48481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 0, 0, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, -1, -2}; 48581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 48681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 48781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.add(bNumber); 48881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 48981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 49081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 49181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 49281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 49381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 49481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 49581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes} 496