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 17package org.apache.harmony.luni.tests.java.lang; 18 19import junit.framework.TestCase; 20 21public class ShortTest extends TestCase { 22 private Short sp = new Short((short) 18000); 23 private Short sn = new Short((short) -19000); 24 25 /** 26 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests 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 * @tests java.lang.Short#valueOf(byte) 345 */ 346 public void test_valueOfS() { 347 assertEquals(new Short(Short.MIN_VALUE), Short.valueOf(Short.MIN_VALUE)); 348 assertEquals(new Short(Short.MAX_VALUE), Short.valueOf(Short.MAX_VALUE)); 349 assertEquals(new Short((short) 0), Short.valueOf((short) 0)); 350 351 short s = -128; 352 while (s < 128) { 353 assertEquals(new Short(s), Short.valueOf(s)); 354 assertSame(Short.valueOf(s), Short.valueOf(s)); 355 s++; 356 } 357 } 358 359 /** 360 * @tests java.lang.Short#hashCode() 361 */ 362 public void test_hashCode() { 363 assertEquals(1, new Short((short)1).hashCode()); 364 assertEquals(2, new Short((short)2).hashCode()); 365 assertEquals(0, new Short((short)0).hashCode()); 366 assertEquals(-1, new Short((short)-1).hashCode()); 367 } 368 369 /** 370 * @tests java.lang.Short#Short(String) 371 */ 372 public void test_ConstructorLjava_lang_String() { 373 assertEquals(new Short((short)0), new Short("0")); 374 assertEquals(new Short((short)1), new Short("1")); 375 assertEquals(new Short((short)-1), new Short("-1")); 376 377 try { 378 new Short("0x1"); 379 fail("Expected NumberFormatException with hex string."); 380 } catch (NumberFormatException e) {} 381 382 try { 383 new Short("9.2"); 384 fail("Expected NumberFormatException with floating point string."); 385 } catch (NumberFormatException e) {} 386 387 try { 388 new Short(""); 389 fail("Expected NumberFormatException with empty string."); 390 } catch (NumberFormatException e) {} 391 392 try { 393 new Short(null); 394 fail("Expected NumberFormatException with null string."); 395 } catch (NumberFormatException e) {} 396 } 397 398 /** 399 * @tests java.lang.Short#Short(short) 400 */ 401 public void test_ConstructorS() { 402 assertEquals(1, new Short((short)1).shortValue()); 403 assertEquals(2, new Short((short)2).shortValue()); 404 assertEquals(0, new Short((short)0).shortValue()); 405 assertEquals(-1, new Short((short)-1).shortValue()); 406 } 407 408 /** 409 * @tests java.lang.Short#byteValue() 410 */ 411 public void test_booleanValue() { 412 assertEquals(1, new Short((short)1).byteValue()); 413 assertEquals(2, new Short((short)2).byteValue()); 414 assertEquals(0, new Short((short)0).byteValue()); 415 assertEquals(-1, new Short((short)-1).byteValue()); 416 } 417 418 /** 419 * @tests java.lang.Short#equals(Object) 420 */ 421 public void test_equalsLjava_lang_Object() { 422 assertEquals(new Short((short)0), Short.valueOf((short)0)); 423 assertEquals(new Short((short)1), Short.valueOf((short)1)); 424 assertEquals(new Short((short)-1), Short.valueOf((short)-1)); 425 426 Short fixture = new Short((short)25); 427 assertEquals(fixture, fixture); 428 assertFalse(fixture.equals(null)); 429 assertFalse(fixture.equals("Not a Short")); 430 } 431 432 /** 433 * @tests java.lang.Short#toString() 434 */ 435 public void test_toString() { 436 assertEquals("-1", new Short((short)-1).toString()); 437 assertEquals("0", new Short((short)0).toString()); 438 assertEquals("1", new Short((short)1).toString()); 439 assertEquals("-1", new Short((short)0xFFFF).toString()); 440 } 441 442 /** 443 * @tests java.lang.Short#toString(short) 444 */ 445 public void test_toStringS() { 446 assertEquals("-1", Short.toString((short)-1)); 447 assertEquals("0", Short.toString((short)0)); 448 assertEquals("1", Short.toString((short)1)); 449 assertEquals("-1", Short.toString((short)0xFFFF)); 450 } 451 452 /** 453 * @tests java.lang.Short#valueOf(String) 454 */ 455 public void test_valueOfLjava_lang_String() { 456 assertEquals(new Short((short)0), Short.valueOf("0")); 457 assertEquals(new Short((short)1), Short.valueOf("1")); 458 assertEquals(new Short((short)-1), Short.valueOf("-1")); 459 460 try { 461 Short.valueOf("0x1"); 462 fail("Expected NumberFormatException with hex string."); 463 } catch (NumberFormatException e) {} 464 465 try { 466 Short.valueOf("9.2"); 467 fail("Expected NumberFormatException with floating point string."); 468 } catch (NumberFormatException e) {} 469 470 try { 471 Short.valueOf(""); 472 fail("Expected NumberFormatException with empty string."); 473 } catch (NumberFormatException e) {} 474 475 try { 476 Short.valueOf(null); 477 fail("Expected NumberFormatException with null string."); 478 } catch (NumberFormatException e) {} 479 } 480 481 /** 482 * @tests java.lang.Short#valueOf(String,int) 483 */ 484 public void test_valueOfLjava_lang_StringI() { 485 assertEquals(new Short((short)0), Short.valueOf("0", 10)); 486 assertEquals(new Short((short)1), Short.valueOf("1", 10)); 487 assertEquals(new Short((short)-1), Short.valueOf("-1", 10)); 488 489 //must be consistent with Character.digit() 490 assertEquals(Character.digit('1', 2), Short.valueOf("1", 2).byteValue()); 491 assertEquals(Character.digit('F', 16), Short.valueOf("F", 16).byteValue()); 492 493 try { 494 Short.valueOf("0x1", 10); 495 fail("Expected NumberFormatException with hex string."); 496 } catch (NumberFormatException e) {} 497 498 try { 499 Short.valueOf("9.2", 10); 500 fail("Expected NumberFormatException with floating point string."); 501 } catch (NumberFormatException e) {} 502 503 try { 504 Short.valueOf("", 10); 505 fail("Expected NumberFormatException with empty string."); 506 } catch (NumberFormatException e) {} 507 508 try { 509 Short.valueOf(null, 10); 510 fail("Expected NumberFormatException with null string."); 511 } catch (NumberFormatException e) {} 512 } 513 514 /** 515 * @tests java.lang.Short#parseShort(String) 516 */ 517 public void test_parseShortLjava_lang_String() { 518 assertEquals(0, Short.parseShort("0")); 519 assertEquals(1, Short.parseShort("1")); 520 assertEquals(-1, Short.parseShort("-1")); 521 522 try { 523 Short.parseShort("0x1"); 524 fail("Expected NumberFormatException with hex string."); 525 } catch (NumberFormatException e) {} 526 527 try { 528 Short.parseShort("9.2"); 529 fail("Expected NumberFormatException with floating point string."); 530 } catch (NumberFormatException e) {} 531 532 try { 533 Short.parseShort(""); 534 fail("Expected NumberFormatException with empty string."); 535 } catch (NumberFormatException e) {} 536 537 try { 538 Short.parseShort(null); 539 fail("Expected NumberFormatException with null string."); 540 } catch (NumberFormatException e) {} 541 } 542 543 /** 544 * @tests java.lang.Short#parseShort(String,int) 545 */ 546 public void test_parseShortLjava_lang_StringI() { 547 assertEquals(0, Short.parseShort("0", 10)); 548 assertEquals(1, Short.parseShort("1", 10)); 549 assertEquals(-1, Short.parseShort("-1", 10)); 550 551 //must be consistent with Character.digit() 552 assertEquals(Character.digit('1', 2), Short.parseShort("1", 2)); 553 assertEquals(Character.digit('F', 16), Short.parseShort("F", 16)); 554 555 try { 556 Short.parseShort("0x1", 10); 557 fail("Expected NumberFormatException with hex string."); 558 } catch (NumberFormatException e) {} 559 560 try { 561 Short.parseShort("9.2", 10); 562 fail("Expected NumberFormatException with floating point string."); 563 } catch (NumberFormatException e) {} 564 565 try { 566 Short.parseShort("", 10); 567 fail("Expected NumberFormatException with empty string."); 568 } catch (NumberFormatException e) {} 569 570 try { 571 Short.parseShort(null, 10); 572 fail("Expected NumberFormatException with null string."); 573 } catch (NumberFormatException e) {} 574 } 575 576 /** 577 * @tests java.lang.Short#decode(String) 578 */ 579 public void test_decodeLjava_lang_String() { 580 assertEquals(new Short((short)0), Short.decode("0")); 581 assertEquals(new Short((short)1), Short.decode("1")); 582 assertEquals(new Short((short)-1), Short.decode("-1")); 583 assertEquals(new Short((short)0xF), Short.decode("0xF")); 584 assertEquals(new Short((short)0xF), Short.decode("#F")); 585 assertEquals(new Short((short)0xF), Short.decode("0XF")); 586 assertEquals(new Short((short)07), Short.decode("07")); 587 588 try { 589 Short.decode("9.2"); 590 fail("Expected NumberFormatException with floating point string."); 591 } catch (NumberFormatException e) {} 592 593 try { 594 Short.decode(""); 595 fail("Expected NumberFormatException with empty string."); 596 } catch (NumberFormatException e) {} 597 598 try { 599 Short.decode(null); 600 //undocumented NPE, but seems consistent across JREs 601 fail("Expected NullPointerException with null string."); 602 } catch (NullPointerException e) {} 603 } 604 605 /** 606 * @tests java.lang.Short#doubleValue() 607 */ 608 public void test_doubleValue() { 609 assertEquals(-1D, new Short((short)-1).doubleValue(), 0D); 610 assertEquals(0D, new Short((short)0).doubleValue(), 0D); 611 assertEquals(1D, new Short((short)1).doubleValue(), 0D); 612 } 613 614 /** 615 * @tests java.lang.Short#floatValue() 616 */ 617 public void test_floatValue() { 618 assertEquals(-1F, new Short((short)-1).floatValue(), 0F); 619 assertEquals(0F, new Short((short)0).floatValue(), 0F); 620 assertEquals(1F, new Short((short)1).floatValue(), 0F); 621 } 622 623 /** 624 * @tests java.lang.Short#intValue() 625 */ 626 public void test_intValue() { 627 assertEquals(-1, new Short((short)-1).intValue()); 628 assertEquals(0, new Short((short)0).intValue()); 629 assertEquals(1, new Short((short)1).intValue()); 630 } 631 632 /** 633 * @tests java.lang.Short#longValue() 634 */ 635 public void test_longValue() { 636 assertEquals(-1L, new Short((short)-1).longValue()); 637 assertEquals(0L, new Short((short)0).longValue()); 638 assertEquals(1L, new Short((short)1).longValue()); 639 } 640 641 /** 642 * @tests java.lang.Short#shortValue() 643 */ 644 public void test_shortValue() { 645 assertEquals(-1, new Short((short)-1).shortValue()); 646 assertEquals(0, new Short((short)0).shortValue()); 647 assertEquals(1, new Short((short)1).shortValue()); 648 } 649 650 /** 651 * @tests java.lang.Short#reverseBytes(short) 652 */ 653 public void test_reverseBytesS() { 654 assertEquals((short)0xABCD, Short.reverseBytes((short)0xCDAB)); 655 assertEquals((short)0x1234, Short.reverseBytes((short)0x3412)); 656 assertEquals((short)0x0011, Short.reverseBytes((short)0x1100)); 657 assertEquals((short)0x2002, Short.reverseBytes((short)0x0220)); 658 } 659 660} 661