- port-has-port-position? and port-position are now honest wrt

the supplied arguments for custom port constructors.
This commit is contained in:
Abdulaziz Ghuloum 2008-11-11 16:31:35 -05:00
parent c7d68432e3
commit ac8cb7d247
3 changed files with 45 additions and 28 deletions

View File

@ -275,18 +275,16 @@
(- pos (- ($port-size p) index)) (- pos (- ($port-size p) index))
(+ pos index)) (+ pos index))
(die who "invalid returned value from get-position" p)))] (die who "invalid returned value from get-position" p)))]
[(eqv? get-position #f) [(eqv? get-position #t)
(+ (vector-ref pos-vec 0) index)] (+ (vector-ref pos-vec 0) index)]
[else [else
(error who "port does not supprt port-position openration" p)])) (die who "port does not support port-position operation" p)]))
(die who "not a port" p))) (die who "not a port" p)))
(define (port-has-port-position? p) (define (port-has-port-position? p)
(define who 'port-has-port-position?) (define who 'port-has-port-position?)
(if (port? p) (if (port? p)
(let ([get-position ($port-get-position p)]) (and ($port-get-position p) #t)
(or (procedure? get-position)
(not get-position)))
(die who "not a port" p))) (die who "not a port" p)))
(define guarded-port (define guarded-port
@ -448,8 +446,8 @@
"*bytevector-input-port*" "*bytevector-input-port*"
(lambda (bv i c) 0) ;;; read! (lambda (bv i c) 0) ;;; read!
#f ;;; write! #f ;;; write!
#f #t ;;; get-position
#f #f ;;; set-position!
#f ;;; close #f ;;; close
#f #f
(vector 0))])) (vector 0))]))
@ -476,10 +474,10 @@
(bytevector-copy! bv i x 0 c) (bytevector-copy! bv i x 0 c)
(set! buf* (cons x buf*)))) (set! buf* (cons x buf*))))
c) c)
#f #t ;;; get-position
#f #f ;;; set-position!
#f #f ;;; close
#f #f ;;; cookie
(vector 0))]) (vector 0))])
(values (values
p p
@ -563,9 +561,9 @@
(set-output-string-cookie-strings! cookie (set-output-string-cookie-strings! cookie
(cons x (output-string-cookie-strings cookie))))) (cons x (output-string-cookie-strings cookie)))))
c) c)
#f #t ;;; get-position
#f #f ;;; set-position!
#f #f ;;; close!
cookie cookie
(vector 0)))) (vector 0))))
@ -618,10 +616,10 @@
id id
(lambda (str i c) 0) ;;; read! (lambda (str i c) 0) ;;; read!
#f ;;; write! #f ;;; write!
#f #t ;;; get-position
#f #f ;;; set-position!
#f ;;; close #f ;;; close
#f #f ;;; cookie
(vector 0))) (vector 0)))
(define (open-string-input-port str) (define (open-string-input-port str)
@ -1421,7 +1419,7 @@
(make-i/o-read-error))])))]) (make-i/o-read-error))])))])
refill) refill)
#f ;;; write! #f ;;; write!
#f ;;; get-position #t ;;; get-position
#f ;;; set-position! #f ;;; set-position!
(cond (cond
[(procedure? close) close] [(procedure? close) close]
@ -1460,7 +1458,7 @@
(io-error 'write id bytes (io-error 'write id bytes
(make-i/o-write-error))])))]) (make-i/o-write-error))])))])
refill) refill)
#f ;;; get-position #t ;;; get-position
#f ;;; set-position! #f ;;; set-position!
(cond (cond
[(procedure? close) close] [(procedure? close) close]

View File

@ -1 +1 @@
1668 1669

View File

@ -509,7 +509,7 @@
'() '()
(cons x (f))))))))) (cons x (f)))))))))
(define (file-size filename) (define (file-size-char-by-char filename)
(with-input-from-file filename (with-input-from-file filename
(lambda () (lambda ()
(let f ([i 0]) (let f ([i 0])
@ -541,6 +541,7 @@
(f (fx+ i 1))))) (f (fx+ i 1)))))
(define (test-input-files) (define (test-input-files)
(assert (= (file-size-char-by-char "tests/SRFI-1.ss") 56573))
(assert (= (file-size "tests/SRFI-1.ss") 56573)) (assert (= (file-size "tests/SRFI-1.ss") 56573))
(let ([bv (file->bytevector "tests/SRFI-1.ss")]) (let ([bv (file->bytevector "tests/SRFI-1.ss")])
(let-values ([(p extract) (open-bytevector-output-port #f)]) (let-values ([(p extract) (open-bytevector-output-port #f)])
@ -666,11 +667,29 @@
[(lambda (x) (equal? x "abcd")) [(lambda (x) (equal? x "abcd"))
(get-line (open-string-input-port "abcd\nefg"))]) (get-line (open-string-input-port "abcd\nefg"))])
(define (run-tests) (define (test-has-port-position)
(test-custom-binary-input-ports) (define-syntax check
(test-custom-binary-output-ports) (syntax-rules ()
(run-exhaustive-tests) [(_ e)
(test-input-files) (begin ;;; evaluating e twice
(test-partial-reads) (assert (not (port-has-port-position? e)))
(test-input-ports)) (assert
(guard (con
[(assertion-violation? con) #t]
[else #f])
(begin (port-position e) #f))))]))
(check (make-custom-binary-input-port "foo" (lambda a 0) #f #f #f))
(check (make-custom-binary-output-port "foo" (lambda a 0) #f #f #f))
(check (make-custom-textual-input-port "foo" (lambda a 0) #f #f #f))
(check (make-custom-textual-output-port "foo" (lambda a 0) #f #f #f)))
(define (run-tests)
(test-custom-binary-input-ports)
(test-custom-binary-output-ports)
(run-exhaustive-tests)
(test-input-files)
(test-partial-reads)
(test-input-ports)
(test-has-port-position))
) )