File: ImmutableMatchResultTest.java

package info (click to toggle)
openjdk-21 21.0.8%2B9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 823,976 kB
  • sloc: java: 5,613,338; xml: 1,643,607; cpp: 1,296,296; ansic: 420,291; asm: 404,850; objc: 20,994; sh: 15,271; javascript: 11,245; python: 6,895; makefile: 2,362; perl: 357; awk: 351; sed: 172; jsp: 24; csh: 3
file content (233 lines) | stat: -rw-r--r-- 8,357 bytes parent folder | download | duplicates (7)
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
 * Copyright (c) 2023, 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.
 */

import jdk.test.lib.RandomFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;

import java.nio.CharBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Random;
import java.util.regex.MatchResult;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.params.provider.Arguments.arguments;

/*
 * @test
 * @bug 8132995 8312976
 * @key randomness
 *
 * @summary Tests to exercise the optimization described in the bug report.
 * @library /test/lib
 * @run junit ImmutableMatchResultTest
 */

public class ImmutableMatchResultTest {

    private static final int prefixLen;
    private static final int infixLen;
    private static final int suffixLen;
    private static final String group1 = "abc";
    private static final String group2 = "wxyz";
    private static final String group0;
    private static final String in;
    private static final String groupResults = "(([a-z]+)([0-9]*))";
    private static final String inResults;
    private static final String letters1 = "abcd";
    private static final String digits1 = "12";
    private static final String letters2 = "pqr";
    private static final String digits2 = "";

    static {
        Random rnd = RandomFactory.getRandom();
        prefixLen = rnd.nextInt(10);
        infixLen = rnd.nextInt(10);
        suffixLen = rnd.nextInt(10);
        group0 = group1 + "-".repeat(infixLen) + group2;
        in = "-".repeat(prefixLen) + group0 + "-".repeat(suffixLen);
        inResults = " ".repeat(prefixLen) + letters1 + digits1 + " ".repeat(infixLen) + letters2 + digits2 + " ".repeat(suffixLen);
    }

    private static void test(CharSequence cs) {
        Matcher m = Pattern.compile("(" + group1 + ")-*(" + group2 + ")").matcher(cs);
        assertTrue(m.find());

        assertEquals(prefixLen, m.start());
        assertEquals(prefixLen + group0.length(), m.end());
        assertEquals(group0, m.toMatchResult().group());

        assertEquals(prefixLen, m.start(1));
        assertEquals(prefixLen + group1.length(), m.end(1));
        assertEquals(group1, m.toMatchResult().group(1));

        assertEquals(prefixLen + group1.length() + infixLen, m.start(2));
        assertEquals(prefixLen + group1.length() + infixLen + group2.length(), m.end(2));
        assertEquals(group2, m.toMatchResult().group(2));
    }

    @Test
    void testString() {
        test(in);
    }

    @Test
    void testStringBuilder() {
        test(new StringBuilder(in));
    }

    @Test
    void testStringBuffer() {
        test(new StringBuffer(in));
    }

    @Test
    void testCharBuffer() {
        test(CharBuffer.wrap(in));
    }

    private static void testResultsStream(CharSequence cs) {
        Matcher m = Pattern.compile(groupResults).matcher(cs);
        List<MatchResult> results = m.results().toList();
        assertEquals(2, results.size());

        int startLetters1 = prefixLen;
        int endLetters1 = startLetters1 + letters1.length();
        int startDigits1 = endLetters1;
        int endDigits1 = startDigits1 + digits1.length();

        assertEquals(startLetters1, results.get(0).start());
        assertEquals(startLetters1, results.get(0).start(0));
        assertEquals(startLetters1, results.get(0).start(1));
        assertEquals(startLetters1, results.get(0).start(2));
        assertEquals(startDigits1, results.get(0).start(3));

        assertEquals(endDigits1, results.get(0).end());
        assertEquals(endDigits1, results.get(0).end(0));
        assertEquals(endDigits1, results.get(0).end(1));
        assertEquals(endLetters1, results.get(0).end(2));
        assertEquals(endDigits1, results.get(0).end(3));

        assertEquals(letters1 + digits1, results.get(0).group());
        assertEquals(letters1 + digits1, results.get(0).group(0));
        assertEquals(letters1 + digits1, results.get(0).group(1));
        assertEquals(letters1, results.get(0).group(2));
        assertEquals(digits1, results.get(0).group(3));

        int startLetters2 = endDigits1 + infixLen;
        int endLetters2 = startLetters2 + letters2.length();
        int startDigits2 = endLetters2;
        int endDigits2 = startDigits2 + digits2.length();

        assertEquals(startLetters2, results.get(1).start());
        assertEquals(startLetters2, results.get(1).start(0));
        assertEquals(startLetters2, results.get(1).start(1));
        assertEquals(startLetters2, results.get(1).start(2));
        assertEquals(startDigits2, results.get(1).start(3));

        assertEquals(endDigits2, results.get(1).end());
        assertEquals(endDigits2, results.get(1).end(0));
        assertEquals(endDigits2, results.get(1).end(1));
        assertEquals(endLetters2, results.get(1).end(2));
        assertEquals(endDigits2, results.get(1).end(3));

        assertEquals(letters2 + digits2, results.get(1).group());
        assertEquals(letters2 + digits2, results.get(1).group(0));
        assertEquals(letters2 + digits2, results.get(1).group(1));
        assertEquals(letters2, results.get(1).group(2));
        assertEquals(digits2, results.get(1).group(3));
    }

    @Test
    void testResultsStreamString() {
        testResultsStream(inResults);
    }

    @Test
    void testResultsStreamStringBuilder() {
        testResultsStream(new StringBuilder(inResults));
    }

    @Test
    void testResultsStreamStringBuffer() {
        testResultsStream(new StringBuffer(inResults));
    }

    @Test
    void testResultsStreamCharBuffer() {
        testResultsStream(CharBuffer.wrap(inResults));
    }

    static Arguments[] testGroupsOutsideMatch() {
        return new Arguments[]{
                arguments("(?<=(\\d{3}))\\D*(?=(\\d{4}))", "-1234abcxyz5678-"),
                arguments("(?<=(\\d{3}))\\D*(?=(\\1))", "-1234abcxyz2348-"),
                arguments("(?<!(\\d{4}))\\D+(?=(\\d{4}))", "123abcxyz5678-"),
        };
    }

    @ParameterizedTest
    @MethodSource
    void testGroupsOutsideMatch(String pattern, String text) {
        char[] data = text.toCharArray();
        Matcher m = Pattern.compile(pattern)
                .matcher(CharBuffer.wrap(data));

        assertEquals(2, m.groupCount());
        assertTrue(m.find());

        int start = m.start();
        int end = m.end();
        String group = m.group();

        int prefixStart = m.start(1);
        int prefixEnd = m.end(1);
        String prefixGroup = m.group(1);

        int suffixStart = m.start(2);
        int suffixEnd = m.end(2);
        String suffixGroup = m.group(2);

        MatchResult mr = m.toMatchResult();
        Arrays.fill(data, '*');  // spoil original input

        assertEquals(start, mr.start());
        assertEquals(end, mr.end());
        assertEquals(group, mr.group());

        assertEquals(prefixStart, mr.start(1));
        assertEquals(prefixEnd, mr.end(1));
        assertEquals(prefixGroup, mr.group(1));

        assertEquals(suffixStart, mr.start(2));
        assertEquals(suffixEnd, mr.end(2));
        assertEquals(suffixGroup, mr.group(2));
    }

}