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 * Methods: divide, remainder, mod, and divideAndRemainder 2981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughespublic class BigIntegerDivideTest extends TestCase { 3181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 3281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide by zero 3381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 3481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase1() { 3581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 3681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {0}; 3781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 3881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 0; 3981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 4081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 4181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 4281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes aNumber.divide(bNumber); 4381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("ArithmeticException has not been caught"); 4481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (ArithmeticException e) { 4581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 4681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 4781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 4881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 4981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide by ZERO 5081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 5181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase2() { 5281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 5381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 5481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 5581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ZERO; 5681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 5781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes aNumber.divide(bNumber); 5881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("ArithmeticException has not been caught"); 5981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (ArithmeticException e) { 6081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 6181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 6281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 6381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 6481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two equal positive numbers 6581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 6681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase3() { 6781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 6881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 6981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 7081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 7181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1}; 7281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 7381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 7481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 7581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 7681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 7781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 7881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 7981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 8081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 8181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 8281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 8381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 8481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two equal in absolute value numbers of different signs. 8581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 8681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase4() { 8781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 8881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 8981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 9081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 9181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-1}; 9281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 9381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 9481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 9581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 9681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 9781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 9881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 9981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 10081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 10181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 10281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 10381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 10481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two numbers of different length and different signs. 10581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is longer. 10681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 10781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase5() { 10881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 10981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 1, 2, 3, 4, 5}; 11081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 11181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 11281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 11381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 11481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 11581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 11681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 11781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 11881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 11981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 12081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 12181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 12281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 12381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 12481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 12581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two positive numbers of the same length. 12681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The second is greater. 12781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 12881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase6() { 12981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127}; 13081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 100, 56, 7, 98, -1, 39, -128, 127}; 13181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 13281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 13381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 13481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 13581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 13681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 13781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 13881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 13981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 14081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 14181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 14281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 14381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 14481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 14581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 14681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two positive numbers. 14781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 14881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase7() { 14981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 15081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 15181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 15281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 15381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {23, 115, 11, 78, 35, -11}; 15481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 15581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 15681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 15781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 15881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 15981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 16081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 16181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 16281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 16381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 16481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 16581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 16681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide a positive number by a negative one. 16781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 16881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase8() { 16981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 17081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 17181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 17281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 17381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-24, -116, -12, -79, -36, 11}; 17481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 17581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 17681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 17781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 17881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 17981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 18081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 18181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 18281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 18381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 18481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 18581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 18681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide a negative number by a positive one. 18781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 18881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase9() { 18981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 19081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 19181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 19281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 19381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-24, -116, -12, -79, -36, 11}; 19481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 19581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 19681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 19781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 19881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 19981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 20081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 20181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 20281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 20381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 20481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 20581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 20681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide two negative numbers. 20781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 20881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase10() { 20981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6, 7, 8, 9}; 21081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 21181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 21281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 21381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {23, 115, 11, 78, 35, -11}; 21481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 21581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 21681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 21781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 21881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 21981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 22081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 22181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 22281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 22381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 22481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 22581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 22681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide zero by a negative number. 22781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 22881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase11() { 22981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {0}; 23081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 23181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 0; 23281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 23381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 23481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 23581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 23681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 23781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 23881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 23981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 24081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 24181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 24281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 24381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 24481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 24581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 24681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide ZERO by a negative number. 24781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 24881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase12() { 24981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 25081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 25181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 25281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = BigInteger.ZERO; 25381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 25481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(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", 0, result.signum()); 26181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 26281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 26381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 26481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide a positive number by ONE. 26581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 26681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase13() { 26781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 26881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 26981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {15, 48, -29, 7, 98, -1, 39, -128}; 27081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 27181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ONE; 27281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 27381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 27481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 27581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 27681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 27781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 27881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 27981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 28081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 28181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 28281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide ONE by ONE. 28381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 28481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase14() { 28581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1}; 28681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = BigInteger.ONE; 28781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = BigInteger.ONE; 28881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 28981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 29081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 29181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 29281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 29381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 29481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 29581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 29681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 29781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 29881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verifies the case when borrow != 0 in the private divide method. 29981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 30081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testDivisionKnuth1() { 30181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7}; 30281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-3, -3, -3, -3}; 30381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 30481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 30581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -5, -12, -33, -96, -36, -105, -56, 92, 15, 48, -109}; 30681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 30781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 30881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 30981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 31081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 31181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 31281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 31381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 31481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 31581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 31681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 31781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 31881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verifies the case when the divisor is already normalized. 31981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 32081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testDivisionKnuthIsNormalized() { 32181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; 32281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-1, -1, -1, -1, -1, -1, -1, -1}; 32381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 32481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 32581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -9, -8, -7, -6, -5, -4, -3}; 32681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 32781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 32881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 32981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 33081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 33181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 33281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 33381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 33481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 33581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 33681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 33781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 33881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Verifies the case when the first digits of the dividend 33981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * and divisor equal. 34081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 34181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testDivisionKnuthFirstDigitsEqual() { 34281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {2, -3, -4, -5, -1, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5}; 34381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {2, -3, -4, -5, -1, -1, -1, -1}; 34481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 34581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 34681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0, -1, -1, -1, -1, -2, -88, -60, 41}; 34781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 34881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 34981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 35081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 35181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 35281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 35381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 35481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 35581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 35681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 35781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 35881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 35981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide the number of one digit by the number of one digit 36081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 36181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testDivisionKnuthOneDigitByOneDigit() { 36281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {113, -83, 123, -5}; 36381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {2, -3, -4, -5}; 36481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 36581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 36681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-37}; 36781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 36881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 36981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 37081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 37181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 37281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 37381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 37481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 37581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 37681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 37781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 37881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 37981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide the number of multi digits by the number of one digit 38081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 38181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testDivisionKnuthMultiDigitsByOneDigit() { 38281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29}; 38381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {2, -3, -4, -5}; 38481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 38581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 38681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-38, 2, 7, 30, 109, -43}; 38781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 38881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 38981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.divide(bNumber); 39081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 39181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 39281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 39381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 39481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 39581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 39681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 39781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 39881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 39981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division by zero 40081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 40181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase15() { 40281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 40381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {0}; 40481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 40581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 0; 40681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 40781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 40881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 40981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes aNumber.remainder(bNumber); 41081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("ArithmeticException has not been caught"); 41181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (ArithmeticException e) { 41281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 41381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 41481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 41581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 41681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division of equal numbers 41781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 41881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase16() { 41981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 42081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127}; 42181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 42281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 42381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {0}; 42481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 42581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 42681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 42781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 42881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 42981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 43081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 43181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 43281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 0, result.signum()); 43381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 43481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 43581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 43681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division of two positive numbers 43781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 43881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase17() { 43981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 44081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 44181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 44281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 44381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, -21, 73, 56, 27}; 44481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 44581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 44681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 44781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 44881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 44981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 45081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 45181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 45281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 45381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 45481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 45581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 45681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division of two negative numbers 45781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 45881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase18() { 45981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 46081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 46181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 46281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 46381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-13, 20, -74, -57, -27}; 46481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 46581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 46681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 46781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 46881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 46981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 47081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 47181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 47281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 47381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 47481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 47581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 47681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division of two numbers of different signs. 47781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is positive. 47881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 47981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase19() { 48081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 48181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 48281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 48381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 48481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, -21, 73, 56, 27}; 48581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 48681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 48781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(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 49681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 49781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Remainder of division of two numbers of different signs. 49881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is negative. 49981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 50081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase20() { 50181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 50281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 50381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 50481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 50581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {-13, 20, -74, -57, -27}; 50681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 50781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 50881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 50981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 51081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 51181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 51281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 51381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 51481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", -1, result.signum()); 51581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 51681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 51781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 51881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Tests the step D6 from the Knuth algorithm 51981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 52081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testRemainderKnuth1() { 52181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1}; 52281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10}; 52381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 52481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 52581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {1, 2, 3, 4, 5, 6, 7, 7, 18, -89}; 52681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 52781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 52881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 52981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 53081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 53181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 53281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 53381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 53481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 53581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 53681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 53781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 53881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide the number of one digit by the number of one digit 53981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 54081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testRemainderKnuthOneDigitByOneDigit() { 54181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {113, -83, 123, -5}; 54281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {2, -3, -4, -50}; 54381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 54481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 54581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {2, -9, -14, 53}; 54681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 54781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 54881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 54981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 55081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 55181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 55281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 55381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 55481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 55581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 55681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 55781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 55881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * Divide the number of multi digits by the number of one digit 55981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 56081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testRemainderKnuthMultiDigitsByOneDigit() { 56181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {113, -83, 123, -5, 18, -34, 67, 39, -29}; 56281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {2, -3, -4, -50}; 56381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 56481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 56581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {2, -37, -60, 59}; 56681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 56781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 56881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.remainder(bNumber); 56981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 57081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 57181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 57281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 57381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 57481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 57581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 57681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 57781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 57881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * divideAndRemainder of two numbers of different signs. 57981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * The first is negative. 58081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 58181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase21() { 58281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 58381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 58481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 58581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 58681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[][] = { 58781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes {-5, 94, -115, -74, -85, 84}, 58881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes {-13, 20, -74, -57, -27} 58981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes }; 59081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 59181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 59281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result[] = aNumber.divideAndRemainder(bNumber); 59381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 59481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result[0].toByteArray(); 59581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 59681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes if (resBytes[i] != rBytes[0][i]) { 59781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("Incorrect quotation"); 59881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 59981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 60081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals(-1, result[0].signum()); 60181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result[1].toByteArray(); 60281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 60381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes if (resBytes[i] != rBytes[1][i]) { 60481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("Incorrect remainder"); 60581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 60681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals(-1, result[1].signum()); 60781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 60881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 60981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 61081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 61181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * mod when modulus is negative 61281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 61381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase22() { 61481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 61581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {1, 30, 40, 56, -1, 45}; 61681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 61781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = -1; 61881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 61981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 62081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes try { 62181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes aNumber.mod(bNumber); 62281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes fail("ArithmeticException has not been caught"); 62381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } catch (ArithmeticException e) { 62481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 62581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 62681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 62781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 62881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * mod when a divisor is positive 62981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 63081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase23() { 63181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 63281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 63381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = 1; 63481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 63581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {12, -21, 73, 56, 27}; 63681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 63781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 63881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.mod(bNumber); 63981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 64081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 64181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 64281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 64381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 64481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 64581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 64681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes 64781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes /** 64881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes * mod when a divisor is negative 64981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes */ 65081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes public void testCase24() { 65181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte aBytes[] = {-127, 100, 56, 7, 98, -1, 39, -128, 127, 75}; 65281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte bBytes[] = {27, -15, 65, 39, 100}; 65381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int aSign = -1; 65481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes int bSign = 1; 65581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte rBytes[] = {15, 5, -9, -17, 73}; 65681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger aNumber = new BigInteger(aSign, aBytes); 65781ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger bNumber = new BigInteger(bSign, bBytes); 65881ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes BigInteger result = aNumber.mod(bNumber); 65981ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes byte resBytes[] = new byte[rBytes.length]; 66081ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes resBytes = result.toByteArray(); 66181ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes for(int i = 0; i < resBytes.length; i++) { 66281ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertTrue(resBytes[i] == rBytes[i]); 66381ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 66481ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes assertEquals("incorrect sign", 1, result.signum()); 66581ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes } 66681ccea015ba3d2a2da1c641b14dd3713c6b40a76Elliott Hughes} 667