1 /*
  2  * Copyright (c) 2015, 2024, 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 import org.testng.annotations.DataProvider;
 25 import org.testng.annotations.Test;
 26 
 27 import java.util.Map;
 28 import static org.testng.Assert.assertEquals;
 29 
 30 /*
 31  * @test
 32  * @bug 8077559 8248655 8264544
 33  * @summary Tests Compact String. This one is for String.regionMatches.
 34  * @run testng/othervm -XX:+CompactStrings RegionMatches
 35  * @run testng/othervm -XX:-CompactStrings RegionMatches
 36  */
 37 
 38 public class RegionMatches extends CompactString {
 39 
 40     @DataProvider
 41     public Object[][] provider() {
 42         return new Object[][] {
 43 
 44                 new Object[] { STRING_EMPTY, true, 0, "", 0, 0, true },
 45                 new Object[] { STRING_EMPTY, true, 0, "", 0, 1, false },
 46                 new Object[] { STRING_EMPTY, true, 0, "A", 0, 0, true },
 47                 new Object[] { STRING_EMPTY, true, 0, "", 0, 0, true },
 48                 new Object[] { STRING_EMPTY, true, 0, "", 0, 1, false },
 49                 new Object[] { STRING_L1, false, 0, "a", 0, 1, false },
 50                 new Object[] { STRING_L1, false, 0, "BA", 1, 1, true },
 51                 new Object[] { STRING_L1, false, 0, "Ba", 1, 1, false },
 52                 new Object[] { STRING_L1, true, 0, "a", 0, 1, true },
 53                 new Object[] { STRING_L1, true, 0, "BA", 1, 1, true },
 54                 new Object[] { STRING_L1, true, 0, "Ba", 1, 1, true },
 55                 new Object[] { STRING_L2, true, 1, "b", 0, 1, true },
 56                 new Object[] { STRING_L2, true, 1, "B", 0, 1, true },
 57                 new Object[] { STRING_L2, true, 0, "xaBc", 1, 2, true },
 58                 new Object[] { STRING_L2, false, 0, "AB", 0, 2, true },
 59                 new Object[] { STRING_L2, false, 0, "Ab", 0, 2, false },
 60                 new Object[] { STRING_L2, false, 1, "BAB", 2, 1, true },
 61                 new Object[] { STRING_LLONG, true, 1, "bCdEF", 0, 5, true },
 62                 new Object[] { STRING_LLONG, false, 2, "CDEFG", 0, 5, true },
 63                 new Object[] { STRING_LLONG, true, 2, "CDEFg", 0, 5, true },
 64                 new Object[] { STRING_U1, true, 0, "\uFF41", 0, 1, true },
 65                 new Object[] { STRING_U1, false, 0, "\uFF41", 0, 1, false },
 66                 new Object[] { STRING_MDUPLICATE1, true, 0, "\uFF41a\uFF41", 0,
 67                         3, true },
 68                 new Object[] { STRING_MDUPLICATE1, false, 0, "\uFF21a\uFF21",
 69                         0, 3, false },
 70                 new Object[] { STRING_SUPPLEMENTARY, true, 0, "\uD801\uDC28\uD801\uDC29",
 71                         0, 4, true },
 72                 new Object[] { STRING_SUPPLEMENTARY, true, 1, "\uDC00\uD801",
 73                         0, 2, true },
 74                 new Object[] { STRING_SUPPLEMENTARY, true, 1, "\uDC28",
 75                         0, 1, false },
 76                 new Object[] { STRING_SUPPLEMENTARY, true, 4, "\uFF21", 0, 1,
 77                         true },
 78                 new Object[] { STRING_SUPPLEMENTARY, true, 5, "A", 0, 1, true },
 79                 new Object[] { STRING_SUPPLEMENTARY, true, 0, "\uD802\uDC00\uD801\uDC01\uFF21A", 0, 2, false },
 80                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 0,
 81                         "\uD801\uDC28\uD801\uDC29", 0, 4, true },
 82                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 0,
 83                         "\uD801\uDC00\uD801\uDC01", 0, 4, true },
 84                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 1,
 85                         "\uDC28\uD801", 0, 2, true },
 86                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 1,
 87                         "\uDC00", 0, 1, false },
 88                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 1,
 89                         "\uDC00\uD801", 0, 2, false },
 90                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 4,
 91                         "\uFF21", 0, 1, true },
 92                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 4,
 93                         "\uFF21", 0, 1, false },
 94                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 4,
 95                         "\uFF41", 0, 1, true },
 96                 // Turkish I with dot
 97                 new Object[] { "\u0130", false, 0, "i", 0, 1, false },
 98                 new Object[] { "\u0130", true,  0, "i", 0, 1, true },
 99                 new Object[] { "\u0130", false, 0, "I", 0, 1, false },
