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