>From 1b7d287dc7aacbd63f8152903092af9c6bc46a6a Mon Sep 17 00:00:00 2001 From: Mario Domenech Goulart Date: Sun, 26 Jul 2020 12:42:42 +0200 Subject: [PATCH] read-lines: small refactoring and some tests This change: * removes a hardcoded arbitrary value for `max' which was clobbering the default value specified in the optional declaration when `read-lines' was called with #f as value for `max' * removes an apparently spurious use of `or' around `(eq? n 0)' * makes `read-lines' check whether `max' is a fixnum * adds a couple of tests for `read-lines' * document the default value of `max' in the manual --- extras.scm | 7 +++-- manual/Module (chicken io) | 7 +++-- tests/read-lines-tests.scm | 58 ++++++++++++++++++++++++++++++++++++++ tests/runtests.bat | 4 +++ tests/runtests.sh | 3 ++ 5 files changed, 73 insertions(+), 6 deletions(-) create mode 100644 tests/read-lines-tests.scm diff --git a/extras.scm b/extras.scm index ac85fe80..76c23a21 100644 --- a/extras.scm +++ b/extras.scm @@ -90,11 +90,12 @@ (loop (fx+ i 1)) ] ) ) ) ) ) ) ) ) ) ) ) ) (define read-lines - (lambda (#!optional (port ##sys#standard-input) (max most-positive-fixnum)) + (lambda (#!optional (port ##sys#standard-input) max) (##sys#check-input-port port #t 'read-lines) + (when max (##sys#check-fixnum max 'read-lines)) (let loop ((lns '()) - (n (or max 1000000000))) ; this is silly - (if (or (eq? n 0)) + (n (or max most-positive-fixnum))) + (if (eq? n 0) (##sys#fast-reverse lns) (let ((ln (read-line port))) (if (eof-object? ln) diff --git a/manual/Module (chicken io) b/manual/Module (chicken io) index 08eeb682..3c67b91b 100644 --- a/manual/Module (chicken io) +++ b/manual/Module (chicken io) @@ -52,9 +52,10 @@ characters per line. {{read-line}} returns a string without the terminating newl (read-lines [PORT [MAX]]) -Read {{MAX}} or fewer lines from {{PORT}}. {{PORT}} defaults to the -value of {{(current-input-port)}}. Returns a list of strings, each -string representing a line read, not including any line separation +Read {{MAX}} or fewer lines from {{PORT}}. {{MAX}} defaults to +{{most-positive-fixnum}} and {{PORT}} defaults to the value of +{{(current-input-port)}}. Returns a list of strings, each string +representing a line read, not including any line separation character(s). diff --git a/tests/read-lines-tests.scm b/tests/read-lines-tests.scm new file mode 100644 index 00000000..7c8aaacc --- /dev/null +++ b/tests/read-lines-tests.scm @@ -0,0 +1,58 @@ +;; Tests for `read-lines' + +(import + (chicken file) + (chicken io)) + +(include "test.scm") + +(define input-test-file "read-lines.in") + +(with-output-to-file input-test-file + (lambda () + (print #<