Commit | Line | Data |
---|---|---|
a2c36371 LC |
1 | ;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*- |
2 | ;;;; Ludovic Courtès <ludo@gnu.org> | |
3 | ;;;; | |
a41b07a3 | 4 | ;;;; Copyright (C) 2011, 2013, 2014 Free Software Foundation, Inc. |
a2c36371 LC |
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) | |
c62da8f8 | 22 | #:use-module ((rnrs io ports) #:select (open-bytevector-input-port)) |
a2c36371 LC |
23 | #:use-module (test-suite lib)) |
24 | ||
25 | (with-fluids ((%default-port-encoding "UTF-8")) | |
26 | ||
27 | (with-test-prefix "read-line" | |
28 | ||
29 | (pass-if "one 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))))) | |
34 | ||
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))))) | |
41 | ||
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))))) | |
49 | ||
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))))) | |
57 | ||
58 | (pass-if "two lines, split" | |
59 | (let* ((s "foo\nbar\n") | |
60 | (p (open-input-string s))) | |
61 | (and (equal? '(("foo" . #\newline) | |
62 | ("bar" . #\newline)) | |
63 | (list (read-line p 'split) | |
64 | (read-line p 'split))) | |
65 | (eof-object? (read-line p))))) | |
66 | ||
c62da8f8 | 67 | (pass-if "two Greek lines, trim" |
a2c36371 LC |
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))) | |
c62da8f8 LC |
72 | (eof-object? (read-line p))))) |
73 | ||
74 | (pass-if "decoding error" | |
96128014 LC |
75 | (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68)))) |
76 | (set-port-encoding! p "UTF-8") | |
c62da8f8 LC |
77 | (set-port-conversion-strategy! p 'error) |
78 | (catch 'decoding-error | |
79 | (lambda () | |
80 | (read-line p) | |
81 | #f) | |
82 | (lambda (key subr message err port) | |
83 | (and (eq? port p) | |
84 | ||
85 | ;; PORT should now point past the error. | |
86 | (string=? (read-line p) "BCD") | |
87 | (eof-object? (read-line p))))))) | |
88 | ||
89 | (pass-if "decoding error, substitute" | |
96128014 LC |
90 | (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68)))) |
91 | (set-port-encoding! p "UTF-8") | |
c62da8f8 LC |
92 | (set-port-conversion-strategy! p 'substitute) |
93 | (and (string=? (read-line p) "A?BCD") | |
fe949e7b LC |
94 | (eof-object? (read-line p)))))) |
95 | ||
96 | \f | |
97 | (with-test-prefix "read-delimited" | |
98 | ||
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))))) | |
104 | ||
105 | (pass-if "delimiter hit, split" | |
106 | (equal? '("hello" . #\,) | |
107 | (read-delimited ",.;" | |
108 | (open-input-string "hello, world!") | |
109 | 'split))) | |
110 | ||
111 | (pass-if "delimiter hit, concat" | |
112 | (equal? '"hello," | |
113 | (read-delimited ",.;" (open-input-string "hello, world!") | |
114 | 'concat))) | |
115 | ||
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))))) | |
120 | ||
121 | (pass-if "eof" | |
122 | (eof-object? (read-delimited "}{" (open-input-string ""))))) | |
123 | ||
124 | \f | |
125 | (with-test-prefix "read-delimited!" | |
126 | ||
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))))) | |
135 | ||
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")))) | |
141 | ||
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!") | |
147 | 'split)) | |
148 | (string=? (substring s 0 5) "hello")))) | |
149 | ||
150 | (pass-if "delimiter hit, concat" | |
151 | (let ((s (make-string 123))) | |
152 | (and (= 6 (read-delimited! ",.;" s | |
153 | (open-input-string "hello, world!") | |
154 | 'concat)) | |
155 | (string=? (substring s 0 6) "hello,")))) | |
156 | ||
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))))) | |
163 | ||
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, ")))) | |
169 | ||
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!") | |
174 | 'trim | |
175 | 70 77)) | |
176 | (string=? (substring s 70 77) "hello, ")))) | |
177 | ||
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!") | |
183 | 'split)) | |
184 | (string=? s "hello, ")))) | |
185 | ||
186 | (pass-if "eof" | |
187 | (eof-object? (read-delimited! ":" (make-string 7) | |
188 | (open-input-string "")))) | |
189 | ||
190 | (pass-if "eof, split" | |
191 | (eof-object? (read-delimited! ":" (make-string 7) | |
5a35d42a AW |
192 | (open-input-string ""))))) |
193 | ||
194 | (with-test-prefix "read-string" | |
195 | ||
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) "")))) | |
201 | ||
202 | (pass-if "100 chars" | |
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) "")))) | |
207 | ||
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) | |
a41b07a3 LC |
212 | (string=? (read-string p) "")))) |
213 | ||
214 | (pass-if-equal "longer than 100 chars, with limit" | |
215 | "hello, world!" | |
216 | (let* ((s (string-concatenate (make-list 20 "hello, world!"))) | |
217 | (p (open-input-string s))) | |
218 | (read-string p 13)))) | |
5a35d42a AW |
219 | |
220 | (with-test-prefix "read-string!" | |
221 | ||
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))))) | |
231 | ||
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))))) | |
241 | ||
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) ".")))))) | |
a2c36371 LC |
252 | |
253 | ;;; Local Variables: | |
254 | ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1) | |
255 | ;;; eval: (put 'pass-if 'scheme-indent-function 1) | |
256 | ;;; End: |