~ chicken-core (master) /tests/file-encoding-test.scm
Trap1(import (chicken io))
2(import (only (scheme base) write-string))
3
4(include "test.scm")
5
6(define utf-file "i-dont-know-i-just-work-here.utf-8.txt")
7(define latin-file "i-dont-know-i-just-work-here.latin-1.txt")
8
9(test-begin "file encoding")
10
11(define utf (with-input-from-file utf-file read-string #:unix))
12(define latin (with-input-from-file latin-file read-string #:unix 'latin-1))
13
14(test-equal "latin-1 decoded matches utf" latin utf)
15
16(with-output-to-file "latin.out"
17 (lambda () (write-string utf))
18 #:unix 'latin-1)
19
20(with-output-to-file "utf.out"
21 (lambda () (write-string latin))
22 #:unix)
23
24(let ((a (with-input-from-file "latin.out" read-bytevector #:binary))
25 (b (with-input-from-file latin-file read-bytevector #:binary)))
26 (test-equal "latin-1 encoded matches original" a b))
27
28(let ((a (with-input-from-file "utf.out" read-bytevector #:binary))
29 (b (with-input-from-file utf-file read-bytevector #:binary)))
30 (test-equal "utf-8 encoded matches original" a b))
31
32(with-output-to-file "chars.out"
33 (lambda ()
34 (display "äöü")
35 (write-char #\ß)
36 (display #\á))
37 #:unix 'latin-1)
38
39(define in (open-input-file "chars.out" #:unix 'latin-1))
40
41(test-equal "read latin-1 char" (read-char in) #\ä)
42(test-equal "peek latin-1 char" (peek-char in) #\ö)
43
44(test-equal "read remaining latin-1 chars"
45 (read-string #f in) "öüßá")
46
47(test-end)
48(test-exit)