Path: blob/master/test/jdk/java/lang/String/Split.java
41152 views
/*1* Copyright (c) 2000, 2013, 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 6840246 655959026* @summary test String.split()27* @key randomness28*/29import java.util.Arrays;30import java.util.Random;31import java.util.regex.*;3233public class Split {3435public static void main(String[] args) throws Exception {36String source = "0123456789";3738for (int limit=-2; limit<3; limit++) {39for (int x=0; x<10; x++) {40String[] result = source.split(Integer.toString(x), limit);41int expectedLength = limit < 1 ? 2 : limit;4243if ((limit == 0) && (x == 9)) {44// expected dropping of ""45if (result.length != 1)46throw new RuntimeException("String.split failure 1");47if (!result[0].equals("012345678")) {48throw new RuntimeException("String.split failure 2");49}50} else {51if (result.length != expectedLength) {52throw new RuntimeException("String.split failure 3");53}54if (!result[0].equals(source.substring(0,x))) {55if (limit != 1) {56throw new RuntimeException(57"String.split failure 4");58} else {59if (!result[0].equals(source.substring(0,10))) {60throw new RuntimeException(61"String.split failure 10");62}63}64}65if (expectedLength > 1) { // Check segment 266if (!result[1].equals(source.substring(x+1,10)))67throw new RuntimeException("String.split failure 5");68}69}70}71}72// Check the case for no match found73for (int limit=-2; limit<3; limit++) {74String[] result = source.split("e", limit);75if (result.length != 1)76throw new RuntimeException("String.split failure 6");77if (!result[0].equals(source))78throw new RuntimeException("String.split failure 7");79}80// Check the case for limit == 0, source = "";81// split() now returns 0-length for empty source "" see #655959082source = "";83String[] result = source.split("e", 0);84if (result.length != 1)85throw new RuntimeException("String.split failure 8");86if (!result[0].equals(source))87throw new RuntimeException("String.split failure 9");8889// check fastpath of String.split()90source = "0123456789abcdefgABCDEFG";91Random r = new Random();9293for (boolean doEscape: new boolean[] {false, true}) {94for (int cp = 0; cp < 0x11000; cp++) {95Pattern p = null;96String regex = new String(Character.toChars(cp));97if (doEscape)98regex = "\\" + regex;99try {100p = Pattern.compile(regex);101} catch (PatternSyntaxException pse) {102// illegal syntax103try {104"abc".split(regex);105} catch (PatternSyntaxException pse0) {106continue;107}108throw new RuntimeException("String.split failure 11");109}110int off = r.nextInt(source.length());111String[] srcStrs = new String[] {112"",113source,114regex + source,115source + regex,116source.substring(0, 3)117+ regex + source.substring(3, 9)118+ regex + source.substring(9, 15)119+ regex + source.substring(15),120source.substring(0, off) + regex + source.substring(off)121};122for (String src: srcStrs) {123for (int limit=-2; limit<3; limit++) {124if (!Arrays.equals(src.split(regex, limit),125p.split(src, limit)))126throw new RuntimeException("String.split failure 12");127}128}129}130}131}132}133134135