test/jdk/java/io/LineNumberReader/Read.java
author dholmes
Wed, 06 Nov 2019 21:18:42 -0500
changeset 58956 9a0a5e70eeb2
parent 58089 e64fec9f1773
permissions -rw-r--r--
8233454: Test fails with assert(!is_init_completed(), "should only happen during init") after JDK-8229516 Reviewed-by: jiefu, dcubed
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
58089
e64fec9f1773 8230342: LineNumberReader.getLineNumber() returns inconsistent results after EOF
bpb
parents: 47216
diff changeset
    25
   @bug 4074875 4063511 8230342
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
   @summary Make sure LineNumberReader.read(char, int , int) will increase
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
            the linenumber correctly.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
   */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class Read {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        String s = "aaaa\nbbb\n";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
        char[] buf = new char[5];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
        int n = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        int line = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        LineNumberReader r = new LineNumberReader(new StringReader(s));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
        do {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            n = r.read(buf, 0, buf.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        } while (n > 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        line = r.getLineNumber();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        if (line != 2)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            throw new Exception("Failed test: Expected line number: 2, got "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
                                + line);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    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
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
}