Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/Scanner/ScannerStreamTest.java
41149 views
1
/*
2
* Copyright (c) 2015, 2017, 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 java.io.File;
28
import java.io.IOException;
29
import java.io.UncheckedIOException;
30
31
import java.util.ArrayList;
32
import java.util.List;
33
import java.util.Scanner;
34
import java.util.function.Consumer;
35
import java.util.function.Supplier;
36
import java.util.regex.Matcher;
37
import java.util.regex.MatchResult;
38
import java.util.regex.Pattern;
39
import java.util.stream.LambdaTestHelpers;
40
import java.util.stream.OpTestCase;
41
import java.util.stream.Stream;
42
import java.util.stream.TestData;
43
44
import static org.testng.Assert.*;
45
46
/**
47
* @test
48
* @bug 8072722 8150488
49
* @summary Tests of stream support in java.util.Scanner
50
* @library /lib/testlibrary/bootlib
51
* @build java.base/java.util.stream.OpTestCase
52
* @run testng/othervm ScannerStreamTest
53
*/
54
55
@Test
56
public class ScannerStreamTest extends OpTestCase {
57
58
static File inputFile = new File(System.getProperty("test.src", "."), "input.txt");
59
60
@DataProvider(name = "Tokens")
61
public static Object[][] makeTokensTestData() {
62
// each inner array is [String description, String input, String delimiter]
63
// delimiter may be null
64
List<Object[]> data = new ArrayList<>();
65
66
data.add(new Object[] { "default delimiter", "abc def ghi", null });
67
data.add(new Object[] { "fixed delimiter", "abc,def,,ghi", "," });
68
data.add(new Object[] { "regex delimiter", "###abc##def###ghi###j", "#+" });
69
70
return data.toArray(new Object[0][]);
71
}
72
73
/*
74
* Creates a scanner over the input, applying a delimiter if non-null.
75
*/
76
Scanner makeScanner(String input, String delimiter) {
77
Scanner sc = new Scanner(input);
78
if (delimiter != null) {
79
sc.useDelimiter(delimiter);
80
}
81
return sc;
82
}
83
84
/*
85
* Given input and a delimiter, tests that tokens() returns the same
86
* results that would be provided by a Scanner hasNext/next loop.
87
*/
88
@Test(dataProvider = "Tokens")
89
public void tokensTest(String description, String input, String delimiter) {
90
// derive expected result by using conventional loop
91
Scanner sc = makeScanner(input, delimiter);
92
List<String> expected = new ArrayList<>();
93
while (sc.hasNext()) {
94
expected.add(sc.next());
95
}
96
97
Supplier<Stream<String>> ss = () -> makeScanner(input, delimiter).tokens();
98
withData(TestData.Factory.ofSupplier(description, ss))
99
.stream(LambdaTestHelpers.identity())
100
.expectedResult(expected)
101
.exercise();
102
}
103
104
/*
105
* Creates a Scanner over the given input file.
106
*/
107
Scanner makeFileScanner(File file) {
108
try {
109
return new Scanner(file, "UTF-8");
110
} catch (IOException ioe) {
111
throw new UncheckedIOException(ioe);
112
}
113
}
114
115
/*
116
* Tests that the matches produced by findAll(pat) are the same
117
* as what are returned by findWithinHorizon(pat, 0). This tests
118
* a single pattern against a single input file.
119
*/
120
public void findAllFileTest() {
121
// derive expected result by using conventional loop
122
Pattern pat = Pattern.compile("[A-Z]{7,}");
123
List<String> expected = new ArrayList<>();
124
125
try (Scanner sc = makeFileScanner(inputFile)) {
126
String match;
127
while ((match = sc.findWithinHorizon(pat, 0)) != null) {
128
expected.add(match);
129
}
130
}
131
132
Supplier<Stream<String>> ss =
133
() -> makeFileScanner(inputFile).findAll(pat).map(MatchResult::group);
134
135
withData(TestData.Factory.ofSupplier("findAllFileTest", ss))
136
.stream(LambdaTestHelpers.identity())
137
.expectedResult(expected)
138
.exercise();
139
}
140
141
@DataProvider(name = "FindAllZero")
142
public static Object[][] makeFindAllZeroTestData() {
143
// each inner array is [String input, String patternString]
144
List<Object[]> data = new ArrayList<>();
145
146
data.add(new Object[] { "aaaaa", "a*" });
147
data.add(new Object[] { "aaaaab", "a*" });
148
data.add(new Object[] { "aaaaabb", "a*" });
149
data.add(new Object[] { "aaaaabbb", "a*" });
150
data.add(new Object[] { "aaabbaaaa", "a*" });
151
data.add(new Object[] { "aaabbaaaab", "a*" });
152
data.add(new Object[] { "aaabbaaaabb", "a*" });
153
data.add(new Object[] { "aaabbaaaabbb", "a*" });
154
data.add(new Object[] { "aaabbaaaa", "a*|b*" });
155
data.add(new Object[] { "aaabbaaaab", "a*|b*" });
156
data.add(new Object[] { "aaabbaaaabb", "a*|b*" });
157
data.add(new Object[] { "aaabbaaaabbb", "a*|b*" });
158
159
return data.toArray(new Object[0][]);
160
}
161
162
/*
163
* Tests findAll() using a pattern against an input string.
164
* The results from findAll() should equal the results obtained
165
* using a loop around Matcher.find().
166
*
167
* The provided regexes should allow zero-length matches.
168
* This primarily tests the auto-advance feature of findAll() that
169
* occurs if the regex match is of zero length to see if it has the
170
* same behavior as Matcher.find()'s auto-advance (JDK-8150488).
171
* Without auto-advance, findAll() would return an infinite stream
172
* of zero-length matches. Apply a limit to the stream so
173
* that an infinite stream will be truncated. The limit must be
174
* high enough that the resulting truncated stream won't be
175
* mistaken for a correct expected result.
176
*/
177
@Test(dataProvider = "FindAllZero")
178
public void findAllZeroTest(String input, String patternString) {
179
Pattern pattern = Pattern.compile(patternString);
180
181
// generate expected result using Matcher.find()
182
Matcher m = pattern.matcher(input);
183
List<String> expected = new ArrayList<>();
184
while (m.find()) {
185
expected.add(m.group());
186
}
187
188
Supplier<Stream<String>> ss = () -> new Scanner(input).findAll(pattern)
189
.limit(100)
190
.map(MatchResult::group);
191
192
withData(TestData.Factory.ofSupplier("findAllZeroTest", ss))
193
.stream(LambdaTestHelpers.identity())
194
.expectedResult(expected)
195
.exercise();
196
}
197
}
198
199