Skip to content

Port

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (port? <expression>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

expression
(current-input-port)
(current-output-port)

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (input-port? (current-input-port)) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (if (output-port? <expression>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

expression
(current-output-port)
(current-error-port)

Given a file named “main.scm” with:

(import (scheme base) (scheme file))
(define port <expression>)
(write-u8 (if (input-port-open? port) 65 66))
(close-input-port port)
(write-u8 (if (input-port-open? port) 65 66))

And a file named “foo.txt” with:

When I successfully run stak main.scm

Then the stdout should contain exactly “AB”.

expression
(open-input-file “foo.txt”)
(open-input-string “foo”)

Given a file named “main.scm” with:

(import (scheme base) (scheme file))
(define port <expression>)
(write-u8 (if (output-port-open? port) 65 66))
(close-output-port port)
(write-u8 (if (output-port-open? port) 65 66))

And a file named “foo.txt” with:

When I successfully run stak main.scm

Then the stdout should contain exactly “AB”.

expression
(open-output-file “foo.txt”)
(open-output-string)

Given a file named “main.scm” with:

(import (scheme base))
(call-with-port
(open-input-string "<string>")
(lambda (port)
(parameterize ((current-input-port port))
(do ((x (read-u8) (read-u8)))
((eof-object? x) #f)
(write-u8 x)))))

When I successfully run stak main.scm

Then the stdout should contain exactly “<string>”.

string
ABC
😄

Given a file named “main.scm” with:

(import (scheme base))
(call-with-port
(open-output-string)
(lambda (port)
(parameterize ((current-output-port port))
(for-each write-u8 '(<bytes>)))
(let ((xs (get-output-string port)))
(unless (= (string-length xs) <length>)
(error "invalid length"))
(write-string xs))))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

bytesoutputlength
65 66 67ABC3
227 129 1301
240 159 152 132😄1

Given a file named “main.scm” with:

(import (scheme base))
(call-with-port
(open-input-bytevector #u8(65 66 67))
(lambda (port)
(parameterize ((current-input-port port))
(do ((x (read-u8) (read-u8)))
((eof-object? x) #f)
(write-u8 x)))))

When I successfully run stak main.scm.

Given a file named “main.scm” with:

(import (scheme base))
(call-with-port
(open-output-bytevector)
(lambda (port)
(parameterize ((current-output-port port))
(for-each write-u8 '(<bytes>)))
(let ((xs (get-output-bytevector port)))
(unless (= (bytevector-length xs) <length>)
(error "invalid length"))
(write-bytevector xs))))

When I successfully run stak main.scm

Then the stdout should contain exactly “<output>”.

bytesoutputlength
65A1
65 66AB2
65 66 67ABC3