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)

Preserve pending input order after peeking

Section titled “Preserve pending input order after peeking”

Given a file named “main.scm” with:

(import (scheme base))
(define port (open-input-string "<input>"))
(peek-char port)
(peek-u8 port)
(write-u8 (if (= (read-u8 port) <first-byte>) 65 66))

When I successfully run stak main.scm

Then the stdout should contain exactly “A”.

input first-byte
éx 195
😄x 240

Preserve pending input order when peeking a character

Section titled “Preserve pending input order when peeking a character”

Given a file named “main.scm” with:

(import (scheme base) (stak io))
(define port
(make-port
(lambda () #f)
#f
#f
(lambda () #f)
'(195 169 120)))
(peek-char port)
(for-each
(lambda (expected)
(write-u8 (if (= (read-u8 port) expected) 65 66)))
'(195 169 120))

When I successfully run stak main.scm

Then the stdout should contain exactly “AAA”.

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>”.

bytes output length
65 66 67 ABC 3
227 129 130 1
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>”.

bytes output length
65 A 1
65 66 AB 2
65 66 67 ABC 3