Skip to content

Commit 080fe60

Browse files
Fix handling of CRLF line endings in read-mps
1 parent bf6930c commit 080fe60

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

Diff for: .gitattributes

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
# ensure line endings are preserved for the mps test files
3+
t/data/simple-problem.mps text eol=lf
4+
t/data/simple-problem-crlf.mps text eol=crlf

Diff for: src/external-formats.lisp

+2-2
Original file line numberDiff line numberDiff line change
@@ -167,9 +167,9 @@ boundaries are supported."
167167
(:raw
168168
raw)))))
169169

170-
(iter (for line = (read-line stream))
170+
(iter (for line = (string-right-trim '(#\Space #\Return) (read-line stream)))
171171
(if-let (header-card (unless (char= #\space (aref line 0))
172-
(string-downcase (string-right-trim " " (substring line 0 15)))))
172+
(string-downcase (substring line 0 15))))
173173
(cond
174174
;; do nothing on comments
175175
((char= (aref header-card 0) #\*))

Diff for: t/data/simple-problem-crlf.mps

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*This is a simple MPS problem to test the MPS reader
2+
NAME simple
3+
ROWS
4+
N obj
5+
L row1
6+
L row2
7+
COLUMNS
8+
X obj 1 row1 3
9+
Y obj 4 row1 1
10+
Y row2 1
11+
Z obj 8 row2 2
12+
RHS
13+
rhs1 row1 8 row2 7
14+
ENDATA

Diff for: t/external-formats.lisp

+19-1
Original file line numberDiff line numberDiff line change
@@ -224,8 +224,26 @@
224224
(is (set-equal '((z . (0 . 4)) (|w| . (0 . 1)) (x . (nil . nil)))
225225
(problem-var-bounds problem)))
226226
(is (simple-linear-constraint-set-equal '((<= ((x . 3) (y . 1)) 8) (<= ((y . 1) (z . 2)) 10) (<= ((|w| . -1) (x . -2) (z . 1)) 1))
227-
(problem-constraints problem))))))
227+
(problem-constraints problem))
228228

229+
(with-open-file (stream (merge-pathnames "t/data/simple-problem-crlf.mps"
230+
(asdf:system-source-directory :linear-programming-test))
231+
:direction :input
232+
:external-format :utf-8)
233+
(let ((problem (read-mps stream 'max)))
234+
(is (typep problem 'problem))
235+
(is (eq 'max (problem-type problem)))
236+
(is-true (null (symbol-package (problem-objective-var problem))))
237+
(is (set-equal '(x y z)
238+
(map 'list #'identity (problem-vars problem))))
239+
(is (set-equal '((x . 1) (y . 4) (z . 8))
240+
(problem-objective-func problem)))
241+
(is (set-equal '()
242+
(problem-integer-vars problem)))
243+
(is (set-equal '()
244+
(problem-var-bounds problem)))
245+
(is (simple-linear-constraint-set-equal '((<= ((x . 3) (y . 1)) 8) (<= ((y . 1) (z . 2)) 7))
246+
(problem-constraints problem)))))))))
229247

230248
(test write-standard-format
231249
(let* ((problem (make-linear-problem (max (+ x y)) (<= (+ (* 2 x) y) 5)))

0 commit comments

Comments
 (0)