< prev index next >

test/jdk/java/lang/String/LiteralReplace.java

Print this page
rev 54647 : [mq]: 8222955-Optimize-String-replace-CharSequence-CharSequence-for-Latin1-encoded-strings
   1 /*
   2  * Copyright (c) 2015, 2017, 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 /* @test
  25  * @bug 8058779 8054307
  26  * @library /test/lib
  27  * @build jdk.test.lib.RandomFactory
  28  * @run testng LiteralReplace
  29  * @summary Basic tests of String.replace(CharSequence, CharSequence)
  30  * @key randomness
  31  */
  32 
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.Iterator;
  36 import java.util.regex.Matcher;
  37 import java.util.regex.Pattern;
  38 import java.util.Random;
  39 
  40 import jdk.test.lib.RandomFactory;
  41 
  42 import org.testng.annotations.Test;
  43 import org.testng.annotations.DataProvider;
  44 import static org.testng.Assert.fail;
  45 


  87             {"abcdefgh", "def", "DEF", "abcDEFgh"},
  88             {"abcdefgh", "123", "DEF", "abcdefgh"},
  89             {"abcdefgh", "abcdefghi", "DEF", "abcdefgh"},
  90             {"abcdefghabc", "abc", "DEF", "DEFdefghDEF"},
  91             {"abcdefghdef", "def", "", "abcgh"},
  92             {"abcdefgh", "", "_", "_a_b_c_d_e_f_g_h_"},
  93             {"", "", "", ""},
  94             {"", "a", "b", ""},
  95             {"", "", "abc", "abc"},
  96             {"abcdefgh", "abcdefgh", "abcdefgh", "abcdefgh"},
  97             {"abcdefgh", "abcdefgh", "abcdefghi", "abcdefghi"},
  98             {"abcdefgh", "abcdefgh", "", ""},
  99             {"abcdabcd", "abcd", "", ""},
 100             {"aaaaaaaaa", "aa", "_X_", "_X__X__X__X_a"},
 101             {"aaaaaaaaa", "aa", "aaa", "aaaaaaaaaaaaa"},
 102             {"aaaaaaaaa", "aa", "aa", "aaaaaaaaa"},
 103             {"a.c.e.g.", ".", "-", "a-c-e-g-"},
 104             {"abcdefgh", "[a-h]", "X", "abcdefgh"},
 105             {"aa+", "a+", "", "a"},
 106             {"^abc$", "abc", "x", "^x$"},








 107 
 108             // more with non-latin1 characters
 109             {"\u4e00\u4e00\u4e00",
 110              "\u4e00\u4e00",
 111              "\u4e01",
 112              "\u4e01\u4e00"},
 113 
 114             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
 115              "\u4e03\u4e04\u4e05",
 116              "\u4e10\u4e11\u4e12",
 117              "\u4e00\u4e01\u4e02\u4e10\u4e11\u4e12\u4e06\u4e07\u4e08"},
 118 
 119             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
 120              "ABC",
 121              "\u4e10\u4e11\u4e12",
 122              "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"},
 123 
 124             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",
 125              "\u4e02\u4e03",
 126              "\u4e12\u4e13",


 190 
 191             {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
 192              "\u4e00\u4e00\u4e00",
 193              "\u4e00\u4e00",
 194              "\u4e00\u4e00\u4e00\u4e00"},
 195 
 196             {"\u4e00.\u4e01.\u4e02.\u4e03.\u4e04.",
 197              ".",
 198              "-",
 199              "\u4e00-\u4e01-\u4e02-\u4e03-\u4e04-"},
 200 
 201             {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
 202              "\u4e00",
 203              "",
 204              ""},
 205 
 206             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
 207              "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
 208              "",
 209              ""},




































 210         };
 211     }
 212 
 213     @DataProvider
 214     public static Iterator<Object[]> sourceTargetReplacement() {
 215         ArrayList<Object[]> list = new ArrayList<>();
 216         for (int maxSrcLen = 1; maxSrcLen <= (1 << 10); maxSrcLen <<= 1) {
 217             for (int maxTrgLen = 1; maxTrgLen <= (1 << 10); maxTrgLen <<= 1) {
 218                 for (int maxPrlLen = 1; maxPrlLen <= (1 << 10); maxPrlLen <<= 1) {
 219                     list.add(makeArray(makeRandomString(maxSrcLen),
 220                                        makeRandomString(maxTrgLen),
 221                                        makeRandomString(maxPrlLen)));
 222 
 223                     String source = makeRandomString(maxSrcLen);
 224                     list.add(makeArray(source,
 225                                        mekeRandomSubstring(source, maxTrgLen),
 226                                        makeRandomString(maxPrlLen)));
 227                 }
 228             }
 229         }


   1 /*
   2  * Copyright (c) 2015, 2019, 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 /* @test
  25  * @bug 8058779 8054307 8222955
  26  * @library /test/lib
  27  * @build jdk.test.lib.RandomFactory
  28  * @run testng LiteralReplace
  29  * @summary Basic tests of String.replace(CharSequence, CharSequence)
  30  * @key randomness
  31  */
  32 
  33 import java.util.ArrayList;
  34 import java.util.Arrays;
  35 import java.util.Iterator;
  36 import java.util.regex.Matcher;
  37 import java.util.regex.Pattern;
  38 import java.util.Random;
  39 
  40 import jdk.test.lib.RandomFactory;
  41 
  42 import org.testng.annotations.Test;
  43 import org.testng.annotations.DataProvider;
  44 import static org.testng.Assert.fail;
  45 


  87             {"abcdefgh", "def", "DEF", "abcDEFgh"},
  88             {"abcdefgh", "123", "DEF", "abcdefgh"},
  89             {"abcdefgh", "abcdefghi", "DEF", "abcdefgh"},
  90             {"abcdefghabc", "abc", "DEF", "DEFdefghDEF"},
  91             {"abcdefghdef", "def", "", "abcgh"},
  92             {"abcdefgh", "", "_", "_a_b_c_d_e_f_g_h_"},
  93             {"", "", "", ""},
  94             {"", "a", "b", ""},
  95             {"", "", "abc", "abc"},
  96             {"abcdefgh", "abcdefgh", "abcdefgh", "abcdefgh"},
  97             {"abcdefgh", "abcdefgh", "abcdefghi", "abcdefghi"},
  98             {"abcdefgh", "abcdefgh", "", ""},
  99             {"abcdabcd", "abcd", "", ""},
 100             {"aaaaaaaaa", "aa", "_X_", "_X__X__X__X_a"},
 101             {"aaaaaaaaa", "aa", "aaa", "aaaaaaaaaaaaa"},
 102             {"aaaaaaaaa", "aa", "aa", "aaaaaaaaa"},
 103             {"a.c.e.g.", ".", "-", "a-c-e-g-"},
 104             {"abcdefgh", "[a-h]", "X", "abcdefgh"},
 105             {"aa+", "a+", "", "a"},
 106             {"^abc$", "abc", "x", "^x$"},
 107             {"abc", "b", "_", "a_c"},
 108             {"abc", "bc", "_", "a_"},
 109             {"abc".repeat(65537) + "end", "b", "_XYZ_", "a_XYZ_c".repeat(65537) + "end"},
 110             {"abc".repeat(65537) + "end", "a", "_", "_bc".repeat(65537) + "end"},
 111             {"a".repeat(65537), "a", "", ""},
 112             {"ab".repeat(65537), "a", "", "b".repeat(65537)},
 113             {"ab".repeat(65537), "ab", "", ""},
 114             {"b" + "ab".repeat(65537), "ab", "", "b"},
 115 
 116             // more with non-latin1 characters
 117             {"\u4e00\u4e00\u4e00",
 118              "\u4e00\u4e00",
 119              "\u4e01",
 120              "\u4e01\u4e00"},
 121 
 122             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
 123              "\u4e03\u4e04\u4e05",
 124              "\u4e10\u4e11\u4e12",
 125              "\u4e00\u4e01\u4e02\u4e10\u4e11\u4e12\u4e06\u4e07\u4e08"},
 126 
 127             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08",
 128              "ABC",
 129              "\u4e10\u4e11\u4e12",
 130              "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05\u4e06\u4e07\u4e08"},
 131 
 132             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e02\u4e03\u4e07\u4e08",
 133              "\u4e02\u4e03",
 134              "\u4e12\u4e13",


 198 
 199             {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
 200              "\u4e00\u4e00\u4e00",
 201              "\u4e00\u4e00",
 202              "\u4e00\u4e00\u4e00\u4e00"},
 203 
 204             {"\u4e00.\u4e01.\u4e02.\u4e03.\u4e04.",
 205              ".",
 206              "-",
 207              "\u4e00-\u4e01-\u4e02-\u4e03-\u4e04-"},
 208 
 209             {"\u4e00\u4e00\u4e00\u4e00\u4e00\u4e00",
 210              "\u4e00",
 211              "",
 212              ""},
 213 
 214             {"\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
 215              "\u4e00\u4e01\u4e02\u4e03\u4e04\u4e05",
 216              "",
 217              ""},
 218 
 219             {"\u4e01\u4e02\u4e03", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03"},
 220             {"\u4e01\u4e02\u4e03", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03"},
 221             {"\u4e01\u4e02\u4e03", "\u4e02", "_", "\u4e01_\u4e03"},
 222             {"a\u4e02c", "\u4e02", "_", "a_c"},
 223             {"\u4e01@\u4e03", "@", "_", "\u4e01_\u4e03"},
 224             {"\u4e01@\u4e03", "@", "\u4e02", "\u4e01\u4e02\u4e03"},
 225             {"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e02\u4e03", "\u4e01\u4e02\u4e03"},
 226             {"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e04\u4e05", "\u4e01\u4e04\u4e05"},
 227             {"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "\u4e06", "\u4e01\u4e06"},
 228             {"\u4e01\u4e02\u4e03", "\u4e02\u4e03", "<>", "\u4e01<>"},
 229             {"@\u4e02\u4e03", "\u4e02\u4e03", "<>", "@<>"},
 230             {"\u4e01@@", "\u4e01@", "", "@"},
 231             {"\u4e01@@", "\u4e01@", "#", "#@"},
 232             {"\u4e01@@", "\u4e01@", "\u4e09", "\u4e09@"},
 233             {"\u4e01@@", "\u4e01@", "#\u4e09", "#\u4e09@"},
 234             {"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e02", "\u4e01\u4e02\u4e03".repeat(32771) + "end"},
 235             {"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04", "\u4e01\u4e04\u4e03".repeat(32771) + "end"},
 236             {"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "\u4e04\u4e05", "\u4e01\u4e04\u4e05\u4e03".repeat(32771) + "end"},
 237             {"\u4e01\u4e02\u4e03".repeat(32771) + "end", "\u4e02", "_", "\u4e01_\u4e03".repeat(32771) + "end"},
 238             {"\u4e01_\u4e03".repeat(32771) + "end", "_", "_", "\u4e01_\u4e03".repeat(32771) + "end"},
 239             {"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06", "\u4e01\u4e06\u4e03".repeat(32771) + "end"},
 240             {"\u4e01_\u4e03".repeat(32771) + "end", "_", "\u4e06\u4e06", "\u4e01\u4e06\u4e06\u4e03".repeat(32771) + "end"},
 241             {"X_Y".repeat(32771) + "end", "_", "\u4e07", "X\u4e07Y".repeat(32771) + "end"},
 242             {"X_Y".repeat(32771) + "end", "_", "\u4e07\u4e08", "X\u4e07\u4e08Y".repeat(32771) + "end"},
 243             {"X_Y".repeat(32771) + "end", "_", ".\u4e08.", "X.\u4e08.Y".repeat(32771) + "end"},
 244             {"\u4e0a".repeat(32771), "\u4e0a", "", ""},
 245             {"\u4e0a\u4e0b".repeat(32771), "\u4e0a", "", "\u4e0b".repeat(32771)},
 246             {"\u4e0a\u4e0b".repeat(32771), "\u4e0b", "", "\u4e0a".repeat(32771)},
 247             {"\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", ""},
 248             {"\u4e0b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "\u4e0b"},
 249             {"\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0a"},
 250             {"\u4e0b" + "\u4e0a\u4e0b".repeat(32771) + "\u4e0a", "\u4e0a\u4e0b", "", "\u4e0b\u4e0a"},
 251             {"b" + "\u4e0a\u4e0b".repeat(32771), "\u4e0a\u4e0b", "", "b"},
 252             {"\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "a"},
 253             {"b" + "\u4e0a\u4e0b".repeat(32771) + "a", "\u4e0a\u4e0b", "", "ba"},
 254         };
 255     }
 256 
 257     @DataProvider
 258     public static Iterator<Object[]> sourceTargetReplacement() {
 259         ArrayList<Object[]> list = new ArrayList<>();
 260         for (int maxSrcLen = 1; maxSrcLen <= (1 << 10); maxSrcLen <<= 1) {
 261             for (int maxTrgLen = 1; maxTrgLen <= (1 << 10); maxTrgLen <<= 1) {
 262                 for (int maxPrlLen = 1; maxPrlLen <= (1 << 10); maxPrlLen <<= 1) {
 263                     list.add(makeArray(makeRandomString(maxSrcLen),
 264                                        makeRandomString(maxTrgLen),
 265                                        makeRandomString(maxPrlLen)));
 266 
 267                     String source = makeRandomString(maxSrcLen);
 268                     list.add(makeArray(source,
 269                                        mekeRandomSubstring(source, maxTrgLen),
 270                                        makeRandomString(maxPrlLen)));
 271                 }
 272             }
 273         }


< prev index next >