Skip to content

Commit d665710

Browse files
committed
Update
1 parent c2901d8 commit d665710

File tree

2 files changed

+106
-24
lines changed

2 files changed

+106
-24
lines changed

src/FileFormats/NL/read.jl

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,19 @@ end
439439
function _parse_section(io::IO, ::Val{'S'}, model::_CacheModel)
440440
k = _next(Int, io, model)
441441
n = _next(Int, io, model)
442-
suffix = readline(io)
443-
@warn("Skipping suffix: `S$k $n$suffix`")
442+
suffix_name = if model.is_binary
443+
len = _next(Int, io, model)
444+
String(read(io, len))
445+
else
446+
strip(readline(io))
447+
end
448+
@warn("Skipping suffix: `S$k $n $suffix_name`")
449+
# The “4” bit of k indicates whether the suffix is real (i.e., double) valued
450+
# or integer valued: (k&4) != 0 --> real valued.
451+
T = ifelse(k & 4 != 0, Float64, Int)
444452
for _ in 1:n
453+
_ = _next(Int, io, model)
454+
_ = _next(T, io, model)
445455
_read_til_newline(io, model)
446456
end
447457
return

test/FileFormats/NL/read.jl

Lines changed: 94 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -573,14 +573,14 @@ function test_parse_S()
573573
io,
574574
"""
575575
S0 8 zork
576-
02
577-
16
578-
27
579-
38
580-
49
581-
53
582-
65
583-
84
576+
0 2
577+
1 6
578+
2 7
579+
3 8
580+
4 9
581+
5 3
582+
6 5
583+
8 4
584584
""",
585585
)
586586
seekstart(io)
@@ -592,6 +592,32 @@ function test_parse_S()
592592
return
593593
end
594594

595+
function test_parse_S_Float64()
596+
model = NL._CacheModel()
597+
io = IOBuffer()
598+
write(
599+
io,
600+
"""
601+
S4 8 zork
602+
0 2.0
603+
1 6.0
604+
2 7.0
605+
3 8.0
606+
4 9.0
607+
5 3.0
608+
6 5.0
609+
8 4.0
610+
""",
611+
)
612+
seekstart(io)
613+
@test_logs(
614+
(:warn, "Skipping suffix: `S4 8 zork`"),
615+
NL._parse_section(io, model),
616+
)
617+
@test eof(io)
618+
return
619+
end
620+
595621
function test_hs071()
596622
model = NL.Model()
597623
open(joinpath(@__DIR__, "data", "hs071.nl"), "r") do io
@@ -880,14 +906,14 @@ function test_binary_parse_O()
880906
model.is_binary = true
881907
NL._resize_variables(model, 4)
882908
io = IOBuffer()
883-
write(io, Char('O'), Int32(0), Int32(0))
884-
write(io, Char('o'), Int32(2))
885-
write(io, Char('v'), Int32(0))
886-
write(io, Char('o'), Int32(2))
887-
write(io, Char('v'), Int32(2))
888-
write(io, Char('o'), Int32(2))
889-
write(io, Char('v'), Int32(3))
890-
write(io, Char('v'), Int32(1))
909+
write(io, Cchar('O'), Int32(0), Int32(0))
910+
write(io, Cchar('o'), Int32(2))
911+
write(io, Cchar('v'), Int32(0))
912+
write(io, Cchar('o'), Int32(2))
913+
write(io, Cchar('v'), Int32(2))
914+
write(io, Cchar('o'), Int32(2))
915+
write(io, Cchar('v'), Int32(3))
916+
write(io, Cchar('v'), Int32(1))
891917
seekstart(io)
892918
NL._parse_section(io, model)
893919
@test eof(io)
@@ -902,7 +928,7 @@ function test_binary_parse_O_max()
902928
model.is_binary = true
903929
NL._resize_variables(model, 4)
904930
io = IOBuffer()
905-
write(io, Char('O'), Int32(0), Int32(1), Char('v'), Int32(0))
931+
write(io, Cchar('O'), Int32(0), Int32(1), Cchar('v'), Int32(0))
906932
seekstart(io)
907933
NL._parse_section(io, model)
908934
@test eof(io)
@@ -917,7 +943,7 @@ function test_binary_parse_x()
917943
model.is_binary = true
918944
NL._resize_variables(model, 5)
919945
io = IOBuffer()
920-
write(io, Char('x'), Int32(3))
946+
write(io, Cchar('x'), Int32(3))
921947
write(io, Int32(0), 1.1)
922948
write(io, Int32(3), 2.2)
923949
write(io, Int32(2), 3.3)
@@ -932,7 +958,7 @@ function test_parse_d()
932958
model = NL._CacheModel()
933959
model.is_binary = true
934960
io = IOBuffer()
935-
write(io, Char('d'), Int32(3))
961+
write(io, Cchar('d'), Int32(3))
936962
write(io, Int32(0), 1.1)
937963
write(io, Int32(3), 2.2)
938964
write(io, Int32(2), 3.3)
@@ -947,7 +973,7 @@ function test_binary_parse_r()
947973
model.is_binary = true
948974
NL._resize_constraints(model, 5)
949975
io = IOBuffer()
950-
write(io, Char('r'))
976+
write(io, Cchar('r'))
951977
write(io, Cchar('1'), 3.3)
952978
write(io, Cchar('3'))
953979
write(io, Cchar('0'), 1.1, 2.2)
@@ -966,7 +992,7 @@ function test_binary_parse_b()
966992
model.is_binary = true
967993
NL._resize_variables(model, 5)
968994
io = IOBuffer()
969-
write(io, Char('b'))
995+
write(io, Cchar('b'))
970996
write(io, Cchar('1'), 3.3)
971997
write(io, Cchar('3'))
972998
write(io, Cchar('0'), 1.1, 2.2)
@@ -985,7 +1011,7 @@ function test_binary_parse_k()
9851011
model.is_binary = true
9861012
NL._resize_variables(model, 3)
9871013
io = IOBuffer()
988-
write(io, Char('k'), Int32(2))
1014+
write(io, Cchar('k'), Int32(2))
9891015
write(io, Int32(2))
9901016
write(io, Int32(4))
9911017
seekstart(io)
@@ -994,6 +1020,52 @@ function test_binary_parse_k()
9941020
return
9951021
end
9961022

