Files
mercury/tests/hard_coded/read_binary_uint16.m
Zoltan Somogyi 25b4b67403 Carve io.file.m out of io.m.
library/io.file.m:
library/io.m:
    Move two sections of io.m, the "file handling predicates" section
    and the "handling temporary files" section to the new submodule io.file.m.

    Leave behind in io.m "forwarding predicates", predicates that do nothing
    except call the moved predicates in io.file.m, to provide backward
    compatibility. But do mark the forwarding predicates as obsolete,
    to tell people to update their (at their leisure, since the obsoleteness
    warning can be turned off).

    Also leave behind in io.m the definitions of the two types used
    by some parameters of some of the moved predicates. Document the reason
    why this is done.

library/MODULES_DOC:
    List the new module among the documented modules.

NEWS:
    Announce the changes.

browser/browse.m:
browser/interactive_query.m:
browser/listing.m:
compiler/analysis.file.m:
compiler/compile_target_code.m:
compiler/export.m:
compiler/fact_table.m:
compiler/file_util.m:
compiler/handle_options.m:
compiler/make.build.m:
compiler/make.module_dep_file.m:
compiler/make.module_target.m:
compiler/make.program_target.m:
compiler/make.util.m:
compiler/mercury_compile_main.m:
compiler/module_cmds.m:
compiler/parse_module.m:
compiler/passes_aux.m:
compiler/prog_event.m:
compiler/recompilation.check.m:
compiler/write_deps_file.m:
compiler/write_module_interface_files.m:
deep_profiler/conf.m:
deep_profiler/mdprof_cgi.m:
library/dir.m:
mdbcomp/program_representation.m:
ssdb/ssdb.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.

    In two modules, add a #include of fcntl.h in C code. These modules contain
    C code that needs this #include, but until now, they got it via a copy
    in an automatically generated C header file of a foreign_decl pragma
    in io.m that contained that #include. This diff moves that foreign_decl
    to io.file.m, removing that crutch.

tests/debugger/browser_test.m:
tests/hard_coded/bit_buffer_test.m:
tests/hard_coded/bitmap_test.m:
tests/hard_coded/construct_bug.m:
tests/hard_coded/dir_fold.m:
tests/hard_coded/dir_test.m:
tests/hard_coded/read_binary_int16.m:
tests/hard_coded/read_binary_int32.m:
tests/hard_coded/read_binary_int64.m:
tests/hard_coded/read_binary_uint16.m:
tests/hard_coded/read_binary_uint32.m:
tests/hard_coded/read_binary_uint64.m:
tests/hard_coded/read_bitmap_size.m:
tests/hard_coded/remove_file.m:
tests/hard_coded/write_binary.m:
tests/hard_coded/write_binary_int8.m:
tests/hard_coded/write_binary_multibyte_int.m:
tests/hard_coded/write_binary_uint8.m:
    Call the file operation predicates directly in io.file.m, not indirectly
    through io.m.
2022-03-08 06:01:21 +11:00

149 lines
4.6 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
%
% Test reading of binary uint16s.
%
% The .exp file is for little endian architectures.
% The .exp2 file is for big endian architectures.
%
%---------------------------------------------------------------------------%
:- module read_binary_uint16.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module io.file.
:- import_module list.
:- import_module string.
:- import_module uint16.
%---------------------------------------------------------------------------%
main(!IO) :-
list.foldl(run_test(big_endian), test_cases, !IO),
list.foldl(run_test(little_endian), test_cases, !IO),
list.foldl(run_test(native), test_cases, !IO).
:- pred run_test(byte_order::in, test_case::in, io::di, io::uo) is det.
run_test(ByteOrder, TestBytes, !IO) :-
io.file.remove_file(test_file, _, !IO),
io.write_string("================\n", !IO),
io.open_binary_output(test_file, OpenOutResult, !IO),
(
OpenOutResult = ok(OutFile),
io.write_string("Input: ", !IO),
io.write(TestBytes, !IO),
(
( TestBytes = []
; TestBytes = [_]
; TestBytes = [_, _, _ | _]
),
io.nl(!IO)
;
TestBytes = [Byte1, Byte2],
io.write_string(" (LE: ", !IO),
io.write_uint16(from_bytes_le(Byte1, Byte2), !IO),
io.write_string(") (BE: ", !IO),
io.write_uint16(from_bytes_be(Byte1, Byte2), !IO),
io.write_string(")\n", !IO)
),
list.foldl(write_binary_uint8(OutFile), TestBytes, !IO),
io.close_binary_output(OutFile, !IO),
io.open_binary_input(test_file, OpenInResult, !IO),
(
OpenInResult = ok(InFile),
(
ByteOrder = big_endian,
read_binary_uint16_be(InFile, ReadResult, !IO)
;
ByteOrder = little_endian,
read_binary_uint16_le(InFile, ReadResult, !IO)
;
ByteOrder = native,
read_binary_uint16(InFile, ReadResult, !IO)
),
io.close_binary_input(InFile, !IO),
(
ReadResult = ok(ResultUInt16),
io.write_string("Result: ", !IO),
io.write(ResultUInt16, !IO),
io.write_string(" (", !IO),
describe_byte_order(ByteOrder, !IO),
io.write_string(")\n", !IO)
;
ReadResult = eof,
io.write_string("Result: EOF (", !IO),
describe_byte_order(ByteOrder, !IO),
io.write_string(")\n", !IO)
;
ReadResult = incomplete(Bytes),
io.format("Result: Incomplete (%s) (", [s(string(Bytes))],
!IO),
describe_byte_order(ByteOrder, !IO),
io.write_string(")\n", !IO)
;
ReadResult = error(IO_Error),
io.format("Result: Error (%s)\n",
[s(io.error_message(IO_Error))], !IO)
),
io.file.remove_file(test_file, _, !IO)
;
OpenInResult = error(IO_Error),
io.format("I/O ERROR: %s\n", [s(io.error_message(IO_Error))],
!IO)
)
;
OpenOutResult = error(IO_Error),
io.format("I/O ERROR: %s\n", [s(io.error_message(IO_Error))], !IO)
).
:- pred describe_byte_order(byte_order::in, io::di, io::uo) is det.
describe_byte_order(ByteOrder, !IO) :-
(
ByteOrder = big_endian,
io.write_string("read big-endian", !IO)
;
ByteOrder = little_endian,
io.write_string("read little-endian", !IO)
;
ByteOrder = native,
io.write_string("read native byte order", !IO)
).
:- func test_file = string.
test_file = "read_binary_uint16.bin".
%---------------------------------------------------------------------------%
:- type byte_order
---> big_endian
; little_endian
; native.
:- type test_case == list(uint8).
:- func test_cases = list(test_case).
test_cases = [
[],
[0u8],
[1u8, 0u8],
[0xffu8, 0u8]
].
%---------------------------------------------------------------------------%
:- end_module read_binary_uint16.
%---------------------------------------------------------------------------%