Skip to content

Read

Read a byte

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (read-u8))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.

Read bytes

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (read-u8))
(write-u8 (read-u8))
(write-u8 (read-u8))

And a file named “input.txt” with:

ABC

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “ABC”.

Peek a byte

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (peek-u8))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.

Peek a byte multiple times

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (peek-u8))
(write-u8 (peek-u8))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “AA”.

Peek and read bytes

Given a file named “main.scm” with:

(import (scheme base))
(write-u8 (peek-u8))
(write-u8 (read-u8))
(write-u8 (read-u8))

And a file named “input.txt” with:

AB

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “AAB”.

Read a character

Given a file named “main.scm” with:

(import (scheme base))
(write-char (read-char))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.

Peek a character

Given a file named “main.scm” with:

(import (scheme base))
(write-char (peek-char))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.

Peek a character multiple times

Given a file named “main.scm” with:

(import (scheme base))
(write-char (peek-char))
(write-char (peek-char))

And a file named “input.txt” with:

A

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “AA”.

Peek and read characters

Given a file named “main.scm” with:

(import (scheme base))
(write-char (peek-char))
(write-char (read-char))
(write-char (read-char))

And a file named “input.txt” with:

AB

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “AAB”.

Read a value

Given a file named “main.scm” with:

(import (scheme base) (scheme read))
(write-u8 (if (equal? (read) '<value>) 65 66))

And a file named “input.txt” with:

<value>

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.

Examples

value
#f
#t
0
1
2
42
-1
-2
-42
a
x
foo
#\A
#\newline
""
"foo"
"Hello, world!"
"\n\r\t”
()
(1)
(1 2)
(1 2 3)
(1 . 2)
(1 2 . 3)
(foo)
(foo bar)
(foo bar baz)
(foo . bar)
(foo bar . baz)
#(foo)
#(foo bar)
#(foo bar baz)
#u8()
#u8(1)
#u8(1 2)
#u8(1 2 3)

Read from a port

Given a file named “main.scm” with:

(import (scheme base) (scheme read))
(write-u8 (if (equal? (read (current-input-port)) 'foo) 65 66))

And a file named “input.txt” with:

foo

When I run scheme main.scm interactively

And I pipe in the file “input.txt”

Then the exit status should be 0

And the stdout should contain exactly “A”.