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 junit.framework.TestCase; 20 21 public class ShortTest extends TestCase { 22 private Short sp = new Short((short) 18000); 23 private Short sn = new Short((short) -19000); 24 25 /** 26 * java.lang.Short#byteValue() 27 */ 28 public void test_byteValue() { 29 // Test for method byte java.lang.Short.byteValue() 30 assertEquals("Returned incorrect byte value", 0, new Short(Short.MIN_VALUE) 31 .byteValue()); 32 assertEquals("Returned incorrect byte value", -1, new Short(Short.MAX_VALUE) 33 .byteValue()); 34 } 35 36 /** 37 * java.lang.Short#compareTo(java.lang.Short) 38 */ 39 public void test_compareToLjava_lang_Short() { 40 // Test for method int java.lang.Short.compareTo(java.lang.Short) 41 Short s = new Short((short) 1); 42 Short x = new Short((short) 3); 43 assertTrue( 44 "Should have returned negative value when compared to greater short", 45 s.compareTo(x) < 0); 46 x = new Short((short) -1); 47 assertTrue( 48 "Should have returned positive value when compared to lesser short", 49 s.compareTo(x) > 0); 50 x = new Short((short) 1); 51 assertEquals("Should have returned zero when compared to equal short", 52 0, s.compareTo(x)); 53 54 try { 55 new Short((short) 0).compareTo(null); 56 fail("No NPE"); 57 } catch (NullPointerException e) { 58 } 59 } 60 61 /** 62 * java.lang.Short#decode(java.lang.String) 63 */ 64 public void test_decodeLjava_lang_String2() { 65 // Test for method java.lang.Short 66 // java.lang.Short.decode(java.lang.String) 67 assertTrue("Did not decode -1 correctly", Short.decode("-1") 68 .shortValue() == (short) -1); 69 assertTrue("Did not decode -100 correctly", Short.decode("-100") 70 .shortValue() == (short) -100); 71 assertTrue("Did not decode 23 correctly", Short.decode("23") 72 .shortValue() == (short) 23); 73 assertTrue("Did not decode 0x10 correctly", Short.decode("0x10") 74 .shortValue() == (short) 16); 75 assertTrue("Did not decode 32767 correctly", Short.decode("32767") 76 .shortValue() == (short) 32767); 77 assertTrue("Did not decode -32767 correctly", Short.decode("-32767") 78 .shortValue() == (short) -32767); 79 assertTrue("Did not decode -32768 correctly", Short.decode("-32768") 80 .shortValue() == (short) -32768); 81 82 boolean exception = false; 83 try { 84 Short.decode("123s"); 85 } catch (NumberFormatException e) { 86 // correct 87 exception = true; 88 } 89 assertTrue("Did not throw NumberFormatException decoding 123s", 90 exception); 91 92 exception = false; 93 try { 94 Short.decode("32768"); 95 } catch (NumberFormatException e) { 96 // Correct 97 exception = true; 98 } 99 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 100 101 exception = false; 102 try { 103 Short.decode("-32769"); 104 } catch (NumberFormatException e) { 105 // Correct 106 exception = true; 107 } 108 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 109 110 exception = false; 111 try { 112 Short.decode("0x8000"); 113 } catch (NumberFormatException e) { 114 // Correct 115 exception = true; 116 } 117 assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception); 118 119 exception = false; 120 try { 121 Short.decode("-0x8001"); 122 } catch (NumberFormatException e) { 123 // Correct 124 exception = true; 125 } 126 assertTrue("Failed to throw exception for hex MIN_VALUE - 1", exception); 127 } 128 129 /** 130 * java.lang.Short#parseShort(java.lang.String) 131 */ 132 public void test_parseShortLjava_lang_String2() { 133 // Test for method short java.lang.Short.parseShort(java.lang.String) 134 short sp = Short.parseShort("32746"); 135 short sn = Short.parseShort("-32746"); 136 137 assertTrue("Incorrect parse of short", sp == (short) 32746 138 && (sn == (short) -32746)); 139 assertEquals("Returned incorrect value for 0", 0, Short.parseShort("0")); 140 assertTrue("Returned incorrect value for most negative value", Short 141 .parseShort("-32768") == (short) 0x8000); 142 assertTrue("Returned incorrect value for most positive value", Short 143 .parseShort("32767") == 0x7fff); 144 145 boolean exception = false; 146 try { 147 Short.parseShort("32768"); 148 } catch (NumberFormatException e) { 149 // Correct 150 exception = true; 151 } 152 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 153 154 exception = false; 155 try { 156 Short.parseShort("-32769"); 157 } catch (NumberFormatException e) { 158 // Correct 159 exception = true; 160 } 161 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 162 } 163 164 /** 165 * java.lang.Short#parseShort(java.lang.String, int) 166 */ 167 public void test_parseShortLjava_lang_StringI2() { 168 // Test for method short java.lang.Short.parseShort(java.lang.String, 169 // int) 170 boolean aThrow = true; 171 assertEquals("Incorrectly parsed hex string", 172 255, Short.parseShort("FF", 16)); 173 assertEquals("Incorrectly parsed oct string", 174 16, Short.parseShort("20", 8)); 175 assertEquals("Incorrectly parsed dec string", 176 20, Short.parseShort("20", 10)); 177 assertEquals("Incorrectly parsed bin string", 178 4, Short.parseShort("100", 2)); 179 assertEquals("Incorrectly parsed -hex string", -255, Short 180 .parseShort("-FF", 16)); 181 assertEquals("Incorrectly parsed -oct string", 182 -16, Short.parseShort("-20", 8)); 183 assertEquals("Incorrectly parsed -bin string", -4, Short 184 .parseShort("-100", 2)); 185 assertEquals("Returned incorrect value for 0 hex", 0, Short.parseShort("0", 186 16)); 187 assertTrue("Returned incorrect value for most negative value hex", 188 Short.parseShort("-8000", 16) == (short) 0x8000); 189 assertTrue("Returned incorrect value for most positive value hex", 190 Short.parseShort("7fff", 16) == 0x7fff); 191 assertEquals("Returned incorrect value for 0 decimal", 0, Short.parseShort( 192 "0", 10)); 193 assertTrue("Returned incorrect value for most negative value decimal", 194 Short.parseShort("-32768", 10) == (short) 0x8000); 195 assertTrue("Returned incorrect value for most positive value decimal", 196 Short.parseShort("32767", 10) == 0x7fff); 197 198 try { 199 Short.parseShort("FF", 2); 200 } catch (NumberFormatException e) { 201 // Correct 202 aThrow = false; 203 } 204 if (aThrow) { 205 fail( 206 "Failed to throw exception when passed hex string and base 2 radix"); 207 } 208 209 boolean exception = false; 210 try { 211 Short.parseShort("10000000000", 10); 212 } catch (NumberFormatException e) { 213 // Correct 214 exception = true; 215 } 216 assertTrue( 217 "Failed to throw exception when passed string larger than 16 bits", 218 exception); 219 220 exception = false; 221 try { 222 Short.parseShort("32768", 10); 223 } catch (NumberFormatException e) { 224 // Correct 225 exception = true; 226 } 227 assertTrue("Failed to throw exception for MAX_VALUE + 1", exception); 228 229 exception = false; 230 try { 231 Short.parseShort("-32769", 10); 232 } catch (NumberFormatException e) { 233 // Correct 234 exception = true; 235 } 236 assertTrue("Failed to throw exception for MIN_VALUE - 1", exception); 237 238 exception = false; 239 try { 240 Short.parseShort("8000", 16); 241 } catch (NumberFormatException e) { 242 // Correct 243 exception = true; 244 } 245 assertTrue("Failed to throw exception for hex MAX_VALUE + 1", exception); 246 247 exception = false; 248 try { 249 Short.parseShort("-8001", 16); 250 } catch (NumberFormatException e) { 251 // Correct 252 exception = true; 253 } 254 assertTrue("Failed to throw exception for hex MIN_VALUE + 1", exception); 255 } 256 257 /** 258 * java.lang.Short#toString() 259 */ 260 public void test_toString2() { 261 // Test for method java.lang.String java.lang.Short.toString() 262 assertTrue("Invalid string returned", sp.toString().equals("18000") 263 && (sn.toString().equals("-19000"))); 264 assertEquals("Returned incorrect string", "32767", new Short((short) 32767) 265 .toString()); 266 assertEquals("Returned incorrect string", "-32767", new Short((short) -32767) 267 .toString()); 268 assertEquals("Returned incorrect string", "-32768", new Short((short) -32768) 269 .toString()); 270 } 271 272 /** 273 * java.lang.Short#toString(short) 274 */ 275 public void test_toStringS2() { 276 // Test for method java.lang.String java.lang.Short.toString(short) 277 assertEquals("Returned incorrect string", "32767", Short.toString((short) 32767) 278 ); 279 assertEquals("Returned incorrect string", "-32767", Short.toString((short) -32767) 280 ); 281 assertEquals("Returned incorrect string", "-32768", Short.toString((short) -32768) 282 ); 283 } 284 285 /** 286 * java.lang.Short#valueOf(java.lang.String) 287 */ 288 public void test_valueOfLjava_lang_String2() { 289 // Test for method java.lang.Short 290 // java.lang.Short.valueOf(java.lang.String) 291 assertEquals("Returned incorrect short", -32768, Short.valueOf("-32768") 292 .shortValue()); 293 assertEquals("Returned incorrect short", 32767, Short.valueOf("32767") 294 .shortValue()); 295 } 296 297 /** 298 * java.lang.Short#valueOf(java.lang.String, int) 299 */ 300 public void test_valueOfLjava_lang_StringI2() { 301 // Test for method java.lang.Short 302 // java.lang.Short.valueOf(java.lang.String, int) 303 boolean aThrow = true; 304 assertEquals("Incorrectly parsed hex string", 255, Short.valueOf("FF", 16) 305 .shortValue()); 306 assertEquals("Incorrectly parsed oct string", 16, Short.valueOf("20", 8) 307 .shortValue()); 308 assertEquals("Incorrectly parsed dec string", 20, Short.valueOf("20", 10) 309 .shortValue()); 310 assertEquals("Incorrectly parsed bin string", 4, Short.valueOf("100", 2) 311 .shortValue()); 312 assertEquals("Incorrectly parsed -hex string", -255, Short.valueOf("-FF", 16) 313 .shortValue()); 314 assertEquals("Incorrectly parsed -oct string", -16, Short.valueOf("-20", 8) 315 .shortValue()); 316 assertEquals("Incorrectly parsed -bin string", -4, Short.valueOf("-100", 2) 317 .shortValue()); 318 assertTrue("Did not decode 32767 correctly", Short.valueOf("32767", 10) 319 .shortValue() == (short) 32767); 320 assertTrue("Did not decode -32767 correctly", Short.valueOf("-32767", 321 10).shortValue() == (short) -32767); 322 assertTrue("Did not decode -32768 correctly", Short.valueOf("-32768", 323 10).shortValue() == (short) -32768); 324 try { 325 Short.valueOf("FF", 2); 326 } catch (NumberFormatException e) { 327 // Correct 328 aThrow = false; 329 } 330 if (aThrow) { 331 fail( 332 "Failed to throw exception when passed hex string and base 2 radix"); 333 } 334 try { 335 Short.valueOf("10000000000", 10); 336 } catch (NumberFormatException e) { 337 // Correct 338 return; 339 } 340 fail( 341 "Failed to throw exception when passed string larger than 16 bits"); 342 } 343 344 /** 345 * java.lang.Short#valueOf(byte) 346 */ 347 public void test_valueOfS() { 348 assertEquals(new Short(Short.MIN_VALUE), Short.valueOf(Short.MIN_VALUE)); 349 assertEquals(new Short(Short.MAX_VALUE), Short.valueOf(Short.MAX_VALUE)); 350 assertEquals(new Short((short) 0), Short.valueOf((short) 0)); 351 352 short s = -128; 353 while (s < 128) { 354 assertEquals(new Short(s), Short.valueOf(s)); 355 assertSame(Short.valueOf(s), Short.valueOf(s)); 356 s++; 357 } 358 } 359 360 /** 361 * java.lang.Short#hashCode() 362 */ 363 public void test_hashCode() { 364 assertEquals(1, new Short((short) 1).hashCode()); 365 assertEquals(2, new Short((short) 2).hashCode()); 366 assertEquals(0, new Short((short) 0).hashCode()); 367 assertEquals(-1, new Short((short) -1).hashCode()); 368 } 369 370 /** 371 * java.lang.Short#Short(String) 372 */ 373 public void test_ConstructorLjava_lang_String() { 374 assertEquals(new Short((short) 0), new Short("0")); 375 assertEquals(new Short((short) 1), new Short("1")); 376 assertEquals(new Short((short) -1), new Short("-1")); 377 378 try { 379 new Short("0x1"); 380 fail("Expected NumberFormatException with hex string."); 381 } catch (NumberFormatException e) { 382 } 383 384 try { 385 new Short("9.2"); 386 fail("Expected NumberFormatException with floating point string."); 387 } catch (NumberFormatException e) { 388 } 389 390 try { 391 new Short(""); 392 fail("Expected NumberFormatException with empty string."); 393 } catch (NumberFormatException e) { 394 } 395 396 try { 397 new Short(null); 398 fail("Expected NumberFormatException with null string."); 399 } catch (NumberFormatException e) { 400 } 401 } 402 403 /** 404 * java.lang.Short#Short(short) 405 */ 406 public void test_ConstructorS() { 407 assertEquals(1, new Short((short) 1).shortValue()); 408 assertEquals(2, new Short((short) 2).shortValue()); 409 assertEquals(0, new Short((short) 0).shortValue()); 410 assertEquals(-1, new Short((short) -1).shortValue()); 411 } 412 413 /** 414 * java.lang.Short#byteValue() 415 */ 416 public void test_booleanValue() { 417 assertEquals(1, new Short((short) 1).byteValue()); 418 assertEquals(2, new Short((short) 2).byteValue()); 419 assertEquals(0, new Short((short) 0).byteValue()); 420 assertEquals(-1, new Short((short) -1).byteValue()); 421 } 422 423 /** 424 * java.lang.Short#equals(Object) 425 */ 426 public void test_equalsLjava_lang_Object() { 427 assertEquals(new Short((short) 0), Short.valueOf((short) 0)); 428 assertEquals(new Short((short) 1), Short.valueOf((short) 1)); 429 assertEquals(new Short((short) -1), Short.valueOf((short) -1)); 430 431 Short fixture = new Short((short) 25); 432 assertEquals(fixture, fixture); 433 assertFalse(fixture.equals(null)); 434 assertFalse(fixture.equals("Not a Short")); 435 } 436 437 /** 438 * java.lang.Short#toString() 439 */ 440 public void test_toString() { 441 assertEquals("-1", new Short((short) -1).toString()); 442 assertEquals("0", new Short((short) 0).toString()); 443 assertEquals("1", new Short((short) 1).toString()); 444 assertEquals("-1", new Short((short) 0xFFFF).toString()); 445 } 446 447 /** 448 * java.lang.Short#toString(short) 449 */ 450 public void test_toStringS() { 451 assertEquals("-1", Short.toString((short) -1)); 452 assertEquals("0", Short.toString((short) 0)); 453 assertEquals("1", Short.toString((short) 1)); 454 assertEquals("-1", Short.toString((short) 0xFFFF)); 455 } 456 457 /** 458 * java.lang.Short#valueOf(String) 459 */ 460 public void test_valueOfLjava_lang_String() { 461 assertEquals(new Short((short) 0), Short.valueOf("0")); 462 assertEquals(new Short((short) 1), Short.valueOf("1")); 463 assertEquals(new Short((short) -1), Short.valueOf("-1")); 464 465 try { 466 Short.valueOf("0x1"); 467 fail("Expected NumberFormatException with hex string."); 468 } catch (NumberFormatException e) { 469 } 470 471 try { 472 Short.valueOf("9.2"); 473 fail("Expected NumberFormatException with floating point string."); 474 } catch (NumberFormatException e) { 475 } 476 477 try { 478 Short.valueOf(""); 479 fail("Expected NumberFormatException with empty string."); 480 } catch (NumberFormatException e) { 481 } 482 483 try { 484 Short.valueOf(null); 485 fail("Expected NumberFormatException with null string."); 486 } catch (NumberFormatException e) { 487 } 488 } 489 490 /** 491 * java.lang.Short#valueOf(String, int) 492 */ 493 public void test_valueOfLjava_lang_StringI() { 494 assertEquals(new Short((short) 0), Short.valueOf("0", 10)); 495 assertEquals(new Short((short) 1), Short.valueOf("1", 10)); 496 assertEquals(new Short((short) -1), Short.valueOf("-1", 10)); 497 498 //must be consistent with Character.digit() 499 assertEquals(Character.digit('1', 2), Short.valueOf("1", 2).byteValue()); 500 assertEquals(Character.digit('F', 16), Short.valueOf("F", 16).byteValue()); 501 502 try { 503 Short.valueOf("0x1", 10); 504 fail("Expected NumberFormatException with hex string."); 505 } catch (NumberFormatException e) { 506 } 507 508 try { 509 Short.valueOf("9.2", 10); 510 fail("Expected NumberFormatException with floating point string."); 511 } catch (NumberFormatException e) { 512 } 513 514 try { 515 Short.valueOf("", 10); 516 fail("Expected NumberFormatException with empty string."); 517 } catch (NumberFormatException e) { 518 } 519 520 try { 521 Short.valueOf(null, 10); 522 fail("Expected NumberFormatException with null string."); 523 } catch (NumberFormatException e) { 524 } 525 } 526 527 /** 528 * java.lang.Short#parseShort(String) 529 */ 530 public void test_parseShortLjava_lang_String() { 531 assertEquals(0, Short.parseShort("0")); 532 assertEquals(1, Short.parseShort("1")); 533 assertEquals(-1, Short.parseShort("-1")); 534 535 try { 536 Short.parseShort("0x1"); 537 fail("Expected NumberFormatException with hex string."); 538 } catch (NumberFormatException e) { 539 } 540 541 try { 542 Short.parseShort("9.2"); 543 fail("Expected NumberFormatException with floating point string."); 544 } catch (NumberFormatException e) { 545 } 546 547 try { 548 Short.parseShort(""); 549 fail("Expected NumberFormatException with empty string."); 550 } catch (NumberFormatException e) { 551 } 552 553 try { 554 Short.parseShort(null); 555 fail("Expected NumberFormatException with null string."); 556 } catch (NumberFormatException e) { 557 } 558 } 559 560 /** 561 * java.lang.Short#parseShort(String, int) 562 */ 563 public void test_parseShortLjava_lang_StringI() { 564 assertEquals(0, Short.parseShort("0", 10)); 565 assertEquals(1, Short.parseShort("1", 10)); 566 assertEquals(-1, Short.parseShort("-1", 10)); 567 568 //must be consistent with Character.digit() 569 assertEquals(Character.digit('1', 2), Short.parseShort("1", 2)); 570 assertEquals(Character.digit('F', 16), Short.parseShort("F", 16)); 571 572 try { 573 Short.parseShort("0x1", 10); 574 fail("Expected NumberFormatException with hex string."); 575 } catch (NumberFormatException e) { 576 } 577 578 try { 579 Short.parseShort("9.2", 10); 580 fail("Expected NumberFormatException with floating point string."); 581 } catch (NumberFormatException e) { 582 } 583 584 try { 585 Short.parseShort("", 10); 586 fail("Expected NumberFormatException with empty string."); 587 } catch (NumberFormatException e) { 588 } 589 590 try { 591 Short.parseShort(null, 10); 592 fail("Expected NumberFormatException with null string."); 593 } catch (NumberFormatException e) { 594 } 595 } 596 597 /** 598 * java.lang.Short#decode(String) 599 */ 600 public void test_decodeLjava_lang_String() { 601 assertEquals(new Short((short) 0), Short.decode("0")); 602 assertEquals(new Short((short) 1), Short.decode("1")); 603 assertEquals(new Short((short) -1), Short.decode("-1")); 604 assertEquals(new Short((short) 0xF), Short.decode("0xF")); 605 assertEquals(new Short((short) 0xF), Short.decode("#F")); 606 assertEquals(new Short((short) 0xF), Short.decode("0XF")); 607 assertEquals(new Short((short) 07), Short.decode("07")); 608 609 try { 610 Short.decode("9.2"); 611 fail("Expected NumberFormatException with floating point string."); 612 } catch (NumberFormatException e) { 613 } 614 615 try { 616 Short.decode(""); 617 fail("Expected NumberFormatException with empty string."); 618 } catch (NumberFormatException e) { 619 } 620 621 try { 622 Short.decode(null); 623 //undocumented NPE, but seems consistent across JREs 624 fail("Expected NullPointerException with null string."); 625 } catch (NullPointerException e) { 626 } 627 } 628 629 /** 630 * java.lang.Short#doubleValue() 631 */ 632 public void test_doubleValue() { 633 assertEquals(-1D, new Short((short) -1).doubleValue(), 0D); 634 assertEquals(0D, new Short((short) 0).doubleValue(), 0D); 635 assertEquals(1D, new Short((short) 1).doubleValue(), 0D); 636 } 637 638 /** 639 * java.lang.Short#floatValue() 640 */ 641 public void test_floatValue() { 642 assertEquals(-1F, new Short((short) -1).floatValue(), 0F); 643 assertEquals(0F, new Short((short) 0).floatValue(), 0F); 644 assertEquals(1F, new Short((short) 1).floatValue(), 0F); 645 } 646 647 /** 648 * java.lang.Short#intValue() 649 */ 650 public void test_intValue() { 651 assertEquals(-1, new Short((short) -1).intValue()); 652 assertEquals(0, new Short((short) 0).intValue()); 653 assertEquals(1, new Short((short) 1).intValue()); 654 } 655 656 /** 657 * java.lang.Short#longValue() 658 */ 659 public void test_longValue() { 660 assertEquals(-1L, new Short((short) -1).longValue()); 661 assertEquals(0L, new Short((short) 0).longValue()); 662 assertEquals(1L, new Short((short) 1).longValue()); 663 } 664 665 /** 666 * java.lang.Short#shortValue() 667 */ 668 public void test_shortValue() { 669 assertEquals(-1, new Short((short) -1).shortValue()); 670 assertEquals(0, new Short((short) 0).shortValue()); 671 assertEquals(1, new Short((short) 1).shortValue()); 672 } 673 674 /** 675 * java.lang.Short#reverseBytes(short) 676 */ 677 public void test_reverseBytesS() { 678 assertEquals((short) 0xABCD, Short.reverseBytes((short) 0xCDAB)); 679 assertEquals((short) 0x1234, Short.reverseBytes((short) 0x3412)); 680 assertEquals((short) 0x0011, Short.reverseBytes((short) 0x1100)); 681 assertEquals((short) 0x2002, Short.reverseBytes((short) 0x0220)); 682 } 683 684 } 685