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: * Method: or
028: */
029: public class BigIntegerOrTest extends TestCase {
030: /**
031: * Or for zero and a positive number
032: */
033: public void testZeroPos() {
034: byte aBytes[] = { 0 };
035: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
036: 14, 23 };
037: int aSign = 0;
038: int bSign = 1;
039: byte rBytes[] = { 0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66,
040: 5, 14, 23 };
041: BigInteger aNumber = new BigInteger(aSign, aBytes);
042: BigInteger bNumber = new BigInteger(bSign, bBytes);
043: BigInteger result = aNumber.or(bNumber);
044: byte resBytes[] = new byte[rBytes.length];
045: resBytes = result.toByteArray();
046: for (int i = 0; i < resBytes.length; i++) {
047: assertTrue(resBytes[i] == rBytes[i]);
048: }
049: assertEquals("incorrect sign", 1, result.signum());
050: }
051:
052: /**
053: * Or for zero and a negative number
054: */
055: public void testZeroNeg() {
056: byte aBytes[] = { 0 };
057: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
058: 14, 23 };
059: int aSign = 0;
060: int bSign = -1;
061: byte rBytes[] = { -1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58,
062: -67, -6, -15, -23 };
063: BigInteger aNumber = new BigInteger(aSign, aBytes);
064: BigInteger bNumber = new BigInteger(bSign, bBytes);
065: BigInteger result = aNumber.or(bNumber);
066: byte resBytes[] = new byte[rBytes.length];
067: resBytes = result.toByteArray();
068: for (int i = 0; i < resBytes.length; i++) {
069: assertTrue(resBytes[i] == rBytes[i]);
070: }
071: assertEquals("incorrect sign", -1, result.signum());
072: }
073:
074: /**
075: * Or for a positive number and zero
076: */
077: public void testPosZero() {
078: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
079: 14, 23 };
080: byte bBytes[] = { 0 };
081: int aSign = 1;
082: int bSign = 0;
083: byte rBytes[] = { 0, -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66,
084: 5, 14, 23 };
085: BigInteger aNumber = new BigInteger(aSign, aBytes);
086: BigInteger bNumber = new BigInteger(bSign, bBytes);
087: BigInteger result = aNumber.or(bNumber);
088: byte resBytes[] = new byte[rBytes.length];
089: resBytes = result.toByteArray();
090: for (int i = 0; i < resBytes.length; i++) {
091: assertTrue(resBytes[i] == rBytes[i]);
092: }
093: assertEquals("incorrect sign", 1, result.signum());
094: }
095:
096: /**
097: * Or for a negative number and zero
098: */
099: public void testNegPos() {
100: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
101: 14, 23 };
102: byte bBytes[] = { 0 };
103: int aSign = -1;
104: int bSign = 0;
105: byte rBytes[] = { -1, 1, 2, 3, 3, -6, -15, -24, -40, -49, -58,
106: -67, -6, -15, -23 };
107: BigInteger aNumber = new BigInteger(aSign, aBytes);
108: BigInteger bNumber = new BigInteger(bSign, bBytes);
109: BigInteger result = aNumber.or(bNumber);
110: byte resBytes[] = new byte[rBytes.length];
111: resBytes = result.toByteArray();
112: for (int i = 0; i < resBytes.length; i++) {
113: assertTrue(resBytes[i] == rBytes[i]);
114: }
115: assertEquals("incorrect sign", -1, result.signum());
116: }
117:
118: /**
119: * Or for zero and zero
120: */
121: public void testZeroZero() {
122: byte aBytes[] = { 0 };
123: byte bBytes[] = { 0 };
124: int aSign = 0;
125: int bSign = 0;
126: byte rBytes[] = { 0 };
127: BigInteger aNumber = new BigInteger(aSign, aBytes);
128: BigInteger bNumber = new BigInteger(bSign, bBytes);
129: BigInteger result = aNumber.or(bNumber);
130: byte resBytes[] = new byte[rBytes.length];
131: resBytes = result.toByteArray();
132: for (int i = 0; i < resBytes.length; i++) {
133: assertTrue(resBytes[i] == rBytes[i]);
134: }
135: assertEquals("incorrect sign", 0, result.signum());
136: }
137:
138: /**
139: * Or for zero and one
140: */
141: public void testZeroOne() {
142: byte aBytes[] = { 0 };
143: byte bBytes[] = { 1 };
144: int aSign = 0;
145: int bSign = 1;
146: byte rBytes[] = { 1 };
147: BigInteger aNumber = new BigInteger(aSign, aBytes);
148: BigInteger bNumber = new BigInteger(bSign, bBytes);
149: BigInteger result = aNumber.or(bNumber);
150: byte resBytes[] = new byte[rBytes.length];
151: resBytes = result.toByteArray();
152: for (int i = 0; i < resBytes.length; i++) {
153: assertTrue(resBytes[i] == rBytes[i]);
154: }
155: assertEquals("incorrect sign", 1, result.signum());
156: }
157:
158: /**
159: * Or for one and one
160: */
161: public void testOneOne() {
162: byte aBytes[] = { 1 };
163: byte bBytes[] = { 1 };
164: int aSign = 1;
165: int bSign = 1;
166: byte rBytes[] = { 1 };
167: BigInteger aNumber = new BigInteger(aSign, aBytes);
168: BigInteger bNumber = new BigInteger(bSign, bBytes);
169: BigInteger result = aNumber.or(bNumber);
170: byte resBytes[] = new byte[rBytes.length];
171: resBytes = result.toByteArray();
172: for (int i = 0; i < resBytes.length; i++) {
173: assertTrue(resBytes[i] == rBytes[i]);
174: }
175: assertEquals("incorrect sign", 1, result.signum());
176: }
177:
178: /**
179: * Or for two positive numbers of the same length
180: */
181: public void testPosPosSameLength() {
182: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
183: 35, 26, -117 };
184: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
185: 14, 23 };
186: int aSign = 1;
187: int bSign = 1;
188: byte rBytes[] = { 0, -2, -3, -4, -4, -1, -66, 95, 47, 123, 59,
189: -13, 39, 30, -97 };
190: BigInteger aNumber = new BigInteger(aSign, aBytes);
191: BigInteger bNumber = new BigInteger(bSign, bBytes);
192: BigInteger result = aNumber.or(bNumber);
193: byte resBytes[] = new byte[rBytes.length];
194: resBytes = result.toByteArray();
195: for (int i = 0; i < resBytes.length; i++) {
196: assertTrue(resBytes[i] == rBytes[i]);
197: }
198: assertEquals("incorrect sign", 1, result.signum());
199: }
200:
201: /**
202: * Or for two positive numbers; the first is longer
203: */
204: public void testPosPosFirstLonger() {
205: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
206: -15, 35, 26, -117, 23, 87, -25, -75 };
207: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
208: 14, 23 };
209: int aSign = 1;
210: int bSign = 1;
211: byte rBytes[] = { 0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15,
212: -9, 39, 58, -69, 87, 87, -17, -73 };
213: BigInteger aNumber = new BigInteger(aSign, aBytes);
214: BigInteger bNumber = new BigInteger(bSign, bBytes);
215: BigInteger result = aNumber.or(bNumber);
216: byte resBytes[] = new byte[rBytes.length];
217: resBytes = result.toByteArray();
218: for (int i = 0; i < resBytes.length; i++) {
219: assertTrue(resBytes[i] == rBytes[i]);
220: }
221: assertEquals("incorrect sign", 1, result.signum());
222: }
223:
224: /**
225: * Or for two positive numbers; the first is shorter
226: */
227: public void testPosPosFirstShorter() {
228: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
229: 14, 23 };
230: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
231: -15, 35, 26, -117, 23, 87, -25, -75 };
232: int aSign = 1;
233: int bSign = 1;
234: byte rBytes[] = { 0, -128, 9, 56, 100, -2, -3, -3, -3, 95, 15,
235: -9, 39, 58, -69, 87, 87, -17, -73 };
236: BigInteger aNumber = new BigInteger(aSign, aBytes);
237: BigInteger bNumber = new BigInteger(bSign, bBytes);
238: BigInteger result = aNumber.or(bNumber);
239: byte resBytes[] = new byte[rBytes.length];
240: resBytes = result.toByteArray();
241: for (int i = 0; i < resBytes.length; i++) {
242: assertTrue(resBytes[i] == rBytes[i]);
243: }
244: assertEquals("incorrect sign", 1, result.signum());
245: }
246:
247: /**
248: * Or for two negative numbers of the same length
249: */
250: public void testNegNegSameLength() {
251: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
252: 35, 26, -117 };
253: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
254: 14, 23 };
255: int aSign = -1;
256: int bSign = -1;
257: byte rBytes[] = { -1, 127, -57, -101, -5, -5, -18, -38, -17,
258: -2, -65, -2, -11, -3 };
259: BigInteger aNumber = new BigInteger(aSign, aBytes);
260: BigInteger bNumber = new BigInteger(bSign, bBytes);
261: BigInteger result = aNumber.or(bNumber);
262: byte resBytes[] = new byte[rBytes.length];
263: resBytes = result.toByteArray();
264: for (int i = 0; i < resBytes.length; i++) {
265: assertTrue(resBytes[i] == rBytes[i]);
266: }
267: assertEquals("incorrect sign", -1, result.signum());
268: }
269:
270: /**
271: * Or for two negative numbers; the first is longer
272: */
273: public void testNegNegFirstLonger() {
274: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
275: -15, 35, 26, -117, 23, 87, -25, -75 };
276: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
277: 14, 23 };
278: int aSign = -1;
279: int bSign = -1;
280: byte rBytes[] = { -1, 1, 75, -89, -45, -2, -3, -18, -36, -17,
281: -10, -3, -6, -7, -21 };
282: BigInteger aNumber = new BigInteger(aSign, aBytes);
283: BigInteger bNumber = new BigInteger(bSign, bBytes);
284: BigInteger result = aNumber.or(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: * Or for two negative numbers; the first is shorter
295: */
296: public void testNegNegFirstShorter() {
297: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
298: 14, 23 };
299: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
300: -15, 35, 26, -117, 23, 87, -25, -75 };
301: int aSign = -1;
302: int bSign = -1;
303: byte rBytes[] = { -1, 1, 75, -89, -45, -2, -3, -18, -36, -17,
304: -10, -3, -6, -7, -21 };
305: BigInteger aNumber = new BigInteger(aSign, aBytes);
306: BigInteger bNumber = new BigInteger(bSign, bBytes);
307: BigInteger result = aNumber.or(bNumber);
308: byte resBytes[] = new byte[rBytes.length];
309: resBytes = result.toByteArray();
310: for (int i = 0; i < resBytes.length; i++) {
311: assertTrue(resBytes[i] == rBytes[i]);
312: }
313: assertEquals("incorrect sign", -1, result.signum());
314: }
315:
316: /**
317: * Or for two numbers of different signs and the same length
318: */
319: public void testPosNegSameLength() {
320: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
321: 35, 26, -117 };
322: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
323: 14, 23 };
324: int aSign = 1;
325: int bSign = -1;
326: byte rBytes[] = { -1, 1, -126, 59, 103, -2, -11, -7, -3, -33,
327: -57, -3, -5, -5, -21 };
328: BigInteger aNumber = new BigInteger(aSign, aBytes);
329: BigInteger bNumber = new BigInteger(bSign, bBytes);
330: BigInteger result = aNumber.or(bNumber);
331: byte resBytes[] = new byte[rBytes.length];
332: resBytes = result.toByteArray();
333: for (int i = 0; i < resBytes.length; i++) {
334: assertTrue(resBytes[i] == rBytes[i]);
335: }
336: assertEquals("incorrect sign", -1, result.signum());
337: }
338:
339: /**
340: * Or for two numbers of different signs and the same length
341: */
342: public void testNegPosSameLength() {
343: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
344: 35, 26, -117 };
345: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
346: 14, 23 };
347: int aSign = -1;
348: int bSign = 1;
349: byte rBytes[] = { -1, 5, 79, -73, -9, -76, -3, 78, -35, -17,
350: 119 };
351: BigInteger aNumber = new BigInteger(aSign, aBytes);
352: BigInteger bNumber = new BigInteger(bSign, bBytes);
353: BigInteger result = aNumber.or(bNumber);
354: byte resBytes[] = new byte[rBytes.length];
355: resBytes = result.toByteArray();
356: for (int i = 0; i < resBytes.length; i++) {
357: assertTrue(resBytes[i] == rBytes[i]);
358: }
359: assertEquals("incorrect sign", -1, result.signum());
360: }
361:
362: /**
363: * Or for a negative and a positive numbers; the first is longer
364: */
365: public void testNegPosFirstLonger() {
366: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
367: -15, 35, 26, -117, 23, 87, -25, -75 };
368: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
369: 14, 23 };
370: int aSign = -1;
371: int bSign = 1;
372: byte rBytes[] = { -1, 127, -10, -57, -101, -1, -1, -2, -2, -91,
373: -2, 31, -1, -11, 125, -22, -83, 30, 95 };
374: BigInteger aNumber = new BigInteger(aSign, aBytes);
375: BigInteger bNumber = new BigInteger(bSign, bBytes);
376: BigInteger result = aNumber.or(bNumber);
377: byte resBytes[] = new byte[rBytes.length];
378: resBytes = result.toByteArray();
379: for (int i = 0; i < resBytes.length; i++) {
380: assertTrue(resBytes[i] == rBytes[i]);
381: }
382: assertEquals("incorrect sign", -1, result.signum());
383: }
384:
385: /**
386: * Or for two negative numbers; the first is shorter
387: */
388: public void testNegPosFirstShorter() {
389: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
390: 14, 23 };
391: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
392: -15, 35, 26, -117, 23, 87, -25, -75 };
393: int aSign = -1;
394: int bSign = 1;
395: byte rBytes[] = { -74, 91, 47, -5, -13, -7, -5, -33, -49, -65,
396: -1, -9, -3 };
397: BigInteger aNumber = new BigInteger(aSign, aBytes);
398: BigInteger bNumber = new BigInteger(bSign, bBytes);
399: BigInteger result = aNumber.or(bNumber);
400: byte resBytes[] = new byte[rBytes.length];
401: resBytes = result.toByteArray();
402: for (int i = 0; i < resBytes.length; i++) {
403: assertTrue(resBytes[i] == rBytes[i]);
404: }
405: assertEquals("incorrect sign", -1, result.signum());
406: }
407:
408: /**
409: * Or for a positive and a negative numbers; the first is longer
410: */
411: public void testPosNegFirstLonger() {
412: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
413: -15, 35, 26, -117, 23, 87, -25, -75 };
414: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
415: 14, 23 };
416: int aSign = 1;
417: int bSign = -1;
418: byte rBytes[] = { -74, 91, 47, -5, -13, -7, -5, -33, -49, -65,
419: -1, -9, -3 };
420: BigInteger aNumber = new BigInteger(aSign, aBytes);
421: BigInteger bNumber = new BigInteger(bSign, bBytes);
422: BigInteger result = aNumber.or(bNumber);
423: byte resBytes[] = new byte[rBytes.length];
424: resBytes = result.toByteArray();
425: for (int i = 0; i < resBytes.length; i++) {
426: assertTrue(resBytes[i] == rBytes[i]);
427: }
428: assertEquals("incorrect sign", -1, result.signum());
429: }
430:
431: /**
432: * Or for a positive and a negative number; the first is shorter
433: */
434: public void testPosNegFirstShorter() {
435: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
436: 14, 23 };
437: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
438: -15, 35, 26, -117, 23, 87, -25, -75 };
439: int aSign = 1;
440: int bSign = -1;
441: byte rBytes[] = { -1, 127, -10, -57, -101, -1, -1, -2, -2, -91,
442: -2, 31, -1, -11, 125, -22, -83, 30, 95 };
443: BigInteger aNumber = new BigInteger(aSign, aBytes);
444: BigInteger bNumber = new BigInteger(bSign, bBytes);
445: BigInteger result = aNumber.or(bNumber);
446: byte resBytes[] = new byte[rBytes.length];
447: resBytes = result.toByteArray();
448: for (int i = 0; i < resBytes.length; i++) {
449: assertTrue(resBytes[i] == rBytes[i]);
450: }
451: assertEquals("incorrect sign", -1, result.signum());
452: }
453:
454: public void testRegression() {
455: // Regression test for HARMONY-1996
456: BigInteger x = new BigInteger("-1023");
457: BigInteger r1 = x.and((BigInteger.ZERO.not()).shiftLeft(32));
458: BigInteger r3 = x.and((BigInteger.ZERO.not().shiftLeft(32))
459: .not());
460: BigInteger result = r1.or(r3);
461: assertEquals(x, result);
462: }
463: }
|