1 /*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24 /**
25 * @test
26 * @bug 6840246 6559590
27 * @summary test String.split()
28 * @key randomness
29 */
30 import java.util.Arrays;
31 import java.util.Random;
32 import java.util.regex.*;
33
34 public class Split {
35
36 public static void main(String[] args) throws Exception {
37 String source = "0123456789";
38
39 for (int limit=-2; limit<3; limit++) {
40 for (int x=0; x<10; x++) {
41 String[] result = source.split(Integer.toString(x), limit);
42 int expectedLength = limit < 1 ? 2 : limit;
43
44 if ((limit == 0) && (x == 9)) {
45 // expected dropping of ""
46 if (result.length != 1)
47 throw new RuntimeException("String.split failure 1");
48 if (!result[0].equals("012345678")) {
49 throw new RuntimeException("String.split failure 2");
50 }
51 } else {
52 if (result.length != expectedLength) {
53 throw new RuntimeException("String.split failure 3");
54 }
55 if (!result[0].equals(source.substring(0,x))) {
56 if (limit != 1) {
57 throw new RuntimeException(
58 "String.split failure 4");
59 } else {
60 if (!result[0].equals(source.substring(0,10))) {
61 throw new RuntimeException(
62 "String.split failure 10");
63 }
64 }
65 }
66 if (expectedLength > 1) { // Check segment 2
67 if (!result[1].equals(source.substring(x+1,10)))
68 throw new RuntimeException("String.split failure 5");
69 }
70 }
71 }
72 }
73 // Check the case for no match found
74 for (int limit=-2; limit<3; limit++) {
75 String[] result = source.split("e", limit);
76 if (result.length != 1)
77 throw new RuntimeException("String.split failure 6");
78 if (!result[0].equals(source))
79 throw new RuntimeException("String.split failure 7");
80 }
81 // Check the case for limit == 0, source = "";
82 // split() now returns 0-length for empty source "" see #6559590
83 source = "";
84 String[] result = source.split("e", 0);
85 if (result.length != 1)
86 throw new RuntimeException("String.split failure 8");
87 if (!result[0].equals(source))
88 throw new RuntimeException("String.split failure 9");
89
90 // check fastpath of String.split()
91 source = "0123456789abcdefgABCDEFG";
92 Random r = new Random();
93
94 for (boolean doEscape: new boolean[] {false, true}) {
95 for (int cp = 0; cp < 0x11000; cp++) {
96 Pattern p = null;
97 String regex = new String(Character.toChars(cp));
98 if (doEscape)
99 regex = "\\" + regex;
100 try {
101 p = Pattern.compile(regex);
102 } catch (PatternSyntaxException pse) {
103 // illegal syntax
104 try {
105 "abc".split(regex);
106 } catch (PatternSyntaxException pse0) {
107 continue;
108 }
109 throw new RuntimeException("String.split failure 11");
110 }
111 int off = r.nextInt(source.length());
112 String[] srcStrs = new String[] {
113 "",
114 source,
115 regex + source,
116 source + regex,
117 source.substring(0, 3)
118 + regex + source.substring(3, 9)
119 + regex + source.substring(9, 15)
120 + regex + source.substring(15),
121 source.substring(0, off) + regex + source.substring(off)
122 };
123 for (String src: srcStrs) {
124 for (int limit=-2; limit<3; limit++) {
125 if (!Arrays.equals(src.split(regex, limit),
126 p.split(src, limit)))
127 throw new RuntimeException("String.split failure 12");
128 }
129 }
130 }
131 }
132 }
133 }
--- EOF ---