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: and
     29  */
     30 public class BigIntegerAndTest extends TestCase {
     31     /**
     32      * And for zero and a positive number
     33      */
     34     public void testZeroPos() {
     35         byte aBytes[] = {0};
     36         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
     37         int aSign = 0;
     38         int bSign = 1;
     39         byte rBytes[] = {0};
     40         BigInteger aNumber = new BigInteger(aSign, aBytes);
     41         BigInteger bNumber = new BigInteger(bSign, bBytes);
     42         BigInteger result = aNumber.and(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", 0, result.signum());
     49     }
     50 
     51     /**
     52      * And for zero and a negative number
     53      */
     54     public void testZeroNeg() {
     55         byte aBytes[] = {0};
     56         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
     57         int aSign = 0;
     58         int bSign = -1;
     59         byte rBytes[] = {0};
     60         BigInteger aNumber = new BigInteger(aSign, aBytes);
     61         BigInteger bNumber = new BigInteger(bSign, bBytes);
     62         BigInteger result = aNumber.and(bNumber);
     63         byte resBytes[] = new byte[rBytes.length];
     64         resBytes = result.toByteArray();
     65         for(int i = 0; i < resBytes.length; i++) {
     66             assertTrue(resBytes[i] == rBytes[i]);
     67         }
     68         assertEquals("incorrect sign", 0, result.signum());
     69     }
     70 
     71     /**
     72      * And for a positive number and zero
     73      */
     74     public void testPosZero() {
     75         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
     76         byte bBytes[] = {0};
     77         int aSign = 1;
     78         int bSign = 0;
     79         byte rBytes[] = {0};
     80         BigInteger aNumber = new BigInteger(aSign, aBytes);
     81         BigInteger bNumber = new BigInteger(bSign, bBytes);
     82         BigInteger result = aNumber.and(bNumber);
     83         byte resBytes[] = new byte[rBytes.length];
     84         resBytes = result.toByteArray();
     85         for(int i = 0; i < resBytes.length; i++) {
     86             assertTrue(resBytes[i] == rBytes[i]);
     87         }
     88         assertEquals("incorrect sign", 0, result.signum());
     89     }
     90 
     91     /**
     92      * And for a negative number and zero
     93      */
     94     public void testNegPos() {
     95         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
     96         byte bBytes[] = {0};
     97         int aSign = -1;
     98         int bSign = 0;
     99         byte rBytes[] = {0};
    100         BigInteger aNumber = new BigInteger(aSign, aBytes);
    101         BigInteger bNumber = new BigInteger(bSign, bBytes);
    102         BigInteger result = aNumber.and(bNumber);
    103         byte resBytes[] = new byte[rBytes.length];
    104         resBytes = result.toByteArray();
    105         for(int i = 0; i < resBytes.length; i++) {
    106             assertTrue(resBytes[i] == rBytes[i]);
    107         }
    108         assertEquals("incorrect sign", 0, result.signum());
    109     }
    110 
    111     /**
    112      * And for zero and zero
    113      */
    114     public void testZeroZero() {
    115         byte aBytes[] = {0};
    116         byte bBytes[] = {0};
    117         int aSign = 0;
    118         int bSign = 0;
    119         byte rBytes[] = {0};
    120         BigInteger aNumber = new BigInteger(aSign, aBytes);
    121         BigInteger bNumber = new BigInteger(bSign, bBytes);
    122         BigInteger result = aNumber.and(bNumber);
    123         byte resBytes[] = new byte[rBytes.length];
    124         resBytes = result.toByteArray();
    125         for(int i = 0; i < resBytes.length; i++) {
    126             assertTrue(resBytes[i] == rBytes[i]);
    127         }
    128         assertEquals("incorrect sign", 0, result.signum());
    129     }
    130 
    131     /**
    132      * And for zero and one
    133      */
    134     public void testZeroOne() {
    135         BigInteger aNumber = BigInteger.ZERO;
    136         BigInteger bNumber = BigInteger.ONE;
    137         BigInteger result = aNumber.and(bNumber);
    138         assertTrue(result.equals(BigInteger.ZERO));
    139         assertEquals("incorrect sign", 0, result.signum());
    140     }
    141 
    142     /**
    143      * And for one and one
    144      */
    145     public void testOneOne() {
    146         BigInteger aNumber = BigInteger.ONE;
    147         BigInteger bNumber = BigInteger.ONE;
    148         BigInteger result = aNumber.and(bNumber);
    149         assertTrue(result.equals(BigInteger.ONE));
    150         assertEquals("incorrect sign", 1, result.signum());
    151     }
    152 
    153     /**
    154      * And for two positive numbers of the same length
    155      */
    156     public void testPosPosSameLength() {
    157         byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    158         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    159         int aSign = 1;
    160         int bSign = 1;
    161         byte rBytes[] = {0, -128, 56, 100, 4, 4, 17, 37, 16, 1, 64, 1, 10, 3};
    162         BigInteger aNumber = new BigInteger(aSign, aBytes);
    163         BigInteger bNumber = new BigInteger(bSign, bBytes);
    164         BigInteger result = aNumber.and(bNumber);
    165         byte resBytes[] = new byte[rBytes.length];
    166         resBytes = result.toByteArray();
    167         for(int i = 0; i < resBytes.length; i++) {
    168             assertTrue(resBytes[i] == rBytes[i]);
    169         }
    170         assertEquals("incorrect sign", 1, result.signum());
    171     }
    172 
    173     /**
    174      * And for two positive numbers; the first is longer
    175      */
    176     public void testPosPosFirstLonger() {
    177         byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    178         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    179         int aSign = 1;
    180         int bSign = 1;
    181         byte rBytes[] = {0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21};
    182         BigInteger aNumber = new BigInteger(aSign, aBytes);
    183         BigInteger bNumber = new BigInteger(bSign, bBytes);
    184         BigInteger result = aNumber.and(bNumber);
    185         byte resBytes[] = new byte[rBytes.length];
    186         resBytes = result.toByteArray();
    187         for(int i = 0; i < resBytes.length; i++) {
    188             assertTrue(resBytes[i] == rBytes[i]);
    189         }
    190         assertEquals("incorrect sign", 1, result.signum());
    191     }
    192 
    193     /**
    194      * And for two positive numbers; the first is shorter
    195      */
    196     public void testPosPosFirstShorter() {
    197         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    198         byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    199         int aSign = 1;
    200         int bSign = 1;
    201         byte rBytes[] = {0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2, 5, 6, 21};
    202         BigInteger aNumber = new BigInteger(aSign, aBytes);
    203         BigInteger bNumber = new BigInteger(bSign, bBytes);
    204         BigInteger result = aNumber.and(bNumber);
    205         byte resBytes[] = new byte[rBytes.length];
    206         resBytes = result.toByteArray();
    207         for(int i = 0; i < resBytes.length; i++) {
    208             assertTrue(resBytes[i] == rBytes[i]);
    209         }
    210         assertEquals("incorrect sign", 1, result.signum());
    211     }
    212 
    213     /**
    214      * And for two negative numbers of the same length
    215      */
    216     public void testNegNegSameLength() {
    217         byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    218         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    219         int aSign = -1;
    220         int bSign = -1;
    221         byte rBytes[] = {-1, 1, 2, 3, 3, 0, 65, -96, -48, -124, -60, 12, -40, -31, 97};
    222         BigInteger aNumber = new BigInteger(aSign, aBytes);
    223         BigInteger bNumber = new BigInteger(bSign, bBytes);
    224         BigInteger result = aNumber.and(bNumber);
    225         byte resBytes[] = new byte[rBytes.length];
    226         resBytes = result.toByteArray();
    227         for(int i = 0; i < resBytes.length; i++) {
    228             assertTrue(resBytes[i] == rBytes[i]);
    229         }
    230         assertEquals("incorrect sign", -1, result.signum());
    231     }
    232 
    233     /**
    234      * And for two negative numbers; the first is longer
    235      */
    236     public void testNegNegFirstLonger() {
    237         byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    238         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    239         int aSign = -1;
    240         int bSign = -1;
    241         byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73};
    242         BigInteger aNumber = new BigInteger(aSign, aBytes);
    243         BigInteger bNumber = new BigInteger(bSign, bBytes);
    244         BigInteger result = aNumber.and(bNumber);
    245         byte resBytes[] = new byte[rBytes.length];
    246         resBytes = result.toByteArray();
    247         for(int i = 0; i < resBytes.length; i++) {
    248             assertTrue(resBytes[i] == rBytes[i]);
    249         }
    250         assertEquals("incorrect sign", -1, result.signum());
    251     }
    252 
    253     /**
    254      * And for two negative numbers; the first is shorter
    255      */
    256     public void testNegNegFirstShorter() {
    257         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    258         byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    259         int aSign = -1;
    260         int bSign = -1;
    261         byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 73};
    262         BigInteger aNumber = new BigInteger(aSign, aBytes);
    263         BigInteger bNumber = new BigInteger(bSign, bBytes);
    264         BigInteger result = aNumber.and(bNumber);
    265         byte resBytes[] = new byte[rBytes.length];
    266         resBytes = result.toByteArray();
    267         for(int i = 0; i < resBytes.length; i++) {
    268             assertTrue(resBytes[i] == rBytes[i]);
    269         }
    270         assertEquals("incorrect sign", -1, result.signum());
    271     }
    272 
    273     /**
    274      * And for two numbers of different signs and the same length
    275      */
    276     public void testPosNegSameLength() {
    277         byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    278         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    279         int aSign = 1;
    280         int bSign = -1;
    281         byte rBytes[] = {0, -6, -80, 72, 8, 75, 2, -79, 34, 16, -119};
    282         BigInteger aNumber = new BigInteger(aSign, aBytes);
    283         BigInteger bNumber = new BigInteger(bSign, bBytes);
    284         BigInteger result = aNumber.and(bNumber);
    285         byte resBytes[] = new byte[rBytes.length];
    286         resBytes = result.toByteArray();
    287         for(int i = 0; i < resBytes.length; i++) {
    288             assertTrue(resBytes[i] == rBytes[i]);
    289         }
    290         assertEquals("incorrect sign", 1, result.signum());
    291     }
    292 
    293     /**
    294      * And for two numbers of different signs and the same length
    295      */
    296     public void testNegPosSameLength() {
    297         byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
    298         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    299         int aSign = -1;
    300         int bSign = 1;
    301         byte rBytes[] = {0, -2, 125, -60, -104, 1, 10, 6, 2, 32, 56, 2, 4, 4, 21};
    302         BigInteger aNumber = new BigInteger(aSign, aBytes);
    303         BigInteger bNumber = new BigInteger(bSign, bBytes);
    304         BigInteger result = aNumber.and(bNumber);
    305         byte resBytes[] = new byte[rBytes.length];
    306         resBytes = result.toByteArray();
    307         for(int i = 0; i < resBytes.length; i++) {
    308             assertTrue(resBytes[i] == rBytes[i]);
    309         }
    310         assertEquals("incorrect sign", 1, result.signum());
    311     }
    312 
    313     /**
    314      * And for a negative and a positive numbers; the first is longer
    315      */
    316     public void testNegPosFirstLonger() {
    317         byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    318         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    319         int aSign = -1;
    320         int bSign = 1;
    321         byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3};
    322         BigInteger aNumber = new BigInteger(aSign, aBytes);
    323         BigInteger bNumber = new BigInteger(bSign, bBytes);
    324         BigInteger result = aNumber.and(bNumber);
    325         byte resBytes[] = new byte[rBytes.length];
    326         resBytes = result.toByteArray();
    327         for(int i = 0; i < resBytes.length; i++) {
    328             assertTrue(resBytes[i] == rBytes[i]);
    329         }
    330         assertEquals("incorrect sign", 1, result.signum());
    331     }
    332 
    333     /**
    334      * And for a negative and a positive numbers; the first is shorter
    335      */
    336     public void testNegPosFirstShorter() {
    337         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    338         byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    339         int aSign = -1;
    340         int bSign = 1;
    341         byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95};
    342         BigInteger aNumber = new BigInteger(aSign, aBytes);
    343         BigInteger bNumber = new BigInteger(bSign, bBytes);
    344         BigInteger result = aNumber.and(bNumber);
    345         byte resBytes[] = new byte[rBytes.length];
    346         resBytes = result.toByteArray();
    347         for(int i = 0; i < resBytes.length; i++) {
    348             assertTrue(resBytes[i] == rBytes[i]);
    349         }
    350         assertEquals("incorrect sign", 1, result.signum());
    351     }
    352 
    353     /**
    354      * And for a positive and a negative numbers; the first is longer
    355      */
    356     public void testPosNegFirstLonger() {
    357         byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    358         byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    359         int aSign = 1;
    360         int bSign = -1;
    361         byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -95};
    362         BigInteger aNumber = new BigInteger(aSign, aBytes);
    363         BigInteger bNumber = new BigInteger(bSign, bBytes);
    364         BigInteger result = aNumber.and(bNumber);
    365         byte resBytes[] = new byte[rBytes.length];
    366         resBytes = result.toByteArray();
    367         for(int i = 0; i < resBytes.length; i++) {
    368             assertTrue(resBytes[i] == rBytes[i]);
    369         }
    370         assertEquals("incorrect sign", 1, result.signum());
    371     }
    372 
    373     /**
    374      * And for a positive and a negative numbers; the first is shorter
    375      */
    376     public void testPosNegFirstShorter() {
    377         byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
    378         byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
    379         int aSign = 1;
    380         int bSign = -1;
    381         byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 3};
    382         BigInteger aNumber = new BigInteger(aSign, aBytes);
    383         BigInteger bNumber = new BigInteger(bSign, bBytes);
    384         BigInteger result = aNumber.and(bNumber);
    385         byte resBytes[] = new byte[rBytes.length];
    386         resBytes = result.toByteArray();
    387         for(int i = 0; i < resBytes.length; i++) {
    388             assertTrue(resBytes[i] == rBytes[i]);
    389         }
    390         assertEquals("incorrect sign", 1, result.signum());
    391     }
    392 
    393     /**
    394      * Test for a special case
    395      */
    396     public void testSpecialCase1() {
    397         byte aBytes[] = {-1, -1, -1, -1};
    398         byte bBytes[] = {5, -4, -3, -2};
    399         int aSign = -1;
    400         int bSign = -1;
    401         byte rBytes[] = {-1, 0, 0, 0, 0};
    402         BigInteger aNumber = new BigInteger(aSign, aBytes);
    403         BigInteger bNumber = new BigInteger(bSign, bBytes);
    404         BigInteger result = aNumber.and(bNumber);
    405         byte resBytes[] = new byte[rBytes.length];
    406         resBytes = result.toByteArray();
    407         for(int i = 0; i < resBytes.length; i++) {
    408             assertTrue(resBytes[i] == rBytes[i]);
    409         }
    410         assertEquals("incorrect sign", -1, result.signum());
    411     }
    412 
    413     /**
    414      * Test for a special case
    415      */
    416     public void testSpecialCase2() {
    417         byte aBytes[] = {-51};
    418         byte bBytes[] = {-52, -51, -50, -49, -48};
    419         int aSign = -1;
    420         int bSign = 1;
    421         byte rBytes[] = {0, -52, -51, -50, -49, 16};
    422         BigInteger aNumber = new BigInteger(aSign, aBytes);
    423         BigInteger bNumber = new BigInteger(bSign, bBytes);
    424         BigInteger result = aNumber.and(bNumber);
    425         byte resBytes[] = new byte[rBytes.length];
    426         resBytes = result.toByteArray();
    427         for(int i = 0; i < resBytes.length; i++) {
    428             assertTrue(resBytes[i] == rBytes[i]);
    429         }
    430         assertEquals("incorrect sign", 1, result.signum());
    431     }
    432 }
    433