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 21 package org.apache.harmony.tests.java.math; 22 23 import junit.framework.TestCase; 24 import java.math.BigInteger; 25 26 /** 27 * Class: java.math.BigInteger 28 * Methods: abs, compareTo, equals, max, min, negate, signum 29 */ 30 public class BigIntegerCompareTest extends TestCase { 31 /** 32 * abs() for a positive number 33 */ 34 public void testAbsPositive() { 35 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 36 int aSign = 1; 37 byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 38 BigInteger aNumber = new BigInteger(aSign, aBytes); 39 BigInteger result = aNumber.abs(); 40 byte resBytes[] = new byte[rBytes.length]; 41 resBytes = result.toByteArray(); 42 for(int i = 0; i < resBytes.length; i++) { 43 assertTrue(resBytes[i] == rBytes[i]); 44 } 45 assertEquals("incorrect sign", 1, result.signum()); 46 } 47 48 /** 49 * abs() for a negative number 50 */ 51 public void testAbsNegative() { 52 byte aBytes[] = {1, 2, 3, 4, 5, 6, 7}; 53 int aSign = -1; 54 byte rBytes[] = {1, 2, 3, 4, 5, 6, 7}; 55 BigInteger aNumber = new BigInteger(aSign, aBytes); 56 BigInteger result = aNumber.abs(); 57 byte resBytes[] = new byte[rBytes.length]; 58 resBytes = result.toByteArray(); 59 for(int i = 0; i < resBytes.length; i++) { 60 assertTrue(resBytes[i] == rBytes[i]); 61 } 62 assertEquals("incorrect sign", 1, result.signum()); 63 } 64 65 /** 66 * compareTo(BigInteger a). 67 * Compare two positive numbers. 68 * The first is greater. 69 */ 70 public void testCompareToPosPos1() { 71 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 72 byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 73 int aSign = 1; 74 int bSign = 1; 75 BigInteger aNumber = new BigInteger(aSign, aBytes); 76 BigInteger bNumber = new BigInteger(bSign, bBytes); 77 assertEquals(1, aNumber.compareTo(bNumber)); 78 } 79 80 /** 81 * compareTo(BigInteger a). 82 * Compare two positive numbers. 83 * The first is less. 84 */ 85 public void testCompareToPosPos2() { 86 byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 87 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 88 int aSign = 1; 89 int bSign = 1; 90 BigInteger aNumber = new BigInteger(aSign, aBytes); 91 BigInteger bNumber = new BigInteger(bSign, bBytes); 92 assertEquals(-1, aNumber.compareTo(bNumber)); 93 } 94 95 /** 96 * compareTo(BigInteger a). 97 * Compare two equal positive numbers. 98 */ 99 public void testCompareToEqualPos() { 100 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 101 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 102 int aSign = 1; 103 int bSign = 1; 104 BigInteger aNumber = new BigInteger(aSign, aBytes); 105 BigInteger bNumber = new BigInteger(bSign, bBytes); 106 assertEquals(0, aNumber.compareTo(bNumber)); 107 } 108 109 /** 110 * compareTo(BigInteger a). 111 * Compare two negative numbers. 112 * The first is greater in absolute value. 113 */ 114 public void testCompareToNegNeg1() { 115 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 116 byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 117 int aSign = -1; 118 int bSign = -1; 119 BigInteger aNumber = new BigInteger(aSign, aBytes); 120 BigInteger bNumber = new BigInteger(bSign, bBytes); 121 assertEquals(-1, aNumber.compareTo(bNumber)); 122 } 123 124 /** 125 * compareTo(BigInteger a). 126 * Compare two negative numbers. 127 * The first is less in absolute value. 128 */ 129 public void testCompareNegNeg2() { 130 byte aBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 131 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 132 int aSign = -1; 133 int bSign = -1; 134 BigInteger aNumber = new BigInteger(aSign, aBytes); 135 BigInteger bNumber = new BigInteger(bSign, bBytes); 136 assertEquals(1, aNumber.compareTo(bNumber)); 137 } 138 139 /** 140 * compareTo(BigInteger a). 141 * Compare two equal negative numbers. 142 */ 143 public void testCompareToEqualNeg() { 144 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 145 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 146 int aSign = -1; 147 int bSign = -1; 148 BigInteger aNumber = new BigInteger(aSign, aBytes); 149 BigInteger bNumber = new BigInteger(bSign, bBytes); 150 assertEquals(0, aNumber.compareTo(bNumber)); 151 } 152 153 /** 154 * compareTo(BigInteger a). 155 * Compare two numbers of different signs. 156 * The first is positive. 157 */ 158 public void testCompareToDiffSigns1() { 159 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 160 byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 161 int aSign = 1; 162 int bSign = -1; 163 BigInteger aNumber = new BigInteger(aSign, aBytes); 164 BigInteger bNumber = new BigInteger(bSign, bBytes); 165 assertEquals(1, aNumber.compareTo(bNumber)); 166 } 167 168 /** 169 * compareTo(BigInteger a). 170 * Compare two numbers of different signs. 171 * The first is negative. 172 */ 173 public void testCompareToDiffSigns2() { 174 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 175 byte bBytes[] = {10, 20, 30, 40, 50, 60, 70, 10, 20, 30}; 176 int aSign = -1; 177 int bSign = 1; 178 BigInteger aNumber = new BigInteger(aSign, aBytes); 179 BigInteger bNumber = new BigInteger(bSign, bBytes); 180 assertEquals(-1, aNumber.compareTo(bNumber)); 181 } 182 183 /** 184 * compareTo(BigInteger a). 185 * Compare a positive number to ZERO. 186 */ 187 public void testCompareToPosZero() { 188 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 189 int aSign = 1; 190 BigInteger aNumber = new BigInteger(aSign, aBytes); 191 BigInteger bNumber = BigInteger.ZERO; 192 assertEquals(1, aNumber.compareTo(bNumber)); 193 } 194 195 /** 196 * compareTo(BigInteger a). 197 * Compare ZERO to a positive number. 198 */ 199 public void testCompareToZeroPos() { 200 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 201 int bSign = 1; 202 BigInteger aNumber = BigInteger.ZERO; 203 BigInteger bNumber = new BigInteger(bSign, bBytes); 204 assertEquals(-1, aNumber.compareTo(bNumber)); 205 } 206 207 /** 208 * compareTo(BigInteger a). 209 * Compare a negative number to ZERO. 210 */ 211 public void testCompareToNegZero() { 212 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 213 int aSign = -1; 214 BigInteger aNumber = new BigInteger(aSign, aBytes); 215 BigInteger bNumber = BigInteger.ZERO; 216 assertEquals(-1, aNumber.compareTo(bNumber)); 217 } 218 219 /** 220 * compareTo(BigInteger a). 221 * Compare ZERO to a negative number. 222 */ 223 public void testCompareToZeroNeg() { 224 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 225 int bSign = -1; 226 BigInteger aNumber = BigInteger.ZERO; 227 BigInteger bNumber = new BigInteger(bSign, bBytes); 228 assertEquals(1, aNumber.compareTo(bNumber)); 229 } 230 231 /** 232 * compareTo(BigInteger a). 233 * Compare ZERO to ZERO. 234 */ 235 public void testCompareToZeroZero() { 236 BigInteger aNumber = BigInteger.ZERO; 237 BigInteger bNumber = BigInteger.ZERO; 238 assertEquals(0, aNumber.compareTo(bNumber)); 239 } 240 241 /** 242 * equals(Object obj). 243 * obj is not a BigInteger 244 */ 245 public void testEqualsObject() { 246 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 247 int aSign = 1; 248 BigInteger aNumber = new BigInteger(aSign, aBytes); 249 Object obj = new Object(); 250 assertFalse(aNumber.equals(obj)); 251 } 252 253 /** 254 * equals(null). 255 */ 256 public void testEqualsNull() { 257 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 258 int aSign = 1; 259 BigInteger aNumber = new BigInteger(aSign, aBytes); 260 assertFalse(aNumber.equals(null)); 261 } 262 263 /** 264 * equals(Object obj). 265 * obj is a BigInteger. 266 * numbers are equal. 267 */ 268 public void testEqualsBigIntegerTrue() { 269 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 270 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 271 int aSign = 1; 272 int bSign = 1; 273 BigInteger aNumber = new BigInteger(aSign, aBytes); 274 Object bNumber = new BigInteger(bSign, bBytes); 275 assertTrue(aNumber.equals(bNumber)); 276 } 277 278 /** 279 * equals(Object obj). 280 * obj is a BigInteger. 281 * numbers are not equal. 282 */ 283 public void testEqualsBigIntegerFalse() { 284 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 285 byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 286 int aSign = 1; 287 int bSign = 1; 288 BigInteger aNumber = new BigInteger(aSign, aBytes); 289 Object bNumber = new BigInteger(bSign, bBytes); 290 assertFalse(aNumber.equals(bNumber)); 291 } 292 293 /** 294 * max(BigInteger val). 295 * the first is greater. 296 */ 297 public void testMaxGreater() { 298 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 299 byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 300 int aSign = 1; 301 int bSign = 1; 302 byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 303 BigInteger aNumber = new BigInteger(aSign, aBytes); 304 BigInteger bNumber = new BigInteger(bSign, bBytes); 305 BigInteger result = aNumber.max(bNumber); 306 byte resBytes[] = new byte[rBytes.length]; 307 resBytes = result.toByteArray(); 308 for(int i = 0; i < resBytes.length; i++) { 309 assertTrue(resBytes[i] == rBytes[i]); 310 } 311 assertTrue("incorrect sign", result.signum() == 1); 312 } 313 314 /** 315 * max(BigInteger val). 316 * the first is less. 317 */ 318 public void testMaxLess() { 319 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 320 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 321 int aSign = 1; 322 int bSign = 1; 323 byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 324 BigInteger aNumber = new BigInteger(aSign, aBytes); 325 BigInteger bNumber = new BigInteger(bSign, bBytes); 326 BigInteger result = aNumber.max(bNumber); 327 byte resBytes[] = new byte[rBytes.length]; 328 resBytes = result.toByteArray(); 329 for(int i = 0; i < resBytes.length; i++) { 330 assertTrue(resBytes[i] == rBytes[i]); 331 } 332 assertTrue("incorrect sign", result.signum() == 1); 333 } 334 335 /** 336 * max(BigInteger val). 337 * numbers are equal. 338 */ 339 public void testMaxEqual() { 340 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 341 byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 342 int aSign = 1; 343 int bSign = 1; 344 byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 345 BigInteger aNumber = new BigInteger(aSign, aBytes); 346 BigInteger bNumber = new BigInteger(bSign, bBytes); 347 BigInteger result = aNumber.max(bNumber); 348 byte resBytes[] = new byte[rBytes.length]; 349 resBytes = result.toByteArray(); 350 for(int i = 0; i < resBytes.length; i++) { 351 assertTrue(resBytes[i] == rBytes[i]); 352 } 353 assertEquals("incorrect sign", 1, result.signum()); 354 } 355 356 /** 357 * max(BigInteger val). 358 * max of negative and ZERO. 359 */ 360 public void testMaxNegZero() { 361 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 362 int aSign = -1; 363 byte rBytes[] = {0}; 364 BigInteger aNumber = new BigInteger(aSign, aBytes); 365 BigInteger bNumber = BigInteger.ZERO; 366 BigInteger result = aNumber.max(bNumber); 367 byte resBytes[] = new byte[rBytes.length]; 368 resBytes = result.toByteArray(); 369 for(int i = 0; i < resBytes.length; i++) { 370 assertTrue(resBytes[i] == rBytes[i]); 371 } 372 assertTrue("incorrect sign", result.signum() == 0); 373 } 374 375 /** 376 * min(BigInteger val). 377 * the first is greater. 378 */ 379 public void testMinGreater() { 380 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 381 byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 382 int aSign = 1; 383 int bSign = 1; 384 byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 385 BigInteger aNumber = new BigInteger(aSign, aBytes); 386 BigInteger bNumber = new BigInteger(bSign, bBytes); 387 BigInteger result = aNumber.min(bNumber); 388 byte resBytes[] = new byte[rBytes.length]; 389 resBytes = result.toByteArray(); 390 for(int i = 0; i < resBytes.length; i++) { 391 assertTrue(resBytes[i] == rBytes[i]); 392 } 393 assertEquals("incorrect sign", 1, result.signum()); 394 } 395 396 /** 397 * min(BigInteger val). 398 * the first is less. 399 */ 400 public void testMinLess() { 401 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 402 byte bBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 403 int aSign = 1; 404 int bSign = 1; 405 byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 406 BigInteger aNumber = new BigInteger(aSign, aBytes); 407 BigInteger bNumber = new BigInteger(bSign, bBytes); 408 BigInteger result = aNumber.min(bNumber); 409 byte resBytes[] = new byte[rBytes.length]; 410 resBytes = result.toByteArray(); 411 for(int i = 0; i < resBytes.length; i++) { 412 assertTrue(resBytes[i] == rBytes[i]); 413 } 414 assertEquals("incorrect sign", 1, result.signum()); 415 } 416 417 /** 418 * min(BigInteger val). 419 * numbers are equal. 420 */ 421 public void testMinEqual() { 422 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 423 byte bBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 424 int aSign = 1; 425 int bSign = 1; 426 byte rBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 427 BigInteger aNumber = new BigInteger(aSign, aBytes); 428 BigInteger bNumber = new BigInteger(bSign, bBytes); 429 BigInteger result = aNumber.min(bNumber); 430 byte resBytes[] = new byte[rBytes.length]; 431 resBytes = result.toByteArray(); 432 for(int i = 0; i < resBytes.length; i++) { 433 assertTrue(resBytes[i] == rBytes[i]); 434 } 435 assertTrue("incorrect sign", result.signum() == 1); 436 } 437 438 /** 439 * max(BigInteger val). 440 * min of positive and ZERO. 441 */ 442 public void testMinPosZero() { 443 byte aBytes[] = {45, 91, 3, -15, 35, 26, 3, 91}; 444 int aSign = 1; 445 byte rBytes[] = {0}; 446 BigInteger aNumber = new BigInteger(aSign, aBytes); 447 BigInteger bNumber = BigInteger.ZERO; 448 BigInteger result = aNumber.min(bNumber); 449 byte resBytes[] = new byte[rBytes.length]; 450 resBytes = result.toByteArray(); 451 for(int i = 0; i < resBytes.length; i++) { 452 assertTrue(resBytes[i] == rBytes[i]); 453 } 454 assertTrue("incorrect sign", result.signum() == 0); 455 } 456 457 /** 458 * negate() a positive number. 459 */ 460 public void testNegatePositive() { 461 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 462 int aSign = 1; 463 byte rBytes[] = {-13, -57, -101, 1, 75, -90, -46, -92, -4, 14, -36, -27, -4, -91}; 464 BigInteger aNumber = new BigInteger(aSign, aBytes); 465 BigInteger result = aNumber.negate(); 466 byte resBytes[] = new byte[rBytes.length]; 467 resBytes = result.toByteArray(); 468 for(int i = 0; i < resBytes.length; i++) { 469 assertTrue(resBytes[i] == rBytes[i]); 470 } 471 assertTrue("incorrect sign", result.signum() == -1); 472 } 473 474 /** 475 * negate() a negative number. 476 */ 477 public void testNegateNegative() { 478 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 479 int aSign = -1; 480 byte rBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 481 BigInteger aNumber = new BigInteger(aSign, aBytes); 482 BigInteger result = aNumber.negate(); 483 byte resBytes[] = new byte[rBytes.length]; 484 resBytes = result.toByteArray(); 485 for(int i = 0; i < resBytes.length; i++) { 486 assertTrue(resBytes[i] == rBytes[i]); 487 } 488 assertTrue("incorrect sign", result.signum() == 1); 489 } 490 491 /** 492 * negate() ZERO. 493 */ 494 public void testNegateZero() { 495 byte rBytes[] = {0}; 496 BigInteger aNumber = BigInteger.ZERO; 497 BigInteger result = aNumber.negate(); 498 byte resBytes[] = new byte[rBytes.length]; 499 resBytes = result.toByteArray(); 500 for(int i = 0; i < resBytes.length; i++) { 501 assertTrue(resBytes[i] == rBytes[i]); 502 } 503 assertEquals("incorrect sign", 0, result.signum()); 504 } 505 506 /** 507 * signum() of a positive number. 508 */ 509 public void testSignumPositive() { 510 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 511 int aSign = 1; 512 BigInteger aNumber = new BigInteger(aSign, aBytes); 513 assertEquals("incorrect sign", 1, aNumber.signum()); 514 } 515 516 /** 517 * signum() of a negative number. 518 */ 519 public void testSignumNegative() { 520 byte aBytes[] = {12, 56, 100, -2, -76, 89, 45, 91, 3, -15, 35, 26, 3, 91}; 521 int aSign = -1; 522 BigInteger aNumber = new BigInteger(aSign, aBytes); 523 assertEquals("incorrect sign", -1, aNumber.signum()); 524 } 525 526 /** 527 * signum() of ZERO. 528 */ 529 public void testSignumZero() { 530 BigInteger aNumber = BigInteger.ZERO; 531 assertEquals("incorrect sign", 0, aNumber.signum()); 532 } 533 } 534