/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/**
* @author Elena Semukhina
*/
package org.apache.harmony.tests.java.math;
import junit.framework.TestCase;
import java.math.BigInteger;
/**
* Class: java.math.BigInteger
* Method: or
*/
public class BigIntegerOrTest extends TestCase {
/**
* Or for zero and a positive number
*/
public void testZeroPos() {
byte aBytes[] = {0};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 0;
int bSign = 1;
byte rBytes[] = {0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for zero and a negative number
*/
public void testZeroNeg() {
byte aBytes[] = {0};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 0;
int bSign = -1;
byte rBytes[] = {-1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for a positive number and zero
*/
public void testPosZero() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {0};
int aSign = 1;
int bSign = 0;
byte rBytes[] = {0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for a negative number and zero
*/
public void testNegPos() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {0};
int aSign = -1;
int bSign = 0;
byte rBytes[] = {-1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58, -67, -6, -15, -23};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for zero and zero
*/
public void testZeroZero() {
byte aBytes[] = {0};
byte bBytes[] = {0};
int aSign = 0;
int bSign = 0;
byte rBytes[] = {0};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 0, result.signum());
}
/**
* Or for zero and one
*/
public void testZeroOne() {
byte aBytes[] = {0};
byte bBytes[] = {1};
int aSign = 0;
int bSign = 1;
byte rBytes[] = {1};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for one and one
*/
public void testOneOne() {
byte aBytes[] = {1};
byte bBytes[] = {1};
int aSign = 1;
int bSign = 1;
byte rBytes[] = {1};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for two positive numbers of the same length
*/
public void testPosPosSameLength() {
byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 1;
int bSign = 1;
byte rBytes[] = {0, -2, -3, -4, -4, -1, -66, 95, 47, 123, 59, -13, 39, 30, -97};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for two positive numbers; the first is longer
*/
public void testPosPosFirstLonger() {
byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 1;
int bSign = 1;
byte rBytes[] = {0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for two positive numbers; the first is shorter
*/
public void testPosPosFirstShorter() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
int aSign = 1;
int bSign = 1;
byte rBytes[] = {0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15, -9, 39, 58, -69, 87, 87, -17, -73};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", 1, result.signum());
}
/**
* Or for two negative numbers of the same length
*/
public void testNegNegSameLength() {
byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = -1;
int bSign = -1;
byte rBytes[] = {-1, 127, -57, -101, -5, -5, -18, -38, -17, -2, -65, -2, -11, -3};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for two negative numbers; the first is longer
*/
public void testNegNegFirstLonger() {
byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = -1;
int bSign = -1;
byte rBytes[] = {-1, 1, 75, -89, -45, -2, -3, -18, -36, -17, -10, -3, -6, -7, -21};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for two negative numbers; the first is shorter
*/
public void testNegNegFirstShorter() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
int aSign = -1;
int bSign = -1;
byte rBytes[] = {-1, 1, 75, -89, -45, -2, -3, -18, -36, -17, -10, -3, -6, -7, -21};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for two numbers of different signs and the same length
*/
public void testPosNegSameLength() {
byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 1;
int bSign = -1;
byte rBytes[] = {-1, 1, -126, 59, 103, -2, -11, -7, -3, -33, -57, -3, -5, -5, -21};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for two numbers of different signs and the same length
*/
public void testNegPosSameLength() {
byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = -1;
int bSign = 1;
byte rBytes[] = {-1, 5, 79, -73, -9, -76, -3, 78, -35, -17, 119};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for a negative and a positive numbers; the first is longer
*/
public void testNegPosFirstLonger() {
byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = -1;
int bSign = 1;
byte rBytes[] = {-1, 127, -10, -57, -101, -1, -1, -2, -2, -91, -2, 31, -1, -11, 125, -22, -83, 30, 95};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for two negative numbers; the first is shorter
*/
public void testNegPosFirstShorter() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
int aSign = -1;
int bSign = 1;
byte rBytes[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for a positive and a negative numbers; the first is longer
*/
public void testPosNegFirstLonger() {
byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
int aSign = 1;
int bSign = -1;
byte rBytes[] = {-74, 91, 47, -5, -13, -7, -5, -33, -49, -65, -1, -9, -3};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
/**
* Or for a positive and a negative number; the first is shorter
*/
public void testPosNegFirstShorter() {
byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
int aSign = 1;
int bSign = -1;
byte rBytes[] = {-1, 127, -10, -57, -101, -1, -1, -2, -2, -91, -2, 31, -1, -11, 125, -22, -83, 30, 95};
BigInteger aNumber = new BigInteger(aSign, aBytes);
BigInteger bNumber = new BigInteger(bSign, bBytes);
BigInteger result = aNumber.or(bNumber);
byte resBytes[] = new byte[rBytes.length];
resBytes = result.toByteArray();
for(int i = 0; i < resBytes.length; i++) {
assertTrue(resBytes[i] == rBytes[i]);
}
assertEquals("incorrect sign", -1, result.signum());
}
public void testRegression() {
// Regression test for HARMONY-1996
BigInteger x = new BigInteger("-1023");
BigInteger r1 = x.and((BigInteger.ZERO.not()).shiftLeft(32));
BigInteger r3 = x.and((BigInteger.ZERO.not().shiftLeft(32) ).not());
BigInteger result = r1.or(r3);
assertEquals(x, result);
}
}