From bb38ef670f07e4b055cc305e3d2ac9987bee72cd Mon Sep 17 00:00:00 2001 From: odow Date: Tue, 20 Feb 2024 16:00:44 +1300 Subject: [PATCH] [FileFormats.NL] improve error msg when parsing unhandled headers --- src/FileFormats/NL/read.jl | 38 ++++++++++++++++++ test/FileFormats/NL/read.jl | 77 +++++++++++++++++++++++++++++++++++++ 2 files changed, 115 insertions(+) diff --git a/src/FileFormats/NL/read.jl b/src/FileFormats/NL/read.jl index 0d27000b91..359ca12ab8 100644 --- a/src/FileFormats/NL/read.jl +++ b/src/FileFormats/NL/read.jl @@ -372,6 +372,44 @@ function _parse_section(::IO, ::Val{T}, ::_CacheModel) where {T} return error("Unable to parse NL file: unhandled header $T") end +function _parse_section(::IO, ::Val{'F'}, ::_CacheModel) + return error( + "Unable to parse NL file: imported function descriptions ('F' " * + "sections) are not yet supported. To request support, please open an " * + "issue at https://github.com/jump-dev/MathOptInterface.jl with a " * + "reproducible example.", + ) +end + +function _parse_section(io::IO, ::Val{'S'}, model::_CacheModel) + k = _next(Int, io, model) + n = _next(Int, io, model) + suffix = readline(io) + @warn("Skipping suffix: `S$k $n$suffix`") + for _ in 1:n + _read_til_newline(io) + end + return +end + +function _parse_section(::IO, ::Val{'V'}, ::_CacheModel) + return error( + "Unable to parse NL file: defined variable definitions ('V' sections)" * + " are not yet supported. To request support, please open an issue at " * + "https://github.com/jump-dev/MathOptInterface.jl with a reproducible " * + "example.", + ) +end + +function _parse_section(::IO, ::Val{'L'}, ::_CacheModel) + return error( + "Unable to parse NL file: logical constraints ('L' sections) are not " * + "yet supported. To request support, please open an issue at " * + "https://github.com/jump-dev/MathOptInterface.jl with a reproducible " * + "example.", + ) +end + function _parse_section(io::IO, ::Val{'C'}, model::_CacheModel) index = _next(Int, io, model) + 1 _read_til_newline(io) diff --git a/test/FileFormats/NL/read.jl b/test/FileFormats/NL/read.jl index 3628987c47..1243e6fe9d 100644 --- a/test/FileFormats/NL/read.jl +++ b/test/FileFormats/NL/read.jl @@ -515,6 +515,83 @@ v1 return end +function test_parse_F() + model = NL._CacheModel() + io = IOBuffer() + write(io, "F") + seekstart(io) + @test_throws( + ErrorException( + "Unable to parse NL file: imported function descriptions ('F' " * + "sections) are not yet supported. To request support, please open an " * + "issue at https://github.com/jump-dev/MathOptInterface.jl with a " * + "reproducible example.", + ), + NL._parse_section(io, model), + ) + return +end + +function test_parse_V() + model = NL._CacheModel() + io = IOBuffer() + write(io, "V") + seekstart(io) + @test_throws( + ErrorException( + "Unable to parse NL file: defined variable definitions ('V' sections)" * + " are not yet supported. To request support, please open an issue at " * + "https://github.com/jump-dev/MathOptInterface.jl with a reproducible " * + "example.", + ), + NL._parse_section(io, model), + ) + return +end + +function test_parse_L() + model = NL._CacheModel() + io = IOBuffer() + write(io, "L") + seekstart(io) + @test_throws( + ErrorException( + "Unable to parse NL file: logical constraints ('L' sections) are not " * + "yet supported. To request support, please open an issue at " * + "https://github.com/jump-dev/MathOptInterface.jl with a reproducible " * + "example.", + ), + NL._parse_section(io, model), + ) + return +end + +function test_parse_S() + model = NL._CacheModel() + io = IOBuffer() + write( + io, + """ + S0 8 zork + 02 + 16 + 27 + 38 + 49 + 53 + 65 + 84 + """, + ) + seekstart(io) + @test_logs( + (:warn, "Skipping suffix: `S0 8 zork`"), + NL._parse_section(io, model), + ) + @test eof(io) + return +end + function test_hs071() model = NL.Model() open(joinpath(@__DIR__, "data", "hs071.nl"), "r") do io