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

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

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

%exec-class BinaryInputStreamTest

public class BinaryInputStreamTest inherits QUnit::Test {

    constructor() : Test("BinaryInputStreamTest", "1.0") {
        addTestCase("ctor empty", \ctorEmpty());
        addTestCase("read test", \readTest());
        addTestCase("peek test", \peekTest());

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

    ctorEmpty() {
        BinaryInputStream bis(binary());
        assertEq(NOTHING, bis.read(100));
        assertEq(NOTHING, bis.read(100));
    }

    readTest() {
        BinaryInputStream bis(<010203>);
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(0); });
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(-2); });
        assertEq(<0102>, bis.read(2));
        assertEq(<03>, bis.read(2));
        assertEq(NOTHING, bis.read(2));
        assertEq(NOTHING, bis.read(10));

        bis = new BinaryInputStream("abc");
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(0); });
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(-2); });
        assertEq(<6162>, bis.read(2));
        assertEq(<63>, bis.read(2));
        assertEq(NOTHING, bis.read(2));
        assertEq(NOTHING, bis.read(10));
    }

    peekTest() {
        BinaryInputStream bis(<010203>);
        assertEq(1, bis.peek());
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(0); });
        assertThrows("INPUT-STREAM-ERROR", sub() { bis.read(-2); });
        assertEq(1, bis.peek());
        assertEq(<0102>, bis.read(2));
        assertEq(3, bis.peek());
        assertEq(<03>, bis.read(2));
        assertEq(-1, bis.peek());
    }
}
