-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtests-4.2-req.scm
77 lines (71 loc) · 2.44 KB
/
tests-4.2-req.scm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
(add-tests-with-string-output "eof-object"
[(eof-object? (eof-object)) => "#t\n"]
[(null? (eof-object)) => "#f\n"]
[(boolean? (eof-object)) => "#f\n"]
[(string? (eof-object)) => "#f\n"]
[(char? (eof-object)) => "#f\n"]
[(pair? (eof-object)) => "#f\n"]
[(symbol? (eof-object)) => "#f\n"]
[(procedure? (eof-object)) => "#f\n"]
[(vector? (eof-object)) => "#f\n"]
[(not (eof-object)) => "#f\n"]
[(eof-object? #\a) => "#f\n"]
[(eof-object? #t) => "#f\n"]
[(eof-object? 12) => "#f\n"]
[(eof-object? '(1 2 3)) => "#f\n"]
[(eof-object? '()) => "#f\n"]
[(eof-object? '#(foo)) => "#f\n"]
[(eof-object? (lambda (x) x)) => "#f\n"]
[(eof-object? 'baz) => "#f\n"]
)
(add-tests-with-string-output "read-char"
[(begin
(let ([p (open-output-file "stst.tmp" 'replace)])
(display "Hello World!" p)
(close-output-port p))
(let ([p (open-input-file "stst.tmp")])
(define loop
(lambda ()
(let ([x (read-char p)])
(if (eof-object? x)
(begin
(close-input-port p)
'())
(begin
(display x)
(loop))))))
(loop))
(exit))
=> "Hello World!"]
[(let ([s (make-string 10000)]
[t "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz12344567890<>,./?;:'\"[]{}\\|`~!@#$%^&*()-_=+"])
(define fill-string!
(lambda (i j)
(unless (fx= i (string-length s))
(if (fx>= j (string-length t))
(fill-string! i (fx- j (string-length t)))
(begin
(string-set! s i (string-ref t j))
(fill-string! (fxadd1 i) (fx+ j 17)))))))
(define write-string!
(lambda (i p)
(cond
[(fx= i (string-length s)) (close-output-port p)]
[else
(write-char (string-ref s i) p)
(write-string! (fxadd1 i) p)])))
(define verify
(lambda (i p)
(let ([x (read-char p)])
(cond
[(eof-object? x)
(close-input-port p)
(fx= i (string-length s))]
[(fx= i (string-length s)) (error 'verify "file too short")]
[(char= (string-ref s i) x)
(verify (fxadd1 i) p)]
[else (error 'verify "mismatch")]))))
(fill-string! 0 0)
(write-string! 0 (open-output-file "stst.tmp" 'replace))
(verify 0 (open-input-file "stst.tmp"))) => "#t\n"]
)