Home | History | Annotate | Download | only in math
      1 /*
      2  *  Licensed to the Apache Software Foundation (ASF) under one or more
      3  *  contributor license agreements.  See the NOTICE file distributed with
      4  *  this work for additional information regarding copyright ownership.
      5  *  The ASF licenses this file to You under the Apache License, Version 2.0
      6  *  (the "License"); you may not use this file except in compliance with
      7  *  the License.  You may obtain a copy of the License at
      8  *
      9  *     http://www.apache.org/licenses/LICENSE-2.0
     10  *
     11  *  Unless required by applicable law or agreed to in writing, software
     12  *  distributed under the License is distributed on an "AS IS" BASIS,
     13  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     14  *  See the License for the specific language governing permissions and
     15  *  limitations under the License.
     16  */
     17 /**
     18  * @author Elena Semukhina
     19  */
     20 
     21 package org.apache.harmony.tests.java.math;
     22 
     23 import junit.framework.TestCase;
     24 import java.math.BigInteger;
     25 
     26 /**
     27  * Class:  java.math.BigInteger
     28  * Method: multiply
     29  */
     30 public class BigIntegerMultiplyTest extends TestCase {
     31     /**
     32      * Multiply two negative numbers of the same length
     33      */
     34     public void testCase1() {
     35         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3};
     36         byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
     37         int aSign = -1;
     38         int bSign = -1;
     39         byte rBytes[] = {10, 40, 100, -55, 96, 51, 76, 40, -45, 85, 105, 4, 28, -86, -117, -52, 100, 120, 90};
     40         BigInteger aNumber = new BigInteger(aSign, aBytes);
     41         BigInteger bNumber = new BigInteger(bSign, bBytes);
     42         BigInteger result = aNumber.multiply(bNumber);
     43         byte resBytes[] = new byte[rBytes.length];
     44         resBytes = result.toByteArray();
     45         for(int i = 0; i < resBytes.length; i++) {
     46             assertTrue(resBytes[i] == rBytes[i]);
     47         }
     48         assertEquals("incorrect sign", 1, result.signum());
     49     }
     50 
     51     /**
     52      * Multiply two numbers of the same length and different signs.
     53      * The first is negative.
     54      */
     55     public void testCase2() {
     56         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3};
     57         byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
     58         int aSign = -1;
     59         int bSign = 1;
     60         byte rBytes[] = {-11, -41, -101, 54, -97, -52, -77, -41, 44, -86, -106, -5, -29, 85, 116, 51, -101, -121, -90};
     61         BigInteger aNumber = new BigInteger(aSign, aBytes);
     62         BigInteger bNumber = new BigInteger(bSign, bBytes);
     63         BigInteger result = aNumber.multiply(bNumber);
     64         byte resBytes[] = new byte[rBytes.length];
     65         resBytes = result.toByteArray();
     66         for(int i = 0; i < resBytes.length; i++) {
     67             assertTrue(resBytes[i] == rBytes[i]);
     68         }
     69         assertEquals("incorrect sign", -1, result.signum());
     70     }
     71 
     72     /**
     73      * Multiply two positive numbers of different length.
     74      * The first is longer.
     75      */
     76     public void testCase3() {
     77         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
     78         byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
     79         int aSign = 1;
     80         int bSign = 1;
     81         byte rBytes[] = {10, 40, 100, -55, 96, 51, 76, 40, -45, 85, 115, 44, -127,
     82                          115, -21, -62, -15, 85, 64, -87, -2, -36, -36, -106};
     83         BigInteger aNumber = new BigInteger(aSign, aBytes);
     84         BigInteger bNumber = new BigInteger(bSign, bBytes);
     85         BigInteger result = aNumber.multiply(bNumber);
     86         byte resBytes[] = new byte[rBytes.length];
     87         resBytes = result.toByteArray();
     88         for(int i = 0; i < resBytes.length; i++) {
     89             assertTrue(resBytes[i] == rBytes[i]);
     90         }
     91         assertEquals("incorrect sign", 1, result.signum());
     92     }
     93 
     94     /**
     95      * Multiply two positive numbers of different length.
     96      * The second is longer.
     97      */
     98     public void testCase4() {
     99         byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
    100         byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    101         int aSign = 1;
    102         int bSign = 1;
    103         byte rBytes[] = {10, 40, 100, -55, 96, 51, 76, 40, -45, 85, 115, 44, -127,
    104                          115, -21, -62, -15, 85, 64, -87, -2, -36, -36, -106};
    105         BigInteger aNumber = new BigInteger(aSign, aBytes);
    106         BigInteger bNumber = new BigInteger(bSign, bBytes);
    107         BigInteger result = aNumber.multiply(bNumber);
    108         byte resBytes[] = new byte[rBytes.length];
    109         resBytes = result.toByteArray();
    110         for(int i = 0; i < resBytes.length; i++) {
    111             assertTrue(resBytes[i] == rBytes[i]);
    112         }
    113         assertEquals("incorrect sign", 1, result.signum());
    114     }
    115 
    116     /**
    117      * Multiply two numbers of different length and different signs.
    118      * The first is positive.
    119      * The first is longer.
    120      */
    121     public void testCase5() {
    122         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    123         byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
    124         int aSign = 1;
    125         int bSign = -1;
    126         byte rBytes[] = {-11, -41, -101, 54, -97, -52, -77, -41, 44, -86, -116, -45, 126,
    127                          -116, 20, 61, 14, -86, -65, 86, 1, 35, 35, 106};
    128         BigInteger aNumber = new BigInteger(aSign, aBytes);
    129         BigInteger bNumber = new BigInteger(bSign, bBytes);
    130         BigInteger result = aNumber.multiply(bNumber);
    131         byte resBytes[] = new byte[rBytes.length];
    132         resBytes = result.toByteArray();
    133         for(int i = 0; i < resBytes.length; i++) {
    134             assertTrue(resBytes[i] == rBytes[i]);
    135         }
    136         assertEquals("incorrect sign", -1, result.signum());
    137     }
    138 
    139     /**
    140      * Multiply two numbers of different length and different signs.
    141      * The first is positive.
    142      * The second is longer.
    143      */
    144     public void testCase6() {
    145         byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30};
    146         byte bBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    147         int aSign = 1;
    148         int bSign = -1;
    149         byte rBytes[] = {-11, -41, -101, 54, -97, -52, -77, -41, 44, -86, -116, -45, 126,
    150                          -116, 20, 61, 14, -86, -65, 86, 1, 35, 35, 106};
    151         BigInteger aNumber = new BigInteger(aSign, aBytes);
    152         BigInteger bNumber = new BigInteger(bSign, bBytes);
    153         BigInteger result = aNumber.multiply(bNumber);
    154         byte resBytes[] = new byte[rBytes.length];
    155         resBytes = result.toByteArray();
    156         for(int i = 0; i < resBytes.length; i++) {
    157             assertTrue(resBytes[i] == rBytes[i]);
    158         }
    159         assertEquals("incorrect sign", -1, result.signum());
    160     }
    161 
    162     /**
    163      * Multiply a number by zero.
    164      */
    165     public void testCase7() {
    166         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    167         byte bBytes[] = {0};
    168         int aSign = 1;
    169         int bSign = 0;
    170         byte rBytes[] = {0};
    171         BigInteger aNumber = new BigInteger(aSign, aBytes);
    172         BigInteger bNumber = new BigInteger(bSign, bBytes);
    173         BigInteger result = aNumber.multiply(bNumber);
    174         byte resBytes[] = new byte[rBytes.length];
    175         resBytes = result.toByteArray();
    176         for(int i = 0; i < resBytes.length; i++) {
    177             assertTrue(resBytes[i] == rBytes[i]);
    178         }
    179         assertEquals("incorrect sign", 0, result.signum());
    180     }
    181 
    182     /**
    183      * Multiply a number by ZERO.
    184      */
    185     public void testCase8() {
    186         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    187         int aSign = 1;
    188         byte rBytes[] = {0};
    189         BigInteger aNumber = new BigInteger(aSign, aBytes);
    190         BigInteger bNumber = BigInteger.ZERO;
    191         BigInteger result = aNumber.multiply(bNumber);
    192         byte resBytes[] = new byte[rBytes.length];
    193         resBytes = result.toByteArray();
    194         for(int i = 0; i < resBytes.length; i++) {
    195             assertTrue(resBytes[i] == rBytes[i]);
    196         }
    197         assertEquals("incorrect sign", 0, result.signum());
    198     }
    199 
    200     /**
    201      * Multiply a positive number by ONE.
    202      */
    203     public void testCase9() {
    204         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    205         int aSign = 1;
    206         byte rBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    207         BigInteger aNumber = new BigInteger(aSign, aBytes);
    208         BigInteger bNumber = BigInteger.ONE;
    209         BigInteger result = aNumber.multiply(bNumber);
    210         byte resBytes[] = new byte[rBytes.length];
    211         resBytes = result.toByteArray();
    212         for(int i = 0; i < resBytes.length; i++) {
    213             assertTrue(resBytes[i] == rBytes[i]);
    214         }
    215         assertEquals("incorrect sign", 1, result.signum());
    216     }
    217 
    218     /**
    219      * Multiply a negative number by ONE.
    220      */
    221     public void testCase10() {
    222         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7, 1, 2, 3, 1, 2, 3, 4, 5};
    223         int aSign = -1;
    224         byte rBytes[] = {-2, -3, -4, -5, -6, -7, -8, -2, -3, -4, -2, -3, -4, -5, -5};
    225         BigInteger aNumber = new BigInteger(aSign, aBytes);
    226         BigInteger bNumber = BigInteger.ONE;
    227         BigInteger result = aNumber.multiply(bNumber);
    228         byte resBytes[] = new byte[rBytes.length];
    229         resBytes = result.toByteArray();
    230         for(int i = 0; i < resBytes.length; i++) {
    231             assertTrue(resBytes[i] == rBytes[i]);
    232         }
    233         assertEquals("incorrect sign", -1, result.signum());
    234     }
    235 
    236     /**
    237      * Multiply two numbers of 4 bytes length.
    238      */
    239     public void testIntbyInt1() {
    240         byte aBytes[] = {10, 20, 30, 40};
    241         byte bBytes[] = {1, 2, 3, 4};
    242         int aSign = 1;
    243         int bSign = -1;
    244         byte rBytes[] = {-11, -41, -101, 55, 5, 15, 96};
    245         BigInteger aNumber = new BigInteger(aSign, aBytes);
    246         BigInteger bNumber = new BigInteger(bSign, bBytes);
    247         BigInteger result = aNumber.multiply(bNumber);
    248         byte resBytes[] = new byte[rBytes.length];
    249         resBytes = result.toByteArray();
    250         for(int i = 0; i < resBytes.length; i++) {
    251             assertTrue(resBytes[i] == rBytes[i]);
    252         }
    253         assertEquals("incorrect sign", -1, result.signum());
    254     }
    255 
    256     /**
    257      * Multiply two numbers of 4 bytes length.
    258      */
    259     public void testIntbyInt2() {
    260         byte aBytes[] = {-1, -1, -1, -1};
    261         byte bBytes[] = {-1, -1, -1, -1};
    262         int aSign = 1;
    263         int bSign = 1;
    264         byte rBytes[] = {0, -1, -1, -1, -2, 0, 0, 0, 1};
    265         BigInteger aNumber = new BigInteger(aSign, aBytes);
    266         BigInteger bNumber = new BigInteger(bSign, bBytes);
    267         BigInteger result = aNumber.multiply(bNumber);
    268         byte resBytes[] = new byte[rBytes.length];
    269         resBytes = result.toByteArray();
    270         for(int i = 0; i < resBytes.length; i++) {
    271             assertTrue(resBytes[i] == rBytes[i]);
    272         }
    273         assertEquals("incorrect sign", 1, result.signum());
    274     }
    275 
    276     /**
    277      * Negative exponent.
    278      */
    279     public void testPowException() {
    280         byte aBytes[] = {1, 2, 3, 4, 5, 6, 7};
    281         int aSign = 1;
    282         int exp = -5;
    283         BigInteger aNumber = new BigInteger(aSign, aBytes);
    284         try {
    285             aNumber.pow(exp);
    286             fail("ArithmeticException has not been caught");
    287         } catch (ArithmeticException e) {
    288         }
    289     }
    290 
    291     /**
    292      * Exponentiation of a negative number to an odd exponent.
    293      */
    294     public void testPowNegativeNumToOddExp() {
    295         byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    296         int aSign = -1;
    297         int exp = 5;
    298         byte rBytes[] = {-21, -94, -42, -15, -127, 113, -50, -88, 115, -35, 3,
    299             59, -92, 111, -75, 103, -42, 41, 34, -114, 99, -32, 105, -59, 127,
    300             45, 108, 74, -93, 105, 33, 12, -5, -20, 17, -21, -119, -127, -115,
    301             27, -122, 26, -67, 109, -125, 16, 91, -70, 109};
    302         BigInteger aNumber = new BigInteger(aSign, aBytes);
    303         BigInteger result = aNumber.pow(exp);
    304         byte resBytes[] = new byte[rBytes.length];
    305         resBytes = result.toByteArray();
    306         for(int i = 0; i < resBytes.length; i++) {
    307             assertTrue(resBytes[i] == rBytes[i]);
    308         }
    309         assertEquals("incorrect sign", -1, result.signum());
    310     }
    311 
    312     /**
    313      * Exponentiation of a negative number to an even exponent.
    314      */
    315     public void testPowNegativeNumToEvenExp() {
    316         byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    317         int aSign = -1;
    318         int exp = 4;
    319         byte rBytes[] = {102, 107, -122, -43, -52, -20, -27, 25, -9, 88, -13,
    320             75, 78, 81, -33, -77, 39, 27, -37, 106, 121, -73, 108, -47, -101,
    321             80, -25, 71, 13, 94, -7, -33, 1, -17, -65, -70, -61, -3, -47};
    322         BigInteger aNumber = new BigInteger(aSign, aBytes);
    323         BigInteger result = aNumber.pow(exp);
    324         byte resBytes[] = new byte[rBytes.length];
    325         resBytes = result.toByteArray();
    326         for(int i = 0; i < resBytes.length; i++) {
    327             assertTrue(resBytes[i] == rBytes[i]);
    328         }
    329         assertEquals("incorrect sign", 1, result.signum());
    330     }
    331 
    332     /**
    333      * Exponentiation of a negative number to zero exponent.
    334      */
    335     public void testPowNegativeNumToZeroExp() {
    336         byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    337         int aSign = -1;
    338         int exp = 0;
    339         byte rBytes[] = {1};
    340         BigInteger aNumber = new BigInteger(aSign, aBytes);
    341         BigInteger result = aNumber.pow(exp);
    342         byte resBytes[] = new byte[rBytes.length];
    343         resBytes = result.toByteArray();
    344         for(int i = 0; i < resBytes.length; i++) {
    345             assertTrue(resBytes[i] == rBytes[i]);
    346         }
    347         assertEquals("incorrect sign", 1, result.signum());
    348     }
    349 
    350     /**
    351      * Exponentiation of a positive number.
    352      */
    353     public void testPowPositiveNum() {
    354         byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    355         int aSign = 1;
    356         int exp = 5;
    357         byte rBytes[] = {20, 93, 41, 14, 126, -114, 49, 87, -116, 34, -4, -60,
    358             91, -112, 74, -104, 41, -42, -35, 113, -100, 31, -106, 58, -128,
    359             -46, -109, -75, 92, -106, -34, -13, 4, 19, -18, 20, 118, 126, 114,
    360             -28, 121, -27, 66, -110, 124, -17, -92, 69, -109};
    361         BigInteger aNumber = new BigInteger(aSign, aBytes);
    362         BigInteger result = aNumber.pow(exp);
    363         byte resBytes[] = new byte[rBytes.length];
    364         resBytes = result.toByteArray();
    365         for(int i = 0; i < resBytes.length; i++) {
    366             assertTrue(resBytes[i] == rBytes[i]);
    367         }
    368         assertEquals("incorrect sign", 1, result.signum());
    369     }
    370 
    371     /**
    372      * Exponentiation of a negative number to zero exponent.
    373      */
    374     public void testPowPositiveNumToZeroExp() {
    375         byte aBytes[] = {50, -26, 90, 69, 120, 32, 63, -103, -14, 35};
    376         int aSign = 1;
    377         int exp = 0;
    378         byte rBytes[] = {1};
    379         BigInteger aNumber = new BigInteger(aSign, aBytes);
    380         BigInteger result = aNumber.pow(exp);
    381         byte resBytes[] = new byte[rBytes.length];
    382         resBytes = result.toByteArray();
    383         for(int i = 0; i < resBytes.length; i++) {
    384             assertTrue(resBytes[i] == rBytes[i]);
    385         }
    386         assertEquals("incorrect sign", 1, result.signum());
    387     }
    388 }
    389