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 */ 16package org.apache.harmony.luni.tests.java.util; 17 18import java.io.BufferedReader; 19import java.io.BufferedWriter; 20import java.io.ByteArrayInputStream; 21import java.io.ByteArrayOutputStream; 22import java.io.Closeable; 23import java.io.EOFException; 24import java.io.File; 25import java.io.FileInputStream; 26import java.io.FileNotFoundException; 27import java.io.FileOutputStream; 28import java.io.InputStream; 29import java.io.InputStreamReader; 30import java.io.IOException; 31import java.io.OutputStream; 32import java.io.OutputStreamWriter; 33import java.io.PipedInputStream; 34import java.io.PipedOutputStream; 35import java.io.StringReader; 36import java.math.BigDecimal; 37import java.math.BigInteger; 38import java.net.InetSocketAddress; 39import java.net.ServerSocket; 40import java.net.Socket; 41import java.net.SocketAddress; 42import java.nio.channels.FileChannel; 43import java.nio.channels.IllegalBlockingModeException; 44import java.nio.channels.ReadableByteChannel; 45import java.nio.channels.ServerSocketChannel; 46import java.nio.channels.SocketChannel; 47import java.nio.CharBuffer; 48import java.nio.charset.Charset; 49import java.util.ArrayList; 50import java.util.Arrays; 51import java.util.InputMismatchException; 52import java.util.List; 53import java.util.Locale; 54import java.util.NoSuchElementException; 55import java.util.regex.MatchResult; 56import java.util.regex.Pattern; 57import java.util.Scanner; 58 59import junit.framework.TestCase; 60 61public class ScannerTest extends TestCase { 62 63 private Scanner s; 64 65 private ServerSocket server; 66 67 private SocketAddress address; 68 69 private SocketChannel client; 70 71 private Socket serverSocket; 72 73 private OutputStream os; 74 75 private static class MockCloseable implements Closeable, Readable { 76 77 public void close() throws IOException { 78 throw new IOException(); 79 } 80 81 public int read(CharBuffer cb) throws IOException { 82 throw new EOFException(); 83 } 84 85 } 86 87 /** 88 * @tests java.util.Scanner#Scanner(File) 89 */ 90 public void test_ConstructorLjava_io_File() throws IOException { 91 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 92 s = new Scanner(tmpFile); 93 assertNotNull(s); 94 s.close(); 95 assertTrue(tmpFile.delete()); 96 97 try { 98 s = new Scanner(tmpFile); 99 fail(); 100 } catch (FileNotFoundException expected) { 101 } 102 103 tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 104 FileOutputStream fos = new FileOutputStream(tmpFile); 105 fos.write("test".getBytes()); 106 fos.close(); 107 108 s = new Scanner(tmpFile); 109 s.close(); 110 tmpFile.delete(); 111 112 // Scanner(File = null) 113 try { 114 s = new Scanner((File) null); 115 fail(); 116 } catch (NullPointerException expected) { 117 } 118 119 // TODO: test if the default charset is used. 120 } 121 122 /** 123 * @tests java.util.Scanner#Scanner(File, String) 124 */ 125 public void test_ConstructorLjava_io_FileLjava_lang_String() 126 throws IOException { 127 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 128 s = new Scanner(tmpFile, Charset.defaultCharset().name()); 129 assertNotNull(s); 130 s.close(); 131 assertTrue(tmpFile.delete()); 132 133 try { 134 s = new Scanner(tmpFile, Charset.defaultCharset().name()); 135 fail(); 136 } catch (FileNotFoundException expected) { 137 } 138 139 try { 140 s = new Scanner(tmpFile, null); 141 fail(); 142 } catch (FileNotFoundException expected) { 143 } 144 145 tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 146 try { 147 s = new Scanner(tmpFile, "invalid charset"); 148 fail(); 149 } catch (IllegalArgumentException expected) { 150 } 151 152 //fail on RI. File is opened but not closed when exception is thrown on 153 // RI. 154 assertTrue(tmpFile.delete()); 155 156 // Scanner(File = null, Charset = null) 157 try { 158 s = new Scanner((File) null, null); 159 fail(); 160 } catch (NullPointerException expected) { 161 } 162 163 // Scanner(File = null, Charset = UTF-8) 164 try { 165 s = new Scanner((File) null, "UTF-8"); 166 fail(); 167 } catch (NullPointerException expected) { 168 } 169 170 // Scanner(File = null, Charset = invalid) 171 try { 172 s = new Scanner((File) null, "invalid"); 173 fail(); 174 } catch (NullPointerException expected) { 175 } 176 177 // Scanner(File, Charset = null) 178 try { 179 File f = File.createTempFile("test", ".tmp"); 180 s = new Scanner(f, null); 181 fail(); 182 } catch (IllegalArgumentException expected) { 183 } 184 185 // TODO: test if the specified charset is used. 186 } 187 188 /** 189 * @tests java.util.Scanner#Scanner(InputStream) 190 */ 191 public void test_ConstructorLjava_io_InputStream() { 192 s = new Scanner(new PipedInputStream()); 193 assertNotNull(s); 194 s.close(); 195 196 // Scanner(InputStream) 197 try { 198 s = new Scanner((InputStream) null); 199 fail(); 200 } catch (NullPointerException expected) { 201 } 202 203 // TODO: test if the default charset is used. 204 } 205 206 /** 207 * @tests java.util.Scanner#Scanner(InputStream, String) 208 */ 209 public void test_ConstructorLjava_io_InputStreamLjava_lang_String() { 210 s = new Scanner(new PipedInputStream(), Charset.defaultCharset().name()); 211 assertNotNull(s); 212 s.close(); 213 214 try { 215 s = new Scanner((PipedInputStream) null, "invalid charset"); 216 fail(); 217 } catch (NullPointerException expected) { 218 } 219 220 try { 221 s = new Scanner(new PipedInputStream(), null); 222 fail(); 223 } catch (NullPointerException expected) { 224 } 225 226 try { 227 s = new Scanner(new PipedInputStream(), "invalid charset"); 228 fail(); 229 } catch (IllegalArgumentException expected) { 230 } 231 232 // TODO: test if the specified charset is used. 233 } 234 235 /** 236 * @tests java.util.Scanner#Scanner(Readable) 237 */ 238 public void test_ConstructorLjava_lang_Readable() { 239 s = new Scanner(new StringReader("test string")); 240 assertNotNull(s); 241 s.close(); 242 243 // Scanner(Readable) 244 try { 245 s = new Scanner((Readable) null); 246 fail(); 247 } catch (NullPointerException expected) { 248 } 249 } 250 251 /** 252 * @tests java.util.Scanner#Scanner(ReadableByteChannel) 253 */ 254 public void test_ConstructorLjava_nio_channels_ReadableByteChannel() 255 throws IOException { 256 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 257 FileChannel fc = new FileOutputStream(tmpFile).getChannel(); 258 s = new Scanner(fc); 259 assertNotNull(s); 260 s.close(); 261 assertTrue(tmpFile.delete()); 262 263 // Scanner(ReadableByteChannel) 264 try { 265 s = new Scanner((ReadableByteChannel) null); 266 fail(); 267 } catch (NullPointerException expected) { 268 } 269 270 // Test if the default charset is used. 271 String sampleData = "1 2 3 4 5 6 7 8 9 10"; 272 File tempFile = File.createTempFile("harmony", "test"); 273 tempFile.deleteOnExit(); 274 FileOutputStream os = new FileOutputStream(tempFile); 275 os.write(sampleData.getBytes()); 276 os.close(); 277 278 FileInputStream is = new FileInputStream(tempFile); 279 FileChannel channel = is.getChannel(); 280 281 Scanner s = new Scanner(channel); 282 int count = 0; 283 while (s.hasNextInt()) { 284 s.nextInt(); 285 count++; 286 } 287 channel.close(); 288 assertEquals(10, count); 289 } 290 291 /** 292 * @tests java.util.Scanner#Scanner(ReadableByteChannel, String) 293 */ 294 public void test_ConstructorLjava_nio_channels_ReadableByteChannelLjava_lang_String() 295 throws IOException { 296 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 297 FileChannel fc = new FileOutputStream(tmpFile).getChannel(); 298 s = new Scanner(fc, Charset.defaultCharset().name()); 299 assertNotNull(s); 300 s.close(); 301 302 fc = new FileOutputStream(tmpFile).getChannel(); 303 try { 304 s = new Scanner(fc, "invalid charset"); 305 fail(); 306 } catch (IllegalArgumentException expected) { 307 } 308 fc.close(); 309 assertTrue(tmpFile.delete()); 310 311 // Scanner(ReadableByteChannel = null, Charset = null) 312 try { 313 s = new Scanner((ReadableByteChannel) null, null); 314 fail(); 315 } catch (NullPointerException expected) { 316 } 317 318 // Scanner(ReadableByteChannel = null, Charset = invalid) 319 try { 320 s = new Scanner((ReadableByteChannel) null, "invalid"); 321 fail(); 322 } catch (NullPointerException expected) { 323 } 324 325 // Scanner(ReadableByteChannel, Charset = null) 326 try { 327 s = new Scanner(fc, null); 328 fail(); 329 } catch (IllegalArgumentException expected) { 330 } 331 // TODO: test if the specified charset is used. 332 } 333 334 public void test_Constructor_LReadableByteChannel() throws IOException { 335 ServerSocketChannel ssc = ServerSocketChannel.open(); 336 ssc.socket().bind(null); 337 338 SocketChannel sc = SocketChannel.open(); 339 sc.connect(ssc.socket().getLocalSocketAddress()); 340 sc.configureBlocking(false); 341 assertFalse(sc.isBlocking()); 342 343 ssc.accept().close(); 344 ssc.close(); 345 assertFalse(sc.isBlocking()); 346 347 Scanner s = new Scanner(sc); 348 try { 349 s.hasNextInt(); 350 fail(); 351 } catch (IllegalBlockingModeException expected) { 352 } 353 354 sc.close(); 355 } 356 357 /** 358 * @tests java.util.Scanner#Scanner(String) 359 */ 360 public void test_ConstructorLjava_lang_String() { 361 s = new Scanner("test string"); 362 assertNotNull(s); 363 s.close(); 364 365 // Scanner(String) 366 try { 367 s = new Scanner((String) null); 368 fail(); 369 } catch (NullPointerException expected) { 370 } 371 } 372 373 /** 374 * @tests java.util.Scanner#close() 375 */ 376 public void test_close() throws IOException { 377 File tmpFile = File.createTempFile("TestFileForScanner", ".tmp"); 378 FileOutputStream fos = new FileOutputStream(tmpFile); 379 FileChannel fc = fos.getChannel(); 380 s = new Scanner(fc); 381 382 // Write out a int before the scanner is closed, should be OK. 383 fos.write(12); 384 385 s.close(); 386 assertFalse(fc.isOpen()); 387 388 // Write out a int after the scanner is closed, IOException should be 389 // thrown out. 390 try { 391 fos.write(12); 392 fail(); 393 } catch (IOException expected) { 394 } 395 396 s.close(); // no exception should be thrown 397 assertTrue(tmpFile.delete()); 398 } 399 400 /** 401 * @tests java.util.Scanner#ioException() 402 */ 403 public void test_ioException() throws IOException { 404 MockCloseable mc = new MockCloseable(); 405 s = new Scanner(mc); 406 assertNull(s.ioException()); // No operation, no exception 407 408 s.close(); // IOException should be cached 409 assertNotNull(s.ioException()); 410 assertTrue(s.ioException() instanceof IOException); 411 } 412 413 /** 414 * @tests java.util.Scanner#delimiter() 415 */ 416 public void test_delimiter() { 417 s = new Scanner("test"); 418 Pattern pattern = s.delimiter(); 419 assertEquals("\\p{javaWhitespace}+", pattern.toString()); 420 } 421 422 /** 423 * @tests java.util.Scanner#useDelimiter(Pattern) 424 */ 425 public void test_useDelimiter_LPattern() { 426 s = new Scanner("test"); 427 s.useDelimiter(Pattern.compile("\\w+")); 428 assertEquals("\\w+", s.delimiter().toString()); 429 430 s = new Scanner("test"); 431 s.useDelimiter((Pattern) null); 432 assertNull(s.delimiter()); 433 } 434 435 /** 436 * @tests java.util.Scanner#useDelimiter(String) 437 */ 438 public void test_useDelimiter_String() { 439 s = new Scanner("test"); 440 try { 441 s.useDelimiter((String) null); 442 fail(); 443 } catch (NullPointerException expected) { 444 } 445 446 s = new Scanner("test"); 447 s.useDelimiter("\\w+"); 448 assertEquals("\\w+", s.delimiter().toString()); 449 } 450 451 /** 452 * @tests java.util.Scanner#locale() 453 */ 454 public void test_locale() { 455 s = new Scanner("test"); 456 assertEquals(Locale.getDefault(), s.locale()); 457 } 458 459 /** 460 * @tests java.util.Scanner#useLocale(Locale) 461 */ 462 public void test_useLocale_LLocale() { 463 s = new Scanner("test"); 464 try { 465 s.useLocale(null); 466 fail(); 467 } catch (NullPointerException expected) { 468 } 469 470 s.useLocale(new Locale("test", "test")); 471 assertEquals(new Locale("test", "test"), s.locale()); 472 } 473 474 /** 475 * @tests java.util.Scanner#radix() 476 */ 477 public void test_radix() { 478 s = new Scanner("test"); 479 assertEquals(10, s.radix()); 480 } 481 482 /** 483 * @tests java.util.Scanner#useRadix() 484 */ 485 public void test_useRadix_I() { 486 s = new Scanner("test"); 487 try { 488 s.useRadix(Character.MIN_RADIX - 1); 489 fail(); 490 } catch (IllegalArgumentException expected) { 491 } 492 try { 493 s.useRadix(Character.MAX_RADIX + 1); 494 fail(); 495 } catch (IllegalArgumentException expected) { 496 } 497 s.useRadix(11); 498 assertEquals(11, s.radix()); 499 } 500 501 /** 502 * @tests java.util.Scanner#remove() 503 */ 504 public void test_remove() { 505 s = new Scanner("aab*b*").useDelimiter("\\*"); 506 try { 507 s.remove(); 508 fail(); 509 } catch (UnsupportedOperationException expected) { 510 } 511 } 512 513 /** 514 * @tests java.util.Scanner#match() 515 */ 516 public void test_match() { 517 MatchResult result ; 518 s = new Scanner("1 2 "); 519 try { 520 s.match(); 521 fail(); 522 } catch (IllegalStateException expected) { 523 } 524 assertEquals("1", s.next()); 525 assertEquals("2", s.next()); 526 result = s.match(); 527 assertEquals(2, result.start()); 528 assertEquals(3, result.end()); 529 assertEquals(2, result.start(0)); 530 assertEquals(3, result.end(0)); 531 assertEquals("2", result.group()); 532 assertEquals("2", result.group(0)); 533 assertEquals(0, result.groupCount()); 534 try { 535 result.start(1); 536 fail(); 537 } catch (IndexOutOfBoundsException expected) { 538 } 539 try { 540 s.next(); 541 fail(); 542 } catch (NoSuchElementException expected) { 543 } 544 try { 545 s.match(); 546 fail(); 547 } catch (IllegalStateException expected) { 548 } 549 550 s = new Scanner("True faLse"); 551 try { 552 s.match(); 553 fail(); 554 } catch (IllegalStateException expected) { 555 } 556 assertTrue(s.nextBoolean()); 557 result = s.match(); 558 assertEquals(0, result.start()); 559 assertEquals(4, result.end()); 560 assertEquals(0, result.start(0)); 561 assertEquals(4, result.end(0)); 562 assertEquals("True", result.group()); 563 assertEquals(0, result.groupCount()); 564 assertFalse(s.nextBoolean()); 565 try { 566 s.nextBoolean(); 567 fail(); 568 } catch (NoSuchElementException expected) { 569 } 570 try { 571 s.match(); 572 fail(); 573 } catch (IllegalStateException expected) { 574 } 575 576 s = new Scanner("True faLse"); 577 assertTrue(s.nextBoolean()); 578 result = s.match(); 579 assertEquals(0, result.start()); 580 assertEquals(4, result.end()); 581 assertEquals(0, result.start(0)); 582 assertEquals(4, result.end(0)); 583 assertEquals("True", result.group()); 584 assertEquals(0, result.groupCount()); 585 s.close(); 586 try { 587 s.nextBoolean(); 588 fail(); 589 } catch (IllegalStateException expected) { 590 } 591 result = s.match(); 592 assertEquals(0, result.start()); 593 assertEquals(4, result.end()); 594 assertEquals(0, result.start(0)); 595 assertEquals(4, result.end(0)); 596 assertEquals("True", result.group()); 597 assertEquals(0, result.groupCount()); 598 599 s = new Scanner("True fase"); 600 assertTrue(s.nextBoolean()); 601 assertEquals(0, result.groupCount()); 602 try { 603 s.nextBoolean(); 604 fail(); 605 } catch (InputMismatchException expected) { 606 } 607 try { 608 s.match(); 609 fail(); 610 } catch (IllegalStateException expected) { 611 } 612 613 s = new Scanner("True fase"); 614 assertTrue(s.nextBoolean()); 615 try { 616 s.next((Pattern)null); 617 fail(); 618 } catch (NullPointerException expected) { 619 } 620 result = s.match(); 621 assertEquals(0, result.start()); 622 assertEquals(4, result.end()); 623 assertEquals(0, result.start(0)); 624 assertEquals(4, result.end(0)); 625 assertEquals("True", result.group()); 626 assertEquals(0, result.groupCount()); 627 628 } 629 630 /** 631 * @throws IOException 632 * @tests java.util.Scanner#next() 633 */ 634 public void test_next() throws IOException { 635 // use special delimiter 636 s = new Scanner("1**2").useDelimiter("\\*"); 637 assertEquals("1", s.next()); 638 assertEquals("", s.next()); 639 assertEquals("2", s.next()); 640 641 s = new Scanner(" \t 1 \t 2").useDelimiter("\\s*"); 642 assertEquals("1", s.next()); 643 assertEquals("2", s.next()); 644 try { 645 s.next(); 646 fail(); 647 } catch (NoSuchElementException expected) { 648 } 649 650 s = new Scanner("a").useDelimiter("a?"); 651 try { 652 s.next(); 653 fail(); 654 } catch (NoSuchElementException expected) { 655 } 656 657 s = new Scanner("aa").useDelimiter("a?"); 658 assertEquals("", s.next()); 659 try { 660 s.next(); 661 fail(); 662 } catch (NoSuchElementException expected) { 663 } 664 665 666 s = new Scanner("word( )test( )").useDelimiter("\\( \\)"); 667 assertEquals("word", s.next()); 668 assertEquals("test", s.next()); 669 670 s = new Scanner("? next ").useDelimiter("( )"); 671 assertEquals("?", s.next()); 672 assertEquals("next", s.next()); 673 assertEquals("", s.next()); 674 675 s = new Scanner("word1 word2 "); 676 assertEquals("word1", s.next()); 677 assertEquals("word2", s.next()); 678 // test boundary case 679 try { 680 s.next(); 681 fail(); 682 } catch (NoSuchElementException expected) { 683 } 684 685 // just delimiter exists in this scanner 686 s = new Scanner(" "); 687 try { 688 s.next(); 689 fail(); 690 } catch (NoSuchElementException expected) { 691 } 692 693 // nothing exists in this scanner 694 s = new Scanner(""); 695 try { 696 s.next(); 697 fail(); 698 } catch (NoSuchElementException expected) { 699 } 700 701 // no delimiter exists in this scanner 702 s = new Scanner("test"); 703 assertEquals("test", s.next()); 704 705 // input resourse starts with delimiter 706 s = new Scanner(" test"); 707 assertEquals("test", s.next()); 708 709 // input resource ends with delimiter 710 s = new Scanner(" test "); 711 assertEquals("test", s.next()); 712 713 // Harmony uses 1024 as default buffer size, 714 // What if a sentence can not be read in all in once. 715 StringBuilder longSentence = new StringBuilder(1025); 716 for (int i = 0; i < 11; i++) { 717 longSentence.append(" "); 718 } 719 for (int i = 11; i < 1026; i++) { 720 longSentence.append("a"); 721 } 722 s = new Scanner(longSentence.toString()); 723 assertEquals(longSentence.toString().trim(), s.next()); 724 725 s = new Scanner(" test test"); 726 assertEquals("test", s.next()); 727 assertEquals("test", s.next()); 728 729 // What if use a delimiter of length 0. 730 s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^", 731 Pattern.MULTILINE)); 732 assertEquals("test\n", s.next()); 733 assertEquals("test", s.next()); 734 735 s = new Scanner("").useDelimiter(Pattern.compile("^", 736 Pattern.MULTILINE)); 737 try { 738 s.next(); 739 fail(); 740 } catch (NoSuchElementException expected) { 741 } 742 743 s = new Scanner("").useDelimiter(Pattern.compile("^*", 744 Pattern.MULTILINE)); 745 try { 746 s.next(); 747 fail(); 748 } catch (NoSuchElementException expected) { 749 } 750 751 s = new Scanner("test\ntest").useDelimiter(Pattern.compile("^*", 752 Pattern.MULTILINE)); 753 assertEquals("t", s.next()); 754 assertEquals("e", s.next()); 755 756 s = new Scanner("\ntest\ntest").useDelimiter(Pattern.compile("$", 757 Pattern.MULTILINE)); 758 assertEquals("\ntest", s.next()); 759 assertEquals("\ntest", s.next()); 760 761 // test socket inputStream 762 // Harmony uses 1024 as default buffer size, 763 // what if the leading delimiter is larger than 1023 764 for (int i = 0; i < 1024; i++) { 765 os.write(" ".getBytes()); 766 } 767 os.write(" 1 2 ".getBytes()); 768 s = new Scanner(client); 769 assertEquals("1", s.next()); 770 assertEquals("2", s.next()); 771 os.write(" 1 2".getBytes()); 772 serverSocket.close(); 773 assertEquals("1", s.next()); 774 assertEquals("2", s.next()); 775 try { 776 s.next(); 777 fail(); 778 } catch (NoSuchElementException expected) { 779 } 780 781 } 782 783 /** 784 * @throws IOException 785 * @tests java.util.Scanner#next(Pattern) 786 */ 787 public void test_nextLPattern() throws IOException { 788 Pattern pattern; 789 s = new Scanner("aab*2*").useDelimiter("\\*"); 790 pattern = Pattern.compile("a*b"); 791 assertEquals("aab", s.next(pattern)); 792 try { 793 s.next(pattern); 794 fail(); 795 } catch (InputMismatchException expected) { 796 } 797 798 s = new Scanner("word ? "); 799 pattern = Pattern.compile("\\w+"); 800 assertEquals("word", s.next(pattern)); 801 try { 802 s.next(pattern); 803 fail(); 804 } catch (InputMismatchException expected) { 805 } 806 807 s = new Scanner("word1 word2 "); 808 pattern = Pattern.compile("\\w+"); 809 assertEquals("word1", s.next(pattern)); 810 assertEquals("word2", s.next(pattern)); 811 // test boundary case 812 try { 813 s.next(pattern); 814 fail(); 815 } catch (NoSuchElementException expected) { 816 } 817 818 // test socket inputStream 819 820 os.write("aab 2".getBytes()); 821 serverSocket.close(); 822 823 s = new Scanner(client); 824 pattern = Pattern.compile("a*b"); 825 assertEquals("aab", s.next(pattern)); 826 try { 827 s.next(pattern); 828 fail(); 829 } catch (InputMismatchException expected) { 830 } 831 } 832 833 /** 834 * @throws IOException 835 * @tests java.util.Scanner#next(String) 836 */ 837 public void test_nextLString() throws IOException { 838 s = new Scanner("b*a*").useDelimiter("\\*"); 839 assertEquals("b", s.next("a*b")); 840 try { 841 s.next("a*b"); 842 fail(); 843 } catch (InputMismatchException expected) { 844 } 845 846 s = new Scanner("word ? "); 847 assertEquals("word", s.next("\\w+")); 848 try { 849 s.next("\\w+"); 850 fail(); 851 } catch (InputMismatchException expected) { 852 } 853 854 s = new Scanner("word1 next "); 855 assertEquals("word1", s.next("\\w+")); 856 assertEquals("next", s.next("\\w+")); 857 // test boundary case 858 try { 859 s.next("\\w+"); 860 fail(); 861 } catch (NoSuchElementException expected) { 862 } 863 864 // test socket inputStream 865 os.write("aab 2".getBytes()); 866 serverSocket.close(); 867 868 s = new Scanner(client); 869 assertEquals("aab", s.next("a*b")); 870 try { 871 s.next("a*b"); 872 fail(); 873 } catch (InputMismatchException expected) { 874 } 875 } 876 877 /** 878 * @throws IOException 879 * @tests java.util.Scanner#nextBoolean() 880 */ 881 public void test_nextBoolean() throws IOException { 882 // case insensitive 883 s = new Scanner("TRue"); 884 assertTrue(s.nextBoolean()); 885 886 s = new Scanner("tRue false"); 887 assertTrue(s.nextBoolean()); 888 assertFalse(s.nextBoolean()); 889 try { 890 s.nextBoolean(); 891 fail(); 892 } catch (NoSuchElementException expected) { 893 } 894 895 s = new Scanner("true1"); 896 try { 897 s.nextBoolean(); 898 fail(); 899 } catch (InputMismatchException expected) { 900 } 901 902 try { 903 s = new Scanner(""); 904 s.nextBoolean(); 905 fail(); 906 } catch (NoSuchElementException expected) { 907 } 908 909 // test socket inputStream 910 os.write("true false".getBytes()); 911 serverSocket.close(); 912 913 s = new Scanner(client); 914 assertTrue(s.nextBoolean()); 915 assertFalse(s.nextBoolean()); 916 917 // ues '*' as delimiter 918 s = new Scanner("true**false").useDelimiter("\\*"); 919 assertTrue(s.nextBoolean()); 920 try { 921 s.nextBoolean(); 922 fail(); 923 } catch (NoSuchElementException expected) { 924 } 925 926 s = new Scanner("false( )").useDelimiter("\\( \\)"); 927 assertFalse(s.nextBoolean()); 928 929 } 930 931 /** 932 * @throws IOException 933 * @tests java.util.Scanner#nextInt(int) 934 */ 935 public void test_nextIntI() throws IOException { 936 s = new Scanner("123 456"); 937 assertEquals(123, s.nextInt(10)); 938 assertEquals(456, s.nextInt(10)); 939 try { 940 s.nextInt(10); 941 fail(); 942 } catch (NoSuchElementException expected) { 943 } 944 945 // If the radix is different from 10 946 s = new Scanner("123 456"); 947 assertEquals(38, s.nextInt(5)); 948 try { 949 s.nextInt(5); 950 fail(); 951 } catch (InputMismatchException expected) { 952 } 953 954 // If the number is out of range 955 s = new Scanner("123456789123456789123456789123456789"); 956 try { 957 s.nextInt(10); 958 fail(); 959 } catch (InputMismatchException expected) { 960 } 961 962 /* 963 * Different locale can only recognize corresponding locale sensitive 964 * string. ',' is used in many locales as group separator. 965 */ 966 s = new Scanner("23,456 23,456"); 967 s.useLocale(Locale.GERMANY); 968 try { 969 s.nextInt(10); 970 fail(); 971 } catch (InputMismatchException expected) { 972 } 973 s.useLocale(Locale.ENGLISH); 974 // If exception is thrown out, input will not be advanced. 975 assertEquals(23456, s.nextInt(10)); 976 assertEquals(23456, s.nextInt(10)); 977 978 /* 979 * ''' is used in many locales as group separator. 980 */ 981 s = new Scanner("23'456 23'456"); 982 s.useLocale(Locale.GERMANY); 983 try { 984 s.nextInt(10); 985 fail(); 986 } catch (InputMismatchException expected) { 987 } 988 s.useLocale(new Locale("it", "CH")); 989 // If exception is thrown out, input will not be advanced. 990 assertEquals(23456, s.nextInt(10)); 991 assertEquals(23456, s.nextInt(10)); 992 993 /* 994 * The input string has Arabic-Indic digits. 995 */ 996 s = new Scanner("1\u06602 1\u06662"); 997 assertEquals(102, s.nextInt(10)); 998 try { 999 s.nextInt(5); 1000 fail(); 1001 } catch (InputMismatchException expected) { 1002 } 1003 assertEquals(162, s.nextInt(10)); 1004 1005 /* 1006 * '.' is used in many locales as group separator. The input string 1007 * has Arabic-Indic digits . 1008 */ 1009 s = new Scanner("23.45\u0666 23.456"); 1010 s.useLocale(Locale.CHINESE); 1011 try { 1012 s.nextInt(10); 1013 fail(); 1014 } catch (InputMismatchException expected) { 1015 } 1016 s.useLocale(Locale.GERMANY); 1017 // If exception is thrown out, input will not be advanced. 1018 assertEquals(23456, s.nextInt(10)); 1019 assertEquals(23456, s.nextInt(10)); 1020 1021 // The input string starts with zero 1022 s = new Scanner("03,456"); 1023 s.useLocale(Locale.ENGLISH); 1024 try { 1025 s.nextInt(10); 1026 fail(); 1027 } catch (InputMismatchException expected) { 1028 } 1029 1030 s = new Scanner("03456"); 1031 assertEquals(3456, s.nextInt(10)); 1032 1033 s = new Scanner("\u06603,456"); 1034 s.useLocale(Locale.ENGLISH); 1035 assertEquals(3456, s.nextInt(10)); 1036 1037 s = new Scanner("E3456"); 1038 assertEquals(930902, s.nextInt(16)); 1039 // The following test case fails on RI, because RI does not support 1040 // letter as leading digit 1041 s = new Scanner("E3,456"); 1042 s.useLocale(Locale.ENGLISH); 1043 assertEquals(930902, s.nextInt(16)); 1044 1045 /* 1046 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1047 * respectively, but they are not differentiated. 1048 */ 1049 s = new Scanner("12300"); 1050 s.useLocale(Locale.CHINESE); 1051 assertEquals(12300, s.nextInt(10)); 1052 1053 s = new Scanner("123\u0966\u0966"); 1054 s.useLocale(Locale.CHINESE); 1055 assertEquals(12300, s.nextInt(10)); 1056 1057 s = new Scanner("123\u0e50\u0e50"); 1058 s.useLocale(Locale.CHINESE); 1059 assertEquals(12300, s.nextInt(10)); 1060 1061 /* 1062 * There are three types of negative prefix all in all. '' '-' '(' There 1063 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 1064 * must be used togethor. Prefix '-' and suffix '-' must be used 1065 * exclusively. 1066 */ 1067 1068 /* 1069 * According to Integer regular expression: Integer :: = ( [-+]? (* 1070 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 1071 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 1072 * recognized by scanner with locale ar_AE, (123) shouble be recognized 1073 * by scanner with locale mk_MK. But this is not the case on RI. 1074 */ 1075 s = new Scanner("-123 123- -123-"); 1076 s.useLocale(new Locale("ar", "AE")); 1077 assertEquals(-123, s.nextInt(10)); 1078 // The following test case fails on RI 1079 assertEquals(-123, s.nextInt(10)); 1080 try { 1081 s.nextInt(10); 1082 fail(); 1083 } catch (InputMismatchException expected) { 1084 } 1085 1086 s = new Scanner("-123 123-"); 1087 s.useLocale(new Locale("mk", "MK")); 1088 assertEquals(-123, s.nextInt(10)); 1089 try { 1090 s.nextInt(); 1091 fail(); 1092 } catch (InputMismatchException expected) { 1093 } 1094 // Skip the un-recognizable token 123-. 1095 assertEquals("123-", s.next()); 1096 1097 // If the parameter radix is illegal, the following test cases fail on 1098 // RI 1099 try { 1100 s.nextInt(Character.MIN_RADIX - 1); 1101 fail(); 1102 } catch (IllegalArgumentException expected) { 1103 } 1104 try { 1105 s.nextInt(Character.MAX_RADIX + 1); 1106 fail(); 1107 } catch (IllegalArgumentException expected) { 1108 } 1109 } 1110 1111 /** 1112 * @throws IOException 1113 * @tests java.util.Scanner#nextInt() 1114 */ 1115 public void test_nextInt() throws IOException { 1116 s = new Scanner("123 456"); 1117 assertEquals(123, s.nextInt()); 1118 assertEquals(456, s.nextInt()); 1119 try { 1120 s.nextInt(); 1121 fail(); 1122 } catch (NoSuchElementException expected) { 1123 } 1124 1125 // If the radix is different from 10 1126 s = new Scanner("123 456"); 1127 s.useRadix(5); 1128 assertEquals(38, s.nextInt()); 1129 try { 1130 s.nextInt(); 1131 fail(); 1132 } catch (InputMismatchException expected) { 1133 } 1134 1135 // If the number is out of range 1136 s = new Scanner("123456789123456789123456789123456789"); 1137 try { 1138 s.nextInt(); 1139 fail(); 1140 } catch (InputMismatchException expected) { 1141 } 1142 1143 /* 1144 * Different locale can only recognize corresponding locale sensitive 1145 * string. ',' is used in many locales as group separator. 1146 */ 1147 s = new Scanner("23,456 23,456"); 1148 s.useLocale(Locale.GERMANY); 1149 try { 1150 s.nextInt(); 1151 fail(); 1152 } catch (InputMismatchException expected) { 1153 } 1154 s.useLocale(Locale.ENGLISH); 1155 // If exception is thrown out, input will not be advanced. 1156 assertEquals(23456, s.nextInt()); 1157 assertEquals(23456, s.nextInt()); 1158 1159 /* 1160 * ''' is used in many locales as group separator. 1161 */ 1162 s = new Scanner("23'456 23'456"); 1163 s.useLocale(Locale.GERMANY); 1164 try { 1165 s.nextInt(); 1166 fail(); 1167 } catch (InputMismatchException expected) { 1168 } 1169 s.useLocale(new Locale("it", "CH")); 1170 // If exception is thrown out, input will not be advanced. 1171 assertEquals(23456, s.nextInt()); 1172 assertEquals(23456, s.nextInt()); 1173 1174 /* 1175 * The input string has Arabic-Indic digits. 1176 */ 1177 s = new Scanner("1\u06602 1\u06662"); 1178 assertEquals(102, s.nextInt()); 1179 s.useRadix(5); 1180 try { 1181 s.nextInt(); 1182 fail(); 1183 } catch (InputMismatchException expected) { 1184 } 1185 s.useRadix(10); 1186 assertEquals(162, s.nextInt()); 1187 1188 /* 1189 * '.' is used in many locales as group separator. The input string 1190 * has Arabic-Indic digits . 1191 */ 1192 s = new Scanner("23.45\u0666 23.456"); 1193 s.useLocale(Locale.CHINESE); 1194 try { 1195 s.nextInt(); 1196 fail(); 1197 } catch (InputMismatchException expected) { 1198 } 1199 s.useLocale(Locale.GERMANY); 1200 // If exception is thrown out, input will not be advanced. 1201 assertEquals(23456, s.nextInt()); 1202 assertEquals(23456, s.nextInt()); 1203 1204 // The input string starts with zero 1205 s = new Scanner("03,456"); 1206 s.useLocale(Locale.ENGLISH); 1207 try { 1208 s.nextInt(); 1209 fail(); 1210 } catch (InputMismatchException expected) { 1211 } 1212 1213 s = new Scanner("03456"); 1214 assertEquals(3456, s.nextInt()); 1215 1216 s = new Scanner("\u06603,456"); 1217 s.useLocale(Locale.ENGLISH); 1218 assertEquals(3456, s.nextInt()); 1219 1220 s = new Scanner("E3456"); 1221 s.useRadix(16); 1222 assertEquals(930902, s.nextInt()); 1223 1224 // The following test case fails on RI, because RI does not support 1225 // letter as leading digit 1226 s = new Scanner("E3,456"); 1227 s.useLocale(Locale.ENGLISH); 1228 s.useRadix(16); 1229 assertEquals(930902, s.nextInt()); 1230 1231 /* 1232 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1233 * respectively, but they are not differentiated. 1234 */ 1235 s = new Scanner("12300"); 1236 s.useLocale(Locale.CHINESE); 1237 assertEquals(12300, s.nextInt()); 1238 1239 s = new Scanner("123\u0966\u0966"); 1240 s.useLocale(Locale.CHINESE); 1241 assertEquals(12300, s.nextInt()); 1242 1243 s = new Scanner("123\u0e50\u0e50"); 1244 s.useLocale(Locale.CHINESE); 1245 assertEquals(12300, s.nextInt()); 1246 1247 /* 1248 * There are three types of negative prefix all in all. '' '-' '(' There 1249 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 1250 * must be used togethor. Prefix '-' and suffix '-' must be used 1251 * exclusively. 1252 */ 1253 1254 /* 1255 * According to Integer regular expression: Integer :: = ( [-+]? (* 1256 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 1257 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 1258 * recognized by scanner with locale ar_AE, (123) shouble be recognized 1259 * by scanner with locale mk_MK. But this is not the case on RI. 1260 */ 1261 s = new Scanner("-123 123- -123-"); 1262 s.useLocale(new Locale("ar", "AE")); 1263 assertEquals(-123, s.nextInt()); 1264 // The following test case fails on RI 1265 assertEquals(-123, s.nextInt()); 1266 try { 1267 s.nextInt(); 1268 fail(); 1269 } catch (InputMismatchException expected) { 1270 } 1271 1272 s = new Scanner("-123 123-"); 1273 s.useLocale(new Locale("mk", "MK")); 1274 assertEquals(-123, s.nextInt()); 1275 try { 1276 s.nextInt(); 1277 fail(); 1278 } catch (InputMismatchException expected) { 1279 } 1280 // Skip the un-recognizable token 123-. 1281 assertEquals("123-", s.next()); 1282 } 1283 1284 /** 1285 * @throws IOException 1286 * @tests java.util.Scanner#nextByte(int) 1287 */ 1288 public void test_nextByteI() throws IOException { 1289 s = new Scanner("123 126"); 1290 assertEquals(123, s.nextByte(10)); 1291 assertEquals(126, s.nextByte(10)); 1292 try { 1293 s.nextByte(10); 1294 fail(); 1295 } catch (NoSuchElementException expected) { 1296 } 1297 1298 // If the radix is different from 10 1299 s = new Scanner("123 126"); 1300 assertEquals(38, s.nextByte(5)); 1301 try { 1302 s.nextByte(5); 1303 fail(); 1304 } catch (InputMismatchException expected) { 1305 } 1306 1307 // If the number is out of range 1308 s = new Scanner("1234"); 1309 try { 1310 s.nextByte(10); 1311 fail(); 1312 } catch (InputMismatchException expected) { 1313 } 1314 1315 /* 1316 * The input string has Arabic-Indic digits. 1317 */ 1318 s = new Scanner("1\u06602 12\u0666"); 1319 assertEquals(102, s.nextByte(10)); 1320 try { 1321 s.nextByte(5); 1322 fail(); 1323 } catch (InputMismatchException expected) { 1324 } 1325 assertEquals(126, s.nextByte(10)); 1326 1327 s = new Scanner("012"); 1328 assertEquals(12, s.nextByte(10)); 1329 1330 s = new Scanner("E"); 1331 assertEquals(14, s.nextByte(16)); 1332 1333 /* 1334 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1335 * respectively, but they are not differentiated. 1336 */ 1337 s = new Scanner("100"); 1338 s.useLocale(Locale.CHINESE); 1339 assertEquals(100, s.nextByte(10)); 1340 1341 s = new Scanner("1\u0966\u0966"); 1342 s.useLocale(Locale.CHINESE); 1343 assertEquals(100, s.nextByte(10)); 1344 1345 s = new Scanner("1\u0e50\u0e50"); 1346 s.useLocale(Locale.CHINESE); 1347 assertEquals(100, s.nextByte(10)); 1348 1349 s = new Scanner("-123"); 1350 s.useLocale(new Locale("ar", "AE")); 1351 assertEquals(-123, s.nextByte(10)); 1352 1353 1354 s = new Scanner("-123"); 1355 s.useLocale(new Locale("mk", "MK")); 1356 assertEquals(-123, s.nextByte(10)); 1357 } 1358 1359 /** 1360 * @throws IOException 1361 * @tests java.util.Scanner#nextByte() 1362 */ 1363 public void test_nextByte() throws IOException { 1364 s = new Scanner("123 126"); 1365 assertEquals(123, s.nextByte()); 1366 assertEquals(126, s.nextByte()); 1367 try { 1368 s.nextByte(); 1369 fail(); 1370 } catch (NoSuchElementException expected) { 1371 } 1372 1373 // If the radix is different from 10 1374 s = new Scanner("123 126"); 1375 s.useRadix(5); 1376 assertEquals(38, s.nextByte()); 1377 try { 1378 s.nextByte(); 1379 fail(); 1380 } catch (InputMismatchException expected) { 1381 } 1382 1383 // If the number is out of range 1384 s = new Scanner("1234"); 1385 try { 1386 s.nextByte(); 1387 fail(); 1388 } catch (InputMismatchException expected) { 1389 } 1390 1391 /* 1392 * The input string has Arabic-Indic digits. 1393 */ 1394 s = new Scanner("1\u06602 12\u0666"); 1395 assertEquals(102, s.nextByte()); 1396 s.useRadix(5); 1397 try { 1398 s.nextByte(); 1399 fail(); 1400 } catch (InputMismatchException expected) { 1401 } 1402 s.useRadix(10); 1403 assertEquals(126, s.nextByte()); 1404 1405 s = new Scanner("012"); 1406 assertEquals(12, s.nextByte()); 1407 1408 s = new Scanner("E"); 1409 s.useRadix(16); 1410 assertEquals(14, s.nextByte()); 1411 1412 /* 1413 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1414 * respectively, but they are not differentiated. 1415 */ 1416 s = new Scanner("100"); 1417 s.useLocale(Locale.CHINESE); 1418 assertEquals(100, s.nextByte()); 1419 1420 s = new Scanner("1\u0966\u0966"); 1421 s.useLocale(Locale.CHINESE); 1422 assertEquals(100, s.nextByte()); 1423 1424 s = new Scanner("1\u0e50\u0e50"); 1425 s.useLocale(Locale.CHINESE); 1426 assertEquals(100, s.nextByte()); 1427 1428 s = new Scanner("-123"); 1429 s.useLocale(new Locale("ar", "AE")); 1430 assertEquals(-123, s.nextByte()); 1431 1432 s = new Scanner("-123"); 1433 s.useLocale(new Locale("mk", "MK")); 1434 assertEquals(-123, s.nextByte()); 1435 } 1436 1437 /** 1438 * @throws IOException 1439 * @tests java.util.Scanner#nextFloat() 1440 */ 1441 public void test_nextFloat() throws IOException { 1442 s = new Scanner("123 45\u0666. 123.4 .123 "); 1443 s.useLocale(Locale.ENGLISH); 1444 assertEquals((float)123.0, s.nextFloat()); 1445 assertEquals((float)456.0, s.nextFloat()); 1446 assertEquals((float)123.4, s.nextFloat()); 1447 assertEquals((float)0.123, s.nextFloat()); 1448 try { 1449 s.nextFloat(); 1450 fail(); 1451 } catch (NoSuchElementException expected) { 1452 } 1453 1454 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 1455 s.useLocale(Locale.ENGLISH); 1456 assertEquals((float)123.4, s.nextFloat()); 1457 assertEquals((float)-456.7, s.nextFloat()); 1458 assertEquals((float)123456.789, s.nextFloat()); 1459 try { 1460 s.nextFloat(); 1461 fail(); 1462 } catch (InputMismatchException expected) { 1463 } 1464 1465 // Scientific notation 1466 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 1467 s.useLocale(Locale.ENGLISH); 1468 assertEquals((float)1.234E12, s.nextFloat()); 1469 assertEquals((float)-4.567E14, s.nextFloat()); 1470 assertEquals((float)1.23456789E-5, s.nextFloat()); 1471 1472 s = new Scanner("NaN Infinity -Infinity"); 1473 assertEquals(Float.NaN, s.nextFloat()); 1474 assertEquals(Float.POSITIVE_INFINITY, s.nextFloat()); 1475 assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat()); 1476 1477 String str=String.valueOf(Float.MAX_VALUE*2); 1478 s=new Scanner(str); 1479 assertEquals(Float.POSITIVE_INFINITY,s.nextFloat()); 1480 1481 /* 1482 * Different locale can only recognize corresponding locale sensitive 1483 * string. ',' is used in many locales as group separator. 1484 */ 1485 s = new Scanner("23,456 23,456"); 1486 s.useLocale(Locale.ENGLISH); 1487 assertEquals((float)23456.0, s.nextFloat()); 1488 s.useLocale(Locale.GERMANY); 1489 assertEquals((float)23.456, s.nextFloat()); 1490 1491 s = new Scanner("23.456 23.456"); 1492 s.useLocale(Locale.ENGLISH); 1493 assertEquals((float)23.456, s.nextFloat()); 1494 s.useLocale(Locale.GERMANY); 1495 assertEquals((float)23456.0, s.nextFloat()); 1496 1497 s = new Scanner("23,456.7 23.456,7"); 1498 s.useLocale(Locale.ENGLISH); 1499 assertEquals((float)23456.7, s.nextFloat()); 1500 s.useLocale(Locale.GERMANY); 1501 assertEquals((float)23456.7, s.nextFloat()); 1502 1503 s = new Scanner("-123.4 123.4- -123.4-"); 1504 s.useLocale(new Locale("ar", "AE")); 1505 // FIXME 1506// assertEquals((float)-123.4, s.nextFloat()); 1507// //The following test case fails on RI 1508// assertEquals((float)-123.4, s.nextFloat()); 1509 try { 1510 s.nextFloat(); 1511 fail(); 1512 } catch (InputMismatchException expected) { 1513 } 1514 1515 s = new Scanner("123- -123"); 1516 s.useLocale(new Locale("mk", "MK")); 1517 try { 1518 s.nextFloat(); 1519 fail(); 1520 } catch (InputMismatchException expected) { 1521 } 1522 // Skip the un-recognizable token 123-. 1523 assertEquals("123-", s.next()); 1524 assertEquals((float)-123.0, s.nextFloat()); 1525 1526 } 1527 1528 /** 1529 * @throws IOException 1530 * @tests java.util.Scanner#nextBigInteger(int) 1531 */ 1532 public void test_nextBigIntegerI() throws IOException { 1533 s = new Scanner("123 456"); 1534 assertEquals(new BigInteger("123"), s.nextBigInteger(10)); 1535 assertEquals(new BigInteger("456"), s.nextBigInteger(10)); 1536 try { 1537 s.nextBigInteger(10); 1538 fail(); 1539 } catch (NoSuchElementException expected) { 1540 } 1541 1542 // If the radix is different from 10 1543 s = new Scanner("123 456"); 1544 assertEquals(new BigInteger("38"), s.nextBigInteger(5)); 1545 try { 1546 s.nextBigInteger(5); 1547 fail(); 1548 } catch (InputMismatchException expected) { 1549 } 1550 1551 /* 1552 * Different locale can only recognize corresponding locale sensitive 1553 * string. ',' is used in many locales as group separator. 1554 */ 1555 s = new Scanner("23,456 23,456"); 1556 s.useLocale(Locale.GERMANY); 1557 try { 1558 s.nextBigInteger(10); 1559 fail(); 1560 } catch (InputMismatchException expected) { 1561 } 1562 s.useLocale(Locale.ENGLISH); 1563 // If exception is thrown out, input will not be advanced. 1564 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1565 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1566 1567 /* 1568 * ''' is used in many locales as group separator. 1569 */ 1570 s = new Scanner("23'456 23'456"); 1571 s.useLocale(Locale.GERMANY); 1572 try { 1573 s.nextBigInteger(10); 1574 fail(); 1575 } catch (InputMismatchException expected) { 1576 } 1577 s.useLocale(new Locale("it", "CH")); 1578 // If exception is thrown out, input will not be advanced. 1579 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1580 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1581 1582 /* 1583 * The input string has Arabic-Indic digits. 1584 */ 1585 s = new Scanner("1\u06602 1\u06662"); 1586 assertEquals(new BigInteger("102"), s.nextBigInteger(10)); 1587 try { 1588 s.nextBigInteger(5); 1589 fail(); 1590 } catch (InputMismatchException expected) { 1591 } 1592 assertEquals(new BigInteger("162"), s.nextBigInteger(10)); 1593 1594 /* 1595 * '.' is used in many locales as group separator. The input string 1596 * has Arabic-Indic digits . 1597 */ 1598 s = new Scanner("23.45\u0666 23.456"); 1599 s.useLocale(Locale.CHINESE); 1600 try { 1601 s.nextBigInteger(10); 1602 fail(); 1603 } catch (InputMismatchException expected) { 1604 } 1605 s.useLocale(Locale.GERMANY); 1606 // If exception is thrown out, input will not be advanced. 1607 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1608 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 1609 1610 // The input string starts with zero 1611 s = new Scanner("03,456"); 1612 s.useLocale(Locale.ENGLISH); 1613 try { 1614 s.nextBigInteger(10); 1615 fail(); 1616 } catch (InputMismatchException expected) { 1617 } 1618 1619 s = new Scanner("03456"); 1620 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 1621 1622 s = new Scanner("\u06603,456"); 1623 s.useLocale(Locale.ENGLISH); 1624 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 1625 1626 s = new Scanner("E34"); 1627 assertEquals(new BigInteger("3636"), s.nextBigInteger(16)); 1628 1629 /* 1630 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1631 * respectively, but they are not differentiated. 1632 */ 1633 s = new Scanner("12300"); 1634 s.useLocale(Locale.CHINESE); 1635 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1636 1637 s = new Scanner("123\u0966\u0966"); 1638 s.useLocale(Locale.CHINESE); 1639 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1640 1641 s = new Scanner("123\u0e50\u0e50"); 1642 s.useLocale(Locale.CHINESE); 1643 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 1644 1645 s = new Scanner("-123"); 1646 s.useLocale(new Locale("ar", "AE")); 1647 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 1648 1649 1650 s = new Scanner("-123"); 1651 s.useLocale(new Locale("mk", "MK")); 1652 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 1653 } 1654 1655 /** 1656 * @throws IOException 1657 * @tests java.util.Scanner#nextBigInteger() 1658 */ 1659 public void test_nextBigInteger() throws IOException { 1660 s = new Scanner("123 456"); 1661 assertEquals(new BigInteger("123"), s.nextBigInteger()); 1662 assertEquals(new BigInteger("456"), s.nextBigInteger()); 1663 try { 1664 s.nextBigInteger(); 1665 fail(); 1666 } catch (NoSuchElementException expected) { 1667 } 1668 1669 // If the radix is different from 10 1670 s = new Scanner("123 456"); 1671 s.useRadix(5); 1672 assertEquals(new BigInteger("38"), s.nextBigInteger()); 1673 try { 1674 s.nextBigInteger(); 1675 fail(); 1676 } catch (InputMismatchException expected) { 1677 } 1678 1679 /* 1680 * Different locale can only recognize corresponding locale sensitive 1681 * string. ',' is used in many locales as group separator. 1682 */ 1683 s = new Scanner("23,456 23,456"); 1684 s.useLocale(Locale.GERMANY); 1685 try { 1686 s.nextBigInteger(); 1687 fail(); 1688 } catch (InputMismatchException expected) { 1689 } 1690 s.useLocale(Locale.ENGLISH); 1691 // If exception is thrown out, input will not be advanced. 1692 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1693 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1694 1695 /* 1696 * ''' is used in many locales as group separator. 1697 */ 1698 s = new Scanner("23'456 23'456"); 1699 s.useLocale(Locale.GERMANY); 1700 try { 1701 s.nextBigInteger(); 1702 fail(); 1703 } catch (InputMismatchException expected) { 1704 } 1705 s.useLocale(new Locale("it", "CH")); 1706 // If exception is thrown out, input will not be advanced. 1707 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1708 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1709 1710 /* 1711 * The input string has Arabic-Indic digits. 1712 */ 1713 s = new Scanner("1\u06602 1\u06662"); 1714 assertEquals(new BigInteger("102"), s.nextBigInteger()); 1715 s.useRadix(5); 1716 try { 1717 s.nextBigInteger(); 1718 fail(); 1719 } catch (InputMismatchException expected) { 1720 } 1721 s.useRadix(10); 1722 assertEquals(new BigInteger("162"), s.nextBigInteger()); 1723 1724 /* 1725 * '.' is used in many locales as group separator. The input string 1726 * has Arabic-Indic digits . 1727 */ 1728 s = new Scanner("23.45\u0666 23.456"); 1729 s.useLocale(Locale.CHINESE); 1730 try { 1731 s.nextBigInteger(); 1732 fail(); 1733 } catch (InputMismatchException expected) { 1734 } 1735 s.useLocale(Locale.GERMANY); 1736 // If exception is thrown out, input will not be advanced. 1737 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1738 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 1739 1740 // The input string starts with zero 1741 s = new Scanner("03,456"); 1742 s.useLocale(Locale.ENGLISH); 1743 try { 1744 s.nextBigInteger(); 1745 fail(); 1746 } catch (InputMismatchException expected) { 1747 } 1748 1749 s = new Scanner("03456"); 1750 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 1751 1752 s = new Scanner("\u06603,456"); 1753 s.useLocale(Locale.ENGLISH); 1754 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 1755 1756 s = new Scanner("E34"); 1757 s.useRadix(16); 1758 assertEquals(new BigInteger("3636"), s.nextBigInteger()); 1759 1760 /* 1761 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1762 * respectively, but they are not differentiated. 1763 */ 1764 s = new Scanner("12300"); 1765 s.useLocale(Locale.CHINESE); 1766 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 1767 1768 s = new Scanner("123\u0966\u0966"); 1769 s.useLocale(Locale.CHINESE); 1770 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 1771 1772 s = new Scanner("123\u0e50\u0e50"); 1773 s.useLocale(Locale.CHINESE); 1774 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 1775 1776 s = new Scanner("-123"); 1777 s.useLocale(new Locale("ar", "AE")); 1778 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 1779 1780 s = new Scanner("-123"); 1781 s.useLocale(new Locale("mk", "MK")); 1782 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 1783 } 1784 1785 /** 1786 * @throws IOException 1787 * @tests java.util.Scanner#nextShort(int) 1788 */ 1789 public void test_nextShortI() throws IOException { 1790 s = new Scanner("123 456"); 1791 assertEquals(123, s.nextShort(10)); 1792 assertEquals(456, s.nextShort(10)); 1793 try { 1794 s.nextShort(10); 1795 fail(); 1796 } catch (NoSuchElementException expected) { 1797 } 1798 1799 // If the radix is different from 10 1800 s = new Scanner("123 456"); 1801 assertEquals(38, s.nextShort(5)); 1802 try { 1803 s.nextShort(5); 1804 fail(); 1805 } catch (InputMismatchException expected) { 1806 } 1807 1808 // If the number is out of range 1809 s = new Scanner("123456789"); 1810 try { 1811 s.nextShort(10); 1812 fail(); 1813 } catch (InputMismatchException expected) { 1814 } 1815 1816 /* 1817 * Different locale can only recognize corresponding locale sensitive 1818 * string. ',' is used in many locales as group separator. 1819 */ 1820 s = new Scanner("23,456 23,456"); 1821 s.useLocale(Locale.GERMANY); 1822 try { 1823 s.nextShort(10); 1824 fail(); 1825 } catch (InputMismatchException expected) { 1826 } 1827 s.useLocale(Locale.ENGLISH); 1828 // If exception is thrown out, input will not be advanced. 1829 assertEquals(23456, s.nextShort(10)); 1830 assertEquals(23456, s.nextShort(10)); 1831 1832 /* 1833 * ''' is used in many locales as group separator. 1834 */ 1835 s = new Scanner("23'456 23'456"); 1836 s.useLocale(Locale.GERMANY); 1837 try { 1838 s.nextShort(10); 1839 fail(); 1840 } catch (InputMismatchException expected) { 1841 } 1842 s.useLocale(new Locale("it", "CH")); 1843 // If exception is thrown out, input will not be advanced. 1844 assertEquals(23456, s.nextShort(10)); 1845 assertEquals(23456, s.nextShort(10)); 1846 1847 /* 1848 * The input string has Arabic-Indic digits. 1849 */ 1850 s = new Scanner("1\u06602 1\u06662"); 1851 assertEquals(102, s.nextShort(10)); 1852 try { 1853 s.nextShort(5); 1854 fail(); 1855 } catch (InputMismatchException expected) { 1856 } 1857 assertEquals(162, s.nextShort(10)); 1858 1859 /* 1860 * '.' is used in many locales as group separator. The input string 1861 * has Arabic-Indic digits . 1862 */ 1863 s = new Scanner("23.45\u0666 23.456"); 1864 s.useLocale(Locale.CHINESE); 1865 try { 1866 s.nextShort(10); 1867 fail(); 1868 } catch (InputMismatchException expected) { 1869 } 1870 s.useLocale(Locale.GERMANY); 1871 // If exception is thrown out, input will not be advanced. 1872 assertEquals(23456, s.nextShort(10)); 1873 assertEquals(23456, s.nextShort(10)); 1874 1875 // The input string starts with zero 1876 s = new Scanner("03,456"); 1877 s.useLocale(Locale.ENGLISH); 1878 try { 1879 s.nextShort(10); 1880 fail(); 1881 } catch (InputMismatchException expected) { 1882 } 1883 1884 s = new Scanner("03456"); 1885 assertEquals(3456, s.nextShort(10)); 1886 1887 s = new Scanner("\u06603,456"); 1888 s.useLocale(Locale.ENGLISH); 1889 assertEquals(3456, s.nextShort(10)); 1890 1891 s = new Scanner("E34"); 1892 assertEquals(3636, s.nextShort(16)); 1893 1894 /* 1895 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 1896 * respectively, but they are not differentiated. 1897 */ 1898 s = new Scanner("12300"); 1899 s.useLocale(Locale.CHINESE); 1900 assertEquals(12300, s.nextShort(10)); 1901 1902 s = new Scanner("123\u0966\u0966"); 1903 s.useLocale(Locale.CHINESE); 1904 assertEquals(12300, s.nextShort(10)); 1905 1906 s = new Scanner("123\u0e50\u0e50"); 1907 s.useLocale(Locale.CHINESE); 1908 assertEquals(12300, s.nextShort(10)); 1909 1910 s = new Scanner("-123"); 1911 s.useLocale(new Locale("ar", "AE")); 1912 assertEquals(-123, s.nextShort(10)); 1913 1914 1915 s = new Scanner("-123"); 1916 s.useLocale(new Locale("mk", "MK")); 1917 assertEquals(-123, s.nextShort(10)); 1918 } 1919 1920 /** 1921 * @throws IOException 1922 * @tests java.util.Scanner#nextShort() 1923 */ 1924 public void test_nextShort() throws IOException { 1925 s = new Scanner("123 456"); 1926 assertEquals(123, s.nextShort()); 1927 assertEquals(456, s.nextShort()); 1928 try { 1929 s.nextShort(); 1930 fail(); 1931 } catch (NoSuchElementException expected) { 1932 } 1933 1934 // If the radix is different from 10 1935 s = new Scanner("123 456"); 1936 s.useRadix(5); 1937 assertEquals(38, s.nextShort()); 1938 try { 1939 s.nextShort(); 1940 fail(); 1941 } catch (InputMismatchException expected) { 1942 } 1943 1944 // If the number is out of range 1945 s = new Scanner("123456789"); 1946 try { 1947 s.nextShort(); 1948 fail(); 1949 } catch (InputMismatchException expected) { 1950 } 1951 1952 /* 1953 * Different locale can only recognize corresponding locale sensitive 1954 * string. ',' is used in many locales as group separator. 1955 */ 1956 s = new Scanner("23,456 23,456"); 1957 s.useLocale(Locale.GERMANY); 1958 try { 1959 s.nextShort(); 1960 fail(); 1961 } catch (InputMismatchException expected) { 1962 } 1963 s.useLocale(Locale.ENGLISH); 1964 // If exception is thrown out, input will not be advanced. 1965 assertEquals(23456, s.nextShort()); 1966 assertEquals(23456, s.nextShort()); 1967 1968 /* 1969 * ''' is used in many locales as group separator. 1970 */ 1971 s = new Scanner("23'456 23'456"); 1972 s.useLocale(Locale.GERMANY); 1973 try { 1974 s.nextShort(); 1975 fail(); 1976 } catch (InputMismatchException expected) { 1977 } 1978 s.useLocale(new Locale("it", "CH")); 1979 // If exception is thrown out, input will not be advanced. 1980 assertEquals(23456, s.nextShort()); 1981 assertEquals(23456, s.nextShort()); 1982 1983 /* 1984 * The input string has Arabic-Indic digits. 1985 */ 1986 s = new Scanner("1\u06602 1\u06662"); 1987 assertEquals(102, s.nextShort()); 1988 s.useRadix(5); 1989 try { 1990 s.nextShort(); 1991 fail(); 1992 } catch (InputMismatchException expected) { 1993 } 1994 s.useRadix(10); 1995 assertEquals(162, s.nextShort()); 1996 1997 /* 1998 * '.' is used in many locales as group separator. The input string 1999 * has Arabic-Indic digits . 2000 */ 2001 s = new Scanner("23.45\u0666 23.456"); 2002 s.useLocale(Locale.CHINESE); 2003 try { 2004 s.nextShort(); 2005 fail(); 2006 } catch (InputMismatchException expected) { 2007 } 2008 s.useLocale(Locale.GERMANY); 2009 // If exception is thrown out, input will not be advanced. 2010 assertEquals(23456, s.nextShort()); 2011 assertEquals(23456, s.nextShort()); 2012 2013 // The input string starts with zero 2014 s = new Scanner("03,456"); 2015 s.useLocale(Locale.ENGLISH); 2016 try { 2017 s.nextShort(); 2018 fail(); 2019 } catch (InputMismatchException expected) { 2020 } 2021 2022 s = new Scanner("03456"); 2023 assertEquals(3456, s.nextShort()); 2024 2025 s = new Scanner("\u06603,456"); 2026 s.useLocale(Locale.ENGLISH); 2027 assertEquals(3456, s.nextShort()); 2028 2029 s = new Scanner("E34"); 2030 s.useRadix(16); 2031 assertEquals(3636, s.nextShort()); 2032 2033 /* 2034 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2035 * respectively, but they are not differentiated. 2036 */ 2037 s = new Scanner("12300"); 2038 s.useLocale(Locale.CHINESE); 2039 assertEquals(12300, s.nextShort()); 2040 2041 s = new Scanner("123\u0966\u0966"); 2042 s.useLocale(Locale.CHINESE); 2043 assertEquals(12300, s.nextShort()); 2044 2045 s = new Scanner("123\u0e50\u0e50"); 2046 s.useLocale(Locale.CHINESE); 2047 assertEquals(12300, s.nextShort()); 2048 2049 s = new Scanner("-123"); 2050 s.useLocale(new Locale("ar", "AE")); 2051 assertEquals(-123, s.nextShort()); 2052 2053 s = new Scanner("-123"); 2054 s.useLocale(new Locale("mk", "MK")); 2055 assertEquals(-123, s.nextShort()); 2056 } 2057 2058 /** 2059 * @throws IOException 2060 * @tests java.util.Scanner#nextLong(int) 2061 */ 2062 public void test_nextLongI() throws IOException { 2063 s = new Scanner("123 456"); 2064 assertEquals(123, s.nextLong(10)); 2065 assertEquals(456, s.nextLong(10)); 2066 try { 2067 s.nextLong(10); 2068 fail(); 2069 } catch (NoSuchElementException expected) { 2070 } 2071 2072 // If the radix is different from 10 2073 s = new Scanner("123 456"); 2074 assertEquals(38, s.nextLong(5)); 2075 try { 2076 s.nextLong(5); 2077 fail(); 2078 } catch (InputMismatchException expected) { 2079 } 2080 2081 // If the number is out of range 2082 s = new Scanner("123456789123456789123456789123456789"); 2083 try { 2084 s.nextLong(10); 2085 fail(); 2086 } catch (InputMismatchException expected) { 2087 } 2088 2089 /* 2090 * Different locale can only recognize corresponding locale sensitive 2091 * string. ',' is used in many locales as group separator. 2092 */ 2093 s = new Scanner("23,456 23,456"); 2094 s.useLocale(Locale.GERMANY); 2095 try { 2096 s.nextLong(10); 2097 fail(); 2098 } catch (InputMismatchException expected) { 2099 } 2100 s.useLocale(Locale.ENGLISH); 2101 // If exception is thrown out, input will not be advanced. 2102 assertEquals(23456, s.nextLong(10)); 2103 assertEquals(23456, s.nextLong(10)); 2104 2105 /* 2106 * ''' is used in many locales as group separator. 2107 */ 2108 s = new Scanner("23'456 23'456"); 2109 s.useLocale(Locale.GERMANY); 2110 try { 2111 s.nextLong(10); 2112 fail(); 2113 } catch (InputMismatchException expected) { 2114 } 2115 s.useLocale(new Locale("it", "CH")); 2116 // If exception is thrown out, input will not be advanced. 2117 assertEquals(23456, s.nextLong(10)); 2118 assertEquals(23456, s.nextLong(10)); 2119 2120 /* 2121 * The input string has Arabic-Indic digits. 2122 */ 2123 s = new Scanner("1\u06602 1\u06662"); 2124 assertEquals(102, s.nextLong(10)); 2125 try { 2126 s.nextLong(5); 2127 fail(); 2128 } catch (InputMismatchException expected) { 2129 } 2130 assertEquals(162, s.nextLong(10)); 2131 2132 /* 2133 * '.' is used in many locales as group separator. The input string 2134 * has Arabic-Indic digits . 2135 */ 2136 s = new Scanner("23.45\u0666 23.456"); 2137 s.useLocale(Locale.CHINESE); 2138 try { 2139 s.nextLong(10); 2140 fail(); 2141 } catch (InputMismatchException expected) { 2142 } 2143 s.useLocale(Locale.GERMANY); 2144 // If exception is thrown out, input will not be advanced. 2145 assertEquals(23456, s.nextLong(10)); 2146 assertEquals(23456, s.nextLong(10)); 2147 2148 // The input string starts with zero 2149 s = new Scanner("03,456"); 2150 s.useLocale(Locale.ENGLISH); 2151 try { 2152 s.nextLong(10); 2153 fail(); 2154 } catch (InputMismatchException expected) { 2155 } 2156 2157 s = new Scanner("03456"); 2158 assertEquals(3456, s.nextLong(10)); 2159 2160 s = new Scanner("\u06603,456"); 2161 s.useLocale(Locale.ENGLISH); 2162 assertEquals(3456, s.nextLong(10)); 2163 2164 s = new Scanner("E34"); 2165 assertEquals(3636, s.nextLong(16)); 2166 2167 /* 2168 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2169 * respectively, but they are not differentiated. 2170 */ 2171 s = new Scanner("12300"); 2172 s.useLocale(Locale.CHINESE); 2173 assertEquals(12300, s.nextLong(10)); 2174 2175 s = new Scanner("123\u0966\u0966"); 2176 s.useLocale(Locale.CHINESE); 2177 assertEquals(12300, s.nextLong(10)); 2178 2179 s = new Scanner("123\u0e50\u0e50"); 2180 s.useLocale(Locale.CHINESE); 2181 assertEquals(12300, s.nextLong(10)); 2182 2183 s = new Scanner("-123"); 2184 s.useLocale(new Locale("ar", "AE")); 2185 assertEquals(-123, s.nextLong(10)); 2186 2187 2188 s = new Scanner("-123"); 2189 s.useLocale(new Locale("mk", "MK")); 2190 assertEquals(-123, s.nextLong(10)); 2191 } 2192 2193 /** 2194 * @throws IOException 2195 * @tests java.util.Scanner#nextLong() 2196 */ 2197 public void test_nextLong() throws IOException { 2198 s = new Scanner("123 456"); 2199 assertEquals(123, s.nextLong()); 2200 assertEquals(456, s.nextLong()); 2201 try { 2202 s.nextLong(); 2203 fail(); 2204 } catch (NoSuchElementException expected) { 2205 } 2206 2207 // If the radix is different from 10 2208 s = new Scanner("123 456"); 2209 s.useRadix(5); 2210 assertEquals(38, s.nextLong()); 2211 try { 2212 s.nextLong(); 2213 fail(); 2214 } catch (InputMismatchException expected) { 2215 } 2216 2217 // If the number is out of range 2218 s = new Scanner("123456789123456789123456789123456789"); 2219 try { 2220 s.nextLong(); 2221 fail(); 2222 } catch (InputMismatchException expected) { 2223 } 2224 2225 /* 2226 * Different locale can only recognize corresponding locale sensitive 2227 * string. ',' is used in many locales as group separator. 2228 */ 2229 s = new Scanner("23,456 23,456"); 2230 s.useLocale(Locale.GERMANY); 2231 try { 2232 s.nextLong(); 2233 fail(); 2234 } catch (InputMismatchException expected) { 2235 } 2236 s.useLocale(Locale.ENGLISH); 2237 // If exception is thrown out, input will not be advanced. 2238 assertEquals(23456, s.nextLong()); 2239 assertEquals(23456, s.nextLong()); 2240 2241 /* 2242 * ''' is used in many locales as group separator. 2243 */ 2244 s = new Scanner("23'456 23'456"); 2245 s.useLocale(Locale.GERMANY); 2246 try { 2247 s.nextLong(); 2248 fail(); 2249 } catch (InputMismatchException expected) { 2250 } 2251 s.useLocale(new Locale("it", "CH")); 2252 // If exception is thrown out, input will not be advanced. 2253 assertEquals(23456, s.nextLong()); 2254 assertEquals(23456, s.nextLong()); 2255 2256 /* 2257 * The input string has Arabic-Indic digits. 2258 */ 2259 s = new Scanner("1\u06602 1\u06662"); 2260 assertEquals(102, s.nextLong()); 2261 s.useRadix(5); 2262 try { 2263 s.nextLong(); 2264 fail(); 2265 } catch (InputMismatchException expected) { 2266 } 2267 s.useRadix(10); 2268 assertEquals(162, s.nextLong()); 2269 2270 /* 2271 * '.' is used in many locales as group separator. The input string 2272 * has Arabic-Indic digits . 2273 */ 2274 s = new Scanner("23.45\u0666 23.456"); 2275 s.useLocale(Locale.CHINESE); 2276 try { 2277 s.nextLong(); 2278 fail(); 2279 } catch (InputMismatchException expected) { 2280 } 2281 s.useLocale(Locale.GERMANY); 2282 // If exception is thrown out, input will not be advanced. 2283 assertEquals(23456, s.nextLong()); 2284 assertEquals(23456, s.nextLong()); 2285 2286 // The input string starts with zero 2287 s = new Scanner("03,456"); 2288 s.useLocale(Locale.ENGLISH); 2289 try { 2290 s.nextLong(); 2291 fail(); 2292 } catch (InputMismatchException expected) { 2293 } 2294 2295 s = new Scanner("03456"); 2296 assertEquals(3456, s.nextLong()); 2297 2298 s = new Scanner("\u06603,456"); 2299 s.useLocale(Locale.ENGLISH); 2300 assertEquals(3456, s.nextLong()); 2301 2302 s = new Scanner("E34"); 2303 s.useRadix(16); 2304 assertEquals(3636, s.nextLong()); 2305 2306 /* 2307 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2308 * respectively, but they are not differentiated. 2309 */ 2310 s = new Scanner("12300"); 2311 s.useLocale(Locale.CHINESE); 2312 assertEquals(12300, s.nextLong()); 2313 2314 s = new Scanner("123\u0966\u0966"); 2315 s.useLocale(Locale.CHINESE); 2316 assertEquals(12300, s.nextLong()); 2317 2318 s = new Scanner("123\u0e50\u0e50"); 2319 s.useLocale(Locale.CHINESE); 2320 assertEquals(12300, s.nextLong()); 2321 2322 s = new Scanner("-123"); 2323 s.useLocale(new Locale("ar", "AE")); 2324 assertEquals(-123, s.nextLong()); 2325 2326 s = new Scanner("-123"); 2327 s.useLocale(new Locale("mk", "MK")); 2328 assertEquals(-123, s.nextLong()); 2329 } 2330 2331 /** 2332 * @throws IOException 2333 * @tests java.util.Scanner#hasNext() 2334 */ 2335 public void test_hasNext() throws IOException { 2336 s = new Scanner("1##2").useDelimiter("\\#"); 2337 assertTrue(s.hasNext()); 2338 assertEquals("1", s.next()); 2339 assertEquals("", s.next()); 2340 assertEquals("2", s.next()); 2341 assertFalse(s.hasNext()); 2342 s.close(); 2343 try { 2344 s.hasNext(); 2345 fail(); 2346 } catch (IllegalStateException expected) { 2347 } 2348 2349 s = new Scanner("1( )2( )").useDelimiter("\\( \\)"); 2350 assertTrue(s.hasNext()); 2351 assertTrue(s.hasNext()); 2352 assertEquals("1", s.next()); 2353 assertEquals("2", s.next()); 2354 2355 s = new Scanner("1 2 ").useDelimiter("( )"); 2356 assertEquals("1", s.next()); 2357 assertEquals("2", s.next()); 2358 assertTrue(s.hasNext()); 2359 assertEquals("", s.next()); 2360 2361 s = new Scanner("1\n2 "); 2362 assertEquals("1", s.next()); 2363 assertTrue(s.hasNext()); 2364 assertEquals("2", s.next()); 2365 assertFalse(s.hasNext()); 2366 // test boundary case 2367 try { 2368 s.next(); 2369 fail(); 2370 } catch (NoSuchElementException expected) { 2371 } 2372 2373 s = new Scanner("1'\n'2 "); 2374 assertEquals("1'", s.next()); 2375 assertTrue(s.hasNext()); 2376 assertEquals("'2", s.next()); 2377 assertFalse(s.hasNext()); 2378 // test boundary case 2379 try { 2380 s.next(); 2381 fail(); 2382 } catch (NoSuchElementException expected) { 2383 } 2384 2385 s = new Scanner(" "); 2386 assertFalse(s.hasNext()); 2387 2388 // test socket inputStream 2389 2390 os.write("1 2".getBytes()); 2391 serverSocket.close(); 2392 2393 s = new Scanner(client); 2394 assertEquals("1", s.next()); 2395 assertTrue(s.hasNext()); 2396 assertEquals("2", s.next()); 2397 assertFalse(s.hasNext()); 2398 try { 2399 s.next(); 2400 fail(); 2401 } catch (NoSuchElementException expected) { 2402 } 2403 } 2404 2405 /** 2406 * @throws IOException 2407 * @tests java.util.Scanner#hasNext(Pattern) 2408 */ 2409 public void test_hasNextLPattern() throws IOException { 2410 Pattern pattern; 2411 s = new Scanner("aab@2@abb@").useDelimiter("\\@"); 2412 pattern = Pattern.compile("a*b"); 2413 assertTrue(s.hasNext(pattern)); 2414 assertEquals("aab", s.next(pattern)); 2415 assertFalse(s.hasNext(pattern)); 2416 try { 2417 s.next(pattern); 2418 fail(); 2419 } catch (InputMismatchException expected) { 2420 } 2421 2422 s = new Scanner("word ? "); 2423 pattern = Pattern.compile("\\w+"); 2424 assertTrue(s.hasNext(pattern)); 2425 assertEquals("word", s.next(pattern)); 2426 assertFalse(s.hasNext(pattern)); 2427 try { 2428 s.next(pattern); 2429 fail(); 2430 } catch (InputMismatchException expected) { 2431 } 2432 2433 s = new Scanner("word1 WorD2 "); 2434 pattern = Pattern.compile("\\w+"); 2435 assertTrue(s.hasNext(pattern)); 2436 assertEquals("word1", s.next(pattern)); 2437 assertTrue(s.hasNext(pattern)); 2438 assertEquals("WorD2", s.next(pattern)); 2439 assertFalse(s.hasNext(pattern)); 2440 try { 2441 s.next(pattern); 2442 fail(); 2443 } catch (NoSuchElementException expected) { 2444 } 2445 2446 s = new Scanner("word1 WorD2 "); 2447 pattern = Pattern.compile("\\w+"); 2448 try { 2449 s.hasNext((Pattern) null); 2450 fail(); 2451 } catch (NullPointerException expected) { 2452 } 2453 s.close(); 2454 try { 2455 s.hasNext(pattern); 2456 fail(); 2457 } catch (IllegalStateException expected) { 2458 } 2459 2460 // test socket inputStream 2461 os.write("aab b".getBytes()); 2462 serverSocket.close(); 2463 2464 s = new Scanner(client); 2465 pattern = Pattern.compile("a+b"); 2466 assertTrue(s.hasNext(pattern)); 2467 assertEquals("aab", s.next(pattern)); 2468 assertFalse(s.hasNext(pattern)); 2469 try { 2470 s.next(pattern); 2471 fail(); 2472 } catch (InputMismatchException expected) { 2473 } 2474 } 2475 2476 /** 2477 * @throws IOException 2478 * @tests java.util.Scanner#hasNext(String) 2479 */ 2480 public void test_hasNextLString() throws IOException { 2481 s = new Scanner("aab@2@abb@").useDelimiter("\\@"); 2482 try { 2483 s.hasNext((String)null); 2484 fail(); 2485 } catch (NullPointerException expected) { 2486 } 2487 2488 s = new Scanner("aab*b*").useDelimiter("\\*"); 2489 assertTrue(s.hasNext("a+b")); 2490 assertEquals("aab", s.next("a+b")); 2491 assertFalse(s.hasNext("a+b")); 2492 try { 2493 s.next("a+b"); 2494 fail(); 2495 } catch (InputMismatchException expected) { 2496 } 2497 s.close(); 2498 try { 2499 s.hasNext("a+b"); 2500 fail(); 2501 } catch (IllegalStateException expected) { 2502 } 2503 2504 s = new Scanner("WORD ? "); 2505 assertTrue(s.hasNext("\\w+")); 2506 assertEquals("WORD", s.next("\\w+")); 2507 assertFalse(s.hasNext("\\w+")); 2508 try { 2509 s.next("\\w+"); 2510 fail(); 2511 } catch (InputMismatchException expected) { 2512 } 2513 2514 s = new Scanner("word1 word2 "); 2515 assertEquals("word1", s.next("\\w+")); 2516 assertEquals("word2", s.next("\\w+")); 2517 // test boundary case 2518 try { 2519 s.next("\\w+"); 2520 fail(); 2521 } catch (NoSuchElementException expected) { 2522 } 2523 2524 // test socket inputStream 2525 2526 os.write("aab 2".getBytes()); 2527 serverSocket.close(); 2528 2529 s = new Scanner(client); 2530 assertTrue(s.hasNext("a*b")); 2531 assertEquals("aab", s.next("a*b")); 2532 assertFalse(s.hasNext("a*b")); 2533 try { 2534 s.next("a*b"); 2535 fail(); 2536 } catch (InputMismatchException expected) { 2537 } 2538 } 2539 2540 /** 2541 * @throws IOException 2542 * @tests java.util.Scanner#hasNextBoolean() 2543 */ 2544 public void test_hasNextBoolean() throws IOException { 2545 2546 s = new Scanner("TRue"); 2547 assertTrue(s.hasNextBoolean()); 2548 assertTrue(s.nextBoolean()); 2549 2550 s = new Scanner("tRue false"); 2551 assertTrue(s.hasNextBoolean()); 2552 assertTrue(s.nextBoolean()); 2553 assertTrue(s.hasNextBoolean()); 2554 assertFalse(s.nextBoolean()); 2555 2556 s = new Scanner(""); 2557 assertFalse(s.hasNextBoolean()); 2558 2559 // test socket inputStream 2560 2561 os.write("true false ".getBytes()); 2562 serverSocket.close(); 2563 2564 s = new Scanner(client); 2565 assertTrue(s.hasNextBoolean()); 2566 assertTrue(s.nextBoolean()); 2567 2568 // ues '*' as delimiter 2569 s = new Scanner("true**false").useDelimiter("\\*"); 2570 assertTrue(s.hasNextBoolean()); 2571 assertTrue(s.nextBoolean()); 2572 assertFalse(s.hasNextBoolean()); 2573 try { 2574 s.nextBoolean(); 2575 fail(); 2576 } catch (NoSuchElementException expected) { 2577 } 2578 2579 s = new Scanner("false( )").useDelimiter("\\( \\)"); 2580 assertTrue(s.hasNextBoolean()); 2581 assertFalse(s.nextBoolean()); 2582 assertFalse(s.hasNextBoolean()); 2583 2584 } 2585 2586 /** 2587 * @throws IOException 2588 * @tests java.util.Scanner#hasNextByte(int) 2589 */ 2590 public void test_hasNextByteI() throws IOException { 2591 s = new Scanner("123 126"); 2592 assertTrue(s.hasNextByte(10)); 2593 assertEquals(123, s.nextByte(10)); 2594 assertTrue(s.hasNextByte(10)); 2595 assertEquals(126, s.nextByte(10)); 2596 assertFalse(s.hasNextByte(10)); 2597 try { 2598 s.nextByte(10); 2599 fail(); 2600 } catch (NoSuchElementException expected) { 2601 } 2602 2603 // If the radix is different from 10 2604 s = new Scanner("123 126"); 2605 assertTrue(s.hasNextByte(5)); 2606 assertEquals(38, s.nextByte(5)); 2607 assertFalse(s.hasNextByte(5)); 2608 try { 2609 s.nextByte(5); 2610 fail(); 2611 } catch (InputMismatchException expected) { 2612 } 2613 2614 // If the number is out of range 2615 s = new Scanner("1234"); 2616 assertFalse(s.hasNextByte(10)); 2617 try { 2618 s.nextByte(10); 2619 fail(); 2620 } catch (InputMismatchException expected) { 2621 } 2622 2623 /* 2624 * The input string has Arabic-Indic digits. 2625 */ 2626 s = new Scanner("1\u06602 12\u0666"); 2627 assertTrue(s.hasNextByte(10)); 2628 assertEquals(102, s.nextByte(10)); 2629 assertFalse(s.hasNextByte(5)); 2630 try { 2631 s.nextByte(5); 2632 fail(); 2633 } catch (InputMismatchException expected) { 2634 } 2635 assertTrue(s.hasNextByte(10)); 2636 assertEquals(126, s.nextByte(10)); 2637 2638 s = new Scanner("012"); 2639 assertTrue(s.hasNextByte(10)); 2640 assertEquals(12, s.nextByte(10)); 2641 2642 s = new Scanner("E"); 2643 assertTrue(s.hasNextByte(16)); 2644 assertEquals(14, s.nextByte(16)); 2645 2646 /* 2647 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2648 * respectively, but they are not differentiated. 2649 */ 2650 s = new Scanner("100"); 2651 s.useLocale(Locale.CHINESE); 2652 assertTrue(s.hasNextByte(10)); 2653 assertEquals(100, s.nextByte(10)); 2654 2655 s = new Scanner("1\u0966\u0966"); 2656 s.useLocale(Locale.CHINESE); 2657 assertTrue(s.hasNextByte(10)); 2658 assertEquals(100, s.nextByte(10)); 2659 2660 s = new Scanner("1\u0e50\u0e50"); 2661 s.useLocale(Locale.CHINESE); 2662 assertTrue(s.hasNextByte(10)); 2663 assertEquals(100, s.nextByte(10)); 2664 2665 s = new Scanner("-123"); 2666 s.useLocale(new Locale("ar", "AE")); 2667 assertTrue(s.hasNextByte(10)); 2668 assertEquals(-123, s.nextByte(10)); 2669 2670 2671 s = new Scanner("-123"); 2672 s.useLocale(new Locale("mk", "MK")); 2673 assertTrue(s.hasNextByte(10)); 2674 assertEquals(-123, s.nextByte(10)); 2675 } 2676 2677 public void test_hasNextByteI_cache() throws IOException{ 2678 //regression for HARMONY-2063 2679 s = new Scanner("123 45"); 2680 assertTrue(s.hasNextByte(8)); 2681 assertEquals(83, s.nextByte()); 2682 assertEquals(45, s.nextByte()); 2683 2684 s = new Scanner("123 45"); 2685 assertTrue(s.hasNextByte(10)); 2686 assertTrue(s.hasNextByte(8)); 2687 assertEquals(83, s.nextByte()); 2688 assertEquals(45, s.nextByte()); 2689 2690 s = new Scanner("-123 -45"); 2691 assertTrue(s.hasNextByte(8)); 2692 assertEquals(-123, s.nextInt()); 2693 assertEquals(-45, s.nextByte()); 2694 2695 s = new Scanner("123 45"); 2696 assertTrue(s.hasNextByte()); 2697 s.close(); 2698 try { 2699 s.nextByte(); 2700 fail(); 2701 } catch (IllegalStateException expected) { 2702 } 2703 } 2704 2705 public void test_hasNextByte() throws IOException { 2706 s = new Scanner("123 126"); 2707 assertTrue(s.hasNextByte()); 2708 assertEquals(123, s.nextByte()); 2709 assertTrue(s.hasNextByte()); 2710 assertEquals(126, s.nextByte()); 2711 assertFalse(s.hasNextByte()); 2712 try { 2713 s.nextByte(); 2714 fail(); 2715 } catch (NoSuchElementException expected) { 2716 } 2717 2718 // If the radix is different from 10 2719 s = new Scanner("123 126"); 2720 s.useRadix(5); 2721 assertTrue(s.hasNextByte()); 2722 assertEquals(38, s.nextByte()); 2723 assertFalse(s.hasNextByte()); 2724 try { 2725 s.nextByte(); 2726 fail(); 2727 } catch (InputMismatchException expected) { 2728 } 2729 2730 // If the number is out of range 2731 s = new Scanner("1234"); 2732 assertFalse(s.hasNextByte()); 2733 try { 2734 s.nextByte(); 2735 fail(); 2736 } catch (InputMismatchException expected) { 2737 } 2738 2739 /* 2740 * The input string has Arabic-Indic digits. 2741 */ 2742 s = new Scanner("1\u06602 12\u0666"); 2743 assertTrue(s.hasNextByte()); 2744 assertEquals(102, s.nextByte()); 2745 s.useRadix(5); 2746 assertFalse(s.hasNextByte()); 2747 try { 2748 s.nextByte(); 2749 fail(); 2750 } catch (InputMismatchException expected) { 2751 } 2752 s.useRadix(10); 2753 assertTrue(s.hasNextByte()); 2754 assertEquals(126, s.nextByte()); 2755 2756 s = new Scanner("012"); 2757 assertEquals(12, s.nextByte()); 2758 2759 s = new Scanner("E"); 2760 s.useRadix(16); 2761 assertTrue(s.hasNextByte()); 2762 assertEquals(14, s.nextByte()); 2763 2764 /* 2765 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2766 * respectively, but they are not differentiated. 2767 */ 2768 s = new Scanner("100"); 2769 s.useLocale(Locale.CHINESE); 2770 assertTrue(s.hasNextByte()); 2771 assertEquals(100, s.nextByte()); 2772 2773 s = new Scanner("1\u0966\u0966"); 2774 s.useLocale(Locale.CHINESE); 2775 assertTrue(s.hasNextByte()); 2776 assertEquals(100, s.nextByte()); 2777 2778 s = new Scanner("1\u0e50\u0e50"); 2779 s.useLocale(Locale.CHINESE); 2780 assertTrue(s.hasNextByte()); 2781 assertEquals(100, s.nextByte()); 2782 2783 s = new Scanner("-123"); 2784 s.useLocale(new Locale("ar", "AE")); 2785 assertTrue(s.hasNextByte()); 2786 assertEquals(-123, s.nextByte()); 2787 2788 s = new Scanner("-123"); 2789 s.useLocale(new Locale("mk", "MK")); 2790 assertTrue(s.hasNextByte()); 2791 assertEquals(-123, s.nextByte()); 2792 } 2793 2794 /** 2795 * @throws IOException 2796 * @tests java.util.Scanner#hasNextBigInteger(int) 2797 */ 2798 public void test_hasNextBigIntegerI() throws IOException { 2799 s = new Scanner("123 456"); 2800 assertTrue(s.hasNextBigInteger(10)); 2801 assertEquals(new BigInteger("123"), s.nextBigInteger(10)); 2802 assertTrue(s.hasNextBigInteger(10)); 2803 assertEquals(new BigInteger("456"), s.nextBigInteger(10)); 2804 assertFalse(s.hasNextBigInteger(10)); 2805 try { 2806 s.nextBigInteger(10); 2807 fail(); 2808 } catch (NoSuchElementException expected) { 2809 } 2810 2811 // If the radix is different from 10 2812 s = new Scanner("123 456"); 2813 assertTrue(s.hasNextBigInteger(5)); 2814 assertEquals(new BigInteger("38"), s.nextBigInteger(5)); 2815 assertFalse(s.hasNextBigInteger(5)); 2816 try { 2817 s.nextBigInteger(5); 2818 fail(); 2819 } catch (InputMismatchException expected) { 2820 } 2821 2822 /* 2823 * Different locale can only recognize corresponding locale sensitive 2824 * string. ',' is used in many locales as group separator. 2825 */ 2826 s = new Scanner("23,456 23,456"); 2827 s.useLocale(Locale.GERMANY); 2828 assertFalse(s.hasNextBigInteger(10)); 2829 try { 2830 s.nextBigInteger(10); 2831 fail(); 2832 } catch (InputMismatchException expected) { 2833 } 2834 s.useLocale(Locale.ENGLISH); 2835 // If exception is thrown out, input will not be advanced. 2836 assertTrue(s.hasNextBigInteger(10)); 2837 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2838 assertTrue(s.hasNextBigInteger(10)); 2839 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2840 2841 /* 2842 * ''' is used in many locales as group separator. 2843 */ 2844 s = new Scanner("23'456 23'456"); 2845 s.useLocale(Locale.GERMANY); 2846 assertFalse(s.hasNextBigInteger(10)); 2847 try { 2848 s.nextBigInteger(10); 2849 fail(); 2850 } catch (InputMismatchException expected) { 2851 } 2852 s.useLocale(new Locale("it", "CH")); 2853 // If exception is thrown out, input will not be advanced. 2854 assertTrue(s.hasNextBigInteger(10)); 2855 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2856 assertTrue(s.hasNextBigInteger(10)); 2857 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2858 2859 /* 2860 * The input string has Arabic-Indic digits. 2861 */ 2862 s = new Scanner("1\u06602 1\u06662"); 2863 assertTrue(s.hasNextBigInteger(10)); 2864 assertEquals(new BigInteger("102"), s.nextBigInteger(10)); 2865 assertFalse(s.hasNextBigInteger(5)); 2866 try { 2867 s.nextBigInteger(5); 2868 fail(); 2869 } catch (InputMismatchException expected) { 2870 } 2871 assertTrue(s.hasNextBigInteger(10)); 2872 assertEquals(new BigInteger("162"), s.nextBigInteger(10)); 2873 2874 /* 2875 * '.' is used in many locales as group separator. The input string 2876 * has Arabic-Indic digits . 2877 */ 2878 s = new Scanner("23.45\u0666 23.456"); 2879 s.useLocale(Locale.CHINESE); 2880 assertFalse(s.hasNextBigInteger(10)); 2881 try { 2882 s.nextBigInteger(10); 2883 fail(); 2884 } catch (InputMismatchException expected) { 2885 } 2886 s.useLocale(Locale.GERMANY); 2887 // If exception is thrown out, input will not be advanced. 2888 assertTrue(s.hasNextBigInteger(10)); 2889 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2890 assertTrue(s.hasNextBigInteger(10)); 2891 assertEquals(new BigInteger("23456"), s.nextBigInteger(10)); 2892 2893 // The input string starts with zero 2894 s = new Scanner("03,456"); 2895 s.useLocale(Locale.ENGLISH); 2896 assertFalse(s.hasNextBigInteger(10)); 2897 try { 2898 s.nextBigInteger(10); 2899 fail(); 2900 } catch (InputMismatchException expected) { 2901 } 2902 2903 s = new Scanner("03456"); 2904 assertTrue(s.hasNextBigInteger(10)); 2905 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 2906 2907 s = new Scanner("\u06603,456"); 2908 s.useLocale(Locale.ENGLISH); 2909 assertTrue(s.hasNextBigInteger(10)); 2910 assertEquals(new BigInteger("3456"), s.nextBigInteger(10)); 2911 2912 s = new Scanner("E34"); 2913 assertTrue(s.hasNextBigInteger(16)); 2914 assertEquals(new BigInteger("3636"), s.nextBigInteger(16)); 2915 2916 /* 2917 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 2918 * respectively, but they are not differentiated. 2919 */ 2920 s = new Scanner("12300"); 2921 s.useLocale(Locale.CHINESE); 2922 assertTrue(s.hasNextBigInteger(10)); 2923 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 2924 2925 s = new Scanner("123\u0966\u0966"); 2926 s.useLocale(Locale.CHINESE); 2927 assertTrue(s.hasNextBigInteger(10)); 2928 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 2929 2930 s = new Scanner("123\u0e50\u0e50"); 2931 s.useLocale(Locale.CHINESE); 2932 assertTrue(s.hasNextBigInteger(10)); 2933 assertEquals(new BigInteger("12300"), s.nextBigInteger(10)); 2934 2935 s = new Scanner("-123"); 2936 s.useLocale(new Locale("ar", "AE")); 2937 assertTrue(s.hasNextBigInteger(10)); 2938 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 2939 2940 2941 s = new Scanner("-123"); 2942 s.useLocale(new Locale("mk", "MK")); 2943 assertTrue(s.hasNextBigInteger(10)); 2944 assertEquals(new BigInteger("-123"), s.nextBigInteger(10)); 2945 } 2946 2947 /** 2948 * @throws IOException 2949 * @tests java.util.Scanner#hasNextBigInteger(int) 2950 */ 2951 public void test_hasNextBigIntegerI_cache() throws IOException { 2952 //regression for HARMONY-2063 2953 s = new Scanner("123 123456789123456789"); 2954 assertTrue(s.hasNextBigInteger(16)); 2955 assertEquals(new BigInteger("291"), s.nextBigInteger()); 2956 assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); 2957 2958 s = new Scanner("123456789123456789 456"); 2959 assertTrue(s.hasNextBigInteger(16)); 2960 assertTrue(s.hasNextBigInteger(10)); 2961 assertEquals(new BigInteger("123456789123456789"), s.nextBigInteger()); 2962 assertEquals(new BigInteger("456"), s.nextBigInteger()); 2963 2964 s = new Scanner("-123 -123456789123456789"); 2965 assertTrue(s.hasNextBigInteger(8)); 2966 assertEquals(-123, s.nextShort()); 2967 assertEquals(new BigInteger("-123456789123456789"), s.nextBigInteger()); 2968 2969 s = new Scanner("123 456"); 2970 assertTrue(s.hasNextBigInteger()); 2971 s.close(); 2972 try { 2973 s.nextBigInteger(); 2974 fail(); 2975 } catch (IllegalStateException expected) { 2976 } 2977 } 2978 2979 /** 2980 * @throws IOException 2981 * @tests java.util.Scanner#hasNextBigInteger() 2982 */ 2983 public void test_hasNextBigInteger() throws IOException { 2984 s = new Scanner("123 456"); 2985 assertTrue(s.hasNextBigInteger()); 2986 assertEquals(new BigInteger("123"), s.nextBigInteger()); 2987 assertTrue(s.hasNextBigInteger()); 2988 assertEquals(new BigInteger("456"), s.nextBigInteger()); 2989 assertFalse(s.hasNextBigInteger()); 2990 try { 2991 s.nextBigInteger(); 2992 fail(); 2993 } catch (NoSuchElementException expected) { 2994 } 2995 2996 // If the radix is different from 10 2997 s = new Scanner("123 456"); 2998 s.useRadix(5); 2999 assertTrue(s.hasNextBigInteger()); 3000 assertEquals(new BigInteger("38"), s.nextBigInteger()); 3001 assertFalse(s.hasNextBigInteger()); 3002 try { 3003 s.nextBigInteger(); 3004 fail(); 3005 } catch (InputMismatchException expected) { 3006 } 3007 3008 /* 3009 * Different locale can only recognize corresponding locale sensitive 3010 * string. ',' is used in many locales as group separator. 3011 */ 3012 s = new Scanner("23,456 23,456"); 3013 s.useLocale(Locale.GERMANY); 3014 assertFalse(s.hasNextBigInteger()); 3015 try { 3016 s.nextBigInteger(); 3017 fail(); 3018 } catch (InputMismatchException expected) { 3019 } 3020 s.useLocale(Locale.ENGLISH); 3021 // If exception is thrown out, input will not be advanced. 3022 assertTrue(s.hasNextBigInteger()); 3023 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3024 assertTrue(s.hasNextBigInteger()); 3025 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3026 3027 /* 3028 * ''' is used in many locales as group separator. 3029 */ 3030 s = new Scanner("23'456 23'456"); 3031 s.useLocale(Locale.GERMANY); 3032 assertFalse(s.hasNextBigInteger()); 3033 try { 3034 s.nextBigInteger(); 3035 fail(); 3036 } catch (InputMismatchException expected) { 3037 } 3038 s.useLocale(new Locale("it", "CH")); 3039 // If exception is thrown out, input will not be advanced. 3040 assertTrue(s.hasNextBigInteger()); 3041 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3042 assertTrue(s.hasNextBigInteger()); 3043 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3044 3045 /* 3046 * The input string has Arabic-Indic digits. 3047 */ 3048 s = new Scanner("1\u06602 1\u06662"); 3049 assertEquals(new BigInteger("102"), s.nextBigInteger()); 3050 s.useRadix(5); 3051 assertFalse(s.hasNextBigInteger()); 3052 try { 3053 s.nextBigInteger(); 3054 fail(); 3055 } catch (InputMismatchException expected) { 3056 } 3057 s.useRadix(10); 3058 assertTrue(s.hasNextBigInteger()); 3059 assertEquals(new BigInteger("162"), s.nextBigInteger()); 3060 3061 /* 3062 * '.' is used in many locales as group separator. The input string 3063 * has Arabic-Indic digits . 3064 */ 3065 s = new Scanner("23.45\u0666 23.456"); 3066 s.useLocale(Locale.CHINESE); 3067 assertFalse(s.hasNextBigInteger()); 3068 try { 3069 s.nextBigInteger(); 3070 fail(); 3071 } catch (InputMismatchException expected) { 3072 } 3073 s.useLocale(Locale.GERMANY); 3074 // If exception is thrown out, input will not be advanced. 3075 assertTrue(s.hasNextBigInteger()); 3076 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3077 assertTrue(s.hasNextBigInteger()); 3078 assertEquals(new BigInteger("23456"), s.nextBigInteger()); 3079 3080 // The input string starts with zero 3081 s = new Scanner("03,456"); 3082 s.useLocale(Locale.ENGLISH); 3083 assertFalse(s.hasNextBigInteger()); 3084 try { 3085 s.nextBigInteger(); 3086 fail(); 3087 } catch (InputMismatchException expected) { 3088 } 3089 3090 s = new Scanner("03456"); 3091 assertTrue(s.hasNextBigInteger()); 3092 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 3093 3094 s = new Scanner("\u06603,456"); 3095 s.useLocale(Locale.ENGLISH); 3096 assertTrue(s.hasNextBigInteger()); 3097 assertEquals(new BigInteger("3456"), s.nextBigInteger()); 3098 3099 s = new Scanner("E34"); 3100 s.useRadix(16); 3101 assertTrue(s.hasNextBigInteger()); 3102 assertEquals(new BigInteger("3636"), s.nextBigInteger()); 3103 3104 /* 3105 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3106 * respectively, but they are not differentiated. 3107 */ 3108 s = new Scanner("12300"); 3109 s.useLocale(Locale.CHINESE); 3110 assertTrue(s.hasNextBigInteger()); 3111 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3112 3113 s = new Scanner("123\u0966\u0966"); 3114 s.useLocale(Locale.CHINESE); 3115 assertTrue(s.hasNextBigInteger()); 3116 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3117 3118 s = new Scanner("123\u0e50\u0e50"); 3119 s.useLocale(Locale.CHINESE); 3120 assertTrue(s.hasNextBigInteger()); 3121 assertEquals(new BigInteger("12300"), s.nextBigInteger()); 3122 3123 s = new Scanner("-123"); 3124 s.useLocale(new Locale("ar", "AE")); 3125 assertTrue(s.hasNextBigInteger()); 3126 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 3127 3128 s = new Scanner("-123"); 3129 s.useLocale(new Locale("mk", "MK")); 3130 assertTrue(s.hasNextBigInteger()); 3131 assertEquals(new BigInteger("-123"), s.nextBigInteger()); 3132 } 3133 3134 /** 3135 * @throws IOException 3136 * @tests java.util.Scanner#hasNextInt(int) 3137 */ 3138 public void test_hasNextIntI() throws IOException { 3139 s = new Scanner("123 456"); 3140 assertEquals(123, s.nextInt(10)); 3141 assertTrue(s.hasNextInt(10)); 3142 assertEquals(456, s.nextInt(10)); 3143 assertFalse(s.hasNextInt(10)); 3144 try { 3145 s.nextInt(10); 3146 fail(); 3147 } catch (NoSuchElementException expected) { 3148 } 3149 3150 // If the radix is different from 10 3151 s = new Scanner("123 456"); 3152 assertTrue(s.hasNextInt(5)); 3153 assertEquals(38, s.nextInt(5)); 3154 assertFalse(s.hasNextInt(5)); 3155 try { 3156 s.nextInt(5); 3157 fail(); 3158 } catch (InputMismatchException expected) { 3159 } 3160 3161 // If the number is out of range 3162 s = new Scanner("123456789123456789123456789123456789"); 3163 assertFalse(s.hasNextInt(10)); 3164 3165 /* 3166 * Different locale can only recognize corresponding locale sensitive 3167 * string. ',' is used in many locales as group separator. 3168 */ 3169 s = new Scanner("23,456"); 3170 s.useLocale(Locale.GERMANY); 3171 assertFalse(s.hasNextInt(10)); 3172 s.useLocale(Locale.ENGLISH); 3173 // If exception is thrown out, input will not be advanced. 3174 assertTrue(s.hasNextInt(10)); 3175 /* 3176 * ''' is used in many locales as group separator. 3177 */ 3178 s = new Scanner("23'456"); 3179 s.useLocale(Locale.GERMANY); 3180 assertFalse(s.hasNextInt(10)); 3181 s.useLocale(new Locale("it", "CH")); 3182 // If exception is thrown out, input will not be advanced. 3183 assertTrue(s.hasNextInt(10)); 3184 3185 /* 3186 * The input string has Arabic-Indic digits. 3187 */ 3188 s = new Scanner("1\u06662"); 3189 assertTrue(s.hasNextInt(10)); 3190 assertFalse(s.hasNextInt(5)); 3191 3192 /* 3193 * '.' is used in many locales as group separator. The input string 3194 * has Arabic-Indic digits . 3195 */ 3196 s = new Scanner("23.45\u0666"); 3197 s.useLocale(Locale.CHINESE); 3198 assertFalse(s.hasNextInt(10)); 3199 try { 3200 s.nextInt(10); 3201 fail(); 3202 } catch (InputMismatchException expected) { 3203 } 3204 s.useLocale(Locale.GERMANY); 3205 assertTrue(s.hasNextInt(10)); 3206 3207 // The input string starts with zero 3208 s = new Scanner("03,456"); 3209 s.useLocale(Locale.ENGLISH); 3210 assertFalse(s.hasNextInt(10)); 3211 try { 3212 s.nextInt(10); 3213 fail(); 3214 } catch (InputMismatchException expected) { 3215 } 3216 3217 s = new Scanner("03456"); 3218 assertTrue(s.hasNextInt(10)); 3219 assertEquals(3456, s.nextInt(10)); 3220 3221 s = new Scanner("\u06603,456"); 3222 s.useLocale(Locale.ENGLISH); 3223 assertTrue(s.hasNextInt(10)); 3224 assertEquals(3456, s.nextInt(10)); 3225 3226 s = new Scanner("E3456"); 3227 assertTrue(s.hasNextInt(16)); 3228 assertEquals(930902, s.nextInt(16)); 3229 // The following test case fails on RI, because RI does not support 3230 // letter as leading digit 3231 s = new Scanner("E3,456"); 3232 s.useLocale(Locale.ENGLISH); 3233 assertTrue(s.hasNextInt(16)); 3234 assertEquals(930902, s.nextInt(16)); 3235 3236 // If parameter radix is illegal, the following test case fails on RI 3237 try { 3238 s.hasNextInt(Character.MIN_RADIX - 1); 3239 fail(); 3240 } catch (IllegalArgumentException expected) { 3241 } 3242 3243 /* 3244 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3245 * respectively, but they are not differentiated. 3246 */ 3247 s = new Scanner("12300"); 3248 s.useLocale(Locale.CHINESE); 3249 assertTrue(s.hasNextInt(10)); 3250 assertEquals(12300, s.nextInt(10)); 3251 3252 s = new Scanner("123\u0966\u0966"); 3253 s.useLocale(Locale.CHINESE); 3254 assertTrue(s.hasNextInt(10)); 3255 assertEquals(12300, s.nextInt(10)); 3256 3257 s = new Scanner("123\u0e50\u0e50"); 3258 s.useLocale(Locale.CHINESE); 3259 assertTrue(s.hasNextInt(10)); 3260 assertEquals(12300, s.nextInt(10)); 3261 3262 /* 3263 * There are three types of negative prefix all in all. '' '-' '(' There 3264 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 3265 * must be used together. Prefix '-' and suffix '-' must be used 3266 * exclusively. 3267 */ 3268 3269 /* 3270 * According to Integer regular expression: Integer :: = ( [-+]? (* 3271 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 3272 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 3273 * recognized by scanner with locale ar_AE, (123) should be recognized 3274 * by scanner with locale mk_MK. But this is not the case on RI. 3275 */ 3276 s = new Scanner("-123 123- -123-"); 3277 s.useLocale(new Locale("ar", "AE")); 3278 assertTrue(s.hasNextInt(10)); 3279 assertEquals(-123, s.nextInt(10)); 3280 // The following test case fails on RI 3281 assertTrue(s.hasNextInt(10)); 3282 assertEquals(-123, s.nextInt(10)); 3283 assertFalse(s.hasNextInt(10)); 3284 try { 3285 s.nextInt(10); 3286 fail(); 3287 } catch (InputMismatchException expected) { 3288 } 3289 3290 s = new Scanner("-123 123-"); 3291 s.useLocale(new Locale("mk", "MK")); 3292 assertTrue(s.hasNextInt(10)); 3293 assertEquals(-123, s.nextInt(10)); 3294 assertFalse(s.hasNextInt(10)); 3295 try { 3296 s.nextInt(); 3297 fail(); 3298 } catch (InputMismatchException expected) { 3299 } 3300 // Skip the un-recognizable token 123-. 3301 assertEquals("123-", s.next()); 3302 } 3303 3304 /** 3305 * @throws IOException 3306 * @tests java.util.Scanner#hasNextInt(int) 3307 */ 3308 public void test_hasNextIntI_cache() throws IOException { 3309 //regression for HARMONY-2063 3310 s = new Scanner("123 456"); 3311 assertTrue(s.hasNextInt(16)); 3312 assertEquals(291, s.nextInt(10)); 3313 assertEquals(456, s.nextInt()); 3314 3315 s = new Scanner("123 456"); 3316 assertTrue(s.hasNextInt(16)); 3317 assertTrue(s.hasNextInt(8)); 3318 assertEquals(83, s.nextInt()); 3319 assertEquals(456, s.nextInt()); 3320 3321 s = new Scanner("-123 -456 -789"); 3322 assertTrue(s.hasNextInt(8)); 3323 assertEquals(-123, s.nextShort()); 3324 assertEquals(-456, s.nextInt()); 3325 assertTrue(s.hasNextShort(16)); 3326 assertEquals(-789, s.nextInt()); 3327 3328 s = new Scanner("123 456"); 3329 assertTrue(s.hasNextInt()); 3330 s.close(); 3331 try { 3332 s.nextInt(); 3333 fail(); 3334 } catch (IllegalStateException expected) { 3335 } 3336 } 3337 /** 3338 * @throws IOException 3339 * @tests java.util.Scanner#hasNextInt() 3340 */ 3341 public void test_hasNextInt() throws IOException { 3342 s = new Scanner("123 456"); 3343 assertTrue(s.hasNextInt()); 3344 assertEquals(123, s.nextInt()); 3345 assertEquals(456, s.nextInt()); 3346 assertFalse(s.hasNextInt()); 3347 try { 3348 s.nextInt(); 3349 fail(); 3350 } catch (NoSuchElementException expected) { 3351 } 3352 3353 // If the radix is different from 10 3354 s = new Scanner("123 456"); 3355 s.useRadix(5); 3356 assertTrue(s.hasNextInt()); 3357 assertEquals(38, s.nextInt()); 3358 assertFalse(s.hasNextInt()); 3359 try { 3360 s.nextInt(); 3361 fail(); 3362 } catch (InputMismatchException expected) { 3363 } 3364 3365 // If the number is out of range 3366 s = new Scanner("123456789123456789123456789123456789"); 3367 assertFalse(s.hasNextInt()); 3368 3369 /* 3370 * Different locale can only recognize corresponding locale sensitive 3371 * string. ',' is used in many locales as group separator. 3372 */ 3373 s = new Scanner("23,456"); 3374 s.useLocale(Locale.GERMANY); 3375 assertFalse(s.hasNextInt()); 3376 s.useLocale(Locale.ENGLISH); 3377 assertTrue(s.hasNextInt()); 3378 3379 /* 3380 * ''' is used in many locales as group separator. 3381 */ 3382 s = new Scanner("23'456"); 3383 s.useLocale(Locale.GERMANY); 3384 assertFalse(s.hasNextInt()); 3385 s.useLocale(new Locale("it", "CH")); 3386 assertTrue(s.hasNextInt()); 3387 3388 /* 3389 * The input string has Arabic-Indic digits. 3390 */ 3391 s = new Scanner("1\u06662"); 3392 s.useRadix(5); 3393 assertFalse(s.hasNextInt()); 3394 3395 /* 3396 * '.' is used in many locales as group separator. The input string 3397 * has Arabic-Indic digits . 3398 */ 3399 s = new Scanner("23.45\u0666"); 3400 s.useLocale(Locale.CHINESE); 3401 assertFalse(s.hasNextInt()); 3402 s.useLocale(Locale.GERMANY); 3403 assertTrue(s.hasNextInt()); 3404 3405 // The input string starts with zero 3406 s = new Scanner("03,456"); 3407 s.useLocale(Locale.ENGLISH); 3408 assertFalse(s.hasNextInt()); 3409 try { 3410 s.nextInt(); 3411 fail(); 3412 } catch (InputMismatchException expected) { 3413 } 3414 3415 s = new Scanner("03456"); 3416 assertTrue(s.hasNextInt()); 3417 assertEquals(3456, s.nextInt()); 3418 3419 s = new Scanner("\u06603,456"); 3420 s.useLocale(Locale.ENGLISH); 3421 assertEquals(3456, s.nextInt()); 3422 3423 s = new Scanner("E3456"); 3424 s.useRadix(16); 3425 assertTrue(s.hasNextInt()); 3426 assertEquals(930902, s.nextInt()); 3427 3428 // The following test case fails on RI, because RI does not support 3429 // letter as leading digit 3430 s = new Scanner("E3,456"); 3431 s.useLocale(Locale.ENGLISH); 3432 s.useRadix(16); 3433 assertTrue(s.hasNextInt()); 3434 assertEquals(930902, s.nextInt()); 3435 3436 /* 3437 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3438 * respectively, but they are not differentiated. 3439 */ 3440 s = new Scanner("12300"); 3441 s.useLocale(Locale.CHINESE); 3442 assertTrue(s.hasNextInt()); 3443 assertEquals(12300, s.nextInt()); 3444 3445 s = new Scanner("123\u0966\u0966"); 3446 s.useLocale(Locale.CHINESE); 3447 assertTrue(s.hasNextInt()); 3448 assertEquals(12300, s.nextInt()); 3449 3450 s = new Scanner("123\u0e50\u0e50"); 3451 s.useLocale(Locale.CHINESE); 3452 assertTrue(s.hasNextInt()); 3453 assertEquals(12300, s.nextInt()); 3454 3455 /* 3456 * There are three types of negative prefix all in all. '' '-' '(' There 3457 * are three types of negative suffix all in all. '' '-' ')' '(' and ')' 3458 * must be used togethor. Prefix '-' and suffix '-' must be used 3459 * exclusively. 3460 */ 3461 3462 /* 3463 * According to Integer regular expression: Integer :: = ( [-+]? (* 3464 * Numeral ) ) | LocalPositivePrefix Numeral LocalPositiveSuffix | 3465 * LocalNegativePrefix Numeral LocalNegativeSuffix 123- should be 3466 * recognized by scanner with locale ar_AE, (123) shouble be recognized 3467 * by scanner with locale mk_MK. But this is not the case on RI. 3468 */ 3469 s = new Scanner("-123 123- -123-"); 3470 s.useLocale(new Locale("ar", "AE")); 3471 assertTrue(s.hasNextInt()); 3472 assertEquals(-123, s.nextInt()); 3473 // The following test case fails on RI 3474 assertTrue(s.hasNextInt()); 3475 assertEquals(-123, s.nextInt()); 3476 assertFalse(s.hasNextInt()); 3477 try { 3478 s.nextInt(); 3479 fail(); 3480 } catch (InputMismatchException expected) { 3481 } 3482 3483 s = new Scanner("-123 123-"); 3484 s.useLocale(new Locale("mk", "MK")); 3485 assertTrue(s.hasNextInt()); 3486 assertEquals(-123, s.nextInt()); 3487 try { 3488 s.nextInt(); 3489 fail(); 3490 } catch (InputMismatchException expected) { 3491 } 3492 // Skip the un-recognizable token 123-. 3493 assertEquals("123-", s.next()); 3494 } 3495 3496 /** 3497 * @throws IOException 3498 * @tests java.util.Scanner#hasNextFloat() 3499 */ 3500 public void test_hasNextFloat() throws IOException { 3501 s = new Scanner("123 45\u0666. 123.4 .123 "); 3502 s.useLocale(Locale.ENGLISH); 3503 assertTrue(s.hasNextFloat()); 3504 assertEquals((float)123.0, s.nextFloat()); 3505 assertTrue(s.hasNextFloat()); 3506 assertEquals((float)456.0, s.nextFloat()); 3507 assertTrue(s.hasNextFloat()); 3508 assertEquals((float)123.4, s.nextFloat()); 3509 assertTrue(s.hasNextFloat()); 3510 assertEquals((float)0.123, s.nextFloat()); 3511 assertFalse(s.hasNextFloat()); 3512 try { 3513 s.nextFloat(); 3514 fail(); 3515 } catch (NoSuchElementException expected) { 3516 } 3517 3518 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 3519 s.useLocale(Locale.ENGLISH); 3520 assertTrue(s.hasNextFloat()); 3521 assertEquals((float)123.4, s.nextFloat()); 3522 assertTrue(s.hasNextFloat()); 3523 assertEquals((float)-456.7, s.nextFloat()); 3524 assertTrue(s.hasNextFloat()); 3525 assertEquals((float)123456.789, s.nextFloat()); 3526 assertFalse(s.hasNextFloat()); 3527 try { 3528 s.nextFloat(); 3529 fail(); 3530 } catch (InputMismatchException expected) { 3531 } 3532 3533 // Scientific notation 3534 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 3535 s.useLocale(Locale.ENGLISH); 3536 assertTrue(s.hasNextFloat()); 3537 assertEquals((float)1.234E12, s.nextFloat()); 3538 assertTrue(s.hasNextFloat()); 3539 assertEquals((float)-4.567E14, s.nextFloat()); 3540 assertTrue(s.hasNextFloat()); 3541 assertEquals((float)1.23456789E-5, s.nextFloat()); 3542 3543 s = new Scanner("NaN Infinity -Infinity"); 3544 assertTrue(s.hasNextFloat()); 3545 assertEquals(Float.NaN, s.nextFloat()); 3546 assertTrue(s.hasNextFloat()); 3547 assertEquals(Float.POSITIVE_INFINITY, s.nextFloat()); 3548 assertTrue(s.hasNextFloat()); 3549 assertEquals(Float.NEGATIVE_INFINITY, s.nextFloat()); 3550 3551 String str=String.valueOf(Float.MAX_VALUE*2); 3552 s=new Scanner(str); 3553 assertTrue(s.hasNextFloat()); 3554 assertEquals(Float.POSITIVE_INFINITY,s.nextFloat()); 3555 3556 /* 3557 * Different locale can only recognize corresponding locale sensitive 3558 * string. ',' is used in many locales as group separator. 3559 */ 3560 s = new Scanner("23,456 23,456"); 3561 s.useLocale(Locale.ENGLISH); 3562 assertTrue(s.hasNextFloat()); 3563 assertEquals((float)23456.0, s.nextFloat()); 3564 s.useLocale(Locale.GERMANY); 3565 assertTrue(s.hasNextFloat()); 3566 assertEquals((float)23.456, s.nextFloat()); 3567 3568 s = new Scanner("23.456 23.456"); 3569 s.useLocale(Locale.ENGLISH); 3570 assertTrue(s.hasNextFloat()); 3571 assertEquals((float)23.456, s.nextFloat()); 3572 s.useLocale(Locale.GERMANY); 3573 assertTrue(s.hasNextFloat()); 3574 assertEquals((float)23456.0, s.nextFloat()); 3575 3576 s = new Scanner("23,456.7 23.456,7"); 3577 s.useLocale(Locale.ENGLISH); 3578 assertTrue(s.hasNextFloat()); 3579 assertEquals((float)23456.7, s.nextFloat()); 3580 s.useLocale(Locale.GERMANY); 3581 assertTrue(s.hasNextFloat()); 3582 assertEquals((float)23456.7, s.nextFloat()); 3583 3584 //FIXME 3585// s = new Scanner("-123.4 123.4- -123.4-"); 3586// s.useLocale(new Locale("ar", "AE")); 3587// assertTrue(s.hasNextFloat()); 3588// assertEquals((float)-123.4, s.nextFloat()); 3589// //The following test case fails on RI 3590// assertTrue(s.hasNextFloat()); 3591// assertEquals((float)-123.4, s.nextFloat()); 3592// try { 3593// s.nextFloat(); 3594// fail(); 3595// } catch (InputMismatchException expected) { 3596// } 3597 3598 s = new Scanner("123- -123"); 3599 s.useLocale(new Locale("mk", "MK")); 3600 assertFalse(s.hasNextFloat()); 3601 try { 3602 s.nextFloat(); 3603 fail(); 3604 } catch (InputMismatchException expected) { 3605 } 3606 // Skip the un-recognizable token 123-. 3607 assertEquals("123-", s.next()); 3608 assertTrue(s.hasNextFloat()); 3609 assertEquals((float)-123.0, s.nextFloat()); 3610 3611 s = new Scanner("+123.4 -456.7"); 3612 s.useLocale(Locale.ENGLISH); 3613 assertTrue(s.hasNextFloat()); 3614 s.close(); 3615 try{ 3616 s.nextFloat(); 3617 fail(); 3618 }catch(IllegalStateException expected) { 3619 } 3620 3621 } 3622 3623 /** 3624 * @throws IOException 3625 * @tests java.util.Scanner#hasNextShort(int) 3626 */ 3627 public void test_hasNextShortI() throws IOException { 3628 s = new Scanner("123 456"); 3629 assertTrue(s.hasNextShort(10)); 3630 assertEquals(123, s.nextShort(10)); 3631 assertTrue(s.hasNextShort(10)); 3632 assertEquals(456, s.nextShort(10)); 3633 assertFalse(s.hasNextShort(10)); 3634 try { 3635 s.nextShort(10); 3636 fail(); 3637 } catch (NoSuchElementException expected) { 3638 } 3639 3640 // If the radix is different from 10 3641 s = new Scanner("123 456"); 3642 assertTrue(s.hasNextShort(5)); 3643 assertEquals(38, s.nextShort(5)); 3644 assertFalse(s.hasNextShort(5)); 3645 try { 3646 s.nextShort(5); 3647 fail(); 3648 } catch (InputMismatchException expected) { 3649 } 3650 3651 // If the number is out of range 3652 s = new Scanner("123456789"); 3653 assertFalse(s.hasNextShort(10)); 3654 try { 3655 s.nextShort(10); 3656 fail(); 3657 } catch (InputMismatchException expected) { 3658 } 3659 3660 /* 3661 * Different locale can only recognize corresponding locale sensitive 3662 * string. ',' is used in many locales as group separator. 3663 */ 3664 s = new Scanner("23,456 23,456"); 3665 s.useLocale(Locale.GERMANY); 3666 assertFalse(s.hasNextShort(10)); 3667 try { 3668 s.nextShort(10); 3669 fail(); 3670 } catch (InputMismatchException expected) { 3671 } 3672 s.useLocale(Locale.ENGLISH); 3673 // If exception is thrown out, input will not be advanced. 3674 assertTrue(s.hasNextShort(10)); 3675 assertEquals(23456, s.nextInt(10)); 3676 assertTrue(s.hasNextShort(10)); 3677 assertEquals(23456, s.nextInt(10)); 3678 3679 /* 3680 * ''' is used in many locales as group separator. 3681 */ 3682 s = new Scanner("23'456 23'456"); 3683 s.useLocale(Locale.GERMANY); 3684 assertFalse(s.hasNextShort(10)); 3685 try { 3686 s.nextShort(10); 3687 fail(); 3688 } catch (InputMismatchException expected) { 3689 } 3690 s.useLocale(new Locale("it", "CH")); 3691 // If exception is thrown out, input will not be advanced. 3692 assertTrue(s.hasNextShort(10)); 3693 assertEquals(23456, s.nextShort(10)); 3694 assertTrue(s.hasNextShort(10)); 3695 assertEquals(23456, s.nextShort(10)); 3696 3697 /* 3698 * The input string has Arabic-Indic digits. 3699 */ 3700 s = new Scanner("1\u06602 1\u06662"); 3701 assertTrue(s.hasNextShort(10)); 3702 assertEquals(102, s.nextShort(10)); 3703 assertFalse(s.hasNextShort(5)); 3704 try { 3705 s.nextShort(5); 3706 fail(); 3707 } catch (InputMismatchException expected) { 3708 } 3709 assertTrue(s.hasNextShort(10)); 3710 assertEquals(162, s.nextShort(10)); 3711 3712 /* 3713 * '.' is used in many locales as group separator. The input string 3714 * has Arabic-Indic digits . 3715 */ 3716 s = new Scanner("23.45\u0666 23.456"); 3717 s.useLocale(Locale.CHINESE); 3718 assertFalse(s.hasNextShort(10)); 3719 try { 3720 s.nextShort(10); 3721 fail(); 3722 } catch (InputMismatchException expected) { 3723 } 3724 s.useLocale(Locale.GERMANY); 3725 // If exception is thrown out, input will not be advanced. 3726 assertTrue(s.hasNextShort(10)); 3727 assertEquals(23456, s.nextShort(10)); 3728 assertTrue(s.hasNextShort(10)); 3729 assertEquals(23456, s.nextShort(10)); 3730 3731 // The input string starts with zero 3732 s = new Scanner("03,456"); 3733 s.useLocale(Locale.ENGLISH); 3734 assertFalse(s.hasNextShort(10)); 3735 try { 3736 s.nextShort(10); 3737 fail(); 3738 } catch (InputMismatchException expected) { 3739 } 3740 3741 s = new Scanner("03456"); 3742 assertTrue(s.hasNextShort(10)); 3743 assertEquals(3456, s.nextShort(10)); 3744 3745 s = new Scanner("\u06603,456"); 3746 s.useLocale(Locale.ENGLISH); 3747 assertTrue(s.hasNextShort(10)); 3748 assertEquals(3456, s.nextShort(10)); 3749 3750 s = new Scanner("E34"); 3751 assertTrue(s.hasNextShort(16)); 3752 assertEquals(3636, s.nextShort(16)); 3753 3754 /* 3755 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3756 * respectively, but they are not differentiated. 3757 */ 3758 s = new Scanner("12300"); 3759 s.useLocale(Locale.CHINESE); 3760 assertTrue(s.hasNextShort(10)); 3761 assertEquals(12300, s.nextShort(10)); 3762 3763 s = new Scanner("123\u0966\u0966"); 3764 s.useLocale(Locale.CHINESE); 3765 assertTrue(s.hasNextShort(10)); 3766 assertEquals(12300, s.nextShort(10)); 3767 3768 s = new Scanner("123\u0e50\u0e50"); 3769 s.useLocale(Locale.CHINESE); 3770 assertTrue(s.hasNextShort(10)); 3771 assertEquals(12300, s.nextShort(10)); 3772 3773 s = new Scanner("-123"); 3774 s.useLocale(new Locale("ar", "AE")); 3775 assertTrue(s.hasNextShort(10)); 3776 assertEquals(-123, s.nextShort(10)); 3777 3778 3779 s = new Scanner("-123"); 3780 s.useLocale(new Locale("mk", "MK")); 3781 assertTrue(s.hasNextShort(10)); 3782 assertEquals(-123, s.nextShort(10)); 3783 } 3784 3785 /** 3786 * @throws IOException 3787 * @tests java.util.Scanner#hasNextShort() 3788 */ 3789 public void test_hasNextShort() throws IOException { 3790 s = new Scanner("123 456"); 3791 assertTrue(s.hasNextShort()); 3792 assertEquals(123, s.nextShort()); 3793 assertTrue(s.hasNextShort()); 3794 assertEquals(456, s.nextShort()); 3795 assertFalse(s.hasNextShort()); 3796 try { 3797 s.nextShort(); 3798 fail(); 3799 } catch (NoSuchElementException expected) { 3800 } 3801 3802 // If the radix is different from 10 3803 s = new Scanner("123 456"); 3804 s.useRadix(5); 3805 assertTrue(s.hasNextShort()); 3806 assertEquals(38, s.nextShort()); 3807 assertFalse(s.hasNextShort()); 3808 try { 3809 s.nextShort(); 3810 fail(); 3811 } catch (InputMismatchException expected) { 3812 } 3813 3814 // If the number is out of range 3815 s = new Scanner("123456789"); 3816 assertFalse(s.hasNextShort()); 3817 try { 3818 s.nextShort(); 3819 fail(); 3820 } catch (InputMismatchException expected) { 3821 } 3822 3823 /* 3824 * Different locale can only recognize corresponding locale sensitive 3825 * string. ',' is used in many locales as group separator. 3826 */ 3827 s = new Scanner("23,456 23,456"); 3828 s.useLocale(Locale.GERMANY); 3829 assertFalse(s.hasNextShort()); 3830 try { 3831 s.nextShort(); 3832 fail(); 3833 } catch (InputMismatchException expected) { 3834 } 3835 s.useLocale(Locale.ENGLISH); 3836 // If exception is thrown out, input will not be advanced. 3837 assertTrue(s.hasNextShort()); 3838 assertEquals(23456, s.nextShort()); 3839 assertTrue(s.hasNextShort()); 3840 assertEquals(23456, s.nextShort()); 3841 3842 /* 3843 * ''' is used in many locales as group separator. 3844 */ 3845 s = new Scanner("23'456 23'456"); 3846 s.useLocale(Locale.GERMANY); 3847 assertFalse(s.hasNextShort()); 3848 try { 3849 s.nextShort(); 3850 fail(); 3851 } catch (InputMismatchException expected) { 3852 } 3853 s.useLocale(new Locale("it", "CH")); 3854 // If exception is thrown out, input will not be advanced. 3855 assertTrue(s.hasNextShort()); 3856 assertEquals(23456, s.nextShort()); 3857 assertTrue(s.hasNextShort()); 3858 assertEquals(23456, s.nextShort()); 3859 3860 /* 3861 * The input string has Arabic-Indic digits. 3862 */ 3863 s = new Scanner("1\u06602 1\u06662"); 3864 assertEquals(102, s.nextShort()); 3865 s.useRadix(5); 3866 assertFalse(s.hasNextShort()); 3867 try { 3868 s.nextShort(); 3869 fail(); 3870 } catch (InputMismatchException expected) { 3871 } 3872 s.useRadix(10); 3873 assertTrue(s.hasNextShort()); 3874 assertEquals(162, s.nextShort()); 3875 3876 /* 3877 * '.' is used in many locales as group separator. The input string 3878 * has Arabic-Indic digits . 3879 */ 3880 s = new Scanner("23.45\u0666 23.456"); 3881 s.useLocale(Locale.CHINESE); 3882 assertFalse(s.hasNextShort()); 3883 try { 3884 s.nextShort(); 3885 fail(); 3886 } catch (InputMismatchException expected) { 3887 } 3888 s.useLocale(Locale.GERMANY); 3889 // If exception is thrown out, input will not be advanced. 3890 assertTrue(s.hasNextShort()); 3891 assertEquals(23456, s.nextShort()); 3892 assertTrue(s.hasNextShort()); 3893 assertEquals(23456, s.nextShort()); 3894 3895 // The input string starts with zero 3896 s = new Scanner("03,456"); 3897 s.useLocale(Locale.ENGLISH); 3898 assertFalse(s.hasNextShort()); 3899 try { 3900 s.nextShort(); 3901 fail(); 3902 } catch (InputMismatchException expected) { 3903 } 3904 3905 s = new Scanner("03456"); 3906 assertTrue(s.hasNextShort()); 3907 assertEquals(3456, s.nextShort()); 3908 3909 s = new Scanner("\u06603,456"); 3910 s.useLocale(Locale.ENGLISH); 3911 assertTrue(s.hasNextShort()); 3912 assertEquals(3456, s.nextShort()); 3913 3914 s = new Scanner("E34"); 3915 s.useRadix(16); 3916 assertTrue(s.hasNextShort()); 3917 assertEquals(3636, s.nextShort()); 3918 3919 /* 3920 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 3921 * respectively, but they are not differentiated. 3922 */ 3923 s = new Scanner("12300"); 3924 s.useLocale(Locale.CHINESE); 3925 assertTrue(s.hasNextShort()); 3926 assertEquals(12300, s.nextShort()); 3927 3928 s = new Scanner("123\u0966\u0966"); 3929 s.useLocale(Locale.CHINESE); 3930 assertTrue(s.hasNextShort()); 3931 assertEquals(12300, s.nextShort()); 3932 3933 s = new Scanner("123\u0e50\u0e50"); 3934 s.useLocale(Locale.CHINESE); 3935 assertTrue(s.hasNextShort()); 3936 assertEquals(12300, s.nextShort()); 3937 3938 s = new Scanner("-123"); 3939 s.useLocale(new Locale("ar", "AE")); 3940 assertTrue(s.hasNextShort()); 3941 assertEquals(-123, s.nextShort()); 3942 3943 s = new Scanner("-123"); 3944 s.useLocale(new Locale("mk", "MK")); 3945 assertTrue(s.hasNextShort()); 3946 assertEquals(-123, s.nextShort()); 3947 } 3948 3949 /** 3950 * @throws IOException 3951 * @tests java.util.Scanner#hasNextShort(int) 3952 */ 3953 public void test_hasNextShortI_cache() throws IOException { 3954 //regression for HARMONY-2063 3955 s = new Scanner("123 456"); 3956 assertTrue(s.hasNextShort(16)); 3957 assertEquals(291, s.nextShort()); 3958 assertEquals(456, s.nextShort()); 3959 3960 s = new Scanner("123 456"); 3961 assertTrue(s.hasNextShort(16)); 3962 assertTrue(s.hasNextShort(8)); 3963 assertEquals(83, s.nextShort()); 3964 assertEquals(456, s.nextShort()); 3965 3966 s = new Scanner("-123 -456 -789"); 3967 assertTrue(s.hasNextShort(8)); 3968 assertEquals(-123, s.nextInt()); 3969 assertEquals(-456, s.nextShort()); 3970 assertTrue(s.hasNextInt(16)); 3971 assertEquals(-789, s.nextShort()); 3972 3973 s = new Scanner("123 456"); 3974 assertTrue(s.hasNextShort()); 3975 s.close(); 3976 try { 3977 s.nextShort(); 3978 fail(); 3979 } catch (IllegalStateException expected) { 3980 } 3981 } 3982 3983 /** 3984 * @throws IOException 3985 * @tests java.util.Scanner#hasNextLong(int) 3986 */ 3987 public void test_hasNextLongI() throws IOException { 3988 s = new Scanner("123 456"); 3989 assertTrue(s.hasNextLong(10)); 3990 assertEquals(123, s.nextLong(10)); 3991 assertTrue(s.hasNextLong(10)); 3992 assertEquals(456, s.nextLong(10)); 3993 assertFalse(s.hasNextLong(10)); 3994 try { 3995 s.nextLong(10); 3996 fail(); 3997 } catch (NoSuchElementException expected) { 3998 } 3999 4000 // If the radix is different from 10 4001 s = new Scanner("123 456"); 4002 assertTrue(s.hasNextLong(5)); 4003 assertEquals(38, s.nextLong(5)); 4004 assertFalse(s.hasNextLong(5)); 4005 try { 4006 s.nextLong(5); 4007 fail(); 4008 } catch (InputMismatchException expected) { 4009 } 4010 4011 // If the number is out of range 4012 s = new Scanner("123456789123456789123456789123456789"); 4013 assertFalse(s.hasNextLong(10)); 4014 try { 4015 s.nextLong(10); 4016 fail(); 4017 } catch (InputMismatchException expected) { 4018 } 4019 4020 /* 4021 * Different locale can only recognize corresponding locale sensitive 4022 * string. ',' is used in many locales as group separator. 4023 */ 4024 s = new Scanner("23,456 23,456"); 4025 s.useLocale(Locale.GERMANY); 4026 assertFalse(s.hasNextShort(10)); 4027 try { 4028 s.nextLong(10); 4029 fail(); 4030 } catch (InputMismatchException expected) { 4031 } 4032 s.useLocale(Locale.ENGLISH); 4033 // If exception is thrown out, input will not be advanced. 4034 assertTrue(s.hasNextLong(10)); 4035 assertEquals(23456, s.nextLong(10)); 4036 assertTrue(s.hasNextLong(10)); 4037 assertEquals(23456, s.nextLong(10)); 4038 4039 /* 4040 * ''' is used in many locales as group separator. 4041 */ 4042 s = new Scanner("23'456 23'456"); 4043 s.useLocale(Locale.GERMANY); 4044 assertFalse(s.hasNextLong(10)); 4045 try { 4046 s.nextLong(10); 4047 fail(); 4048 } catch (InputMismatchException expected) { 4049 } 4050 s.useLocale(new Locale("it", "CH")); 4051 // If exception is thrown out, input will not be advanced. 4052 assertTrue(s.hasNextLong(10)); 4053 assertEquals(23456, s.nextLong(10)); 4054 assertTrue(s.hasNextLong(10)); 4055 assertEquals(23456, s.nextLong(10)); 4056 4057 /* 4058 * The input string has Arabic-Indic digits. 4059 */ 4060 s = new Scanner("1\u06602 1\u06662"); 4061 assertTrue(s.hasNextLong(10)); 4062 assertEquals(102, s.nextLong(10)); 4063 assertFalse(s.hasNextLong(5)); 4064 try { 4065 s.nextLong(5); 4066 fail(); 4067 } catch (InputMismatchException expected) { 4068 } 4069 assertTrue(s.hasNextLong(10)); 4070 assertEquals(162, s.nextLong(10)); 4071 4072 /* 4073 * '.' is used in many locales as group separator. The input string 4074 * has Arabic-Indic digits . 4075 */ 4076 s = new Scanner("23.45\u0666 23.456"); 4077 s.useLocale(Locale.CHINESE); 4078 assertFalse(s.hasNextLong(10)); 4079 try { 4080 s.nextLong(10); 4081 fail(); 4082 } catch (InputMismatchException expected) { 4083 } 4084 s.useLocale(Locale.GERMANY); 4085 // If exception is thrown out, input will not be advanced. 4086 assertTrue(s.hasNextLong(10)); 4087 assertEquals(23456, s.nextLong(10)); 4088 assertTrue(s.hasNextLong(10)); 4089 assertEquals(23456, s.nextLong(10)); 4090 4091 // The input string starts with zero 4092 s = new Scanner("03,456"); 4093 s.useLocale(Locale.ENGLISH); 4094 assertFalse(s.hasNextLong(10)); 4095 try { 4096 s.nextLong(10); 4097 fail(); 4098 } catch (InputMismatchException expected) { 4099 } 4100 4101 s = new Scanner("03456"); 4102 assertTrue(s.hasNextLong(10)); 4103 assertEquals(3456, s.nextLong(10)); 4104 4105 s = new Scanner("\u06603,456"); 4106 s.useLocale(Locale.ENGLISH); 4107 assertTrue(s.hasNextLong(10)); 4108 assertEquals(3456, s.nextLong(10)); 4109 4110 s = new Scanner("E34"); 4111 assertTrue(s.hasNextLong(16)); 4112 assertEquals(3636, s.nextLong(16)); 4113 4114 /* 4115 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4116 * respectively, but they are not differentiated. 4117 */ 4118 s = new Scanner("12300"); 4119 s.useLocale(Locale.CHINESE); 4120 assertTrue(s.hasNextLong(10)); 4121 assertEquals(12300, s.nextLong(10)); 4122 4123 s = new Scanner("123\u0966\u0966"); 4124 s.useLocale(Locale.CHINESE); 4125 assertTrue(s.hasNextLong(10)); 4126 assertEquals(12300, s.nextLong(10)); 4127 4128 s = new Scanner("123\u0e50\u0e50"); 4129 s.useLocale(Locale.CHINESE); 4130 assertTrue(s.hasNextLong(10)); 4131 assertEquals(12300, s.nextLong(10)); 4132 4133 s = new Scanner("-123"); 4134 s.useLocale(new Locale("ar", "AE")); 4135 assertTrue(s.hasNextLong(10)); 4136 assertEquals(-123, s.nextLong(10)); 4137 4138 4139 s = new Scanner("-123"); 4140 s.useLocale(new Locale("mk", "MK")); 4141 assertTrue(s.hasNextLong(10)); 4142 assertEquals(-123, s.nextLong(10)); 4143 } 4144 4145 /** 4146 * @throws IOException 4147 * @tests java.util.Scanner#hasNextLong(int) 4148 */ 4149 public void test_hasNextLongI_cache() throws IOException { 4150 //regression for HARMONY-2063 4151 s = new Scanner("123 456"); 4152 assertTrue(s.hasNextLong(16)); 4153 assertEquals(291, s.nextLong()); 4154 assertEquals(456, s.nextLong()); 4155 4156 s = new Scanner("123 456"); 4157 assertTrue(s.hasNextLong(16)); 4158 assertTrue(s.hasNextLong(8)); 4159 assertEquals(83, s.nextLong()); 4160 assertEquals(456, s.nextLong()); 4161 4162 s = new Scanner("-123 -456 -789"); 4163 assertTrue(s.hasNextLong(8)); 4164 assertEquals(-123, s.nextInt()); 4165 assertEquals(-456, s.nextLong()); 4166 assertTrue(s.hasNextShort(16)); 4167 assertEquals(-789, s.nextLong()); 4168 4169 s = new Scanner("123 456"); 4170 assertTrue(s.hasNextLong()); 4171 s.close(); 4172 try { 4173 s.nextLong(); 4174 fail(); 4175 } catch (IllegalStateException expected) { 4176 } 4177 } 4178 4179 /** 4180 * @throws IOException 4181 * @tests java.util.Scanner#hasNextLong() 4182 */ 4183 public void test_hasNextLong() throws IOException { 4184 s = new Scanner("123 456"); 4185 assertTrue(s.hasNextLong()); 4186 assertEquals(123, s.nextLong()); 4187 assertTrue(s.hasNextLong()); 4188 assertEquals(456, s.nextLong()); 4189 assertFalse(s.hasNextLong()); 4190 try { 4191 s.nextLong(); 4192 fail(); 4193 } catch (NoSuchElementException expected) { 4194 } 4195 4196 // If the radix is different from 10 4197 s = new Scanner("123 456"); 4198 s.useRadix(5); 4199 assertTrue(s.hasNextLong()); 4200 assertEquals(38, s.nextLong()); 4201 assertFalse(s.hasNextLong()); 4202 try { 4203 s.nextLong(); 4204 fail(); 4205 } catch (InputMismatchException expected) { 4206 } 4207 4208 // If the number is out of range 4209 s = new Scanner("123456789123456789123456789123456789"); 4210 assertFalse(s.hasNextLong()); 4211 try { 4212 s.nextLong(); 4213 fail(); 4214 } catch (InputMismatchException expected) { 4215 } 4216 4217 /* 4218 * Different locale can only recognize corresponding locale sensitive 4219 * string. ',' is used in many locales as group separator. 4220 */ 4221 s = new Scanner("23,456 23,456"); 4222 s.useLocale(Locale.GERMANY); 4223 assertFalse(s.hasNextLong()); 4224 try { 4225 s.nextLong(); 4226 fail(); 4227 } catch (InputMismatchException expected) { 4228 } 4229 s.useLocale(Locale.ENGLISH); 4230 // If exception is thrown out, input will not be advanced. 4231 assertTrue(s.hasNextLong()); 4232 assertEquals(23456, s.nextLong()); 4233 assertTrue(s.hasNextLong()); 4234 assertEquals(23456, s.nextLong()); 4235 4236 /* 4237 * ''' is used in many locales as group separator. 4238 */ 4239 s = new Scanner("23'456 23'456"); 4240 s.useLocale(Locale.GERMANY); 4241 assertFalse(s.hasNextLong()); 4242 try { 4243 s.nextLong(); 4244 fail(); 4245 } catch (InputMismatchException expected) { 4246 } 4247 s.useLocale(new Locale("it", "CH")); 4248 // If exception is thrown out, input will not be advanced. 4249 assertTrue(s.hasNextLong()); 4250 assertEquals(23456, s.nextLong()); 4251 assertTrue(s.hasNextLong()); 4252 assertEquals(23456, s.nextLong()); 4253 4254 /* 4255 * The input string has Arabic-Indic digits. 4256 */ 4257 s = new Scanner("1\u06602 1\u06662"); 4258 assertEquals(102, s.nextLong()); 4259 s.useRadix(5); 4260 assertFalse(s.hasNextLong()); 4261 try { 4262 s.nextLong(); 4263 fail(); 4264 } catch (InputMismatchException expected) { 4265 } 4266 s.useRadix(10); 4267 assertTrue(s.hasNextLong()); 4268 assertEquals(162, s.nextLong()); 4269 4270 /* 4271 * '.' is used in many locales as group separator. The input string 4272 * has Arabic-Indic digits . 4273 */ 4274 s = new Scanner("23.45\u0666 23.456"); 4275 s.useLocale(Locale.CHINESE); 4276 assertFalse(s.hasNextLong()); 4277 try { 4278 s.nextLong(); 4279 fail(); 4280 } catch (InputMismatchException expected) { 4281 } 4282 s.useLocale(Locale.GERMANY); 4283 // If exception is thrown out, input will not be advanced. 4284 assertTrue(s.hasNextLong()); 4285 assertEquals(23456, s.nextLong()); 4286 assertTrue(s.hasNextLong()); 4287 assertEquals(23456, s.nextLong()); 4288 4289 // The input string starts with zero 4290 s = new Scanner("03,456"); 4291 s.useLocale(Locale.ENGLISH); 4292 assertFalse(s.hasNextLong()); 4293 try { 4294 s.nextLong(); 4295 fail(); 4296 } catch (InputMismatchException expected) { 4297 } 4298 4299 s = new Scanner("03456"); 4300 assertTrue(s.hasNextLong()); 4301 assertEquals(3456, s.nextLong()); 4302 4303 s = new Scanner("\u06603,456"); 4304 s.useLocale(Locale.ENGLISH); 4305 assertTrue(s.hasNextLong()); 4306 assertEquals(3456, s.nextLong()); 4307 4308 s = new Scanner("E34"); 4309 s.useRadix(16); 4310 assertTrue(s.hasNextLong()); 4311 assertEquals(3636, s.nextLong()); 4312 4313 /* 4314 * There are 3 types of zero digit in all locales, '0' '\u0966' '\u0e50' 4315 * respectively, but they are not differentiated. 4316 */ 4317 s = new Scanner("12300"); 4318 s.useLocale(Locale.CHINESE); 4319 assertTrue(s.hasNextLong()); 4320 assertEquals(12300, s.nextLong()); 4321 4322 s = new Scanner("123\u0966\u0966"); 4323 s.useLocale(Locale.CHINESE); 4324 assertTrue(s.hasNextLong()); 4325 assertEquals(12300, s.nextLong()); 4326 4327 s = new Scanner("123\u0e50\u0e50"); 4328 s.useLocale(Locale.CHINESE); 4329 assertTrue(s.hasNextLong()); 4330 assertEquals(12300, s.nextLong()); 4331 4332 s = new Scanner("-123"); 4333 s.useLocale(new Locale("ar", "AE")); 4334 assertTrue(s.hasNextLong()); 4335 assertEquals(-123, s.nextLong()); 4336 4337 s = new Scanner("-123"); 4338 s.useLocale(new Locale("mk", "MK")); 4339 assertTrue(s.hasNextLong()); 4340 assertEquals(-123, s.nextLong()); 4341 } 4342 4343 /** 4344 * @throws IOException 4345 * @tests java.util.Scanner#nextDouble() 4346 */ 4347 public void test_hasNextDouble() throws IOException { 4348 s = new Scanner("123 45\u0666. 123.4 .123 "); 4349 s.useLocale(Locale.ENGLISH); 4350 assertTrue(s.hasNextDouble()); 4351 assertEquals(123.0, s.nextDouble()); 4352 assertTrue(s.hasNextDouble()); 4353 assertEquals(456.0, s.nextDouble()); 4354 assertTrue(s.hasNextDouble()); 4355 assertEquals(123.4, s.nextDouble()); 4356 assertTrue(s.hasNextDouble()); 4357 assertEquals(0.123, s.nextDouble()); 4358 assertFalse(s.hasNextDouble()); 4359 try { 4360 s.nextDouble(); 4361 fail(); 4362 } catch (NoSuchElementException expected) { 4363 } 4364 4365 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 4366 s.useLocale(Locale.ENGLISH); 4367 assertTrue(s.hasNextDouble()); 4368 assertEquals(123.4, s.nextDouble()); 4369 assertTrue(s.hasNextDouble()); 4370 assertEquals(-456.7, s.nextDouble()); 4371 assertTrue(s.hasNextDouble()); 4372 assertEquals(123456.789, s.nextDouble()); 4373 assertFalse(s.hasNextDouble()); 4374 try { 4375 s.nextDouble(); 4376 fail(); 4377 } catch (InputMismatchException expected) { 4378 } 4379 4380 // Scientific notation 4381 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 4382 s.useLocale(Locale.ENGLISH); 4383 assertTrue(s.hasNextDouble()); 4384 assertEquals(1.234E12, s.nextDouble()); 4385 assertTrue(s.hasNextDouble()); 4386 assertEquals(-4.567E14, s.nextDouble()); 4387 assertTrue(s.hasNextDouble()); 4388 assertEquals(1.23456789E-5, s.nextDouble()); 4389 4390 s = new Scanner("NaN Infinity -Infinity"); 4391 assertTrue(s.hasNextDouble()); 4392 assertEquals(Double.NaN, s.nextDouble()); 4393 assertTrue(s.hasNextDouble()); 4394 assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 4395 assertTrue(s.hasNextDouble()); 4396 assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble()); 4397 4398 String str=String.valueOf(Double.MAX_VALUE*2); 4399 s=new Scanner(str); 4400 assertTrue(s.hasNextDouble()); 4401 assertEquals(Double.POSITIVE_INFINITY,s.nextDouble()); 4402 4403 /* 4404 * Different locale can only recognize corresponding locale sensitive 4405 * string. ',' is used in many locales as group separator. 4406 */ 4407 s = new Scanner("23,456 23,456"); 4408 s.useLocale(Locale.ENGLISH); 4409 assertTrue(s.hasNextDouble()); 4410 assertEquals(23456.0, s.nextDouble()); 4411 s.useLocale(Locale.GERMANY); 4412 assertTrue(s.hasNextDouble()); 4413 assertEquals(23.456, s.nextDouble()); 4414 4415 s = new Scanner("23.456 23.456"); 4416 s.useLocale(Locale.ENGLISH); 4417 assertTrue(s.hasNextDouble()); 4418 assertEquals(23.456, s.nextDouble()); 4419 s.useLocale(Locale.GERMANY); 4420 assertTrue(s.hasNextDouble()); 4421 assertEquals(23456.0, s.nextDouble()); 4422 4423 s = new Scanner("23,456.7 23.456,7"); 4424 s.useLocale(Locale.ENGLISH); 4425 assertTrue(s.hasNextDouble()); 4426 assertEquals(23456.7, s.nextDouble()); 4427 s.useLocale(Locale.GERMANY); 4428 assertTrue(s.hasNextDouble()); 4429 assertEquals(23456.7, s.nextDouble()); 4430 4431 s = new Scanner("-123.4"); 4432 s.useLocale(Locale.ENGLISH); 4433 assertTrue(s.hasNextDouble()); 4434 assertEquals(-123.4, s.nextDouble()); 4435 4436 s = new Scanner("+123.4 -456.7"); 4437 s.useLocale(Locale.ENGLISH); 4438 assertTrue(s.hasNextDouble()); 4439 s.close(); 4440 try{ 4441 s.nextDouble(); 4442 fail(); 4443 }catch(IllegalStateException expected) { 4444 } 4445 } 4446 4447 /** 4448 * @throws IOException 4449 * @tests java.util.Scanner#hasNextBigDecimal() 4450 */ 4451 public void test_hasNextBigDecimal() throws IOException { 4452 s = new Scanner("123 45\u0666. 123.4 .123 "); 4453 s.useLocale(Locale.ENGLISH); 4454 assertTrue(s.hasNextBigDecimal()); 4455 assertEquals(new BigDecimal("123"), s.nextBigDecimal()); 4456 assertTrue(s.hasNextBigDecimal()); 4457 assertEquals(new BigDecimal("456"), s.nextBigDecimal()); 4458 assertTrue(s.hasNextBigDecimal()); 4459 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 4460 assertTrue(s.hasNextBigDecimal()); 4461 assertEquals(new BigDecimal("0.123"), s.nextBigDecimal()); 4462 assertFalse(s.hasNextBigDecimal()); 4463 try { 4464 s.nextBigDecimal(); 4465 fail(); 4466 } catch (NoSuchElementException expected) { 4467 } 4468 4469 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 4470 s.useLocale(Locale.ENGLISH); 4471 assertTrue(s.hasNextBigDecimal()); 4472 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 4473 assertTrue(s.hasNextBigDecimal()); 4474 assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal()); 4475 assertTrue(s.hasNextBigDecimal()); 4476 assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal()); 4477 assertFalse(s.hasNextBigDecimal()); 4478 try { 4479 s.nextBigDecimal(); 4480 fail(); 4481 } catch (InputMismatchException expected) { 4482 } 4483 4484 // Scientific notation 4485 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 4486 s.useLocale(Locale.ENGLISH); 4487 assertTrue(s.hasNextBigDecimal()); 4488 assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal()); 4489 assertTrue(s.hasNextBigDecimal()); 4490 assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal()); 4491 assertTrue(s.hasNextBigDecimal()); 4492 assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal()); 4493 4494 s = new Scanner("NaN"); 4495 assertFalse(s.hasNextBigDecimal()); 4496 try { 4497 s.nextBigDecimal(); 4498 fail(); 4499 } catch (InputMismatchException expected) { 4500 } 4501 4502 /* 4503 * Different locale can only recognize corresponding locale sensitive 4504 * string. ',' is used in many locales as group separator. 4505 */ 4506 s = new Scanner("23,456 23,456"); 4507 s.useLocale(Locale.ENGLISH); 4508 assertTrue(s.hasNextBigDecimal()); 4509 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 4510 s.useLocale(Locale.GERMANY); 4511 assertTrue(s.hasNextBigDecimal()); 4512 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 4513 4514 s = new Scanner("23.456 23.456"); 4515 s.useLocale(Locale.ENGLISH); 4516 assertTrue(s.hasNextBigDecimal()); 4517 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 4518 s.useLocale(Locale.GERMANY); 4519 assertTrue(s.hasNextBigDecimal()); 4520 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 4521 4522 s = new Scanner("23,456.7"); 4523 s.useLocale(Locale.ENGLISH); 4524 assertTrue(s.hasNextBigDecimal()); 4525 assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal()); 4526 4527 s = new Scanner("-123.4"); 4528 s.useLocale(Locale.ENGLISH); 4529 assertTrue(s.hasNextBigDecimal()); 4530 assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal()); 4531 } 4532 4533 private static class MockStringReader extends StringReader { 4534 4535 public MockStringReader(String param) { 4536 super(param); 4537 } 4538 4539 public int read(CharBuffer target) throws IOException { 4540 target.append('t'); 4541 target.append('e'); 4542 target.append('s'); 4543 target.append('t'); 4544 throw new IOException(); 4545 } 4546 4547 } 4548 4549 private static class MockStringReader2Read extends StringReader { 4550 int timesRead = 0; 4551 4552 public MockStringReader2Read(String param) { 4553 super(param); 4554 } 4555 4556 public int read(CharBuffer target) throws IOException { 4557 if (timesRead == 0) { 4558 target.append('1'); 4559 target.append('2'); 4560 target.append('3'); 4561 timesRead++; 4562 return 3; 4563 } else if (timesRead == 1) { 4564 target.append('t'); 4565 timesRead++; 4566 return 1; 4567 } else { 4568 throw new IOException(); 4569 } 4570 } 4571 4572 } 4573 4574 // https://code.google.com/p/android/issues/detail?id=40555 4575 public void test_40555() throws Exception { 4576 MockStringReader2Read reader = new MockStringReader2Read("MockStringReader"); 4577 s = new Scanner(reader); 4578 // We should get a match straight away. 4579 String result = s.findWithinHorizon("1", 0); 4580 assertEquals("1", result); 4581 // The stream should not be consumed as there's already a match after the first read. 4582 assertEquals(1, reader.timesRead); 4583 } 4584 4585 /** 4586 * @tests java.util.Scanner#findWithinHorizon(Pattern, int) 4587 */ 4588 public void test_findWithinHorizon_LPatternI() { 4589 4590 // This method searches through the input up to the specified search 4591 // horizon(exclusive). 4592 s = new Scanner("123test"); 4593 String result = s.findWithinHorizon(Pattern.compile("\\p{Lower}"), 5); 4594 assertEquals("t", result); 4595 MatchResult mresult = s.match(); 4596 assertEquals(3, mresult.start()); 4597 assertEquals(4, mresult.end()); 4598 4599 s = new Scanner("12345test1234test next"); 4600 /* 4601 * If the pattern is found the scanner advances past the input that 4602 * matched and returns the string that matched the pattern. 4603 */ 4604 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 2); 4605 assertEquals("12", result); 4606 mresult = s.match(); 4607 assertEquals(0, mresult.start()); 4608 assertEquals(2, mresult.end()); 4609 // Position is now pointing at the bar. "12|345test1234test next" 4610 4611 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 6); 4612 assertEquals("345", result); 4613 4614 mresult = s.match(); 4615 assertEquals(2, mresult.start()); 4616 assertEquals(5, mresult.end()); 4617 // Position is now pointing at the bar. "12345|test1234test next" 4618 4619 // If no such pattern is detected then the null is returned and the 4620 // scanner's position remains unchanged. 4621 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 3); 4622 assertNull(result); 4623 4624 try { 4625 s.match(); 4626 fail(); 4627 } catch (IllegalStateException expected) { 4628 } 4629 assertEquals("345", mresult.group()); 4630 assertEquals(2, mresult.start()); 4631 assertEquals(5, mresult.end()); 4632 // Position is now still pointing at the bar. "12345|test1234test next" 4633 4634 // If horizon is 0, then the horizon is ignored and this method 4635 // continues to search through the input looking for the specified 4636 // pattern without bound. 4637 result = s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), 0); 4638 mresult = s.match(); 4639 assertEquals(9, mresult.start()); 4640 assertEquals(13, mresult.end()); 4641 // Position is now pointing at the bar. "12345test1234|test next" 4642 4643 assertEquals("test", s.next()); 4644 mresult = s.match(); 4645 assertEquals(13, mresult.start()); 4646 assertEquals(17, mresult.end()); 4647 4648 assertEquals("next", s.next()); 4649 mresult = s.match(); 4650 assertEquals(18, mresult.start()); 4651 assertEquals(22, mresult.end()); 4652 4653 try { 4654 s.findWithinHorizon((Pattern) null, -1); 4655 fail(); 4656 } catch (NullPointerException expected) { 4657 } 4658 4659 try { 4660 s.findWithinHorizon(Pattern.compile("\\p{Digit}+"), -1); 4661 fail(); 4662 } catch (IllegalArgumentException expected) { 4663 } 4664 4665 s.close(); 4666 try { 4667 s.findWithinHorizon((Pattern) null, -1); 4668 fail(); 4669 } catch (IllegalStateException expected) { 4670 } 4671 4672 s = new Scanner("test"); 4673 result = s.findWithinHorizon(Pattern.compile("\\w+"), 10); 4674 assertEquals("test", result); 4675 4676 s = new Scanner("aa\n\rb"); 4677 result = s.findWithinHorizon(Pattern.compile("a"), 5); 4678 assertEquals("a", result); 4679 mresult = s.match(); 4680 assertEquals(0, mresult.start()); 4681 assertEquals(1, mresult.end()); 4682 4683 result = s.findWithinHorizon(Pattern.compile("^(a)$", Pattern.MULTILINE), 5); 4684 assertNull(result); 4685 4686 try { 4687 mresult = s.match(); 4688 fail(); 4689 } catch (IllegalStateException expected) { 4690 } 4691 4692 s = new Scanner(""); 4693 result = s.findWithinHorizon(Pattern.compile("^"), 0); 4694 assertEquals("", result); 4695 MatchResult matchResult = s.match(); 4696 assertEquals(0, matchResult.start()); 4697 assertEquals(0, matchResult.end()); 4698 4699 result = s.findWithinHorizon(Pattern.compile("$"), 0); 4700 assertEquals("", result); 4701 matchResult = s.match(); 4702 assertEquals(0, matchResult.start()); 4703 assertEquals(0, matchResult.end()); 4704 4705 s = new Scanner("1 fish 2 fish red fish blue fish"); 4706 result = s.findWithinHorizon(Pattern 4707 .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 10); 4708 assertNull(result); 4709 4710 try { 4711 mresult = s.match(); 4712 fail(); 4713 } catch (IllegalStateException expected) { 4714 } 4715 assertEquals(0, mresult.groupCount()); 4716 4717 result = s.findWithinHorizon(Pattern 4718 .compile("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)"), 100); 4719 assertEquals("1 fish 2 fish red fish blue", result); 4720 mresult = s.match(); 4721 assertEquals(4, mresult.groupCount()); 4722 assertEquals("1", mresult.group(1)); 4723 assertEquals("2", mresult.group(2)); 4724 assertEquals("red", mresult.group(3)); 4725 assertEquals("blue", mresult.group(4)); 4726 4727 s = new Scanner("test"); 4728 s.close(); 4729 try { 4730 s.findWithinHorizon(Pattern.compile("test"), 1); 4731 fail(); 4732 } catch (IllegalStateException expected) { 4733 } 4734 4735 s = new Scanner("word1 WorD2 "); 4736 s.close(); 4737 try { 4738 s.findWithinHorizon(Pattern.compile("\\d+"), 10); 4739 fail(); 4740 } catch (IllegalStateException expected) { 4741 } 4742 4743 s = new Scanner("word1 WorD2 wOrd3 "); 4744 Pattern pattern = Pattern.compile("\\d+"); 4745 assertEquals("1", s.findWithinHorizon(pattern, 10)); 4746 assertEquals("WorD2", s.next()); 4747 assertEquals("3", s.findWithinHorizon(pattern, 15)); 4748 4749 // Regression test 4750 s = new Scanner(new MockStringReader("MockStringReader")); 4751 pattern = Pattern.compile("test"); 4752 result = s.findWithinHorizon(pattern, 10); 4753 assertEquals("test", result); 4754 4755 // Test the situation when input length is longer than buffer size. 4756 StringBuilder stringBuilder = new StringBuilder(); 4757 for (int i = 0; i < 1026; i++) { 4758 stringBuilder.append('a'); 4759 } 4760 s = new Scanner(stringBuilder.toString()); 4761 pattern = Pattern.compile("\\p{Lower}+"); 4762 result = s.findWithinHorizon(pattern, 1026); 4763 assertEquals(stringBuilder.toString().length(), result.length()); 4764 assertEquals(stringBuilder.toString(), result); 4765 4766 // Test the situation when input length is longer than buffer size and 4767 // set horizon to buffer size. 4768 stringBuilder = new StringBuilder(); 4769 for (int i = 0; i < 1026; i++) { 4770 stringBuilder.append('a'); 4771 } 4772 s = new Scanner(stringBuilder.toString()); 4773 pattern = Pattern.compile("\\p{Lower}+"); 4774 result = s.findWithinHorizon(pattern, 1022); 4775 assertEquals(1022, result.length()); 4776 assertEquals(stringBuilder.subSequence(0, 1022), result); 4777 4778 // Test the situation, under which pattern is clipped by buffer. 4779 stringBuilder = new StringBuilder(); 4780 for (int i = 0; i < 1022; i++) { 4781 stringBuilder.append(' '); 4782 } 4783 stringBuilder.append("bbc"); 4784 assertEquals(1025, stringBuilder.length()); 4785 s = new Scanner(stringBuilder.toString()); 4786 pattern = Pattern.compile("bbc"); 4787 result = s.findWithinHorizon(pattern, 1025); 4788 assertEquals(3, result.length()); 4789 assertEquals(stringBuilder.subSequence(1022, 1025), result); 4790 4791 stringBuilder = new StringBuilder(); 4792 for (int i = 0; i < 1026; i++) { 4793 stringBuilder.append('a'); 4794 } 4795 s = new Scanner(stringBuilder.toString()); 4796 pattern = Pattern.compile("\\p{Lower}+"); 4797 result = s.findWithinHorizon(pattern, 0); 4798 assertEquals(stringBuilder.toString(), result); 4799 4800 stringBuilder = new StringBuilder(); 4801 for (int i = 0; i < 10240; i++) { 4802 stringBuilder.append('-'); 4803 } 4804 stringBuilder.replace(0, 2, "aa"); 4805 s = new Scanner(stringBuilder.toString()); 4806 result = s.findWithinHorizon(Pattern.compile("aa"), 0); 4807 assertEquals("aa", result); 4808 4809 s = new Scanner("aaaa"); 4810 result = s.findWithinHorizon(Pattern.compile("a*"), 0); 4811 assertEquals("aaaa", result); 4812 } 4813 4814 /** 4815 * @tests java.util.Scanner#findInLine(Pattern) 4816 */ 4817 public void test_findInLine_LPattern() { 4818 4819 Scanner s = new Scanner(""); 4820 try { 4821 s.findInLine((Pattern) null); 4822 fail(); 4823 } catch (NullPointerException expected) { 4824 } 4825 String result = s.findInLine(Pattern.compile("^")); 4826 assertEquals("", result); 4827 MatchResult matchResult = s.match(); 4828 assertEquals(0, matchResult.start()); 4829 assertEquals(0, matchResult.end()); 4830 4831 result = s.findInLine(Pattern.compile("$")); 4832 assertEquals("", result); 4833 matchResult = s.match(); 4834 assertEquals(0, matchResult.start()); 4835 assertEquals(0, matchResult.end()); 4836 4837 /* 4838 * When we use the operation of findInLine(Pattern), the match region 4839 * should not span the line separator. 4840 */ 4841 s = new Scanner("aa\nb.b"); 4842 result = s.findInLine(Pattern.compile("a\nb*")); 4843 assertNull(result); 4844 4845 s = new Scanner("aa\nbb.b"); 4846 result = s.findInLine(Pattern.compile("\\.")); 4847 assertNull(result); 4848 4849 s = new Scanner("abcd1234test\n"); 4850 result = s.findInLine(Pattern.compile("\\p{Lower}+")); 4851 assertEquals("abcd", result); 4852 matchResult = s.match(); 4853 assertEquals(0, matchResult.start()); 4854 assertEquals(4, matchResult.end()); 4855 4856 result = s.findInLine(Pattern.compile("\\p{Digit}{5}")); 4857 assertNull(result); 4858 try { 4859 matchResult = s.match(); 4860 fail(); 4861 } catch (IllegalStateException expected) { 4862 } 4863 assertEquals(0, matchResult.start()); 4864 assertEquals(4, matchResult.end()); 4865 4866 result = s.findInLine(Pattern.compile("\\p{Lower}+")); 4867 assertEquals("test", result); 4868 matchResult = s.match(); 4869 assertEquals(8, matchResult.start()); 4870 assertEquals(12, matchResult.end()); 4871 4872 char[] chars = new char[2048]; 4873 Arrays.fill(chars, 'a'); 4874 StringBuilder stringBuilder = new StringBuilder(); 4875 stringBuilder.append(chars); 4876 stringBuilder.append("1234"); 4877 s = new Scanner(stringBuilder.toString()); 4878 result = s.findInLine(Pattern.compile("\\p{Digit}+")); 4879 assertEquals("1234", result); 4880 matchResult = s.match(); 4881 assertEquals(2048, matchResult.start()); 4882 assertEquals(2052, matchResult.end()); 4883 4884 s = new Scanner("test"); 4885 s.close(); 4886 try { 4887 s.findInLine((Pattern) null); 4888 fail(); 4889 } catch (IllegalStateException expected) { 4890 } 4891 4892 s = new Scanner("test1234\n1234 test"); 4893 result = s.findInLine(Pattern.compile("test")); 4894 assertEquals("test", result); 4895 matchResult = s.match(); 4896 assertEquals(0, matchResult.start()); 4897 assertEquals(4, matchResult.end()); 4898 4899 int number = s.nextInt(); 4900 assertEquals(1234, number); 4901 matchResult = s.match(); 4902 assertEquals(4, matchResult.start()); 4903 assertEquals(8, matchResult.end()); 4904 4905 result = s.next(); 4906 assertEquals("1234", result); 4907 matchResult = s.match(); 4908 assertEquals(9, matchResult.start()); 4909 assertEquals(13, matchResult.end()); 4910 4911 result = s.findInLine(Pattern.compile("test")); 4912 assertEquals("test", result); 4913 matchResult = s.match(); 4914 assertEquals(14, matchResult.start()); 4915 assertEquals(18, matchResult.end()); 4916 4917 s = new Scanner("test\u0085\ntest"); 4918 result = s.findInLine("est"); 4919 assertEquals("est", result); 4920 result = s.findInLine("est"); 4921 assertEquals("est", result); 4922 4923 s = new Scanner("test\ntest"); 4924 result = s.findInLine("est"); 4925 assertEquals("est", result); 4926 result = s.findInLine("est"); 4927 assertEquals("est", result); 4928 4929 s = new Scanner("test\n123\ntest"); 4930 result = s.findInLine("est"); 4931 assertEquals("est", result); 4932 result = s.findInLine("est"); 4933 // RI fails. It is a RI's bug. 4934 assertNull(result); 4935 4936 s = new Scanner( " *\n"); 4937 result = s.findInLine(Pattern.compile( "^\\s*(?:\\*(?=[^/]))")); 4938 assertEquals(" *", result); 4939 } 4940 4941 public void test_findInLine_LString_NPEs() { 4942 s = new Scanner("test"); 4943 try { 4944 s.findInLine((String) null); 4945 fail(); 4946 } catch (NullPointerException expected) { 4947 } 4948 s.close(); 4949 try { 4950 s.findInLine((String) null); 4951 fail(); 4952 } catch (NullPointerException expected) { 4953 } 4954 try { 4955 s.findInLine("test"); 4956 fail(); 4957 } catch (IllegalStateException expected) { 4958 } 4959 } 4960 4961 public void test_findInLine_LString() { 4962 Scanner s = new Scanner(""); 4963 String result = s.findInLine("^"); 4964 assertEquals("", result); 4965 MatchResult matchResult = s.match(); 4966 assertEquals(0, matchResult.start()); 4967 assertEquals(0, matchResult.end()); 4968 4969 result = s.findInLine("$"); 4970 assertEquals("", result); 4971 matchResult = s.match(); 4972 assertEquals(0, matchResult.start()); 4973 assertEquals(0, matchResult.end()); 4974 4975 // When we use the operation of findInLine(Pattern), the match region 4976 // should not span the line separator. 4977 s = new Scanner("aa\nb.b"); 4978 result = s.findInLine("a\nb*"); 4979 assertNull(result); 4980 4981 s = new Scanner("aa\nbb.b"); 4982 result = s.findInLine("\\."); 4983 assertNull(result); 4984 4985 s = new Scanner("abcd1234test\n"); 4986 result = s.findInLine("\\p{Lower}+"); 4987 assertEquals("abcd", result); 4988 matchResult = s.match(); 4989 assertEquals(0, matchResult.start()); 4990 assertEquals(4, matchResult.end()); 4991 4992 result = s.findInLine("\\p{Digit}{5}"); 4993 assertNull(result); 4994 try { 4995 matchResult = s.match(); 4996 fail(); 4997 } catch (IllegalStateException expected) { 4998 } 4999 assertEquals(0, matchResult.start()); 5000 assertEquals(4, matchResult.end()); 5001 5002 result = s.findInLine("\\p{Lower}+"); 5003 assertEquals("test", result); 5004 matchResult = s.match(); 5005 assertEquals(8, matchResult.start()); 5006 assertEquals(12, matchResult.end()); 5007 5008 char[] chars = new char[2048]; 5009 Arrays.fill(chars, 'a'); 5010 StringBuilder stringBuilder = new StringBuilder(); 5011 stringBuilder.append(chars); 5012 stringBuilder.append("1234"); 5013 s = new Scanner(stringBuilder.toString()); 5014 result = s.findInLine("\\p{Digit}+"); 5015 assertEquals("1234", result); 5016 matchResult = s.match(); 5017 assertEquals(2048, matchResult.start()); 5018 assertEquals(2052, matchResult.end()); 5019 5020 s = new Scanner("test1234\n1234 test"); 5021 result = s.findInLine("test"); 5022 assertEquals("test", result); 5023 matchResult = s.match(); 5024 assertEquals(0, matchResult.start()); 5025 assertEquals(4, matchResult.end()); 5026 5027 int number = s.nextInt(); 5028 assertEquals(1234, number); 5029 matchResult = s.match(); 5030 assertEquals(4, matchResult.start()); 5031 assertEquals(8, matchResult.end()); 5032 5033 result = s.next(); 5034 assertEquals("1234", result); 5035 matchResult = s.match(); 5036 assertEquals(9, matchResult.start()); 5037 assertEquals(13, matchResult.end()); 5038 5039 result = s.findInLine("test"); 5040 assertEquals("test", result); 5041 matchResult = s.match(); 5042 assertEquals(14, matchResult.start()); 5043 assertEquals(18, matchResult.end()); 5044 5045 s = new Scanner("test\u0085\ntest"); 5046 result = s.findInLine("est"); 5047 assertEquals("est", result); 5048 result = s.findInLine("est"); 5049 assertEquals("est", result); 5050 5051 s = new Scanner("test\ntest"); 5052 result = s.findInLine("est"); 5053 assertEquals("est", result); 5054 result = s.findInLine("est"); 5055 assertEquals("est", result); 5056 5057 s = new Scanner("test\n123\ntest"); 5058 result = s.findInLine("est"); 5059 assertEquals("est", result); 5060 result = s.findInLine("est"); 5061 } 5062 5063 /** 5064 * @tests java.util.Scanner#skip(Pattern) 5065 */ 5066 public void test_skip_LPattern() { 5067 s = new Scanner("test"); 5068 try { 5069 s.skip((String) null); 5070 fail(); 5071 } catch (NullPointerException expected) { 5072 } 5073 5074 // If pattern does not match, NoSuchElementException will be thrown out. 5075 s = new Scanner("1234"); 5076 try { 5077 s.skip(Pattern.compile("\\p{Lower}")); 5078 fail(); 5079 } catch (NoSuchElementException expected) { 5080 } 5081 // Then, no matchResult will be thrown out. 5082 try { 5083 s.match(); 5084 fail(); 5085 } catch (IllegalStateException expected) { 5086 } 5087 5088 s.skip(Pattern.compile("\\p{Digit}")); 5089 MatchResult matchResult = s.match(); 5090 assertEquals(0, matchResult.start()); 5091 assertEquals(1, matchResult.end()); 5092 5093 s.skip(Pattern.compile("\\p{Digit}+")); 5094 matchResult = s.match(); 5095 assertEquals(1, matchResult.start()); 5096 assertEquals(4, matchResult.end()); 5097 5098 s.close(); 5099 try { 5100 s.skip(Pattern.compile("test")); 5101 fail(); 5102 } catch (IllegalStateException expected) { 5103 } 5104 5105 MockStringReader2Read reader = new MockStringReader2Read("test"); 5106 s = new Scanner(reader); 5107 try { 5108 s.skip(Pattern.compile("\\p{Digit}{4}")); 5109 fail(); 5110 } catch (NoSuchElementException expected) { 5111 } 5112 try { 5113 s.match(); 5114 fail(); 5115 } catch (IllegalStateException expected) { 5116 } 5117 s.skip(Pattern.compile("\\p{Digit}{3}\\p{Lower}")); 5118 matchResult = s.match(); 5119 assertEquals(0, matchResult.start()); 5120 assertEquals(4, matchResult.end()); 5121 5122 s.close(); 5123 try { 5124 s.skip((Pattern) null); 5125 fail(); 5126 } catch (IllegalStateException expected) { 5127 } 5128 5129 StringBuilder stringBuilder = new StringBuilder(); 5130 char [] chars = new char[1024]; 5131 Arrays.fill(chars, 'a'); 5132 stringBuilder.append(chars); 5133 stringBuilder.append('3'); 5134 s = new Scanner(stringBuilder.toString()); 5135 s.skip(Pattern.compile("\\p{Lower}+\\p{Digit}")); 5136 matchResult = s.match(); 5137 assertEquals(0, matchResult.start()); 5138 assertEquals(1025, matchResult.end()); 5139 5140 // Large amount of input may be cached 5141 chars = new char[102400]; 5142 Arrays.fill(chars, 'a'); 5143 stringBuilder = new StringBuilder(); 5144 stringBuilder.append(chars); 5145 s = new Scanner(stringBuilder.toString()); 5146 s.skip(Pattern.compile(".*")); 5147 matchResult = s.match(); 5148 assertEquals(0, matchResult.start()); 5149 assertEquals(102400, matchResult.end()); 5150 5151 // skip something without risking a NoSuchElementException 5152 s.skip(Pattern.compile("[ \t]*")); 5153 matchResult = s.match(); 5154 assertEquals(102400, matchResult.start()); 5155 assertEquals(102400, matchResult.end()); 5156 } 5157 5158 /** 5159 * @tests java.util.Scanner#skip(String) 5160 */ 5161 public void test_skip_LString() { 5162 s = new Scanner("test"); 5163 try { 5164 s.skip((String) null); 5165 fail(); 5166 } catch (NullPointerException expected) { 5167 } 5168 } 5169 5170 /** 5171 * @throws IOException 5172 * @tests java.util.Scanner#nextDouble() 5173 */ 5174 public void test_nextDouble() throws IOException { 5175 s = new Scanner("123 45\u0666. 123.4 .123 "); 5176 s.useLocale(Locale.ENGLISH); 5177 assertEquals(123.0, s.nextDouble()); 5178 assertEquals(456.0, s.nextDouble()); 5179 assertEquals(123.4, s.nextDouble()); 5180 assertEquals(0.123, s.nextDouble()); 5181 try { 5182 s.nextDouble(); 5183 fail(); 5184 } catch (NoSuchElementException expected) { 5185 } 5186 5187 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 5188 s.useLocale(Locale.ENGLISH); 5189 assertEquals(123.4, s.nextDouble()); 5190 assertEquals(-456.7, s.nextDouble()); 5191 assertEquals(123456.789, s.nextDouble()); 5192 try { 5193 s.nextDouble(); 5194 fail(); 5195 } catch (InputMismatchException expected) { 5196 } 5197 5198 // Scientific notation 5199 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 5200 s.useLocale(Locale.ENGLISH); 5201 assertEquals(1.234E12, s.nextDouble()); 5202 assertEquals(-4.567E14, s.nextDouble()); 5203 assertEquals(1.23456789E-5, s.nextDouble()); 5204 5205 s = new Scanner("NaN Infinity -Infinity"); 5206 assertEquals(Double.NaN, s.nextDouble()); 5207 assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 5208 assertEquals(Double.NEGATIVE_INFINITY, s.nextDouble()); 5209 5210 //The following test case fails on RI 5211 s=new Scanner("\u221e"); 5212 s.useLocale(Locale.ENGLISH); 5213 assertEquals(Double.POSITIVE_INFINITY, s.nextDouble()); 5214 5215 String str=String.valueOf(Double.MAX_VALUE*2); 5216 s=new Scanner(str); 5217 assertEquals(Double.POSITIVE_INFINITY,s.nextDouble()); 5218 5219 /* 5220 * Different locale can only recognize corresponding locale sensitive 5221 * string. ',' is used in many locales as group separator. 5222 */ 5223 s = new Scanner("23,456 23,456"); 5224 s.useLocale(Locale.ENGLISH); 5225 assertEquals(23456.0, s.nextDouble()); 5226 s.useLocale(Locale.GERMANY); 5227 assertEquals(23.456, s.nextDouble()); 5228 5229 s = new Scanner("23.456 23.456"); 5230 s.useLocale(Locale.ENGLISH); 5231 assertEquals(23.456, s.nextDouble()); 5232 s.useLocale(Locale.GERMANY); 5233 assertEquals(23456.0, s.nextDouble()); 5234 5235 s = new Scanner("23,456.7 23.456,7"); 5236 s.useLocale(Locale.ENGLISH); 5237 assertEquals(23456.7, s.nextDouble()); 5238 s.useLocale(Locale.GERMANY); 5239 assertEquals(23456.7, s.nextDouble()); 5240 5241 s = new Scanner("-123.4"); 5242 s.useLocale(Locale.ENGLISH); 5243 assertEquals(-123.4, s.nextDouble()); 5244 } 5245 5246 /** 5247 * @throws IOException 5248 * @tests java.util.Scanner#nextBigDecimal() 5249 */ 5250 public void test_nextBigDecimal() throws IOException { 5251 s = new Scanner("123 45\u0666. 123.4 .123 "); 5252 s.useLocale(Locale.ENGLISH); 5253 assertEquals(new BigDecimal("123"), s.nextBigDecimal()); 5254 assertEquals(new BigDecimal("456"), s.nextBigDecimal()); 5255 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 5256 assertEquals(new BigDecimal("0.123"), s.nextBigDecimal()); 5257 try { 5258 s.nextBigDecimal(); 5259 fail(); 5260 } catch (NoSuchElementException expected) { 5261 } 5262 5263 s = new Scanner("+123.4 -456.7 123,456.789 0.1\u06623,4"); 5264 s.useLocale(Locale.ENGLISH); 5265 assertEquals(new BigDecimal("123.4"), s.nextBigDecimal()); 5266 assertEquals(new BigDecimal("-456.7"), s.nextBigDecimal()); 5267 assertEquals(new BigDecimal("123456.789"), s.nextBigDecimal()); 5268 try { 5269 s.nextBigDecimal(); 5270 fail(); 5271 } catch (InputMismatchException expected) { 5272 } 5273 5274 // Scientific notation 5275 s = new Scanner("+123.4E10 -456.7e+12 123,456.789E-10"); 5276 s.useLocale(Locale.ENGLISH); 5277 assertEquals(new BigDecimal("1.234E12"), s.nextBigDecimal()); 5278 assertEquals(new BigDecimal("-4.567E14"), s.nextBigDecimal()); 5279 assertEquals(new BigDecimal("1.23456789E-5"), s.nextBigDecimal()); 5280 5281 s = new Scanner("NaN"); 5282 try { 5283 s.nextBigDecimal(); 5284 fail(); 5285 } catch (InputMismatchException expected) { 5286 } 5287 5288 /* 5289 * Different locale can only recognize corresponding locale sensitive 5290 * string. ',' is used in many locales as group separator. 5291 */ 5292 s = new Scanner("23,456 23,456"); 5293 s.useLocale(Locale.ENGLISH); 5294 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 5295 s.useLocale(Locale.GERMANY); 5296 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 5297 5298 s = new Scanner("23.456 23.456"); 5299 s.useLocale(Locale.ENGLISH); 5300 assertEquals(new BigDecimal("23.456"), s.nextBigDecimal()); 5301 s.useLocale(Locale.GERMANY); 5302 assertEquals(new BigDecimal("23456"), s.nextBigDecimal()); 5303 5304 s = new Scanner("23,456.7"); 5305 s.useLocale(Locale.ENGLISH); 5306 assertEquals(new BigDecimal("23456.7"), s.nextBigDecimal()); 5307 5308 s = new Scanner("-123.4"); 5309 s.useLocale(Locale.ENGLISH); 5310 assertEquals(new BigDecimal("-123.4"), s.nextBigDecimal()); 5311 } 5312 5313 /** 5314 * @tests java.util.Scanner#toString() 5315 */ 5316 public void test_toString() { 5317 s = new Scanner("test"); 5318 assertNotNull(s.toString()); 5319 } 5320 5321 /** 5322 * @tests java.util.Scanner#nextLine() 5323 */ 5324 public void test_nextLine() { 5325 s = new Scanner(""); 5326 s.close(); 5327 try { 5328 s.nextLine(); 5329 fail(); 5330 } catch (IllegalStateException expected) { 5331 } 5332 5333 s = new Scanner("test\r\ntest"); 5334 String result = s.nextLine(); 5335 assertEquals("test", result); 5336 MatchResult matchResult = s.match(); 5337 assertEquals(0, matchResult.start()); 5338 assertEquals(6, matchResult.end()); 5339 5340 s = new Scanner("\u0085"); 5341 result = s.nextLine(); 5342 assertEquals("", result); 5343 matchResult = s.match(); 5344 assertEquals(0, matchResult.start()); 5345 assertEquals(1, matchResult.end()); 5346 5347 s = new Scanner("\u2028"); 5348 result = s.nextLine(); 5349 assertEquals("", result); 5350 matchResult = s.match(); 5351 assertEquals(0, matchResult.start()); 5352 assertEquals(1, matchResult.end()); 5353 5354 s = new Scanner("\u2029"); 5355 result = s.nextLine(); 5356 assertEquals("", result); 5357 matchResult = s.match(); 5358 assertEquals(0, matchResult.start()); 5359 assertEquals(1, matchResult.end()); 5360 5361 s = new Scanner(""); 5362 try { 5363 result = s.nextLine(); 5364 fail(); 5365 } catch (NoSuchElementException expected) { 5366 } 5367 try { 5368 s.match(); 5369 fail(); 5370 } catch (IllegalStateException expected) { 5371 } 5372 5373 s = new Scanner("Ttest"); 5374 result = s.nextLine(); 5375 assertEquals("Ttest", result); 5376 matchResult = s.match(); 5377 assertEquals(0, matchResult.start()); 5378 assertEquals(5, matchResult.end()); 5379 5380 s = new Scanner("\r\n"); 5381 result = s.nextLine(); 5382 assertEquals("", result); 5383 matchResult = s.match(); 5384 assertEquals(0, matchResult.start()); 5385 assertEquals(2, matchResult.end()); 5386 5387 char[] chars = new char[1024]; 5388 Arrays.fill(chars, 'a'); 5389 StringBuilder stringBuilder = new StringBuilder(); 5390 stringBuilder.append(chars); 5391 chars = new char[] { '+', '-' }; 5392 stringBuilder.append(chars); 5393 stringBuilder.append("\u2028"); 5394 s = new Scanner(stringBuilder.toString()); 5395 result = s.nextLine(); 5396 5397 assertEquals(stringBuilder.toString().substring(0, 1026), result); 5398 matchResult = s.match(); 5399 assertEquals(0, matchResult.start()); 5400 assertEquals(1027, matchResult.end()); 5401 5402 chars = new char[1023]; 5403 Arrays.fill(chars, 'a'); 5404 stringBuilder = new StringBuilder(); 5405 stringBuilder.append(chars); 5406 stringBuilder.append("\r\n"); 5407 s = new Scanner(stringBuilder.toString()); 5408 result = s.nextLine(); 5409 5410 assertEquals(stringBuilder.toString().substring(0, 1023), result); 5411 matchResult = s.match(); 5412 assertEquals(0, matchResult.start()); 5413 assertEquals(1025, matchResult.end()); 5414 5415 s = new Scanner(" "); 5416 result = s.nextLine(); 5417 assertEquals(" ", result); 5418 5419 s = new Scanner("test\n\n\n"); 5420 result = s.nextLine(); 5421 assertEquals("test", result); 5422 matchResult = s.match(); 5423 assertEquals(0, matchResult.start()); 5424 assertEquals(5, matchResult.end()); 5425 result = s.nextLine(); 5426 matchResult = s.match(); 5427 assertEquals(5, matchResult.start()); 5428 assertEquals(6, matchResult.end()); 5429 5430 s = new Scanner("\n\n\n"); 5431 result = s.nextLine(); 5432 assertEquals("", result); 5433 matchResult = s.match(); 5434 assertEquals(0, matchResult.start()); 5435 assertEquals(1, matchResult.end()); 5436 result = s.nextLine(); 5437 matchResult = s.match(); 5438 assertEquals(1, matchResult.start()); 5439 assertEquals(2, matchResult.end()); 5440 5441 s = new Scanner("123 test\n "); 5442 int value = s.nextInt(); 5443 assertEquals(123, value); 5444 5445 result = s.nextLine(); 5446 assertEquals(" test", result); 5447 5448 s = new Scanner("test\n "); 5449 result = s.nextLine(); 5450 assertEquals("test", result); 5451 5452 // Regression test for Harmony-4774 5453 class CountReadable implements Readable { 5454 int counter = 0; 5455 public int read(CharBuffer charBuffer) throws IOException { 5456 counter++; 5457 charBuffer.append("hello\n"); 5458 return 6; 5459 } 5460 } 5461 CountReadable cr = new CountReadable(); 5462 s = new Scanner(cr); 5463 result = s.nextLine(); 5464 // We expect read() to be called only once, otherwise we see the problem 5465 // when reading from System.in described in Harmony-4774 5466 assertEquals(1, cr.counter); 5467 assertEquals("hello", result); 5468 } 5469 5470 /** 5471 * @tests java.util.Scanner#hasNextLine() 5472 */ 5473 public void test_hasNextLine() { 5474 s = new Scanner(""); 5475 s.close(); 5476 try { 5477 s.hasNextLine(); 5478 fail(); 5479 } catch (IllegalStateException expected) { 5480 } 5481 5482 s = new Scanner("test\r\ntest"); 5483 boolean result = s.hasNextLine(); 5484 assertTrue(result); 5485 MatchResult matchResult = s.match(); 5486 assertEquals(0, matchResult.start()); 5487 assertEquals(6, matchResult.end()); 5488 5489 s = new Scanner("\u0085"); 5490 result = s.hasNextLine(); 5491 assertTrue(result); 5492 matchResult = s.match(); 5493 assertEquals(0, matchResult.start()); 5494 assertEquals(1, matchResult.end()); 5495 5496 s = new Scanner("\u2028"); 5497 result = s.hasNextLine(); 5498 assertTrue(result); 5499 matchResult = s.match(); 5500 assertEquals(0, matchResult.start()); 5501 assertEquals(1, matchResult.end()); 5502 5503 s = new Scanner("\u2029"); 5504 result = s.hasNextLine(); 5505 assertTrue(result); 5506 matchResult = s.match(); 5507 assertEquals(0, matchResult.start()); 5508 assertEquals(1, matchResult.end()); 5509 5510 s = new Scanner("test\n"); 5511 assertTrue(s.hasNextLine()); 5512 matchResult = s.match(); 5513 assertEquals(0, matchResult.start()); 5514 assertEquals(5, matchResult.end()); 5515 5516 char[] chars = new char[2048]; 5517 Arrays.fill(chars, 'a'); 5518 StringBuilder stringBuilder = new StringBuilder(); 5519 stringBuilder.append(chars); 5520 s = new Scanner(stringBuilder.toString()); 5521 result = s.hasNextLine(); 5522 assertTrue(result); 5523 5524 matchResult = s.match(); 5525 assertEquals(0, matchResult.start()); 5526 assertEquals(2048, matchResult.end()); 5527 5528 s = new Scanner("\n\n\n"); 5529 assertTrue(s.hasNextLine()); 5530 matchResult = s.match(); 5531 assertEquals(0, matchResult.start()); 5532 assertEquals(1, matchResult.end()); 5533 5534 // The scanner will not advance any input. 5535 assertTrue(s.hasNextLine()); 5536 matchResult = s.match(); 5537 assertEquals(0, matchResult.start()); 5538 assertEquals(1, matchResult.end()); 5539 } 5540 5541 public void test_hasNextLine_sequence() throws IOException { 5542 final PipedInputStream pis = new PipedInputStream(); 5543 final PipedOutputStream pos = new PipedOutputStream(); 5544 final Scanner scanner = new Scanner(pis); 5545 pis.connect(pos); 5546 final List<String> result = new ArrayList<String>(); 5547 Thread thread = new Thread(new Runnable() { 5548 public void run() { 5549 while (scanner.hasNextLine()) { 5550 String line = scanner.nextLine(); 5551 result.add(line); 5552 } 5553 } 5554 }); 5555 thread.start(); 5556 for (int index = 0; index < 5; index++) { 5557 String line = "line" + index + "\n"; 5558 pos.write(line.getBytes()); 5559 pos.flush(); 5560 try { 5561 Thread.sleep(1000); 5562 } catch (InterruptedException ignored) { 5563 } 5564 assertEquals(index + 1, result.size()); 5565 } 5566 pis.close(); 5567 pos.close(); 5568 try { 5569 thread.join(1000); 5570 } catch (InterruptedException ignored) { 5571 } 5572 assertFalse(scanner.hasNextLine()); 5573 } 5574 5575 protected void setUp() throws Exception { 5576 super.setUp(); 5577 5578 server = new ServerSocket(0); 5579 address = new InetSocketAddress("127.0.0.1", server.getLocalPort()); 5580 5581 client = SocketChannel.open(); 5582 client.connect(address); 5583 serverSocket = server.accept(); 5584 5585 os = serverSocket.getOutputStream(); 5586 } 5587 5588 protected void tearDown() throws Exception { 5589 super.tearDown(); 5590 5591 try { 5592 serverSocket.close(); 5593 } catch (Exception ignored) { 5594 } 5595 try { 5596 client.close(); 5597 } catch (Exception ignored) { 5598 } 5599 try { 5600 server.close(); 5601 } catch (Exception ignored) { 5602 } 5603 } 5604 5605 // http://code.google.com/p/android/issues/detail?id=57050 5606 public void testPerformance() throws Exception { 5607 int count = 100000; 5608 5609 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 5610 BufferedWriter out = new BufferedWriter(new OutputStreamWriter(baos)); 5611 for (int i = 0; i < count; ++i) { 5612 out.write(Integer.toString(123) + " "); 5613 } 5614 out.close(); 5615 5616 ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); 5617 bais.mark(-1); 5618 5619 Scanner s = new Scanner(new BufferedReader(new InputStreamReader(bais))); 5620 for (int i = 0; i < count; ++i) { 5621 if (s.nextInt() != 123) { 5622 fail(); 5623 } 5624 } 5625 5626 bais.reset(); 5627 s = new Scanner(new BufferedReader(new InputStreamReader(bais))); 5628 for (int i = 0; i < count; ++i) { 5629 if (s.nextFloat() != 123.0) { 5630 fail(); 5631 } 5632 } 5633 } 5634} 5635