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, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.lang; 19 20import dalvik.annotation.TestLevel; 21import dalvik.annotation.TestTargetClass; 22import dalvik.annotation.TestTargetNew; 23 24import junit.framework.TestCase; 25 26import java.io.UnsupportedEncodingException; 27import java.math.BigDecimal; 28 29@TestTargetClass(String.class) 30public class StringTest extends TestCase { 31 32 private static String newString(int start, int len, char[] data) { 33 return new String(data, start,len); 34 } 35 36 /** 37 * @tests java.lang.String#String() 38 */ 39 @TestTargetNew( 40 level = TestLevel.COMPLETE, 41 notes = "", 42 method = "String", 43 args = {} 44 ) 45 public void test_Constructor() { 46 assertEquals("Created incorrect string", "", new String()); 47 } 48 49 /** 50 * @tests java.lang.String#String(byte[]) 51 */ 52 @TestTargetNew( 53 level = TestLevel.COMPLETE, 54 notes = "", 55 method = "String", 56 args = {byte[].class} 57 ) 58 public void test_Constructor$B() { 59 assertEquals("Failed to create string", "HelloWorld", new String( 60 "HelloWorld".getBytes())); 61 } 62 63 /** 64 * @tests java.lang.String#String(byte[], int) 65 */ 66 @TestTargetNew( 67 level = TestLevel.COMPLETE, 68 notes = "", 69 method = "String", 70 args = {byte[].class, int.class} 71 ) 72 @SuppressWarnings("deprecation") 73 public void test_Constructor$BI() { 74 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0); 75 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 76 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1); 77 assertFalse("Did not use nonzero hibyte", s.equals("ABCDE")); 78 } 79 80 /** 81 * @tests java.lang.String#String(byte[], int, int) 82 */ 83 @TestTargetNew( 84 level = TestLevel.COMPLETE, 85 notes = "", 86 method = "String", 87 args = {byte[].class, int.class, int.class} 88 ) 89 public void test_Constructor$BII() { 90 byte[] hwba = "HelloWorld".getBytes(); 91 assertEquals("Failed to create string", "HelloWorld", new String(hwba, 92 0, hwba.length)); 93 94 try { 95 new String(new byte[0], 0, Integer.MAX_VALUE); 96 fail("No IndexOutOfBoundsException"); 97 } catch (IndexOutOfBoundsException e) { 98 } 99 } 100 101 /** 102 * @tests java.lang.String#String(byte[], int, int, int) 103 */ 104 @TestTargetNew( 105 level = TestLevel.PARTIAL, 106 notes = "IndexOutOfBoundsException is not verified.", 107 method = "String", 108 args = {byte[].class, int.class, int.class, int.class} 109 ) 110 @SuppressWarnings("deprecation") 111 public void test_Constructor$BIII() { 112 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 1, 3); 113 assertEquals("Incorrect string returned: " + s, "BCD", s); 114 s = new String(new byte[] { 65, 66, 67, 68, 69 }, 1, 0, 5); 115 assertFalse("Did not use nonzero hibyte", s.equals("ABCDE")); 116 } 117 118 /** 119 * @tests java.lang.String#String(byte[], int, int, java.lang.String) 120 */ 121 @TestTargetNew( 122 level = TestLevel.COMPLETE, 123 notes = "", 124 method = "String", 125 args = {byte[].class, int.class, int.class, java.lang.String.class} 126 ) 127 public void test_Constructor$BIILjava_lang_String() throws Exception { 128 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, "8859_1"); 129 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 130 131 try { 132 new String(new byte[] { 65, 66, 67, 68, 69 }, 0, 5, ""); 133 fail("Should throw UnsupportedEncodingException"); 134 } catch (UnsupportedEncodingException e) { 135 //expected 136 } 137 } 138 139 /** 140 * @tests java.lang.String#String(byte[], java.lang.String) 141 */ 142 @TestTargetNew( 143 level = TestLevel.PARTIAL, 144 notes = "UnsupportedEncodingException is not verified.", 145 method = "String", 146 args = {byte[].class, java.lang.String.class} 147 ) 148 public void test_Constructor$BLjava_lang_String() throws Exception { 149 String s = new String(new byte[] { 65, 66, 67, 68, 69 }, "8859_1"); 150 assertEquals("Incorrect string returned: " + s, "ABCDE", s); 151 } 152 153 /** 154 * @tests java.lang.String#String(char[]) 155 */ 156 @TestTargetNew( 157 level = TestLevel.COMPLETE, 158 notes = "", 159 method = "String", 160 args = {char[].class} 161 ) 162 public void test_Constructor$C() { 163 assertEquals("Failed Constructor test", "World", new String(new char[] { 164 'W', 'o', 'r', 'l', 'd' })); 165 } 166 167 /** 168 * @tests java.lang.String#String(char[], int, int) 169 */ 170 @TestTargetNew( 171 level = TestLevel.COMPLETE, 172 notes = "", 173 method = "String", 174 args = {char[].class, int.class, int.class} 175 ) 176 public void test_Constructor$CII() throws Exception { 177 char[] buf = { 'H', 'e', 'l', 'l', 'o', 'W', 'o', 'r', 'l', 'd' }; 178 String s = new String(buf, 0, buf.length); 179 assertEquals("Incorrect string created", "HelloWorld", s); 180 181 try { 182 new String(new char[0], 0, Integer.MAX_VALUE); 183 fail("No IndexOutOfBoundsException"); 184 } catch (IndexOutOfBoundsException e) { 185 } 186 } 187 188 /** 189 * @tests java.lang.String#String(java.lang.String) 190 */ 191 @TestTargetNew( 192 level = TestLevel.COMPLETE, 193 notes = "", 194 method = "String", 195 args = {java.lang.String.class} 196 ) 197 public void test_ConstructorLjava_lang_String() { 198 String s = new String("Hello World"); 199 assertEquals("Failed to construct correct string", "Hello World", s); 200 } 201 202 /** 203 * @tests java.lang.String#String(java.lang.StringBuffer) 204 */ 205 @TestTargetNew( 206 level = TestLevel.COMPLETE, 207 notes = "", 208 method = "String", 209 args = {java.lang.StringBuffer.class} 210 ) 211 public void test_ConstructorLjava_lang_StringBuffer() { 212 StringBuffer sb = new StringBuffer(); 213 sb.append("HelloWorld"); 214 assertEquals("Created incorrect string", "HelloWorld", new String(sb)); 215 } 216 217 /** 218 * @tests java.lang.String#String(java.lang.StringBuilder) 219 */ 220 @TestTargetNew( 221 level = TestLevel.COMPLETE, 222 notes = "", 223 method = "String", 224 args = {java.lang.StringBuilder.class} 225 ) 226 public void test_ConstructorLjava_lang_StringBuilder() { 227 StringBuilder sb = new StringBuilder(32); 228 sb.append("HelloWorld"); 229 assertEquals("HelloWorld", new String(sb)); 230 231 try { 232 new String((StringBuilder) null); 233 fail("No NPE"); 234 } catch (NullPointerException e) { 235 } 236 } 237 238 /** 239 * @tests java.lang.String#String(int[],int,int) 240 */ 241 @TestTargetNew( 242 level = TestLevel.COMPLETE, 243 notes = "", 244 method = "String", 245 args = {int[].class, int.class, int.class} 246 ) 247 public void test_Constructor$III() { 248 assertEquals("HelloWorld", new String(new int[] { 'H', 'e', 'l', 'l', 249 'o', 'W', 'o', 'r', 'l', 'd' }, 0, 10)); 250 assertEquals("Hello", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 251 'W', 'o', 'r', 'l', 'd' }, 0, 5)); 252 assertEquals("World", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 253 'W', 'o', 'r', 'l', 'd' }, 5, 5)); 254 assertEquals("", new String(new int[] { 'H', 'e', 'l', 'l', 'o', 'W', 255 'o', 'r', 'l', 'd' }, 5, 0)); 256 257 assertEquals("\uD800\uDC00", new String(new int[] { 0x010000 }, 0, 1)); 258 assertEquals("\uD800\uDC00a\uDBFF\uDFFF", new String(new int[] { 259 0x010000, 'a', 0x010FFFF }, 0, 3)); 260 261 try { 262 new String((int[]) null, 0, 1); 263 fail("No NPE"); 264 } catch (NullPointerException e) { 265 } 266 267 try { 268 new String(new int[] { 'a', 'b' }, -1, 2); 269 fail("No IOOBE, negative offset"); 270 } catch (IndexOutOfBoundsException e) { 271 } 272 273 try { 274 new String(new int[] { 'a', 'b' }, 0, -1); 275 fail("No IOOBE, negative count"); 276 } catch (IndexOutOfBoundsException e) { 277 } 278 279 try { 280 new String(new int[] { 'a', 'b' }, 0, -1); 281 fail("No IOOBE, negative count"); 282 } catch (IndexOutOfBoundsException e) { 283 } 284 285 try { 286 new String(new int[] { 'a', 'b' }, 0, 3); 287 fail("No IOOBE, too large"); 288 } catch (IndexOutOfBoundsException e) { 289 } 290 } 291 292 /** 293 * @tests java.lang.String#contentEquals(CharSequence) 294 */ 295 @TestTargetNew( 296 level = TestLevel.COMPLETE, 297 notes = "", 298 method = "contentEquals", 299 args = {java.lang.CharSequence.class} 300 ) 301 public void test_contentEqualsLjava_lang_CharSequence() { 302 String s = "abc"; 303 assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc"))); 304 assertFalse(s.contentEquals((CharSequence) new StringBuffer("def"))); 305 assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij"))); 306 307 s = newString(1, 3, "_abc_".toCharArray()); 308 assertTrue(s.contentEquals((CharSequence) new StringBuffer("abc"))); 309 assertFalse(s.contentEquals((CharSequence) new StringBuffer("def"))); 310 assertFalse(s.contentEquals((CharSequence) new StringBuffer("ghij"))); 311 312 try { 313 s.contentEquals((CharSequence) null); 314 fail("No NPE"); 315 } catch (NullPointerException e) { 316 } 317 } 318 319 /** 320 * @tests java.lang.String#contentEquals(StringBuffer) 321 */ 322 @TestTargetNew( 323 level = TestLevel.COMPLETE, 324 notes = "", 325 method = "contentEquals", 326 args = {java.lang.StringBuffer.class} 327 ) 328 @SuppressWarnings("nls") 329 public void test_boolean_contentEquals_StringBuffer() { 330 String s = "abc"; 331 assertTrue(s.contentEquals(new StringBuffer("abc"))); 332 assertFalse(s.contentEquals(new StringBuffer("def"))); 333 assertFalse(s.contentEquals(new StringBuffer("ghij"))); 334 335 s = newString(1, 3, "_abc_".toCharArray()); 336 assertTrue(s.contentEquals(new StringBuffer("abc"))); 337 assertFalse(s.contentEquals(new StringBuffer("def"))); 338 assertFalse(s.contentEquals(new StringBuffer("ghij"))); 339 340 try { 341 s.contentEquals((StringBuffer) null); 342 fail("Should throw a NullPointerException"); 343 } catch (NullPointerException e) { 344 // expected 345 } 346 } 347 348 /** 349 * @tests java.lang.String#contains(CharSequence) 350 */ 351 @TestTargetNew( 352 level = TestLevel.COMPLETE, 353 notes = "", 354 method = "contains", 355 args = {java.lang.CharSequence.class} 356 ) 357 @SuppressWarnings("cast") 358 public void test_containsLjava_lang_CharSequence() { 359 String s = "abcdefghijklmnopqrstuvwxyz"; 360 assertTrue(s.contains((CharSequence) new StringBuffer("abc"))); 361 assertTrue(s.contains((CharSequence) new StringBuffer("def"))); 362 assertFalse(s.contains((CharSequence) new StringBuffer("ac"))); 363 364 s = newString(1, 26, "_abcdefghijklmnopqrstuvwxyz_".toCharArray()); 365 assertTrue(s.contains((CharSequence) new StringBuffer("abc"))); 366 assertTrue(s.contains((CharSequence) new StringBuffer("def"))); 367 assertFalse(s.contains((CharSequence) new StringBuffer("ac"))); 368 369 try { 370 s.contentEquals((CharSequence) null); 371 fail("No NPE"); 372 } catch (NullPointerException e) { 373 } 374 } 375 376 /** 377 * @tests java.lang.String.offsetByCodePoints(int, int)' 378 */ 379 @TestTargetNew( 380 level = TestLevel.COMPLETE, 381 notes = "", 382 method = "offsetByCodePoints", 383 args = {int.class, int.class} 384 ) 385 public void test_offsetByCodePointsII() { 386 int result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 2); 387 assertEquals(3, result); 388 389 result = new String("abcd").offsetByCodePoints(3, -1); 390 assertEquals(2, result); 391 392 result = new String("a\uD800\uDC00b").offsetByCodePoints(0, 3); 393 assertEquals(4, result); 394 395 result = new String("a\uD800\uDC00b").offsetByCodePoints(3, -1); 396 assertEquals(1, result); 397 398 result = new String("a\uD800\uDC00b").offsetByCodePoints(3, 0); 399 assertEquals(3, result); 400 401 result = new String("\uD800\uDC00bc").offsetByCodePoints(3, 0); 402 assertEquals(3, result); 403 404 result = new String("a\uDC00bc").offsetByCodePoints(3, -1); 405 assertEquals(2, result); 406 407 result = new String("a\uD800bc").offsetByCodePoints(3, -1); 408 assertEquals(2, result); 409 410 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 411 .offsetByCodePoints(0, 2); 412 assertEquals(3, result); 413 414 result = newString(2, 4, "__abcd__".toCharArray()).offsetByCodePoints( 415 3, -1); 416 assertEquals(2, result); 417 418 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 419 .offsetByCodePoints(0, 3); 420 assertEquals(4, result); 421 422 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 423 .offsetByCodePoints(3, -1); 424 assertEquals(1, result); 425 426 result = newString(2, 4, "__a\uD800\uDC00b__".toCharArray()) 427 .offsetByCodePoints(3, 0); 428 assertEquals(3, result); 429 430 result = newString(2, 4, "__\uD800\uDC00bc__".toCharArray()) 431 .offsetByCodePoints(3, 0); 432 assertEquals(3, result); 433 434 result = newString(2, 4, "__a\uDC00bc__".toCharArray()) 435 .offsetByCodePoints(3, -1); 436 assertEquals(2, result); 437 438 result = newString(2, 4, "__a\uD800bc__".toCharArray()) 439 .offsetByCodePoints(3, -1); 440 assertEquals(2, result); 441 442 String s = "abc"; 443 try { 444 s.offsetByCodePoints(-1, 1); 445 fail("No IOOBE for negative index."); 446 } catch (IndexOutOfBoundsException e) { 447 } 448 449 try { 450 s.offsetByCodePoints(0, 4); 451 fail("No IOOBE for offset that's too large."); 452 } catch (IndexOutOfBoundsException e) { 453 } 454 455 try { 456 s.offsetByCodePoints(3, -4); 457 fail("No IOOBE for offset that's too small."); 458 } catch (IndexOutOfBoundsException e) { 459 } 460 461 try { 462 s.offsetByCodePoints(3, 1); 463 fail("No IOOBE for index that's too large."); 464 } catch (IndexOutOfBoundsException e) { 465 } 466 467 try { 468 s.offsetByCodePoints(4, -1); 469 fail("No IOOBE for index that's too large."); 470 } catch (IndexOutOfBoundsException e) { 471 } 472 473 s = newString(2,3,"__abc__".toCharArray()); 474 try { 475 s.offsetByCodePoints(-1, 1); 476 fail("No IOOBE for negative index."); 477 } catch (IndexOutOfBoundsException e) { 478 } 479 480 try { 481 s.offsetByCodePoints(0, 4); 482 fail("No IOOBE for offset that's too large."); 483 } catch (IndexOutOfBoundsException e) { 484 } 485 486 try { 487 s.offsetByCodePoints(3, -4); 488 fail("No IOOBE for offset that's too small."); 489 } catch (IndexOutOfBoundsException e) { 490 } 491 492 try { 493 s.offsetByCodePoints(3, 1); 494 fail("No IOOBE for index that's too large."); 495 } catch (IndexOutOfBoundsException e) { 496 } 497 498 try { 499 s.offsetByCodePoints(4, -1); 500 fail("No IOOBE for index that's too large."); 501 } catch (IndexOutOfBoundsException e) { 502 } 503 } 504 505 /** 506 * @tests java.lang.StringBuilder.codePointAt(int) 507 */ 508 @TestTargetNew( 509 level = TestLevel.COMPLETE, 510 notes = "", 511 method = "codePointAt", 512 args = {int.class} 513 ) 514 public void test_codePointAtI() { 515 String s = "abc"; 516 assertEquals('a', s.codePointAt(0)); 517 assertEquals('b', s.codePointAt(1)); 518 assertEquals('c', s.codePointAt(2)); 519 520 s = newString(2,3,"__abc__".toCharArray()); 521 assertEquals('a', s.codePointAt(0)); 522 assertEquals('b', s.codePointAt(1)); 523 assertEquals('c', s.codePointAt(2)); 524 525 s = "\uD800\uDC00"; 526 assertEquals(0x10000, s.codePointAt(0)); 527 assertEquals('\uDC00', s.codePointAt(1)); 528 529 s = newString(2,2,"__\uD800\uDC00__".toCharArray()); 530 assertEquals(0x10000, s.codePointAt(0)); 531 assertEquals('\uDC00', s.codePointAt(1)); 532 533 s = "abc"; 534 try { 535 s.codePointAt(-1); 536 fail("No IOOBE on negative index."); 537 } catch (IndexOutOfBoundsException e) { 538 } 539 540 try { 541 s.codePointAt(s.length()); 542 fail("No IOOBE on index equal to length."); 543 } catch (IndexOutOfBoundsException e) { 544 } 545 546 try { 547 s.codePointAt(s.length() + 1); 548 fail("No IOOBE on index greater than length."); 549 } catch (IndexOutOfBoundsException e) { 550 } 551 552 s = newString(2,3,"__abc__".toCharArray()); 553 try { 554 s.codePointAt(-1); 555 fail("No IOOBE on negative index."); 556 } catch (IndexOutOfBoundsException e) { 557 } 558 559 try { 560 s.codePointAt(s.length()); 561 fail("No IOOBE on index equal to length."); 562 } catch (IndexOutOfBoundsException e) { 563 } 564 565 try { 566 s.codePointAt(s.length() + 1); 567 fail("No IOOBE on index greater than length."); 568 } catch (IndexOutOfBoundsException e) { 569 } 570 } 571 572 /** 573 * @tests java.lang.StringBuilder.codePointBefore(int) 574 */ 575 @TestTargetNew( 576 level = TestLevel.COMPLETE, 577 notes = "", 578 method = "codePointBefore", 579 args = {int.class} 580 ) 581 public void test_codePointBeforeI() { 582 String s = "abc"; 583 assertEquals('a', s.codePointBefore(1)); 584 assertEquals('b', s.codePointBefore(2)); 585 assertEquals('c', s.codePointBefore(3)); 586 587 s = newString(2,3,"__abc__".toCharArray()); 588 assertEquals('a', s.codePointBefore(1)); 589 assertEquals('b', s.codePointBefore(2)); 590 assertEquals('c', s.codePointBefore(3)); 591 592 s = "\uD800\uDC00"; 593 assertEquals(0x10000, s.codePointBefore(2)); 594 assertEquals('\uD800', s.codePointBefore(1)); 595 596 s = newString(2,2,"__\uD800\uDC00__".toCharArray()); 597 assertEquals(0x10000, s.codePointBefore(2)); 598 assertEquals('\uD800', s.codePointBefore(1)); 599 600 s = "abc"; 601 try { 602 s.codePointBefore(0); 603 fail("No IOOBE on zero index."); 604 } catch (IndexOutOfBoundsException e) { 605 } 606 607 try { 608 s.codePointBefore(-1); 609 fail("No IOOBE on negative index."); 610 } catch (IndexOutOfBoundsException e) { 611 } 612 613 try { 614 s.codePointBefore(s.length() + 1); 615 fail("No IOOBE on index greater than length."); 616 } catch (IndexOutOfBoundsException e) { 617 } 618 619 s = newString(2,3,"__abc__".toCharArray()); 620 try { 621 s.codePointBefore(0); 622 fail("No IOOBE on zero index."); 623 } catch (IndexOutOfBoundsException e) { 624 } 625 626 try { 627 s.codePointBefore(-1); 628 fail("No IOOBE on negative index."); 629 } catch (IndexOutOfBoundsException e) { 630 } 631 632 try { 633 s.codePointBefore(s.length() + 1); 634 fail("No IOOBE on index greater than length."); 635 } catch (IndexOutOfBoundsException e) { 636 } 637 } 638 639 /** 640 * @tests java.lang.StringBuilder.codePointCount(int, int) 641 */ 642 @TestTargetNew( 643 level = TestLevel.COMPLETE, 644 notes = "", 645 method = "codePointCount", 646 args = {int.class, int.class} 647 ) 648 public void test_codePointCountII() { 649 assertEquals(1, "\uD800\uDC00".codePointCount(0, 2)); 650 assertEquals(1, "\uD800\uDC01".codePointCount(0, 2)); 651 assertEquals(1, "\uD801\uDC01".codePointCount(0, 2)); 652 assertEquals(1, "\uDBFF\uDFFF".codePointCount(0, 2)); 653 654 assertEquals(3, "a\uD800\uDC00b".codePointCount(0, 4)); 655 assertEquals(4, "a\uD800\uDC00b\uD800".codePointCount(0, 5)); 656 657 assertEquals(1, newString(2,2,"__\uD800\uDC00__".toCharArray()).codePointCount(0, 2)); 658 assertEquals(1, newString(2,2,"__\uD800\uDC01__".toCharArray()).codePointCount(0, 2)); 659 assertEquals(1, newString(2,2,"__\uD801\uDC01__".toCharArray()).codePointCount(0, 2)); 660 assertEquals(1, newString(2,2,"__\uDBFF\uDFFF__".toCharArray()).codePointCount(0, 2)); 661 662 assertEquals(3, newString(2,4,"__a\uD800\uDC00b__".toCharArray()).codePointCount(0, 4)); 663 assertEquals(4, newString(2,5,"__a\uD800\uDC00b\uD800__".toCharArray()).codePointCount(0, 5)); 664 665 String s = "abc"; 666 try { 667 s.codePointCount(-1, 2); 668 fail("No IOOBE for negative begin index."); 669 } catch (IndexOutOfBoundsException e) { 670 } 671 672 try { 673 s.codePointCount(0, 4); 674 fail("No IOOBE for end index that's too large."); 675 } catch (IndexOutOfBoundsException e) { 676 } 677 678 try { 679 s.codePointCount(3, 2); 680 fail("No IOOBE for begin index larger than end index."); 681 } catch (IndexOutOfBoundsException e) { 682 } 683 684 s = newString(2, 3, "__abc__".toCharArray()); 685 try { 686 s.codePointCount(-1, 2); 687 fail("No IOOBE for negative begin index."); 688 } catch (IndexOutOfBoundsException e) { 689 } 690 691 try { 692 s.codePointCount(0, 4); 693 fail("No IOOBE for end index that's too large."); 694 } catch (IndexOutOfBoundsException e) { 695 } 696 697 try { 698 s.codePointCount(3, 2); 699 fail("No IOOBE for begin index larger than end index."); 700 } catch (IndexOutOfBoundsException e) { 701 } 702 } 703 704 @TestTargetNew( 705 level = TestLevel.ADDITIONAL, 706 notes = "Regression test for some existing bugs and crashes", 707 method = "format", 708 args = { String.class, Object[].class } 709 ) 710 public void testProblemCases() { 711 BigDecimal[] input = new BigDecimal[] { 712 new BigDecimal("20.00000"), 713 new BigDecimal("20.000000"), 714 new BigDecimal(".2"), 715 new BigDecimal("2"), 716 new BigDecimal("-2"), 717 new BigDecimal("200000000000000000000000"), 718 new BigDecimal("20000000000000000000000000000000000000000000000000") 719 }; 720 721 String[] output = new String[] { 722 "20.00", 723 "20.00", 724 "0.20", 725 "2.00", 726 "-2.00", 727 "200000000000000000000000.00", 728 "20000000000000000000000000000000000000000000000000.00" 729 }; 730 731 for (int i = 0; i < input.length; i++) { 732 String result = String.format("%.2f", input[i]); 733 assertEquals("Format test for \"" + input[i] + "\" failed, " + 734 "expected=" + output[i] + ", " + 735 "actual=" + result, output[i], result); 736 } 737 } 738 739} 740