1 ;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*-
2 ;;;; Ludovic Courtès <ludo@gnu.org>
4 ;;;; Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc.
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.
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.
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
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))
25 (with-fluids ((%default-port-encoding "UTF-8"))
27 (with-test-prefix "read-line"
30 (let* ((s "hello, world")
31 (p (open-input-string s)))
32 (and (string=? s (read-line p))
33 (eof-object? (read-line p)))))
35 (pass-if "two lines, trim"
36 (let* ((s "foo\nbar\n")
37 (p (open-input-string s)))
38 (and (equal? (string-tokenize s)
39 (list (read-line p) (read-line p)))
40 (eof-object? (read-line p)))))
42 (pass-if "two lines, concat"
43 (let* ((s "foo\nbar\n")
44 (p (open-input-string s)))
45 (and (equal? '("foo\n" "bar\n")
46 (list (read-line p 'concat)
47 (read-line p 'concat)))
48 (eof-object? (read-line p)))))
50 (pass-if "two lines, peek"
51 (let* ((s "foo\nbar\n")
52 (p (open-input-string s)))
53 (and (equal? '("foo" #\newline "bar" #\newline)
54 (list (read-line p 'peek) (read-char p)
55 (read-line p 'peek) (read-char p)))
56 (eof-object? (read-line p)))))
58 (pass-if "two lines, split"
59 (let* ((s "foo\nbar\n")
60 (p (open-input-string s)))
61 (and (equal? '(("foo" . #\newline)
63 (list (read-line p 'split)
64 (read-line p 'split)))
65 (eof-object? (read-line p)))))
67 (pass-if "two Greek lines, trim"
68 (let* ((s "λαμβδα\nμυ\n")
69 (p (open-input-string s)))
70 (and (equal? (string-tokenize s)
71 (list (read-line p) (read-line p)))
72 (eof-object? (read-line p)))))
74 (pass-if "decoding error"
75 (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
76 (set-port-encoding! p "UTF-8")
77 (set-port-conversion-strategy! p 'error)
78 (catch 'decoding-error
82 (lambda (key subr message err port)
85 ;; PORT should now point past the error.
86 (string=? (read-line p) "BCD")
87 (eof-object? (read-line p)))))))
89 (pass-if "decoding error, substitute"
90 (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
91 (set-port-encoding! p "UTF-8")
92 (set-port-conversion-strategy! p 'substitute)
93 (and (string=? (read-line p) "A?BCD")
94 (eof-object? (read-line p))))))
97 (with-test-prefix "read-delimited"
99 (pass-if "delimiter hit"
100 (let ((p (open-input-string "hello, world!")))
101 (and (string=? "hello" (read-delimited ",.;" p))
102 (string=? " world!" (read-delimited ",.;" p))
103 (eof-object? (read-delimited ",.;" p)))))
105 (pass-if "delimiter hit, split"
106 (equal? '("hello" . #\,)
107 (read-delimited ",.;"
108 (open-input-string "hello, world!")
111 (pass-if "delimiter hit, concat"
113 (read-delimited ",.;" (open-input-string "hello, world!")
116 (pass-if "delimiter hit, peek"
117 (let ((p (open-input-string "hello, world!")))
118 (and (string=? "hello" (read-delimited ",.;" p 'peek))
119 (char=? #\, (peek-char p)))))
122 (eof-object? (read-delimited "}{" (open-input-string "")))))
125 (with-test-prefix "read-delimited!"
127 (pass-if "delimiter hit"
128 (let ((s (make-string 123))
129 (p (open-input-string "hello, world!")))
130 (and (= 5 (read-delimited! ",.;" s p))
131 (string=? (substring s 0 5) "hello")
132 (= 7 (read-delimited! ",.;" s p))
133 (string=? (substring s 0 7) " world!")
134 (eof-object? (read-delimited! ",.;" s p)))))
136 (pass-if "delimiter hit, start+end"
137 (let ((s (make-string 123))
138 (p (open-input-string "hello, world!")))
139 (and (= 5 (read-delimited! ",.;" s p 'trim 10 30))
140 (string=? (substring s 10 15) "hello"))))
142 (pass-if "delimiter hit, split"
143 (let ((s (make-string 123)))
144 (and (equal? '(5 . #\,)
145 (read-delimited! ",.;" s
146 (open-input-string "hello, world!")
148 (string=? (substring s 0 5) "hello"))))
150 (pass-if "delimiter hit, concat"
151 (let ((s (make-string 123)))
152 (and (= 6 (read-delimited! ",.;" s
153 (open-input-string "hello, world!")
155 (string=? (substring s 0 6) "hello,"))))
157 (pass-if "delimiter hit, peek"
158 (let ((s (make-string 123))
159 (p (open-input-string "hello, world!")))
160 (and (= 5 (read-delimited! ",.;" s p 'peek))
161 (string=? (substring s 0 5) "hello")
162 (char=? #\, (peek-char p)))))
164 (pass-if "string too small"
165 (let ((s (make-string 7)))
166 (and (= 7 (read-delimited! "}{" s
167 (open-input-string "hello, world!")))
168 (string=? s "hello, "))))
170 (pass-if "string too small, start+end"
171 (let ((s (make-string 123)))
172 (and (= 7 (read-delimited! "}{" s
173 (open-input-string "hello, world!")
176 (string=? (substring s 70 77) "hello, "))))
178 (pass-if "string too small, split"
179 (let ((s (make-string 7)))
180 (and (equal? '(7 . #f)
181 (read-delimited! "}{" s
182 (open-input-string "hello, world!")
184 (string=? s "hello, "))))
187 (eof-object? (read-delimited! ":" (make-string 7)
188 (open-input-string ""))))
190 (pass-if "eof, split"
191 (eof-object? (read-delimited! ":" (make-string 7)
192 (open-input-string "")))))
194 (with-test-prefix "read-string"
196 (pass-if "short string"
197 (let* ((s "hello, world!")
198 (p (open-input-string s)))
199 (and (string=? (read-string p) s)
200 (string=? (read-string p) ""))))
203 (let* ((s (make-string 100 #\space))
204 (p (open-input-string s)))
205 (and (string=? (read-string p) s)
206 (string=? (read-string p) ""))))
208 (pass-if "longer than 100 chars"
209 (let* ((s (string-concatenate (make-list 20 "hello, world!")))
210 (p (open-input-string s)))
211 (and (string=? (read-string p) s)
212 (string=? (read-string p) ""))))
214 (pass-if-equal "longer than 100 chars, with limit"
216 (let* ((s (string-concatenate (make-list 20 "hello, world!")))
217 (p (open-input-string s)))
218 (read-string p 13))))
220 (with-test-prefix "read-string!"
222 (pass-if "buf smaller"
223 (let* ((s "hello, world!")
224 (len (1- (string-length s)))
225 (buf (make-string len #\.))
226 (p (open-input-string s)))
227 (and (= (read-string! buf p) len)
228 (string=? buf (substring s 0 len))
229 (= (read-string! buf p) 1)
230 (string=? (substring buf 0 1) (substring s len)))))
232 (pass-if "buf right size"
233 (let* ((s "hello, world!")
234 (len (string-length s))
235 (buf (make-string len #\.))
236 (p (open-input-string s)))
237 (and (= (read-string! buf p) len)
238 (string=? buf (substring s 0 len))
239 (= (read-string! buf p) 0)
240 (string=? buf (substring s 0 len)))))
242 (pass-if "buf bigger"
243 (let* ((s "hello, world!")
244 (len (string-length s))
245 (buf (make-string (1+ len) #\.))
246 (p (open-input-string s)))
247 (and (= (read-string! buf p) len)
248 (string=? (substring buf 0 len) s)
249 (= (read-string! buf p) 0)
250 (string=? (substring buf 0 len) s)
251 (string=? (substring buf len) "."))))))
254 ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1)
255 ;;; eval: (put 'pass-if 'scheme-indent-function 1)