Files
mercury/tests/hard_coded/read_bitmap_size.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

106 lines
3.2 KiB
Mathematica

%---------------------------------------------------------------------------%
% vim: ft=mercury ts=4 sw=4 et
%---------------------------------------------------------------------------%
% This a regression test for the issue in commit 3e3dbab
% (Github pull req #76): when reading a bitmap from a binary file stream the
% size of the result bitmap was being set based on the size of the file
% without regard for our current position in the stream. This was leading
% to an abort since less bytes than % expected ended up being read from
% the stream.
%---------------------------------------------------------------------------%
:- module read_bitmap_size.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
%---------------------------------------------------------------------------%
%---------------------------------------------------------------------------%
:- implementation.
:- import_module bitmap.
:- import_module exception.
:- import_module int.
:- import_module io.file.
:- import_module list.
:- import_module string.
%---------------------------------------------------------------------------%
main(!IO) :-
io.open_binary_output(test_file, OpenResult, !IO),
(
OpenResult = ok(TestFile),
list.foldl(write_binary_uint8(TestFile),
[1u8, 2u8, 4u8, 8u8, 16u8, 32u8, 64u8, 128u8, 255u8], !IO),
io.close_binary_output(TestFile, !IO),
do_test(0, !IO),
do_test(1, !IO),
do_test(2, !IO),
do_test(4, !IO),
do_test(8, !IO),
do_test(9, !IO)
;
OpenResult = error(IOError),
io.error_message(IOError, Msg),
io.print_line(Msg, !IO)
),
io.file.remove_file(test_file, _, !IO).
:- pred do_test(int::in, io::di, io::uo) is det.
do_test(N, !IO) :-
io.format("SKIP %d: ", [i(N)], !IO),
io.open_binary_input(test_file, OpenResult, !IO),
(
OpenResult = ok(File),
read_and_discard(File, N, !IO),
io.read_binary_file_as_bitmap(File, ReadRes, !IO),
(
ReadRes = ok(Bitmap),
ByteString = bitmap.to_byte_string(Bitmap),
io.print_line(ByteString, !IO),
io.close_binary_input(File, !IO)
;
ReadRes = error(IOError),
io.error_message(IOError, Msg),
io.print_line(Msg, !IO)
)
;
OpenResult = error(IOError),
io.error_message(IOError, Msg),
io.print_line(Msg, !IO)
).
% We could use seek here, but that isn't supported properly
% by all backends.
:- pred read_and_discard(io.binary_input_stream::in, int::in,
io::di, io::uo) is det.
read_and_discard(File, N, !IO) :-
( if N > 0 then
read_binary_uint8(File, ReadResult, !IO),
(
ReadResult = ok(_),
read_and_discard(File, N - 1, !IO)
;
ReadResult = eof
;
ReadResult = error(IOError),
throw(software_error(io.error_message(IOError)))
)
else
true
).
:- func test_file = string.
test_file = "read_bitmap_size.bin".
%---------------------------------------------------------------------------%
:- end_module read_bitmap_size.
%---------------------------------------------------------------------------%