% WAM-lite stream/I/O smoke tests.

ok :-
    current_output(S0),
    set_output(S0),
    current_output(S1),
    S0 == S1,

    File = 'build/tmp_wam_io_test.txt',
    open(File, write, S),
    tab(S, 3),
    write(S, hello),
    nl(S),
    flush_output(S),
    close(S),

    open(File, read, S2),
    stream_property(S2, file_name(FN)),
    FN == File,

    set_stream(S2, alias(foo)),
    stream_property(foo, alias(foo)),
    close(S2),

    Empty = 'build/tmp_wam_io_empty.txt',
    open(Empty, write, S3),
    close(S3),
    open(Empty, read, S4),
    at_end_of_stream(S4),
    set_input(S4),
    at_end_of_stream,
    close(S4),

    Txt = 'build/tmp_wam_io_text.txt',
    open(Txt, write, TW),
    put_code(TW, 65),
    put_char(TW, b),
    nl(TW),
    close(TW),
    open(Txt, read, TR),
    peek_code(TR, C0), C0 == 65,
    get_code(TR, C1), C1 == 65,
    get_char(TR, Ch0), Ch0 == b,
    unget_char(TR, b),
    peek_char(TR, Ch1), Ch1 == b,
    get_char(TR, Ch2), Ch2 == b,
    get_code(TR, NLCode), NLCode == 10,
    get_char(TR, EOF), EOF == end_of_file,
    close(TR),

    Bin = 'build/tmp_wam_io_bin.dat',
    open(Bin, write, BW, [type(binary)]),
    put_byte(BW, 255),
    close(BW),
    open(Bin, read, BR, [type(binary)]),
    peek_byte(BR, B0), B0 == 255,
    get_byte(BR, B1), B1 == 255,
    unget_byte(BR, 255),
    get_byte(BR, B2), B2 == 255,
    get_byte(BR, B3), B3 == -1,
    close(BR),

    ReadFile = 'build/tmp_wam_read_term.pl',
    open(ReadFile, write, RW),
    write(RW, 'foo(a).'), nl(RW),
    write(RW, 'bar(X,_).'), nl(RW),
    close(RW),
    open(ReadFile, read, RR),
    read_term(RR, T1, []), T1 = foo(a),
    read_term(RR, T2, [variables(Vs), variable_names(VNs), singletons(Ss)]),
    functor(T2, bar, 2),
    arg(1, T2, A1), var(A1),
    arg(2, T2, A2), var(A2),
    A1 \== A2,
    Vs = [V1, V2], V1 == A1, V2 == A2,
    VNs = [Name=Var], Name == 'X', Var == A1,
    Ss = [Name2=Var2], Name2 == 'X', Var2 == A1,
    read_term(RR, EOF2, []), EOF2 == end_of_file,
    close(RR).
