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: and
028: */
029: public class BigIntegerAndTest extends TestCase {
030: /**
031: * And 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 };
040: BigInteger aNumber = new BigInteger(aSign, aBytes);
041: BigInteger bNumber = new BigInteger(bSign, bBytes);
042: BigInteger result = aNumber.and(bNumber);
043: byte resBytes[] = new byte[rBytes.length];
044: resBytes = result.toByteArray();
045: for (int i = 0; i < resBytes.length; i++) {
046: assertTrue(resBytes[i] == rBytes[i]);
047: }
048: assertEquals("incorrect sign", 0, result.signum());
049: }
050:
051: /**
052: * And for zero and a negative number
053: */
054: public void testZeroNeg() {
055: byte aBytes[] = { 0 };
056: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
057: 14, 23 };
058: int aSign = 0;
059: int bSign = -1;
060: byte rBytes[] = { 0 };
061: BigInteger aNumber = new BigInteger(aSign, aBytes);
062: BigInteger bNumber = new BigInteger(bSign, bBytes);
063: BigInteger result = aNumber.and(bNumber);
064: byte resBytes[] = new byte[rBytes.length];
065: resBytes = result.toByteArray();
066: for (int i = 0; i < resBytes.length; i++) {
067: assertTrue(resBytes[i] == rBytes[i]);
068: }
069: assertEquals("incorrect sign", 0, result.signum());
070: }
071:
072: /**
073: * And for a positive number and zero
074: */
075: public void testPosZero() {
076: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
077: 14, 23 };
078: byte bBytes[] = { 0 };
079: int aSign = 1;
080: int bSign = 0;
081: byte rBytes[] = { 0 };
082: BigInteger aNumber = new BigInteger(aSign, aBytes);
083: BigInteger bNumber = new BigInteger(bSign, bBytes);
084: BigInteger result = aNumber.and(bNumber);
085: byte resBytes[] = new byte[rBytes.length];
086: resBytes = result.toByteArray();
087: for (int i = 0; i < resBytes.length; i++) {
088: assertTrue(resBytes[i] == rBytes[i]);
089: }
090: assertEquals("incorrect sign", 0, result.signum());
091: }
092:
093: /**
094: * And for a negative number and zero
095: */
096: public void testNegPos() {
097: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
098: 14, 23 };
099: byte bBytes[] = { 0 };
100: int aSign = -1;
101: int bSign = 0;
102: byte rBytes[] = { 0 };
103: BigInteger aNumber = new BigInteger(aSign, aBytes);
104: BigInteger bNumber = new BigInteger(bSign, bBytes);
105: BigInteger result = aNumber.and(bNumber);
106: byte resBytes[] = new byte[rBytes.length];
107: resBytes = result.toByteArray();
108: for (int i = 0; i < resBytes.length; i++) {
109: assertTrue(resBytes[i] == rBytes[i]);
110: }
111: assertEquals("incorrect sign", 0, result.signum());
112: }
113:
114: /**
115: * And for zero and zero
116: */
117: public void testZeroZero() {
118: byte aBytes[] = { 0 };
119: byte bBytes[] = { 0 };
120: int aSign = 0;
121: int bSign = 0;
122: byte rBytes[] = { 0 };
123: BigInteger aNumber = new BigInteger(aSign, aBytes);
124: BigInteger bNumber = new BigInteger(bSign, bBytes);
125: BigInteger result = aNumber.and(bNumber);
126: byte resBytes[] = new byte[rBytes.length];
127: resBytes = result.toByteArray();
128: for (int i = 0; i < resBytes.length; i++) {
129: assertTrue(resBytes[i] == rBytes[i]);
130: }
131: assertEquals("incorrect sign", 0, result.signum());
132: }
133:
134: /**
135: * And for zero and one
136: */
137: public void testZeroOne() {
138: BigInteger aNumber = BigInteger.ZERO;
139: BigInteger bNumber = BigInteger.ONE;
140: BigInteger result = aNumber.and(bNumber);
141: assertTrue(result.equals(BigInteger.ZERO));
142: assertEquals("incorrect sign", 0, result.signum());
143: }
144:
145: /**
146: * And for one and one
147: */
148: public void testOneOne() {
149: BigInteger aNumber = BigInteger.ONE;
150: BigInteger bNumber = BigInteger.ONE;
151: BigInteger result = aNumber.and(bNumber);
152: assertTrue(result.equals(BigInteger.ONE));
153: assertEquals("incorrect sign", 1, result.signum());
154: }
155:
156: /**
157: * And for two positive numbers of the same length
158: */
159: public void testPosPosSameLength() {
160: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
161: 35, 26, -117 };
162: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
163: 14, 23 };
164: int aSign = 1;
165: int bSign = 1;
166: byte rBytes[] = { 0, -128, 56, 100, 4, 4, 17, 37, 16, 1, 64, 1,
167: 10, 3 };
168: BigInteger aNumber = new BigInteger(aSign, aBytes);
169: BigInteger bNumber = new BigInteger(bSign, bBytes);
170: BigInteger result = aNumber.and(bNumber);
171: byte resBytes[] = new byte[rBytes.length];
172: resBytes = result.toByteArray();
173: for (int i = 0; i < resBytes.length; i++) {
174: assertTrue(resBytes[i] == rBytes[i]);
175: }
176: assertEquals("incorrect sign", 1, result.signum());
177: }
178:
179: /**
180: * And for two positive numbers; the first is longer
181: */
182: public void testPosPosFirstLonger() {
183: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
184: -15, 35, 26, -117, 23, 87, -25, -75 };
185: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
186: 14, 23 };
187: int aSign = 1;
188: int bSign = 1;
189: byte rBytes[] = { 0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2,
190: 5, 6, 21 };
191: BigInteger aNumber = new BigInteger(aSign, aBytes);
192: BigInteger bNumber = new BigInteger(bSign, bBytes);
193: BigInteger result = aNumber.and(bNumber);
194: byte resBytes[] = new byte[rBytes.length];
195: resBytes = result.toByteArray();
196: for (int i = 0; i < resBytes.length; i++) {
197: assertTrue(resBytes[i] == rBytes[i]);
198: }
199: assertEquals("incorrect sign", 1, result.signum());
200: }
201:
202: /**
203: * And for two positive numbers; the first is shorter
204: */
205: public void testPosPosFirstShorter() {
206: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
207: 14, 23 };
208: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
209: -15, 35, 26, -117, 23, 87, -25, -75 };
210: int aSign = 1;
211: int bSign = 1;
212: byte rBytes[] = { 0, -2, -76, 88, 44, 1, 2, 17, 35, 16, 9, 2,
213: 5, 6, 21 };
214: BigInteger aNumber = new BigInteger(aSign, aBytes);
215: BigInteger bNumber = new BigInteger(bSign, bBytes);
216: BigInteger result = aNumber.and(bNumber);
217: byte resBytes[] = new byte[rBytes.length];
218: resBytes = result.toByteArray();
219: for (int i = 0; i < resBytes.length; i++) {
220: assertTrue(resBytes[i] == rBytes[i]);
221: }
222: assertEquals("incorrect sign", 1, result.signum());
223: }
224:
225: /**
226: * And for two negative numbers of the same length
227: */
228: public void testNegNegSameLength() {
229: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
230: 35, 26, -117 };
231: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
232: 14, 23 };
233: int aSign = -1;
234: int bSign = -1;
235: byte rBytes[] = { -1, 1, 2, 3, 3, 0, 65, -96, -48, -124, -60,
236: 12, -40, -31, 97 };
237: BigInteger aNumber = new BigInteger(aSign, aBytes);
238: BigInteger bNumber = new BigInteger(bSign, bBytes);
239: BigInteger result = aNumber.and(bNumber);
240: byte resBytes[] = new byte[rBytes.length];
241: resBytes = result.toByteArray();
242: for (int i = 0; i < resBytes.length; i++) {
243: assertTrue(resBytes[i] == rBytes[i]);
244: }
245: assertEquals("incorrect sign", -1, result.signum());
246: }
247:
248: /**
249: * And for two negative numbers; the first is longer
250: */
251: public void testNegNegFirstLonger() {
252: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
253: -15, 35, 26, -117, 23, 87, -25, -75 };
254: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
255: 14, 23 };
256: int aSign = -1;
257: int bSign = -1;
258: byte rBytes[] = { -1, 127, -10, -57, -101, 1, 2, 2, 2, -96,
259: -16, 8, -40, -59, 68, -88, -88, 16, 73 };
260: BigInteger aNumber = new BigInteger(aSign, aBytes);
261: BigInteger bNumber = new BigInteger(bSign, bBytes);
262: BigInteger result = aNumber.and(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", -1, result.signum());
269: }
270:
271: /**
272: * And for two negative numbers; the first is shorter
273: */
274: public void testNegNegFirstShorter() {
275: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
276: 14, 23 };
277: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
278: -15, 35, 26, -117, 23, 87, -25, -75 };
279: int aSign = -1;
280: int bSign = -1;
281: byte rBytes[] = { -1, 127, -10, -57, -101, 1, 2, 2, 2, -96,
282: -16, 8, -40, -59, 68, -88, -88, 16, 73 };
283: BigInteger aNumber = new BigInteger(aSign, aBytes);
284: BigInteger bNumber = new BigInteger(bSign, bBytes);
285: BigInteger result = aNumber.and(bNumber);
286: byte resBytes[] = new byte[rBytes.length];
287: resBytes = result.toByteArray();
288: for (int i = 0; i < resBytes.length; i++) {
289: assertTrue(resBytes[i] == rBytes[i]);
290: }
291: assertEquals("incorrect sign", -1, result.signum());
292: }
293:
294: /**
295: * And for two numbers of different signs and the same length
296: */
297: public void testPosNegSameLength() {
298: byte aBytes[] = { -128, 56, 100, -2, -76, 89, 45, 91, 3, -15,
299: 35, 26, -117 };
300: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
301: 14, 23 };
302: int aSign = 1;
303: int bSign = -1;
304: byte rBytes[] = { 0, -6, -80, 72, 8, 75, 2, -79, 34, 16, -119 };
305: BigInteger aNumber = new BigInteger(aSign, aBytes);
306: BigInteger bNumber = new BigInteger(bSign, bBytes);
307: BigInteger result = aNumber.and(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: * And for two numbers of different signs and the same length
318: */
319: public void testNegPosSameLength() {
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[] = { 0, -2, 125, -60, -104, 1, 10, 6, 2, 32, 56,
327: 2, 4, 4, 21 };
328: BigInteger aNumber = new BigInteger(aSign, aBytes);
329: BigInteger bNumber = new BigInteger(bSign, bBytes);
330: BigInteger result = aNumber.and(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: * And for a negative and a positive numbers; the first is longer
341: */
342: public void testNegPosFirstLonger() {
343: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
344: -15, 35, 26, -117, 23, 87, -25, -75 };
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[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8,
350: 3 };
351: BigInteger aNumber = new BigInteger(aSign, aBytes);
352: BigInteger bNumber = new BigInteger(bSign, bBytes);
353: BigInteger result = aNumber.and(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: * And for a negative and a positive numbers; the first is shorter
364: */
365: public void testNegPosFirstShorter() {
366: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
367: 14, 23 };
368: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
369: -15, 35, 26, -117, 23, 87, -25, -75 };
370: int aSign = -1;
371: int bSign = 1;
372: byte rBytes[] = { 0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32,
373: 0, 10, -126, 21, 82, -31, -95 };
374: BigInteger aNumber = new BigInteger(aSign, aBytes);
375: BigInteger bNumber = new BigInteger(bSign, bBytes);
376: BigInteger result = aNumber.and(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: * And for a positive and a negative numbers; the first is longer
387: */
388: public void testPosNegFirstLonger() {
389: byte aBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
390: -15, 35, 26, -117, 23, 87, -25, -75 };
391: byte bBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
392: 14, 23 };
393: int aSign = 1;
394: int bSign = -1;
395: byte rBytes[] = { 0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32,
396: 0, 10, -126, 21, 82, -31, -95 };
397: BigInteger aNumber = new BigInteger(aSign, aBytes);
398: BigInteger bNumber = new BigInteger(bSign, bBytes);
399: BigInteger result = aNumber.and(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: * And for a positive and a negative numbers; the first is shorter
410: */
411: public void testPosNegFirstShorter() {
412: byte aBytes[] = { -2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5,
413: 14, 23 };
414: byte bBytes[] = { -128, 9, 56, 100, -2, -76, 89, 45, 91, 3,
415: -15, 35, 26, -117, 23, 87, -25, -75 };
416: int aSign = 1;
417: int bSign = -1;
418: byte rBytes[] = { 73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8,
419: 3 };
420: BigInteger aNumber = new BigInteger(aSign, aBytes);
421: BigInteger bNumber = new BigInteger(bSign, bBytes);
422: BigInteger result = aNumber.and(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: * Test for a special case
433: */
434: public void testSpecialCase1() {
435: byte aBytes[] = { -1, -1, -1, -1 };
436: byte bBytes[] = { 5, -4, -3, -2 };
437: int aSign = -1;
438: int bSign = -1;
439: byte rBytes[] = { -1, 0, 0, 0, 0 };
440: BigInteger aNumber = new BigInteger(aSign, aBytes);
441: BigInteger bNumber = new BigInteger(bSign, bBytes);
442: BigInteger result = aNumber.and(bNumber);
443: byte resBytes[] = new byte[rBytes.length];
444: resBytes = result.toByteArray();
445: for (int i = 0; i < resBytes.length; i++) {
446: assertTrue(resBytes[i] == rBytes[i]);
447: }
448: assertEquals("incorrect sign", -1, result.signum());
449: }
450:
451: /**
452: * Test for a special case
453: */
454: public void testSpecialCase2() {
455: byte aBytes[] = { -51 };
456: byte bBytes[] = { -52, -51, -50, -49, -48 };
457: int aSign = -1;
458: int bSign = 1;
459: byte rBytes[] = { 0, -52, -51, -50, -49, 16 };
460: BigInteger aNumber = new BigInteger(aSign, aBytes);
461: BigInteger bNumber = new BigInteger(bSign, bBytes);
462: BigInteger result = aNumber.and(bNumber);
463: byte resBytes[] = new byte[rBytes.length];
464: resBytes = result.toByteArray();
465: for (int i = 0; i < resBytes.length; i++) {
466: assertTrue(resBytes[i] == rBytes[i]);
467: }
468: assertEquals("incorrect sign", 1, result.signum());
469: }
470: }
|