rdelim.test 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  1. ;;;; rdelim.test --- Delimited I/O. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;; Ludovic Courtès <ludo@gnu.org>
  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. (define-module (test-suite test-rdelim)
  20. #:use-module (ice-9 rdelim)
  21. #:use-module ((rnrs io ports) #:select (open-bytevector-input-port get-u8))
  22. #:use-module (test-suite lib))
  23. (with-test-prefix "read-line"
  24. (pass-if "one line"
  25. (let* ((s "hello, world")
  26. (p (open-input-string s)))
  27. (and (string=? s (read-line p))
  28. (eof-object? (read-line p)))))
  29. (pass-if "two lines, trim"
  30. (let* ((s "foo\nbar\n")
  31. (p (open-input-string s)))
  32. (and (equal? (string-tokenize s)
  33. (list (read-line p) (read-line p)))
  34. (eof-object? (read-line p)))))
  35. (pass-if "two lines, concat"
  36. (let* ((s "foo\nbar\n")
  37. (p (open-input-string s)))
  38. (and (equal? '("foo\n" "bar\n")
  39. (list (read-line p 'concat)
  40. (read-line p 'concat)))
  41. (eof-object? (read-line p)))))
  42. (pass-if "two lines, peek"
  43. (let* ((s "foo\nbar\n")
  44. (p (open-input-string s)))
  45. (and (equal? '("foo" #\newline "bar" #\newline)
  46. (list (read-line p 'peek) (read-char p)
  47. (read-line p 'peek) (read-char p)))
  48. (eof-object? (read-line p)))))
  49. (pass-if "two lines, split"
  50. (let* ((s "foo\nbar\n")
  51. (p (open-input-string s)))
  52. (and (equal? '(("foo" . #\newline)
  53. ("bar" . #\newline))
  54. (list (read-line p 'split)
  55. (read-line p 'split)))
  56. (eof-object? (read-line p)))))
  57. (pass-if "two Greek lines, trim"
  58. (let* ((s "λαμβδα\nμυ\n")
  59. (p (open-input-string s)))
  60. (and (equal? (string-tokenize s)
  61. (list (read-line p) (read-line p)))
  62. (eof-object? (read-line p)))))
  63. (pass-if "decoding error"
  64. (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
  65. (set-port-encoding! p "UTF-8")
  66. (set-port-conversion-strategy! p 'error)
  67. (catch 'decoding-error
  68. (lambda ()
  69. (read-line p)
  70. #f)
  71. (lambda (key subr message err port)
  72. (and (eq? port p)
  73. (eqv? (get-u8 p) 255)
  74. (string=? (read-line p) "BCD")
  75. (eof-object? (read-line p)))))))
  76. (pass-if "decoding error, substitute"
  77. (let ((p (open-bytevector-input-port #vu8(65 255 66 67 68))))
  78. (set-port-encoding! p "UTF-8")
  79. (set-port-conversion-strategy! p 'substitute)
  80. (and (string=? (read-line p) "A\uFFFDBCD")
  81. (eof-object? (read-line p))))))
  82. (with-test-prefix "read-delimited"
  83. (pass-if "delimiter hit"
  84. (let ((p (open-input-string "hello, world!")))
  85. (and (string=? "hello" (read-delimited ",.;" p))
  86. (string=? " world!" (read-delimited ",.;" p))
  87. (eof-object? (read-delimited ",.;" p)))))
  88. (pass-if "delimiter hit, split"
  89. (equal? '("hello" . #\,)
  90. (read-delimited ",.;"
  91. (open-input-string "hello, world!")
  92. 'split)))
  93. (pass-if "delimiter hit, concat"
  94. (equal? '"hello,"
  95. (read-delimited ",.;" (open-input-string "hello, world!")
  96. 'concat)))
  97. (pass-if "delimiter hit, peek"
  98. (let ((p (open-input-string "hello, world!")))
  99. (and (string=? "hello" (read-delimited ",.;" p 'peek))
  100. (char=? #\, (peek-char p)))))
  101. (pass-if "eof"
  102. (eof-object? (read-delimited "}{" (open-input-string "")))))
  103. (with-test-prefix "read-delimited!"
  104. (pass-if "delimiter hit"
  105. (let ((s (make-string 123))
  106. (p (open-input-string "hello, world!")))
  107. (and (= 5 (read-delimited! ",.;" s p))
  108. (string=? (substring s 0 5) "hello")
  109. (= 7 (read-delimited! ",.;" s p))
  110. (string=? (substring s 0 7) " world!")
  111. (eof-object? (read-delimited! ",.;" s p)))))
  112. (pass-if "delimiter hit, start+end"
  113. (let ((s (make-string 123))
  114. (p (open-input-string "hello, world!")))
  115. (and (= 5 (read-delimited! ",.;" s p 'trim 10 30))
  116. (string=? (substring s 10 15) "hello"))))
  117. (pass-if "delimiter hit, split"
  118. (let ((s (make-string 123)))
  119. (and (equal? '(5 . #\,)
  120. (read-delimited! ",.;" s
  121. (open-input-string "hello, world!")
  122. 'split))
  123. (string=? (substring s 0 5) "hello"))))
  124. (pass-if "delimiter hit, concat"
  125. (let ((s (make-string 123)))
  126. (and (= 6 (read-delimited! ",.;" s
  127. (open-input-string "hello, world!")
  128. 'concat))
  129. (string=? (substring s 0 6) "hello,"))))
  130. (pass-if "delimiter hit, peek"
  131. (let ((s (make-string 123))
  132. (p (open-input-string "hello, world!")))
  133. (and (= 5 (read-delimited! ",.;" s p 'peek))
  134. (string=? (substring s 0 5) "hello")
  135. (char=? #\, (peek-char p)))))
  136. (pass-if "string too small"
  137. (let ((s (make-string 7)))
  138. (and (= 7 (read-delimited! "}{" s
  139. (open-input-string "hello, world!")))
  140. (string=? s "hello, "))))
  141. (pass-if "string too small, start+end"
  142. (let ((s (make-string 123)))
  143. (and (= 7 (read-delimited! "}{" s
  144. (open-input-string "hello, world!")
  145. 'trim
  146. 70 77))
  147. (string=? (substring s 70 77) "hello, "))))
  148. (pass-if "string too small, split"
  149. (let ((s (make-string 7)))
  150. (and (equal? '(7 . #f)
  151. (read-delimited! "}{" s
  152. (open-input-string "hello, world!")
  153. 'split))
  154. (string=? s "hello, "))))
  155. (pass-if "eof"
  156. (eof-object? (read-delimited! ":" (make-string 7)
  157. (open-input-string ""))))
  158. (pass-if "eof, split"
  159. (eof-object? (read-delimited! ":" (make-string 7)
  160. (open-input-string "")))))
  161. (with-test-prefix "read-string"
  162. (pass-if "short string"
  163. (let* ((s "hello, world!")
  164. (p (open-input-string s)))
  165. (and (string=? (read-string p) s)
  166. (string=? (read-string p) ""))))
  167. (pass-if "100 chars"
  168. (let* ((s (make-string 100 #\space))
  169. (p (open-input-string s)))
  170. (and (string=? (read-string p) s)
  171. (string=? (read-string p) ""))))
  172. (pass-if "longer than 100 chars"
  173. (let* ((s (string-concatenate (make-list 20 "hello, world!")))
  174. (p (open-input-string s)))
  175. (and (string=? (read-string p) s)
  176. (string=? (read-string p) ""))))
  177. (pass-if-equal "longer than 100 chars, with limit"
  178. "hello, world!"
  179. (let* ((s (string-concatenate (make-list 20 "hello, world!")))
  180. (p (open-input-string s)))
  181. (read-string p 13))))
  182. (with-test-prefix "read-string!"
  183. (pass-if "buf smaller"
  184. (let* ((s "hello, world!")
  185. (len (1- (string-length s)))
  186. (buf (make-string len #\.))
  187. (p (open-input-string s)))
  188. (and (= (read-string! buf p) len)
  189. (string=? buf (substring s 0 len))
  190. (= (read-string! buf p) 1)
  191. (string=? (substring buf 0 1) (substring s len)))))
  192. (pass-if "buf right size"
  193. (let* ((s "hello, world!")
  194. (len (string-length s))
  195. (buf (make-string len #\.))
  196. (p (open-input-string s)))
  197. (and (= (read-string! buf p) len)
  198. (string=? buf (substring s 0 len))
  199. (= (read-string! buf p) 0)
  200. (string=? buf (substring s 0 len)))))
  201. (pass-if "buf bigger"
  202. (let* ((s "hello, world!")
  203. (len (string-length s))
  204. (buf (make-string (1+ len) #\.))
  205. (p (open-input-string s)))
  206. (and (= (read-string! buf p) len)
  207. (string=? (substring buf 0 len) s)
  208. (= (read-string! buf p) 0)
  209. (string=? (substring buf 0 len) s)
  210. (string=? (substring buf len) ".")))))
  211. ;;; Local Variables:
  212. ;;; eval: (put 'with-test-prefix 'scheme-indent-function 1)
  213. ;;; eval: (put 'pass-if 'scheme-indent-function 1)
  214. ;;; End: