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