1 /* Licensed to the Apache Software Foundation (ASF) under one or more 2 * contributor license agreements. See the NOTICE file distributed with 3 * this work for additional information regarding copyright ownership. 4 * The ASF licenses this file to You under the Apache License, Version 2.0 5 * (the "License"); you may not use this file except in compliance with 6 * the License. You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package org.apache.harmony.tests.java.lang; 18 19 import java.util.Properties; 20 21 import junit.framework.TestCase; 22 23 public class LongTest extends TestCase { 24 private Properties orgProps; 25 26 @Override 27 protected void setUp() { 28 orgProps = System.getProperties(); 29 } 30 31 @Override 32 protected void tearDown() { 33 System.setProperties(orgProps); 34 } 35 36 /** 37 * java.lang.Long#byteValue() 38 */ 39 public void test_byteValue() { 40 // Test for method byte java.lang.Long.byteValue() 41 Long l = new Long(127); 42 assertEquals("Returned incorrect byte value", 127, l.byteValue()); 43 assertEquals("Returned incorrect byte value", -1, new Long(Long.MAX_VALUE) 44 .byteValue()); 45 } 46 47 /** 48 * java.lang.Long#compareTo(java.lang.Long) 49 */ 50 public void test_compareToLjava_lang_Long() { 51 // Test for method int java.lang.Long.compareTo(java.lang.Long) 52 assertTrue("-2 compared to 1 gave non-negative answer", new Long(-2L) 53 .compareTo(new Long(1L)) < 0); 54 assertEquals("-2 compared to -2 gave non-zero answer", 0, new Long(-2L) 55 .compareTo(new Long(-2L))); 56 assertTrue("3 compared to 2 gave non-positive answer", new Long(3L) 57 .compareTo(new Long(2L)) > 0); 58 59 try { 60 new Long(0).compareTo(null); 61 fail("No NPE"); 62 } catch (NullPointerException e) { 63 } 64 } 65 66 /** 67 * java.lang.Long#decode(java.lang.String) 68 */ 69 public void test_decodeLjava_lang_String2() { 70 // Test for method java.lang.Long 71 // java.lang.Long.decode(java.lang.String) 72 assertEquals("Returned incorrect value for hex string", 255L, Long.decode( 73 "0xFF").longValue()); 74 assertEquals("Returned incorrect value for dec string", -89000L, Long.decode( 75 "-89000").longValue()); 76 assertEquals("Returned incorrect value for 0 decimal", 0, Long.decode("0") 77 .longValue()); 78 assertEquals("Returned incorrect value for 0 hex", 0, Long.decode("0x0") 79 .longValue()); 80 assertTrue( 81 "Returned incorrect value for most negative value decimal", 82 Long.decode("-9223372036854775808").longValue() == 0x8000000000000000L); 83 assertTrue( 84 "Returned incorrect value for most negative value hex", 85 Long.decode("-0x8000000000000000").longValue() == 0x8000000000000000L); 86 assertTrue( 87 "Returned incorrect value for most positive value decimal", 88 Long.decode("9223372036854775807").longValue() == 0x7fffffffffffffffL); 89 assertTrue( 90 "Returned incorrect value for most positive value hex", 91 Long.decode("0x7fffffffffffffff").longValue() == 0x7fffffffffffffffL); 92 assertTrue("Failed for 07654321765432", Long.decode("07654321765432") 93 .longValue() == 07654321765432l); 94 95 boolean exception = false; 96 try { 97 Long 98 .decode("999999999999999999999999999999999999999999999999999999"); 99 } catch (NumberFormatException e) { 100 // Correct 101 exception = true; 102 } 103 assertTrue("Failed to throw exception for value > ilong", exception); 104 105 exception = false; 106 try { 107 Long.decode("9223372036854775808"); 108 } catch (NumberFormatException e) { 109 // Correct 110 exception = true; 111 } 112 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 113 114 exception = false; 115 try { 116 Long.decode("-9223372036854775809"); 117 } catch (NumberFormatException e) { 118 // Correct 119 exception = true; 120 } 121 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 122 123 exception = false; 124 try { 125 Long.decode("0x8000000000000000"); 126 } catch (NumberFormatException e) { 127 // Correct 128 exception = true; 129 } 130 assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception); 131 132 exception = false; 133 try { 134 Long.decode("-0x8000000000000001"); 135 } catch (NumberFormatException e) { 136 // Correct 137 exception = true; 138 } 139 assertTrue("Failed to throw exception for hex MIN_VALUE - 1", exception); 140 141 exception = false; 142 try { 143 Long.decode("42325917317067571199"); 144 } catch (NumberFormatException e) { 145 // Correct 146 exception = true; 147 } 148 assertTrue("Failed to throw exception for 42325917317067571199", 149 exception); 150 } 151 152 /** 153 * java.lang.Long#getLong(java.lang.String) 154 */ 155 public void test_getLongLjava_lang_String() { 156 // Test for method java.lang.Long 157 // java.lang.Long.getLong(java.lang.String) 158 Properties tProps = new Properties(); 159 tProps.put("testLong", "99"); 160 System.setProperties(tProps); 161 assertTrue("returned incorrect Long", Long.getLong("testLong").equals( 162 new Long(99))); 163 assertNull("returned incorrect default Long", 164 Long.getLong("ff")); 165 } 166 167 /** 168 * java.lang.Long#getLong(java.lang.String, long) 169 */ 170 public void test_getLongLjava_lang_StringJ() { 171 // Test for method java.lang.Long 172 // java.lang.Long.getLong(java.lang.String, long) 173 Properties tProps = new Properties(); 174 tProps.put("testLong", "99"); 175 System.setProperties(tProps); 176 assertTrue("returned incorrect Long", Long.getLong("testLong", 4L) 177 .equals(new Long(99))); 178 assertTrue("returned incorrect default Long", Long.getLong("ff", 4L) 179 .equals(new Long(4))); 180 } 181 182 /** 183 * java.lang.Long#getLong(java.lang.String, java.lang.Long) 184 */ 185 public void test_getLongLjava_lang_StringLjava_lang_Long() { 186 // Test for method java.lang.Long 187 // java.lang.Long.getLong(java.lang.String, java.lang.Long) 188 Properties tProps = new Properties(); 189 tProps.put("testLong", "99"); 190 System.setProperties(tProps); 191 assertTrue("returned incorrect Long", Long.getLong("testLong", 192 new Long(4)).equals(new Long(99))); 193 assertTrue("returned incorrect default Long", Long.getLong("ff", 194 new Long(4)).equals(new Long(4))); 195 } 196 197 /** 198 * java.lang.Long#parseLong(java.lang.String) 199 */ 200 public void test_parseLongLjava_lang_String2() { 201 // Test for method long java.lang.Long.parseLong(java.lang.String) 202 203 long l = Long.parseLong("89000000005"); 204 assertEquals("Parsed to incorrect long value", 89000000005L, l); 205 assertEquals("Returned incorrect value for 0", 0, Long.parseLong("0")); 206 assertTrue("Returned incorrect value for most negative value", Long 207 .parseLong("-9223372036854775808") == 0x8000000000000000L); 208 assertTrue("Returned incorrect value for most positive value", Long 209 .parseLong("9223372036854775807") == 0x7fffffffffffffffL); 210 211 boolean exception = false; 212 try { 213 Long.parseLong("9223372036854775808"); 214 } catch (NumberFormatException e) { 215 // Correct 216 exception = true; 217 } 218 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 219 220 exception = false; 221 try { 222 Long.parseLong("-9223372036854775809"); 223 } catch (NumberFormatException e) { 224 // Correct 225 exception = true; 226 } 227 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 228 } 229 230 /** 231 * java.lang.Long#parseLong(java.lang.String, int) 232 */ 233 public void test_parseLongLjava_lang_StringI() { 234 // Test for method long java.lang.Long.parseLong(java.lang.String, int) 235 assertEquals("Returned incorrect value", 236 100000000L, Long.parseLong("100000000", 10)); 237 assertEquals("Returned incorrect value from hex string", 68719476735L, Long.parseLong( 238 "FFFFFFFFF", 16)); 239 assertTrue("Returned incorrect value from octal string: " 240 + Long.parseLong("77777777777"), Long.parseLong("77777777777", 241 8) == 8589934591L); 242 assertEquals("Returned incorrect value for 0 hex", 0, Long 243 .parseLong("0", 16)); 244 assertTrue("Returned incorrect value for most negative value hex", Long 245 .parseLong("-8000000000000000", 16) == 0x8000000000000000L); 246 assertTrue("Returned incorrect value for most positive value hex", Long 247 .parseLong("7fffffffffffffff", 16) == 0x7fffffffffffffffL); 248 assertEquals("Returned incorrect value for 0 decimal", 0, Long.parseLong( 249 "0", 10)); 250 assertTrue( 251 "Returned incorrect value for most negative value decimal", 252 Long.parseLong("-9223372036854775808", 10) == 0x8000000000000000L); 253 assertTrue( 254 "Returned incorrect value for most positive value decimal", 255 Long.parseLong("9223372036854775807", 10) == 0x7fffffffffffffffL); 256 257 boolean exception = false; 258 try { 259 Long.parseLong("999999999999", 8); 260 } catch (NumberFormatException e) { 261 // correct 262 exception = true; 263 } 264 assertTrue("Failed to throw exception when passed invalid string", 265 exception); 266 267 exception = false; 268 try { 269 Long.parseLong("9223372036854775808", 10); 270 } catch (NumberFormatException e) { 271 // Correct 272 exception = true; 273 } 274 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 275 276 exception = false; 277 try { 278 Long.parseLong("-9223372036854775809", 10); 279 } catch (NumberFormatException e) { 280 // Correct 281 exception = true; 282 } 283 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 284 285 exception = false; 286 try { 287 Long.parseLong("8000000000000000", 16); 288 } catch (NumberFormatException e) { 289 // Correct 290 exception = true; 291 } 292 assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception); 293 294 exception = false; 295 try { 296 Long.parseLong("-8000000000000001", 16); 297 } catch (NumberFormatException e) { 298 // Correct 299 exception = true; 300 } 301 assertTrue("Failed to throw exception for hex MIN_VALUE + 1", exception); 302 303 exception = false; 304 try { 305 Long.parseLong("42325917317067571199", 10); 306 } catch (NumberFormatException e) { 307 // Correct 308 exception = true; 309 } 310 assertTrue("Failed to throw exception for 42325917317067571199", 311 exception); 312 } 313 314 /** 315 * java.lang.Long#toBinaryString(long) 316 */ 317 public void test_toBinaryStringJ() { 318 // Test for method java.lang.String java.lang.Long.toBinaryString(long) 319 assertEquals("Incorrect binary string returned", "11011001010010010000", Long.toBinaryString( 320 890000L)); 321 assertEquals("Incorrect binary string returned", 322 323 "1000000000000000000000000000000000000000000000000000000000000000", Long 324 .toBinaryString(Long.MIN_VALUE) 325 ); 326 assertEquals("Incorrect binary string returned", 327 328 "111111111111111111111111111111111111111111111111111111111111111", Long 329 .toBinaryString(Long.MAX_VALUE) 330 ); 331 } 332 333 /** 334 * java.lang.Long#toHexString(long) 335 */ 336 public void test_toHexStringJ() { 337 // Test for method java.lang.String java.lang.Long.toHexString(long) 338 assertEquals("Incorrect hex string returned", "54e0845", Long.toHexString(89000005L) 339 ); 340 assertEquals("Incorrect hex string returned", "8000000000000000", Long.toHexString( 341 Long.MIN_VALUE)); 342 assertEquals("Incorrect hex string returned", "7fffffffffffffff", Long.toHexString( 343 Long.MAX_VALUE)); 344 } 345 346 /** 347 * java.lang.Long#toOctalString(long) 348 */ 349 public void test_toOctalStringJ() { 350 // Test for method java.lang.String java.lang.Long.toOctalString(long) 351 assertEquals("Returned incorrect oct string", "77777777777", Long.toOctalString( 352 8589934591L)); 353 assertEquals("Returned incorrect oct string", "1000000000000000000000", Long.toOctalString( 354 Long.MIN_VALUE)); 355 assertEquals("Returned incorrect oct string", "777777777777777777777", Long.toOctalString( 356 Long.MAX_VALUE)); 357 } 358 359 /** 360 * java.lang.Long#toString() 361 */ 362 public void test_toString2() { 363 // Test for method java.lang.String java.lang.Long.toString() 364 Long l = new Long(89000000005L); 365 assertEquals("Returned incorrect String", 366 "89000000005", l.toString()); 367 assertEquals("Returned incorrect String", "-9223372036854775808", new Long(Long.MIN_VALUE) 368 .toString()); 369 assertEquals("Returned incorrect String", "9223372036854775807", new Long(Long.MAX_VALUE) 370 .toString()); 371 } 372 373 /** 374 * java.lang.Long#toString(long) 375 */ 376 public void test_toStringJ2() { 377 // Test for method java.lang.String java.lang.Long.toString(long) 378 379 assertEquals("Returned incorrect String", "89000000005", Long.toString(89000000005L) 380 ); 381 assertEquals("Returned incorrect String", "-9223372036854775808", Long.toString(Long.MIN_VALUE) 382 ); 383 assertEquals("Returned incorrect String", "9223372036854775807", Long.toString(Long.MAX_VALUE) 384 ); 385 } 386 387 /** 388 * java.lang.Long#toString(long, int) 389 */ 390 public void test_toStringJI() { 391 // Test for method java.lang.String java.lang.Long.toString(long, int) 392 assertEquals("Returned incorrect dec string", "100000000", Long.toString(100000000L, 393 10)); 394 assertEquals("Returned incorrect hex string", "fffffffff", Long.toString(68719476735L, 395 16)); 396 assertEquals("Returned incorrect oct string", "77777777777", Long.toString(8589934591L, 397 8)); 398 assertEquals("Returned incorrect bin string", 399 "1111111111111111111111111111111111111111111", Long.toString( 400 8796093022207L, 2)); 401 assertEquals("Returned incorrect min string", "-9223372036854775808", Long.toString( 402 0x8000000000000000L, 10)); 403 assertEquals("Returned incorrect max string", "9223372036854775807", Long.toString( 404 0x7fffffffffffffffL, 10)); 405 assertEquals("Returned incorrect min string", "-8000000000000000", Long.toString( 406 0x8000000000000000L, 16)); 407 assertEquals("Returned incorrect max string", "7fffffffffffffff", Long.toString( 408 0x7fffffffffffffffL, 16)); 409 } 410 411 /** 412 * java.lang.Long#valueOf(java.lang.String) 413 */ 414 public void test_valueOfLjava_lang_String2() { 415 // Test for method java.lang.Long 416 // java.lang.Long.valueOf(java.lang.String) 417 assertEquals("Returned incorrect value", 100000000L, Long.valueOf("100000000") 418 .longValue()); 419 assertTrue("Returned incorrect value", Long.valueOf( 420 "9223372036854775807").longValue() == Long.MAX_VALUE); 421 assertTrue("Returned incorrect value", Long.valueOf( 422 "-9223372036854775808").longValue() == Long.MIN_VALUE); 423 424 boolean exception = false; 425 try { 426 Long 427 .valueOf("999999999999999999999999999999999999999999999999999999999999"); 428 } catch (NumberFormatException e) { 429 // correct 430 exception = true; 431 } 432 assertTrue("Failed to throw exception when passed invalid string", 433 exception); 434 435 exception = false; 436 try { 437 Long.valueOf("9223372036854775808"); 438 } catch (NumberFormatException e) { 439 // correct 440 exception = true; 441 } 442 assertTrue("Failed to throw exception when passed invalid string", 443 exception); 444 445 exception = false; 446 try { 447 Long.valueOf("-9223372036854775809"); 448 } catch (NumberFormatException e) { 449 // correct 450 exception = true; 451 } 452 assertTrue("Failed to throw exception when passed invalid string", 453 exception); 454 } 455 456 /** 457 * java.lang.Long#valueOf(java.lang.String, int) 458 */ 459 public void test_valueOfLjava_lang_StringI() { 460 // Test for method java.lang.Long 461 // java.lang.Long.valueOf(java.lang.String, int) 462 assertEquals("Returned incorrect value", 100000000L, Long.valueOf("100000000", 10) 463 .longValue()); 464 assertEquals("Returned incorrect value from hex string", 68719476735L, Long.valueOf( 465 "FFFFFFFFF", 16).longValue()); 466 assertTrue("Returned incorrect value from octal string: " 467 + Long.valueOf("77777777777", 8).toString(), Long.valueOf( 468 "77777777777", 8).longValue() == 8589934591L); 469 assertTrue("Returned incorrect value", Long.valueOf( 470 "9223372036854775807", 10).longValue() == Long.MAX_VALUE); 471 assertTrue("Returned incorrect value", Long.valueOf( 472 "-9223372036854775808", 10).longValue() == Long.MIN_VALUE); 473 assertTrue("Returned incorrect value", Long.valueOf("7fffffffffffffff", 474 16).longValue() == Long.MAX_VALUE); 475 assertTrue("Returned incorrect value", Long.valueOf( 476 "-8000000000000000", 16).longValue() == Long.MIN_VALUE); 477 478 boolean exception = false; 479 try { 480 Long.valueOf("999999999999", 8); 481 } catch (NumberFormatException e) { 482 // correct 483 exception = true; 484 } 485 assertTrue("Failed to throw exception when passed invalid string", 486 exception); 487 488 exception = false; 489 try { 490 Long.valueOf("9223372036854775808", 10); 491 } catch (NumberFormatException e) { 492 // correct 493 exception = true; 494 } 495 assertTrue("Failed to throw exception when passed invalid string", 496 exception); 497 498 exception = false; 499 try { 500 Long.valueOf("-9223372036854775809", 10); 501 } catch (NumberFormatException e) { 502 // correct 503 exception = true; 504 } 505 assertTrue("Failed to throw exception when passed invalid string", 506 exception); 507 } 508 509 /** 510 * java.lang.Long#valueOf(long) 511 */ 512 public void test_valueOfJ() { 513 assertEquals(new Long(Long.MIN_VALUE), Long.valueOf(Long.MIN_VALUE)); 514 assertEquals(new Long(Long.MAX_VALUE), Long.valueOf(Long.MAX_VALUE)); 515 assertEquals(new Long(0), Long.valueOf(0)); 516 517 long lng = -128; 518 while (lng < 128) { 519 assertEquals(new Long(lng), Long.valueOf(lng)); 520 assertSame(Long.valueOf(lng), Long.valueOf(lng)); 521 lng++; 522 } 523 } 524 525 /** 526 * java.lang.Long#hashCode() 527 */ 528 public void test_hashCode() { 529 assertEquals((int) (1L ^ (1L >>> 32)), new Long(1).hashCode()); 530 assertEquals((int) (2L ^ (2L >>> 32)), new Long(2).hashCode()); 531 assertEquals((int) (0L ^ (0L >>> 32)), new Long(0).hashCode()); 532 assertEquals((int) (-1L ^ (-1L >>> 32)), new Long(-1).hashCode()); 533 } 534 535 /** 536 * java.lang.Long#Long(String) 537 */ 538 public void test_ConstructorLjava_lang_String() { 539 assertEquals(new Long(0), new Long("0")); 540 assertEquals(new Long(1), new Long("1")); 541 assertEquals(new Long(-1), new Long("-1")); 542 543 try { 544 new Long("0x1"); 545 fail("Expected NumberFormatException with hex string."); 546 } catch (NumberFormatException e) { 547 } 548 549 try { 550 new Long("9.2"); 551 fail("Expected NumberFormatException with floating point string."); 552 } catch (NumberFormatException e) { 553 } 554 555 try { 556 new Long(""); 557 fail("Expected NumberFormatException with empty string."); 558 } catch (NumberFormatException e) { 559 } 560 561 try { 562 new Long(null); 563 fail("Expected NumberFormatException with null string."); 564 } catch (NumberFormatException e) { 565 } 566 } 567 568 /** 569 * java.lang.Long#Long 570 */ 571 public void test_ConstructorJ() { 572 assertEquals(1, new Long(1).intValue()); 573 assertEquals(2, new Long(2).intValue()); 574 assertEquals(0, new Long(0).intValue()); 575 assertEquals(-1, new Long(-1).intValue()); 576 } 577 578 /** 579 * java.lang.Long#byteValue() 580 */ 581 public void test_booleanValue() { 582 assertEquals(1, new Long(1).byteValue()); 583 assertEquals(2, new Long(2).byteValue()); 584 assertEquals(0, new Long(0).byteValue()); 585 assertEquals(-1, new Long(-1).byteValue()); 586 } 587 588 /** 589 * java.lang.Long#equals(Object) 590 */ 591 public void test_equalsLjava_lang_Object() { 592 assertEquals(new Long(0), Long.valueOf(0)); 593 assertEquals(new Long(1), Long.valueOf(1)); 594 assertEquals(new Long(-1), Long.valueOf(-1)); 595 596 Long fixture = new Long(25); 597 assertEquals(fixture, fixture); 598 assertFalse(fixture.equals(null)); 599 assertFalse(fixture.equals("Not a Long")); 600 } 601 602 /** 603 * java.lang.Long#toString() 604 */ 605 public void test_toString() { 606 assertEquals("-1", new Long(-1).toString()); 607 assertEquals("0", new Long(0).toString()); 608 assertEquals("1", new Long(1).toString()); 609 assertEquals("-1", new Long(0xFFFFFFFF).toString()); 610 } 611 612 /** 613 * java.lang.Long#toString 614 */ 615 public void test_toStringJ() { 616 assertEquals("-1", Long.toString(-1)); 617 assertEquals("0", Long.toString(0)); 618 assertEquals("1", Long.toString(1)); 619 assertEquals("-1", Long.toString(0xFFFFFFFF)); 620 } 621 622 /** 623 * java.lang.Long#valueOf(String) 624 */ 625 public void test_valueOfLjava_lang_String() { 626 assertEquals(new Long(0), Long.valueOf("0")); 627 assertEquals(new Long(1), Long.valueOf("1")); 628 assertEquals(new Long(-1), Long.valueOf("-1")); 629 630 try { 631 Long.valueOf("0x1"); 632 fail("Expected NumberFormatException with hex string."); 633 } catch (NumberFormatException e) { 634 } 635 636 try { 637 Long.valueOf("9.2"); 638 fail("Expected NumberFormatException with floating point string."); 639 } catch (NumberFormatException e) { 640 } 641 642 try { 643 Long.valueOf(""); 644 fail("Expected NumberFormatException with empty string."); 645 } catch (NumberFormatException e) { 646 } 647 648 try { 649 Long.valueOf(null); 650 fail("Expected NumberFormatException with null string."); 651 } catch (NumberFormatException e) { 652 } 653 } 654 655 /** 656 * java.lang.Long#valueOf(String, long) 657 */ 658 public void test_valueOfLjava_lang_StringJ() { 659 assertEquals(new Long(0), Long.valueOf("0", 10)); 660 assertEquals(new Long(1), Long.valueOf("1", 10)); 661 assertEquals(new Long(-1), Long.valueOf("-1", 10)); 662 663 //must be consistent with Character.digit() 664 assertEquals(Character.digit('1', 2), Long.valueOf("1", 2).byteValue()); 665 assertEquals(Character.digit('F', 16), Long.valueOf("F", 16).byteValue()); 666 667 try { 668 Long.valueOf("0x1", 10); 669 fail("Expected NumberFormatException with hex string."); 670 } catch (NumberFormatException e) { 671 } 672 673 try { 674 Long.valueOf("9.2", 10); 675 fail("Expected NumberFormatException with floating point string."); 676 } catch (NumberFormatException e) { 677 } 678 679 try { 680 Long.valueOf("", 10); 681 fail("Expected NumberFormatException with empty string."); 682 } catch (NumberFormatException e) { 683 } 684 685 try { 686 Long.valueOf(null, 10); 687 fail("Expected NumberFormatException with null string."); 688 } catch (NumberFormatException e) { 689 } 690 } 691 692 /** 693 * java.lang.Long#parseLong(String) 694 */ 695 public void test_parseLongLjava_lang_String() { 696 assertEquals(0, Long.parseLong("0")); 697 assertEquals(1, Long.parseLong("1")); 698 assertEquals(-1, Long.parseLong("-1")); 699 700 try { 701 Long.parseLong("0x1"); 702 fail("Expected NumberFormatException with hex string."); 703 } catch (NumberFormatException e) { 704 } 705 706 try { 707 Long.parseLong("9.2"); 708 fail("Expected NumberFormatException with floating point string."); 709 } catch (NumberFormatException e) { 710 } 711 712 try { 713 Long.parseLong(""); 714 fail("Expected NumberFormatException with empty string."); 715 } catch (NumberFormatException e) { 716 } 717 718 try { 719 Long.parseLong(null); 720 fail("Expected NumberFormatException with null string."); 721 } catch (NumberFormatException e) { 722 } 723 } 724 725 /** 726 * java.lang.Long#parseLong(String, long) 727 */ 728 public void test_parseLongLjava_lang_StringJ() { 729 assertEquals(0, Long.parseLong("0", 10)); 730 assertEquals(1, Long.parseLong("1", 10)); 731 assertEquals(-1, Long.parseLong("-1", 10)); 732 733 //must be consistent with Character.digit() 734 assertEquals(Character.digit('1', 2), Long.parseLong("1", 2)); 735 assertEquals(Character.digit('F', 16), Long.parseLong("F", 16)); 736 737 try { 738 Long.parseLong("0x1", 10); 739 fail("Expected NumberFormatException with hex string."); 740 } catch (NumberFormatException e) { 741 } 742 743 try { 744 Long.parseLong("9.2", 10); 745 fail("Expected NumberFormatException with floating point string."); 746 } catch (NumberFormatException e) { 747 } 748 749 try { 750 Long.parseLong("", 10); 751 fail("Expected NumberFormatException with empty string."); 752 } catch (NumberFormatException e) { 753 } 754 755 try { 756 Long.parseLong(null, 10); 757 fail("Expected NumberFormatException with null string."); 758 } catch (NumberFormatException e) { 759 } 760 } 761 762 /** 763 * java.lang.Long#decode(String) 764 */ 765 public void test_decodeLjava_lang_String() { 766 assertEquals(new Long(0), Long.decode("0")); 767 assertEquals(new Long(1), Long.decode("1")); 768 assertEquals(new Long(-1), Long.decode("-1")); 769 assertEquals(new Long(0xF), Long.decode("0xF")); 770 assertEquals(new Long(0xF), Long.decode("#F")); 771 assertEquals(new Long(0xF), Long.decode("0XF")); 772 assertEquals(new Long(07), Long.decode("07")); 773 774 try { 775 Long.decode("9.2"); 776 fail("Expected NumberFormatException with floating point string."); 777 } catch (NumberFormatException e) { 778 } 779 780 try { 781 Long.decode(""); 782 fail("Expected NumberFormatException with empty string."); 783 } catch (NumberFormatException e) { 784 } 785 786 try { 787 Long.decode(null); 788 //undocumented NPE, but seems consistent across JREs 789 fail("Expected NullPointerException with null string."); 790 } catch (NullPointerException e) { 791 } 792 } 793 794 /** 795 * java.lang.Long#doubleValue() 796 */ 797 public void test_doubleValue() { 798 assertEquals(-1D, new Long(-1).doubleValue(), 0D); 799 assertEquals(0D, new Long(0).doubleValue(), 0D); 800 assertEquals(1D, new Long(1).doubleValue(), 0D); 801 } 802 803 /** 804 * java.lang.Long#floatValue() 805 */ 806 public void test_floatValue() { 807 assertEquals(-1F, new Long(-1).floatValue(), 0F); 808 assertEquals(0F, new Long(0).floatValue(), 0F); 809 assertEquals(1F, new Long(1).floatValue(), 0F); 810 } 811 812 /** 813 * java.lang.Long#intValue() 814 */ 815 public void test_intValue() { 816 assertEquals(-1, new Long(-1).intValue()); 817 assertEquals(0, new Long(0).intValue()); 818 assertEquals(1, new Long(1).intValue()); 819 } 820 821 /** 822 * java.lang.Long#longValue() 823 */ 824 public void test_longValue() { 825 assertEquals(-1L, new Long(-1).longValue()); 826 assertEquals(0L, new Long(0).longValue()); 827 assertEquals(1L, new Long(1).longValue()); 828 } 829 830 /** 831 * java.lang.Long#shortValue() 832 */ 833 public void test_shortValue() { 834 assertEquals(-1, new Long(-1).shortValue()); 835 assertEquals(0, new Long(0).shortValue()); 836 assertEquals(1, new Long(1).shortValue()); 837 } 838 839 /** 840 * java.lang.Long#highestOneBit(long) 841 */ 842 public void test_highestOneBitJ() { 843 assertEquals(0x08, Long.highestOneBit(0x0A)); 844 assertEquals(0x08, Long.highestOneBit(0x0B)); 845 assertEquals(0x08, Long.highestOneBit(0x0C)); 846 assertEquals(0x08, Long.highestOneBit(0x0F)); 847 assertEquals(0x80, Long.highestOneBit(0xFF)); 848 849 assertEquals(0x080000, Long.highestOneBit(0x0F1234)); 850 assertEquals(0x800000, Long.highestOneBit(0xFF9977)); 851 852 assertEquals(0x8000000000000000L, Long.highestOneBit(0xFFFFFFFFFFFFFFFFL)); 853 854 assertEquals(0, Long.highestOneBit(0)); 855 assertEquals(1, Long.highestOneBit(1)); 856 assertEquals(0x8000000000000000L, Long.highestOneBit(-1)); 857 } 858 859 /** 860 * java.lang.Long#lowestOneBit(long) 861 */ 862 public void test_lowestOneBitJ() { 863 assertEquals(0x10, Long.lowestOneBit(0xF0)); 864 865 assertEquals(0x10, Long.lowestOneBit(0x90)); 866 assertEquals(0x10, Long.lowestOneBit(0xD0)); 867 868 assertEquals(0x10, Long.lowestOneBit(0x123490)); 869 assertEquals(0x10, Long.lowestOneBit(0x1234D0)); 870 871 assertEquals(0x100000, Long.lowestOneBit(0x900000)); 872 assertEquals(0x100000, Long.lowestOneBit(0xD00000)); 873 874 assertEquals(0x40, Long.lowestOneBit(0x40)); 875 assertEquals(0x40, Long.lowestOneBit(0xC0)); 876 877 assertEquals(0x4000, Long.lowestOneBit(0x4000)); 878 assertEquals(0x4000, Long.lowestOneBit(0xC000)); 879 880 assertEquals(0x4000, Long.lowestOneBit(0x99994000)); 881 assertEquals(0x4000, Long.lowestOneBit(0x9999C000)); 882 883 assertEquals(0, Long.lowestOneBit(0)); 884 assertEquals(1, Long.lowestOneBit(1)); 885 assertEquals(1, Long.lowestOneBit(-1)); 886 } 887 888 /** 889 * java.lang.Long#numberOfLeadingZeros(long) 890 */ 891 public void test_numberOfLeadingZerosJ() { 892 assertEquals(64, Long.numberOfLeadingZeros(0x0L)); 893 assertEquals(63, Long.numberOfLeadingZeros(0x1)); 894 assertEquals(62, Long.numberOfLeadingZeros(0x2)); 895 assertEquals(62, Long.numberOfLeadingZeros(0x3)); 896 assertEquals(61, Long.numberOfLeadingZeros(0x4)); 897 assertEquals(61, Long.numberOfLeadingZeros(0x5)); 898 assertEquals(61, Long.numberOfLeadingZeros(0x6)); 899 assertEquals(61, Long.numberOfLeadingZeros(0x7)); 900 assertEquals(60, Long.numberOfLeadingZeros(0x8)); 901 assertEquals(60, Long.numberOfLeadingZeros(0x9)); 902 assertEquals(60, Long.numberOfLeadingZeros(0xA)); 903 assertEquals(60, Long.numberOfLeadingZeros(0xB)); 904 assertEquals(60, Long.numberOfLeadingZeros(0xC)); 905 assertEquals(60, Long.numberOfLeadingZeros(0xD)); 906 assertEquals(60, Long.numberOfLeadingZeros(0xE)); 907 assertEquals(60, Long.numberOfLeadingZeros(0xF)); 908 assertEquals(59, Long.numberOfLeadingZeros(0x10)); 909 assertEquals(56, Long.numberOfLeadingZeros(0x80)); 910 assertEquals(56, Long.numberOfLeadingZeros(0xF0)); 911 assertEquals(55, Long.numberOfLeadingZeros(0x100)); 912 assertEquals(52, Long.numberOfLeadingZeros(0x800)); 913 assertEquals(52, Long.numberOfLeadingZeros(0xF00)); 914 assertEquals(51, Long.numberOfLeadingZeros(0x1000)); 915 assertEquals(48, Long.numberOfLeadingZeros(0x8000)); 916 assertEquals(48, Long.numberOfLeadingZeros(0xF000)); 917 assertEquals(47, Long.numberOfLeadingZeros(0x10000)); 918 assertEquals(44, Long.numberOfLeadingZeros(0x80000)); 919 assertEquals(44, Long.numberOfLeadingZeros(0xF0000)); 920 assertEquals(43, Long.numberOfLeadingZeros(0x100000)); 921 assertEquals(40, Long.numberOfLeadingZeros(0x800000)); 922 assertEquals(40, Long.numberOfLeadingZeros(0xF00000)); 923 assertEquals(39, Long.numberOfLeadingZeros(0x1000000)); 924 assertEquals(36, Long.numberOfLeadingZeros(0x8000000)); 925 assertEquals(36, Long.numberOfLeadingZeros(0xF000000)); 926 assertEquals(35, Long.numberOfLeadingZeros(0x10000000)); 927 assertEquals(0, Long.numberOfLeadingZeros(0x80000000)); 928 assertEquals(0, Long.numberOfLeadingZeros(0xF0000000)); 929 930 assertEquals(1, Long.numberOfLeadingZeros(Long.MAX_VALUE)); 931 assertEquals(0, Long.numberOfLeadingZeros(Long.MIN_VALUE)); 932 } 933 934 /** 935 * java.lang.Long#numberOfTrailingZeros(long) 936 */ 937 public void test_numberOfTrailingZerosJ() { 938 assertEquals(64, Long.numberOfTrailingZeros(0x0)); 939 assertEquals(63, Long.numberOfTrailingZeros(Long.MIN_VALUE)); 940 assertEquals(0, Long.numberOfTrailingZeros(Long.MAX_VALUE)); 941 942 assertEquals(0, Long.numberOfTrailingZeros(0x1)); 943 assertEquals(3, Long.numberOfTrailingZeros(0x8)); 944 assertEquals(0, Long.numberOfTrailingZeros(0xF)); 945 946 assertEquals(4, Long.numberOfTrailingZeros(0x10)); 947 assertEquals(7, Long.numberOfTrailingZeros(0x80)); 948 assertEquals(4, Long.numberOfTrailingZeros(0xF0)); 949 950 assertEquals(8, Long.numberOfTrailingZeros(0x100)); 951 assertEquals(11, Long.numberOfTrailingZeros(0x800)); 952 assertEquals(8, Long.numberOfTrailingZeros(0xF00)); 953 954 assertEquals(12, Long.numberOfTrailingZeros(0x1000)); 955 assertEquals(15, Long.numberOfTrailingZeros(0x8000)); 956 assertEquals(12, Long.numberOfTrailingZeros(0xF000)); 957 958 assertEquals(16, Long.numberOfTrailingZeros(0x10000)); 959 assertEquals(19, Long.numberOfTrailingZeros(0x80000)); 960 assertEquals(16, Long.numberOfTrailingZeros(0xF0000)); 961 962 assertEquals(20, Long.numberOfTrailingZeros(0x100000)); 963 assertEquals(23, Long.numberOfTrailingZeros(0x800000)); 964 assertEquals(20, Long.numberOfTrailingZeros(0xF00000)); 965 966 assertEquals(24, Long.numberOfTrailingZeros(0x1000000)); 967 assertEquals(27, Long.numberOfTrailingZeros(0x8000000)); 968 assertEquals(24, Long.numberOfTrailingZeros(0xF000000)); 969 970 assertEquals(28, Long.numberOfTrailingZeros(0x10000000)); 971 assertEquals(31, Long.numberOfTrailingZeros(0x80000000)); 972 assertEquals(28, Long.numberOfTrailingZeros(0xF0000000)); 973 } 974 975 /** 976 * java.lang.Long#bitCount(long) 977 */ 978 public void test_bitCountJ() { 979 assertEquals(0, Long.bitCount(0x0)); 980 assertEquals(1, Long.bitCount(0x1)); 981 assertEquals(1, Long.bitCount(0x2)); 982 assertEquals(2, Long.bitCount(0x3)); 983 assertEquals(1, Long.bitCount(0x4)); 984 assertEquals(2, Long.bitCount(0x5)); 985 assertEquals(2, Long.bitCount(0x6)); 986 assertEquals(3, Long.bitCount(0x7)); 987 assertEquals(1, Long.bitCount(0x8)); 988 assertEquals(2, Long.bitCount(0x9)); 989 assertEquals(2, Long.bitCount(0xA)); 990 assertEquals(3, Long.bitCount(0xB)); 991 assertEquals(2, Long.bitCount(0xC)); 992 assertEquals(3, Long.bitCount(0xD)); 993 assertEquals(3, Long.bitCount(0xE)); 994 assertEquals(4, Long.bitCount(0xF)); 995 996 assertEquals(8, Long.bitCount(0xFF)); 997 assertEquals(12, Long.bitCount(0xFFF)); 998 assertEquals(16, Long.bitCount(0xFFFF)); 999 assertEquals(20, Long.bitCount(0xFFFFF)); 1000 assertEquals(24, Long.bitCount(0xFFFFFF)); 1001 assertEquals(28, Long.bitCount(0xFFFFFFF)); 1002 assertEquals(64, Long.bitCount(0xFFFFFFFFFFFFFFFFL)); 1003 } 1004 1005 /** 1006 * java.lang.Long#rotateLeft(long, long) 1007 */ 1008 public void test_rotateLeftJI() { 1009 assertEquals(0xF, Long.rotateLeft(0xF, 0)); 1010 assertEquals(0xF0, Long.rotateLeft(0xF, 4)); 1011 assertEquals(0xF00, Long.rotateLeft(0xF, 8)); 1012 assertEquals(0xF000, Long.rotateLeft(0xF, 12)); 1013 assertEquals(0xF0000, Long.rotateLeft(0xF, 16)); 1014 assertEquals(0xF00000, Long.rotateLeft(0xF, 20)); 1015 assertEquals(0xF000000, Long.rotateLeft(0xF, 24)); 1016 assertEquals(0xF0000000L, Long.rotateLeft(0xF, 28)); 1017 assertEquals(0xF000000000000000L, Long.rotateLeft(0xF000000000000000L, 64)); 1018 } 1019 1020 /** 1021 * java.lang.Long#rotateRight(long, long) 1022 */ 1023 public void test_rotateRightJI() { 1024 assertEquals(0xF, Long.rotateRight(0xF0, 4)); 1025 assertEquals(0xF, Long.rotateRight(0xF00, 8)); 1026 assertEquals(0xF, Long.rotateRight(0xF000, 12)); 1027 assertEquals(0xF, Long.rotateRight(0xF0000, 16)); 1028 assertEquals(0xF, Long.rotateRight(0xF00000, 20)); 1029 assertEquals(0xF, Long.rotateRight(0xF000000, 24)); 1030 assertEquals(0xF, Long.rotateRight(0xF0000000L, 28)); 1031 assertEquals(0xF000000000000000L, Long.rotateRight(0xF000000000000000L, 64)); 1032 assertEquals(0xF000000000000000L, Long.rotateRight(0xF000000000000000L, 0)); 1033 1034 } 1035 1036 /** 1037 * java.lang.Long#reverseBytes(long) 1038 */ 1039 public void test_reverseBytesJ() { 1040 assertEquals(0xAABBCCDD00112233L, Long.reverseBytes(0x33221100DDCCBBAAL)); 1041 assertEquals(0x1122334455667788L, Long.reverseBytes(0x8877665544332211L)); 1042 assertEquals(0x0011223344556677L, Long.reverseBytes(0x7766554433221100L)); 1043 assertEquals(0x2000000000000002L, Long.reverseBytes(0x0200000000000020L)); 1044 } 1045 1046 /** 1047 * java.lang.Long#reverse(long) 1048 */ 1049 public void test_reverseJ() { 1050 assertEquals(0, Long.reverse(0)); 1051 assertEquals(-1, Long.reverse(-1)); 1052 assertEquals(0x8000000000000000L, Long.reverse(1)); 1053 } 1054 1055 /** 1056 * java.lang.Long#signum(long) 1057 */ 1058 public void test_signumJ() { 1059 for (int i = -128; i < 0; i++) { 1060 assertEquals(-1, Long.signum(i)); 1061 } 1062 assertEquals(0, Long.signum(0)); 1063 for (int i = 1; i <= 127; i++) { 1064 assertEquals(1, Long.signum(i)); 1065 } 1066 } 1067 }