2008-12-09 03:00:44 -05:00
|
|
|
|
|
|
|
(library (tests set-position)
|
|
|
|
(export run-tests)
|
|
|
|
(import (ikarus))
|
|
|
|
|
|
|
|
(define fname "temp-test-file")
|
2008-12-09 03:41:59 -05:00
|
|
|
(define pos-list '([500 12] [720 34] [12 180] [400 4]))
|
|
|
|
|
|
|
|
(define (write-bytes)
|
|
|
|
(when (file-exists? fname) (delete-file fname))
|
|
|
|
(let ([p (open-file-output-port fname)])
|
|
|
|
(for-each
|
|
|
|
(lambda (x)
|
|
|
|
(set-port-position! p (car x))
|
2008-12-09 03:47:08 -05:00
|
|
|
(assert (= (port-position p) (car x)))
|
|
|
|
(put-u8 p (cadr x))
|
|
|
|
(assert (= (port-position p) (add1 (car x)))))
|
2008-12-09 03:41:59 -05:00
|
|
|
pos-list)
|
|
|
|
(close-output-port p)))
|
|
|
|
|
|
|
|
(define (get-bytes)
|
|
|
|
(let ([p (open-file-input-port fname)])
|
|
|
|
(let ([bv (get-bytevector-all p)])
|
|
|
|
(close-input-port p)
|
|
|
|
bv)))
|
2008-12-09 03:00:44 -05:00
|
|
|
|
|
|
|
(define (test-setting-position-for-binary-output-files)
|
2008-12-09 03:41:59 -05:00
|
|
|
(write-bytes)
|
|
|
|
(let ([bv (get-bytes)])
|
2008-12-09 03:47:08 -05:00
|
|
|
(assert (= (bytevector-length bv) (add1 (apply max (map car pos-list)))))
|
2008-12-09 03:41:59 -05:00
|
|
|
(for-each
|
|
|
|
(lambda (x)
|
|
|
|
(assert (= (bytevector-u8-ref bv (car x)) (cadr x))))
|
|
|
|
pos-list))
|
|
|
|
(delete-file fname))
|
|
|
|
|
|
|
|
(define (test-setting-position-for-binary-input-files)
|
|
|
|
(write-bytes)
|
|
|
|
(let ([p (open-file-input-port fname)])
|
|
|
|
(define (check-pos x)
|
|
|
|
(set-port-position! p (car x))
|
2008-12-09 03:47:08 -05:00
|
|
|
(assert (= (port-position p) (car x)))
|
|
|
|
(assert (= (get-u8 p) (cadr x)))
|
|
|
|
(assert (= (port-position p) (add1 (car x)))))
|
2008-12-09 03:41:59 -05:00
|
|
|
(for-each check-pos pos-list)
|
|
|
|
(for-each check-pos (reverse pos-list))
|
|
|
|
(close-input-port p))
|
|
|
|
(delete-file fname))
|
2008-12-09 03:00:44 -05:00
|
|
|
|
2008-12-09 05:27:50 -05:00
|
|
|
(define (test-fixed-input-ports)
|
|
|
|
(assert (eof-object?
|
|
|
|
(let ([p (open-string-input-port "Hello")])
|
|
|
|
(set-port-position! p 5)
|
|
|
|
(get-char p))))
|
|
|
|
(assert (char=? #\o
|
|
|
|
(let ([p (open-string-input-port "Hello")])
|
|
|
|
(set-port-position! p 4)
|
|
|
|
(get-char p))))
|
|
|
|
(assert (eof-object?
|
|
|
|
(let ([p (open-bytevector-input-port #vu8(1 2 3 4 5))])
|
|
|
|
(set-port-position! p 5)
|
|
|
|
(get-u8 p))))
|
|
|
|
(assert (= 5
|
|
|
|
(let ([p (open-bytevector-input-port #vu8(1 2 3 4 5))])
|
|
|
|
(set-port-position! p 4)
|
|
|
|
(get-u8 p)))))
|
|
|
|
|
|
|
|
|
2008-12-09 03:00:44 -05:00
|
|
|
(define (run-tests)
|
2008-12-09 03:41:59 -05:00
|
|
|
(test-setting-position-for-binary-output-files)
|
2008-12-09 05:27:50 -05:00
|
|
|
(test-setting-position-for-binary-input-files)
|
|
|
|
(test-fixed-input-ports)))
|
2008-12-09 03:00:44 -05:00
|
|
|
|