Add call-with-stack-overflow-handler tests
[bpt/guile.git] / test-suite / tests / rdelim.test
1 ;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*-
2 ;;;; Ludovic Courtès <ludo@gnu.org>
3 ;;;;
4 ;;;; Copyright (C) 2011, 2013 Free Software Foundation, Inc.
5 ;;;;
6 ;;;; This library is free software; you can redistribute it and/or
7 ;;;; modify it under the terms of the GNU Lesser General Public
8 ;;;; License as published by the Free Software Foundation; either
9 ;;;; version 3 of the License, or (at your option) any later version.
10 ;;;;
11 ;;;; This library is distributed in the hope that it will be useful,
12 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 ;;;; Lesser General Public License for more details.
15 ;;;;
16 ;;;; You should have received a copy of the GNU Lesser General Public
17 ;;;; License along with this library; if not, write to the Free Software
18 ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
20 (define-module (test-suite test-rdelim)
21 #:use-module (ice-9 rdelim)
22 #:use-module ((rnrs io ports) #:select (open-bytevector-input-port))
23 #:use-module (test-suite lib))
24
25 (with-test-prefix "read-line"
26
27 (pass-if "one line"
28 (let* ((s "hello, world")
29 (p (open-input-string s)))
30 (and (string=? s (read-line p))
31 (eof-object? (read-line p)))))
32
33 (pass-if "two lines, trim"
34 (let* ((s "foo\nbar\n")
35 (p (open-input-string s)))
36 (and (equal? (string-tokenize s)
37 (list (read-line p) (read-line p)))
38 (eof-object? (read-line p)))))
39
40 (pass-if "two lines, concat"
41 (let* ((s "foo\nbar\n")
42 (p (open-input-string s)))
43 (and (equal? '("foo\n" "bar\n")
44 (list (read-line p 'concat)
45 (read-line p 'concat)))
46 (eof-object? (read-line p)))))
47
48 (pass-if "two lines, peek"
49 (let* ((s "foo\nbar\n")
50 (p (open-input-string s)))
51 (and (equal? '("foo" #\newline "bar" #\newline)
52 (list (read-line p 'peek) (read-char p)
53 (read-line p 'peek) (read-char p)))
54 (eof-object? (read-line p)))))
55
56 (pass-if "two lines, split"
57 (let* ((s "foo\nbar\n")
58 (p (open-input-string s)))
59 (and (equal? '(("foo" . #\newline)
60 ("bar" . #\newline))
61 (list (read-line p 'split)
62 (read-line p 'split)))
63 (eof-object? (read-line p)))))
64
65 (pass-if "two Greek lines, trim"
66 (let* ((s "λαμβδα\nμυ\n")
67 (p (open-input-string s)))
68 (and (equal? (string-tokenize s)
69 (list (read-line p) (read-line p)))
70 (eof-object? (read-line p)))))
71
72 (pass-if "decoding error"
73 (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
74 (set-port-encoding! p "UTF-8")
75 (set-port-conversion-strategy! p 'error)
76 (catch 'decoding-error
77 (lambda ()
78 (read-line p)
79 #f)
80 (lambda (key subr message err port)
81 (and (eq? port p)
82
83 ;; PORT should now point past the error.
84 (string=? (read-line p) "BCD")
85 (eof-object? (read-line p)))))))
86
87 (pass-if "decoding error, substitute"
88 (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
89 (set-port-encoding! p "UTF-8")
90 (set-port-conversion-strategy! p 'substitute)
91 (and (string=? (read-line p) "A?BCD")
92 (eof-object? (read-line p))))))
93
94 \f
95 (with-test-prefix "read-delimited"
96
97 (pass-if "delimiter hit"
98 (let ((p (open-input-string "hello, world!")))
99 (and (string=? "hello" (read-delimited ",.;" p))
100 (string=? " world!" (read-delimited ",.;" p))
101 (eof-object? (read-delimited ",.;" p)))))
102
103 (pass-if "delimiter hit, split"
104 (equal? '("hello" . #\,)
105 (read-delimited ",.;"
106 (open-input-string "hello, world!")
107 'split)))
108
109 (pass-if "delimiter hit, concat"
110 (equal? '"hello,"
111 (read-delimited ",.;" (open-input-string "hello, world!")
112 'concat)))
113
114 (pass-if "delimiter hit, peek"
115 (let ((p (open-input-string "hello, world!")))
116 (and (string=? "hello" (read-delimited ",.;" p 'peek))
117 (char=? #\, (peek-char p)))))
118
119 (pass-if "eof"
120 (eof-object? (read-delimited "}{" (open-input-string "")))))
121
122 \f
123 (with-test-prefix "read-delimited!"
124
125 (pass-if "delimiter hit"
126 (let ((s (make-string 123))
127 (p (open-input-string "hello, world!")))
128 (and (= 5 (read-delimited! ",.;" s p))
129 (string=? (substring s 0 5) "hello")
130 (= 7 (read-delimited! ",.;" s p))
131 (string=? (substring s 0 7) " world!")
132 (eof-object? (read-delimited! ",.;" s p)))))
133
134 (pass-if "delimiter hit, start+end"
135 (let ((s (make-string 123))
136 (p (open-input-string "hello, world!")))
137 (and (= 5 (read-delimited! ",.;" s p 'trim 10 30))
138 (string=? (substring s 10 15) "hello"))))
139
140 (pass-if "delimiter hit, split"
141 (let ((s (make-string 123)))
142 (and (equal? '(5 . #\,)
143 (read-delimited! ",.;" s
144 (open-input-string "hello, world!")
145 'split))
146 (string=? (substring s 0 5) "hello"))))
147
148 (pass-if "delimiter hit, concat"
149 (let ((s (make-string 123)))
150 (and (= 6 (read-delimited! ",.;" s
151 (open-input-string "hello, world!")
152 'concat))
153 (string=? (substring s 0 6) "hello,"))))
154
155 (pass-if "delimiter hit, peek"
156 (let ((s (make-string 123))
157 (p (open-input-string "hello, world!")))
158 (and (= 5 (read-delimited! ",.;" s p 'peek))
159 (string=? (substring s 0 5) "hello")
160 (char=? #\, (peek-char p)))))
161
162 (pass-if "string too small"
163 (let ((s (make-string 7)))
164 (and (= 7 (read-delimited! "}{" s
165 (open-input-string "hello, world!")))
166 (string=? s "hello, "))))
167
168 (pass-if "string too small, start+end"
169 (let ((s (make-string 123)))
170 (and (= 7 (read-delimited! "}{" s
171 (open-input-string "hello, world!")
172 'trim
173 70 77))
174 (string=? (substring s 70 77) "hello, "))))
175
176 (pass-if "string too small, split"
177 (let ((s (make-string 7)))
178 (and (equal? '(7 . #f)
179 (read-delimited! "}{" s
180 (open-input-string "hello, world!")
181 'split))
182 (string=? s "hello, "))))
183
184 (pass-if "eof"
185 (eof-object? (read-delimited! ":" (make-string 7)
186 (open-input-string ""))))
187
188 (pass-if "eof, split"
189 (eof-object? (read-delimited! ":" (make-string 7)
190 (open-input-string "")))))
191
192 (with-test-prefix "read-string"
193
194 (pass-if "short string"
195 (let* ((s "hello, world!")
196 (p (open-input-string s)))
197 (and (string=? (read-string p) s)
198 (string=? (read-string p) ""))))
199
200 (pass-if "100 chars"
201 (let* ((s (make-string 100 #\space))
202 (p (open-input-string s)))
203 (and (string=? (read-string p) s)
204 (string=? (read-string p) ""))))
205
206 (pass-if "longer than 100 chars"
207 (let* ((s (string-concatenate (make-list 20 "hello, world!")))
208 (p (open-input-string s)))
209 (and (string=? (read-string p) s)
210 (string=? (read-string p) "")))))
211
212 (with-test-prefix "read-string!"
213
214 (pass-if "buf smaller"
215 (let* ((s "hello, world!")
216 (len (1- (string-length s)))
217 (buf (make-string len #\.))
218 (p (open-input-string s)))
219 (and (= (read-string! buf p) len)
220 (string=? buf (substring s 0 len))
221 (= (read-string! buf p) 1)
222 (string=? (substring buf 0 1) (substring s len)))))
223
224 (pass-if "buf right size"
225 (let* ((s "hello, world!")
226 (len (string-length s))
227 (buf (make-string len #\.))
228 (p (open-input-string s)))
229 (and (= (read-string! buf p) len)
230 (string=? buf (substring s 0 len))
231 (= (read-string! buf p) 0)
232 (string=? buf (substring s 0 len)))))
233
234 (pass-if "buf bigger"
235 (let* ((s "hello, world!")
236 (len (string-length s))
237 (buf (make-string (1+ len) #\.))
238 (p (open-input-string s)))
239 (and (= (read-string! buf p) len)
240 (string=? (substring buf 0 len) s)
241 (= (read-string! buf p) 0)
242 (string=? (substring buf 0 len) s)
243 (string=? (substring buf len) ".")))))
244
245 ;;; Local Variables:
246 ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1)
247 ;;; eval: (put 'pass-if 'scheme-indent-function 1)
248 ;;; End: