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