1 /*
   2  * Copyright (c) 2015, 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 static org.testng.Assert.assertEquals;
  28 
  29 /*
  30  * @test
  31  * @bug 8077559
  32  * @summary Tests Compact String. This one is for String.regionMatches.
  33  * @run testng/othervm -XX:+CompactStrings RegionMatches
  34  * @run testng/othervm -XX:-CompactStrings RegionMatches
  35  */
  36 
  37 public class RegionMatches extends CompactString {
  38 
  39     @DataProvider
  40     public Object[][] provider() {
  41         return new Object[][] {
  42 
  43                 new Object[] { STRING_EMPTY, true, 0, "", 0, 0, true },
  44                 new Object[] { STRING_EMPTY, true, 0, "", 0, 1, false },
  45                 new Object[] { STRING_EMPTY, true, 0, "A", 0, 0, true },
  46                 new Object[] { STRING_EMPTY, true, 0, "", 0, 0, true },
  47                 new Object[] { STRING_EMPTY, true, 0, "", 0, 1, false },
  48                 new Object[] { STRING_L1, false, 0, "a", 0, 1, false },
  49                 new Object[] { STRING_L1, false, 0, "BA", 1, 1, true },
  50                 new Object[] { STRING_L1, false, 0, "Ba", 1, 1, false },
  51                 new Object[] { STRING_L1, true, 0, "a", 0, 1, true },
  52                 new Object[] { STRING_L1, true, 0, "BA", 1, 1, true },
  53                 new Object[] { STRING_L1, true, 0, "Ba", 1, 1, true },
  54                 new Object[] { STRING_L2, true, 1, "b", 0, 1, true },
  55                 new Object[] { STRING_L2, true, 1, "B", 0, 1, true },
  56                 new Object[] { STRING_L2, true, 0, "xaBc", 1, 2, true },
  57                 new Object[] { STRING_L2, false, 0, "AB", 0, 2, true },
  58                 new Object[] { STRING_L2, false, 0, "Ab", 0, 2, false },
  59                 new Object[] { STRING_L2, false, 1, "BAB", 2, 1, true },
  60                 new Object[] { STRING_LLONG, true, 1, "bCdEF", 0, 5, true },
  61                 new Object[] { STRING_LLONG, false, 2, "CDEFG", 0, 5, true },
  62                 new Object[] { STRING_LLONG, true, 2, "CDEFg", 0, 5, true },
  63                 new Object[] { STRING_U1, true, 0, "\uFF41", 0, 1, true },
  64                 new Object[] { STRING_U1, false, 0, "\uFF41", 0, 1, false },
  65                 new Object[] { STRING_MDUPLICATE1, true, 0, "\uFF41a\uFF41", 0,
  66                         3, true },
  67                 new Object[] { STRING_MDUPLICATE1, false, 0, "\uFF21a\uFF21",
  68                         0, 3, false },
  69                 new Object[] { STRING_SUPPLEMENTARY, true, 1, "\uDC00\uD801",
  70                         0, 2, true },
  71                 new Object[] { STRING_SUPPLEMENTARY, true, 4, "\uFF21", 0, 1,
  72                         true },
  73                 new Object[] { STRING_SUPPLEMENTARY, true, 5, "A", 0, 1, true },
  74                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 0,
  75                         "\uD801\uDC28\uD801\uDC29", 0, 4, true },
  76                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 1,
  77                         "\uDC28\uD801", 0, 2, true },
  78                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 1,
  79                         "\uDC00\uD801", 0, 2, false },
  80                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, true, 4,
  81                         "\uFF21", 0, 1, true },
  82                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 4,
  83                         "\uFF21", 0, 1, false },
  84                 new Object[] { STRING_SUPPLEMENTARY_LOWERCASE, false, 4,
  85                         "\uFF41", 0, 1, true }, };
  86     }
  87 
  88     @Test(dataProvider = "provider")
  89     public void testRegionMatches(String str, boolean ignoreCase, int toffset,
  90             String other, int ooffset, int len, boolean expected) {
  91         map.get(str)
  92                 .forEach(
  93                         (source, data) -> {
  94                             assertEquals(
  95                                     data.regionMatches(ignoreCase, toffset,
  96                                             other, ooffset, len),
  97                                     expected,
  98                                     String.format(
  99                                             "testing String(%s).regionMatches(%b, %d, %s, %d, %d), source : %s, ",
 100                                             escapeNonASCIIs(data), ignoreCase,
 101                                             toffset, escapeNonASCIIs(other),
 102                                             ooffset, len, source));
 103                         });
 104     }
 105 }