Hi, Attached is a patch with [hopefully] small improvements for `read-lines'. The patch:
* 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 All the best. Mario -- http://parenteses.org/mario
>From 1b7d287dc7aacbd63f8152903092af9c6bc46a6a Mon Sep 17 00:00:00 2001 From: Mario Domenech Goulart <ma...@parenteses.org> 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 <procedure>(read-lines [PORT [MAX]])</procedure> -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 #<<EOF +1 +2 +3 +4 +5 +EOF +))) + +(test-begin "read-lines") + +(test-equal + "without arguments" + '("1" "2" "3" "4" "5") + (with-input-from-file input-test-file read-lines)) + +(test-equal + "with a port as argument" + '("1" "2" "3" "4" "5") + (call-with-input-file input-test-file + (lambda (port) + (read-lines port)))) + +(test-equal + "with a limit" + '("1" "2") + (call-with-input-file input-test-file + (lambda (port) + (read-lines port 2)))) + +(test-error + "with an invalid first argument (port)" + (read-lines input-test-file)) + +(test-error + "with an invalid second argument (max)" + (call-with-input-file input-test-file + (lambda (port) + (read-lines port 2.0)))) + +(test-end "read-lines") + + +(delete-file input-test-file) + +(test-exit) diff --git a/tests/runtests.bat b/tests/runtests.bat index 29f2e821..e1803727 100644 --- a/tests/runtests.bat +++ b/tests/runtests.bat @@ -484,6 +484,10 @@ echo ======================================== port tests ... %interpret% -s port-tests.scm if errorlevel 1 exit /b 1 +echo ======================================== read-lines tests ... +%interpret% -s read-lines-tests.scm +if errorlevel 1 exit /b 1 + echo ======================================== fixnum tests ... %compile% fixnum-tests.scm if errorlevel 1 exit /b 1 diff --git a/tests/runtests.sh b/tests/runtests.sh index 5556ace6..92fd961f 100755 --- a/tests/runtests.sh +++ b/tests/runtests.sh @@ -362,6 +362,9 @@ $compile_r -static module-static-link.scm -o a.out echo "======================================== port tests ..." $interpret -s port-tests.scm +echo "======================================== read-lines tests ..." +$interpret -s read-lines-tests.scm + echo "======================================== fixnum tests ..." $compile fixnum-tests.scm ./a.out -- 2.20.1