author | dholmes |
Wed, 06 Nov 2019 21:18:42 -0500 | |
changeset 58956 | 9a0a5e70eeb2 |
parent 58089 | e64fec9f1773 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
2 |
* Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 22 |
*/ |
23 |
||
24 |
/* @test |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
25 |
@bug 4074875 4063511 8230342 |
2 | 26 |
@summary Make sure LineNumberReader.read(char, int , int) will increase |
27 |
the linenumber correctly. |
|
28 |
*/ |
|
29 |
||
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
30 |
import java.io.IOException; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
31 |
import java.io.LineNumberReader; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
32 |
import java.io.StringReader; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
33 |
import java.util.function.Consumer; |
2 | 34 |
|
35 |
public class Read { |
|
36 |
||
37 |
public static void main(String[] args) throws Exception { |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
38 |
testReadChars(); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
39 |
testEofs(); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
40 |
} |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
41 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
42 |
private static void testReadChars() throws Exception { |
2 | 43 |
String s = "aaaa\nbbb\n"; |
44 |
char[] buf = new char[5]; |
|
45 |
int n = 0; |
|
46 |
int line = 0; |
|
47 |
||
48 |
LineNumberReader r = new LineNumberReader(new StringReader(s)); |
|
49 |
||
50 |
do { |
|
51 |
n = r.read(buf, 0, buf.length); |
|
52 |
} while (n > 0); |
|
53 |
||
54 |
line = r.getLineNumber(); |
|
55 |
||
56 |
if (line != 2) |
|
57 |
throw new Exception("Failed test: Expected line number: 2, got " |
|
58 |
+ line); |
|
59 |
} |
|
58089
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
60 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
61 |
private static void testEofs() throws Exception { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
62 |
String string = "first \n second"; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
63 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
64 |
Consumer<LineNumberReader> c = (LineNumberReader r) -> { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
65 |
try { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
66 |
while (r.read() != -1) |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
67 |
continue; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
68 |
} catch (IOException e) { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
69 |
throw new RuntimeException(e); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
70 |
} |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
71 |
}; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
72 |
testEof(c, string, 1); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
73 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
74 |
c = (LineNumberReader r) -> { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
75 |
try { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
76 |
char[] buf = new char[128]; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
77 |
while (r.read(buf) != -1) |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
78 |
continue; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
79 |
} catch (IOException e) { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
80 |
throw new RuntimeException(e); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
81 |
} |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
82 |
}; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
83 |
testEof(c, string, 1); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
84 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
85 |
c = (LineNumberReader r) -> { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
86 |
try { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
87 |
while (r.readLine() != null) |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
88 |
continue; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
89 |
} catch (IOException e) { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
90 |
throw new RuntimeException(e); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
91 |
} |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
92 |
}; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
93 |
testEof(c, string, 1); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
94 |
} |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
95 |
|
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
96 |
private static void testEof(Consumer<LineNumberReader> c, String s, int n) |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
97 |
throws Exception { |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
98 |
LineNumberReader r = new LineNumberReader(new StringReader(s)); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
99 |
c.accept(r); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
100 |
int line; |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
101 |
if ((line = r.getLineNumber()) != n) |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
102 |
throw new Exception("Failed test: Expected line number: " + n + |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
103 |
" , got " + line); |
e64fec9f1773
8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents:
47216
diff
changeset
|
104 |
} |
2 | 105 |
} |