Path: blob/master/test/jdk/java/lang/String/Lines.java
41149 views
/*1* Copyright (c) 2018, 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* @summary Basic lines functionality26* @bug 820038027* @run main/othervm Lines28*/2930import java.util.Iterator;31import java.util.stream.Stream;32import java.io.BufferedReader;33import java.io.StringReader;3435public class Lines {36public static void main(String... arg) {37testLines();38}3940/*41* Test with strings42*/43static void testLines() {44testString("");45testString(" ");46testString("\n");47testString("\n\n\n");48testString("\r\r\r");49testString("\r\n\r\n\r\n");50testString("\n\r\r\n");51testString("abc\ndef\nghi\n");52testString("abc\ndef\nghi");53testString("abc\rdef\rghi\r");54testString("abc\rdef\rghi");55testString("abc\r\ndef\r\nghi\r\n");56testString("abc\r\ndef\r\nghi");5758testString("\2022");59testString("\2022\n");60testString("\2022\n\2022\n\2022\n");61testString("\2022\r\2022\r\2022\r");62testString("\2022\r\n\2022\r\n\2022\r\n");63testString("\2022\n\2022\r\2022\r\n");64testString("abc\2022\ndef\2022\nghi\2022\n");65testString("abc\2022\ndef\2022\nghi\2022");66testString("abc\2022\rdef\2022\rghi\2022\r");67testString("abc\2022\rdef\2022\rghi\2022");68testString("abc\2022\r\ndef\2022\r\nghi\2022\r\n");69testString("abc\2022\r\ndef\2022\r\nghi\2022");70testString("\2022\n\n\n");71}7273static void testString(String string) {74Stream<String> lines = string.lines();75Stream<String> brLines = new BufferedReader(new StringReader(string)).lines();7677Iterator<String> iterator = lines.iterator();78Iterator<String> brIterator = brLines.iterator();79int count = 0;8081while (iterator.hasNext() && brIterator.hasNext()) {82count++;83String line = iterator.next();84String brLine = brIterator.next();8586if (!line.equals(brLine)) {87String replace = string.replaceAll("\n", "\\n").replaceAll("\r", "\\r");88System.err.format("Mismatch at line %d of \"%s\"%n", count, replace);89throw new RuntimeException();90}91}9293if (iterator.hasNext() || brIterator.hasNext()) {94System.err.format("Mismatch after line %d of \"%s\"%n", count, string);95throw new RuntimeException();96}97}98}99100101