Debian Codegraph @

test/io.scm

raw
1 ;;
2 ;; test for port I/O functions
3 ;;
4
5 (use gauche.test)
6
7 (test-start "io")
8
9 ;;-------------------------------------------------------------------
10 (test-section "file i/o")
11
12 (sys-system "rm -rf tmp2.o")
13
14 (test* "open-input-file" (test-error <system-error>)
15 (open-input-file "tmp2.o"))
16
17 (test* "open-input-file :if-does-not-exist #f" #f
18 (open-input-file "tmp2.o" :if-does-not-exist #f))
19
20 (test* "open-output-file :if-does-not-exist :error" (test-error)
21 (open-output-file "tmp2.o" :if-does-not-exist :error))
22
23 (test* "open-output-file :if-does-not-exit #f" #f
24 (open-output-file "tmp2.o" :if-does-not-exist #f))
25
26 (test* "open-output-file" #t
27 (let* ([p (open-output-file "tmp2.o")]
28 [r (output-port? p)])
29 (display "abcde" p)
30 (close-output-port p)
31 r))
32
33 (test* "open-input-file" 'abcde
34 (let* ([p (open-input-file "tmp2.o")]
35 [s (read p)])
36 (close-input-port p)
37 s))
38
39 (test* "open-output-file :if-exists :error" (test-error)
40 (open-output-file "tmp2.o" :if-exists :error))
41
42 (test* "open-output-file :if-exists :supersede" 'cdefg
43 (let1 o (open-output-file "tmp2.o")
44 (display "cdefg" o)
45 (close-output-port o)
46 (let* ([i (open-input-file "tmp2.o")]
47 [s (read i)])
48 (close-input-port i)
49 s)))
50
51 (test* "open-output-file :if-exists :append" 'cdefghij
52 (let1 o (open-output-file "tmp2.o" :if-exists :append)
53 (display "hij" o)
54 (close-output-port o)
55 (let* ([i (open-input-file "tmp2.o")]
56 [s (read i)])
57 (close-input-port i)
58 s)))
59
60 (test* "open-output-file :if-exists :append" 'cdefghijklm
61 (let1 o (open-output-file "tmp2.o"
62 :if-exists :append
63 :if-does-not-exist :error)
64 (display "klm" o)
65 (close-output-port o)
66 (let* ([i (open-input-file "tmp2.o")]
67 [s (read i)])
68 (close-input-port i)
69 s)))
70
71 (test* "open-output-file :if-exists :supersede" 'nopqr
72 (let1 o (open-output-file "tmp2.o"
73 :if-exists :supersede
74 :if-does-not-exist #f)
75 (display "nopqr" o)
76 (close-output-port o)
77 (let* ([i (open-input-file "tmp2.o")]
78 [s (read i)])
79 (close-input-port i)
80 s)))
81
82 (sys-system "rm -f tmp2.o")
83
84 (test* "call-with-input-file :if-does-not-exist #f" '(#f #f)
85 (call-with-input-file "tmp2.o" (^p (list p p))
86 :if-does-not-exist #f))
87
88 (test* "with-input-from-file :if-does-not-exist #f" #f
89 (with-input-from-file "tmp2.o" (^() 5)
90 :if-does-not-exist #f))
91
92 (call-with-output-file "tmp2.o" (^p (display "stu" p)))
93
94 (test* "call-with-output-file :if-exists #f" 'stu
95 (begin
96 (call-with-output-file "tmp2.o" (^p (and p (display "vwx" p)))
97 :if-exists #f)
98 (call-with-input-file "tmp2.o" read)))
99
100 (test* "with-output-to-file :if-exists #f" 'stu
101 (or (with-output-to-file "tmp2.o"
102 (^() (display "yz" p) 4)
103 :if-exists #f)
104 (call-with-input-file "tmp2.o" read)))
105
106 ;;-------------------------------------------------------------------
107 (test-section "port-attributes")
108
109 (let ([p (open-output-file "tmp.o" :if-exists :supersede)])
110 (test* "port-attribute-ref (nonexistent)" (test-error)
111 (port-attribute-ref p 'my-attribute))
112 (test* "port-attribute-ref (fallback)" 'none
113 (port-attribute-ref p 'my-attribute 'none))
114 (test* "port-attribute-set! (autocreate)" 'ok
115 (begin (port-attribute-set! p 'my-attribute 'ok)
116 (port-attribute-ref p 'my-attribute)))
117 (test* "port-attributes" '((my-attribute . ok)
118 (my-attribute2 . 103)
119 (my-attribute3 . 12)
120 (name . "tmp.o"))
121 (begin
122 (port-attribute-set! p 'my-attribute2 103)
123 (port-attribute-set! p 'my-attribute3 12)
124 (alist-delete 'reader-lexical-mode
125 (sort (port-attributes p)
126 string<?
127 ($ symbol->string $ car $)))))
128 (test* "port-attribute-delete!" '((my-attribute3 . 12) (name . "tmp.o"))
129 (begin
130 (port-attribute-delete! p 'my-attribute)
131 (port-attribute-delete! p 'my-attribute2)
132 (alist-delete 'reader-lexical-mode (port-attributes p))))
133
134 ;; make sure 'name' attribute is read-only and undeletable
135 (test* "name attribute is read-only" (test-error <error> #/read-only/)
136 (port-attribute-set! p 'name "foo"))
137 (test* "name attribute is undeletable" (test-error <error> #/not deletable/)
138 (port-attribute-delete! p 'name))
139 )
140
141 ;;-------------------------------------------------------------------
142 (test-section "port-fd-dup!")
143
144 (cond-expand
145 (gauche.os.windows #f)
146 (else
147 (test* "port-fd-dup!" '("foo" "bar")
148 (let* ([p1 (open-output-file "tmp1.o")]
149 [p2 (open-output-file "tmp2.o")])
150 (display "foo\n" p1)
151 (port-fd-dup! p1 p2)
152 (display "bar\n" p1)
153 (close-output-port p1)
154 (close-output-port p2)
155 (list (call-with-input-file "tmp1.o" read-line)
156 (call-with-input-file "tmp2.o" read-line))))
157
158 (test* "port-fd-dup!" '("foo" "bar")
159 (let* ([p1 (open-input-file "tmp1.o")]
160 [p2 (open-input-file "tmp2.o")]
161 [s1 (read-line p1)])
162 (port-fd-dup! p1 p2)
163 (list s1 (read-line p1))))
164
165 (test* "port-fd-dup!" (test-error)
166 (let* ([p1 (open-output-file "tmp1.o")]
167 [p2 (open-input-file "tmp2.o")])
168 (guard (e (else
169 (close-output-port p1)
170 (close-input-port p2)
171 (raise e)))
172 (port-fd-dup! p1 p2))))
173
174 (test* "port-fd-dup!" (test-error)
175 (let1 p1 (open-input-file "tmp2.o")
176 (guard (e (else
177 (close-input-port p1)
178 (raise e)))
179 (port-fd-dup! (open-input-string "") p1))))
180 )) ; !gauche.os.windows
181
182 ;;-------------------------------------------------------------------
183 (test-section "input ports")
184
185 (sys-unlink "tmp1.o")
186 (with-output-to-file "tmp1.o" (cut display ""))
187 (test* "read-char (EOF)" #t
188 (eof-object? (call-with-input-file "tmp1.o" read-char)))
189 (test* "read-byte (EOF)" #t
190 (eof-object? (call-with-input-file "tmp1.o" read-byte)))
191 (test* "read-line (EOF)" #t
192 (eof-object? (call-with-input-file "tmp1.o" read-line)))
193 (test* "read-block (EOF)" #t
194 (eof-object? (call-with-input-file "tmp1.o"
195 (cut read-block 10 <>))))
196
197 (with-output-to-file "tmp1.o" (cut display "ab"))
198 (test* "read-char (a)" #\a
199 (call-with-input-file "tmp1.o" read-char))
200 (test* "read-byte (a)" 97
201 (call-with-input-file "tmp1.o" read-byte))
202 (test* "read-byte (ungotten)" 97
203 (call-with-input-file "tmp1.o"
204 (^p (peek-char p) (read-byte p))))
205 (test* "read-line (a)" "ab"
206 (call-with-input-file "tmp1.o" read-line))
207 (test* "read-byte (ungotten)" 97
208 (call-with-input-file "tmp1.o"
209 (^p (peek-char p) (read-byte p))))
210 (test* "peek-byte (a)" '(97 97)
211 (call-with-input-file "tmp1.o"
212 (^p (let1 a (peek-byte p) (list a (read-byte p))))))
213 (test* "peek-byte (ungotten)" '(97 97)
214 (call-with-input-file "tmp1.o"
215 (^p
216 (peek-char p) (let1 a (peek-byte p) (list a (read-byte p))))))
217 (test* "peek-byte and read-char" #\a
218 (call-with-input-file "tmp1.o"
219 (^p (peek-byte p) (read-char p))))
220 (test* "peek-byte and peek-char" #\a
221 (call-with-input-file "tmp1.o"
222 (^p (peek-byte p) (peek-char p))))
223 (test* "read-block (a)" #*"ab"
224 (call-with-input-file "tmp1.o" (cut read-block 10 <>)))
225 (test* "read-block (ungotten)" #*"ab"
226 (call-with-input-file "tmp1.o"
227 (^p (peek-char p) (read-block 10 p))))
228
229 (with-output-to-file "tmp1.o" (cut display "\n"))
230 (test* "read-line (LF)" ""
231 (call-with-input-file "tmp1.o" read-line))
232 (test* "read-line (LF, ungotten)" ""
233 (call-with-input-file "tmp1.o"
234 (^p (peek-char p) (read-line p))))
235 (with-output-to-file "tmp1.o" (cut display "\r"))
236 (test* "read-line (CR)" ""
237 (call-with-input-file "tmp1.o" read-line))
238 (test* "read-line (CR, ungotten)" ""
239 (call-with-input-file "tmp1.o"
240 (^p (peek-char p) (read-line p))))
241 (with-output-to-file "tmp1.o" (cut display "\n\n"))
242 (test* "read-line (LF)" '("" "" #t)
243 (call-with-input-file "tmp1.o"
244 (^_ (let* ([c1 (peek-char _)]
245 [l1 (read-line _)]
246 [c2 (peek-char _)]
247 [l2 (read-line _)]
248 [c2 (peek-char _)]
249 [l3 (read-line _)])
250 (list l1 l2 (eof-object? l3))))))
251 (with-output-to-file "tmp1.o" (cut display "\r\r\n"))
252 (test* "read-line (CR, CRLF)" '("" "" #t)
253 (call-with-input-file "tmp1.o"
254 (^_ (let* ([c1 (peek-char _)]
255 [l1 (read-line _)]
256 [c2 (peek-char _)]
257 [l2 (read-line _)]
258 [c2 (peek-char _)]
259 [l3 (read-line _)])
260 (list l1 l2 (eof-object? l3))))))
261 (with-output-to-file "tmp1.o" (cut display "a\r\nb\nc"))
262 (test* "read-line (mix)" '("a" "b" "c" #t)
263 (call-with-input-file "tmp1.o"
264 (^_ (let* ([c1 (peek-char _)]
265 [l1 (read-line _)]
266 [c2 (peek-char _)]
267 [l2 (read-line _)]
268 [c2 (peek-char _)]
269 [l3 (read-line _)]
270 [c3 (peek-char _)])
271 (list l1 l2 l3 (eof-object? c3))))))
272
273 (with-output-to-file "tmp1.o"
274 (cut for-each write-byte '(#x80 #xff #x80 #xff #x80 #x0d #x0a #x0d #x0a)))
275 (test* "read-line (bad sequence)" '(5 0)
276 (call-with-input-file "tmp1.o"
277 (^_ (let* ([s1 (read-line _ #t)]
278 [s2 (read-line _ #t)]
279 [s3 (read-line _ #t)])
280 (and (eof-object? s3)
281 (list (string-size s1) (string-size s2)))))))
282
283 (with-output-to-file "tmp1.o"
284 (cut display "a b c \"d e\" f g\n(0 1 2\n3 4 5)\n"))
285
286 (test* "port->string" "a b c \"d e\" f g\n(0 1 2\n3 4 5)\n"
287 (call-with-input-file "tmp1.o" port->string))
288 (test* "port->list" '(a b c "d e" f g (0 1 2 3 4 5))
289 (call-with-input-file "tmp1.o" (cut port->list read <>)))
290 (test* "port->list" '("a b c \"d e\" f g" "(0 1 2" "3 4 5)")
291 (call-with-input-file "tmp1.o" (cut port->list read-line <>)))
292 (test* "port->string-list" '("a b c \"d e\" f g" "(0 1 2" "3 4 5)")
293 (call-with-input-file "tmp1.o" port->string-list))
294 (test* "port->sexp-list" '(a b c "d e" f g (0 1 2 3 4 5))
295 (call-with-input-file "tmp1.o" port->sexp-list))
296
297 (test* "port-fold" '((0 1 2 3 4 5) g f "d e" c b a)
298 (with-input-from-file "tmp1.o"
299 (cut port-fold cons '() read)))
300 (test* "port-fold" '("3 4 5)" "(0 1 2" "a b c \"d e\" f g")
301 (with-input-from-file "tmp1.o"
302 (cut port-fold cons '() read-line)))
303 (test* "port-fold, side-effecting" '(#\c 3 #\b 2 #\a 1)
304 (with-input-from-string "abc"
305 (cut port-fold (^[x s] `(,x ,(port-tell (current-input-port)) ,@s))
306 '() read-char)))
307 (test* "port-fold-right" '(a b c "d e" f g (0 1 2 3 4 5))
308 (with-input-from-file "tmp1.o"
309 (cut port-fold-right cons '() read)))
310
311 (test* "port-map" '(a b c "d e" f g (0 1 2 3 4 5))
312 (with-input-from-file "tmp1.o"
313 (cut port-map identity read)))
314
315 (test* "port-map, side-effecting" '((#\a 1) (#\b 2) (#\c 3))
316 (with-input-from-string "abc"
317 (cut port-map (^x `(,x ,(port-tell (current-input-port)))) read-char)))
318
319 ;;-------------------------------------------------------------------
320 (test-section "with-ports")
321
322 (test* "with-input-from-port" '(#\b #\d #\c #\a)
323 (let ([x (open-input-string "ab")]
324 [y (open-input-string "cd")]
325 [r '()]
326 [restart #f])
327 (if (call/cc
328 (^[escape]
329 (with-input-from-port x
330 (^()
331 (push! r (read-char))
332 (with-input-from-port y
333 (^()
334 (push! r (read-char))
335 (call/cc (^k (set! restart k) (escape #t)))
336 (push! r (read-char))))
337 (push! r (read-char))))
338 #f))
339 (restart #f)
340 r)))
341
342 (for-each
343 (lambda (with cur name)
344 (test* "with-,|name|-to-port" '("ad" "bc")
345 (let ([x (open-output-string)]
346 [y (open-output-string)]
347 [restart #f])
348 (if (call/cc
349 (^[escape]
350 (with x
351 (^()
352 (write-char #\a (cur))
353 (with y
354 (^()
355 (write-char #\b (cur))
356 (call/cc (^k (set! restart k) (escape #t)))
357 (write-char #\c (cur))))
358 (write-char #\d (cur))))
359 #f))
360 (restart #f)
361 (list (get-output-string x) (get-output-string y))))))
362 `(,with-output-to-port ,with-error-to-port)
363 `(,current-output-port ,current-error-port)
364 '("output" "error"))
365
366 (test* "with-ports 1" '("a" "b")
367 (let ([o0 (open-output-string)]
368 [o1 (open-output-string)])
369 (with-ports (open-input-string "abcd") o0 o1
370 (^()
371 (write-char (read-char))
372 (write-char (read-char) (current-error-port))))
373 (list (get-output-string o0) (get-output-string o1))))
374 (test* "with-ports 2" '("B" "A")
375 (let ([o0 (open-output-string)]
376 [o1 (open-output-string)])
377 (with-ports (open-input-string "abcd") o0 o0
378 (^()
379 (with-ports (open-input-string "ABCD") o1 #f
380 (^()
381 (write-char (read-char))
382 (write-char (read-char) (current-error-port))))))
383 (list (get-output-string o0) (get-output-string o1))))
384 (test* "with-ports 3" '("A" "B")
385 (let ([o0 (open-output-string)]
386 [o1 (open-output-string)])
387 (with-ports (open-input-string "abcd") o0 o0
388 (^()
389 (with-ports (open-input-string "ABCD") #f o1
390 (^()
391 (write-char (read-char))
392 (write-char (read-char) (current-error-port))))))
393 (list (get-output-string o0) (get-output-string o1))))
394 (test* "with-ports 4" '("" "ab")
395 (let ([o0 (open-output-string)]
396 [o1 (open-output-string)])
397 (with-ports (open-input-string "abcd") o0 o0
398 (^()
399 (with-ports #f o1 o1
400 (^()
401 (write-char (read-char))
402 (write-char (read-char) (current-error-port))))))
403 (list (get-output-string o0) (get-output-string o1))))
404
405 ;;-------------------------------------------------------------------
406 (test-section "seeking")
407
408 (define (seek-tester1 p)
409 (display (read-block 5 p))
410 (let1 p0 (port-tell p)
411 (port-seek p -3 SEEK_CUR)
412 (display (read-block 5 p))
413 (port-seek p p0)
414 (display (read p))
415 (port-seek p 0 SEEK_SET)
416 (display (read-block 3 p))
417 (port-seek p -3 SEEK_END)
418 (display (read p))))
419
420 (test* "seek (istr)" "abcdecdefgfghijabchij"
421 (with-output-to-string
422 (cut call-with-input-string "abcdefghij" seek-tester1)))
423 (test* "seek (istr, boundary)" #\a
424 (call-with-input-string "abcdefghij"
425 (^p
426 (read-char p)
427 (port-seek p -1 SEEK_CUR)
428 (read-char p))))
429 (test* "seek (istr, boundary)" #t
430 (call-with-input-string "abcdefghij"
431 (^p
432 (read-char p)
433 (port-seek p 10)
434 (eof-object? (read-char p)))))
435 (test* "seek (istr, out of range)" #f
436 (call-with-input-string "abcdefghij"
437 (^p
438 (read-char p)
439 (port-seek p 10 SEEK_CUR))))
440 (test* "seek (istr, out of range)" #f
441 (call-with-input-string "abcdefghij"
442 (^p
443 (read-char p)
444 (port-seek p -2))))
445 ;; ungetc and seek interaction; pointed out by Alex Shinn
446 (test* "seek (istr, with peek-char)" '("hello" "hello")
447 (letrec ([read-zstring
448 (^p (let loop ([ls '()])
449 (let1 c (peek-char p)
450 (if (or (eof-object? c) (eqv? c #\null))
451 (list->string (reverse ls))
452 (begin (read-char p) (loop (cons c ls)))))))])
453 (call-with-input-string "hello\0world"
454 (^p (let* ([first (read-zstring p)]
455 [dummy (port-seek p 0)]
456 [second (read-zstring p)])
457 (list first second))))))
458 (test* "seek (istr, with peek-char)" '(#\b #\b)
459 (with-input-from-string "abc"
460 (^()
461 (read-char)
462 (let1 c1 (peek-char)
463 (port-seek (current-input-port) 0 SEEK_CUR)
464 (list c1 (peek-char))))))
465 (test* "seek (istr, with peek-char)" '(#\a 0 #\b)
466 (with-input-from-string "ab"
467 (^()
468 (let* ((c0 (peek-char))
469 (pos (port-tell (current-input-port)))
470 (c1 (begin
471 (port-seek (current-input-port) 1 SEEK_CUR)
472 (read-char))))
473 (list c0 pos c1)))))
474 (test* "seek (istr, with peek-char, negative offset)" '(#\c 2 #\b)
475 (with-input-from-string "abc"
476 (^()
477 (read-char)
478 (read-char)
479 (let* ((c0 (peek-char))
480 (pos (port-tell (current-input-port)))
481 (c1 (begin
482 (port-seek (current-input-port) -1 SEEK_CUR)
483 (read-char))))
484 (list c0 pos c1)))))
485 (test* "seek (istr, with peek-byte)" '(#x61 #x62)
486 (with-input-from-string (rlet1 s (make-byte-string 2 #x61)
487 (string-byte-set! s 1 #x62))
488 (^()
489 (let1 b1 (peek-byte)
490 (port-seek (current-input-port) 1)
491 (list b1 (peek-byte))))))
492 (test* "seek (istr, with peek-byte)" '(#x61 0 #x62)
493 (with-input-from-string (rlet1 s (make-byte-string 2 #x61)
494 (string-byte-set! s 1 #x62))
495 (^()
496 (let* ([b1 (peek-byte)]
497 [pos (port-tell (current-input-port))])
498 (port-seek (current-input-port) 1 SEEK_CUR)
499 (list b1 pos (peek-byte))))))
500
501 ;; NB: in the following four test, each ifile-ofile test is a pair
502 ;; (the ofile test depends on the previous state by ifile). do not
503 ;; separate them.
504 (test* "seek (ifile)" "abcdecdefgfghijabchij"
505 (begin
506 (sys-unlink "test.o")
507 (with-output-to-file "test.o" (cut display "abcdefghij"))
508 (with-output-to-string
509 (cut call-with-input-file "test.o" seek-tester1))))
510
511 (test* "seek (ofile)" "--//efg**j++"
512 (begin
513 (call-with-output-file "test.o"
514 (^p
515 (port-seek p 0)
516 (display "--" p)
517 (let1 p0 (port-tell p)
518 (port-seek p 0 SEEK_END)
519 (display "++" p)
520 (port-seek p -5 SEEK_CUR)
521 (display "**" p)
522 (port-seek p p0)
523 (display "//" p)))
524 :if-exists :overwrite)
525 (call-with-input-file "test.o" port->string)))
526
527 (test* "seek (ifile, large)"
528 "0000050055019999050100027500"
529 (begin
530 (sys-unlink "test.o")
531 (with-output-to-file "test.o"
532 (^() (dotimes (n 10000) (format #t "~4,'0d" n))))
533 (with-output-to-string
534 (^()
535 (call-with-input-file "test.o"
536 (^p
537 (display (read-block 4 p))
538 (port-seek p 2000)
539 (display (read-block 4 p))
540 (let1 p0 (port-tell p)
541 (port-seek p 20000 SEEK_CUR)
542 (display (read-block 4 p))
543 (port-seek p -4 SEEK_END)
544 (display (read-block 4 p))
545 (port-seek p p0)
546 (display (read-block 4 p))
547 (port-seek p -2000 SEEK_CUR)
548 (display (read-block 4 p))
549 (port-seek p -10000 SEEK_END)
550 (display (read-block 4 p))
551 )))))))
552
553 (test* "seek (ofile, large)"
554 "*0-0*/-0999+"
555 (begin
556 (call-with-output-file "test.o"
557 (^p
558 (display "*" p)
559 (port-seek p 20000)
560 (display "*" p)
561 (let1 p0 (port-tell p)
562 (port-seek p -19999 SEEK_CUR)
563 (display "-" p)
564 (port-seek p -19998 SEEK_END)
565 (display "-" p)
566 (port-seek p 19996 SEEK_CUR)
567 (display "+" p)
568 (port-seek p p0)
569 (display "/" p)))
570 :if-exists :overwrite)
571 (with-output-to-string
572 (^()
573 (call-with-input-file "test.o"
574 (^p
575 (display (read-block 4 p))
576 (port-seek p 20000)
577 (display (read-block 4 p))
578 (port-seek p 39996)
579 (display (read-block 4 p))))))
580 ))
581
582 (test* "seek (ifile, with peek-char)" '("hello" "hello")
583 (letrec ([read-zstring
584 (^p
585 (let loop ((ls '()))
586 (let1 c (peek-char p)
587 (if (or (eof-object? c) (eqv? c #\null))
588 (list->string (reverse ls))
589 (begin (read-char p) (loop (cons c ls)))))))])
590 (begin
591 (sys-unlink "test.o")
592 (with-output-to-file "test.o"
593 (cut display "hello\0world"))
594 (call-with-input-file "test.o"
595 (^p
596 (let* ([first (read-zstring p)]
597 [dummy (port-seek p 0)]
598 [second (read-zstring p)])
599 (list first second)))))))
600
601 (test* "seek (ifile, with peek-byte)" '(#x61 #x62)
602 (begin
603 (sys-unlink "test.o")
604 (with-output-to-file "test.o"
605 (cut display (rlet1 s (make-byte-string 2 #x61)
606 (string-byte-set! s 1 #x62))))
607 (call-with-input-file "test.o"
608 (^p
609 (let1 b1 (peek-byte p)
610 (port-seek p 1)
611 (list b1 (peek-byte p)))))))
612
613 (test* "seek (with appending output)" '(50 100)
614 (begin
615 (sys-unlink "test.o")
616 (with-output-to-file "test.o"
617 (cut display (make-string 50 #\a)))
618 (call-with-output-file "test.o"
619 (^p (let1 a (port-tell p)
620 (display (make-string 50 #\b) p)
621 (list a (port-tell p))))
622 :if-exists :append)))
623
624 (sys-unlink "test.o")
625
626 ;;-------------------------------------------------------------------
627 (test-section "format")
628
629 (test* "format ~s" "\"abc\"" (format "~s" "abc"))
630 (test* "format ~s" "\"abc\" " (format "~10s" "abc"))
631 (test* "format ~s" " \"abc\"" (format "~10@s" "abc"))
632 (test* "format ~s" "\"abc\" " (format "~10,3s" "abc"))
633 (test* "format ~s" " \"abc\"" (format "~10,3@s" "abc"))
634 (test* "format ~s" "\"abc\" " (format "~,,1s" "abc"))
635 (test* "format ~s" " \"abc\"" (format "~,,1@s" "abc"))
636 (test* "format ~s" "\"abc\"*****" (format "~10,,,'*s" "abc"))
637 (test* "format ~s" "*****\"abc\"" (format "~10,,,'*@s" "abc"))
638
639 (test* "format ~s" "\"abc\"*****" (format "~10,,,'*,15s" "abc"))
640 (test* "format ~s" "*****\"abc\"" (format "~10,,,'*,15@s" "abc"))
641 (test* "format ~s" "(\"abc\" \"def\" \"g"
642 (format "~10,,,'*,15s" '("abc" "def" "ghi" "jkl")))
643 (test* "format ~s" "(\"abc\" \"def\" \"g"
644 (format "~10,,,'*,15@s" '("abc" "def" "ghi" "jkl")))
645 (test* "format ~s" "(\"abc\" \"def\"..."
646 (format "~10,,,'*,15:s" '("abc" "def" "ghi" "jkl")))
647 (test* "format ~s" "(\"abc\" \"def\"..."
648 (format "~10,,,'*,15@:s" '("abc" "def" "ghi" "jkl")))
649 (test* "format ~s" "(\"ab\\\"c\" #\\\" \"de\"..."
650 (format "~10,,,'*,20@:s" '("ab\"c" #\" "defghi")))
651 (test* "format ~s" "(\"ab\\\"c\" #\\\" \"d\\\\\".."
652 (format "~10,,,'*,20@:s" '("ab\"c" #\" "d\nfghi")))
653
654 (test* "format ~a" "abc" (format "~a" "abc"))
655 (test* "format ~a" "abc " (format "~10a" "abc"))
656 (test* "format ~a" " abc" (format "~10@a" "abc"))
657 (test* "format ~a" "abc " (format "~10,3a" "abc"))
658 (test* "format ~a" " abc" (format "~10,3@a" "abc"))
659 (test* "format ~a" "abc " (format "~,,1a" "abc"))
660 (test* "format ~a" " abc" (format "~,,1@a" "abc"))
661 (test* "format ~a" "abc*******" (format "~10,,,'*a" "abc"))
662 (test* "format ~a" "*******abc" (format "~10,,,'*@a" "abc"))
663
664 (test* "format ~a" "(abc def ghi j"
665 (format "~10,,,'*,14a" '("abc" "def" "ghi" "jkl")))
666 (test* "format ~a" "(abc def ghi j"
667 (format "~10,,,'*,14@a" '("abc" "def" "ghi" "jkl")))
668 (test* "format ~a" "(abc def g ..."
669 (format "~10,,,'*,14:a" '("abc" "def" "ghi" "jkl")))
670 (test* "format ~a" "(abc def g ..."
671 (format "~10,,,'*,14@:a" '("abc" "def" "ghi" "jkl")))
672
673 (test* "format ~w" "(#0=(1) #0#)"
674 (format "~w" (let1 x '(1) (list x x))))
675
676 (test* "format ~c" "|a|" (format "|~c|" #\a))
677 (test* "format ~c" "|#\\a|" (format "|~@c|" #\a))
678 (test* "format ~c" (test-error) (format "|~c|" 1))
679
680 (test* "format ~d" "12345" (format "~d" 12345))
681 (test* "format ~d" "-12345" (format "~d" -12345))
682 (test* "format ~d" "+12345" (format "~@d" 12345))
683 (test* "format ~d" "-12345" (format "~@d" -12345))
684 (test* "format ~d" " 12345" (format "~10d" 12345))
685 (test* "format ~d" " -12345" (format "~10d" -12345))
686 (test* "format ~d" " +12345" (format "~10@d" 12345))
687 (test* "format ~d" " -12345" (format "~10@d" -12345))
688 (test* "format ~d" "0000012345" (format "~10,'0d" 12345))
689 (test* "format ~d" "0000-12345" (format "~10,'0d" -12345))
690
691 (test* "format ~:d" "1" (format "~:d" 1))
692 (test* "format ~:d" "-1" (format "~:d" -1))
693 (test* "format ~:d" "12" (format "~:d" 12))
694 (test* "format ~:d" "-12" (format "~:d" -12))
695 (test* "format ~:d" "123" (format "~:d" 123))
696 (test* "format ~:d" "-123" (format "~:d" -123))
697 (test* "format ~:d" "+123" (format "~:@d" 123))
698 (test* "format ~:d" "1,234" (format "~:d" 1234))
699 (test* "format ~:d" "-1,234" (format "~:d" -1234))
700 (test* "format ~:d" "+1,234" (format "~:@d" 1234))
701 (test* "format ~:d" "12,345" (format "~:d" 12345))
702 (test* "format ~:d" "-12,345" (format "~:d" -12345))
703 (test* "format ~:d" "123,456,789" (format "~:d" 123456789))
704 (test* "format ~:d" "-123,456,789" (format "~:d" -123456789))
705 (test* "format ~:d" "123.456.789" (format "~,,'.:d" 123456789))
706 (test* "format ~:d" "-123.456.789" (format "~,,'.:d" -123456789))
707 (test* "format ~:d" "1.2345.6789" (format "~,,'.,4:d" 123456789))
708 (test* "format ~:d" "-1.2345.6789" (format "~,,'.,4:d" -123456789))
709 (test* "format ~:d" " 12,345" (format "~10:d" 12345))
710 (test* "format ~:d" " -12,345" (format "~10:d" -12345))
711 (test* "format ~:d" " +12,345" (format "~10:@d" 12345))
712
713 (test* "format ~b" "10101" (format "~b" 21))
714 (test* "format ~b" "-10101" (format "~b" -21))
715 (test* "format ~b" "+10101" (format "~@b" 21))
716 (test* "format ~b" "-10101" (format "~@b" -21))
717 (test* "format ~b" " 10101" (format "~10b" 21))
718 (test* "format ~b" " -10101" (format "~10b" -21))
719 (test* "format ~b" " +10101" (format "~10@b" 21))
720 (test* "format ~b" " -10101" (format "~10@b" -21))
721 (test* "format ~b" "0000010101" (format "~10,'0b" 21))
722 (test* "format ~b" "0000-10101" (format "~10,'0b" -21))
723
724 (test* "format ~b" "101" (format "~,,' ,4:b" 5))
725 (test* "format ~b" "101 0101" (format "~,,' ,4:b" 85))
726
727 (test* "format ~o" "12345" (format "~o" 5349))
728 (test* "format ~o" "-12345" (format "~o" -5349))
729 (test* "format ~o" "+12345" (format "~@o" 5349))
730 (test* "format ~o" "-12345" (format "~@o" -5349))
731 (test* "format ~o" " 12345" (format "~10o" 5349))
732 (test* "format ~o" " -12345" (format "~10o" -5349))
733 (test* "format ~o" " +12345" (format "~10@o" 5349))
734 (test* "format ~o" " -12345" (format "~10@o" -5349))
735 (test* "format ~o" "0000012345" (format "~10,'0o" 5349))
736 (test* "format ~o" "0000-12345" (format "~10,'0o" -5349))
737
738 (test* "format ~x" "12345" (format "~x" 74565))
739 (test* "format ~x" "-12345" (format "~x" -74565))
740 (test* "format ~x" "+12345" (format "~@x" 74565))
741 (test* "format ~x" "-12345" (format "~@x" -74565))
742 (test* "format ~x" " 12345" (format "~10x" 74565))
743 (test* "format ~x" " -12345" (format "~10x" -74565))
744 (test* "format ~x" " +12345" (format "~10@x" 74565))
745 (test* "format ~x" " -12345" (format "~10@x" -74565))
746 (test* "format ~x" "0000012345" (format "~10,'0x" 74565))
747 (test* "format ~x" "0000-12345" (format "~10,'0x" -74565))
748
749 (test* "format ~f" "1.2345" (format "~f" 1.2345))
750 (test* "format ~5,3f" "1.234" (format "~5,3f" 1.2345))
751 ;(test* "format ~5,3:f" "1.234" (format "~5,3:f" 1.2345)) ;banker's rounding
752 (test* "format ~5,3:f" "1.235" (format "~5,3:f" 1.2345)) ;commercial rounding
753 (test* "format ~6,2f" " 1.23" (format "~6,2f" 1.2345))
754 (test* "format ~6,2f" " 5.01" (format "~6,2f" 5.015))
755 (test* "format ~6,2:f" " 5.02" (format "~6,2:f" 5.015))
756 (test* "format ~6,2:f" "-10.00" (format "~6,2:f" -9.995))
757 (test* "format ~10f" " abcde" (format "~10,2f" 'abcde))
758 (test* "format ~1,2f" "0.00" (format "~1,2f" 0.003))
759 (test* "format ~1,0f" "-1." (format "~1,0f" -0.555))
760 (test* "format ~,2f" "0.00" (format "~,2f" 0))
761
762 (test* "format carry over ~,4f" "0.0100" (format "~,4f" 0.00999))
763 (test* "format carry over across decimal point ~,1f" "124.0"
764 (format "~,1f" 123.95))
765 (test* "format carry over across decimal point ~,2f" "-1000.00"
766 (format "~,2f" -999.995))
767
768 (test* "format ~,1000f" (string-append "0.01" (make-string 998 #\0))
769 (format "~,1000f" 0.01))
770
771 (test* "format ~f with non-numeric object" " abc" (format "~6f" 'abc))
772 (test* "format ~f with complex" "0.707+0.707i" (format "~1,3f" (sqrt +i)))
773 (test* "format ~f with complex" "0.707-0.707i" (format "~1,3f" (sqrt -i)))
774 (test* "format ~f with complex" " 1.10+0.46i"
775 (format "~20,2f" (sqrt 1+i)))
776 (test* "format ~f with complex" " +1.10+0.46i"
777 (format "~20,2@f" (sqrt 1+i)))
778
779 (test* "format ~$" "100.00" (format "~$" 100))
780 (test* "format ~$" "0.33" (format "~$" 1/3))
781 (test* "format ~$" "3.14" (format "~$" 3.1415926))
782 (test* "format ~$" "3.14" (format "~2$" 3.1415926))
783 (test* "format ~$" "3.142" (format "~3$" 3.1415926))
784 (test* "format ~$" "3.1416" (format "~4$" 3.1415926))
785 (test* "format ~$" "3.14159" (format "~5$" 3.1415926))
786 (test* "format ~$ pre-digits" "00.00" (format "~,2$" 0))
787 (test* "format ~$ padding" " 0.00" (format "~,,7$" 0))
788 (test* "format ~$ padding" "***0.00" (format "~,,7,'*$" 0))
789 (test* "format ~$ sign" " -3.14" (format "~,,7$" -3.1416))
790 (test* "format ~$ sign" " -3.14" (format "~,,7@$" -3.1416))
791 (test* "format ~$ sign" " +3.14" (format "~,,7@$" 3.1416))
792 (test* "format ~$ sign" " -3.14" (format "~,,7$" -3.1416))
793 (test* "format ~$ sign" " -3.14" (format "~,,7@$" -3.1416))
794 (test* "format ~$ sign" " +3.14" (format "~,,7@$" 3.1416))
795 (test* "format ~$ sign" "- 3.14" (format "~,,7:$" -3.1416))
796 (test* "format ~$ sign" " 3.14" (format "~,,7:$" 3.1416))
797 (test* "format ~$ sign" "- 3.14" (format "~,,7@:$" -3.1416))
798 (test* "format ~$ sign" "+ 3.14" (format "~,,7@:$" 3.1416))
799 (test* "format ~$ non-real" " zzz" (format "~,,7$" 'zzz))
800
801 (test* "format ~nr" "wud0up" (format "~36r" 1985913745))
802 (test* "format ~nr" " wud0up" (format "~36,10r" 1985913745))
803 (test* "format ~nr" "****wud0up" (format "~36,10,'*r" 1985913745))
804 (test* "format ~nr" "***+wud0up" (format "~36,10,'*@r" 1985913745))
805 (test* "format ~nR" "WUD0UP" (format "~36R" 1985913745))
806 (test* "format ~vr" "wud0up" (format "~vr" 36 1985913745))
807 (test* "format ~vr" " wud0up" (format "~v,10r" 36 1985913745))
808
809 (test* "format v param" " 12345"
810 (format "~vd" 10 12345))
811 (test* "format v param" "0000012345"
812 (format "~v,vd" 10 #\0 12345))
813
814 (test* "format ~*" "1 2 4 5"
815 (format "~a ~a ~*~a ~a" 1 2 3 4 5))
816 (test* "format ~*" "1 5"
817 (format "~a ~3*~a" 1 2 3 4 5))
818 (test* "format ~:*" "1 2 2 3 4 5"
819 (format "~a ~a ~:*~a ~a ~a ~a" 1 2 3 4 5))
820 (test* "format ~:*" "1 2 3 1 2 3 4 5"
821 (format "~a ~a ~a ~3:*~a ~a ~a ~a ~a" 1 2 3 4 5))
822 (test* "format ~:*" "1 2 3 1"
823 (format "~a ~a ~a ~3:*~a" 1 2 3 4 5))
824 (test* "format ~@*" "1 2 5"
825 (format "~a ~a ~4@*~a" 1 2 3 4 5))
826
827 (test* "format ~&" "abc" (format "~&abc"))
828 (test* "format ~&" "abc\ndef" (format "abc~&def"))
829 (test* "format ~&" "abc\ndef" (format "abc\n~&def"))
830 (test* "format ~&" "abc\ndef" (format "abc~%~&def"))
831 (test* "format ~&" "abc\ndef" (format "abc~%~&~&def"))
832 (test* "format ~n&" "\n\nabc" (format "~3&abc"))
833 (test* "format ~n&" "abc\n\n\ndef" (format "abc~3&def"))
834
835 (test* "format single character"
836 '(("~" "~~~" "~~~~~")
837 ("\t" "\t\t\t" "\t\t\t\t\t")
838 ("\n" "\n\n\n" "\n\n\n\n\n")
839 ("\f" "\f\f\f" "\f\f\f\f\f"))
840 (map (^c (list (format (string #\~ c))
841 (format (string #\~ #\3 c))
842 (format (string #\~ #\v c) 5)))
843 '(#\~ #\t #\% #\|)))
844
845 (test* "format string node concatenation"
846 "~az" (format "~~a~a" 'z))
847
848 (test* "format ~?" "ding[\"dang\" dong]ping"
849 (format "~s~?~a" 'ding "[~s ~a]" '("dang" "dong") "ping"))
850 (test* "format ~?" "ding[\"dang\" gong]ping" ; extra args for ~? are ignored
851 (format "~s~?~a" 'ding "[~s ~a]" '("dang" "gong" "dong") "ping"))
852 (test* "format ~@?" "ding[\"dang\" dong]ping"
853 (format "~s~@?~a" 'ding "[~s ~a]" "dang" "dong" "ping"))
854 (test* "format nested ~?" "<[<<{\"O\" O}>>]>"
855 (format "~s[~?]~s" '< "~s{~?}~s" '(<< "~s ~a" ("O" "O") >>) '>))
856 (test* "format nested ~@?" "<[<<{\"O\" O}>>]>"
857 (format "~s[~@?]~s" '< "~s{~@?}~s" '<< "~s ~a" "O" "O" '>> '>))
858
859
860 (test* "format ~ + newline" "abcdef"
861 (format "abc~\ndef"))
862 (test* "format ~ + newline" "abcdef"
863 (format "abc~\n def"))
864 (test* "format ~ + newline" "abcdef"
865 (format "abc~\n \n def"))
866 (test* "format ~@ + newline" "abc\ndef"
867 (format "abc~@\ndef"))
868 (test* "format ~@ + newline" "abc\ndef"
869 (format "abc~@\n def"))
870 (test* "format ~@ + newline" "abc\ndef"
871 (format "abc~@\n \ndef"))
872 (test* "format ~: + newline" "abcdef"
873 (format "abc~:\ndef"))
874 (test* "format ~: + newline" "abc def"
875 (format "abc~:\n def"))
876 (test* "format ~: + newline" "abc \n def"
877 (format "abc~:\n \n def"))
878
879 (test* "format incomplete tilde sequence" (test-error)
880 (format "~"))
881 (test* "format incomplete tilde sequence" (test-error)
882 (format "~123"))
883
884 (test* "format skip to the end" "||"
885 (format "|~*|" 1))
886
887 ;; regression check for format/ss
888 (test* "format/ss" "z " (format/ss "~v,a" 3 'z))
889
890 ;;-------------------------------------------------------------------
891 (test-section "some corner cases in list reader")
892
893 (define (dot-reader-tester str expect)
894 (test* (format "dot ~a" str) expect (read-from-string str)))
895
896 (dot-reader-tester "(().())" '(()))
897 (dot-reader-tester "([].[])" '(()))
898 (dot-reader-tester "(x .,y)" '(x unquote y))
899 (dot-reader-tester "(x .,@y)" '(x unquote-splicing y))
900 (dot-reader-tester "(().)" (test-error <read-error>))
901 (dot-reader-tester "((). .)" (test-error <read-error>))
902
903
904 ;;-------------------------------------------------------------------
905 (test-section "nested multi-line comments")
906
907 (test* "#|...|#" '(foo bar baz)
908 (read-from-string "(foo bar #||# baz)"))
909 (test* "#|...|#" '(foo bar baz)
910 (read-from-string "(foo bar #| oof rab |# baz)"))
911 (test* "#|...|#" '(foo bar baz)
912 (read-from-string "(foo bar #| oof rab) |# baz)"))
913 (test* "#|...|# (multiline)" '(foo bar baz)
914 (read-from-string "(foo bar #| oof \nrab) |# baz)"))
915 (test* "#|...|# (multiline)" '(foo bar baz)
916 (read-from-string "(foo bar #| oof \nrab)\n|# baz)"))
917 (test* "#|...|# (nested)" '(foo bar baz)
918 (read-from-string "(foo bar #| oof #|\nrab)|#\n|# baz)"))
919 (test* "#|...|# (nested)" '(foo bar baz)
920 (read-from-string "(foo bar #|#|\nrab)|#|# baz)"))
921 (test* "#|...|# (intertwined with string)"
922 '(foo bar this is outside of comment "hence this is in a string")
923 (read-from-string
924 "(foo bar #| grok
925 \"the following bar-and-sharp terminates the comment |#
926 this is outside of comment
927 \"hence this is in a string\")
928 "))
929 (test* "#|...|# (intertwined with string)"
930 '(foo bar
931 "#| this is a string, not a comment"
932 |# and this is not a comment terminator but an escaped symbol|)
933 (read-from-string
934 "(foo bar
935 \"#| this is a string, not a comment\"
936 |# and this is not a comment terminator but an escaped symbol|)"))
937
938 ;;-------------------------------------------------------------------
939 (test-section "#;-style commend (a la Chez)")
940
941 (test* "#;123 456" 456
942 (read-from-string "#;123 456"))
943 (test* "#; (123 456) 789" 789
944 (read-from-string "#; (123 456) 789"))
945 (test* "#;#;(123 456) 789 1" 1
946 (read-from-string "#;#;(123 456) 789 1"))
947
948 (define *counter* 0)
949
950 (define-reader-ctor 'countup
951 (^() (inc! *counter*) #t))
952
953 (test* "S-expression comment and read-time constructor" 2
954 (begin (list #,(countup) #;#,(countup) #,(countup))
955 *counter*))
956
957 ;;-------------------------------------------------------------------
958 (test-section "port->* basic")
959
960 ;; testing port->string etc.
961
962 (define (%test-port->* name proc data writer)
963 (test* (format "~a ~s" name data) data
964 (begin
965 (sys-unlink "tmp2.o")
966 (with-output-to-file "tmp2.o" (cut writer data))
967 (call-with-input-file "tmp2.o" proc))))
968 (define-syntax test-port->*
969 (syntax-rules ()
970 [(_ proc data writer) (%test-port->* 'proc proc data writer)]))
971
972 (test-port->* port->string "" display)
973 (test-port->* port->string "abc" display)
974 (test-port->* port->string "abc\ndef\n" display)
975 (test-port->* (.$ string-complete->incomplete port->string)
976 #*"\x00\x80\xc0\xd0\xff\xfe\xef" display)
977
978 (test-port->* port->string-list '("abc") (cut for-each print <>))
979 (test-port->* port->string-list '("abc" "def") (cut for-each print <>))
980 (test-port->* (.$ (cut map string-complete->incomplete <>) port->string-list)
981 '(#*"\x00\x80\xc0\xd0\xff\xfe\xef" #*"abc")
982 (cut for-each (^z (display z) (newline)) <>))
983
984 (test-port->* port->sexp-list '(abc) (cut for-each print <>))
985
986 ;;-------------------------------------------------------------------
987 (test-section "coding-aware-port basic")
988
989 ;; Testing source port _without_ any conversion. Basically, these
990 ;; tests just checks up the boundary condition of source-port prefetching
991 ;; routine.
992 ;; The actual conversion is tested in ext/charconv.
993
994 (define (with-coding-aware-port input proc)
995 (let* ([src (open-input-string input)]
996 [wrap (open-coding-aware-port src)])
997 (proc src wrap)))
998
999 (test* "ownership" #t
1000 (with-coding-aware-port
1001 "abc"
1002 (^[src wrap] (close-input-port wrap) (port-closed? src))))
1003
1004 (test* "read from empty port" '(#t #t #t #t #t)
1005 (map (^p (with-coding-aware-port "" p))
1006 (list (^[src wrap] (eof-object? (read-char wrap)))
1007 (^[src wrap] (eof-object? (read-byte wrap)))
1008 (^[src wrap] (eof-object? (peek-char wrap)))
1009 (^[src wrap] (eof-object? (peek-byte wrap)))
1010 (^[src wrap] (eof-object? (read-line wrap))))))
1011
1012 (let ([tdata '("abc" "abc\n" "\nabc" "abc\ndef\n"
1013 "abc\ndef\nghi" "abc\ndef\nghi\n"
1014 "abc\r\ndef\r\nghi" "abc\r\ndef\r\nghi\r\n"
1015 "abc\rdef\rghi" "abc\rdef\rghi\r"
1016 "abc\ndef\nghi\njkl" "abc\ndef\nghi\njkl\n")])
1017 (test* "read from simple contents"
1018 tdata
1019 (map (^i (with-coding-aware-port
1020 i
1021 (^[src wrap]
1022 (let loop ((ch (read-char wrap))
1023 (r '()))
1024 (if (eof-object? ch)
1025 (list->string (reverse r))
1026 (loop (read-char wrap) (cons ch r)))))))
1027 tdata)))
1028
1029 (let ([tdata '("coding: abcde\naa"
1030 ";coding:\nabcdef\naa"
1031 ";coding:\r\nabcdef\r\naa"
1032 ";coding:\rabcdef\raa"
1033 "coding: coding: coding:; abcde\naa"
1034 "coding: coding: coding:; abcde\r\naa"
1035 ";; co\nding: foobar\naa"
1036 ";; co\r\nding: foobar\r\naa"
1037 ";; co\rding: foobar\raa"
1038 ";; coding:\n;; foobar\naa"
1039 ";; coding : foobar\naa"
1040 ";; coding : foobar\r\naa"
1041 ";; coding : foobar\raa"
1042 "\n\n;; coding: foobar\naa"
1043 "\r\n\r\n;; coding: foobar\r\naa"
1044 "\r\r;; coding: foobar\raa"
1045 "\n;; codincodincoding:\naa")])
1046 (test* "to confuse DFA"
1047 tdata
1048 (map (^i (with-coding-aware-port
1049 i
1050 (^[src wrap]
1051 (let loop ((ch (read-char wrap))
1052 (r '()))
1053 (if (eof-object? ch)
1054 (list->string (reverse r))
1055 (loop (read-char wrap) (cons ch r)))))))
1056 tdata)))
1057
1058 ;;-------------------------------------------------------------------
1059 ;; Literal uvector on load
1060 ;; Cf. https://github.com/shirok/Gauche/issues/521
1061
1062 (load (string-append (sys-dirname (current-load-path)) "/load-literal-test"))
1063
1064 (test* "uvector immutability on load & read" '(#t #f #f)
1065 (map uvector-immutable? (load-read-test-uvectors)))
1066
1067 (test-end)