1/*
2 *  Licensed to the Apache Software Foundation (ASF) under one or more
3 *  contributor license agreements.  See the NOTICE file distributed with
4 *  this work for additional information regarding copyright ownership.
5 *  The ASF licenses this file to You under the Apache License, Version 2.0
6 *  (the "License"); you may not use this file except in compliance with
7 *  the License.  You may obtain a copy of the License at
8 *
9 *     http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *  Unless required by applicable law or agreed to in writing, software
12 *  distributed under the License is distributed on an "AS IS" BASIS,
13 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  See the License for the specific language governing permissions and
15 *  limitations under the License.
16 */
17/**
18 * @author Elena Semukhina
19 */
20
21package org.apache.harmony.tests.java.math;
22
23import junit.framework.TestCase;
24import java.math.BigInteger;
25
26/**
27 * Class:  java.math.BigInteger
28 * Methods: and, andNot
29 */
30public class BigIntegerNotTest extends TestCase {
31    /**
32     * andNot for two positive numbers; the first is longer
33     */
34    public void testAndNotPosPosFirstLonger() {
35        byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
36        byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
37        int aSign = 1;
38        int bSign = 1;
39        byte rBytes[] = {0, -128, 9, 56, 100, 0, 0, 1, 1, 90, 1, -32, 0, 10, -126, 21, 82, -31, -96};
40        BigInteger aNumber = new BigInteger(aSign, aBytes);
41        BigInteger bNumber = new BigInteger(bSign, bBytes);
42        BigInteger result = aNumber.andNot(bNumber);
43        byte resBytes[] = new byte[rBytes.length];
44        resBytes = result.toByteArray();
45        for(int i = 0; i < resBytes.length; i++) {
46            assertTrue(resBytes[i] == rBytes[i]);
47        }
48        assertEquals("incorrect sign", 1, result.signum());
49    }
50
51    /**
52     * andNot for two positive numbers; the first is shorter
53     */
54    public void testAndNotPosPosFirstShorter() {
55        byte aBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
56        byte bBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
57        int aSign = 1;
58        int bSign = 1;
59        byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2};
60        BigInteger aNumber = new BigInteger(aSign, aBytes);
61        BigInteger bNumber = new BigInteger(bSign, bBytes);
62        BigInteger result = aNumber.andNot(bNumber);
63        byte resBytes[] = new byte[rBytes.length];
64        resBytes = result.toByteArray();
65        for(int i = 0; i < resBytes.length; i++) {
66            assertTrue(resBytes[i] == rBytes[i]);
67        }
68        assertEquals("incorrect sign", 1, result.signum());
69    }
70
71    /**
72     * andNot for two negative numbers; the first is longer
73     */
74    public void testAndNotNegNegFirstLonger() {
75        byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
76        byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
77        int aSign = -1;
78        int bSign = -1;
79        byte rBytes[] = {73, -92, -48, 4, 12, 6, 4, 32, 48, 64, 0, 8, 2};
80        BigInteger aNumber = new BigInteger(aSign, aBytes);
81        BigInteger bNumber = new BigInteger(bSign, bBytes);
82        BigInteger result = aNumber.andNot(bNumber);
83        byte resBytes[] = new byte[rBytes.length];
84        resBytes = result.toByteArray();
85        for(int i = 0; i < resBytes.length; i++) {
86            assertTrue(resBytes[i] == rBytes[i]);
87        }
88        assertEquals("incorrect sign", 1, result.signum());
89    }
90
91    /**
92     * andNot for a negative and a positive numbers; the first is longer
93     */
94    public void testNegPosFirstLonger() {
95        byte aBytes[] = {-128, 9, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117, 23, 87, -25, -75};
96        byte bBytes[] = {-2, -3, -4, -4, 5, 14, 23, 39, 48, 57, 66, 5, 14, 23};
97        int aSign = -1;
98        int bSign = 1;
99        byte rBytes[] = {-1, 127, -10, -57, -101, 1, 2, 2, 2, -96, -16, 8, -40, -59, 68, -88, -88, 16, 72};
100        BigInteger aNumber = new BigInteger(aSign, aBytes);
101        BigInteger bNumber = new BigInteger(bSign, bBytes);
102        BigInteger result = aNumber.andNot(bNumber);
103        byte resBytes[] = new byte[rBytes.length];
104        resBytes = result.toByteArray();
105        for(int i = 0; i < resBytes.length; i++) {
106            assertTrue(resBytes[i] == rBytes[i]);
107        }
108        assertEquals("incorrect sign", -1, result.signum());
109    }
110
111    /**
112     * Not for ZERO
113     */
114    public void testNotZero() {
115        byte rBytes[] = {-1};
116        BigInteger aNumber = BigInteger.ZERO;
117        BigInteger result = aNumber.not();
118        byte resBytes[] = new byte[rBytes.length];
119        resBytes = result.toByteArray();
120        for(int i = 0; i < resBytes.length; i++) {
121            assertTrue(resBytes[i] == rBytes[i]);
122        }
123        assertEquals("incorrect sign", -1, result.signum());
124    }
125
126    /**
127     * Not for ONE
128     */
129    public void testNotOne() {
130        byte rBytes[] = {-2};
131        BigInteger aNumber = BigInteger.ONE;
132        BigInteger result = aNumber.not();
133        byte resBytes[] = new byte[rBytes.length];
134        resBytes = result.toByteArray();
135        for(int i = 0; i < resBytes.length; i++) {
136            assertTrue(resBytes[i] == rBytes[i]);
137        }
138        assertEquals("incorrect sign", -1, result.signum());
139    }
140
141    /**
142     * Not for a positive number
143     */
144    public void testNotPos() {
145        byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
146        int aSign = 1;
147        byte rBytes[] = {-1, 127, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -27, 116};
148        BigInteger aNumber = new BigInteger(aSign, aBytes);
149        BigInteger result = aNumber.not();
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     * Not for a negative number
160     */
161    public void testNotNeg() {
162        byte aBytes[] = {-128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -117};
163        int aSign = -1;
164        byte rBytes[] = {0, -128, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, -118};
165        BigInteger aNumber = new BigInteger(aSign, aBytes);
166        BigInteger result = aNumber.not();
167        byte resBytes[] = new byte[rBytes.length];
168        resBytes = result.toByteArray();
169        for(int i = 0; i < resBytes.length; i++) {
170            assertTrue(resBytes[i] == rBytes[i]);
171        }
172        assertEquals("incorrect sign", 1, result.signum());
173    }
174
175    /**
176     * Not for a negative number
177     */
178
179    public void testNotSpecialCase() {
180        byte aBytes[] = {-1, -1, -1, -1};
181        int aSign = 1;
182        byte rBytes[] = {-1, 0, 0, 0, 0};
183        BigInteger aNumber = new BigInteger(aSign, aBytes);
184        BigInteger result = aNumber.not();
185        byte resBytes[] = new byte[rBytes.length];
186        resBytes = result.toByteArray();
187        for(int i = 0; i < resBytes.length; i++) {
188            assertTrue(resBytes[i] == rBytes[i]);
189        }
190        assertEquals("incorrect sign", -1, result.signum());
191    }
192}