Path: blob/master/test/jdk/java/util/Scanner/ScanTest.java
41149 views
/*1* Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 4313885 4926319 4927634 5032610 5032622 5049968 5059533 6223711 6277261 6269946 628882326* 8072722 8139414 8166261 817269527* @summary Basic tests of java.util.Scanner methods28* @key randomness29* @modules jdk.localedata30* @run main/othervm ScanTest31*/3233import java.io.*;34import java.math.*;35import java.nio.*;36import java.text.*;37import java.util.*;38import java.util.function.BiConsumer;39import java.util.function.Consumer;40import java.util.regex.*;41import java.util.stream.*;4243public class ScanTest {4445private static boolean failure = false;46private static int failCount = 0;47private static int NUM_SOURCE_TYPES = 2;48private static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");4950public static void main(String[] args) throws Exception {51Locale defaultLocale = Locale.getDefault();52try {53// Before we have resource to improve the test to be ready for54// arbitrary locale, force the default locale to be ROOT for now.55Locale.setDefault(Locale.US);5657skipTest();58findInLineTest();59findWithinHorizonTest();60findInEmptyLineTest();61removeTest();62fromFileTest();63ioExceptionTest();64matchTest();65delimiterTest();66boundaryDelimTest();67useLocaleTest();68closeTest();69cacheTest();70cacheTest2();71nonASCIITest();72resetTest();73streamCloseTest();74streamComodTest();75outOfRangeRadixTest();7677for (int j = 0; j < NUM_SOURCE_TYPES; j++) {78hasNextTest(j);79nextTest(j);80hasNextPatternTest(j);81nextPatternTest(j);82booleanTest(j);83byteTest(j);84shortTest(j);85intTest(j);86longTest(j);87floatTest(j);88doubleTest(j);89integerPatternTest(j);90floatPatternTest(j);91bigIntegerPatternTest(j);92bigDecimalPatternTest(j);93hasNextLineTest(j);94nextLineTest(j);95singleDelimTest(j);96}9798// Examples99//example1();100//example2();101//example3();102103// Usage cases104useCase1();105useCase2();106useCase3();107useCase4();108useCase5();109110if (failure)111throw new RuntimeException("Failure in the scanning tests.");112else113System.err.println("OKAY: All tests passed.");114} finally {115// restore the default locale116Locale.setDefault(defaultLocale);117}118}119120public static void useCase1() throws Exception {121try (Scanner sc = new Scanner(inputFile)) {122sc.findWithinHorizon("usage case 1", 0);123String[] names = new String[4];124for (int i=0; i<4; i++) {125while (sc.hasNextFloat())126sc.nextFloat();127names[i] = sc.next();128sc.nextLine();129}130if (!names[0].equals("Frank"))131failCount++;132if (!names[1].equals("Joe"))133failCount++;134if (!names[2].equals("Mary"))135failCount++;136if (!names[3].equals("Michelle"))137failCount++;138}139report("Use case 1");140}141142public static void useCase2() throws Exception {143try (Scanner sc = new Scanner(inputFile).useDelimiter("-")) {144String testDataTag = sc.findWithinHorizon("usage case 2\n", 0);145if (!testDataTag.equals("usage case 2\n"))146failCount++;147if (!sc.next().equals("cat"))148failCount++;149if (sc.nextInt() != 9)150failCount++;151if (!sc.next().equals("dog"))152failCount++;153if (sc.nextInt() != 6)154failCount++;155if (!sc.next().equals("pig"))156failCount++;157if (sc.nextInt() != 2)158failCount++;159if (!sc.next().equals(""))160failCount++;161if (sc.nextInt() != 5)162failCount++;163}164report("Use case 2");165}166167public static void useCase3() throws Exception {168try (Scanner sc = new Scanner(inputFile)) {169String testDataTag = sc.findWithinHorizon("usage case 3\n", 0);170if (!testDataTag.equals("usage case 3\n"))171failCount++;172Pattern tagPattern = Pattern.compile("@[a-z]+");173Pattern endPattern = Pattern.compile("\\*\\/");174String tag;175String end = sc.findInLine(endPattern);176177while (end == null) {178if ((tag = sc.findInLine(tagPattern)) != null) {179String text = sc.nextLine();180text = text.substring(0, text.length() - 1);181//System.out.println(text);182} else {183sc.nextLine();184}185end = sc.findInLine(endPattern);186}187}188report("Use case 3");189}190191public static void useCase4() throws Exception {192try (Scanner sc = new Scanner(inputFile)) {193String testDataTag = sc.findWithinHorizon("usage case 4\n", 0);194if (!testDataTag.equals("usage case 4\n"))195failCount++;196197// Read some text parts of four hrefs198String[] expected = { "Diffs", "Sdiffs", "Old", "New" };199for (int i=0; i<4; i++) {200sc.findWithinHorizon("<a href", 1000);201sc.useDelimiter("[<>\n]+");202sc.next();203String textOfRef = sc.next();204if (!textOfRef.equals(expected[i]))205failCount++;206}207// Read some html tags using < and > as delimiters208if (!sc.next().equals("/a"))209failCount++;210if (!sc.next().equals("b"))211failCount++;212213// Scan some html tags using skip and next214Pattern nonTagStart = Pattern.compile("[^<]+");215Pattern tag = Pattern.compile("<[^>]+?>");216Pattern spotAfterTag = Pattern.compile("(?<=>)");217String[] expected2 = { "</b>", "<p>", "<ul>", "<li>" };218sc.useDelimiter(spotAfterTag);219int tagsFound = 0;220while (tagsFound < 4) {221if (!sc.hasNext(tag)) {222// skip text between tags223sc.skip(nonTagStart);224}225String tagContents = sc.next(tag);226if (!tagContents.equals(expected2[tagsFound]))227failCount++;228tagsFound++;229}230}231232report("Use case 4");233}234235public static void useCase5() throws Exception {236try (Scanner sc = new Scanner(inputFile)) {237String testDataTag = sc.findWithinHorizon("usage case 5\n", 0);238if (!testDataTag.equals("usage case 5\n"))239failCount++;240241sc.findWithinHorizon("Share Definitions", 0);242sc.nextLine();243sc.next("\\[([a-z]+)\\]");244String shareName = sc.match().group(1);245if (!shareName.equals("homes"))246failCount++;247248String[] keys = { "comment", "browseable", "writable", "valid users" };249String[] vals = { "Home Directories", "no", "yes", "%S" };250for (int i=0; i<4; i++) {251sc.useDelimiter("=");252String key = sc.next().trim();253if (!key.equals(keys[i]))254failCount++;255sc.skip("[ =]+");256sc.useDelimiter("\n");257String value = sc.next();258if (!value.equals(vals[i]))259failCount++;260sc.nextLine();261}262}263264report("Use case 5");265}266267public static void nonASCIITest() throws Exception {268String yourBasicTibetanNumberZero = "\u0f20";269String yourBasicTibetanFloatingNumber = "\u0f23.\u0f27";270String weirdMixtureOfTibetanAndASCII = "\u0f23.7";271String weirdMixtureOfASCIIAndTibetan = "3.\u0f27";272Scanner sc = new Scanner(yourBasicTibetanNumberZero);273int i = sc.nextInt();274if (i != 0)275failCount++;276sc = new Scanner(yourBasicTibetanFloatingNumber);277float f = sc.nextFloat();278if (f != Float.parseFloat("3.7"))279failCount++;280sc = new Scanner(weirdMixtureOfTibetanAndASCII);281f = sc.nextFloat();282if (f != Float.parseFloat("3.7"))283failCount++;284sc = new Scanner(weirdMixtureOfASCIIAndTibetan);285f = sc.nextFloat();286if (f != Float.parseFloat("3.7"))287failCount++;288report("Scanning non ASCII digits");289}290291public static void findWithinHorizonTest() throws Exception {292// Test with a string source293Scanner sc = new Scanner("dog cat cat dog cat");294try {295sc.findWithinHorizon("dog", -1);296failCount++;297} catch (IllegalArgumentException iae) {298// Correct result299}300if (sc.findWithinHorizon("dog", 2) != null)301failCount++;302if (!sc.findWithinHorizon("dog", 3).equals("dog"))303failCount++;304if (sc.findWithinHorizon("cat", 4) != null)305failCount++;306if (!sc.findWithinHorizon("cat", 5).equals("cat"))307failCount++;308if (sc.findWithinHorizon("cat", 7) != null)309failCount++;310if (sc.findWithinHorizon("dog", 7) != null)311failCount++;312if (!sc.findWithinHorizon("cat", 0).equals("cat"))313failCount++;314if (!sc.findWithinHorizon("dog", 0).equals("dog"))315failCount++;316if (!sc.findWithinHorizon("cat", 0).equals("cat"))317failCount++;318319// Test with a stream source320StutteringInputStream stutter = new StutteringInputStream();321for (int index=0; index<stutter.length(); index++) {322//System.out.println("index is now "+index);323sc = new Scanner(stutter);324String word = stutter.wordInIndex(index);325if (word != null) {326String result = sc.findWithinHorizon(word, index);327if ((result == null) || (!result.equals(word)))328failCount++;329}330stutter.reset();331word = stutter.wordBeyondIndex(index);332sc = new Scanner(stutter);333String result = sc.findWithinHorizon(word, index);334if ((result != null) && (index > 0))335failCount++;336stutter.reset();337}338339// We must loop to let StutteringInputStream do its magic340for (int j=0; j<10; j++) {341// An anchor at the end of stream should work342stutter.reset();343sc = new Scanner(stutter);344String result = sc.findWithinHorizon("phant$", 0);345if (!result.equals("phant"))346failCount++;347stutter.reset();348sc = new Scanner(stutter);349result = sc.findWithinHorizon("phant$", 54);350if (!result.equals("phant"))351failCount++;352// An anchor at the end of horizon should not353stutter.reset();354sc = new Scanner(stutter);355result = sc.findWithinHorizon("brummer$", 7);356if (result != null)357failCount++;358// An anchor at start should work359stutter.reset();360sc = new Scanner(stutter);361result = sc.findWithinHorizon("^brummer", 0);362if (!result.equals("brummer"))363failCount++;364}365366report("Find to horizon test");367}368369// StutteringInputStream returns 1 to 3 characters at a time370static class StutteringInputStream implements Readable {371StutteringInputStream() {372text = "brummer hisser tort zardzard rantrant caimagator phant";373datalen = 54;374}375StutteringInputStream(String text) {376this.text = text;377datalen = text.length();378}379Random generator = new Random();380String text;381int datalen;382int index = 0;383public int length() {384return datalen;385}386public void reset() {387index = 0;388}389public String wordInIndex(int index) {390if (index < 7) return null;391if (index < 14) return "brummer";392if (index < 19) return "hisser";393if (index < 28) return "tort";394if (index < 37) return "zardzard";395if (index < 48) return "rantrant";396return "caimagator";397}398public String wordBeyondIndex(int index) {399if (index < 7) return "brummer";400if (index < 14) return "hisser";401if (index < 19) return "tort";402if (index < 28) return "zardzard";403if (index < 37) return "rantrant";404if (index < 48) return "caimagator";405return "phantphant";406}407public int read(java.nio.CharBuffer target) throws IOException {408if (index > datalen-1)409return -1; // EOS410int len = target.remaining();411if (len > 4) // return 1 to 3 characters412len = generator.nextInt(3) + 1;413while ((index + len) > datalen)414len--;415for (int i=0; i<len; i++)416target.put(text.charAt(index++));417return len;418}419}420421public static void hasNextLineTest(int sourceType) throws Exception {422Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);423if (!sc.hasNextLine()) failCount++;424if (!sc.nextLine().equals("1")) failCount++;425if (!sc.hasNextLine()) failCount++;426if (sc.nextInt() != 2) failCount++;427if (!sc.hasNextLine()) failCount++;428if (!sc.nextLine().equals("")) failCount++;429if (!sc.hasNextLine()) failCount++;430if (sc.nextInt() != 3) failCount++;431if (!sc.hasNextLine()) failCount++;432if (!sc.nextLine().equals(" 3")) failCount++;433if (!sc.hasNextLine()) failCount++;434if (sc.nextInt() != 4) failCount++;435if (!sc.hasNextLine()) failCount++;436if (sc.nextInt() != 4) failCount++;437if (!sc.hasNextLine()) failCount++;438if (!sc.nextLine().equals(" 4")) failCount++;439if (!sc.hasNextLine()) failCount++;440if (!sc.nextLine().equals("5")) failCount++;441if (sc.hasNextLine()) failCount++;442sc = new Scanner("blah blah blah blah blah blah");443if (!sc.hasNextLine()) failCount++;444if (!sc.nextLine().equals("blah blah blah blah blah blah"))445failCount++;446if (sc.hasNextLine()) failCount++;447448// Go through all the lines in a file449try (Scanner sc2 = new Scanner(inputFile)) {450String lastLine = "blah";451while (sc2.hasNextLine())452lastLine = sc2.nextLine();453if (!lastLine.equals("# Data for usage case 6")) failCount++;454}455456report("Has next line test");457}458459public static void nextLineTest(int sourceType) throws Exception {460Scanner sc = scannerFor("1\n2\n3 3\r\n4 4 4\r5", sourceType);461if (!sc.nextLine().equals("1"))462failCount++;463if (sc.nextInt() != 2)464failCount++;465if (!sc.nextLine().equals(""))466failCount++;467if (sc.nextInt() != 3)468failCount++;469if (!sc.nextLine().equals(" 3"))470failCount++;471if (sc.nextInt() != 4)472failCount++;473if (sc.nextInt() != 4)474failCount++;475if (!sc.nextLine().equals(" 4"))476failCount++;477if (!sc.nextLine().equals("5"))478failCount++;479sc = new Scanner("blah blah blah blah blah blah");480if (!sc.nextLine().equals("blah blah blah blah blah blah"))481failCount++;482report("Next line test");483}484485public static void singleDelimTest(int sourceType) throws Exception {486Scanner sc = scannerFor("12 13 14 15 16 17 ",487sourceType);488sc.useDelimiter(" ");489for (int i=0; i<6; i++) {490int j = sc.nextInt();491if (j != 12 + i)492failCount++;493for (int k=0; k<i; k++) {494String empty = sc.next();495if (!empty.equals(""))496failCount++;497}498}499report("Single delim test");500}501502private static void append(StringBuilder sb, char c, int n) {503for (int i = 0; i < n; i++) {504sb.append(c);505}506}507508public static void boundaryDelimTest() throws Exception {509// 8139414510int i = 1019;511StringBuilder sb = new StringBuilder();512sb.append("--;");513for (int j = 0; j < 1019; ++j) {514sb.append(j%10);515}516sb.append("-;-");517String text = sb.toString();518try (Scanner scanner = new Scanner(text)) {519scanner.useDelimiter("-;(-)?");520while (scanner.hasNext()) {521scanner.next();522}523} catch (NoSuchElementException e) {524System.out.println("Caught NoSuchElementException " + e);525e.printStackTrace();526failCount++;527}528529report("delim at boundary test");530}531532/*533* The hasNextPattern caches a match of a pattern called the regular cache534* The hasNextType caches a match of that type called the type cache535* Any next must clear the caches whether it uses them or not, because536* it advances past a token thus invalidating any cached token; any537* hasNext must set a cache to what it finds.538*/539public static void cacheTest() throws Exception {540// Test clearing of the type cache541Scanner scanner = new Scanner("777 dog");542scanner.hasNextInt();543scanner.findInLine("777");544try {545scanner.nextInt();546System.out.println("type cache not cleared by find");547failCount++;548} catch (InputMismatchException ime) {549// Correct550}551552scanner = new Scanner("777 dog");553scanner.hasNextInt();554scanner.skip("777");555try {556scanner.nextInt();557System.out.println("type cache not cleared by skip");558failCount++;559} catch (InputMismatchException ime) {560// Correct561}562563// Test clearing of the regular cache564scanner = new Scanner("777 dog");565scanner.hasNext("777");566scanner.findInLine("777");567try {568scanner.next("777");569System.out.println("regular cache not cleared by find");570failCount++;571} catch (InputMismatchException ime) {572// Correct573}574575// Test two primitive next clearing of type cache576scanner = new Scanner("777 dog");577scanner.hasNextInt();578scanner.nextLong();579try {580scanner.nextInt();581System.out.println("type cache not cleared by primitive next");582failCount++;583} catch (InputMismatchException ime) {584// Correct585}586587// Test using both of them, type first588scanner = new Scanner("777 dog");589scanner.hasNext("777");590scanner.nextInt();591try {592scanner.next("777");593System.out.println("regular cache not cleared by primitive next");594failCount++;595} catch (InputMismatchException ime) {596// Correct597}598599// Test using both of them, regular first600scanner = new Scanner("777 dog");601scanner.hasNext("777");602scanner.hasNextInt();603scanner.next("777");604try {605scanner.nextInt();606System.out.println("type cache not cleared by regular next");607failCount++;608} catch (InputMismatchException ime) {609// Correct610}611report("Cache test");612}613614/*615* The hasNext<IntegerType>(radix) method caches a matched integer type616* with specified radix for the next next<IntegerType>(radix) invoke.617* The cache value should not be used if the next<IntegerType>(radix)618* has different radix value with the last hasNext<IntegerType>(radix).619*/620public static void cacheTest2() throws Exception {621// Test clearing of the type cache622Scanner scanner = new Scanner("10");623scanner.hasNextByte(16);624if (scanner.nextByte(10) != 10) {625System.out.println("wrong radix cache is used");626failCount++;627}628scanner = new Scanner("10");629scanner.hasNextShort(16);630if (scanner.nextShort(10) != 10) {631System.out.println("wrong radix cache is used");632failCount++;633}634scanner = new Scanner("10");635scanner.hasNextInt(16);636if (scanner.nextInt(10) != 10) {637System.out.println("wrong radix cache is used");638failCount++;639}640scanner = new Scanner("10");641scanner.hasNextLong(16);642if (scanner.nextLong(10) != 10) {643System.out.println("wrong radix cache is used");644failCount++;645}646scanner = new Scanner("10");647scanner.hasNextBigInteger(16);648if (scanner.nextBigInteger(10).intValue() != 10) {649System.out.println("wrong radix cache is used");650failCount++;651}652report("Cache test2");653}654655656public static void closeTest() throws Exception {657Scanner sc = new Scanner("testing");658sc.close();659sc.ioException();660sc.delimiter();661sc.useDelimiter("blah");662sc.useDelimiter(Pattern.compile("blah"));663664for (Consumer<Scanner> method : methodList) {665try {666method.accept(sc);667failCount++;668} catch (IllegalStateException ise) {669// Correct670}671}672673report("Close test");674}675676static List<Consumer<Scanner>> methodList = Arrays.asList(677Scanner::hasNext,678Scanner::next,679sc -> sc.hasNext(Pattern.compile("blah")),680sc -> sc.next(Pattern.compile("blah")),681Scanner::hasNextBoolean,682Scanner::nextBoolean,683Scanner::hasNextByte,684Scanner::nextByte,685Scanner::hasNextShort,686Scanner::nextShort,687Scanner::hasNextInt,688Scanner::nextInt,689Scanner::hasNextLong,690Scanner::nextLong,691Scanner::hasNextFloat,692Scanner::nextFloat,693Scanner::hasNextDouble,694Scanner::nextDouble,695Scanner::hasNextBigInteger,696Scanner::nextBigInteger,697Scanner::hasNextBigDecimal,698Scanner::nextBigDecimal,699Scanner::hasNextLine,700Scanner::tokens,701sc -> sc.findAll(Pattern.compile("blah")),702sc -> sc.findAll("blah")703);704705public static void removeTest() throws Exception {706Scanner sc = new Scanner("testing");707try {708sc.remove();709failCount++;710} catch (UnsupportedOperationException uoe) {711// Correct result712}713report("Remove test");714}715716public static void delimiterTest() throws Exception {717Scanner sc = new Scanner("blah");718Pattern test = sc.delimiter();719if (!test.toString().equals("\\p{javaWhitespace}+"))720failCount++;721sc.useDelimiter("a");722test = sc.delimiter();723if (!test.toString().equals("a"))724failCount++;725sc.useDelimiter(Pattern.compile("b"));726test = sc.delimiter();727if (!test.toString().equals("b"))728failCount++;729report("Delimiter test");730}731732public static void ioExceptionTest() throws Exception {733Readable thrower = new ThrowingReadable();734Scanner sc = new Scanner(thrower);735try {736sc.nextInt();737failCount++;738} catch (NoSuchElementException nsee) {739// Correct result740}741Exception thrown = sc.ioException();742String detail = thrown.getMessage();743if (!detail.equals("ThrowingReadable always throws"))744failCount++;745746report("IOException test");747}748749public static void bigIntegerPatternTest(int sourceType) throws Exception {750Scanner sc = scannerFor("23 9223372036854775817", sourceType);751if (!sc.nextBigInteger().equals(BigInteger.valueOf(23)))752failCount++;753if (!sc.nextBigInteger().equals(new BigInteger(754"9223372036854775817", 10)))755failCount++;756757// Test another radix758sc = new Scanner("4a4 4A4").useRadix(16);759if (!sc.nextBigInteger().equals(new BigInteger("4a4", 16)))760failCount++;761if (!sc.nextBigInteger().equals(new BigInteger("4A4", 16)))762failCount++;763764// Test alternating radices765sc = new Scanner("12 4a4 14 4f4");766if (!sc.nextBigInteger(10).equals(new BigInteger("12", 10)))767failCount++;768if (!sc.nextBigInteger(16).equals(new BigInteger("4a4", 16)))769failCount++;770if (!sc.nextBigInteger(10).equals(new BigInteger("14", 10)))771failCount++;772if (!sc.nextBigInteger(16).equals(new BigInteger("4f4", 16)))773failCount++;774775// Test use of a lot of radices776for (int i=2; i<17; i++) {777sc = new Scanner("1111");778if (!sc.nextBigInteger(i).equals(new BigInteger("1111", i)))779failCount++;780}781782report("BigInteger pattern");783}784785public static void bigDecimalPatternTest(int sourceType) throws Exception {786Scanner sc = scannerFor("23 45.99 -45,067.444 3.4e10", sourceType);787if (!sc.nextBigDecimal().equals(BigDecimal.valueOf(23)))788failCount++;789if (!sc.nextBigDecimal().equals(new BigDecimal("45.99")))790failCount++;791if (!sc.nextBigDecimal().equals(new BigDecimal("-45067.444")))792failCount++;793if (!sc.nextBigDecimal().equals(new BigDecimal("3.4e10")))794failCount++;795report("BigDecimal pattern");796}797798public static void integerPatternTest(int sourceType) throws Exception {799String input =800"1 22 f FF Z -3 -44 123 1,200 -123 -3,400,000 5,40 ,500 ";801Scanner sc = scannerFor(input, sourceType);802integerPatternBody(sc);803CharBuffer cb = CharBuffer.wrap(input);804sc = new Scanner(cb);805integerPatternBody(sc);806report("Integer pattern");807}808809public static void integerPatternBody(Scanner sc) throws Exception {810if (sc.nextInt() != 1) failCount++;811if (sc.nextShort() != 22) failCount++;812if (sc.nextShort(16) != 15) failCount++;813if (sc.nextShort(16) != 255) failCount++;814if (sc.nextShort(36) != 35) failCount++;815if (!sc.hasNextInt()) failCount++;816if (sc.nextInt() != -3) failCount++;817if (sc.nextInt() != -44) failCount++;818if (sc.nextLong() != 123) failCount++;819if (!sc.hasNextInt()) failCount++;820if (sc.nextInt() != 1200) failCount++;821if (sc.nextInt() != -123) failCount++;822if (sc.nextInt() != -3400000) failCount++;823try {824sc.nextInt();825failCount++;826} catch (InputMismatchException ime) {827// Correct result828}829sc.next();830try {831sc.nextLong();832failCount++;833} catch (InputMismatchException ime) {834// Correct result835}836sc.next();837try {838sc.next();839failCount++;840} catch (InputMismatchException ime) {841failCount++;842} catch (NoSuchElementException nse) {843// Correct result844}845}846847public static void floatPatternTest(int sourceType) throws Exception {848String input =849"090.090 1 22.0 -3 -44.05 +.123 -.1234 -3400000 56,566.6 " +850"Infinity +Infinity -Infinity NaN -NaN +NaN 5.4.0 5-.00 ++6.07";851Scanner sc = scannerFor(input, sourceType);852floatPatternBody(sc);853CharBuffer cb = CharBuffer.wrap(input);854sc = new Scanner(cb);855floatPatternBody(sc);856report("Float pattern");857}858859public static void floatPatternBody(Scanner sc) throws Exception {860if (sc.nextFloat() != 090.090f) failCount++;861if (sc.nextFloat() != 1f) failCount++;862if (sc.nextFloat() != 22.0f) failCount++;863if (sc.nextDouble() != -3d) failCount++;864if (sc.nextDouble() != -44.05d) failCount++;865if (sc.nextFloat() != .123f) failCount++;866if (sc.nextFloat() != -.1234f) failCount++;867if (sc.nextDouble() != -3400000d) failCount++;868if (sc.nextDouble() != 56566.6d) failCount++;869if (sc.nextDouble() != Double.POSITIVE_INFINITY) failCount++;870if (sc.nextDouble() != Double.POSITIVE_INFINITY) failCount++;871if (sc.nextDouble() != Double.NEGATIVE_INFINITY) failCount++;872if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;873if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;874if (!Double.valueOf(sc.nextDouble()).isNaN()) failCount++;875try {876sc.nextFloat();877failCount++;878} catch (NoSuchElementException nse) {879// Correct result880}881try {882sc.nextDouble();883failCount++;884} catch (NoSuchElementException nse) {885// Correct result886}887try {888sc.nextDouble();889failCount++;890} catch (NoSuchElementException nse) {891// Correct result892}893}894895public static void fromFileTest() throws Exception {896File f = new File(System.getProperty("test.src", "."), "input.txt");897try (Scanner sc = new Scanner(f)) {898sc.useDelimiter("\n+");899String testDataTag = sc.findWithinHorizon("fromFileTest", 0);900if (!testDataTag.equals("fromFileTest"))901failCount++;902903int count = 0;904while (sc.hasNextLong()) {905long blah = sc.nextLong();906count++;907}908if (count != 7)909failCount++;910}911report("From file");912}913914private static void example1() throws Exception {915Scanner s = new Scanner("1 fish 2 fish red fish blue fish");916s.useDelimiter("\\s*fish\\s*");917List <String> results = new ArrayList<String>();918while (s.hasNext())919results.add(s.next());920System.out.println(results);921}922923private static void example2() throws Exception {924Scanner s = new Scanner("1 fish 2 fish red fish blue fish");925s.useDelimiter("\\s*fish\\s*");926System.out.println(s.nextInt());927System.out.println(s.nextInt());928System.out.println(s.next());929System.out.println(s.next());930}931932private static void example3() throws Exception {933Scanner s = new Scanner("1 fish 2 fish red fish blue fish");934s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");935for (int i=1; i<=s.match().groupCount(); i++)936System.out.println(s.match().group(i));937}938939private static void findInLineTest() throws Exception {940Scanner s = new Scanner("abc def ghi jkl mno");941Pattern letters = Pattern.compile("[a-z]+");942Pattern frogs = Pattern.compile("frogs");943String str = s.findInLine(letters);944if (!str.equals("abc"))945failCount++;946if (!s.hasNext(letters))947failCount++;948try {949str = s.findInLine(frogs);950} catch (NoSuchElementException nsee) {951// Correct952}953if (!s.hasNext())954failCount++;955if (!s.hasNext(letters))956failCount++;957str = s.findInLine(letters);958if (!str.equals("def"))959failCount++;960961report("Find patterns");962}963964private static void findInEmptyLineTest() throws Exception {965String eol = System.getProperty("line.separator");966Scanner s = new Scanner("line 1" + eol + "" + eol + "line 3" + eol);967int lineNo = 0;968while (s.hasNextLine()) {969lineNo++;970s.findInLine("3");971s.nextLine();972}973if (lineNo != 3)974failCount++;975report("findInEmptyLine test");976}977978private static void matchTest() throws Exception {979Scanner s = new Scanner("1 fish 2 fish red fish blue fish");980s.findInLine("(\\d+) fish (\\d+) fish (\\w+) fish (\\w+)");981982MatchResult result = s.match();983if (!result.group(1).equals("1"))984failCount++;985if (!result.group(2).equals("2"))986failCount++;987if (!result.group(3).equals("red"))988failCount++;989if (!result.group(4).equals("blue"))990failCount++;991992report("Match patterns");993}994995private static void skipTest() throws Exception {996Scanner s = new Scanner("abc def ghi jkl mno");997Pattern letters = Pattern.compile("[a-z]+");998Pattern spaceLetters = Pattern.compile(" [a-z]+");999Pattern frogs = Pattern.compile("frogs");1000try {1001s.skip(letters);1002} catch (NoSuchElementException ime) {1003failCount++;1004}1005String token = s.next(letters);1006if (!token.equals("def")) {1007System.out.println("expected def");1008System.out.println("I found "+token);1009failCount++;1010}1011try {1012s.skip(letters);1013failCount++;1014} catch (NoSuchElementException ime) {1015// Correct result1016}1017token = s.next(letters);1018if (!token.equals("ghi")) {1019System.out.println("expected ghi");1020System.out.println("I found "+token);1021failCount++;1022}1023try {1024s.skip(letters);1025failCount++;1026} catch (NoSuchElementException ime) {1027// Correct result because skip ignores delims1028}1029try {1030s.skip(spaceLetters);1031} catch (NoSuchElementException ime) {1032failCount++;1033}1034token = s.next(letters);1035if (!token.equals("mno")) {1036System.out.println("expected mno");1037System.out.println("I found "+token);1038failCount++;1039}1040try {1041s.skip(letters);1042failCount++;1043} catch (NoSuchElementException ime) {1044// Correct result1045}1046report("Skip patterns");1047}10481049private static void byteTest(int sourceType) throws Exception {1050String input = " 3 0 00 b -B 012 44 -55 12 127 129 -131 dog 0x12";1051Scanner s = scannerFor(input, sourceType);1052if (!s.hasNextByte()) failCount++;1053if (s.nextByte() != (byte)3) failCount++;1054if (!s.hasNextByte()) failCount++;1055if (s.nextByte() != (byte)0) failCount++;1056if (!s.hasNextByte()) failCount++;1057if (s.nextByte() != (byte)0) failCount++;1058if (!s.hasNextByte(16)) failCount++;1059if (s.nextByte(16) != (byte)11)failCount++;1060if (!s.hasNextByte(16)) failCount++;1061if (s.nextByte(16) != (byte)-11) failCount++;1062if (!s.hasNextByte()) failCount++;1063if (s.nextByte() != (byte)12) failCount++;1064if (!s.hasNextByte()) failCount++;1065if (s.nextByte() != (byte)44) failCount++;1066if (!s.hasNextByte()) failCount++;1067if (s.nextByte() != (byte)-55) failCount++;1068if (!s.hasNextByte()) failCount++;1069if (s.nextByte() != (byte)12) failCount++;1070if (!s.hasNextByte()) failCount++;1071if (s.nextByte() != (byte)127) failCount++;1072if (s.hasNextByte()) failCount++;10731074try {1075s.nextByte();1076failCount++;1077} catch (InputMismatchException ime) {1078// Correct result1079}1080if (s.hasNextByte()) failCount++;1081if (s.nextInt() != 129) failCount++;1082if (s.hasNextByte()) failCount++;1083try {1084s.nextByte();1085failCount++;1086} catch (InputMismatchException ime) {1087// Correct result1088}1089if (s.nextInt() != -131) failCount++;1090if (s.hasNextByte()) failCount++;1091try {1092s.nextByte();1093failCount++;1094} catch (InputMismatchException ime) {1095// Correct result1096}1097s.next(Pattern.compile("\\w+"));1098if (s.hasNextByte())1099failCount++;1100try {1101s.nextByte();1102failCount++;1103} catch (NoSuchElementException nsee) {1104// Correct result1105}1106s.next();1107if (s.hasNextByte())1108failCount++;1109try {1110byte bb = s.nextByte();1111failCount++;1112} catch (NoSuchElementException nsee) {1113// Correct result1114}1115report("Scan bytes");1116}11171118private static void shortTest(int sourceType) throws Exception {1119String input = " 017 22 00E -34 44,333 -53999 0x19 dog";1120Scanner s = scannerFor(input, sourceType);1121if (!s.hasNextShort()) failCount++;1122if (s.nextShort() != (short)17) failCount++;1123if (!s.hasNextShort()) failCount++;1124if (s.nextShort() != (short)22) failCount++;1125if (!s.hasNextShort(16)) failCount++;1126if (s.nextShort(16) != (short)14) failCount++;1127if (!s.hasNextShort()) failCount++;1128if (s.nextShort() != (short)-34) failCount++;1129for (int i=0; i<4; i++) {1130if (s.hasNextShort())1131failCount++;1132try {1133s.nextShort();1134failCount++;1135} catch (InputMismatchException ime) {1136// Correct result1137}1138s.next();1139}1140try {1141s.next();1142failCount++;1143} catch (InputMismatchException ime) {1144failCount++;1145} catch (NoSuchElementException nse) {1146// Correct result1147}1148report("Scan shorts");1149}11501151private static void intTest(int sourceType) throws Exception {1152Scanner s = scannerFor(1153"22 022 C -34 0x80000000 -2147483649 dog ", sourceType);1154if (!s.hasNextInt()) failCount++;1155if (s.nextInt() != 22) failCount++;1156if (!s.hasNextInt()) failCount++;1157if (s.nextInt() != 22) failCount++;1158if (!s.hasNextInt(16)) failCount++;1159if (s.nextInt(16) != 12) failCount++;1160if (!s.hasNextInt()) failCount++;1161if (s.nextInt() != -34) failCount++;1162for (int i=0; i<3; i++) {1163if (s.hasNextInt())1164failCount++;1165try {1166s.nextInt();1167failCount++;1168} catch (InputMismatchException ime) {1169// Correct result1170}1171s.next();1172}1173try {1174s.next();1175failCount++;1176} catch (InputMismatchException ime) {1177failCount++;1178} catch (NoSuchElementException nse) {1179// Correct result1180}1181report("Scan ints");1182}11831184private static void longTest(int sourceType) throws Exception {1185Scanner s = scannerFor(1186"022 9223372036854775807 0x8000000000000000 9223372036854775808 dog ",1187sourceType);1188if (!s.hasNextLong()) failCount++;1189if (s.nextLong() != (long)22) failCount++;1190if (!s.hasNextLong()) failCount++;1191if (s.nextLong() != 9223372036854775807L) failCount++;1192for (int i=0; i<3; i++) {1193if (s.hasNextLong())1194failCount++;1195try {1196s.nextLong();1197failCount++;1198} catch (InputMismatchException ime) {1199// Correct result1200}1201s.next();1202}1203try {1204s.next();1205failCount++;1206} catch (InputMismatchException ime) {1207failCount++;1208} catch (NoSuchElementException nse) {1209// Correct result1210}1211report("Scan longs");1212}12131214private static void floatTest(int sourceType) throws Exception {1215Scanner s = scannerFor(1216"0 0. 0.0 2 2. 2.0 2.3 -2 -2.0 -2.3 -. 2-. 2..3", sourceType);1217if (!s.hasNextFloat()) failCount++;1218if (s.nextFloat() != 0f) failCount++;1219if (!s.hasNextFloat()) failCount++;1220if (s.nextFloat() != 0f) failCount++;1221if (!s.hasNextFloat()) failCount++;1222if (s.nextFloat() != 0f) failCount++;1223if (!s.hasNextFloat()) failCount++;1224if (s.nextFloat() != 2f) failCount++;1225if (!s.hasNextFloat()) failCount++;1226if (s.nextFloat() != 2f) failCount++;1227if (!s.hasNextFloat()) failCount++;1228if (s.nextFloat() != 2f) failCount++;1229if (!s.hasNextFloat()) failCount++;1230if (s.nextFloat() != 2.3f) failCount++;1231if (!s.hasNextFloat()) failCount++;1232if (s.nextFloat() != -2f) failCount++;1233if (!s.hasNextFloat()) failCount++;1234if (s.nextFloat() != -2f) failCount++;1235if (!s.hasNextFloat()) failCount++;1236if (s.nextFloat() != -2.3f) failCount++;1237for (int i=0; i<3; i++) {1238if (s.hasNextLong())1239failCount++;1240try {1241s.nextFloat();1242failCount++;1243} catch (InputMismatchException ime) {1244// Correct result1245}1246s.next();1247}1248try {1249s.next();1250failCount++;1251} catch (InputMismatchException ime) {1252failCount++;1253} catch (NoSuchElementException nse) {1254// Correct result1255}1256report("Scan floats");1257}12581259private static void doubleTest(int sourceType) throws Exception {1260Scanner s = scannerFor(1261"0 0. 0.0 2 2. 2.0 2.3 -2 -2.0 -2.3 -. 2-. 2..3", sourceType);1262if (!s.hasNextDouble()) failCount++;1263if (s.nextDouble() != 0d) failCount++;1264if (!s.hasNextDouble()) failCount++;1265if (s.nextDouble() != 0d) failCount++;1266if (!s.hasNextDouble()) failCount++;1267if (s.nextDouble() != 0d) failCount++;1268if (!s.hasNextDouble()) failCount++;1269if (s.nextDouble() != 2d) failCount++;1270if (!s.hasNextDouble()) failCount++;1271if (s.nextDouble() != 2d) failCount++;1272if (!s.hasNextDouble()) failCount++;1273if (s.nextDouble() != 2d) failCount++;1274if (!s.hasNextDouble()) failCount++;1275if (s.nextDouble() != 2.3d) failCount++;1276if (!s.hasNextDouble()) failCount++;1277if (s.nextDouble() != -2d) failCount++;1278if (!s.hasNextDouble()) failCount++;1279if (s.nextDouble() != -2d) failCount++;1280if (!s.hasNextDouble()) failCount++;1281if (s.nextDouble() != -2.3d) failCount++;1282for (int i=0; i<3; i++) {1283if (s.hasNextLong())1284failCount++;1285try {1286s.nextDouble();1287failCount++;1288} catch (InputMismatchException ime) {1289// Correct result1290}1291s.next();1292}1293try {1294s.next();1295failCount++;1296} catch (InputMismatchException ime) {1297failCount++;1298} catch (NoSuchElementException nse) {1299// Correct result1300}1301report("Scan doubles");1302}13031304private static void booleanTest(int sourceType) throws Exception {1305Scanner s = scannerFor(1306" true false\t \r\n true FaLse \n True Tru", sourceType);1307if (!s.nextBoolean()) failCount++;1308if (!s.hasNextBoolean()) failCount++;1309if (s.nextBoolean()) failCount++;1310if (!s.nextBoolean()) failCount++;1311if (s.nextBoolean()) failCount++;1312if (!s.nextBoolean()) failCount++;1313if (s.hasNextBoolean()) failCount++;1314try {1315s.nextBoolean();1316failCount++;1317} catch (NoSuchElementException nsee) {1318// Expected result1319}1320report("Scan booleans");1321}13221323private static void hasNextTest(int sourceType) throws Exception {1324Scanner s = scannerFor(1325" blah blech\t blather alongblatherindeed", sourceType);1326if (!s.hasNext()) failCount++;1327if (!s.hasNext()) failCount++;1328String result = s.next();1329if (!result.equals("blah")) failCount++;1330if (!s.hasNext()) failCount++;1331if (!s.hasNext()) failCount++;1332result = s.next();1333if (!result.equals("blech")) failCount++;1334if (!s.hasNext()) failCount++;1335result = s.next();1336if (!result.equals("blather")) failCount++;1337if (!s.hasNext()) failCount++;1338if (!s.hasNext()) failCount++;1339result = s.next();1340if (!result.equals("alongblatherindeed")) failCount++;1341if (s.hasNext()) failCount++;1342try {1343result = s.next();1344failCount++;1345} catch (NoSuchElementException nsee) {1346// Correct result1347}1348report("Has next test");1349}13501351private static void nextTest(int sourceType) throws Exception {1352Scanner s = scannerFor(1353" blah blech\t blather alongblatherindeed", sourceType);1354String result = (String)s.next();1355if (!result.equals("blah")) failCount++;1356result = (String)s.next();1357if (!result.equals("blech")) failCount++;1358result = (String)s.next();1359if (!result.equals("blather")) failCount++;1360result = (String)s.next();1361if (!result.equals("alongblatherindeed"))1362failCount++;1363try {1364result = (String)s.next();1365failCount++;1366} catch (NoSuchElementException nsee) {1367// Correct result1368}1369report("Next test");1370}13711372private static void hasNextPatternTest(int sourceType) throws Exception {1373Scanner s = scannerFor(1374" blah blech\t blather alongblatherindeed", sourceType);1375Pattern p1 = Pattern.compile("\\w+");1376Pattern p2 = Pattern.compile("blech");1377if (!s.hasNext(p1)) failCount++;1378if (!s.hasNext(p1)) failCount++;1379if (s.hasNext(p2)) failCount++;1380String result = (String)s.next();1381if (!result.equals("blah")) failCount++;1382if (!s.hasNext(p1)) failCount++;1383if (!s.hasNext(p2)) failCount++;1384result = (String)s.next();1385if (!result.equals("blech")) failCount++;1386if (!s.hasNext(p1)) failCount++;1387if (s.hasNext(p2)) failCount++;1388result = (String)s.next();1389if (!result.equals("blather")) failCount++;1390if (!s.hasNext(p1)) failCount++;1391if (s.hasNext(p2)) failCount++;1392result = (String)s.next();1393if (!result.equals("alongblatherindeed")) failCount++;1394if (s.hasNext(p1)) failCount++;1395if (s.hasNext(p2)) failCount++;1396report("Has Next Pattern test");1397}13981399private static void nextPatternTest(int sourceType) throws Exception {1400Scanner s = scannerFor(1401" blah blech\t blather alongblatherindeed", sourceType);1402Pattern p1 = Pattern.compile("blah");1403Pattern p2 = Pattern.compile("blech");1404Pattern p3 = Pattern.compile("blather");1405Pattern p4 = Pattern.compile("alongblatherindeed");1406String result = null;1407try {1408result = (String)s.next(p2);1409failCount++;1410} catch (NoSuchElementException nsee) {1411// Correct result1412}1413result = (String)s.next(p1);1414if (!result.equals("blah"))1415failCount++;1416try {1417result = (String)s.next(p1);1418failCount++;1419} catch (NoSuchElementException nsee) {1420// Correct result1421}1422result = (String)s.next(p2);1423if (!result.equals("blech"))1424failCount++;1425try {1426result = (String)s.next(p4);1427failCount++;1428} catch (NoSuchElementException nsee) {1429// Correct result1430}1431result = (String)s.next(p3);1432if (!result.equals("blather"))1433failCount++;1434try {1435result = (String)s.next(p3);1436failCount++;1437} catch (NoSuchElementException nsee) {1438// Correct result1439}1440result = (String)s.next(p4);1441if (!result.equals("alongblatherindeed"))1442failCount++;1443try {1444result = (String)s.next();1445failCount++;1446} catch (NoSuchElementException nsee) {1447// Correct result1448}1449report("Next pattern test");1450}14511452private static void useLocaleTest() throws Exception {1453Scanner s = new Scanner("334.65").useLocale(Locale.ENGLISH);1454if (!s.hasNextFloat()) failCount++;1455if (s.nextFloat() != 334.65f) failCount++;14561457s = new Scanner("334,65").useLocale(Locale.FRENCH);1458if (!s.hasNextFloat()) failCount++;1459if (s.nextFloat() != 334.65f) failCount++;14601461s = new Scanner("4.334,65").useLocale(Locale.GERMAN);1462if (!s.hasNextFloat()) failCount++;1463if (s.nextFloat() != 4334.65f) failCount++;14641465// Test case reported from India1466try {1467String Message = "123978.90 $";1468Locale locale = new Locale("hi","IN");1469NumberFormat form = NumberFormat.getInstance(locale);1470double myNumber = 1902.09;1471Scanner scanner = new Scanner(form.format(myNumber).toString());1472scanner.useLocale(locale);1473double d = scanner.nextDouble();1474} catch (InputMismatchException ime) {1475failCount++;1476}1477report("Use locale test");1478}14791480public static void resetTest() throws Exception {1481Scanner sc = new Scanner("");1482int radix = sc.radix();1483Locale locale = sc.locale();1484Pattern delimiter = sc.delimiter();1485Pattern a = Pattern.compile("A");1486sc.useDelimiter(a);1487Locale dummy = new Locale("en", "US", "dummy");1488sc.useLocale(dummy);1489sc.useRadix(16);1490if (sc.radix() != 16 ||1491!sc.locale().equals(dummy) ||1492!sc.delimiter().pattern().equals(a.pattern())) {1493failCount++;1494} else {1495sc.reset();1496if (sc.radix() != radix ||1497!sc.locale().equals(locale) ||1498!sc.delimiter().pattern().equals(delimiter.pattern())) {1499failCount++;1500}1501}1502sc.close();1503report("Reset test");1504}15051506static List<BiConsumer <Scanner, Integer>> methodWRList = Arrays.asList(1507(s, r) -> s.hasNextByte(r),1508(s, r) -> s.nextByte(r),1509(s, r) -> s.hasNextShort(r),1510(s, r) -> s.nextShort(r),1511(s, r) -> s.hasNextInt(r),1512(s, r) -> s.nextInt(r),1513(s, r) -> s.hasNextLong(r),1514(s, r) -> s.nextLong(r),1515(s, r) -> s.hasNextBigInteger(r),1516(s, r) -> s.nextBigInteger(r)1517);15181519/*1520* Test that setting the radix to an out of range value triggers1521* an IllegalArgumentException1522*/1523public static void outOfRangeRadixTest() throws Exception {1524int[] bad = new int[] { -1, 0, 1, 37, 38 };1525int[] good = IntStream.rangeClosed(Character.MIN_RADIX, Character.MAX_RADIX)1526.toArray();15271528methodWRList.stream().forEach( m -> {1529for (int r : bad) {1530try (Scanner sc = new Scanner("10 10 10 10")) {1531m.accept(sc, r);1532failCount++;1533} catch (IllegalArgumentException ise) {}1534}1535});1536methodWRList.stream().forEach( m -> {1537for (int r : good) {1538try (Scanner sc = new Scanner("10 10 10 10")) {1539m.accept(sc, r);1540} catch (Exception x) {1541failCount++;1542}1543}1544});1545report("Radix out of range test");1546}15471548/*1549* Test that closing the stream also closes the underlying Scanner.1550* The cases of attempting to open streams on a closed Scanner are1551* covered by closeTest().1552*/1553public static void streamCloseTest() throws Exception {1554Scanner sc;15551556Scanner sc1 = new Scanner("xyzzy");1557sc1.tokens().close();1558try {1559sc1.hasNext();1560failCount++;1561} catch (IllegalStateException ise) {1562// Correct result1563}15641565Scanner sc2 = new Scanner("a b c d e f");1566try {1567sc2.tokens()1568.peek(s -> sc2.close())1569.count();1570} catch (IllegalStateException ise) {1571// Correct result1572}15731574Scanner sc3 = new Scanner("xyzzy");1575sc3.findAll("q").close();1576try {1577sc3.hasNext();1578failCount++;1579} catch (IllegalStateException ise) {1580// Correct result1581}15821583try (Scanner sc4 = new Scanner(inputFile)) {1584sc4.findAll("[0-9]+")1585.peek(s -> sc4.close())1586.count();1587failCount++;1588} catch (IllegalStateException ise) {1589// Correct result1590}15911592report("Streams Close test");1593}15941595/*1596* Test ConcurrentModificationException1597*/1598public static void streamComodTest() {1599try {1600Scanner sc = new Scanner("a b c d e f");1601sc.tokens()1602.peek(s -> sc.hasNext())1603.count();1604failCount++;1605} catch (ConcurrentModificationException cme) {1606// Correct result1607}16081609try {1610Scanner sc = new Scanner("a b c d e f");1611Iterator<String> it = sc.tokens().iterator();1612it.next();1613sc.next();1614it.next();1615failCount++;1616} catch (ConcurrentModificationException cme) {1617// Correct result1618}16191620try {1621String input = IntStream.range(0, 100)1622.mapToObj(String::valueOf)1623.collect(Collectors.joining(" "));1624Scanner sc = new Scanner(input);1625sc.findAll("[0-9]+")1626.peek(s -> sc.hasNext())1627.count();1628failCount++;1629} catch (ConcurrentModificationException cme) {1630// Correct result1631}16321633try {1634String input = IntStream.range(0, 100)1635.mapToObj(String::valueOf)1636.collect(Collectors.joining(" "));1637Scanner sc = new Scanner(input);1638Iterator<MatchResult> it = sc.findAll("[0-9]+").iterator();1639it.next();1640sc.next();1641it.next();1642failCount++;1643} catch (ConcurrentModificationException cme) {1644// Correct result1645}16461647report("Streams Comod test");1648}16491650private static void report(String testName) {1651System.err.printf("%-30s: %s%n", testName,1652(failCount == 0) ? "Passed" : String.format("Failed(%d)", failCount));16531654if (failCount > 0)1655failure = true;1656failCount = 0;1657}16581659static Scanner scannerFor(String input, int sourceType) {1660if (sourceType == 1)1661return new Scanner(input);1662else1663return new Scanner(new StutteringInputStream(input));1664}16651666static class ThrowingReadable implements Readable {1667ThrowingReadable() {1668}1669public int read(java.nio.CharBuffer cb) throws IOException {1670throw new IOException("ThrowingReadable always throws");1671}1672}1673}167416751676