1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
|
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/**
* @test
* @summary tests RegExp unicode case-insensitive match (?ui)
* @bug 8360459
* @library /lib/testlibrary/java/lang
* @run junit CaseFoldingTest
*/
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CaseFoldingTest {
@Test
void testUnicodeCaseInsensitiveMatch() throws Throwable {
var testAll = true; // true to test all codepoints defined in CaseFolding.txt
var verbose = true; // true to display all codepoints being tested
var filter = "^.*; [CTS]; .*$"; // update C,T,S to test different type
var excluded = Set.of(
// these 'S' characters failed for known reason. they don't map to their
// folding form with toUpperCase or toLowerCase, only map with case-folding.
// exclude them for now.
0x1fd3, // 1FD3 [lo: 1fd3, up: 1fd3] 0390 [lo: 0390, up: 0390]
0x1fe3, // 1FE3 [lo: 1fe3, up: 1fe3] 03B0 [lo: 03b0, up: 03b0]
0xfb05 // FB05 [lo: fb05, up: fb05] FB06 [lo: fb06, up: fb06]
);
var results = Files.lines(UCDFiles.CASEFOLDING)
.filter(line -> !line.startsWith("#") && line.matches(filter))
.map(line -> {
var strs = line.split("; ");
return new String[] {strs[0], strs[1], strs[2]};
})
.filter(cps -> {
var cp1 = Integer.parseInt(cps[0], 16);
var cp2 = Integer.parseInt(cps[2], 16);
if (excluded.contains(cp1))
return false;
if (testAll) {
return true;
}
// the folding codepoint doesn't map back to the original codepoint.
return Character.toUpperCase(cp2) != cp1 && Character.toLowerCase(cp2) != cp1;
})
.flatMap(cps -> {
// test slice, single & range
var cp = Integer.parseInt(cps[0], 16);
var folding = Integer.parseInt(cps[2], 16);
var errors = testCaseFolding(cp, folding);
if (verbose)
System.out.format(" [%s] %s [lo: %04x, up: %04x] %s [lo: %04x, up: %04x]\n",
cps[1],
cps[0],
Character.toLowerCase(cp),
Character.toUpperCase(cp),
cps[2],
Character.toLowerCase(folding),
Character.toUpperCase(folding)
);
errors.forEach(error -> System.out.print(error));
return errors.stream();
})
.collect(Collectors.toList());
assertEquals(results.size(), 0);
}
private static ArrayList<String> testCaseFolding(int cp, int folding) {
ArrayList<String> errors = new ArrayList<>();
testCaseFolding0(cp, folding, errors, "s-t");
testCaseFolding0(folding, cp, errors, "t-s");
// test all uppercase, lowercase combinations
var up = Character.toUpperCase(cp);
var lo = Character.toLowerCase(cp);
var folding_up = Character.toUpperCase(folding); // folding should be normally lowercase
if (up != cp) {
testCaseFolding0(up, folding, errors, "s(u)-t");
testCaseFolding0(folding, up, errors, "t-s(u)");
if (folding_up != folding) {
testCaseFolding0(up, folding_up, errors, "s(u)-t(u)");
testCaseFolding0(folding_up, up, errors, "t(u)-s(u)");
}
}
if (lo != cp) {
testCaseFolding0(lo, folding, errors, "s(l)-t");
testCaseFolding0(folding, lo, errors, "t-s(l)");
if (folding_up != folding) {
testCaseFolding0(lo, folding_up, errors, "s(l)-t(u)");
testCaseFolding0(folding_up, lo, errors, "t(u)-s(l)");
}
}
return errors;
}
private static void testCaseFolding0(int cp, int folding, ArrayList<String> errors, String type) {
var cp_str = Character.isSupplementaryCodePoint(cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(cp), (int)Character.lowSurrogate(cp))
: String.format("\\u%04x", cp);
var t = new String(Character.toChars(folding));
var p = String.format("(?iu)%s", cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] slice: %-20s t: u+%04x (%s)\n", p, folding, type));
}
p = String.format("(?iu)[%s]", cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] single: %-20s t: u+%04x (%s)\n", p, folding, type));
}
p = String.format("(?iu)[%s-%s]", cp_str, cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
// small range
var end_cp = cp + 16;
var end_cp_str = Character.isSupplementaryCodePoint(end_cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(end_cp), (int)Character.lowSurrogate(end_cp))
: String.format("\\u%04x", end_cp);
p = String.format("(?iu)[%s-%s]", cp_str, end_cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
end_cp = cp + 128; // bigger than the expanded_casefolding_map.
end_cp_str = Character.isSupplementaryCodePoint(end_cp)
? String.format("\\u%04x\\u%04x", (int)Character.highSurrogate(end_cp), (int)Character.lowSurrogate(end_cp))
: String.format("\\u%04x", end_cp);
p = String.format("(?iu)[%s-%s]", cp_str, end_cp_str);
if (Pattern.compile(p).matcher(t).matches() == false) {
errors.add(String.format(" [FAILED] range: %-20s t: u+%04x (%s)\n", p, folding, type));
}
}
}
|