001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /**
018: * @author Elena Semukhina
019: * @version $Revision$
020: */package org.apache.harmony.tests.java.math;
021:
022: import junit.framework.TestCase;
023: import java.math.BigInteger;
024:
025: /**
026: * Class: java.math.BigInteger
027: * Methods: and, andNot
028: */
029: public class BigIntegerNotTest extends TestCase {
030: /**
031: * andNot for two positive numbers; the first is longer
032: */
033: public void testAndNotPosPosFirstLonger() {
034: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
035: -15, 35, 26, -117, 23, 87, -25, -75 };
036: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
037: 14, 23 };
038: int aSign = 1;
039: int bSign = 1;
040: byte rBytes[] = { 0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32,
041: 0, 10, -126, 21, 82, -31, -96 };
042: BigInteger aNumber = new BigInteger(aSign, aBytes);
043: BigInteger bNumber = new BigInteger(bSign, bBytes);
044: BigInteger result = aNumber.andNot(bNumber);
045: byte resBytes[] = new byte[rBytes.length];
046: resBytes = result.toByteArray();
047: for (int i = 0; i < resBytes.length; i++) {
048: assertTrue(resBytes[i] == rBytes[i]);
049: }
050: assertEquals("incorrect sign", 1, result.signum());
051: }
052:
053: /**
054: * andNot for two positive numbers; the first is shorter
055: */
056: public void testAndNotPosPosFirstShorter() {
057: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
058: 14, 23 };
059: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
060: -15, 35, 26, -117, 23, 87, -25, -75 };
061: int aSign = 1;
062: int bSign = 1;
063: byte rBytes[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8,
064: 2 };
065: BigInteger aNumber = new BigInteger(aSign, aBytes);
066: BigInteger bNumber = new BigInteger(bSign, bBytes);
067: BigInteger result = aNumber.andNot(bNumber);
068: byte resBytes[] = new byte[rBytes.length];
069: resBytes = result.toByteArray();
070: for (int i = 0; i < resBytes.length; i++) {
071: assertTrue(resBytes[i] == rBytes[i]);
072: }
073: assertEquals("incorrect sign", 1, result.signum());
074: }
075:
076: /**
077: * andNot for two negative numbers; the first is longer
078: */
079: public void testAndNotNegNegFirstLonger() {
080: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
081: -15, 35, 26, -117, 23, 87, -25, -75 };
082: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
083: 14, 23 };
084: int aSign = -1;
085: int bSign = -1;
086: byte rBytes[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8,
087: 2 };
088: BigInteger aNumber = new BigInteger(aSign, aBytes);
089: BigInteger bNumber = new BigInteger(bSign, bBytes);
090: BigInteger result = aNumber.andNot(bNumber);
091: byte resBytes[] = new byte[rBytes.length];
092: resBytes = result.toByteArray();
093: for (int i = 0; i < resBytes.length; i++) {
094: assertTrue(resBytes[i] == rBytes[i]);
095: }
096: assertEquals("incorrect sign", 1, result.signum());
097: }
098:
099: /**
100: * andNot for a negative and a positive numbers; the first is longer
101: */
102: public void testNegPosFirstLonger() {
103: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
104: -15, 35, 26, -117, 23, 87, -25, -75 };
105: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
106: 14, 23 };
107: int aSign = -1;
108: int bSign = 1;
109: byte rBytes[] = { -1, 127, -10, -57, -101, 1, 2, 2, 2, -96,
110: -16, 8, -40, -59, 68, -88, -88, 16, 72 };
111: BigInteger aNumber = new BigInteger(aSign, aBytes);
112: BigInteger bNumber = new BigInteger(bSign, bBytes);
113: BigInteger result = aNumber.andNot(bNumber);
114: byte resBytes[] = new byte[rBytes.length];
115: resBytes = result.toByteArray();
116: for (int i = 0; i < resBytes.length; i++) {
117: assertTrue(resBytes[i] == rBytes[i]);
118: }
119: assertEquals("incorrect sign", -1, result.signum());
120: }
121:
122: /**
123: * Not for ZERO
124: */
125: public void testNotZero() {
126: byte rBytes[] = { -1 };
127: BigInteger aNumber = BigInteger.ZERO;
128: BigInteger result = aNumber.not();
129: byte resBytes[] = new byte[rBytes.length];
130: resBytes = result.toByteArray();
131: for (int i = 0; i < resBytes.length; i++) {
132: assertTrue(resBytes[i] == rBytes[i]);
133: }
134: assertEquals("incorrect sign", -1, result.signum());
135: }
136:
137: /**
138: * Not for ONE
139: */
140: public void testNotOne() {
141: byte rBytes[] = { -2 };
142: BigInteger aNumber = BigInteger.ONE;
143: BigInteger result = aNumber.not();
144: byte resBytes[] = new byte[rBytes.length];
145: resBytes = result.toByteArray();
146: for (int i = 0; i < resBytes.length; i++) {
147: assertTrue(resBytes[i] == rBytes[i]);
148: }
149: assertEquals("incorrect sign", -1, result.signum());
150: }
151:
152: /**
153: * Not for a positive number
154: */
155: public void testNotPos() {
156: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
157: 35, 26, -117 };
158: int aSign = 1;
159: byte rBytes[] = { -1, 127, -57, -101, 1, 75, -90, -46, -92, -4,
160: 14, -36, -27, 116 };
161: BigInteger aNumber = new BigInteger(aSign, aBytes);
162: BigInteger result = aNumber.not();
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: * Not for a negative number
173: */
174: public void testNotNeg() {
175: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
176: 35, 26, -117 };
177: int aSign = -1;
178: byte rBytes[] = { 0, -128, 56, 100, -2, -76, 89, 45, 91, 3,
179: -15, 35, 26, -118 };
180: BigInteger aNumber = new BigInteger(aSign, aBytes);
181: BigInteger result = aNumber.not();
182: byte resBytes[] = new byte[rBytes.length];
183: resBytes = result.toByteArray();
184: for (int i = 0; i < resBytes.length; i++) {
185: assertTrue(resBytes[i] == rBytes[i]);
186: }
187: assertEquals("incorrect sign", 1, result.signum());
188: }
189:
190: /**
191: * Not for a negative number
192: */
193:
194: public void testNotSpecialCase() {
195: byte aBytes[] = { -1, -1, -1, -1 };
196: int aSign = 1;
197: byte rBytes[] = { -1, 0, 0, 0, 0 };
198: BigInteger aNumber = new BigInteger(aSign, aBytes);
199: BigInteger result = aNumber.not();
200: byte resBytes[] = new byte[rBytes.length];
201: resBytes = result.toByteArray();
202: for (int i = 0; i < resBytes.length; i++) {
203: assertTrue(resBytes[i] == rBytes[i]);
204: }
205: assertEquals("incorrect sign", -1, result.signum());
206: }
207: }
|