test/java/lang/String/Split.java

Print this page




   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
  27  * @summary test String.split()
  28  */
  29 import java.util.Arrays;
  30 import java.util.Random;
  31 import java.util.regex.*;
  32 
  33 public class Split {
  34 
  35     public static void main(String[] args) throws Exception {
  36         String source = "0123456789";
  37 
  38         for (int limit=-2; limit<3; limit++) {
  39             for (int x=0; x<10; x++) {
  40                 String[] result = source.split(Integer.toString(x), limit);
  41                 int expectedLength = limit < 1 ? 2 : limit;
  42 
  43                 if ((limit == 0) && (x == 9)) {
  44                     // expected dropping of ""
  45                     if (result.length != 1)
  46                         throw new RuntimeException("String.split failure 1");


  61                                 "String.split failure 10");
  62                             }
  63                         }
  64                     }
  65                     if (expectedLength > 1) { // Check segment 2
  66                        if (!result[1].equals(source.substring(x+1,10)))
  67                           throw new RuntimeException("String.split failure 5");
  68                     }
  69                 }
  70             }
  71         }
  72         // Check the case for no match found
  73         for (int limit=-2; limit<3; limit++) {
  74             String[] result = source.split("e", limit);
  75             if (result.length != 1)
  76                 throw new RuntimeException("String.split failure 6");
  77             if (!result[0].equals(source))
  78                 throw new RuntimeException("String.split failure 7");
  79         }
  80         // Check the case for limit == 0, source = "";

  81         source = "";
  82         String[] result = source.split("e", 0);
  83         if (result.length != 1)
  84             throw new RuntimeException("String.split failure 8");
  85         if (!result[0].equals(source))
  86             throw new RuntimeException("String.split failure 9");
  87 
  88         // check fastpath of String.split()
  89         source = "0123456789abcdefgABCDEFG";
  90         Random r = new Random();
  91 
  92         for (boolean doEscape: new boolean[] {false, true}) {
  93             for (int cp = 0; cp < 0x11000; cp++) {
  94                 Pattern p = null;
  95                 String regex = new String(Character.toChars(cp));
  96                 if (doEscape)
  97                     regex = "\\" + regex;
  98                 try {
  99                     p = Pattern.compile(regex);
 100                 } catch (PatternSyntaxException pse) {
 101                     // illegal syntax
 102                     try {
 103                         "abc".split(regex);
 104                     } catch (PatternSyntaxException pse0) {
 105                         continue;
 106                     }




   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  */
  29 import java.util.Arrays;
  30 import java.util.Random;
  31 import java.util.regex.*;
  32 
  33 public class Split {
  34 
  35     public static void main(String[] args) throws Exception {
  36         String source = "0123456789";
  37 
  38         for (int limit=-2; limit<3; limit++) {
  39             for (int x=0; x<10; x++) {
  40                 String[] result = source.split(Integer.toString(x), limit);
  41                 int expectedLength = limit < 1 ? 2 : limit;
  42 
  43                 if ((limit == 0) && (x == 9)) {
  44                     // expected dropping of ""
  45                     if (result.length != 1)
  46                         throw new RuntimeException("String.split failure 1");


  61                                 "String.split failure 10");
  62                             }
  63                         }
  64                     }
  65                     if (expectedLength > 1) { // Check segment 2
  66                        if (!result[1].equals(source.substring(x+1,10)))
  67                           throw new RuntimeException("String.split failure 5");
  68                     }
  69                 }
  70             }
  71         }
  72         // Check the case for no match found
  73         for (int limit=-2; limit<3; limit++) {
  74             String[] result = source.split("e", limit);
  75             if (result.length != 1)
  76                 throw new RuntimeException("String.split failure 6");
  77             if (!result[0].equals(source))
  78                 throw 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 #6559590
  82         source = "";
  83         String[] result = source.split("e", 0);
  84         if (result.length != 0)
  85             throw new RuntimeException("String.split failure 8");


  86 
  87         // check fastpath of String.split()
  88         source = "0123456789abcdefgABCDEFG";
  89         Random r = new Random();
  90 
  91         for (boolean doEscape: new boolean[] {false, true}) {
  92             for (int cp = 0; cp < 0x11000; cp++) {
  93                 Pattern p = null;
  94                 String regex = new String(Character.toChars(cp));
  95                 if (doEscape)
  96                     regex = "\\" + regex;
  97                 try {
  98                     p = Pattern.compile(regex);
  99                 } catch (PatternSyntaxException pse) {
 100                     // illegal syntax
 101                     try {
 102                         "abc".split(regex);
 103                     } catch (PatternSyntaxException pse0) {
 104                         continue;
 105                     }