~ chicken-core (chicken-5) cfa1e750faeac1b0a267cbbd90befd11dd5e819f


commit cfa1e750faeac1b0a267cbbd90befd11dd5e819f
Author:     Mario Domenech Goulart <mario@parenteses.org>
AuthorDate: Sun Jul 26 12:42:42 2020 +0200
Commit:     Evan Hanson <evhan@foldling.org>
CommitDate: Thu Jul 30 09:06:45 2020 +1200

    Small refactoring and some tests for `read-lines'
    
    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 a 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
    
    Signed-off-by: Evan Hanson <evhan@foldling.org>

diff --git a/distribution/manifest b/distribution/manifest
index 504ddb5e..cf2dc045 100644
--- a/distribution/manifest
+++ b/distribution/manifest
@@ -131,6 +131,7 @@ tests/inline-unroll.scm
 tests/compiler-tests.scm
 tests/inlining-tests.scm
 tests/locative-stress-test.scm
+tests/read-lines-tests.scm
 tests/record-rename-test.scm
 tests/record-printer-test.scm
 tests/r4rstest.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..28cdd223
--- /dev/null
+++ b/tests/read-lines-tests.scm
@@ -0,0 +1,56 @@
+;; 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
Trap