100                 new Object[] { "\u0130", true,  0, "I", 0, 1, true },
101                 // i without dot
102                 new Object[] { "\u0131", false, 0, "i", 0, 1, false },
103                 new Object[] { "\u0131", true,  0, "i", 0, 1, true },
104                 new Object[] { "\u0131", false, 0, "I", 0, 1, false },
105                 new Object[] { "\u0131", true,  0, "I", 0, 1, true },
106                 // Exhaustive list of 1-char latin1 strings that match
107                 // case-insensitively:
108                 // for (char c1 = 0; c1 < 255; c1++) {
109                 //   for (char c2 = c; c2 < 255; c2++) {
110                 //     if (c1 == c2) continue;
111                 //     if (String.valueOf(c1).regionMatches(true, 0, String.valueOf(c2), 0, 1)) {
112                 //       System.out.println("                new Object[] { \"\\u00"
113                 //                          + Integer.toHexString((int)c) + "\", true, 0, \"\\u00"
114                 //                          + Integer.toHexString((int)c2) + "\", 0, 1, true },");
115                 // }}}
116                 new Object[] { "\u0041", true, 0, "\u0061", 0, 1, true },
117                 new Object[] { "\u0042", true, 0, "\u0062", 0, 1, true },
118                 new Object[] { "\u0043", true, 0, "\u0063", 0, 1, true },
119                 new Object[] { "\u0044", true, 0, "\u0064", 0, 1, true },
120                 new Object[] { "\u0045", true, 0, "\u0065", 0, 1, true },
121                 new Object[] { "\u0046", true, 0, "\u0066", 0, 1, true },
122                 new Object[] { "\u0047", true, 0, "\u0067", 0, 1, true },
123                 new Object[] { "\u0048", true, 0, "\u0068", 0, 1, true },
124                 new Object[] { "\u0049", true, 0, "\u0069", 0, 1, true },
125                 new Object[] { "\u004a", true, 0, "\u006a", 0, 1, true },
126                 new Object[] { "\u004b", true, 0, "\u006b", 0, 1, true },
127                 new Object[] { "\u004c", true, 0, "\u006c", 0, 1, true },
128                 new Object[] { "\u004d", true, 0, "\u006d", 0, 1, true },
129                 new Object[] { "\u004e", true, 0, "\u006e", 0, 1, true },
130                 new Object[] { "\u004f", true, 0, "\u006f", 0, 1, true },
131                 new Object[] { "\u0050", true, 0, "\u0070", 0, 1, true },
132                 new Object[] { "\u0051", true, 0, "\u0071", 0, 1, true },
133                 new Object[] { "\u0052", true, 0, "\u0072", 0, 1, true },
134                 new Object[] { "\u0053", true, 0, "\u0073", 0, 1, true },
135                 new Object[] { "\u0054", true, 0, "\u0074", 0, 1, true },
136                 new Object[] { "\u0055", true, 0, "\u0075", 0, 1, true },
137                 new Object[] { "\u0056", true, 0, "\u0076", 0, 1, true },
138                 new Object[] { "\u0057", true, 0, "\u0077", 0, 1, true },
139                 new Object[] { "\u0058", true, 0, "\u0078", 0, 1, true },
140                 new Object[] { "\u0059", true, 0, "\u0079", 0, 1, true },
141                 new Object[] { "\u005a", true, 0, "\u007a", 0, 1, true },
142                 new Object[] { "\u00c0", true, 0, "\u00e0", 0, 1, true },
143                 new Object[] { "\u00c1", true, 0, "\u00e1", 0, 1, true },
144                 new Object[] { "\u00c2", true, 0, "\u00e2", 0, 1, true },
145                 new Object[] { "\u00c3", true, 0, "\u00e3", 0, 1, true },
146                 new Object[] { "\u00c4", true, 0, "\u00e4", 0, 1, true },
147                 new Object[] { "\u00c5", true, 0, "\u00e5", 0, 1, true },
148                 new Object[] { "\u00c6", true, 0, "\u00e6", 0, 1, true },
149                 new Object[] { "\u00c7", true, 0, "\u00e7", 0, 1, true },
150                 new Object[] { "\u00c8", true, 0, "\u00e8", 0, 1, true },
151                 new Object[] { "\u00c9", true, 0, "\u00e9", 0, 1, true },
152                 new Object[] { "\u00ca", true, 0, "\u00ea", 0, 1, true },
153                 new Object[] { "\u00cb", true, 0, "\u00eb", 0, 1, true },
154                 new Object[] { "\u00cc", true, 0, "\u00ec", 0, 1, true },
155                 new Object[] { "\u00cd", true, 0, "\u00ed", 0, 1, true },
156                 new Object[] { "\u00ce", true, 0, "\u00ee", 0, 1, true },
157                 new Object[] { "\u00cf", true, 0, "\u00ef", 0, 1, true },
158                 new Object[] { "\u00d0", true, 0, "\u00f0", 0, 1, true },
159                 new Object[] { "\u00d1", true, 0, "\u00f1", 0, 1, true },
160                 new Object[] { "\u00d2", true, 0, "\u00f2", 0, 1, true },
161                 new Object[] { "\u00d3", true, 0, "\u00f3", 0, 1, true },
162                 new Object[] { "\u00d4", true, 0, "\u00f4", 0, 1, true },
163                 new Object[] { "\u00d5", true, 0, "\u00f5", 0, 1, true },
164                 new Object[] { "\u00d6", true, 0, "\u00f6", 0, 1, true },
165                 new Object[] { "\u00d8", true, 0, "\u00f8", 0, 1, true },
166                 new Object[] { "\u00d9", true, 0, "\u00f9", 0, 1, true },
167                 new Object[] { "\u00da", true, 0, "\u00fa", 0, 1, true },
168                 new Object[] { "\u00db", true, 0, "\u00fb", 0, 1, true },
169                 new Object[] { "\u00dc", true, 0, "\u00fc", 0, 1, true },
170                 new Object[] { "\u00dd", true, 0, "\u00fd", 0, 1, true },
171                 new Object[] { "\u00de", true, 0, "\u00fe", 0, 1, true },
172         };
173     }
174 
175     @Test(dataProvider = "provider")
176     public void testRegionMatches(String str, boolean ignoreCase, int toffset,
177             String other, int ooffset, int len, boolean expected) {
178         map.getOrDefault(str, Map.of(str, str)) // Allow data provider to test adhoc strings
179                 .forEach(
180                         (source, data) -> {
181                             assertEquals(
182                                     data.regionMatches(ignoreCase, toffset,
183                                             other, ooffset, len),
184                                     expected,
185                                     String.format(
186                                             "testing String(%s).regionMatches(%b, %d, %s, %d, %d), source : %s, ",
187                                             escapeNonASCIIs(data), ignoreCase,
188                                             toffset, escapeNonASCIIs(other),
189                                             ooffset, len, source));
190                         });
191     }
192 }