| 1 |
;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*- |
| 2 |
;;;; Ludovic Courtès <[email protected]> |
| 3 |
;;;; |
| 4 |
;;;; Copyright (C) 2011, 2013, 2014 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 get-u8)) |
| 23 |
#:use-module (test-suite lib) |
| 24 |
#:use-module (test-suite guile-test)) |
| 25 |
|
| 26 |
(with-test-prefix "read-line" |
| 27 |
|
| 28 |
(pass-if "one line" |
| 29 |
(let* ((s "hello, world") |
| 30 |
(p (open-input-string s))) |
| 31 |
(and (string=? s (read-line p)) |
| 32 |
(eof-object? (read-line p))))) |
| 33 |
|
| 34 |
(pass-if "two lines, trim" |
| 35 |
(let* ((s "foo\nbar\n") |
| 36 |
(p (open-input-string s))) |
| 37 |
(and (equal? (string-tokenize s) |
| 38 |
(list (read-line p) (read-line p))) |
| 39 |
(eof-object? (read-line p))))) |
| 40 |
|
| 41 |
(pass-if "two lines, concat" |
| 42 |
(let* ((s "foo\nbar\n") |
| 43 |
(p (open-input-string s))) |
| 44 |
(and (equal? '("foo\n" "bar\n") |
| 45 |
(list (read-line p 'concat) |
| 46 |
(read-line p 'concat))) |
| 47 |
(eof-object? (read-line p))))) |
| 48 |
|
| 49 |
(pass-if "two lines, peek" |
| 50 |
(let* ((s "foo\nbar\n") |
| 51 |
(p (open-input-string s))) |
| 52 |
(and (equal? '("foo" #\newline "bar" #\newline) |
| 53 |
(list (read-line p 'peek) (read-char p) |
| 54 |
(read-line p 'peek) (read-char p))) |
| 55 |
(eof-object? (read-line p))))) |
| 56 |
|
| 57 |
(pass-if "two lines, split" |
| 58 |
(let* ((s "foo\nbar\n") |
| 59 |
(p (open-input-string s))) |
| 60 |
(and (equal? '(("foo" . #\newline) |
| 61 |
("bar" . #\newline)) |
| 62 |
(list (read-line p 'split) |
| 63 |
(read-line p 'split))) |
| 64 |
(eof-object? (read-line p))))) |
| 65 |
|
| 66 |
(pass-if "two Greek lines, trim" |
| 67 |
(let* ((s "λαμβδα\nμυ\n") |
| 68 |
(p (open-input-string s))) |
| 69 |
(and (equal? (string-tokenize s) |
| 70 |
(list (read-line p) (read-line p))) |
| 71 |
(eof-object? (read-line p))))) |
| 72 |
|
| 73 |
(pass-if "decoding error" |
| 74 |
(let ((p (open-bytevector-input-port #vu8(65 255 66 67 68)))) |
| 75 |
(set-port-encoding! p "UTF-8") |
| 76 |
(set-port-conversion-strategy! p 'error) |
| 77 |
(catch 'decoding-error |
| 78 |
(lambda () |
| 79 |
(read-line p) |
| 80 |
#f) |
| 81 |
(lambda (key subr message err port) |
| 82 |
(and (eq? port p) |
| 83 |
(eqv? (get-u8 p) 255) |
| 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\uFFFDBCD") |
| 92 |
(eof-object? (read-line p)))))) |
| 93 |
|
| 94 |
|
| 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 |
|
| 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 |
(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)))) |
| 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) "."))))) |
| 250 |
|
| 251 |
|
| 252 |
(with-test-prefix "for-line-in-file" |
| 253 |
|
| 254 |
(define (test-file) |
| 255 |
(data-file-name "ports-test.tmp")) |
| 256 |
|
| 257 |
(let ((lst '()) |
| 258 |
(lines '()) |
| 259 |
(string "line1\nline2\nline3") |
| 260 |
(filename (test-file))) |
| 261 |
(call-with-input-string |
| 262 |
"A\0B\0C" |
| 263 |
(lambda (port) |
| 264 |
(pass-if "for-delimited-from-port parses stream correctly" |
| 265 |
(for-delimited-from-port port |
| 266 |
(lambda (entry) |
| 267 |
(set! lst (cons entry lst))) |
| 268 |
#:delims "\0") |
| 269 |
(equal? lst '("C" "B" "A"))))) |
| 270 |
(let ((port (open-output-file filename))) |
| 271 |
(display string port) |
| 272 |
(close-port port)) |
| 273 |
(pass-if "for-line-in-file parses file correctly" |
| 274 |
(for-line-in-file filename |
| 275 |
(lambda (line) |
| 276 |
(set! lines (cons line lines)))) |
| 277 |
(equal? lines '("line3" "line2" "line1")))) |
| 278 |
|
| 279 |
(delete-file (test-file)) |
| 280 |
) |
| 281 |
|
| 282 |
;;; Local Variables: |
| 283 |
;;; eval: (put 'with-test-prefix 'scheme-indent-function 1) |
| 284 |
;;; eval: (put 'pass-if 'scheme-indent-function 1) |
| 285 |
;;; End: |