1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
|
;;;; ports.test --- Guile I/O ports. -*- coding: utf-8; mode: scheme; -*-
;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
;;;;
;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010,
;;;; 2011 Free Software Foundation, Inc.
;;;;
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
;;;; License as published by the Free Software Foundation; either
;;;; version 3 of the License, or (at your option) any later version.
;;;;
;;;; This library is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;;;; Lesser General Public License for more details.
;;;;
;;;; You should have received a copy of the GNU Lesser General Public
;;;; License along with this library; if not, write to the Free Software
;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
(define-module (test-suite test-ports)
#:use-module (test-suite lib)
#:use-module (test-suite guile-test)
#:use-module (ice-9 popen)
#:use-module (ice-9 rdelim)
#:use-module (rnrs bytevectors)
#:use-module ((rnrs io ports) #:select (open-bytevector-input-port)))
(define (display-line . args)
(for-each display args)
(newline))
(define (test-file)
(data-file-name "ports-test.tmp"))
;;;; Some general utilities for testing ports.
;; Make sure we are set up for 8-bit Latin-1 data.
(fluid-set! %default-port-encoding "ISO-8859-1")
(for-each (lambda (p)
(set-port-encoding! p (fluid-ref %default-port-encoding)))
(list (current-input-port) (current-output-port)
(current-error-port)))
;;; Read from PORT until EOF, and return the result as a string.
(define (read-all port)
(let loop ((chars '()))
(let ((char (read-char port)))
(if (eof-object? char)
(list->string (reverse! chars))
(loop (cons char chars))))))
(define (read-file filename)
(let* ((port (open-input-file filename))
(string (read-all port)))
(close-port port)
string))
;;;; Normal file ports.
;;; Write out an s-expression, and read it back.
(let ((string '("From fairest creatures we desire increase,"
"That thereby beauty's rose might never die,"))
(filename (test-file)))
(let ((port (open-output-file filename)))
(write string port)
(close-port port))
(let ((port (open-input-file filename)))
(let ((in-string (read port)))
(pass-if "file: write and read back list of strings"
(equal? string in-string)))
(close-port port))
(delete-file filename))
;;; Write out a string, and read it back a character at a time.
(let ((string "This is a test string\nwith no newline at the end")
(filename (test-file)))
(let ((port (open-output-file filename)))
(display string port)
(close-port port))
(let ((in-string (read-file filename)))
(pass-if "file: write and read back characters"
(equal? string in-string)))
(delete-file filename))
;;; Buffered input/output port with seeking.
(let* ((filename (test-file))
(port (open-file filename "w+")))
(display "J'Accuse" port)
(seek port -1 SEEK_CUR)
(pass-if "file: r/w 1"
(char=? (read-char port) #\e))
(pass-if "file: r/w 2"
(eof-object? (read-char port)))
(seek port -1 SEEK_CUR)
(write-char #\x port)
(seek port 7 SEEK_SET)
(pass-if "file: r/w 3"
(char=? (read-char port) #\x))
(seek port -2 SEEK_END)
(pass-if "file: r/w 4"
(char=? (read-char port) #\s))
(close-port port)
(delete-file filename))
;;; Unbuffered input/output port with seeking.
(let* ((filename (test-file))
(port (open-file filename "w+0")))
(display "J'Accuse" port)
(seek port -1 SEEK_CUR)
(pass-if "file: ub r/w 1"
(char=? (read-char port) #\e))
(pass-if "file: ub r/w 2"
(eof-object? (read-char port)))
(seek port -1 SEEK_CUR)
(write-char #\x port)
(seek port 7 SEEK_SET)
(pass-if "file: ub r/w 3"
(char=? (read-char port) #\x))
(seek port -2 SEEK_END)
(pass-if "file: ub r/w 4"
(char=? (read-char port) #\s))
(close-port port)
(delete-file filename))
;;; Buffered output-only and input-only ports with seeking.
(let* ((filename (test-file))
(port (open-output-file filename)))
(display "J'Accuse" port)
(pass-if "file: out tell"
(= (seek port 0 SEEK_CUR) 8))
(seek port -1 SEEK_CUR)
(write-char #\x port)
(close-port port)
(let ((iport (open-input-file filename)))
(pass-if "file: in tell 0"
(= (seek iport 0 SEEK_CUR) 0))
(read-char iport)
(pass-if "file: in tell 1"
(= (seek iport 0 SEEK_CUR) 1))
(unread-char #\z iport)
(pass-if "file: in tell 0 after unread"
(= (seek iport 0 SEEK_CUR) 0))
(pass-if "file: unread char still there"
(char=? (read-char iport) #\z))
(seek iport 7 SEEK_SET)
(pass-if "file: in last char"
(char=? (read-char iport) #\x))
(close-port iport))
(delete-file filename))
;;; unusual characters.
(let* ((filename (test-file))
(port (open-output-file filename)))
(display (string #\nul (integer->char 255) (integer->char 128)
#\nul) port)
(close-port port)
(let* ((port (open-input-file filename))
(line (read-line port)))
(pass-if "file: read back NUL 1"
(char=? (string-ref line 0) #\nul))
(pass-if "file: read back 255"
(char=? (string-ref line 1) (integer->char 255)))
(pass-if "file: read back 128"
(char=? (string-ref line 2) (integer->char 128)))
(pass-if "file: read back NUL 2"
(char=? (string-ref line 3) #\nul))
(pass-if "file: EOF"
(eof-object? (read-char port)))
(close-port port))
(delete-file filename))
;;; line buffering mode.
(let* ((filename (test-file))
(port (open-file filename "wl"))
(test-string "one line more or less"))
(write-line test-string port)
(let* ((in-port (open-input-file filename))
(line (read-line in-port)))
(close-port in-port)
(close-port port)
(pass-if "file: line buffering"
(string=? line test-string)))
(delete-file filename))
;;; read-line should use the port encoding (not the locale encoding).
(let ((str "ĉu bone?"))
(with-locale "C"
(let* ((filename (test-file))
(port (open-file filename "wl")))
(set-port-encoding! port "UTF-8")
(write-line str port)
(let ((in-port (open-input-file filename)))
(set-port-encoding! in-port "UTF-8")
(let ((line (read-line in-port)))
(close-port in-port)
(close-port port)
(pass-if "file: read-line honors port encoding"
(string=? line str))))
(delete-file filename))))
;;; binary mode ignores port encoding
(pass-if "file: binary mode ignores port encoding"
(with-fluids ((%default-port-encoding "UTF-8"))
(let* ((filename (test-file))
(port (open-file filename "w"))
(test-string "一二三")
(binary-test-string
(apply string
(map integer->char
(uniform-vector->list
(string->utf8 test-string))))))
(write-line test-string port)
(close-port port)
(let* ((in-port (open-file filename "rb"))
(line (read-line in-port)))
(close-port in-port)
(delete-file filename)
(string=? line binary-test-string)))))
;;; binary mode ignores file coding declaration
(pass-if "file: binary mode ignores file coding declaration"
(with-fluids ((%default-port-encoding "UTF-8"))
(let* ((filename (test-file))
(port (open-file filename "w"))
(test-string "一二三")
(binary-test-string
(apply string
(map integer->char
(uniform-vector->list
(string->utf8 test-string))))))
(write-line ";; coding: utf-8" port)
(write-line test-string port)
(close-port port)
(let* ((in-port (open-file filename "rb"))
(line1 (read-line in-port))
(line2 (read-line in-port)))
(close-port in-port)
(delete-file filename)
(string=? line2 binary-test-string)))))
;; open-file honors file coding declarations
(pass-if "file: open-file honors coding declarations"
(with-fluids ((%default-port-encoding "UTF-8"))
(let* ((filename (test-file))
(port (open-output-file filename))
(test-string "€100"))
(set-port-encoding! port "ISO-8859-15")
(write-line ";; coding: iso-8859-15" port)
(write-line test-string port)
(close-port port)
(let* ((in-port (open-input-file filename))
(line1 (read-line in-port))
(line2 (read-line in-port)))
(close-port in-port)
(delete-file filename)
(string=? line2 test-string)))))
;;; ungetting characters and strings.
(with-input-from-string "walk on the moon\nmoon"
(lambda ()
(read-char)
(unread-char #\a (current-input-port))
(pass-if "unread-char"
(char=? (read-char) #\a))
(read-line)
(let ((replacenoid "chicken enchilada"))
(unread-char #\newline (current-input-port))
(unread-string replacenoid (current-input-port))
(pass-if "unread-string"
(string=? (read-line) replacenoid)))
(pass-if "unread residue"
(string=? (read-line) "moon"))))
;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
;;; the reading end. try to read a byte: should get EAGAIN or
;;; EWOULDBLOCK error.
(let* ((p (pipe))
(r (car p)))
(fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
(pass-if "non-blocking-I/O"
(catch 'system-error
(lambda () (read-char r) #f)
(lambda (key . args)
(and (eq? key 'system-error)
(let ((errno (car (list-ref args 3))))
(or (= errno EAGAIN)
(= errno EWOULDBLOCK))))))))
;;;; Pipe (popen) ports.
;;; Run a command, and read its output.
(let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
(in-string (read-all pipe)))
(close-pipe pipe)
(pass-if "pipe: read"
(equal? in-string "Howdy there, partner!\n")))
;;; Run a command, send some output to it, and see if it worked.
(let* ((filename (test-file))
(pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
(display "Now Jimmy lives on a mushroom cloud\n" pipe)
(display "Mommy, why does everybody have a bomb?\n" pipe)
(close-pipe pipe)
(let ((in-string (read-file filename)))
(pass-if "pipe: write"
(equal? in-string "Mommy, why does everybody have a bomb?\n")))
(delete-file filename))
;;;; Void ports. These are so trivial we don't test them.
;;;; String ports.
(with-test-prefix "string ports"
;; Write text to a string port.
(let* ((string "Howdy there, partner!")
(in-string (call-with-output-string
(lambda (port)
(display string port)
(newline port)))))
(pass-if "display text"
(equal? in-string (string-append string "\n"))))
;; Write an s-expression to a string port.
(let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
(in-sexpr
(call-with-input-string (call-with-output-string
(lambda (port)
(write sexpr port)))
read)))
(pass-if "write/read sexpr"
(equal? in-sexpr sexpr)))
;; seeking and unreading from an input string.
(let ((text "that text didn't look random to me"))
(call-with-input-string text
(lambda (p)
(pass-if "input tell 0"
(= (seek p 0 SEEK_CUR) 0))
(read-char p)
(pass-if "input tell 1"
(= (seek p 0 SEEK_CUR) 1))
(unread-char #\x p)
(pass-if "input tell back to 0"
(= (seek p 0 SEEK_CUR) 0))
(pass-if "input ungetted char"
(char=? (read-char p) #\x))
(seek p 0 SEEK_END)
(pass-if "input seek to end"
(= (seek p 0 SEEK_CUR)
(string-length text)))
(unread-char #\x p)
(pass-if "input seek to beginning"
(= (seek p 0 SEEK_SET) 0))
(pass-if "input reread first char"
(char=? (read-char p)
(string-ref text 0))))))
;; seeking an output string.
(let* ((text (string-copy "123456789"))
(len (string-length text))
(result (call-with-output-string
(lambda (p)
(pass-if "output tell 0"
(= (seek p 0 SEEK_CUR) 0))
(display text p)
(pass-if "output tell end"
(= (seek p 0 SEEK_CUR) len))
(pass-if "output seek to beginning"
(= (seek p 0 SEEK_SET) 0))
(write-char #\a p)
(seek p -1 SEEK_END)
(pass-if "output seek to last char"
(= (seek p 0 SEEK_CUR)
(- len 1)))
(write-char #\b p)))))
(string-set! text 0 #\a)
(string-set! text (- len 1) #\b)
(pass-if "output check"
(string=? text result)))
(pass-if "%default-port-encoding is honored"
(let ((encodings '("UTF-8" "UTF-16" "ISO-8859-1" "ISO-8859-3")))
(equal? (map (lambda (e)
(with-fluids ((%default-port-encoding e))
(call-with-output-string
(lambda (p)
(and (string=? e (port-encoding p))
(display (port-encoding p) p))))))
encodings)
encodings)))
(pass-if "suitable encoding [latin-1]"
(let ((str "hello, world"))
(with-fluids ((%default-port-encoding "ISO-8859-1"))
(equal? str
(with-output-to-string
(lambda ()
(display str)))))))
(pass-if "suitable encoding [latin-3]"
(let ((str "ĉu bone?"))
(with-fluids ((%default-port-encoding "ISO-8859-3"))
(equal? str
(with-output-to-string
(lambda ()
(display str)))))))
(pass-if "wrong encoding"
(let ((str "ĉu bone?"))
(catch 'encoding-error
(lambda ()
;; Latin-1 cannot represent ‘ĉ’.
(with-fluids ((%default-port-encoding "ISO-8859-1"))
(with-output-to-string
(lambda ()
(display str)))))
(lambda (key subr message errno port chr)
(and (eq? chr #\ĉ)
(string? (strerror errno)))))))
(pass-if "wrong encoding, substitute"
(let ((str "ĉu bone?"))
(with-fluids ((%default-port-encoding "ISO-8859-1"))
(string=? (with-output-to-string
(lambda ()
(set-port-conversion-strategy! (current-output-port)
'substitute)
(display str)))
"?u bone?"))))
(pass-if "wrong encoding, escape"
(let ((str "ĉu bone?"))
(with-fluids ((%default-port-encoding "ISO-8859-1"))
(string=? (with-output-to-string
(lambda ()
(set-port-conversion-strategy! (current-output-port)
'escape)
(display str)))
"\\u0109u bone?"))))
(pass-if "peek-char [latin-1]"
(let ((p (with-fluids ((%default-port-encoding #f))
(open-input-string "hello, world"))))
(and (char=? (peek-char p) #\h)
(char=? (peek-char p) #\h)
(char=? (peek-char p) #\h)
(= (port-line p) 0)
(= (port-column p) 0))))
(pass-if "peek-char [utf-8]"
(let ((p (with-fluids ((%default-port-encoding "UTF-8"))
(open-input-string "안녕하세요"))))
(and (char=? (peek-char p) #\안)
(char=? (peek-char p) #\안)
(char=? (peek-char p) #\안)
(= (port-line p) 0)
(= (port-column p) 0))))
(pass-if "peek-char [utf-16]"
(let ((p (with-fluids ((%default-port-encoding "UTF-16BE"))
(open-input-string "안녕하세요"))))
(and (char=? (peek-char p) #\안)
(char=? (peek-char p) #\안)
(char=? (peek-char p) #\안)
(= (port-line p) 0)
(= (port-column p) 0))))
;; Mini DSL to test decoding error handling.
(letrec-syntax ((decoding-error?
(syntax-rules ()
((_ port exp)
(catch 'decoding-error
(lambda ()
(pk 'exp exp)
#f)
(lambda (key subr message errno p)
(and (eq? p port)
(not (= 0 errno))))))))
(make-check
(syntax-rules (-> error eof)
((_ port (proc -> error))
(if (eq? 'substitute
(port-conversion-strategy port))
(eq? (proc port) #\?)
(decoding-error? port (proc port))))
((_ port (proc -> eof))
(eof-object? (proc port)))
((_ port (proc -> char))
(eq? (proc port) char))))
(make-checks
(syntax-rules ()
((_ port check ...)
(and (make-check port check) ...))))
(make-peek+read-checks
(syntax-rules ()
((_ port (result ...) e1 expected ...)
(make-peek+read-checks port
(result ...
(peek-char -> e1)
(read-char -> e1))
expected ...))
((_ port (result ...))
(make-checks port result ...))
((_ port #f e1 expected ...)
(make-peek+read-checks port
((peek-char -> e1)
(read-char -> e1))
expected ...))))
(test-decoding-error*
(syntax-rules ()
((_ sequence encoding strategy (expected ...))
(begin
(pass-if (format #f "test-decoding-error: ~s ~s ~s"
'sequence encoding strategy)
(let ((p (open-bytevector-input-port
(u8-list->bytevector 'sequence))))
(set-port-encoding! p encoding)
(set-port-conversion-strategy! p strategy)
(make-checks p
(read-char -> expected) ...)))
;; Generate the same test, but with one
;; `peek-char' call before each `read-char'.
;; Both should yield the same result.
(pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
'sequence encoding strategy)
(let ((p (open-bytevector-input-port
(u8-list->bytevector 'sequence))))
(set-port-encoding! p encoding)
(set-port-conversion-strategy! p strategy)
(make-peek+read-checks p #f expected
...)))))))
(test-decoding-error
(syntax-rules ()
((_ sequence encoding (expected ...))
(begin
(test-decoding-error* sequence encoding 'error
(expected ...))
;; `escape' should behave exactly like `error'.
(test-decoding-error* sequence encoding 'escape
(expected ...))
(test-decoding-error* sequence encoding 'substitute
(expected ...)))))))
(test-decoding-error (255 65 66 67) "UTF-8"
(error #\A #\B #\C eof))
(test-decoding-error (255 206 187 206 188) "UTF-8"
(error #\λ #\μ eof))
(test-decoding-error (206 187 206) "UTF-8"
;; Unterminated sequence.
(#\λ error eof))
;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
;; of the "Conformance" chapter of Unicode 6.0.0.)
(test-decoding-error (#xc0 #x80 #x41) "UTF-8"
(error ;; C0: should be in the C2..DF range
error ;; 80: invalid
#\A
eof))
(test-decoding-error (#xc2 #x41 #x42) "UTF-8"
(error ;; 41: should be in the 80..BF range
#\B
eof))
(test-decoding-error (#xe0 #x88 #x88) "UTF-8"
(error ;; 2nd byte should be in the A0..BF range
eof))
(test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
(error ;; 3rd byte should be in the 80..BF range
#\B
eof))
(test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
(error ;; 2nd byte should be in the 90..BF range
eof))))
(with-test-prefix "call-with-output-string"
;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
;; occur.
(pass-if-exception "proc closes port" exception:wrong-type-arg
(call-with-output-string close-port)))
;;;; Soft ports. No tests implemented yet.
;;;; Generic operations across all port types.
(let ((port-loop-temp (test-file)))
;; Return a list of input ports that all return the same text.
;; We map tests over this list.
(define (input-port-list text)
;; Create a text file some of the ports will use.
(let ((out-port (open-output-file port-loop-temp)))
(display text out-port)
(close-port out-port))
(list (open-input-file port-loop-temp)
(open-input-pipe (string-append "cat " port-loop-temp))
(call-with-input-string text (lambda (x) x))
;; We don't test soft ports at the moment.
))
(define port-list-names '("file" "pipe" "string"))
;; Test the line counter.
(define (test-line-counter text second-line final-column)
(with-test-prefix "line counter"
(let ((ports (input-port-list text)))
(for-each
(lambda (port port-name)
(with-test-prefix port-name
(pass-if "at beginning of input"
(= (port-line port) 0))
(pass-if "read first character"
(eqv? (read-char port) #\x))
(pass-if "after reading one character"
(= (port-line port) 0))
(pass-if "read first newline"
(eqv? (read-char port) #\newline))
(pass-if "after reading first newline char"
(= (port-line port) 1))
(pass-if "second line read correctly"
(equal? (read-line port) second-line))
(pass-if "read-line increments line number"
(= (port-line port) 2))
(pass-if "read-line returns EOF"
(let loop ((i 0))
(cond
((eof-object? (read-line port)) #t)
((> i 20) #f)
(else (loop (+ i 1))))))
(pass-if "line count is 5 at EOF"
(= (port-line port) 5))
(pass-if "column is correct at EOF"
(= (port-column port) final-column))))
ports port-list-names)
(for-each close-port ports)
(delete-file port-loop-temp))))
(with-test-prefix "newline"
(test-line-counter
(string-append "x\n"
"He who receives an idea from me, receives instruction\n"
"himself without lessening mine; as he who lights his\n"
"taper at mine, receives light without darkening me.\n"
" --- Thomas Jefferson\n")
"He who receives an idea from me, receives instruction"
0))
(with-test-prefix "no newline"
(test-line-counter
(string-append "x\n"
"He who receives an idea from me, receives instruction\n"
"himself without lessening mine; as he who lights his\n"
"taper at mine, receives light without darkening me.\n"
" --- Thomas Jefferson\n"
"no newline here")
"He who receives an idea from me, receives instruction"
15)))
;; Test port-line and port-column for output ports
(define (test-output-line-counter text final-column)
(with-test-prefix "port-line and port-column for output ports"
(let ((port (open-output-string)))
(pass-if "at beginning of input"
(and (= (port-line port) 0)
(= (port-column port) 0)))
(write-char #\x port)
(pass-if "after writing one character"
(and (= (port-line port) 0)
(= (port-column port) 1)))
(write-char #\newline port)
(pass-if "after writing first newline char"
(and (= (port-line port) 1)
(= (port-column port) 0)))
(display text port)
(pass-if "line count is 5 at end"
(= (port-line port) 5))
(pass-if "column is correct at end"
(= (port-column port) final-column)))))
(test-output-line-counter
(string-append "He who receives an idea from me, receives instruction\n"
"himself without lessening mine; as he who lights his\n"
"taper at mine, receives light without darkening me.\n"
" --- Thomas Jefferson\n"
"no newline here")
15)
(with-test-prefix "port-column"
(with-test-prefix "output"
(pass-if "x"
(let ((port (open-output-string)))
(display "x" port)
(= 1 (port-column port))))
(pass-if "\\a"
(let ((port (open-output-string)))
(display "\a" port)
(= 0 (port-column port))))
(pass-if "x\\a"
(let ((port (open-output-string)))
(display "x\a" port)
(= 1 (port-column port))))
(pass-if "\\x08 backspace"
(let ((port (open-output-string)))
(display "\x08" port)
(= 0 (port-column port))))
(pass-if "x\\x08 backspace"
(let ((port (open-output-string)))
(display "x\x08" port)
(= 0 (port-column port))))
(pass-if "\\n"
(let ((port (open-output-string)))
(display "\n" port)
(= 0 (port-column port))))
(pass-if "x\\n"
(let ((port (open-output-string)))
(display "x\n" port)
(= 0 (port-column port))))
(pass-if "\\r"
(let ((port (open-output-string)))
(display "\r" port)
(= 0 (port-column port))))
(pass-if "x\\r"
(let ((port (open-output-string)))
(display "x\r" port)
(= 0 (port-column port))))
(pass-if "\\t"
(let ((port (open-output-string)))
(display "\t" port)
(= 8 (port-column port))))
(pass-if "x\\t"
(let ((port (open-output-string)))
(display "x\t" port)
(= 8 (port-column port)))))
(with-test-prefix "input"
(pass-if "x"
(let ((port (open-input-string "x")))
(while (not (eof-object? (read-char port))))
(= 1 (port-column port))))
(pass-if "\\a"
(let ((port (open-input-string "\a")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "x\\a"
(let ((port (open-input-string "x\a")))
(while (not (eof-object? (read-char port))))
(= 1 (port-column port))))
(pass-if "\\x08 backspace"
(let ((port (open-input-string "\x08")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "x\\x08 backspace"
(let ((port (open-input-string "x\x08")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "\\n"
(let ((port (open-input-string "\n")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "x\\n"
(let ((port (open-input-string "x\n")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "\\r"
(let ((port (open-input-string "\r")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "x\\r"
(let ((port (open-input-string "x\r")))
(while (not (eof-object? (read-char port))))
(= 0 (port-column port))))
(pass-if "\\t"
(let ((port (open-input-string "\t")))
(while (not (eof-object? (read-char port))))
(= 8 (port-column port))))
(pass-if "x\\t"
(let ((port (open-input-string "x\t")))
(while (not (eof-object? (read-char port))))
(= 8 (port-column port))))))
(with-test-prefix "port-line"
;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
;; scm_t_port actually holds a long; this restricted the range on 64-bit
;; systems
(pass-if "set most-positive-fixnum/2"
(let ((n (quotient most-positive-fixnum 2))
(port (open-output-string)))
(set-port-line! port n)
(eqv? n (port-line port)))))
(with-test-prefix "port-encoding"
(pass-if-exception "set-port-encoding!, wrong encoding"
exception:miscellaneous-error
(set-port-encoding! (open-input-string "") "does-not-exist"))
(pass-if-exception "%default-port-encoding, wrong encoding"
exception:miscellaneous-error
(read (with-fluids ((%default-port-encoding "does-not-exist"))
(open-input-string "")))))
;;;
;;; port-for-each
;;;
(with-test-prefix "port-for-each"
;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
;; its iterator func if a port was inaccessible in the last gc mark but
;; the lazy sweeping has not yet reached it to remove it from the port
;; table (scm_i_port_table). Provoking those gc conditions is a little
;; tricky, but the following code made it happen in 1.8.2.
(pass-if "passing freed cell"
(let ((lst '()))
;; clear out the heap
(gc) (gc) (gc)
;; allocate cells so the opened ports aren't at the start of the heap
(make-list 1000)
(open-input-file "/dev/null")
(make-list 1000)
(open-input-file "/dev/null")
;; this gc leaves the above ports unmarked, ie. inaccessible
(gc)
;; but they're still in the port table, so this sees them
(port-for-each (lambda (port)
(set! lst (cons port lst))))
;; this forces completion of the sweeping
(gc) (gc) (gc)
;; and (if the bug is present) the cells accumulated in LST are now
;; freed cells, which give #f from `port?'
(not (memq #f (map port? lst))))))
(with-test-prefix
"fdes->port"
(pass-if "fdes->ports finds port"
(let ((port (open-file (test-file) "w")))
(not (not (memq port (fdes->ports (port->fdes port))))))))
;;;
;;; seek
;;;
(with-test-prefix "seek"
(with-test-prefix "file port"
(pass-if "SEEK_CUR"
(call-with-output-file (test-file)
(lambda (port)
(display "abcde" port)))
(let ((port (open-file (test-file) "r")))
(read-char port)
(seek port 2 SEEK_CUR)
(eqv? #\d (read-char port))))
(pass-if "SEEK_SET"
(call-with-output-file (test-file)
(lambda (port)
(display "abcde" port)))
(let ((port (open-file (test-file) "r")))
(read-char port)
(seek port 3 SEEK_SET)
(eqv? #\d (read-char port))))
(pass-if "SEEK_END"
(call-with-output-file (test-file)
(lambda (port)
(display "abcde" port)))
(let ((port (open-file (test-file) "r")))
(read-char port)
(seek port -2 SEEK_END)
(eqv? #\d (read-char port))))))
;;;
;;; truncate-file
;;;
(with-test-prefix "truncate-file"
(pass-if-exception "flonum file" exception:wrong-type-arg
(truncate-file 1.0 123))
(pass-if-exception "frac file" exception:wrong-type-arg
(truncate-file 7/3 123))
(with-test-prefix "filename"
(pass-if-exception "flonum length" exception:wrong-type-arg
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(truncate-file (test-file) 1.0))
(pass-if "shorten"
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(truncate-file (test-file) 1)
(eqv? 1 (stat:size (stat (test-file)))))
(pass-if-exception "shorten to current pos" exception:miscellaneous-error
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(truncate-file (test-file))))
(with-test-prefix "file descriptor"
(pass-if "shorten"
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(let ((fd (open-fdes (test-file) O_RDWR)))
(truncate-file fd 1)
(close-fdes fd))
(eqv? 1 (stat:size (stat (test-file)))))
(pass-if "shorten to current pos"
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(let ((fd (open-fdes (test-file) O_RDWR)))
(seek fd 1 SEEK_SET)
(truncate-file fd)
(close-fdes fd))
(eqv? 1 (stat:size (stat (test-file))))))
(with-test-prefix "file port"
(pass-if "shorten"
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(let ((port (open-file (test-file) "r+")))
(truncate-file port 1))
(eqv? 1 (stat:size (stat (test-file)))))
(pass-if "shorten to current pos"
(call-with-output-file (test-file)
(lambda (port)
(display "hello" port)))
(let ((port (open-file (test-file) "r+")))
(read-char port)
(truncate-file port))
(eqv? 1 (stat:size (stat (test-file)))))))
;;;; testing read-delimited and friends
(with-test-prefix "read-delimited!"
(let ((c (make-string 20 #\!)))
(call-with-input-string
"defdef\nghighi\n"
(lambda (port)
(read-delimited! "\n" c port 'concat)
(pass-if "read-delimited! reads a first line"
(string=? c "defdef\n!!!!!!!!!!!!!"))
(read-delimited! "\n" c port 'concat 3)
(pass-if "read-delimited! reads a first line"
(string=? c "defghighi\n!!!!!!!!!!"))))))
;;;; char-ready?
(call-with-input-string
"howdy"
(lambda (port)
(pass-if "char-ready? returns true on string port"
(char-ready? port))))
;;; This segfaults on some versions of Guile. We really should run
;;; the tests in a subprocess...
(call-with-input-string
"howdy"
(lambda (port)
(with-input-from-port
port
(lambda ()
(pass-if "char-ready? returns true on string port as default port"
(char-ready?))))))
;;;; Close current-input-port, and make sure everyone can handle it.
(with-test-prefix "closing current-input-port"
(for-each (lambda (procedure name)
(with-input-from-port
(call-with-input-string "foo" (lambda (p) p))
(lambda ()
(close-port (current-input-port))
(pass-if-exception name
exception:wrong-type-arg
(procedure)))))
(list read read-char read-line)
'("read" "read-char" "read-line")))
(delete-file (test-file))
;;; Local Variables:
;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
;;; End:
|