import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.StringReader;

public class CollapsingLineTerminatorTest {
    private static final String STRING = "\r\ntest";

    public static void main(String[] args) throws IOException {
        System.out.println("Methods considering skipLF:");
        // All of these method calls read / skip one character
        // They should ignore the \n which was part of \r\n
        System.out.println(" read: " + successful(testRead()));
        System.out.println(" read(char[]): " + successful(testReadBuff()));
        System.out.println(" mark/reset: " + successful(testMarkReset()));
        System.out.println(" skip: " + successful(testSkip()));
    }

    private static boolean successful(Reader reader) throws IOException {
        // \r\n was read by initial read(), then specific test method should have
        // read `t`, so next character is expected to be `e`
        return reader.read() == 'e';
    }

    private static LineNumberReader createReader() throws IOException {
        LineNumberReader reader = new LineNumberReader(new StringReader(STRING));
        // Collapses \r\n into \n, i.e. sets skipLF
        reader.read();

        return reader;
    }

    private static LineNumberReader testRead() throws IOException {
        LineNumberReader reader = createReader();
        reader.read();
        return reader;
    }

    private static LineNumberReader testReadBuff() throws IOException {
        LineNumberReader reader = createReader();
        reader.read(new char[1]);
        return reader;
    }

    private static LineNumberReader testMarkReset() throws IOException {
        LineNumberReader reader = createReader();
        // Mark / reset restored skipLF
        // readAheadLimit is also adjusted, see JDK-8218280
        reader.mark(1);
        reader.read();
        reader.reset();
        reader.read();
        return reader;
    }

    private static LineNumberReader testSkip() throws IOException {
        LineNumberReader reader = createReader();
        reader.skip(1);
        return reader;
    }
}
