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