#!/usr/bin/env qore
# -*- mode: qore; indent-tabs-mode: nil -*-

%new-style
%enable-all-warnings
%require-types
%strict-args

%requires ../../../../qlib/Util.qm
%requires ../../../../qlib/QUnit.qm

%exec-class FileStreamTest

public class FileStreamTest inherits QUnit::Test {

    constructor() : Test("FileStreamTest", "1.0") {
        addTestCase("basic test", \basic());

        # Return for compatibility with test harness that checks return value.
        set_return_value(main());
    }

    basic() {
        string file = tmp_location() + "/test";

        on_exit
            unlink(file);

        FileOutputStream fos(file);
        fos.write(<416263>);
        fos.write(<0A>);
        fos.close();

        FileOutputStream fos2(file, True);
        fos2.write(<3132330A>);
        fos2.close();

        FileInputStream fis(file);
        assertEq(<4162630A>, fis.read(4));
        assertEq(<31>, fis.read(1));
        assertEq(<32330A>, fis.read(4));
        assertEq(NOTHING, fis.read(10));

        fis = new FileInputStream(file);
        assertEq(65, fis.peek());
        assertEq(<4162630A>, fis.read(4));
        assertEq(49, fis.peek());
        assertEq(<31>, fis.read(1));
        assertEq(50, fis.peek());
        assertEq(<32330A>, fis.read(4));
        assertEq(-1, fis.peek());
        assertEq(NOTHING, fis.read(10));
        assertEq(-1, fis.peek());
    }
}
