;;;; run.scm - tests for buffer-ports (use buffer-ports lolevel extras srfi-4) (define lines-file (cond-expand (windows "buffer-ports.scm") (else "/usr/share/dict/words"))) (define data (make-blob 10000000)) (define data2 (make-blob 10000000)) (define-syntax time (syntax-rules () ((_ expr) (let*-values (((u1 s1) (cpu-time)) ((x) expr) ((u2 s2) (cpu-time))) (print "CPU: " (/ (- u2 u1) 1000) ", SYS: " (/ (- s2 s1) 1000)) x)))) (with-output-to-file "/tmp/data" (lambda () (write-u8vector (blob->u8vector/shared data)))) (read-all "/tmp/data") ; fill block buffers (print "read (u8vector):") (assert (equal? data (time (with-input-from-file "/tmp/data" (lambda () (read-u8vector! (blob-size data) (blob->u8vector/shared data2)) data2))))) (print "read (mmap):") (assert (equal? data (time (let ((in (open-memory-mapped-input-file "/tmp/data"))) (read-u8vector! (blob-size data) (blob->u8vector/shared data2) in) (close-input-port in) data2)))) (print "read lines (file):") (let ((lns (time (with-input-from-file lines-file read-lines)))) (print "read lines (buffer):") (assert (equal? lns (time (let* ((in (open-memory-mapped-input-file lines-file)) (lns2 (read-lines in))) (close-input-port in) lns2)))) (print (length lns))) (delete-file "/tmp/data") (set! data #f) (set! data2 #f) (define text (string->blob "this is a test.")) (print "read from blob:") (let ((in (open-input-buffer text #f 4))) (assert (string=? " is " (read-string 4 in))) (print " (gc)") (gc) (assert (string=? "a test." (read-string #f in))) (close-input-port in)) (print "read u32vector:") (let* ((buf '#u32(0 0 0)) (out (open-output-buffer buf))) (display "yes.1" out) (print buf))