-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathRegexTests.java
140 lines (130 loc) · 6.08 KB
/
RegexTests.java
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
/*
* SPDX-License-Identifier: Apache-2.0
*
* The OpenSearch Contributors require contributions made to
* this file be licensed under the Apache-2.0 license or a
* compatible open source license.
*/
/*
* Licensed to Elasticsearch under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
/*
* Modifications Copyright OpenSearch Contributors. See
* GitHub history for details.
*/
package org.opensearch.common.regex;
import org.opensearch.test.OpenSearchTestCase;
import java.util.Locale;
import java.util.Random;
import java.util.regex.Pattern;
import static org.hamcrest.Matchers.equalTo;
public class RegexTests extends OpenSearchTestCase {
public void testFlags() {
String[] supportedFlags = new String[] {
"CASE_INSENSITIVE",
"MULTILINE",
"DOTALL",
"UNICODE_CASE",
"CANON_EQ",
"UNIX_LINES",
"LITERAL",
"COMMENTS",
"UNICODE_CHAR_CLASS",
"UNICODE_CHARACTER_CLASS" };
int[] flags = new int[] {
Pattern.CASE_INSENSITIVE,
Pattern.MULTILINE,
Pattern.DOTALL,
Pattern.UNICODE_CASE,
Pattern.CANON_EQ,
Pattern.UNIX_LINES,
Pattern.LITERAL,
Pattern.COMMENTS,
Regex.UNICODE_CHARACTER_CLASS };
Random random = random();
int num = 10 + random.nextInt(100);
for (int i = 0; i < num; i++) {
int numFlags = random.nextInt(flags.length + 1);
int current = 0;
StringBuilder builder = new StringBuilder();
for (int j = 0; j < numFlags; j++) {
int index = random.nextInt(flags.length);
current |= flags[index];
builder.append(supportedFlags[index]);
if (j < numFlags - 1) {
builder.append("|");
}
}
String flagsToString = Regex.flagsToString(current);
assertThat(Regex.flagsFromString(builder.toString()), equalTo(current));
assertThat(Regex.flagsFromString(builder.toString()), equalTo(Regex.flagsFromString(flagsToString)));
Pattern.compile("\\w\\d{1,2}", current); // accepts the flags?
}
}
public void testDoubleWildcardMatch() {
assertTrue(Regex.simpleMatch("ddd", "ddd"));
assertTrue(Regex.simpleMatch("ddd", "Ddd", true));
assertFalse(Regex.simpleMatch("ddd", "Ddd"));
assertTrue(Regex.simpleMatch("d*d*d", "dadd"));
assertTrue(Regex.simpleMatch("**ddd", "dddd"));
assertTrue(Regex.simpleMatch("**ddD", "dddd", true));
assertFalse(Regex.simpleMatch("**ddd", "fff"));
assertTrue(Regex.simpleMatch("fff*ddd", "fffabcddd"));
assertTrue(Regex.simpleMatch("fff**ddd", "fffabcddd"));
assertFalse(Regex.simpleMatch("fff**ddd", "fffabcdd"));
assertTrue(Regex.simpleMatch("fff*******ddd", "fffabcddd"));
assertTrue(Regex.simpleMatch("fff*******ddd", "FffAbcdDd", true));
assertFalse(Regex.simpleMatch("fff*******ddd", "FffAbcdDd", false));
assertFalse(Regex.simpleMatch("fff******ddd", "fffabcdd"));
assertTrue(Regex.simpleMatch("abCDefGH******ddd", "abCDefGHddd", false));
assertTrue(Regex.simpleMatch("******", "a"));
assertTrue(Regex.simpleMatch("***WILDcard***", "aaaaaaaaWILDcardZZZZZZ", false));
assertFalse(Regex.simpleMatch("***xxxxx123456789xxxxxx***", "xxxxxabcdxxxxx", false));
assertFalse(Regex.simpleMatch("***xxxxxabcdxxxxx***", "xxxxxABCDxxxxx", false));
assertTrue(Regex.simpleMatch("***xxxxxabcdxxxxx***", "xxxxxABCDxxxxx", true));
assertTrue(Regex.simpleMatch("**stephenIsSuperCool**", "ItIsTrueThatStephenIsSuperCoolSoYouShouldLetThisIn", true));
assertTrue(
Regex.simpleMatch(
"**w**X**y**Z**",
"abcdeFGHIJKLMNOPqrstuvwabcdeFGHIJKLMNOPqrstuvwXabcdeFGHIJKLMNOPqrstuvwXyabcdeFGHIJKLMNOPqrstuvwXyZ",
false
)
);
}
public void testSimpleMatch() {
for (int i = 0; i < 1000; i++) {
final String matchingString = randomAlphaOfLength(between(0, 50));
// construct a pattern that matches this string by repeatedly replacing random substrings with '*' characters
String pattern = matchingString;
for (int shrink = between(0, 5); shrink > 0; shrink--) {
final int shrinkStart = between(0, pattern.length());
final int shrinkEnd = between(shrinkStart, pattern.length());
pattern = pattern.substring(0, shrinkStart) + "*" + pattern.substring(shrinkEnd);
}
assertTrue("[" + pattern + "] should match [" + matchingString + "]", Regex.simpleMatch(pattern, matchingString));
assertTrue(
"[" + pattern + "] should match [" + matchingString.toUpperCase(Locale.ROOT) + "]",
Regex.simpleMatch(pattern, matchingString.toUpperCase(Locale.ROOT), true)
);
// construct a pattern that does not match this string by inserting a non-matching character (a digit)
final int insertPos = between(0, pattern.length());
pattern = pattern.substring(0, insertPos) + between(0, 9) + pattern.substring(insertPos);
assertFalse("[" + pattern + "] should not match [" + matchingString + "]", Regex.simpleMatch(pattern, matchingString));
}
}
}