Merge remote-tracking branch 'origin/stable-2.0'
[bpt/guile.git] / test-suite / tests / ports.test
CommitLineData
7b041912 1;;;; ports.test --- Guile I/O ports. -*- coding: utf-8; mode: scheme; -*-
7ef450bf 2;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
000ee07f 3;;;;
2e59af21 4;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010,
2a7d614c 5;;;; 2011, 2012, 2013 Free Software Foundation, Inc.
2e59af21 6;;;;
53befeb7
NJ
7;;;; This library is free software; you can redistribute it and/or
8;;;; modify it under the terms of the GNU Lesser General Public
9;;;; License as published by the Free Software Foundation; either
10;;;; version 3 of the License, or (at your option) any later version.
000ee07f 11;;;;
53befeb7 12;;;; This library is distributed in the hope that it will be useful,
000ee07f 13;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
53befeb7
NJ
14;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15;;;; Lesser General Public License for more details.
000ee07f 16;;;;
53befeb7
NJ
17;;;; You should have received a copy of the GNU Lesser General Public
18;;;; License along with this library; if not, write to the Free Software
19;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
000ee07f 20
8aa28a91 21(define-module (test-suite test-ports)
2e59af21
LC
22 #:use-module (test-suite lib)
23 #:use-module (test-suite guile-test)
24 #:use-module (ice-9 popen)
25 #:use-module (ice-9 rdelim)
cc540d0b 26 #:use-module (rnrs bytevectors)
7f6c3f8f
MW
27 #:use-module ((ice-9 binary-ports) #:select (open-bytevector-input-port
28 open-bytevector-output-port
29 put-bytevector
30 get-bytevector-n
31 get-bytevector-all
32 unget-bytevector)))
000ee07f
JB
33
34(define (display-line . args)
35 (for-each display args)
36 (newline))
37
38(define (test-file)
c685b42f 39 (data-file-name "ports-test.tmp"))
000ee07f
JB
40
41\f
42;;;; Some general utilities for testing ports.
43
d6a6989e
LC
44;; Make sure we are set up for 8-bit Latin-1 data.
45(fluid-set! %default-port-encoding "ISO-8859-1")
46(for-each (lambda (p)
47 (set-port-encoding! p (fluid-ref %default-port-encoding)))
48 (list (current-input-port) (current-output-port)
49 (current-error-port)))
889975e5 50
000ee07f
JB
51;;; Read from PORT until EOF, and return the result as a string.
52(define (read-all port)
53 (let loop ((chars '()))
54 (let ((char (read-char port)))
55 (if (eof-object? char)
56 (list->string (reverse! chars))
57 (loop (cons char chars))))))
58
59(define (read-file filename)
60 (let* ((port (open-input-file filename))
61 (string (read-all port)))
62 (close-port port)
63 string))
64
65\f
b22e94db
LC
66
67(with-test-prefix "%default-port-conversion-strategy"
68
69 (pass-if "initial value"
70 (eq? 'substitute (fluid-ref %default-port-conversion-strategy)))
71
72 (pass-if "file port"
73 (let ((strategies '(error substitute escape)))
74 (equal? (map (lambda (s)
75 (with-fluids ((%default-port-conversion-strategy s))
76 (call-with-output-file "/dev/null"
77 (lambda (p)
78 (port-conversion-strategy p)))))
79 strategies)
80 strategies)))
81
82 (pass-if "(set-port-conversion-strategy! #f sym)"
83 (begin
84 (set-port-conversion-strategy! #f 'error)
85 (and (eq? (fluid-ref %default-port-conversion-strategy) 'error)
86 (begin
87 (set-port-conversion-strategy! #f 'substitute)
88 (eq? (fluid-ref %default-port-conversion-strategy)
89 'substitute)))))
90
91)
92
93\f
000ee07f
JB
94;;;; Normal file ports.
95
96;;; Write out an s-expression, and read it back.
57e7f270
DH
97(let ((string '("From fairest creatures we desire increase,"
98 "That thereby beauty's rose might never die,"))
99 (filename (test-file)))
100 (let ((port (open-output-file filename)))
101 (write string port)
102 (close-port port))
103 (let ((port (open-input-file filename)))
104 (let ((in-string (read port)))
105 (pass-if "file: write and read back list of strings"
106 (equal? string in-string)))
107 (close-port port))
108 (delete-file filename))
000ee07f
JB
109
110;;; Write out a string, and read it back a character at a time.
57e7f270
DH
111(let ((string "This is a test string\nwith no newline at the end")
112 (filename (test-file)))
113 (let ((port (open-output-file filename)))
114 (display string port)
115 (close-port port))
116 (let ((in-string (read-file filename)))
117 (pass-if "file: write and read back characters"
118 (equal? string in-string)))
119 (delete-file filename))
000ee07f 120
7c035009 121;;; Buffered input/output port with seeking.
57e7f270
DH
122(let* ((filename (test-file))
123 (port (open-file filename "w+")))
124 (display "J'Accuse" port)
125 (seek port -1 SEEK_CUR)
126 (pass-if "file: r/w 1"
127 (char=? (read-char port) #\e))
128 (pass-if "file: r/w 2"
129 (eof-object? (read-char port)))
130 (seek port -1 SEEK_CUR)
131 (write-char #\x port)
132 (seek port 7 SEEK_SET)
133 (pass-if "file: r/w 3"
134 (char=? (read-char port) #\x))
135 (seek port -2 SEEK_END)
136 (pass-if "file: r/w 4"
137 (char=? (read-char port) #\s))
8f99e3f3 138 (close-port port)
57e7f270 139 (delete-file filename))
7c035009
GH
140
141;;; Unbuffered input/output port with seeking.
57e7f270
DH
142(let* ((filename (test-file))
143 (port (open-file filename "w+0")))
144 (display "J'Accuse" port)
145 (seek port -1 SEEK_CUR)
146 (pass-if "file: ub r/w 1"
147 (char=? (read-char port) #\e))
148 (pass-if "file: ub r/w 2"
149 (eof-object? (read-char port)))
150 (seek port -1 SEEK_CUR)
151 (write-char #\x port)
152 (seek port 7 SEEK_SET)
153 (pass-if "file: ub r/w 3"
154 (char=? (read-char port) #\x))
155 (seek port -2 SEEK_END)
156 (pass-if "file: ub r/w 4"
157 (char=? (read-char port) #\s))
8f99e3f3 158 (close-port port)
57e7f270 159 (delete-file filename))
7c035009 160
4fcd6551 161;;; Buffered output-only and input-only ports with seeking.
57e7f270
DH
162(let* ((filename (test-file))
163 (port (open-output-file filename)))
164 (display "J'Accuse" port)
165 (pass-if "file: out tell"
166 (= (seek port 0 SEEK_CUR) 8))
167 (seek port -1 SEEK_CUR)
168 (write-char #\x port)
169 (close-port port)
170 (let ((iport (open-input-file filename)))
171 (pass-if "file: in tell 0"
172 (= (seek iport 0 SEEK_CUR) 0))
173 (read-char iport)
174 (pass-if "file: in tell 1"
175 (= (seek iport 0 SEEK_CUR) 1))
176 (unread-char #\z iport)
177 (pass-if "file: in tell 0 after unread"
178 (= (seek iport 0 SEEK_CUR) 0))
179 (pass-if "file: unread char still there"
180 (char=? (read-char iport) #\z))
181 (seek iport 7 SEEK_SET)
182 (pass-if "file: in last char"
183 (char=? (read-char iport) #\x))
184 (close-port iport))
185 (delete-file filename))
4fcd6551 186
7f214e60 187;;; unusual characters.
57e7f270
DH
188(let* ((filename (test-file))
189 (port (open-output-file filename)))
190 (display (string #\nul (integer->char 255) (integer->char 128)
191 #\nul) port)
192 (close-port port)
193 (let* ((port (open-input-file filename))
194 (line (read-line port)))
195 (pass-if "file: read back NUL 1"
196 (char=? (string-ref line 0) #\nul))
197 (pass-if "file: read back 255"
198 (char=? (string-ref line 1) (integer->char 255)))
199 (pass-if "file: read back 128"
200 (char=? (string-ref line 2) (integer->char 128)))
201 (pass-if "file: read back NUL 2"
202 (char=? (string-ref line 3) #\nul))
203 (pass-if "file: EOF"
8f99e3f3
SJ
204 (eof-object? (read-char port)))
205 (close-port port))
57e7f270 206 (delete-file filename))
7f214e60 207
0eb2e8cd 208;;; line buffering mode.
57e7f270
DH
209(let* ((filename (test-file))
210 (port (open-file filename "wl"))
211 (test-string "one line more or less"))
212 (write-line test-string port)
213 (let* ((in-port (open-input-file filename))
214 (line (read-line in-port)))
215 (close-port in-port)
216 (close-port port)
217 (pass-if "file: line buffering"
218 (string=? line test-string)))
219 (delete-file filename))
0eb2e8cd 220
e50d921b
MG
221;;; read-line should use the port encoding (not the locale encoding).
222(let ((str "ĉu bone?"))
211683cc
MG
223 (with-locale "C"
224 (let* ((filename (test-file))
225 (port (open-file filename "wl")))
226 (set-port-encoding! port "UTF-8")
227 (write-line str port)
228 (let ((in-port (open-input-file filename)))
229 (set-port-encoding! in-port "UTF-8")
230 (let ((line (read-line in-port)))
231 (close-port in-port)
232 (close-port port)
233 (pass-if "file: read-line honors port encoding"
234 (string=? line str))))
235 (delete-file filename))))
236
237;;; binary mode ignores port encoding
238(pass-if "file: binary mode ignores port encoding"
239 (with-fluids ((%default-port-encoding "UTF-8"))
240 (let* ((filename (test-file))
241 (port (open-file filename "w"))
242 (test-string "一二三")
243 (binary-test-string
244 (apply string
245 (map integer->char
246 (uniform-vector->list
247 (string->utf8 test-string))))))
248 (write-line test-string port)
249 (close-port port)
250 (let* ((in-port (open-file filename "rb"))
251 (line (read-line in-port)))
252 (close-port in-port)
253 (delete-file filename)
254 (string=? line binary-test-string)))))
255
256;;; binary mode ignores file coding declaration
257(pass-if "file: binary mode ignores file coding declaration"
258 (with-fluids ((%default-port-encoding "UTF-8"))
259 (let* ((filename (test-file))
260 (port (open-file filename "w"))
261 (test-string "一二三")
262 (binary-test-string
263 (apply string
264 (map integer->char
265 (uniform-vector->list
266 (string->utf8 test-string))))))
267 (write-line ";; coding: utf-8" port)
268 (write-line test-string port)
269 (close-port port)
270 (let* ((in-port (open-file filename "rb"))
271 (line1 (read-line in-port))
272 (line2 (read-line in-port)))
273 (close-port in-port)
274 (delete-file filename)
275 (string=? line2 binary-test-string)))))
276
3ace9a8e
MW
277;; open-file ignores file coding declaration by default
278(pass-if "file: open-file ignores coding declaration by default"
211683cc
MG
279 (with-fluids ((%default-port-encoding "UTF-8"))
280 (let* ((filename (test-file))
281 (port (open-output-file filename))
282 (test-string "€100"))
211683cc
MG
283 (write-line ";; coding: iso-8859-15" port)
284 (write-line test-string port)
285 (close-port port)
286 (let* ((in-port (open-input-file filename))
287 (line1 (read-line in-port))
288 (line2 (read-line in-port)))
289 (close-port in-port)
290 (delete-file filename)
291 (string=? line2 test-string)))))
e50d921b 292
3ace9a8e
MW
293;; open-input-file with guess-encoding honors coding declaration
294(pass-if "file: open-input-file with guess-encoding honors coding declaration"
295 (with-fluids ((%default-port-encoding "UTF-8"))
296 (let* ((filename (test-file))
297 (port (open-output-file filename))
298 (test-string "€100"))
299 (set-port-encoding! port "iso-8859-15")
300 (write-line ";; coding: iso-8859-15" port)
301 (write-line test-string port)
302 (close-port port)
303 (let* ((in-port (open-input-file filename
304 #:guess-encoding #t))
305 (line1 (read-line in-port))
306 (line2 (read-line in-port)))
307 (close-port in-port)
308 (delete-file filename)
309 (string=? line2 test-string)))))
310
311(with-test-prefix "keyword arguments for file openers"
312 (with-fluids ((%default-port-encoding "UTF-8"))
313 (let ((filename (test-file)))
314
315 (with-test-prefix "write #:encoding"
316
317 (pass-if-equal "open-file"
318 #vu8(116 0 101 0 115 0 116 0)
319 (let ((port (open-file filename "w"
320 #:encoding "UTF-16LE")))
321 (display "test" port)
322 (close-port port))
323 (let* ((port (open-file filename "rb"))
324 (bv (get-bytevector-all port)))
325 (close-port port)
326 bv))
327
328 (pass-if-equal "open-output-file"
329 #vu8(116 0 101 0 115 0 116 0)
330 (let ((port (open-output-file filename
331 #:encoding "UTF-16LE")))
332 (display "test" port)
333 (close-port port))
334 (let* ((port (open-file filename "rb"))
335 (bv (get-bytevector-all port)))
336 (close-port port)
337 bv))
338
339 (pass-if-equal "call-with-output-file"
340 #vu8(116 0 101 0 115 0 116 0)
341 (call-with-output-file filename
342 (lambda (port)
343 (display "test" port))
344 #:encoding "UTF-16LE")
345 (let* ((port (open-file filename "rb"))
346 (bv (get-bytevector-all port)))
347 (close-port port)
348 bv))
349
350 (pass-if-equal "with-output-to-file"
351 #vu8(116 0 101 0 115 0 116 0)
352 (with-output-to-file filename
353 (lambda ()
354 (display "test"))
355 #:encoding "UTF-16LE")
356 (let* ((port (open-file filename "rb"))
357 (bv (get-bytevector-all port)))
358 (close-port port)
359 bv))
360
361 (pass-if-equal "with-error-to-file"
362 #vu8(116 0 101 0 115 0 116 0)
363 (with-error-to-file
364 filename
365 (lambda ()
366 (display "test" (current-error-port)))
367 #:encoding "UTF-16LE")
368 (let* ((port (open-file filename "rb"))
369 (bv (get-bytevector-all port)))
370 (close-port port)
371 bv)))
372
373 (with-test-prefix "write #:binary"
374
375 (pass-if-equal "open-output-file"
376 "ISO-8859-1"
377 (let* ((port (open-output-file filename #:binary #t))
378 (enc (port-encoding port)))
379 (close-port port)
380 enc))
381
382 (pass-if-equal "call-with-output-file"
383 "ISO-8859-1"
384 (call-with-output-file filename port-encoding #:binary #t))
385
386 (pass-if-equal "with-output-to-file"
387 "ISO-8859-1"
388 (with-output-to-file filename
389 (lambda () (port-encoding (current-output-port)))
390 #:binary #t))
391
392 (pass-if-equal "with-error-to-file"
393 "ISO-8859-1"
394 (with-error-to-file
395 filename
396 (lambda () (port-encoding (current-error-port)))
397 #:binary #t)))
398
399 (with-test-prefix "read #:encoding"
400
401 (pass-if-equal "open-file read #:encoding"
402 "test"
403 (call-with-output-file filename
404 (lambda (port)
405 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
406 (let* ((port (open-file filename "r" #:encoding "UTF-16LE"))
407 (str (read-string port)))
408 (close-port port)
409 str))
410
411 (pass-if-equal "open-input-file #:encoding"
412 "test"
413 (call-with-output-file filename
414 (lambda (port)
415 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
416 (let* ((port (open-input-file filename #:encoding "UTF-16LE"))
417 (str (read-string port)))
418 (close-port port)
419 str))
420
421 (pass-if-equal "call-with-input-file #:encoding"
422 "test"
423 (call-with-output-file filename
424 (lambda (port)
425 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
426 (call-with-input-file filename
427 read-string
428 #:encoding "UTF-16LE"))
429
430 (pass-if-equal "with-input-from-file #:encoding"
431 "test"
432 (call-with-output-file filename
433 (lambda (port)
434 (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
435 (with-input-from-file filename
436 read-string
437 #:encoding "UTF-16LE")))
438
439 (with-test-prefix "read #:binary"
440
441 (pass-if-equal "open-input-file"
442 "ISO-8859-1"
443 (let* ((port (open-input-file filename #:binary #t))
444 (enc (port-encoding port)))
445 (close-port port)
446 enc))
447
448 (pass-if-equal "call-with-input-file"
449 "ISO-8859-1"
450 (call-with-input-file filename port-encoding #:binary #t))
451
452 (pass-if-equal "with-input-from-file"
453 "ISO-8859-1"
454 (with-input-from-file filename
455 (lambda () (port-encoding (current-input-port)))
456 #:binary #t)))
457
458 (with-test-prefix "#:guess-encoding with coding declaration"
459
460 (pass-if-equal "open-file"
461 "€100"
462 (with-output-to-file filename
463 (lambda ()
464 (write-line "test")
465 (write-line "; coding: ISO-8859-15")
466 (write-line "€100"))
467 #:encoding "ISO-8859-15")
468 (let* ((port (open-file filename "r"
469 #:guess-encoding #t
470 #:encoding "UTF-16LE"))
471 (str (begin (read-line port)
472 (read-line port)
473 (read-line port))))
474 (close-port port)
475 str))
476
477 (pass-if-equal "open-input-file"
478 "€100"
479 (with-output-to-file filename
480 (lambda ()
481 (write-line "test")
482 (write-line "; coding: ISO-8859-15")
483 (write-line "€100"))
484 #:encoding "ISO-8859-15")
485 (let* ((port (open-input-file filename
486 #:guess-encoding #t
487 #:encoding "UTF-16LE"))
488 (str (begin (read-line port)
489 (read-line port)
490 (read-line port))))
491 (close-port port)
492 str))
493
494 (pass-if-equal "call-with-input-file"
495 "€100"
496 (with-output-to-file filename
497 (lambda ()
498 (write-line "test")
499 (write-line "; coding: ISO-8859-15")
500 (write-line "€100"))
501 #:encoding "ISO-8859-15")
502 (call-with-input-file filename
503 (lambda (port)
504 (read-line port)
505 (read-line port)
506 (read-line port))
507 #:guess-encoding #t
508 #:encoding "UTF-16LE"))
509
510 (pass-if-equal "with-input-from-file"
511 "€100"
512 (with-output-to-file filename
513 (lambda ()
514 (write-line "test")
515 (write-line "; coding: ISO-8859-15")
516 (write-line "€100"))
517 #:encoding "ISO-8859-15")
518 (with-input-from-file filename
519 (lambda ()
520 (read-line)
521 (read-line)
522 (read-line))
523 #:guess-encoding #t
524 #:encoding "UTF-16LE")))
525
526 (with-test-prefix "#:guess-encoding without coding declaration"
527
528 (pass-if-equal "open-file"
529 "€100"
530 (with-output-to-file filename
531 (lambda () (write-line "€100"))
532 #:encoding "ISO-8859-15")
533 (let* ((port (open-file filename "r"
534 #:guess-encoding #t
535 #:encoding "ISO-8859-15"))
536 (str (read-line port)))
537 (close-port port)
538 str))
539
540 (pass-if-equal "open-input-file"
541 "€100"
542 (with-output-to-file filename
543 (lambda () (write-line "€100"))
544 #:encoding "ISO-8859-15")
545 (let* ((port (open-input-file filename
546 #:guess-encoding #t
547 #:encoding "ISO-8859-15"))
548 (str (read-line port)))
549 (close-port port)
550 str))
551
552 (pass-if-equal "call-with-input-file"
553 "€100"
554 (with-output-to-file filename
555 (lambda () (write-line "€100"))
556 #:encoding "ISO-8859-15")
557 (call-with-input-file filename
558 read-line
559 #:guess-encoding #t
560 #:encoding "ISO-8859-15"))
561
562 (pass-if-equal "with-input-from-file"
563 "€100"
564 (with-output-to-file filename
565 (lambda () (write-line "€100"))
566 #:encoding "ISO-8859-15")
567 (with-input-from-file filename
568 read-line
569 #:guess-encoding #t
570 #:encoding "ISO-8859-15")))
571
572 (delete-file filename))))
573
d1b143e9 574;;; ungetting characters and strings.
57e7f270
DH
575(with-input-from-string "walk on the moon\nmoon"
576 (lambda ()
577 (read-char)
578 (unread-char #\a (current-input-port))
579 (pass-if "unread-char"
580 (char=? (read-char) #\a))
581 (read-line)
582 (let ((replacenoid "chicken enchilada"))
583 (unread-char #\newline (current-input-port))
584 (unread-string replacenoid (current-input-port))
585 (pass-if "unread-string"
586 (string=? (read-line) replacenoid)))
587 (pass-if "unread residue"
588 (string=? (read-line) "moon"))))
d1b143e9 589
6e822cce 590;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
8cc58ec1
GH
591;;; the reading end. try to read a byte: should get EAGAIN or
592;;; EWOULDBLOCK error.
57e7f270
DH
593(let* ((p (pipe))
594 (r (car p)))
595 (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
596 (pass-if "non-blocking-I/O"
597 (catch 'system-error
598 (lambda () (read-char r) #f)
599 (lambda (key . args)
600 (and (eq? key 'system-error)
601 (let ((errno (car (list-ref args 3))))
602 (or (= errno EAGAIN)
603 (= errno EWOULDBLOCK))))))))
22d35615 604
000ee07f 605\f
6e822cce 606;;;; Pipe (popen) ports.
000ee07f
JB
607
608;;; Run a command, and read its output.
57e7f270
DH
609(let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
610 (in-string (read-all pipe)))
611 (close-pipe pipe)
612 (pass-if "pipe: read"
613 (equal? in-string "Howdy there, partner!\n")))
000ee07f
JB
614
615;;; Run a command, send some output to it, and see if it worked.
57e7f270
DH
616(let* ((filename (test-file))
617 (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
618 (display "Now Jimmy lives on a mushroom cloud\n" pipe)
619 (display "Mommy, why does everybody have a bomb?\n" pipe)
620 (close-pipe pipe)
621 (let ((in-string (read-file filename)))
622 (pass-if "pipe: write"
623 (equal? in-string "Mommy, why does everybody have a bomb?\n")))
624 (delete-file filename))
000ee07f
JB
625
626\f
627;;;; Void ports. These are so trivial we don't test them.
628
629\f
630;;;; String ports.
631
73cb0a97
JB
632(with-test-prefix "string ports"
633
634 ;; Write text to a string port.
57e7f270
DH
635 (let* ((string "Howdy there, partner!")
636 (in-string (call-with-output-string
637 (lambda (port)
638 (display string port)
639 (newline port)))))
640 (pass-if "display text"
641 (equal? in-string (string-append string "\n"))))
000ee07f 642
73cb0a97 643 ;; Write an s-expression to a string port.
57e7f270
DH
644 (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
645 (in-sexpr
646 (call-with-input-string (call-with-output-string
647 (lambda (port)
648 (write sexpr port)))
649 read)))
650 (pass-if "write/read sexpr"
651 (equal? in-sexpr sexpr)))
2d9e5bca
GH
652
653 ;; seeking and unreading from an input string.
57e7f270
DH
654 (let ((text "that text didn't look random to me"))
655 (call-with-input-string text
656 (lambda (p)
657 (pass-if "input tell 0"
658 (= (seek p 0 SEEK_CUR) 0))
659 (read-char p)
660 (pass-if "input tell 1"
661 (= (seek p 0 SEEK_CUR) 1))
662 (unread-char #\x p)
663 (pass-if "input tell back to 0"
664 (= (seek p 0 SEEK_CUR) 0))
665 (pass-if "input ungetted char"
666 (char=? (read-char p) #\x))
667 (seek p 0 SEEK_END)
668 (pass-if "input seek to end"
669 (= (seek p 0 SEEK_CUR)
670 (string-length text)))
671 (unread-char #\x p)
672 (pass-if "input seek to beginning"
673 (= (seek p 0 SEEK_SET) 0))
674 (pass-if "input reread first char"
675 (char=? (read-char p)
676 (string-ref text 0))))))
677
2d9e5bca 678 ;; seeking an output string.
e4cbd1d8 679 (let* ((text (string-copy "123456789"))
57e7f270
DH
680 (len (string-length text))
681 (result (call-with-output-string
682 (lambda (p)
683 (pass-if "output tell 0"
684 (= (seek p 0 SEEK_CUR) 0))
685 (display text p)
686 (pass-if "output tell end"
687 (= (seek p 0 SEEK_CUR) len))
688 (pass-if "output seek to beginning"
689 (= (seek p 0 SEEK_SET) 0))
690 (write-char #\a p)
691 (seek p -1 SEEK_END)
692 (pass-if "output seek to last char"
693 (= (seek p 0 SEEK_CUR)
694 (- len 1)))
695 (write-char #\b p)))))
696 (string-set! text 0 #\a)
697 (string-set! text (- len 1) #\b)
698 (pass-if "output check"
7b041912
LC
699 (string=? text result)))
700
03fcf93b
LC
701 (pass-if "encoding failure leads to exception"
702 ;; Prior to 2.0.6, this would trigger a deadlock in `scm_mkstrport'.
703 ;; See the discussion at <http://bugs.gnu.org/11197>, for details.
704 (catch 'encoding-error
705 (lambda ()
706 (with-fluids ((%default-port-encoding "ISO-8859-1"))
707 (let ((p (open-input-string "λ"))) ; raise an exception
708 #f)))
709 (lambda (key . rest)
710 #t)
711 (lambda (key . rest)
712 ;; At this point, the port-table mutex used to be still held,
713 ;; hence the deadlock. This situation would occur when trying
714 ;; to print a backtrace, for instance.
715 (input-port? (open-input-string "foo")))))
716
7b041912
LC
717 (pass-if "%default-port-encoding is honored"
718 (let ((encodings '("UTF-8" "UTF-16" "ISO-8859-1" "ISO-8859-3")))
719 (equal? (map (lambda (e)
720 (with-fluids ((%default-port-encoding e))
721 (call-with-output-string
722 (lambda (p)
d84783a8
LC
723 (and (string=? e (port-encoding p))
724 (display (port-encoding p) p))))))
7b041912
LC
725 encodings)
726 encodings)))
727
9f6e3f5a
LC
728 (pass-if "%default-port-conversion-strategy is honored"
729 (let ((strategies '(error substitute escape)))
730 (equal? (map (lambda (s)
731 (with-fluids ((%default-port-conversion-strategy s))
732 (call-with-output-string
733 (lambda (p)
734 (and (eq? s (port-conversion-strategy p))
735 (begin
736 (set-port-conversion-strategy! p s)
737 (display (port-conversion-strategy p)
738 p)))))))
739 strategies)
740 (map symbol->string strategies))))
741
7b041912
LC
742 (pass-if "suitable encoding [latin-1]"
743 (let ((str "hello, world"))
744 (with-fluids ((%default-port-encoding "ISO-8859-1"))
745 (equal? str
746 (with-output-to-string
747 (lambda ()
748 (display str)))))))
749
750 (pass-if "suitable encoding [latin-3]"
751 (let ((str "ĉu bone?"))
752 (with-fluids ((%default-port-encoding "ISO-8859-3"))
753 (equal? str
754 (with-output-to-string
755 (lambda ()
756 (display str)))))))
757
9f6e3f5a 758 (pass-if "wrong encoding, error"
7b041912 759 (let ((str "ĉu bone?"))
ef7e4ba3
LC
760 (catch 'encoding-error
761 (lambda ()
762 ;; Latin-1 cannot represent ‘ĉ’.
9f6e3f5a
LC
763 (with-fluids ((%default-port-encoding "ISO-8859-1")
764 (%default-port-conversion-strategy 'error))
ef7e4ba3
LC
765 (with-output-to-string
766 (lambda ()
9f6e3f5a
LC
767 (display str))))
768 #f) ; so the test really fails here
6851d3be 769 (lambda (key subr message errno port chr)
764246cf 770 (and (eqv? chr #\ĉ)
fd5eec2b
LC
771 (string? (strerror errno)))))))
772
2e59af21
LC
773 (pass-if "wrong encoding, substitute"
774 (let ((str "ĉu bone?"))
775 (with-fluids ((%default-port-encoding "ISO-8859-1"))
776 (string=? (with-output-to-string
777 (lambda ()
778 (set-port-conversion-strategy! (current-output-port)
779 'substitute)
780 (display str)))
781 "?u bone?"))))
782
783 (pass-if "wrong encoding, escape"
784 (let ((str "ĉu bone?"))
785 (with-fluids ((%default-port-encoding "ISO-8859-1"))
786 (string=? (with-output-to-string
787 (lambda ()
788 (set-port-conversion-strategy! (current-output-port)
789 'escape)
790 (display str)))
791 "\\u0109u bone?"))))
792
fd5eec2b
LC
793 (pass-if "peek-char [latin-1]"
794 (let ((p (with-fluids ((%default-port-encoding #f))
795 (open-input-string "hello, world"))))
796 (and (char=? (peek-char p) #\h)
797 (char=? (peek-char p) #\h)
798 (char=? (peek-char p) #\h)
799 (= (port-line p) 0)
800 (= (port-column p) 0))))
801
802 (pass-if "peek-char [utf-8]"
803 (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
804 (open-input-string "안녕하세요"))))
805 (and (char=? (peek-char p) #\안)
806 (char=? (peek-char p) #\안)
807 (char=? (peek-char p) #\안)
808 (= (port-line p) 0)
cc540d0b
LC
809 (= (port-column p) 0))))
810
d84783a8
LC
811 (pass-if "peek-char [utf-16]"
812 (let ((p (with-fluids ((%default-port-encoding "UTF-16BE"))
813 (open-input-string "안녕하세요"))))
814 (and (char=? (peek-char p) #\안)
815 (char=? (peek-char p) #\안)
816 (char=? (peek-char p) #\안)
817 (= (port-line p) 0)
818 (= (port-column p) 0))))
819
9a201881
LC
820 ;; Mini DSL to test decoding error handling.
821 (letrec-syntax ((decoding-error?
822 (syntax-rules ()
823 ((_ port exp)
824 (catch 'decoding-error
825 (lambda ()
826 (pk 'exp exp)
827 #f)
828 (lambda (key subr message errno p)
829 (and (eq? p port)
830 (not (= 0 errno))))))))
831 (make-check
832 (syntax-rules (-> error eof)
833 ((_ port (proc -> error))
3009b93e
LC
834 (if (eq? 'substitute
835 (port-conversion-strategy port))
764246cf 836 (eqv? (proc port) #\?)
3009b93e 837 (decoding-error? port (proc port))))
9a201881
LC
838 ((_ port (proc -> eof))
839 (eof-object? (proc port)))
840 ((_ port (proc -> char))
764246cf 841 (eqv? (proc port) char))))
9a201881
LC
842 (make-checks
843 (syntax-rules ()
844 ((_ port check ...)
845 (and (make-check port check) ...))))
e6e286bb
LC
846 (make-peek+read-checks
847 (syntax-rules ()
848 ((_ port (result ...) e1 expected ...)
849 (make-peek+read-checks port
850 (result ...
851 (peek-char -> e1)
852 (read-char -> e1))
853 expected ...))
854 ((_ port (result ...))
855 (make-checks port result ...))
856 ((_ port #f e1 expected ...)
857 (make-peek+read-checks port
858 ((peek-char -> e1)
859 (read-char -> e1))
860 expected ...))))
3009b93e
LC
861
862 (test-decoding-error*
e6e286bb
LC
863 (syntax-rules ()
864 ((_ sequence encoding strategy (expected ...))
865 (begin
866 (pass-if (format #f "test-decoding-error: ~s ~s ~s"
867 'sequence encoding strategy)
868 (let ((p (open-bytevector-input-port
869 (u8-list->bytevector 'sequence))))
870 (set-port-encoding! p encoding)
871 (set-port-conversion-strategy! p strategy)
872 (make-checks p
873 (read-char -> expected) ...)))
874
875 ;; Generate the same test, but with one
876 ;; `peek-char' call before each `read-char'.
877 ;; Both should yield the same result.
878 (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
879 'sequence encoding strategy)
880 (let ((p (open-bytevector-input-port
881 (u8-list->bytevector 'sequence))))
882 (set-port-encoding! p encoding)
883 (set-port-conversion-strategy! p strategy)
3009b93e
LC
884 (make-peek+read-checks p #f expected
885 ...)))))))
886 (test-decoding-error
887 (syntax-rules ()
888 ((_ sequence encoding (expected ...))
889 (begin
890 (test-decoding-error* sequence encoding 'error
891 (expected ...))
9a201881 892
3009b93e
LC
893 ;; `escape' should behave exactly like `error'.
894 (test-decoding-error* sequence encoding 'escape
895 (expected ...))
9a201881 896
3009b93e
LC
897 (test-decoding-error* sequence encoding 'substitute
898 (expected ...)))))))
899
900 (test-decoding-error (255 65 66 67) "UTF-8"
e6e286bb 901 (error #\A #\B #\C eof))
9a201881 902
3009b93e
LC
903 (test-decoding-error (255 206 187 206 188) "UTF-8"
904 (error #\λ #\μ eof))
9a201881 905
3009b93e 906 (test-decoding-error (206 187 206) "UTF-8"
a42d7971 907 ;; Unterminated sequence.
e6e286bb 908 (#\λ error eof))
a42d7971 909
4cadf64f
LC
910 ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
911 ;; of the "Conformance" chapter of Unicode 6.0.0.)
912
3009b93e 913 (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
e6e286bb
LC
914 (error ;; C0: should be in the C2..DF range
915 error ;; 80: invalid
916 #\A
917 eof))
918
3009b93e 919 (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
7be1705d
LC
920 ;; Section 3.9 of Unicode 6.0.0 reads:
921 ;; "If the converter encounters an ill-formed UTF-8 code unit
922 ;; sequence which starts with a valid first byte, but which does
923 ;; not continue with valid successor bytes (see Table 3-7), it
924 ;; must not consume the successor bytes".
925 ;; Glibc/libiconv do not conform to it and instead swallow the
926 ;; #x41. This example appears literally in Section 3.9.
927 (error ;; 41: invalid successor
928 #\A ;; 41: valid starting byte
e6e286bb
LC
929 #\B
930 eof))
4cadf64f 931
7be1705d
LC
932 (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
933 ;; According to Unicode 6.0.0, Section 3.9, "the only formal
934 ;; requirement mandated by Unicode conformance for a converter is
935 ;; that the <41> be processed and correctly interpreted as
936 ;; <U+0041>".
e6e286bb 937 (error ;; 2nd byte should be in the A0..BF range
7be1705d
LC
938 error ;; 80: not a valid starting byte
939 error ;; 80: not a valid starting byte
940 #\A
e6e286bb
LC
941 eof))
942
3009b93e 943 (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
e6e286bb 944 (error ;; 3rd byte should be in the 80..BF range
7be1705d 945 #\A
e6e286bb
LC
946 #\B
947 eof))
4cadf64f 948
3009b93e 949 (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
e6e286bb 950 (error ;; 2nd byte should be in the 90..BF range
7be1705d
LC
951 error ;; 88: not a valid starting byte
952 error ;; 88: not a valid starting byte
953 error ;; 88: not a valid starting byte
e6e286bb 954 eof))))
2d9e5bca 955
ee6eedcd
KR
956(with-test-prefix "call-with-output-string"
957
958 ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
959 ;; occur.
960 (pass-if-exception "proc closes port" exception:wrong-type-arg
961 (call-with-output-string close-port)))
962
000ee07f
JB
963
964\f
965;;;; Soft ports. No tests implemented yet.
966
967\f
968;;;; Generic operations across all port types.
969
970(let ((port-loop-temp (test-file)))
971
972 ;; Return a list of input ports that all return the same text.
973 ;; We map tests over this list.
974 (define (input-port-list text)
975
976 ;; Create a text file some of the ports will use.
977 (let ((out-port (open-output-file port-loop-temp)))
978 (display text out-port)
979 (close-port out-port))
980
981 (list (open-input-file port-loop-temp)
982 (open-input-pipe (string-append "cat " port-loop-temp))
983 (call-with-input-string text (lambda (x) x))
984 ;; We don't test soft ports at the moment.
985 ))
986
987 (define port-list-names '("file" "pipe" "string"))
988
989 ;; Test the line counter.
73cb0a97 990 (define (test-line-counter text second-line final-column)
000ee07f
JB
991 (with-test-prefix "line counter"
992 (let ((ports (input-port-list text)))
993 (for-each
994 (lambda (port port-name)
995 (with-test-prefix port-name
996 (pass-if "at beginning of input"
997 (= (port-line port) 0))
998 (pass-if "read first character"
999 (eqv? (read-char port) #\x))
1000 (pass-if "after reading one character"
1001 (= (port-line port) 0))
1002 (pass-if "read first newline"
1003 (eqv? (read-char port) #\newline))
1004 (pass-if "after reading first newline char"
1005 (= (port-line port) 1))
1006 (pass-if "second line read correctly"
1007 (equal? (read-line port) second-line))
1008 (pass-if "read-line increments line number"
1009 (= (port-line port) 2))
0b8faa0e
JB
1010 (pass-if "read-line returns EOF"
1011 (let loop ((i 0))
1012 (cond
1013 ((eof-object? (read-line port)) #t)
1014 ((> i 20) #f)
1015 (else (loop (+ i 1))))))
000ee07f 1016 (pass-if "line count is 5 at EOF"
73cb0a97
JB
1017 (= (port-line port) 5))
1018 (pass-if "column is correct at EOF"
1019 (= (port-column port) final-column))))
000ee07f
JB
1020 ports port-list-names)
1021 (for-each close-port ports)
1022 (delete-file port-loop-temp))))
1023
57e7f270
DH
1024 (with-test-prefix "newline"
1025 (test-line-counter
1026 (string-append "x\n"
1027 "He who receives an idea from me, receives instruction\n"
1028 "himself without lessening mine; as he who lights his\n"
1029 "taper at mine, receives light without darkening me.\n"
1030 " --- Thomas Jefferson\n")
1031 "He who receives an idea from me, receives instruction"
1032 0))
1033
1034 (with-test-prefix "no newline"
1035 (test-line-counter
1036 (string-append "x\n"
1037 "He who receives an idea from me, receives instruction\n"
1038 "himself without lessening mine; as he who lights his\n"
1039 "taper at mine, receives light without darkening me.\n"
1040 " --- Thomas Jefferson\n"
1041 "no newline here")
1042 "He who receives an idea from me, receives instruction"
1043 15)))
5bc1201f 1044
9a8be5a7
MV
1045;; Test port-line and port-column for output ports
1046
1047(define (test-output-line-counter text final-column)
1048 (with-test-prefix "port-line and port-column for output ports"
1049 (let ((port (open-output-string)))
1050 (pass-if "at beginning of input"
1051 (and (= (port-line port) 0)
1052 (= (port-column port) 0)))
1053 (write-char #\x port)
1054 (pass-if "after writing one character"
1055 (and (= (port-line port) 0)
1056 (= (port-column port) 1)))
1057 (write-char #\newline port)
1058 (pass-if "after writing first newline char"
1059 (and (= (port-line port) 1)
1060 (= (port-column port) 0)))
1061 (display text port)
1062 (pass-if "line count is 5 at end"
1063 (= (port-line port) 5))
1064 (pass-if "column is correct at end"
1065 (= (port-column port) final-column)))))
1066
1067(test-output-line-counter
1068 (string-append "He who receives an idea from me, receives instruction\n"
1069 "himself without lessening mine; as he who lights his\n"
1070 "taper at mine, receives light without darkening me.\n"
1071 " --- Thomas Jefferson\n"
1072 "no newline here")
1073 15)
1074
7424deab
KR
1075(with-test-prefix "port-column"
1076
1077 (with-test-prefix "output"
1078
1079 (pass-if "x"
1080 (let ((port (open-output-string)))
1081 (display "x" port)
1082 (= 1 (port-column port))))
1083
1084 (pass-if "\\a"
1085 (let ((port (open-output-string)))
1086 (display "\a" port)
1087 (= 0 (port-column port))))
1088
1089 (pass-if "x\\a"
1090 (let ((port (open-output-string)))
1091 (display "x\a" port)
1092 (= 1 (port-column port))))
1093
1094 (pass-if "\\x08 backspace"
1095 (let ((port (open-output-string)))
1096 (display "\x08" port)
1097 (= 0 (port-column port))))
1098
1099 (pass-if "x\\x08 backspace"
1100 (let ((port (open-output-string)))
1101 (display "x\x08" port)
1102 (= 0 (port-column port))))
1103
1104 (pass-if "\\n"
1105 (let ((port (open-output-string)))
1106 (display "\n" port)
1107 (= 0 (port-column port))))
1108
1109 (pass-if "x\\n"
1110 (let ((port (open-output-string)))
1111 (display "x\n" port)
1112 (= 0 (port-column port))))
1113
1114 (pass-if "\\r"
1115 (let ((port (open-output-string)))
1116 (display "\r" port)
1117 (= 0 (port-column port))))
1118
1119 (pass-if "x\\r"
1120 (let ((port (open-output-string)))
1121 (display "x\r" port)
1122 (= 0 (port-column port))))
1123
1124 (pass-if "\\t"
1125 (let ((port (open-output-string)))
1126 (display "\t" port)
1127 (= 8 (port-column port))))
1128
1129 (pass-if "x\\t"
1130 (let ((port (open-output-string)))
1131 (display "x\t" port)
1132 (= 8 (port-column port)))))
1133
1134 (with-test-prefix "input"
1135
1136 (pass-if "x"
1137 (let ((port (open-input-string "x")))
1138 (while (not (eof-object? (read-char port))))
1139 (= 1 (port-column port))))
1140
1141 (pass-if "\\a"
1142 (let ((port (open-input-string "\a")))
1143 (while (not (eof-object? (read-char port))))
1144 (= 0 (port-column port))))
1145
1146 (pass-if "x\\a"
1147 (let ((port (open-input-string "x\a")))
1148 (while (not (eof-object? (read-char port))))
1149 (= 1 (port-column port))))
1150
1151 (pass-if "\\x08 backspace"
1152 (let ((port (open-input-string "\x08")))
1153 (while (not (eof-object? (read-char port))))
1154 (= 0 (port-column port))))
1155
1156 (pass-if "x\\x08 backspace"
1157 (let ((port (open-input-string "x\x08")))
1158 (while (not (eof-object? (read-char port))))
1159 (= 0 (port-column port))))
1160
1161 (pass-if "\\n"
1162 (let ((port (open-input-string "\n")))
1163 (while (not (eof-object? (read-char port))))
1164 (= 0 (port-column port))))
1165
1166 (pass-if "x\\n"
1167 (let ((port (open-input-string "x\n")))
1168 (while (not (eof-object? (read-char port))))
1169 (= 0 (port-column port))))
1170
1171 (pass-if "\\r"
1172 (let ((port (open-input-string "\r")))
1173 (while (not (eof-object? (read-char port))))
1174 (= 0 (port-column port))))
1175
1176 (pass-if "x\\r"
1177 (let ((port (open-input-string "x\r")))
1178 (while (not (eof-object? (read-char port))))
1179 (= 0 (port-column port))))
1180
1181 (pass-if "\\t"
1182 (let ((port (open-input-string "\t")))
1183 (while (not (eof-object? (read-char port))))
1184 (= 8 (port-column port))))
1185
1186 (pass-if "x\\t"
1187 (let ((port (open-input-string "x\t")))
1188 (while (not (eof-object? (read-char port))))
1189 (= 8 (port-column port))))))
1190
004be623
KR
1191(with-test-prefix "port-line"
1192
1193 ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
1194 ;; scm_t_port actually holds a long; this restricted the range on 64-bit
1195 ;; systems
1196 (pass-if "set most-positive-fixnum/2"
1197 (let ((n (quotient most-positive-fixnum 2))
1198 (port (open-output-string)))
1199 (set-port-line! port n)
1200 (eqv? n (port-line port)))))
1201
064c27c4
LC
1202(with-test-prefix "port-encoding"
1203
1204 (pass-if-exception "set-port-encoding!, wrong encoding"
1205 exception:miscellaneous-error
cdd3d6c9
MW
1206 (let ((p (open-input-string "")))
1207 (set-port-encoding! p "does-not-exist")
1208 (read p)))
064c27c4
LC
1209
1210 (pass-if-exception "%default-port-encoding, wrong encoding"
1211 exception:miscellaneous-error
1212 (read (with-fluids ((%default-port-encoding "does-not-exist"))
1213 (open-input-string "")))))
1214
256f34e7
KR
1215;;;
1216;;; port-for-each
1217;;;
1218
1219(with-test-prefix "port-for-each"
1220
1221 ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
1222 ;; its iterator func if a port was inaccessible in the last gc mark but
1223 ;; the lazy sweeping has not yet reached it to remove it from the port
1224 ;; table (scm_i_port_table). Provoking those gc conditions is a little
1225 ;; tricky, but the following code made it happen in 1.8.2.
1226 (pass-if "passing freed cell"
256f34e7
KR
1227 (let ((lst '()))
1228 ;; clear out the heap
1229 (gc) (gc) (gc)
1230 ;; allocate cells so the opened ports aren't at the start of the heap
1231 (make-list 1000)
1232 (open-input-file "/dev/null")
1233 (make-list 1000)
1234 (open-input-file "/dev/null")
1235 ;; this gc leaves the above ports unmarked, ie. inaccessible
1236 (gc)
1237 ;; but they're still in the port table, so this sees them
1238 (port-for-each (lambda (port)
1239 (set! lst (cons port lst))))
1240 ;; this forces completion of the sweeping
1241 (gc) (gc) (gc)
1242 ;; and (if the bug is present) the cells accumulated in LST are now
1243 ;; freed cells, which give #f from `port?'
1244 (not (memq #f (map port? lst))))))
1245
e9966dbb
HWN
1246(with-test-prefix
1247 "fdes->port"
1248 (pass-if "fdes->ports finds port"
1249 (let ((port (open-file (test-file) "w")))
1250
1251 (not (not (memq port (fdes->ports (port->fdes port))))))))
1252
8ab3d8a0
KR
1253;;;
1254;;; seek
1255;;;
1256
1257(with-test-prefix "seek"
1258
1259 (with-test-prefix "file port"
1260
1261 (pass-if "SEEK_CUR"
1262 (call-with-output-file (test-file)
1263 (lambda (port)
1264 (display "abcde" port)))
1265 (let ((port (open-file (test-file) "r")))
1266 (read-char port)
1267 (seek port 2 SEEK_CUR)
1268 (eqv? #\d (read-char port))))
1269
1270 (pass-if "SEEK_SET"
1271 (call-with-output-file (test-file)
1272 (lambda (port)
1273 (display "abcde" port)))
1274 (let ((port (open-file (test-file) "r")))
1275 (read-char port)
1276 (seek port 3 SEEK_SET)
1277 (eqv? #\d (read-char port))))
1278
1279 (pass-if "SEEK_END"
1280 (call-with-output-file (test-file)
1281 (lambda (port)
1282 (display "abcde" port)))
1283 (let ((port (open-file (test-file) "r")))
1284 (read-char port)
1285 (seek port -2 SEEK_END)
1286 (eqv? #\d (read-char port))))))
1287
6e7d5622
KR
1288;;;
1289;;; truncate-file
1290;;;
1291
1292(with-test-prefix "truncate-file"
1293
8ab3d8a0
KR
1294 (pass-if-exception "flonum file" exception:wrong-type-arg
1295 (truncate-file 1.0 123))
1296
1297 (pass-if-exception "frac file" exception:wrong-type-arg
1298 (truncate-file 7/3 123))
1299
6e7d5622
KR
1300 (with-test-prefix "filename"
1301
8ab3d8a0
KR
1302 (pass-if-exception "flonum length" exception:wrong-type-arg
1303 (call-with-output-file (test-file)
1304 (lambda (port)
1305 (display "hello" port)))
1306 (truncate-file (test-file) 1.0))
1307
6e7d5622
KR
1308 (pass-if "shorten"
1309 (call-with-output-file (test-file)
1310 (lambda (port)
1311 (display "hello" port)))
1312 (truncate-file (test-file) 1)
8ab3d8a0
KR
1313 (eqv? 1 (stat:size (stat (test-file)))))
1314
1315 (pass-if-exception "shorten to current pos" exception:miscellaneous-error
1316 (call-with-output-file (test-file)
1317 (lambda (port)
1318 (display "hello" port)))
1319 (truncate-file (test-file))))
6e7d5622
KR
1320
1321 (with-test-prefix "file descriptor"
1322
1323 (pass-if "shorten"
1324 (call-with-output-file (test-file)
1325 (lambda (port)
1326 (display "hello" port)))
1327 (let ((fd (open-fdes (test-file) O_RDWR)))
1328 (truncate-file fd 1)
1329 (close-fdes fd))
8ab3d8a0
KR
1330 (eqv? 1 (stat:size (stat (test-file)))))
1331
1332 (pass-if "shorten to current pos"
1333 (call-with-output-file (test-file)
1334 (lambda (port)
1335 (display "hello" port)))
1336 (let ((fd (open-fdes (test-file) O_RDWR)))
1337 (seek fd 1 SEEK_SET)
1338 (truncate-file fd)
1339 (close-fdes fd))
6e7d5622
KR
1340 (eqv? 1 (stat:size (stat (test-file))))))
1341
1342 (with-test-prefix "file port"
1343
1344 (pass-if "shorten"
1345 (call-with-output-file (test-file)
1346 (lambda (port)
1347 (display "hello" port)))
1348 (let ((port (open-file (test-file) "r+")))
1349 (truncate-file port 1))
8ab3d8a0
KR
1350 (eqv? 1 (stat:size (stat (test-file)))))
1351
1352 (pass-if "shorten to current pos"
1353 (call-with-output-file (test-file)
1354 (lambda (port)
1355 (display "hello" port)))
1356 (let ((port (open-file (test-file) "r+")))
1357 (read-char port)
1358 (truncate-file port))
6e7d5622
KR
1359 (eqv? 1 (stat:size (stat (test-file)))))))
1360
7424deab 1361
5bc1201f
JB
1362;;;; testing read-delimited and friends
1363
1364(with-test-prefix "read-delimited!"
1365 (let ((c (make-string 20 #\!)))
1366 (call-with-input-string
1367 "defdef\nghighi\n"
1368 (lambda (port)
1369
1370 (read-delimited! "\n" c port 'concat)
1371 (pass-if "read-delimited! reads a first line"
1372 (string=? c "defdef\n!!!!!!!!!!!!!"))
1373
1374 (read-delimited! "\n" c port 'concat 3)
1375 (pass-if "read-delimited! reads a first line"
1376 (string=? c "defghighi\n!!!!!!!!!!"))))))
1b054952
JB
1377
1378\f
1379;;;; char-ready?
1380
1381(call-with-input-string
1382 "howdy"
1383 (lambda (port)
1384 (pass-if "char-ready? returns true on string port"
1385 (char-ready? port))))
1386
1387;;; This segfaults on some versions of Guile. We really should run
1388;;; the tests in a subprocess...
1389
1390(call-with-input-string
1391 "howdy"
1392 (lambda (port)
1393 (with-input-from-port
1394 port
1395 (lambda ()
1396 (pass-if "char-ready? returns true on string port as default port"
1397 (char-ready?))))))
fe5b6beb
JB
1398
1399\f
45c0878b
MW
1400;;;; pending-eof behavior
1401
1402(with-test-prefix "pending EOF behavior"
1403 ;; Make a test port that will produce the given sequence. Each
1404 ;; element of 'lst' may be either a character or #f (which means EOF).
1405 (define (test-soft-port . lst)
1406 (make-soft-port
1407 (vector (lambda (c) #f) ; write char
1408 (lambda (s) #f) ; write string
1409 (lambda () #f) ; flush
1410 (lambda () ; read char
1411 (let ((c (car lst)))
1412 (set! lst (cdr lst))
1413 c))
1414 (lambda () #f)) ; close
1415 "rw"))
1416
1417 (define (call-with-port p proc)
1418 (dynamic-wind
1419 (lambda () #f)
1420 (lambda () (proc p))
1421 (lambda () (close-port p))))
1422
1423 (define (call-with-test-file str proc)
1424 (let ((filename (test-file)))
1425 (dynamic-wind
1426 (lambda () (call-with-output-file filename
1427 (lambda (p) (display str p))))
1428 (lambda () (call-with-input-file filename proc))
1429 (lambda () (delete-file (test-file))))))
1430
1431 (pass-if "peek-char does not swallow EOF (soft port)"
1432 (call-with-port (test-soft-port #\a #f #\b)
1433 (lambda (p)
1434 (and (char=? #\a (peek-char p))
1435 (char=? #\a (read-char p))
1436 (eof-object? (peek-char p))
1437 (eof-object? (read-char p))
1438 (char=? #\b (peek-char p))
1439 (char=? #\b (read-char p))))))
1440
1441 (pass-if "unread clears pending EOF (soft port)"
1442 (call-with-port (test-soft-port #\a #f #\b)
1443 (lambda (p)
1444 (and (char=? #\a (read-char p))
1445 (eof-object? (peek-char p))
1446 (begin (unread-char #\u p)
1447 (char=? #\u (read-char p)))))))
1448
1449 (pass-if "unread clears pending EOF (string port)"
1450 (call-with-input-string "a"
1451 (lambda (p)
1452 (and (char=? #\a (read-char p))
1453 (eof-object? (peek-char p))
1454 (begin (unread-char #\u p)
1455 (char=? #\u (read-char p)))))))
1456
1457 (pass-if "unread clears pending EOF (file port)"
1458 (call-with-test-file
1459 "a"
1460 (lambda (p)
1461 (and (char=? #\a (read-char p))
1462 (eof-object? (peek-char p))
1463 (begin (unread-char #\u p)
1464 (char=? #\u (read-char p)))))))
1465
1466 (pass-if "seek clears pending EOF (string port)"
1467 (call-with-input-string "a"
1468 (lambda (p)
1469 (and (char=? #\a (read-char p))
1470 (eof-object? (peek-char p))
1471 (begin (seek p 0 SEEK_SET)
1472 (char=? #\a (read-char p)))))))
1473
1474 (pass-if "seek clears pending EOF (file port)"
1475 (call-with-test-file
1476 "a"
1477 (lambda (p)
1478 (and (char=? #\a (read-char p))
1479 (eof-object? (peek-char p))
1480 (begin (seek p 0 SEEK_SET)
1481 (char=? #\a (read-char p))))))))
1482
1483\f
fe5b6beb
JB
1484;;;; Close current-input-port, and make sure everyone can handle it.
1485
1486(with-test-prefix "closing current-input-port"
1487 (for-each (lambda (procedure name)
1488 (with-input-from-port
1489 (call-with-input-string "foo" (lambda (p) p))
1490 (lambda ()
1491 (close-port (current-input-port))
6b4113af
DH
1492 (pass-if-exception name
1493 exception:wrong-type-arg
1494 (procedure)))))
fe5b6beb
JB
1495 (list read read-char read-line)
1496 '("read" "read-char" "read-line")))
c56c0f79 1497
e8b21eec
LC
1498\f
1499
1500(with-test-prefix "setvbuf"
1501
1502 (pass-if "line/column number preserved"
1503 ;; In Guile 2.0.5, `setvbuf' would erroneously decrease the port's
1504 ;; line and/or column number.
1505 (call-with-output-file (test-file)
1506 (lambda (p)
1507 (display "This is GNU Guile.\nWelcome." p)))
1508 (call-with-input-file (test-file)
1509 (lambda (p)
764246cf 1510 (and (eqv? #\T (read-char p))
e8b21eec
LC
1511 (let ((line (port-line p))
1512 (col (port-column p)))
1513 (and (= line 0) (= col 1)
1514 (begin
1515 (setvbuf p _IOFBF 777)
1516 (let ((line* (port-line p))
1517 (col* (port-column p)))
1518 (and (= line line*)
1519 (= col col*)))))))))))
1520
2ae7b7b6
LC
1521\f
1522
7f6c3f8f
MW
1523(pass-if-equal "unget-bytevector"
1524 #vu8(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 200 201 202 203
1525 1 2 3 4 251 253 254 255)
1526 (let ((port (open-bytevector-input-port #vu8(1 2 3 4 251 253 254 255))))
1527 (unget-bytevector port #vu8(200 201 202 203))
1528 (unget-bytevector port #vu8(20 21 22 23 24))
1529 (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 4)
1530 (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 2 2)
1531 (unget-bytevector port #vu8(10 11))
1532 (get-bytevector-all port)))
1533
1534\f
1535
cdd3d6c9
MW
1536(with-test-prefix "unicode byte-order marks (BOMs)"
1537
1538 (define (bv-read-test* encoding bv proc)
1539 (let ((port (open-bytevector-input-port bv)))
1540 (set-port-encoding! port encoding)
1541 (proc port)))
1542
1543 (define (bv-read-test encoding bv)
1544 (bv-read-test* encoding bv read-string))
1545
1546 (define (bv-write-test* encoding proc)
1547 (call-with-values
1548 (lambda () (open-bytevector-output-port))
1549 (lambda (port get-bytevector)
1550 (set-port-encoding! port encoding)
1551 (proc port)
1552 (get-bytevector))))
1553
1554 (define (bv-write-test encoding str)
1555 (bv-write-test* encoding
1556 (lambda (p)
1557 (display str p))))
1558
1559 (pass-if-equal "BOM not discarded from Latin-1 stream"
1560 "\xEF\xBB\xBF\x61"
1561 (bv-read-test "ISO-8859-1" #vu8(#xEF #xBB #xBF #x61)))
1562
1563 (pass-if-equal "BOM not discarded from Latin-2 stream"
1564 "\u010F\u0165\u017C\x61"
1565 (bv-read-test "ISO-8859-2" #vu8(#xEF #xBB #xBF #x61)))
1566
1567 (pass-if-equal "BOM not discarded from UTF-16BE stream"
1568 "\uFEFF\x61"
1569 (bv-read-test "UTF-16BE" #vu8(#xFE #xFF #x00 #x61)))
1570
1571 (pass-if-equal "BOM not discarded from UTF-16LE stream"
1572 "\uFEFF\x61"
1573 (bv-read-test "UTF-16LE" #vu8(#xFF #xFE #x61 #x00)))
1574
1575 (pass-if-equal "BOM not discarded from UTF-32BE stream"
1576 "\uFEFF\x61"
1577 (bv-read-test "UTF-32BE" #vu8(#x00 #x00 #xFE #xFF
1578 #x00 #x00 #x00 #x61)))
1579
1580 (pass-if-equal "BOM not discarded from UTF-32LE stream"
1581 "\uFEFF\x61"
1582 (bv-read-test "UTF-32LE" #vu8(#xFF #xFE #x00 #x00
1583 #x61 #x00 #x00 #x00)))
1584
1585 (pass-if-equal "BOM not written to UTF-8 stream"
1586 #vu8(#x61)
1587 (bv-write-test "UTF-8" "a"))
1588
1589 (pass-if-equal "BOM not written to UTF-16BE stream"
1590 #vu8(#x00 #x61)
1591 (bv-write-test "UTF-16BE" "a"))
1592
1593 (pass-if-equal "BOM not written to UTF-16LE stream"
1594 #vu8(#x61 #x00)
1595 (bv-write-test "UTF-16LE" "a"))
1596
1597 (pass-if-equal "BOM not written to UTF-32BE stream"
1598 #vu8(#x00 #x00 #x00 #x61)
1599 (bv-write-test "UTF-32BE" "a"))
1600
1601 (pass-if-equal "BOM not written to UTF-32LE stream"
1602 #vu8(#x61 #x00 #x00 #x00)
1603 (bv-write-test "UTF-32LE" "a"))
1604
1605 (pass-if "Don't read from the port unless user asks to"
1606 (let* ((p (make-soft-port
1607 (vector
1608 (lambda (c) #f) ; write char
1609 (lambda (s) #f) ; write string
1610 (lambda () #f) ; flush
1611 (lambda () (throw 'fail)) ; read char
1612 (lambda () #f))
1613 "rw")))
1614 (set-port-encoding! p "UTF-16")
1615 (display "abc" p)
1616 (set-port-encoding! p "UTF-32")
1617 (display "def" p)
1618 #t))
1619
1620 ;; TODO: test that input and output streams are independent when
1621 ;; appropriate, and linked when appropriate.
1622
1623 (pass-if-equal "BOM discarded from start of UTF-8 stream"
1624 "a"
1625 (bv-read-test "Utf-8" #vu8(#xEF #xBB #xBF #x61)))
1626
1627 (pass-if-equal "BOM discarded from start of UTF-8 stream after seek to 0"
1628 '(#\a "a")
1629 (bv-read-test* "uTf-8" #vu8(#xEF #xBB #xBF #x61)
1630 (lambda (p)
1631 (let ((c (read-char p)))
1632 (seek p 0 SEEK_SET)
1633 (let ((s (read-string p)))
1634 (list c s))))))
1635
1636 (pass-if-equal "Only one BOM discarded from start of UTF-8 stream"
1637 "\uFEFFa"
1638 (bv-read-test "UTF-8" #vu8(#xEF #xBB #xBF #xEF #xBB #xBF #x61)))
1639
1640 (pass-if-equal "BOM not discarded from UTF-8 stream after seek to > 0"
1641 "\uFEFFb"
1642 (bv-read-test* "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)
1643 (lambda (p)
1644 (seek p 1 SEEK_SET)
1645 (read-string p))))
1646
1647 (pass-if-equal "BOM not discarded unless at start of UTF-8 stream"
1648 "a\uFEFFb"
1649 (bv-read-test "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)))
1650
1651 (pass-if-equal "BOM (BE) written to start of UTF-16 stream"
1652 #vu8(#xFE #xFF #x00 #x61 #x00 #x62)
1653 (bv-write-test "UTF-16" "ab"))
1654
1655 (pass-if-equal "BOM (BE) written to UTF-16 stream after set-port-encoding!"
1656 #vu8(#xFE #xFF #x00 #x61 #x00 #x62 #xFE #xFF #x00 #x63 #x00 #x64)
1657 (bv-write-test* "UTF-16"
1658 (lambda (p)
1659 (display "ab" p)
1660 (set-port-encoding! p "UTF-16")
1661 (display "cd" p))))
1662
1663 (pass-if-equal "BOM discarded from start of UTF-16 stream (BE)"
1664 "a"
1665 (bv-read-test "UTF-16" #vu8(#xFE #xFF #x00 #x61)))
1666
1667 (pass-if-equal "BOM discarded from start of UTF-16 stream (BE) after seek to 0"
1668 '(#\a "a")
1669 (bv-read-test* "utf-16" #vu8(#xFE #xFF #x00 #x61)
1670 (lambda (p)
1671 (let ((c (read-char p)))
1672 (seek p 0 SEEK_SET)
1673 (let ((s (read-string p)))
1674 (list c s))))))
1675
1676 (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (BE)"
1677 "\uFEFFa"
1678 (bv-read-test "Utf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)))
1679
1680 (pass-if-equal "BOM not discarded from UTF-16 stream (BE) after seek to > 0"
1681 "\uFEFFa"
1682 (bv-read-test* "uTf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)
1683 (lambda (p)
1684 (seek p 2 SEEK_SET)
1685 (read-string p))))
1686
1687 (pass-if-equal "BOM not discarded unless at start of UTF-16 stream"
1688 "a\uFEFFb"
3f315b64 1689 (bv-read-test "utf-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)))
cdd3d6c9
MW
1690
1691 (pass-if-equal "BOM discarded from start of UTF-16 stream (LE)"
1692 "a"
1693 (bv-read-test "UTF-16" #vu8(#xFF #xFE #x61 #x00)))
1694
1695 (pass-if-equal "BOM discarded from start of UTF-16 stream (LE) after seek to 0"
1696 '(#\a "a")
1697 (bv-read-test* "Utf-16" #vu8(#xFF #xFE #x61 #x00)
1698 (lambda (p)
1699 (let ((c (read-char p)))
1700 (seek p 0 SEEK_SET)
1701 (let ((s (read-string p)))
1702 (list c s))))))
1703
1704 (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (LE)"
1705 "\uFEFFa"
1706 (bv-read-test "UTf-16" #vu8(#xFF #xFE #xFF #xFE #x61 #x00)))
1707
1708 (pass-if-equal "BOM discarded from start of UTF-32 stream (BE)"
1709 "a"
1710 (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
1711 #x00 #x00 #x00 #x61)))
1712
1713 (pass-if-equal "BOM discarded from start of UTF-32 stream (BE) after seek to 0"
1714 '(#\a "a")
1715 (bv-read-test* "utF-32" #vu8(#x00 #x00 #xFE #xFF
1716 #x00 #x00 #x00 #x61)
1717 (lambda (p)
1718 (let ((c (read-char p)))
1719 (seek p 0 SEEK_SET)
1720 (let ((s (read-string p)))
1721 (list c s))))))
1722
1723 (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (BE)"
1724 "\uFEFFa"
1725 (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
1726 #x00 #x00 #xFE #xFF
1727 #x00 #x00 #x00 #x61)))
1728
1729 (pass-if-equal "BOM not discarded from UTF-32 stream (BE) after seek to > 0"
1730 "\uFEFFa"
1731 (bv-read-test* "UtF-32" #vu8(#x00 #x00 #xFE #xFF
1732 #x00 #x00 #xFE #xFF
1733 #x00 #x00 #x00 #x61)
1734 (lambda (p)
1735 (seek p 4 SEEK_SET)
1736 (read-string p))))
1737
1738 (pass-if-equal "BOM discarded within UTF-16 stream (BE) after set-port-encoding!"
1739 "ab"
1740 (bv-read-test* "UTF-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)
1741 (lambda (p)
1742 (let ((a (read-char p)))
1743 (set-port-encoding! p "UTF-16")
1744 (string a (read-char p))))))
1745
1746 (pass-if-equal "BOM discarded within UTF-16 stream (LE,BE) after set-port-encoding!"
1747 "ab"
1748 (bv-read-test* "utf-16" #vu8(#x00 #x61 #xFF #xFE #x62 #x00)
1749 (lambda (p)
1750 (let ((a (read-char p)))
1751 (set-port-encoding! p "UTF-16")
1752 (string a (read-char p))))))
1753
1754 (pass-if-equal "BOM discarded within UTF-32 stream (BE) after set-port-encoding!"
1755 "ab"
1756 (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
1757 #x00 #x00 #xFE #xFF
1758 #x00 #x00 #x00 #x62)
1759 (lambda (p)
1760 (let ((a (read-char p)))
1761 (set-port-encoding! p "UTF-32")
1762 (string a (read-char p))))))
1763
1764 (pass-if-equal "BOM discarded within UTF-32 stream (LE,BE) after set-port-encoding!"
1765 "ab"
1766 (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
1767 #xFF #xFE #x00 #x00
1768 #x62 #x00 #x00 #x00)
1769 (lambda (p)
1770 (let ((a (read-char p)))
1771 (set-port-encoding! p "UTF-32")
1772 (string a (read-char p))))))
1773
1774 (pass-if-equal "BOM not discarded unless at start of UTF-32 stream"
1775 "a\uFEFFb"
3f315b64 1776 (bv-read-test "UTF-32" #vu8(#x00 #x00 #x00 #x61
f6f4feb0
MW
1777 #x00 #x00 #xFE #xFF
1778 #x00 #x00 #x00 #x62)))
cdd3d6c9
MW
1779
1780 (pass-if-equal "BOM discarded from start of UTF-32 stream (LE)"
1781 "a"
1782 (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
1783 #x61 #x00 #x00 #x00)))
1784
1785 (pass-if-equal "BOM discarded from start of UTF-32 stream (LE) after seek to 0"
1786 '(#\a "a")
1787 (bv-read-test* "UTf-32" #vu8(#xFF #xFE #x00 #x00
1788 #x61 #x00 #x00 #x00)
1789 (lambda (p)
1790 (let ((c (read-char p)))
1791 (seek p 0 SEEK_SET)
1792 (let ((s (read-string p)))
1793 (list c s))))))
1794
1795 (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (LE)"
1796 "\uFEFFa"
1797 (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
1798 #xFF #xFE #x00 #x00
1799 #x61 #x00 #x00 #x00))))
1800
1801\f
1802
2ae7b7b6
LC
1803(define-syntax-rule (with-load-path path body ...)
1804 (let ((new path)
1805 (old %load-path))
1806 (dynamic-wind
1807 (lambda ()
1808 (set! %load-path new))
1809 (lambda ()
1810 body ...)
1811 (lambda ()
1812 (set! %load-path old)))))
1813
1814(with-test-prefix "%file-port-name-canonicalization"
1815
2a7d614c 1816 (pass-if-equal "absolute file name & empty %load-path entry" "/dev/null"
2ae7b7b6
LC
1817 ;; In Guile 2.0.5 and earlier, this would return "dev/null" instead
1818 ;; of "/dev/null". See
1819 ;; <http://lists.gnu.org/archive/html/guile-devel/2012-05/msg00059.html>
1820 ;; for a discussion.
2a7d614c
LC
1821 (with-load-path (cons "" (delete "/" %load-path))
1822 (with-fluids ((%file-port-name-canonicalization 'relative))
1823 (port-filename (open-input-file "/dev/null")))))
1824
1825 (pass-if-equal "relative canonicalization with /" "dev/null"
1826 (with-load-path (cons "/" %load-path)
1827 (with-fluids ((%file-port-name-canonicalization 'relative))
1828 (port-filename (open-input-file "/dev/null")))))
1829
1830 (pass-if-equal "relative canonicalization from ice-9" "ice-9/q.scm"
1831 ;; If an entry in %LOAD-PATH is not canonical, then
1832 ;; `scm_i_relativize_path' is unable to do its job.
1833 (if (equal? (map canonicalize-path %load-path) %load-path)
1834 (with-fluids ((%file-port-name-canonicalization 'relative))
1835 (port-filename
1836 (open-input-file (%search-load-path "ice-9/q.scm"))))
1837 (throw 'unresolved)))
1838
1839 (pass-if-equal "absolute canonicalization from ice-9"
1840 (canonicalize-path
1841 (string-append (assoc-ref %guile-build-info 'top_srcdir)
1842 "/module/ice-9/q.scm"))
1843 (with-fluids ((%file-port-name-canonicalization 'absolute))
1844 (port-filename (open-input-file (%search-load-path "ice-9/q.scm"))))))
2ae7b7b6 1845
c56c0f79 1846(delete-file (test-file))
9a201881
LC
1847
1848;;; Local Variables:
1849;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
2ae7b7b6 1850;;; eval: (put 'with-load-path 'scheme-indent-function 1)
9a201881 1851;;; End: