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: abs, compareTo, equals, max, min, negate, signum
028: */
029: public class BigIntegerCompareTest extends TestCase {
030: /**
031: * abs() for a positive number
032: */
033: public void testAbsPositive() {
034: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
035: int aSign = 1;
036: byte rBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
037: BigInteger aNumber = new BigInteger(aSign, aBytes);
038: BigInteger result = aNumber.abs();
039: byte resBytes[] = new byte[rBytes.length];
040: resBytes = result.toByteArray();
041: for (int i = 0; i < resBytes.length; i++) {
042: assertTrue(resBytes[i] == rBytes[i]);
043: }
044: assertEquals("incorrect sign", 1, result.signum());
045: }
046:
047: /**
048: * abs() for a negative number
049: */
050: public void testAbsNegative() {
051: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
052: int aSign = -1;
053: byte rBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
054: BigInteger aNumber = new BigInteger(aSign, aBytes);
055: BigInteger result = aNumber.abs();
056: byte resBytes[] = new byte[rBytes.length];
057: resBytes = result.toByteArray();
058: for (int i = 0; i < resBytes.length; i++) {
059: assertTrue(resBytes[i] == rBytes[i]);
060: }
061: assertEquals("incorrect sign", 1, result.signum());
062: }
063:
064: /**
065: * compareTo(BigInteger a).
066: * Compare two positive numbers.
067: * The first is greater.
068: */
069: public void testCompareToPosPos1() {
070: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
071: 26, 3, 91 };
072: byte bBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
073: int aSign = 1;
074: int bSign = 1;
075: BigInteger aNumber = new BigInteger(aSign, aBytes);
076: BigInteger bNumber = new BigInteger(bSign, bBytes);
077: assertEquals(1, aNumber.compareTo(bNumber));
078: }
079:
080: /**
081: * compareTo(BigInteger a).
082: * Compare two positive numbers.
083: * The first is less.
084: */
085: public void testCompareToPosPos2() {
086: byte aBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
087: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
088: 26, 3, 91 };
089: int aSign = 1;
090: int bSign = 1;
091: BigInteger aNumber = new BigInteger(aSign, aBytes);
092: BigInteger bNumber = new BigInteger(bSign, bBytes);
093: assertEquals(-1, aNumber.compareTo(bNumber));
094: }
095:
096: /**
097: * compareTo(BigInteger a).
098: * Compare two equal positive numbers.
099: */
100: public void testCompareToEqualPos() {
101: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
102: 26, 3, 91 };
103: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
104: 26, 3, 91 };
105: int aSign = 1;
106: int bSign = 1;
107: BigInteger aNumber = new BigInteger(aSign, aBytes);
108: BigInteger bNumber = new BigInteger(bSign, bBytes);
109: assertEquals(0, aNumber.compareTo(bNumber));
110: }
111:
112: /**
113: * compareTo(BigInteger a).
114: * Compare two negative numbers.
115: * The first is greater in absolute value.
116: */
117: public void testCompareToNegNeg1() {
118: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
119: 26, 3, 91 };
120: byte bBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
121: int aSign = -1;
122: int bSign = -1;
123: BigInteger aNumber = new BigInteger(aSign, aBytes);
124: BigInteger bNumber = new BigInteger(bSign, bBytes);
125: assertEquals(-1, aNumber.compareTo(bNumber));
126: }
127:
128: /**
129: * compareTo(BigInteger a).
130: * Compare two negative numbers.
131: * The first is less in absolute value.
132: */
133: public void testCompareNegNeg2() {
134: byte aBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
135: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
136: 26, 3, 91 };
137: int aSign = -1;
138: int bSign = -1;
139: BigInteger aNumber = new BigInteger(aSign, aBytes);
140: BigInteger bNumber = new BigInteger(bSign, bBytes);
141: assertEquals(1, aNumber.compareTo(bNumber));
142: }
143:
144: /**
145: * compareTo(BigInteger a).
146: * Compare two equal negative numbers.
147: */
148: public void testCompareToEqualNeg() {
149: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
150: 26, 3, 91 };
151: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
152: 26, 3, 91 };
153: int aSign = -1;
154: int bSign = -1;
155: BigInteger aNumber = new BigInteger(aSign, aBytes);
156: BigInteger bNumber = new BigInteger(bSign, bBytes);
157: assertEquals(0, aNumber.compareTo(bNumber));
158: }
159:
160: /**
161: * compareTo(BigInteger a).
162: * Compare two numbers of different signs.
163: * The first is positive.
164: */
165: public void testCompareToDiffSigns1() {
166: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
167: 26, 3, 91 };
168: byte bBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
169: int aSign = 1;
170: int bSign = -1;
171: BigInteger aNumber = new BigInteger(aSign, aBytes);
172: BigInteger bNumber = new BigInteger(bSign, bBytes);
173: assertEquals(1, aNumber.compareTo(bNumber));
174: }
175:
176: /**
177: * compareTo(BigInteger a).
178: * Compare two numbers of different signs.
179: * The first is negative.
180: */
181: public void testCompareToDiffSigns2() {
182: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
183: 26, 3, 91 };
184: byte bBytes[] = { 10, 20, 30, 40, 50, 60, 70, 10, 20, 30 };
185: int aSign = -1;
186: int bSign = 1;
187: BigInteger aNumber = new BigInteger(aSign, aBytes);
188: BigInteger bNumber = new BigInteger(bSign, bBytes);
189: assertEquals(-1, aNumber.compareTo(bNumber));
190: }
191:
192: /**
193: * compareTo(BigInteger a).
194: * Compare a positive number to ZERO.
195: */
196: public void testCompareToPosZero() {
197: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
198: 26, 3, 91 };
199: int aSign = 1;
200: BigInteger aNumber = new BigInteger(aSign, aBytes);
201: BigInteger bNumber = BigInteger.ZERO;
202: assertEquals(1, aNumber.compareTo(bNumber));
203: }
204:
205: /**
206: * compareTo(BigInteger a).
207: * Compare ZERO to a positive number.
208: */
209: public void testCompareToZeroPos() {
210: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
211: 26, 3, 91 };
212: int bSign = 1;
213: BigInteger aNumber = BigInteger.ZERO;
214: BigInteger bNumber = new BigInteger(bSign, bBytes);
215: assertEquals(-1, aNumber.compareTo(bNumber));
216: }
217:
218: /**
219: * compareTo(BigInteger a).
220: * Compare a negative number to ZERO.
221: */
222: public void testCompareToNegZero() {
223: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
224: 26, 3, 91 };
225: int aSign = -1;
226: BigInteger aNumber = new BigInteger(aSign, aBytes);
227: BigInteger bNumber = BigInteger.ZERO;
228: assertEquals(-1, aNumber.compareTo(bNumber));
229: }
230:
231: /**
232: * compareTo(BigInteger a).
233: * Compare ZERO to a negative number.
234: */
235: public void testCompareToZeroNeg() {
236: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
237: 26, 3, 91 };
238: int bSign = -1;
239: BigInteger aNumber = BigInteger.ZERO;
240: BigInteger bNumber = new BigInteger(bSign, bBytes);
241: assertEquals(1, aNumber.compareTo(bNumber));
242: }
243:
244: /**
245: * compareTo(BigInteger a).
246: * Compare ZERO to ZERO.
247: */
248: public void testCompareToZeroZero() {
249: BigInteger aNumber = BigInteger.ZERO;
250: BigInteger bNumber = BigInteger.ZERO;
251: assertEquals(0, aNumber.compareTo(bNumber));
252: }
253:
254: /**
255: * equals(Object obj).
256: * obj is not a BigInteger
257: */
258: public void testEqualsObject() {
259: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
260: 26, 3, 91 };
261: int aSign = 1;
262: BigInteger aNumber = new BigInteger(aSign, aBytes);
263: Object obj = new Object();
264: assertFalse(aNumber.equals(obj));
265: }
266:
267: /**
268: * equals(null).
269: */
270: public void testEqualsNull() {
271: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
272: 26, 3, 91 };
273: int aSign = 1;
274: BigInteger aNumber = new BigInteger(aSign, aBytes);
275: assertFalse(aNumber.equals(null));
276: }
277:
278: /**
279: * equals(Object obj).
280: * obj is a BigInteger.
281: * numbers are equal.
282: */
283: public void testEqualsBigIntegerTrue() {
284: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
285: 26, 3, 91 };
286: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
287: 26, 3, 91 };
288: int aSign = 1;
289: int bSign = 1;
290: BigInteger aNumber = new BigInteger(aSign, aBytes);
291: Object bNumber = new BigInteger(bSign, bBytes);
292: assertTrue(aNumber.equals(bNumber));
293: }
294:
295: /**
296: * equals(Object obj).
297: * obj is a BigInteger.
298: * numbers are not equal.
299: */
300: public void testEqualsBigIntegerFalse() {
301: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
302: 26, 3, 91 };
303: byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
304: int aSign = 1;
305: int bSign = 1;
306: BigInteger aNumber = new BigInteger(aSign, aBytes);
307: Object bNumber = new BigInteger(bSign, bBytes);
308: assertFalse(aNumber.equals(bNumber));
309: }
310:
311: /**
312: * max(BigInteger val).
313: * the first is greater.
314: */
315: public void testMaxGreater() {
316: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
317: 26, 3, 91 };
318: byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
319: int aSign = 1;
320: int bSign = 1;
321: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
322: 26, 3, 91 };
323: BigInteger aNumber = new BigInteger(aSign, aBytes);
324: BigInteger bNumber = new BigInteger(bSign, bBytes);
325: BigInteger result = aNumber.max(bNumber);
326: byte resBytes[] = new byte[rBytes.length];
327: resBytes = result.toByteArray();
328: for (int i = 0; i < resBytes.length; i++) {
329: assertTrue(resBytes[i] == rBytes[i]);
330: }
331: assertTrue("incorrect sign", result.signum() == 1);
332: }
333:
334: /**
335: * max(BigInteger val).
336: * the first is less.
337: */
338: public void testMaxLess() {
339: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
340: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
341: 26, 3, 91 };
342: int aSign = 1;
343: int bSign = 1;
344: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
345: 26, 3, 91 };
346: BigInteger aNumber = new BigInteger(aSign, aBytes);
347: BigInteger bNumber = new BigInteger(bSign, bBytes);
348: BigInteger result = aNumber.max(bNumber);
349: byte resBytes[] = new byte[rBytes.length];
350: resBytes = result.toByteArray();
351: for (int i = 0; i < resBytes.length; i++) {
352: assertTrue(resBytes[i] == rBytes[i]);
353: }
354: assertTrue("incorrect sign", result.signum() == 1);
355: }
356:
357: /**
358: * max(BigInteger val).
359: * numbers are equal.
360: */
361: public void testMaxEqual() {
362: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
363: byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
364: int aSign = 1;
365: int bSign = 1;
366: byte rBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
367: BigInteger aNumber = new BigInteger(aSign, aBytes);
368: BigInteger bNumber = new BigInteger(bSign, bBytes);
369: BigInteger result = aNumber.max(bNumber);
370: byte resBytes[] = new byte[rBytes.length];
371: resBytes = result.toByteArray();
372: for (int i = 0; i < resBytes.length; i++) {
373: assertTrue(resBytes[i] == rBytes[i]);
374: }
375: assertEquals("incorrect sign", 1, result.signum());
376: }
377:
378: /**
379: * max(BigInteger val).
380: * max of negative and ZERO.
381: */
382: public void testMaxNegZero() {
383: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
384: int aSign = -1;
385: byte rBytes[] = { 0 };
386: BigInteger aNumber = new BigInteger(aSign, aBytes);
387: BigInteger bNumber = BigInteger.ZERO;
388: BigInteger result = aNumber.max(bNumber);
389: byte resBytes[] = new byte[rBytes.length];
390: resBytes = result.toByteArray();
391: for (int i = 0; i < resBytes.length; i++) {
392: assertTrue(resBytes[i] == rBytes[i]);
393: }
394: assertTrue("incorrect sign", result.signum() == 0);
395: }
396:
397: /**
398: * min(BigInteger val).
399: * the first is greater.
400: */
401: public void testMinGreater() {
402: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
403: 26, 3, 91 };
404: byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
405: int aSign = 1;
406: int bSign = 1;
407: byte rBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
408: BigInteger aNumber = new BigInteger(aSign, aBytes);
409: BigInteger bNumber = new BigInteger(bSign, bBytes);
410: BigInteger result = aNumber.min(bNumber);
411: byte resBytes[] = new byte[rBytes.length];
412: resBytes = result.toByteArray();
413: for (int i = 0; i < resBytes.length; i++) {
414: assertTrue(resBytes[i] == rBytes[i]);
415: }
416: assertEquals("incorrect sign", 1, result.signum());
417: }
418:
419: /**
420: * min(BigInteger val).
421: * the first is less.
422: */
423: public void testMinLess() {
424: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
425: byte bBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
426: 26, 3, 91 };
427: int aSign = 1;
428: int bSign = 1;
429: byte rBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
430: BigInteger aNumber = new BigInteger(aSign, aBytes);
431: BigInteger bNumber = new BigInteger(bSign, bBytes);
432: BigInteger result = aNumber.min(bNumber);
433: byte resBytes[] = new byte[rBytes.length];
434: resBytes = result.toByteArray();
435: for (int i = 0; i < resBytes.length; i++) {
436: assertTrue(resBytes[i] == rBytes[i]);
437: }
438: assertEquals("incorrect sign", 1, result.signum());
439: }
440:
441: /**
442: * min(BigInteger val).
443: * numbers are equal.
444: */
445: public void testMinEqual() {
446: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
447: byte bBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
448: int aSign = 1;
449: int bSign = 1;
450: byte rBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
451: BigInteger aNumber = new BigInteger(aSign, aBytes);
452: BigInteger bNumber = new BigInteger(bSign, bBytes);
453: BigInteger result = aNumber.min(bNumber);
454: byte resBytes[] = new byte[rBytes.length];
455: resBytes = result.toByteArray();
456: for (int i = 0; i < resBytes.length; i++) {
457: assertTrue(resBytes[i] == rBytes[i]);
458: }
459: assertTrue("incorrect sign", result.signum() == 1);
460: }
461:
462: /**
463: * max(BigInteger val).
464: * min of positive and ZERO.
465: */
466: public void testMinPosZero() {
467: byte aBytes[] = { 45, 91, 3, -15, 35, 26, 3, 91 };
468: int aSign = 1;
469: byte rBytes[] = { 0 };
470: BigInteger aNumber = new BigInteger(aSign, aBytes);
471: BigInteger bNumber = BigInteger.ZERO;
472: BigInteger result = aNumber.min(bNumber);
473: byte resBytes[] = new byte[rBytes.length];
474: resBytes = result.toByteArray();
475: for (int i = 0; i < resBytes.length; i++) {
476: assertTrue(resBytes[i] == rBytes[i]);
477: }
478: assertTrue("incorrect sign", result.signum() == 0);
479: }
480:
481: /**
482: * negate() a positive number.
483: */
484: public void testNegatePositive() {
485: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
486: 26, 3, 91 };
487: int aSign = 1;
488: byte rBytes[] = { -13, -57, -101, 1, 75, -90, -46, -92, -4, 14,
489: -36, -27, -4, -91 };
490: BigInteger aNumber = new BigInteger(aSign, aBytes);
491: BigInteger result = aNumber.negate();
492: byte resBytes[] = new byte[rBytes.length];
493: resBytes = result.toByteArray();
494: for (int i = 0; i < resBytes.length; i++) {
495: assertTrue(resBytes[i] == rBytes[i]);
496: }
497: assertTrue("incorrect sign", result.signum() == -1);
498: }
499:
500: /**
501: * negate() a negative number.
502: */
503: public void testNegateNegative() {
504: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
505: 26, 3, 91 };
506: int aSign = -1;
507: byte rBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
508: 26, 3, 91 };
509: BigInteger aNumber = new BigInteger(aSign, aBytes);
510: BigInteger result = aNumber.negate();
511: byte resBytes[] = new byte[rBytes.length];
512: resBytes = result.toByteArray();
513: for (int i = 0; i < resBytes.length; i++) {
514: assertTrue(resBytes[i] == rBytes[i]);
515: }
516: assertTrue("incorrect sign", result.signum() == 1);
517: }
518:
519: /**
520: * negate() ZERO.
521: */
522: public void testNegateZero() {
523: byte rBytes[] = { 0 };
524: BigInteger aNumber = BigInteger.ZERO;
525: BigInteger result = aNumber.negate();
526: byte resBytes[] = new byte[rBytes.length];
527: resBytes = result.toByteArray();
528: for (int i = 0; i < resBytes.length; i++) {
529: assertTrue(resBytes[i] == rBytes[i]);
530: }
531: assertEquals("incorrect sign", 0, result.signum());
532: }
533:
534: /**
535: * signum() of a positive number.
536: */
537: public void testSignumPositive() {
538: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
539: 26, 3, 91 };
540: int aSign = 1;
541: BigInteger aNumber = new BigInteger(aSign, aBytes);
542: assertEquals("incorrect sign", 1, aNumber.signum());
543: }
544:
545: /**
546: * signum() of a negative number.
547: */
548: public void testSignumNegative() {
549: byte aBytes[] = { 12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35,
550: 26, 3, 91 };
551: int aSign = -1;
552: BigInteger aNumber = new BigInteger(aSign, aBytes);
553: assertEquals("incorrect sign", -1, aNumber.signum());
554: }
555:
556: /**
557: * signum() of ZERO.
558: */
559: public void testSignumZero() {
560: BigInteger aNumber = BigInteger.ZERO;
561: assertEquals("incorrect sign", 0, aNumber.signum());
562: }
563: }
|