Debian Codegraph @

test/include/srfi-181-192-tests.scm

raw
1 ;; -*- coding:utf-8 -*-
2
3 (cond-expand
4 (gauche (import (scheme base)
5 (scheme write)
6 (scheme read)
7 (gauche base)
8 (srfi 1)
9 (srfi 13)
10 (srfi 64)))
11 (chibi (import (except (scheme base)
12 input-port? output-port? textual-port? binary-port?
13 port? close-port close-input-port close-output-port
14 read-char peek-char read-line char-ready?
15 read-string read-u8 peek-u8 u8-ready?
16 read-bytevector read-bytevector!
17 write-char write-string write-u8
18 write-bytevector flush-output-port file-error?)
19 (srfi 1)
20 (srfi 130)
21 (except (chibi test) test-equal)
22 (rename (chibi test) (test-equal chibi:test-equal))
23 (srfi 181 adapter)))
24 )
25
26 (import (srfi 181))
27 (import (srfi 192))
28
29 (cond-expand
30 (chibi
31 ;; adapt srfi-64 macros to (chibi test)
32 (define-syntax test-equal
33 (syntax-rules ()
34 ((_ expect expr) (chibi:test-equal equal? expect expr))
35 ((_ name expect expr) (chibi:test-equal equal? name expect expr))))
36 (define-syntax test-eqv
37 (syntax-rules ()
38 ((_ expect expr) (chibi:test-equal eqv? expect expr))
39 ((_ name expect expr) (chibi:test-equal eqv? name expect expr)))))
40 (else))
41
42 (test-begin "srfi-181-192-test")
43
44 (test-group
45 "Binary input, no port positioning"
46 (define data (apply bytevector
47 (list-tabulate 1000 (lambda (i) (modulo i 256)))))
48 (define pos 0)
49 (define closed #f)
50 (define p (make-custom-binary-input-port
51 "binary-input"
52 (lambda (buf start count) ; read!
53 (let ((size (min count (- (bytevector-length data) pos))))
54 (bytevector-copy! buf start data pos (+ pos size))
55 (set! pos (+ pos size))
56 size))
57 #f ; get-position
58 #f ; set-position
59 (lambda () (set! closed #t)))) ; close
60 (test-assert "port?" (port? p))
61 (test-assert "input-port?" (input-port? p))
62 (test-assert "not output port?" (not (output-port? p)))
63 (test-assert "not has position?" (not (port-has-port-position? p)))
64 (test-assert "not set position?" (not (port-has-set-port-position!? p)))
65
66 (test-eqv 0 (read-u8 p))
67 (test-eqv 1 (read-u8 p))
68 (test-eqv 2 (peek-u8 p))
69 (test-eqv 2 (read-u8 p))
70
71 (test-equal (bytevector-copy data 3)
72 (read-bytevector 997 p))
73 (test-equal (eof-object) (read-u8 p))
74
75 (test-assert "close" (begin (close-port p)
76 closed))
77 )
78
79 (test-group
80 "Binary input, port positioning"
81 (define data (apply bytevector
82 (list-tabulate 1000 (lambda (i) (modulo i 256)))))
83 (define pos 0)
84 (define saved-pos #f)
85 (define closed #f)
86 (define p (make-custom-binary-input-port
87 "binary-input"
88 (lambda (buf start count) ; read!
89 (let ((size (min count (- (bytevector-length data) pos))))
90 (bytevector-copy! buf start data pos (+ pos size))
91 (set! pos (+ pos size))
92 size))
93 (lambda () pos) ; get-position
94 (lambda (k) (set! pos k)) ; set-position
95 (lambda () (set! closed #t)) ;close
96 ))
97 (test-assert "port?" (port? p))
98 (test-assert "input-port?" (input-port? p))
99 (test-assert "not output port?" (not (output-port? p)))
100 (test-assert "has position?" (port-has-port-position? p))
101 (test-assert "set position?" (port-has-set-port-position!? p))
102
103 (test-eqv 0 (read-u8 p))
104 (test-eqv 1 (read-u8 p))
105 (test-eqv 2 (peek-u8 p))
106 (set! saved-pos (port-position p))
107 (test-eqv 2 (read-u8 p))
108
109 (test-equal (bytevector-copy data 3)
110 (read-bytevector 997 p))
111 (test-equal (eof-object) (read-u8 p))
112
113 (set-port-position! p saved-pos)
114 (test-eqv 2 (read-u8 p))
115
116 (test-assert "close" (begin (close-port p)
117 closed))
118 )
119
120 (test-group
121 "Textual input, no port positioning"
122 (define data (string-tabulate (lambda (i)
123 (integer->char
124 (cond-expand
125 (full-unicode (+ #x3000 i))
126 (else (modulo i 256)))))
127 1000))
128 (define pos 0)
129 (define closed #f)
130 (define p (make-custom-textual-input-port
131 "textual-input"
132 (lambda (buf start count) ; read!
133 (let ((size (min count (- (string-length data) pos))))
134 (unless (zero? size)
135 (if (string? buf)
136 (string-copy! buf start data pos (+ pos size))
137 (do ((i 0 (+ i 1))
138 (j pos (+ j 1)))
139 ((= i size) (set! pos j))
140 (vector-set! buf (+ start i) (string-ref data j)))))
141 size))
142 #f ; get-position
143 #f ; set-position
144 (lambda () (set! closed #t)); close
145 ))
146 (test-assert "port?" (port? p))
147 (test-assert "input-port?" (input-port? p))
148 (test-assert "not output port?" (not (output-port? p)))
149 (test-assert "not has position?" (not (port-has-port-position? p)))
150 (test-assert "not set position?" (not (port-has-set-port-position!? p)))
151
152 (test-eqv (string-ref data 0) (read-char p))
153 (test-eqv (string-ref data 1) (read-char p))
154 (test-eqv (string-ref data 2) (peek-char p))
155 (test-eqv (string-ref data 2) (read-char p))
156
157 (test-equal (string-copy data 3)
158 (read-string 997 p))
159 (test-equal (eof-object) (read-char p))
160
161 (test-assert "close" (begin (close-port p)
162 closed))
163 )
164
165 (test-group
166 "Textual input, port positioning"
167 (define data (string-tabulate (lambda (i)
168 (integer->char
169 (cond-expand
170 (full-unicode (+ #x3000 i))
171 (else (modulo i 256)))))
172 1000))
173 (define pos 0)
174 (define saved-pos #f)
175 (define closed #f)
176 (define p (make-custom-textual-input-port
177 "textual-input"
178 (lambda (buf start count) ; read!
179 (list start count)
180 (let ((size (min count (- (string-length data) pos))))
181 (unless (zero? size)
182 (if (string? buf)
183 (string-copy! buf start data pos (+ pos size))
184 (do ((i 0 (+ i 1))
185 (j pos (+ j 1)))
186 ((= i size) (set! pos j))
187 (vector-set! buf (+ start i) (string-ref data j)))))
188 size))
189 (lambda () pos) ; get-position
190 (lambda (k) (set! pos k)) ; set-position
191 (lambda () (set! closed #t)); close
192 ))
193 (test-assert (port? p))
194 (test-assert (input-port? p))
195 (test-assert (not (output-port? p)))
196 (test-assert (port-has-port-position? p))
197 (test-assert (port-has-set-port-position!? p))
198
199 (test-eqv (string-ref data 0) (read-char p))
200 (test-eqv (string-ref data 1) (read-char p))
201 (test-eqv (string-ref data 2) (peek-char p))
202 (set! saved-pos (port-position p))
203 (test-eqv (string-ref data 2) (read-char p))
204 (test-eqv (string-ref data 3) (peek-char p))
205
206 (test-equal (string-copy data 3)
207 (read-string 997 p))
208 (test-equal (eof-object) (read-char p))
209
210 (set-port-position! p saved-pos)
211 (test-eqv (string-ref data 2) (peek-char p))
212
213 (test-assert (begin (close-port p)
214 closed))
215 )
216
217 (test-group
218 "Binary output, port positioning"
219 (define data (apply bytevector
220 (list-tabulate 1000 (lambda (i) (modulo i 256)))))
221 (define sink (make-vector 2000 #f))
222 (define pos 0)
223 (define saved-pos #f)
224 (define closed #f)
225 (define flushed #f)
226 (define p (make-custom-binary-output-port
227 "binary-output"
228 (lambda (buf start count) ;write!
229 (do ((i start (+ i 1))
230 (j pos (+ j 1)))
231 ((>= i (+ start count)) (set! pos j))
232 (vector-set! sink j (bytevector-u8-ref buf i)))
233 count)
234 (lambda () pos) ;get-position
235 (lambda (k) (set! pos k)) ;set-position!
236 (lambda () (set! closed #t)) ; close
237 (lambda () (set! flushed #t)) ; flush
238 ))
239 (test-assert "port?" (port? p))
240 (test-assert "not input-port?" (not (input-port? p)))
241 (test-assert "output port?" (output-port? p))
242 (test-assert "has position?" (port-has-port-position? p))
243 (test-assert "set position?" (port-has-set-port-position!? p))
244
245 (write-u8 3 p)
246 (write-u8 1 p)
247 (write-u8 4 p)
248 (flush-output-port p)
249 (test-assert "flush" flushed)
250 (set! saved-pos (port-position p))
251
252 (test-equal '#(3 1 4)
253 (vector-copy sink 0 pos))
254 (write-bytevector '#u8(1 5 9 2 6) p)
255 (flush-output-port p)
256 (test-equal '#(3 1 4 1 5 9 2 6)
257 (vector-copy sink 0 pos))
258
259 (set-port-position! p saved-pos)
260 (for-each (lambda (b) (write-u8 b p)) '(5 3 5))
261 (flush-output-port p)
262 (test-equal '#(3 1 4 5 3 5)
263 (vector-copy sink 0 pos))
264 (test-equal '#(3 1 4 5 3 5 2 6)
265 (vector-copy sink 0 (+ pos 2)))
266
267 (test-assert "close" (begin (close-port p)
268 closed))
269 )
270
271 (test-group
272 "Textual output, port positioning"
273 (define data (apply bytevector
274 (list-tabulate 1000 (lambda (i) (modulo i 256)))))
275 (define sink (make-vector 2000 #f))
276 (define pos 0)
277 (define saved-pos #f)
278 (define closed #f)
279 (define flushed #f)
280 (define p (make-custom-textual-output-port
281 "textual-output"
282 (lambda (buf start count) ;write!
283 (do ((i start (+ i 1))
284 (j pos (+ j 1)))
285 ((>= i (+ start count)) (set! pos j))
286 (vector-set! sink j
287 (if (string? buf)
288 (string-ref buf i)
289 (vector-ref buf i))))
290 count)
291 (lambda () pos) ;get-position
292 (lambda (k) (set! pos k)) ;set-position!
293 (lambda () (set! closed #t)) ; close
294 (lambda () (set! flushed #t)) ; flush
295 ))
296 (test-assert "port?" (port? p))
297 (test-assert "not input?" (not (input-port? p)))
298 (test-assert "output?" (output-port? p))
299 (test-assert "has position?" (port-has-port-position? p))
300 (test-assert "set position?" (port-has-set-port-position!? p))
301
302 (write-char #\a p)
303 (write-char #\b p)
304 (write-char #\c p)
305 (flush-output-port p)
306 (test-assert "flush" flushed)
307 (set! saved-pos (port-position p))
308
309 (test-equal '#(#\a #\b #\c)
310 (vector-copy sink 0 pos))
311 (write-string "Quack" p)
312 (flush-output-port p)
313 (test-equal '#(#\a #\b #\c #\Q #\u #\a #\c #\k)
314 (vector-copy sink 0 pos))
315
316 (set-port-position! p saved-pos)
317 (write-string "Cli" p)
318 (flush-output-port p)
319 (test-equal '#(#\a #\b #\c #\C #\l #\i)
320 (vector-copy sink 0 pos))
321 (test-equal '#(#\a #\b #\c #\C #\l #\i #\c #\k)
322 (vector-copy sink 0 (+ pos 2)))
323
324 (test-assert (begin (close-port p)
325 closed))
326 )
327
328 (test-group
329 "binary input/output"
330 (define data (apply bytevector
331 (list-tabulate 1000 (lambda (i) (modulo i 256)))))
332 (define original-size 500) ;writing may extend the size
333 (define pos 0)
334 (define saved-pos #f)
335 (define flushed #f)
336 (define closed #f)
337 (define p (make-custom-binary-input/output-port
338 "binary i/o"
339 (lambda (buf start count) ; read!
340 (let ((size (min count (- original-size pos))))
341 (bytevector-copy! buf start data pos (+ pos size))
342 (set! pos (+ pos size))
343 size))
344 (lambda (buf start count) ;write!
345 (let ((size (min count (- (bytevector-length data) pos))))
346 (bytevector-copy! data pos buf start (+ start size))
347 (set! pos (+ pos size))
348 (set! original-size (max original-size pos))
349 size))
350 (lambda () pos) ;get-position
351 (lambda (k) (set! pos k)) ;set-position!
352 (lambda () (set! closed #t)) ; close
353 (lambda () (set! flushed #t)) ; flush
354 ))
355 (test-assert "port?" (port? p))
356 (test-assert "input?" (input-port? p))
357 (test-assert "output?" (output-port? p))
358 (test-assert "has position?" (port-has-port-position? p))
359 (test-assert "set position?" (port-has-set-port-position!? p))
360
361 (test-eqv 0 (read-u8 p))
362 (test-eqv 1 (read-u8 p))
363 (test-eqv 2 (read-u8 p))
364 (set! saved-pos (port-position p))
365 (test-equal "rest of input"
366 (bytevector-copy data 3 original-size)
367 (read-bytevector 1000 p))
368 (write-bytevector '#u8(255 255 255) p)
369 (test-equal "appended"
370 '#u8(255 255 255)
371 (bytevector-copy data 500 503))
372 (write-u8 254 p)
373 (test-equal "appended more"
374 '#u8(255 255 255 254)
375 (bytevector-copy data 500 504))
376 (write-u8 254 p)
377 (test-eqv "still eof" (eof-object) (read-u8 p))
378
379 (set-port-position! p saved-pos)
380 (test-eqv "rewind & peek" 3 (peek-u8 p))
381 ;; TODO: We have to keep track of port position and peek. Needs modification
382 ;; in the core port code. For now, this is a known bug.
383 ;; (write-u8 100 p)
384 ;; (set-port-position! p saved-pos)
385 ;; (test-eqv "overwritten" 100 (read-u8 p))
386 ;; (test-eqv "overwritten" 4 (read-u8 p))
387 )
388
389 (test-group
390 "file-error"
391 (test-assert "file-error" (file-error? (make-file-error "bad"))))
392
393 (test-group
394 "i/o-invalid-position-error"
395 (test-assert "i/o-invalid-position-error"
396 (i/o-invalid-position-error?
397 (make-i/o-invalid-position-error 0))))
398
399 (test-group
400 "high-level i/o"
401 (define source
402 (let ((orig "((a b c . d)
403 (a . (b . (c . (d . ()))))
404 #(a b c d)
405 (#\\a #\\b #\\c #\\d #\\x07 #\\) #\\#)
406 \"abcd\"
407 #t #f #true #false
408 |a\\x07;bcd|
409 ;;comment
410 #u8(1 2 3 4 5)
411 #0=(a) #0#
412 1 1.111 3e5 #xff #b-0101 -5/3+3.9i +inf.0 +nan.0
413 )"))
414 (string-append orig "\n" orig "\n;end of input")))
415 (define data (read (open-input-string source)))
416 (define source-pos 0)
417 (define sink (open-output-string))
418 (define inp (make-custom-textual-input-port
419 "textual-input"
420 (lambda (buf start count) ;read!
421 (let ((size (min count (- (string-length source) source-pos))))
422 (unless (zero? size)
423 (if (string? buf)
424 (string-copy! buf start source
425 source-pos (+ source-pos count))
426 (do ((i 0 (+ i 1))
427 (j source-pos (+ j 1)))
428 ((= i size) (set! source-pos j))
429 (vector-set! buf (+ start i) (string-ref source j)))))
430 size))
431 #f #f #f))
432 (define outp (make-custom-textual-output-port
433 "textual-output"
434 (lambda (buf start count) ;write!
435 (do ((i start (+ i 1)))
436 ((>= i (+ start count)))
437 (write-char (if (string? buf)
438 (string-ref buf i)
439 (vector-ref buf i))
440 sink))
441 count)
442 #f #f #f))
443
444 (let ((expr1 (read inp)))
445 (test-equal "read first expr" data expr1)
446 (write expr1 outp))
447 (let ((expr2 (read inp)))
448 (test-equal "read second expr" data expr2)
449 (write expr2 outp))
450 (test-assert "finishing" (eof-object? (read inp)))
451 (let ((p (open-input-string (get-output-string sink))))
452 (test-equal "written expr 1" data (read p))
453 (test-equal "written expr 2" data (read p)))
454 )
455
456 ;;
457 ;; For Gauche - comprehensive transcoder tests are done in gauche.charconv.
458 ;; The reference implementation test assumes the native encoding is ascii,
459 ;; so we skip such tests.
460 ;;
461
462 (test-group
463 "transcoded input"
464 ;; This test assumes native transcoder supports ascii range
465 (test-equal "native" "ABCD"
466 (bytevector->string '#u8(#x41 #x42 #x43 #x44)
467 (native-transcoder)))
468 '(test-equal "latin1 -> ascii" "ABC???XYZ???"
469 (bytevector->string '#u8(#x41 #x42 #x43 #xa1 #xa2 #xa3
470 #x58 #x59 #x5a #xc1 #xc2 #xc3)
471 (make-transcoder (latin-1-codec)
472 (native-eol-style)
473 'replace)))
474 '(test-assert "latin1 raise"
475 (guard (e ((i/o-decoding-error? e)))
476 (bytevector->string '#u8(#xc1)
477 (make-transcoder (latin-1-codec)
478 (native-eol-style)
479 'raise))
480 #f))
481 '(test-equal "utf-16 (bom, be) -> ascii" "AB??CD"
482 (bytevector->string '#u8(#xfe #xff #x00 #x41 #x00 #x42
483 #x30 #x00 #x00 #xc1 #x00 #x43 #x00 #x44)
484 (make-transcoder (utf-16-codec)
485 (native-eol-style)
486 'replace)))
487 '(test-equal "utf-16 (bom, le) -> ascii" "AB??CD"
488 (bytevector->string '#u8(#xff #xfe #x41 #x00 #x42 #x00
489 #x00 #x30 #xc1 #x00 #x43 #x00 #x44 #x00)
490 (make-transcoder (utf-16-codec)
491 (native-eol-style)
492 'replace)))
493 '(test-equal "utf-16 (native) -> ascii" "AB??CD"
494 (bytevector->string
495 (if (equal? (bytevector->string '#u8(#x00 #x41)
496 (make-transcoder (utf-16-codec)
497 (native-eol-style)
498 'replace))
499 "A")
500 '#u8(#x00 #x41 #x00 #x42 #x30 #x00 #x00 #xc1 #x00 #x43 #x00 #x44)
501 '#u8(#x41 #x00 #x42 #x00 #x00 #x30 #xc1 #x00 #x43 #x00 #x44 #x00))
502 (make-transcoder (utf-16-codec)
503 (native-eol-style)
504 'replace)))
505
506 (test-equal "eol-style none, lf, crlf" '("A\nB\rC\r\nD"
507 "A\nB\nC\nD"
508 "A\nB\nC\nD")
509 (map
510 (lambda (style)
511 (bytevector->string #u8(#x41 #x0a #x42 #x0d #x43 #x0d #x0a #x44)
512 (make-transcoder (latin-1-codec)
513 style
514 'raise)))
515 '(none lf crlf)))
516 )
517
518 (test-group
519 "transcoded output"
520 ;; This test assumes native transcoder supports ascii range
521 (test-equal "native" '#u8(#x41 #x42 #x43 #x44)
522 (string->bytevector "ABCD"
523 (native-transcoder)))
524 (test-equal "ascii -> latin1" #u8(#x41 #x42 #x43 #x44)
525 (string->bytevector "ABCD"
526 (make-transcoder (latin-1-codec)
527 (native-eol-style)
528 'raise)))
529 (test-equal "ascii -> utf-16" #f
530 (not
531 (member
532 (string->bytevector "ABCD"
533 (make-transcoder (utf-16-codec)
534 (native-eol-style)
535 'raise))
536 '(#u8(#x00 #x41 #x00 #x42 #x00 #x43 #x00 #x44)
537 #u8(#x41 #x00 #x42 #x00 #x43 #x00 #x44 #x00)
538 #u8(#xfe #xff #x00 #x41 #x00 #x42 #x00 #x43 #x00 #x44)
539 #u8(#xff #xfe #x41 #x00 #x42 #x00 #x43 #x00 #x44 #x00)))))
540
541 (test-equal "eol-style none, lf, crlf"
542 '(#u8(#x41 #x0a #x42 #x0d #x43 #x0d #x0a #x44 #x0d #x0d #x0a)
543 #u8(#x41 #x0a #x42 #x0a #x43 #x0a #x44 #x0a #x0a)
544 #u8(#x41 #x0d #x0a #x42 #x0d #x0a #x43 #x0d #x0a #x44 #x0d #x0a #x0d #x0a))
545 (map
546 (lambda (style)
547 (string->bytevector "A\nB\rC\r\nD\r\r\n"
548 (make-transcoder (latin-1-codec)
549 style
550 'raise)))
551 '(none lf crlf)))
552 )
553
554 (test-end "srfi-181-192-test")