2 *******************************************************************************
\r
3 * Copyright (C) 2009, International Business Machines Corporation and *
\r
4 * others. All Rights Reserved. *
\r
5 *******************************************************************************
\r
7 package com.ibm.icu.dev.test.translit;
\r
9 import java.util.ArrayList;
\r
10 import java.util.List;
\r
11 import java.util.regex.Matcher;
\r
12 import java.util.regex.Pattern;
\r
14 import com.ibm.icu.dev.test.TestFmwk;
\r
15 import com.ibm.icu.impl.UnicodeRegex;
\r
16 import com.ibm.icu.lang.UCharacter;
\r
17 import com.ibm.icu.lang.UProperty;
\r
18 import com.ibm.icu.lang.UProperty.NameChoice;
\r
19 import com.ibm.icu.text.Transliterator;
\r
20 import com.ibm.icu.text.UTF16;
\r
21 import com.ibm.icu.text.UnicodeSet;
\r
26 public class RegexUtilitiesTest extends TestFmwk {
\r
28 public static void main(String[] args) throws Exception {
\r
29 new RegexUtilitiesTest().run(args);
\r
33 * Check basic construction.
\r
35 public void TestConstruction() {
\r
36 String[][] tests = {
\r
39 {"[ba-z]", "[a-z]"},
\r
40 {"q[ba-z]", "q[a-z]"},
\r
41 {"[ba-z]q", "[a-z]q"},
\r
42 {"a\\p{joincontrol}b", "a[\u200C\u200D]b"},
\r
43 {"a\\P{joincontrol}b", "a[^\u200C\u200D]b"},
\r
44 {"a[[:whitespace:]&[:Zl:]]b", "a[\\\u2028]b"},
\r
45 {"a [[:bc=cs:]&[:wspace:]] b", "a [\u00A0\u202F] b"},
\r
47 for (int i = 0; i < tests.length; ++i) {
\r
48 final String source = tests[i][0];
\r
49 String expected = tests[i].length == 1 ? source : tests[i][1];
\r
50 String actual = UnicodeRegex.fix(source);
\r
51 assertEquals(source, expected, actual);
\r
55 Transliterator hex = Transliterator.getInstance("hex");
\r
58 * Perform an exhaustive test on all Unicode characters to make sure that the UnicodeSet with each
\r
61 public void TestCharacters() {
\r
62 UnicodeSet requiresQuote = new UnicodeSet("[\\$\\&\\-\\:\\[\\\\\\]\\^\\{\\}[:pattern_whitespace:]]");
\r
63 boolean skip = getInclusion() < 10;
\r
64 for (int cp = 0; cp < 0x110000; ++cp) {
\r
65 if (cp > 0xFF && skip && (cp % 37 != 0)) {
\r
68 String cpString = UTF16.valueOf(cp);
\r
69 String s = requiresQuote.contains(cp) ? "\\" + cpString : cpString;
\r
70 String pattern = null;
\r
71 final String rawPattern = "[" + s + s + "]";
\r
73 pattern = UnicodeRegex.fix(rawPattern);
\r
74 } catch (Exception e) {
\r
75 errln(e.getMessage());
\r
78 final String expected = "[" + s + "]";
\r
79 assertEquals("Doubled character works" + hex.transform(s), expected, pattern);
\r
81 // verify that we can create a regex pattern and use as expected
\r
82 String shouldNotMatch = UTF16.valueOf((cp + 1) % 0x110000);
\r
83 checkCharPattern(Pattern.compile(pattern), pattern, cpString, shouldNotMatch);
\r
85 // verify that the Pattern.compile works
\r
86 checkCharPattern(UnicodeRegex.compile(rawPattern), pattern, cpString, shouldNotMatch);
\r
91 * Check all integer Unicode properties to make sure they work.
\r
93 public void TestUnicodeProperties() {
\r
94 final boolean skip = getInclusion() < 10;
\r
95 UnicodeSet temp = new UnicodeSet();
\r
96 for (int propNum = UProperty.INT_START; propNum < UProperty.INT_LIMIT; ++propNum) {
\r
97 if (skip && (propNum % 5 != 0)) {
\r
100 String propName = UCharacter.getPropertyName(propNum, NameChoice.LONG);
\r
101 final int intPropertyMinValue = UCharacter.getIntPropertyMinValue(propNum);
\r
102 int intPropertyMaxValue = UCharacter.getIntPropertyMaxValue(propNum);
\r
103 if (skip) { // only test first if not exhaustive
\r
104 intPropertyMaxValue = intPropertyMinValue;
\r
106 for (int valueNum = intPropertyMinValue; valueNum <= intPropertyMaxValue; ++valueNum) {
\r
107 // hack for getting property value name
\r
108 String valueName = UCharacter.getPropertyValueName(propNum, valueNum, NameChoice.LONG);
\r
109 if (valueName == null) {
\r
110 valueName = UCharacter.getPropertyValueName(propNum, valueNum, NameChoice.SHORT);
\r
111 if (valueName == null) {
\r
112 valueName = Integer.toString(valueNum);
\r
115 temp.applyIntPropertyValue(propNum, valueNum);
\r
116 if (temp.size() == 0) {
\r
119 final String prefix = "a";
\r
120 final String suffix = "b";
\r
121 String shouldMatch = prefix + UTF16.valueOf(temp.charAt(0)) + suffix;
\r
123 String shouldNotMatch = prefix + UTF16.valueOf(temp.charAt(0)) + suffix;
\r
125 // posix style pattern
\r
126 String rawPattern = prefix + "[:" + propName + "=" + valueName + ":]" + suffix;
\r
127 String rawNegativePattern = prefix + "[:^" + propName + "=" + valueName + ":]" + suffix;
\r
128 checkCharPattern(UnicodeRegex.compile(rawPattern), rawPattern, shouldMatch, shouldNotMatch);
\r
129 checkCharPattern(UnicodeRegex.compile(rawNegativePattern), rawNegativePattern, shouldNotMatch, shouldMatch);
\r
131 // perl style pattern
\r
132 rawPattern = prefix + "\\p{" + propName + "=" + valueName + "}" + suffix;
\r
133 rawNegativePattern = prefix + "\\P{" + propName + "=" + valueName + "}" + suffix;
\r
134 checkCharPattern(UnicodeRegex.compile(rawPattern), rawPattern, shouldMatch, shouldNotMatch);
\r
135 checkCharPattern(UnicodeRegex.compile(rawNegativePattern), rawNegativePattern, shouldNotMatch, shouldMatch);
\r
140 public void TestBnf() {
\r
141 UnicodeRegex regex = new UnicodeRegex();
\r
142 final String[][] tests = {
\r
155 "uri = (?: (scheme) \\:)? (host) (?: \\? (query))? (?: \\u0023 (fragment))?;\n" +
\r
156 "scheme = reserved+;\n" +
\r
157 "host = // reserved+;\n" +
\r
158 "query = [\\=reserved]+;\n" +
\r
159 "fragment = reserved+;\n" +
\r
160 "reserved = [[:ascii:][:sc=grek:]&[:alphabetic:]];\n",
\r
161 "http://\u03B1\u03B2\u03B3?huh=hi#there"},
\r
166 for (int i = 0; i < tests.length; ++i) {
\r
167 String test = tests[i][0];
\r
168 final boolean expectException = tests[i].length < 2 ? false : tests[i][1].equals("Exception");
\r
171 if (test.endsWith(".txt")) {
\r
172 java.io.InputStream is = RegexUtilitiesTest.class.getResourceAsStream(test);
\r
173 List lines = UnicodeRegex.appendLines(new ArrayList(), is, "UTF-8");
\r
174 result = regex.compileBnf(lines);
\r
176 result = regex.compileBnf(test);
\r
178 if (expectException) {
\r
179 errln("Expected exception for " + test);
\r
182 result = result.replaceAll("[0-9]+%", ""); // just so we can use the language subtag stuff
\r
183 String resolved = regex.transform(result);
\r
185 Matcher m = Pattern.compile(resolved, Pattern.COMMENTS).matcher("");
\r
186 String checks = "";
\r
187 for (int j = 1; j < tests[i].length; ++j) {
\r
188 String check = tests[i][j];
\r
189 if (!m.reset(check).matches()) {
\r
190 checks = checks + "Fails " + check + "\n";
\r
192 for (int k = 1; k <= m.groupCount(); ++k) {
\r
193 checks += "(" + m.group(k) + ")";
\r
198 logln("Result: " + result + "\n" + checks + "\n" + test);
\r
199 } catch (Exception e) {
\r
200 if (!expectException) {
\r
201 errln(e.getClass().getName() + ": " + e.getMessage());
\r
209 * Utility for checking patterns
\r
211 private void checkCharPattern(Pattern pat, String matchTitle, String shouldMatch, String shouldNotMatch) {
\r
212 Matcher matcher = pat.matcher(shouldMatch);
\r
213 assertTrue(matchTitle + " and " + shouldMatch, matcher.matches());
\r
214 matcher.reset(shouldNotMatch);
\r
215 assertFalse(matchTitle + " and " + shouldNotMatch, matcher.matches());
\r