1023+
function test_binary_parse_S()
1024+
model = NL._CacheModel()
1025+
model.is_binary = true
1026+
io = IOBuffer()
1027+
write(io, Cchar('S'), Int32(0), Int32(8))
1028+
write(io, Int32(4), Cchar('z'), Cchar('o'), Cchar('r'), Cchar('k'))
1029+
write(io, Int32(0), Int32(2))
1030+
write(io, Int32(1), Int32(6))
1031+
write(io, Int32(2), Int32(7))
1032+
write(io, Int32(3), Int32(8))
1033+
write(io, Int32(4), Int32(9))
1034+
write(io, Int32(5), Int32(3))
1035+
write(io, Int32(6), Int32(5))
1036+
write(io, Int32(8), Int32(4))
1037+
seekstart(io)
1038+
@test_logs(
1039+
(:warn, "Skipping suffix: `S0 8 zork`"),
1040+
NL._parse_section(io, model),
1041+
)
1042+
@test eof(io)
1043+
return
1044+
end
1045+
1046+
function test_binary_parse_S_Float64()
1047+
model = NL._CacheModel()
1048+
model.is_binary = true
1049+
io = IOBuffer()
1050+
write(io, Cchar('S'), Int32(4), Int32(8))
1051+
write(io, Int32(4), Cchar('z'), Cchar('o'), Cchar('r'), Cchar('k'))
1052+
write(io, Int32(0), 2.1)
1053+
write(io, Int32(1), 6.1)
1054+
write(io, Int32(2), 7.1)
1055+
write(io, Int32(3), 8.1)
1056+
write(io, Int32(4), 9.1)
1057+
write(io, Int32(5), 3.1)
1058+
write(io, Int32(6), 5.1)
1059+
write(io, Int32(8), 4.1)
1060+
seekstart(io)
1061+
@test_logs(
1062+
(:warn, "Skipping suffix: `S4 8 zork`"),
1063+
NL._parse_section(io, model),
1064+
)
1065+
@test eof(io)
1066+
return
1067+
end
1068+
9971069
end
9981070

9991071
TestNonlinearRead.runtests()

0 commit comments

Comments
 (0)