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 | ||
6dce942c MW |
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)))))) | |
fe949e7b LC |
93 | |
94 | \f | |
6dce942c | 95 | (with-test-prefix "read-delimited" |
fe949e7b | 96 | |
6dce942c MW |
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))))) | |
fe949e7b | 102 | |
6dce942c MW |
103 | (pass-if "delimiter hit, split" |
104 | (equal? '("hello" . #\,) | |
105 | (read-delimited ",.;" | |
106 | (open-input-string "hello, world!") | |
107 | 'split))) | |
fe949e7b | 108 | |
6dce942c MW |
109 | (pass-if "delimiter hit, concat" |
110 | (equal? '"hello," | |
111 | (read-delimited ",.;" (open-input-string "hello, world!") | |
112 | 'concat))) | |
fe949e7b | 113 | |
6dce942c MW |
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))))) | |
fe949e7b | 118 | |
6dce942c MW |
119 | (pass-if "eof" |
120 | (eof-object? (read-delimited "}{" (open-input-string ""))))) | |
fe949e7b LC |
121 | |
122 | \f | |
6dce942c MW |
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) | |
856d318a MW |
210 | (string=? (read-string p) "")))) |
211 | ||
212 | (pass-if-equal "longer than 100 chars, with limit" | |
213 | "hello, world!" | |
214 | (let* ((s (string-concatenate (make-list 20 "hello, world!"))) | |
215 | (p (open-input-string s))) | |
216 | (read-string p 13)))) | |
6dce942c MW |
217 | |
218 | (with-test-prefix "read-string!" | |
219 | ||
220 | (pass-if "buf smaller" | |
221 | (let* ((s "hello, world!") | |
222 | (len (1- (string-length s))) | |
223 | (buf (make-string len #\.)) | |
224 | (p (open-input-string s))) | |
225 | (and (= (read-string! buf p) len) | |
226 | (string=? buf (substring s 0 len)) | |
227 | (= (read-string! buf p) 1) | |
228 | (string=? (substring buf 0 1) (substring s len))))) | |
229 | ||
230 | (pass-if "buf right size" | |
231 | (let* ((s "hello, world!") | |
232 | (len (string-length s)) | |
233 | (buf (make-string len #\.)) | |
234 | (p (open-input-string s))) | |
235 | (and (= (read-string! buf p) len) | |
236 | (string=? buf (substring s 0 len)) | |
237 | (= (read-string! buf p) 0) | |
238 | (string=? buf (substring s 0 len))))) | |
239 | ||
240 | (pass-if "buf bigger" | |
241 | (let* ((s "hello, world!") | |
242 | (len (string-length s)) | |
243 | (buf (make-string (1+ len) #\.)) | |
244 | (p (open-input-string s))) | |
245 | (and (= (read-string! buf p) len) | |
246 | (string=? (substring buf 0 len) s) | |
247 | (= (read-string! buf p) 0) | |
248 | (string=? (substring buf 0 len) s) | |
249 | (string=? (substring buf len) "."))))) | |
a2c36371 LC |
250 | |
251 | ;;; Local Variables: | |
252 | ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1) | |
253 | ;;; eval: (put 'pass-if 'scheme-indent-function 1) | |
254 | ;;; End: |