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: divide, remainder, mod, and divideAndRemainder
028: */
029: public class BigIntegerDivideTest extends TestCase {
030: /**
031: * Divide by zero
032: */
033: public void testCase1() {
034: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
035: byte bBytes[] = { 0 };
036: int aSign = 1;
037: int bSign = 0;
038: BigInteger aNumber = new BigInteger(aSign, aBytes);
039: BigInteger bNumber = new BigInteger(bSign, bBytes);
040: try {
041: aNumber.divide(bNumber);
042: fail("ArithmeticException has not been caught");
043: } catch (ArithmeticException e) {
044: assertEquals("Improper exception message",
045: "BigInteger divide by zero", e.getMessage());
046: }
047: }
048:
049: /**
050: * Divide by ZERO
051: */
052: public void testCase2() {
053: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
054: int aSign = 1;
055: BigInteger aNumber = new BigInteger(aSign, aBytes);
056: BigInteger bNumber = BigInteger.ZERO;
057: try {
058: aNumber.divide(bNumber);
059: fail("ArithmeticException has not been caught");
060: } catch (ArithmeticException e) {
061: assertEquals("Improper exception message",
062: "BigInteger divide by zero", e.getMessage());
063: }
064: }
065:
066: /**
067: * Divide two equal positive numbers
068: */
069: public void testCase3() {
070: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
071: byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
072: int aSign = 1;
073: int bSign = 1;
074: byte rBytes[] = { 1 };
075: BigInteger aNumber = new BigInteger(aSign, aBytes);
076: BigInteger bNumber = new BigInteger(bSign, bBytes);
077: BigInteger result = aNumber.divide(bNumber);
078: byte resBytes[] = new byte[rBytes.length];
079: resBytes = result.toByteArray();
080: for (int i = 0; i < resBytes.length; i++) {
081: assertTrue(resBytes[i] == rBytes[i]);
082: }
083: assertEquals("incorrect sign", 1, result.signum());
084: }
085:
086: /**
087: * Divide two equal in absolute value numbers of different signs.
088: */
089: public void testCase4() {
090: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
091: byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
092: int aSign = -1;
093: int bSign = 1;
094: byte rBytes[] = { -1 };
095: BigInteger aNumber = new BigInteger(aSign, aBytes);
096: BigInteger bNumber = new BigInteger(bSign, bBytes);
097: BigInteger result = aNumber.divide(bNumber);
098: byte resBytes[] = new byte[rBytes.length];
099: resBytes = result.toByteArray();
100: for (int i = 0; i < resBytes.length; i++) {
101: assertTrue(resBytes[i] == rBytes[i]);
102: }
103: assertEquals("incorrect sign", -1, result.signum());
104: }
105:
106: /**
107: * Divide two numbers of different length and different signs.
108: * The second is longer.
109: */
110: public void testCase5() {
111: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
112: byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 1,
113: 2, 3, 4, 5 };
114: int aSign = -1;
115: int bSign = 1;
116: byte rBytes[] = { 0 };
117: BigInteger aNumber = new BigInteger(aSign, aBytes);
118: BigInteger bNumber = new BigInteger(bSign, bBytes);
119: BigInteger result = aNumber.divide(bNumber);
120: byte resBytes[] = new byte[rBytes.length];
121: resBytes = result.toByteArray();
122: for (int i = 0; i < resBytes.length; i++) {
123: assertTrue(resBytes[i] == rBytes[i]);
124: }
125: assertEquals("incorrect sign", 0, result.signum());
126: }
127:
128: /**
129: * Divide two positive numbers of the same length.
130: * The second is greater.
131: */
132: public void testCase6() {
133: byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127 };
134: byte bBytes[] = { 15, 100, 56, 7, 98, -1, 39, -128, 127 };
135: int aSign = 1;
136: int bSign = 1;
137: byte rBytes[] = { 0 };
138: BigInteger aNumber = new BigInteger(aSign, aBytes);
139: BigInteger bNumber = new BigInteger(bSign, bBytes);
140: BigInteger result = aNumber.divide(bNumber);
141: byte resBytes[] = new byte[rBytes.length];
142: resBytes = result.toByteArray();
143: for (int i = 0; i < resBytes.length; i++) {
144: assertTrue(resBytes[i] == rBytes[i]);
145: }
146: assertEquals("incorrect sign", 0, result.signum());
147: }
148:
149: /**
150: * Divide two positive numbers.
151: */
152: public void testCase7() {
153: byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6,
154: 7, 8, 9 };
155: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
156: int aSign = 1;
157: int bSign = 1;
158: byte rBytes[] = { 23, 115, 11, 78, 35, -11 };
159: BigInteger aNumber = new BigInteger(aSign, aBytes);
160: BigInteger bNumber = new BigInteger(bSign, bBytes);
161: BigInteger result = aNumber.divide(bNumber);
162: byte resBytes[] = new byte[rBytes.length];
163: resBytes = result.toByteArray();
164: for (int i = 0; i < resBytes.length; i++) {
165: assertTrue(resBytes[i] == rBytes[i]);
166: }
167: assertEquals("incorrect sign", 1, result.signum());
168: }
169:
170: /**
171: * Divide a positive number by a negative one.
172: */
173: public void testCase8() {
174: byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6,
175: 7, 8, 9 };
176: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
177: int aSign = 1;
178: int bSign = -1;
179: byte rBytes[] = { -24, -116, -12, -79, -36, 11 };
180: BigInteger aNumber = new BigInteger(aSign, aBytes);
181: BigInteger bNumber = new BigInteger(bSign, bBytes);
182: BigInteger result = aNumber.divide(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: * Divide a negative number by a positive one.
193: */
194: public void testCase9() {
195: byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6,
196: 7, 8, 9 };
197: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
198: int aSign = -1;
199: int bSign = 1;
200: byte rBytes[] = { -24, -116, -12, -79, -36, 11 };
201: BigInteger aNumber = new BigInteger(aSign, aBytes);
202: BigInteger bNumber = new BigInteger(bSign, bBytes);
203: BigInteger result = aNumber.divide(bNumber);
204: byte resBytes[] = new byte[rBytes.length];
205: resBytes = result.toByteArray();
206: for (int i = 0; i < resBytes.length; i++) {
207: assertTrue(resBytes[i] == rBytes[i]);
208: }
209: assertEquals("incorrect sign", -1, result.signum());
210: }
211:
212: /**
213: * Divide two negative numbers.
214: */
215: public void testCase10() {
216: byte aBytes[] = { 1, 100, 56, 7, 98, -1, 39, -128, 127, 5, 6,
217: 7, 8, 9 };
218: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
219: int aSign = -1;
220: int bSign = -1;
221: byte rBytes[] = { 23, 115, 11, 78, 35, -11 };
222: BigInteger aNumber = new BigInteger(aSign, aBytes);
223: BigInteger bNumber = new BigInteger(bSign, bBytes);
224: BigInteger result = aNumber.divide(bNumber);
225: byte resBytes[] = new byte[rBytes.length];
226: resBytes = result.toByteArray();
227: for (int i = 0; i < resBytes.length; i++) {
228: assertTrue(resBytes[i] == rBytes[i]);
229: }
230: assertEquals("incorrect sign", 1, result.signum());
231: }
232:
233: /**
234: * Divide zero by a negative number.
235: */
236: public void testCase11() {
237: byte aBytes[] = { 0 };
238: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
239: int aSign = 0;
240: int bSign = -1;
241: byte rBytes[] = { 0 };
242: BigInteger aNumber = new BigInteger(aSign, aBytes);
243: BigInteger bNumber = new BigInteger(bSign, bBytes);
244: BigInteger result = aNumber.divide(bNumber);
245: byte resBytes[] = new byte[rBytes.length];
246: resBytes = result.toByteArray();
247: for (int i = 0; i < resBytes.length; i++) {
248: assertTrue(resBytes[i] == rBytes[i]);
249: }
250: assertEquals("incorrect sign", 0, result.signum());
251: }
252:
253: /**
254: * Divide ZERO by a negative number.
255: */
256: public void testCase12() {
257: byte bBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
258: int bSign = -1;
259: byte rBytes[] = { 0 };
260: BigInteger aNumber = BigInteger.ZERO;
261: BigInteger bNumber = new BigInteger(bSign, bBytes);
262: BigInteger result = aNumber.divide(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", 0, result.signum());
269: }
270:
271: /**
272: * Divide a positive number by ONE.
273: */
274: public void testCase13() {
275: byte aBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
276: int aSign = 1;
277: byte rBytes[] = { 15, 48, -29, 7, 98, -1, 39, -128 };
278: BigInteger aNumber = new BigInteger(aSign, aBytes);
279: BigInteger bNumber = BigInteger.ONE;
280: BigInteger result = aNumber.divide(bNumber);
281: byte resBytes[] = new byte[rBytes.length];
282: resBytes = result.toByteArray();
283: for (int i = 0; i < resBytes.length; i++) {
284: assertTrue(resBytes[i] == rBytes[i]);
285: }
286: assertEquals("incorrect sign", 1, result.signum());
287: }
288:
289: /**
290: * Divide ONE by ONE.
291: */
292: public void testCase14() {
293: byte rBytes[] = { 1 };
294: BigInteger aNumber = BigInteger.ONE;
295: BigInteger bNumber = BigInteger.ONE;
296: BigInteger result = aNumber.divide(bNumber);
297: byte resBytes[] = new byte[rBytes.length];
298: resBytes = result.toByteArray();
299: for (int i = 0; i < resBytes.length; i++) {
300: assertTrue(resBytes[i] == rBytes[i]);
301: }
302: assertEquals("incorrect sign", 1, result.signum());
303: }
304:
305: /**
306: * Verifies the case when borrow != 0 in the private divide method.
307: */
308: public void testDivisionKnuth1() {
309: byte aBytes[] = { -7, -6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5,
310: 6, 7 };
311: byte bBytes[] = { -3, -3, -3, -3 };
312: int aSign = 1;
313: int bSign = 1;
314: byte rBytes[] = { 0, -5, -12, -33, -96, -36, -105, -56, 92, 15,
315: 48, -109 };
316: BigInteger aNumber = new BigInteger(aSign, aBytes);
317: BigInteger bNumber = new BigInteger(bSign, bBytes);
318: BigInteger result = aNumber.divide(bNumber);
319: byte resBytes[] = new byte[rBytes.length];
320: resBytes = result.toByteArray();
321: for (int i = 0; i < resBytes.length; i++) {
322: assertTrue(resBytes[i] == rBytes[i]);
323: }
324: assertEquals("incorrect sign", 1, result.signum());
325: }
326:
327: /**
328: * Verifies the case when the divisor is already normalized.
329: */
330: public void testDivisionKnuthIsNormalized() {
331: byte aBytes[] = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1, 2,
332: 3, 4, 5 };
333: byte bBytes[] = { -1, -1, -1, -1, -1, -1, -1, -1 };
334: int aSign = -1;
335: int bSign = -1;
336: byte rBytes[] = { 0, -9, -8, -7, -6, -5, -4, -3 };
337: BigInteger aNumber = new BigInteger(aSign, aBytes);
338: BigInteger bNumber = new BigInteger(bSign, bBytes);
339: BigInteger result = aNumber.divide(bNumber);
340: byte resBytes[] = new byte[rBytes.length];
341: resBytes = result.toByteArray();
342: for (int i = 0; i < resBytes.length; i++) {
343: assertTrue(resBytes[i] == rBytes[i]);
344: }
345: assertEquals("incorrect sign", 1, result.signum());
346: }
347:
348: /**
349: * Verifies the case when the first digits of the dividend
350: * and divisor equal.
351: */
352: public void testDivisionKnuthFirstDigitsEqual() {
353: byte aBytes[] = { 2, -3, -4, -5, -1, -5, -4, -3, -2, -1, 0, 1,
354: 2, 3, 4, 5 };
355: byte bBytes[] = { 2, -3, -4, -5, -1, -1, -1, -1 };
356: int aSign = -1;
357: int bSign = -1;
358: byte rBytes[] = { 0, -1, -1, -1, -1, -2, -88, -60, 41 };
359: BigInteger aNumber = new BigInteger(aSign, aBytes);
360: BigInteger bNumber = new BigInteger(bSign, bBytes);
361: BigInteger result = aNumber.divide(bNumber);
362: byte resBytes[] = new byte[rBytes.length];
363: resBytes = result.toByteArray();
364: for (int i = 0; i < resBytes.length; i++) {
365: assertTrue(resBytes[i] == rBytes[i]);
366: }
367: assertEquals("incorrect sign", 1, result.signum());
368: }
369:
370: /**
371: * Divide the number of one digit by the number of one digit
372: */
373: public void testDivisionKnuthOneDigitByOneDigit() {
374: byte aBytes[] = { 113, -83, 123, -5 };
375: byte bBytes[] = { 2, -3, -4, -5 };
376: int aSign = 1;
377: int bSign = -1;
378: byte rBytes[] = { -37 };
379: BigInteger aNumber = new BigInteger(aSign, aBytes);
380: BigInteger bNumber = new BigInteger(bSign, bBytes);
381: BigInteger result = aNumber.divide(bNumber);
382: byte resBytes[] = new byte[rBytes.length];
383: resBytes = result.toByteArray();
384: for (int i = 0; i < resBytes.length; i++) {
385: assertTrue(resBytes[i] == rBytes[i]);
386: }
387: assertEquals("incorrect sign", -1, result.signum());
388: }
389:
390: /**
391: * Divide the number of multi digits by the number of one digit
392: */
393: public void testDivisionKnuthMultiDigitsByOneDigit() {
394: byte aBytes[] = { 113, -83, 123, -5, 18, -34, 67, 39, -29 };
395: byte bBytes[] = { 2, -3, -4, -5 };
396: int aSign = 1;
397: int bSign = -1;
398: byte rBytes[] = { -38, 2, 7, 30, 109, -43 };
399: BigInteger aNumber = new BigInteger(aSign, aBytes);
400: BigInteger bNumber = new BigInteger(bSign, bBytes);
401: BigInteger result = aNumber.divide(bNumber);
402: byte resBytes[] = new byte[rBytes.length];
403: resBytes = result.toByteArray();
404: for (int i = 0; i < resBytes.length; i++) {
405: assertTrue(resBytes[i] == rBytes[i]);
406: }
407: assertEquals("incorrect sign", -1, result.signum());
408: }
409:
410: /**
411: * Remainder of division by zero
412: */
413: public void testCase15() {
414: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
415: byte bBytes[] = { 0 };
416: int aSign = 1;
417: int bSign = 0;
418: BigInteger aNumber = new BigInteger(aSign, aBytes);
419: BigInteger bNumber = new BigInteger(bSign, bBytes);
420: try {
421: aNumber.remainder(bNumber);
422: fail("ArithmeticException has not been caught");
423: } catch (ArithmeticException e) {
424: assertEquals("Improper exception message",
425: "BigInteger divide by zero", e.getMessage());
426: }
427: }
428:
429: /**
430: * Remainder of division of equal numbers
431: */
432: public void testCase16() {
433: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
434: byte bBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127 };
435: int aSign = 1;
436: int bSign = 1;
437: byte rBytes[] = { 0 };
438: BigInteger aNumber = new BigInteger(aSign, aBytes);
439: BigInteger bNumber = new BigInteger(bSign, bBytes);
440: BigInteger result = aNumber.remainder(bNumber);
441: byte resBytes[] = new byte[rBytes.length];
442: resBytes = result.toByteArray();
443: for (int i = 0; i < resBytes.length; i++) {
444: assertTrue(resBytes[i] == rBytes[i]);
445: }
446: assertEquals("incorrect sign", 0, result.signum());
447: }
448:
449: /**
450: * Remainder of division of two positive numbers
451: */
452: public void testCase17() {
453: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
454: byte bBytes[] = { 27, -15, 65, 39, 100 };
455: int aSign = 1;
456: int bSign = 1;
457: byte rBytes[] = { 12, -21, 73, 56, 27 };
458: BigInteger aNumber = new BigInteger(aSign, aBytes);
459: BigInteger bNumber = new BigInteger(bSign, bBytes);
460: BigInteger result = aNumber.remainder(bNumber);
461: byte resBytes[] = new byte[rBytes.length];
462: resBytes = result.toByteArray();
463: for (int i = 0; i < resBytes.length; i++) {
464: assertTrue(resBytes[i] == rBytes[i]);
465: }
466: assertEquals("incorrect sign", 1, result.signum());
467: }
468:
469: /**
470: * Remainder of division of two negative numbers
471: */
472: public void testCase18() {
473: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
474: byte bBytes[] = { 27, -15, 65, 39, 100 };
475: int aSign = -1;
476: int bSign = -1;
477: byte rBytes[] = { -13, 20, -74, -57, -27 };
478: BigInteger aNumber = new BigInteger(aSign, aBytes);
479: BigInteger bNumber = new BigInteger(bSign, bBytes);
480: BigInteger result = aNumber.remainder(bNumber);
481: byte resBytes[] = new byte[rBytes.length];
482: resBytes = result.toByteArray();
483: for (int i = 0; i < resBytes.length; i++) {
484: assertTrue(resBytes[i] == rBytes[i]);
485: }
486: assertEquals("incorrect sign", -1, result.signum());
487: }
488:
489: /**
490: * Remainder of division of two numbers of different signs.
491: * The first is positive.
492: */
493: public void testCase19() {
494: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
495: byte bBytes[] = { 27, -15, 65, 39, 100 };
496: int aSign = 1;
497: int bSign = -1;
498: byte rBytes[] = { 12, -21, 73, 56, 27 };
499: BigInteger aNumber = new BigInteger(aSign, aBytes);
500: BigInteger bNumber = new BigInteger(bSign, bBytes);
501: BigInteger result = aNumber.remainder(bNumber);
502: byte resBytes[] = new byte[rBytes.length];
503: resBytes = result.toByteArray();
504: for (int i = 0; i < resBytes.length; i++) {
505: assertTrue(resBytes[i] == rBytes[i]);
506: }
507: assertEquals("incorrect sign", 1, result.signum());
508: }
509:
510: /**
511: * Remainder of division of two numbers of different signs.
512: * The first is negative.
513: */
514: public void testCase20() {
515: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
516: byte bBytes[] = { 27, -15, 65, 39, 100 };
517: int aSign = -1;
518: int bSign = 1;
519: byte rBytes[] = { -13, 20, -74, -57, -27 };
520: BigInteger aNumber = new BigInteger(aSign, aBytes);
521: BigInteger bNumber = new BigInteger(bSign, bBytes);
522: BigInteger result = aNumber.remainder(bNumber);
523: byte resBytes[] = new byte[rBytes.length];
524: resBytes = result.toByteArray();
525: for (int i = 0; i < resBytes.length; i++) {
526: assertTrue(resBytes[i] == rBytes[i]);
527: }
528: assertEquals("incorrect sign", -1, result.signum());
529: }
530:
531: /**
532: * Tests the step D6 from the Knuth algorithm
533: */
534: public void testRemainderKnuth1() {
535: byte aBytes[] = { -9, -8, -7, -6, -5, -4, -3, -2, -1, 0, 1 };
536: byte bBytes[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
537: int aSign = 1;
538: int bSign = 1;
539: byte rBytes[] = { 1, 2, 3, 4, 5, 6, 7, 7, 18, -89 };
540: BigInteger aNumber = new BigInteger(aSign, aBytes);
541: BigInteger bNumber = new BigInteger(bSign, bBytes);
542: BigInteger result = aNumber.remainder(bNumber);
543: byte resBytes[] = new byte[rBytes.length];
544: resBytes = result.toByteArray();
545: for (int i = 0; i < resBytes.length; i++) {
546: assertTrue(resBytes[i] == rBytes[i]);
547: }
548: assertEquals("incorrect sign", 1, result.signum());
549: }
550:
551: /**
552: * Divide the number of one digit by the number of one digit
553: */
554: public void testRemainderKnuthOneDigitByOneDigit() {
555: byte aBytes[] = { 113, -83, 123, -5 };
556: byte bBytes[] = { 2, -3, -4, -50 };
557: int aSign = 1;
558: int bSign = -1;
559: byte rBytes[] = { 2, -9, -14, 53 };
560: BigInteger aNumber = new BigInteger(aSign, aBytes);
561: BigInteger bNumber = new BigInteger(bSign, bBytes);
562: BigInteger result = aNumber.remainder(bNumber);
563: byte resBytes[] = new byte[rBytes.length];
564: resBytes = result.toByteArray();
565: for (int i = 0; i < resBytes.length; i++) {
566: assertTrue(resBytes[i] == rBytes[i]);
567: }
568: assertEquals("incorrect sign", 1, result.signum());
569: }
570:
571: /**
572: * Divide the number of multi digits by the number of one digit
573: */
574: public void testRemainderKnuthMultiDigitsByOneDigit() {
575: byte aBytes[] = { 113, -83, 123, -5, 18, -34, 67, 39, -29 };
576: byte bBytes[] = { 2, -3, -4, -50 };
577: int aSign = 1;
578: int bSign = -1;
579: byte rBytes[] = { 2, -37, -60, 59 };
580: BigInteger aNumber = new BigInteger(aSign, aBytes);
581: BigInteger bNumber = new BigInteger(bSign, bBytes);
582: BigInteger result = aNumber.remainder(bNumber);
583: byte resBytes[] = new byte[rBytes.length];
584: resBytes = result.toByteArray();
585: for (int i = 0; i < resBytes.length; i++) {
586: assertTrue(resBytes[i] == rBytes[i]);
587: }
588: assertEquals("incorrect sign", 1, result.signum());
589: }
590:
591: /**
592: * divideAndRemainder of two numbers of different signs.
593: * The first is negative.
594: */
595: public void testCase21() {
596: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
597: byte bBytes[] = { 27, -15, 65, 39, 100 };
598: int aSign = -1;
599: int bSign = 1;
600: byte rBytes[][] = { { -5, 94, -115, -74, -85, 84 },
601: { -13, 20, -74, -57, -27 } };
602: BigInteger aNumber = new BigInteger(aSign, aBytes);
603: BigInteger bNumber = new BigInteger(bSign, bBytes);
604: BigInteger result[] = aNumber.divideAndRemainder(bNumber);
605: byte resBytes[] = new byte[rBytes.length];
606: resBytes = result[0].toByteArray();
607: for (int i = 0; i < resBytes.length; i++) {
608: if (resBytes[i] != rBytes[0][i]) {
609: fail("Incorrect quotation");
610: }
611: }
612: assertEquals(-1, result[0].signum());
613: resBytes = result[1].toByteArray();
614: for (int i = 0; i < resBytes.length; i++) {
615: if (resBytes[i] != rBytes[1][i]) {
616: fail("Incorrect remainder");
617: }
618: assertEquals(-1, result[1].signum());
619: }
620: }
621:
622: /**
623: * mod when modulus is negative
624: */
625: public void testCase22() {
626: byte aBytes[] = { 1, 2, 3, 4, 5, 6, 7 };
627: byte bBytes[] = { 1, 30, 40, 56, -1, 45 };
628: int aSign = 1;
629: int bSign = -1;
630: BigInteger aNumber = new BigInteger(aSign, aBytes);
631: BigInteger bNumber = new BigInteger(bSign, bBytes);
632: try {
633: aNumber.mod(bNumber);
634: fail("ArithmeticException has not been caught");
635: } catch (ArithmeticException e) {
636: assertEquals("Improper exception message",
637: "BigInteger: modulus not positive", e.getMessage());
638: }
639: }
640:
641: /**
642: * mod when a divisor is positive
643: */
644: public void testCase23() {
645: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
646: byte bBytes[] = { 27, -15, 65, 39, 100 };
647: int aSign = 1;
648: int bSign = 1;
649: byte rBytes[] = { 12, -21, 73, 56, 27 };
650: BigInteger aNumber = new BigInteger(aSign, aBytes);
651: BigInteger bNumber = new BigInteger(bSign, bBytes);
652: BigInteger result = aNumber.mod(bNumber);
653: byte resBytes[] = new byte[rBytes.length];
654: resBytes = result.toByteArray();
655: for (int i = 0; i < resBytes.length; i++) {
656: assertTrue(resBytes[i] == rBytes[i]);
657: }
658: assertEquals("incorrect sign", 1, result.signum());
659: }
660:
661: /**
662: * mod when a divisor is negative
663: */
664: public void testCase24() {
665: byte aBytes[] = { -127, 100, 56, 7, 98, -1, 39, -128, 127, 75 };
666: byte bBytes[] = { 27, -15, 65, 39, 100 };
667: int aSign = -1;
668: int bSign = 1;
669: byte rBytes[] = { 15, 5, -9, -17, 73 };
670: BigInteger aNumber = new BigInteger(aSign, aBytes);
671: BigInteger bNumber = new BigInteger(bSign, bBytes);
672: BigInteger result = aNumber.mod(bNumber);
673: byte resBytes[] = new byte[rBytes.length];
674: resBytes = result.toByteArray();
675: for (int i = 0; i < resBytes.length; i++) {
676: assertTrue(resBytes[i] == rBytes[i]);
677: }
678: assertEquals("incorrect sign", 1, result.signum());
679: }
680: }
|