r6rs-ports.test 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611
  1. ;;;; r6rs-ports.test --- R6RS I/O port tests. -*- coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2009-2012, 2013-2015, 2018 Free Software Foundation, Inc.
  4. ;;;; Ludovic Courtès
  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-io-ports)
  20. #:use-module (test-suite lib)
  21. #:use-module (test-suite guile-test)
  22. #:use-module (srfi srfi-1)
  23. #:use-module (srfi srfi-11)
  24. #:use-module (ice-9 match)
  25. #:use-module (rnrs io ports)
  26. #:use-module (rnrs io simple)
  27. #:use-module (rnrs exceptions)
  28. #:use-module (rnrs bytevectors))
  29. (define-syntax pass-if-condition
  30. (syntax-rules ()
  31. ((_ name predicate body0 body ...)
  32. (let ((cookie (list 'cookie)))
  33. (pass-if name
  34. (eq? cookie (guard (c ((predicate c) cookie))
  35. body0 body ...)))))))
  36. (define (test-file)
  37. (data-file-name "ports-test.tmp"))
  38. ;; A input/output port that swallows all output, and produces just
  39. ;; spaces on input. Reading and writing beyond `failure-position'
  40. ;; produces `system-error' exceptions. Used for testing exception
  41. ;; behavior.
  42. (define* (make-failing-port #:optional (failure-position 0))
  43. (define (maybe-fail index errno)
  44. (if (> index failure-position)
  45. (scm-error 'system-error
  46. 'failing-port
  47. "I/O beyond failure position" '()
  48. (list errno))))
  49. (let ((read-index 0)
  50. (write-index 0))
  51. (define (write-char chr)
  52. (set! write-index (+ 1 write-index))
  53. (maybe-fail write-index ENOSPC))
  54. (make-soft-port
  55. (vector write-char
  56. (lambda (str) ;; write-string
  57. (for-each write-char (string->list str)))
  58. (lambda () #t) ;; flush-output
  59. (lambda () ;; read-char
  60. (set! read-index (+ read-index 1))
  61. (maybe-fail read-index EIO)
  62. #\space)
  63. (lambda () #t)) ;; close-port
  64. "rw")))
  65. (define (call-with-bytevector-output-port/transcoded transcoder receiver)
  66. (call-with-bytevector-output-port
  67. (lambda (bv-port)
  68. (call-with-port (transcoded-port bv-port transcoder)
  69. receiver))))
  70. (with-test-prefix "8.2.5 End-of-File Object"
  71. (pass-if "eof-object"
  72. (and (eqv? (eof-object) (eof-object))
  73. (eq? (eof-object) (eof-object))))
  74. (pass-if "port-eof?"
  75. (port-eof? (open-input-string ""))))
  76. (with-test-prefix "8.2.8 Binary Input"
  77. (pass-if "get-u8"
  78. (let ((port (open-input-string "A")))
  79. (and (= (char->integer #\A) (get-u8 port))
  80. (eof-object? (get-u8 port)))))
  81. (pass-if "lookahead-u8"
  82. (let ((port (open-input-string "A")))
  83. (and (= (char->integer #\A) (lookahead-u8 port))
  84. (= (char->integer #\A) (lookahead-u8 port))
  85. (= (char->integer #\A) (get-u8 port))
  86. (eof-object? (get-u8 port)))))
  87. (pass-if "lookahead-u8 non-ASCII"
  88. (let ((port (open-input-string "λ")))
  89. (and (= 206 (lookahead-u8 port))
  90. (= 206 (lookahead-u8 port))
  91. (= 206 (get-u8 port))
  92. (= 187 (lookahead-u8 port))
  93. (= 187 (lookahead-u8 port))
  94. (= 187 (get-u8 port))
  95. (eof-object? (lookahead-u8 port))
  96. (eof-object? (get-u8 port)))))
  97. (pass-if "lookahead-u8: result is unsigned"
  98. ;; Bug #31081.
  99. (let ((port (open-bytevector-input-port #vu8(255))))
  100. (= (lookahead-u8 port) 255)))
  101. (pass-if "get-bytevector-n [short]"
  102. (let* ((port (open-input-string "GNU Guile"))
  103. (bv (get-bytevector-n port 4)))
  104. (and (bytevector? bv)
  105. (equal? (bytevector->u8-list bv)
  106. (map char->integer (string->list "GNU "))))))
  107. (pass-if "get-bytevector-n [long]"
  108. (let* ((port (open-input-string "GNU Guile"))
  109. (bv (get-bytevector-n port 256)))
  110. (and (bytevector? bv)
  111. (equal? (bytevector->u8-list bv)
  112. (map char->integer (string->list "GNU Guile"))))))
  113. (pass-if-exception "get-bytevector-n with closed port"
  114. exception:wrong-type-arg
  115. (let ((port (%make-void-port "r")))
  116. (close-port port)
  117. (get-bytevector-n port 3)))
  118. (let ((expected (make-bytevector 20 (char->integer #\a))))
  119. (pass-if-equal "http://bugs.gnu.org/17466"
  120. ;; <http://bugs.gnu.org/17466> is about a memory corruption
  121. ;; whereas bytevector shrunk in 'get-bytevector-n' would keep
  122. ;; referring to the previous (larger) bytevector.
  123. expected
  124. (let loop ((count 50))
  125. (if (zero? count)
  126. expected
  127. (let ((bv (call-with-input-string "aaaaaaaaaaaaaaaaaaaa"
  128. (lambda (port)
  129. (get-bytevector-n port 4096)))))
  130. ;; Cause the 4 KiB bytevector initially created by
  131. ;; 'get-bytevector-n' to be reclaimed.
  132. (make-bytevector 4096)
  133. (if (equal? bv expected)
  134. (loop (- count 1))
  135. bv))))))
  136. (pass-if "get-bytevector-n! [short]"
  137. (let* ((port (open-input-string "GNU Guile"))
  138. (bv (make-bytevector 4))
  139. (read (get-bytevector-n! port bv 0 4)))
  140. (and (equal? read 4)
  141. (equal? (bytevector->u8-list bv)
  142. (map char->integer (string->list "GNU "))))))
  143. (pass-if "get-bytevector-n! [long]"
  144. (let* ((str "GNU Guile")
  145. (port (open-input-string str))
  146. (bv (make-bytevector 256))
  147. (read (get-bytevector-n! port bv 0 256)))
  148. (and (equal? read (string-length str))
  149. (equal? (map (lambda (i)
  150. (bytevector-u8-ref bv i))
  151. (iota read))
  152. (map char->integer (string->list str))))))
  153. (pass-if "get-bytevector-some [simple]"
  154. (let* ((str "GNU Guile")
  155. (port (open-input-string str))
  156. (bv (get-bytevector-some port)))
  157. (and (bytevector? bv)
  158. (equal? (bytevector->u8-list bv)
  159. (map char->integer (string->list str))))))
  160. (pass-if "get-bytevector-all"
  161. (let* ((str "GNU Guile")
  162. (index 0)
  163. (port (make-soft-port
  164. (vector #f #f #f
  165. (lambda ()
  166. (if (>= index (string-length str))
  167. (eof-object)
  168. (let ((c (string-ref str index)))
  169. (set! index (+ index 1))
  170. c)))
  171. (lambda () #t)
  172. (let ((cont? #f))
  173. (lambda ()
  174. ;; Number of readily available octets: falls to
  175. ;; zero after 4 octets have been read and then
  176. ;; starts again.
  177. (let ((a (if cont?
  178. (- (string-length str) index)
  179. (- 4 (modulo index 5)))))
  180. (if (= 0 a) (set! cont? #t))
  181. a))))
  182. "r"))
  183. (bv (get-bytevector-all port)))
  184. (and (bytevector? bv)
  185. (= index (string-length str))
  186. (= (bytevector-length bv) (string-length str))
  187. (equal? (bytevector->u8-list bv)
  188. (map char->integer (string->list str)))))))
  189. (define (make-soft-output-port)
  190. (let* ((bv (make-bytevector 1024))
  191. (read-index 0)
  192. (write-index 0)
  193. (write-char (lambda (chr)
  194. (bytevector-u8-set! bv write-index
  195. (char->integer chr))
  196. (set! write-index (+ 1 write-index)))))
  197. (make-soft-port
  198. (vector write-char
  199. (lambda (str) ;; write-string
  200. (for-each write-char (string->list str)))
  201. (lambda () #t) ;; flush-output
  202. (lambda () ;; read-char
  203. (if (>= read-index (bytevector-length bv))
  204. (eof-object)
  205. (let ((c (bytevector-u8-ref bv read-index)))
  206. (set! read-index (+ read-index 1))
  207. (integer->char c))))
  208. (lambda () #t)) ;; close-port
  209. "rw")))
  210. (with-test-prefix "8.2.11 Binary Output"
  211. (pass-if "put-u8"
  212. (let ((port (make-soft-output-port)))
  213. (put-u8 port 77)
  214. (equal? (get-u8 port) 77)))
  215. ;; Note: The `put-bytevector' tests below temporarily set the default
  216. ;; port encoding to ISO-8859-1 so that the soft-port will let all the
  217. ;; bytes through, unmodified. This is hacky, but we can't use "custom
  218. ;; binary output ports" here because they're only tested later.
  219. (pass-if "put-bytevector [2 args]"
  220. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  221. (let ((port (make-soft-output-port))
  222. (bv (make-bytevector 256)))
  223. (put-bytevector port bv)
  224. (equal? (bytevector->u8-list bv)
  225. (bytevector->u8-list
  226. (get-bytevector-n port (bytevector-length bv)))))))
  227. (pass-if "put-bytevector [3 args]"
  228. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  229. (let ((port (make-soft-output-port))
  230. (bv (make-bytevector 256))
  231. (start 10))
  232. (put-bytevector port bv start)
  233. (equal? (drop (bytevector->u8-list bv) start)
  234. (bytevector->u8-list
  235. (get-bytevector-n port (- (bytevector-length bv) start)))))))
  236. (pass-if "put-bytevector [4 args]"
  237. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  238. (let ((port (make-soft-output-port))
  239. (bv (make-bytevector 256))
  240. (start 10)
  241. (count 77))
  242. (put-bytevector port bv start count)
  243. (equal? (take (drop (bytevector->u8-list bv) start) count)
  244. (bytevector->u8-list
  245. (get-bytevector-n port count))))))
  246. (pass-if-exception "put-bytevector with closed port"
  247. exception:wrong-type-arg
  248. (let* ((bv (make-bytevector 4))
  249. (port (%make-void-port "w")))
  250. (close-port port)
  251. (put-bytevector port bv)))
  252. (pass-if "put-bytevector with UTF-16 string port"
  253. (let* ((str "hello, world")
  254. (bv (string->utf16 str)))
  255. (equal? str
  256. (call-with-output-string
  257. (lambda (port)
  258. (set-port-encoding! port "UTF-16BE")
  259. (put-bytevector port bv))))))
  260. (pass-if "put-bytevector with wrong-encoding string port"
  261. (let* ((str "hello, world")
  262. (bv (string->utf16 str)))
  263. (catch 'decoding-error
  264. (lambda ()
  265. (with-fluids ((%default-port-conversion-strategy 'error))
  266. (call-with-output-string
  267. (lambda (port)
  268. (set-port-encoding! port "UTF-32")
  269. (put-bytevector port bv)))
  270. #f)) ; fail if we reach this point
  271. (lambda (key subr message errno port)
  272. (string? (strerror errno)))))))
  273. (define (test-input-file-opener open filename)
  274. (let ((contents (string->utf8 "GNU λ")))
  275. ;; Create file
  276. (call-with-output-file filename
  277. (lambda (port) (put-bytevector port contents)))
  278. (pass-if "opens binary input port with correct contents"
  279. (with-fluids ((%default-port-encoding "UTF-8"))
  280. (call-with-port (open-file-input-port filename)
  281. (lambda (port)
  282. (and (binary-port? port)
  283. (input-port? port)
  284. (bytevector=? contents (get-bytevector-all port))))))))
  285. (delete-file filename))
  286. (with-test-prefix "8.2.7 Input Ports"
  287. (with-test-prefix "open-file-input-port"
  288. (test-input-file-opener open-file-input-port (test-file)))
  289. ;; This section appears here so that it can use the binary input
  290. ;; primitives.
  291. (pass-if "open-bytevector-input-port [1 arg]"
  292. (let* ((str "Hello Port!")
  293. (bv (u8-list->bytevector (map char->integer
  294. (string->list str))))
  295. (port (open-bytevector-input-port bv))
  296. (read-to-string
  297. (lambda (port)
  298. (let loop ((chr (read-char port))
  299. (result '()))
  300. (if (eof-object? chr)
  301. (apply string (reverse! result))
  302. (loop (read-char port)
  303. (cons chr result)))))))
  304. (equal? (read-to-string port) str)))
  305. (pass-if "bytevector-input-port is binary"
  306. (with-fluids ((%default-port-encoding "UTF-8"))
  307. (binary-port? (open-bytevector-input-port #vu8(1 2 3)))))
  308. (pass-if-equal "bytevector-input-port uses ISO-8859-1 (Guile extension)"
  309. "©©"
  310. (with-fluids ((%default-port-encoding "UTF-8"))
  311. (get-string-all (open-bytevector-input-port #vu8(194 169 194 169)))))
  312. (pass-if-exception "bytevector-input-port is read-only"
  313. exception:wrong-type-arg
  314. (let* ((str "Hello Port!")
  315. (bv (u8-list->bytevector (map char->integer
  316. (string->list str))))
  317. (port (open-bytevector-input-port bv #f)))
  318. (write "hello" port)))
  319. (pass-if "bytevector input port supports seeking"
  320. (let* ((str "Hello Port!")
  321. (bv (u8-list->bytevector (map char->integer
  322. (string->list str))))
  323. (port (open-bytevector-input-port bv #f)))
  324. (and (port-has-port-position? port)
  325. (= 0 (port-position port))
  326. (port-has-set-port-position!? port)
  327. (begin
  328. (set-port-position! port 6)
  329. (= 6 (port-position port)))
  330. (bytevector=? (get-bytevector-all port)
  331. (u8-list->bytevector
  332. (map char->integer (string->list "Port!")))))))
  333. (pass-if "bytevector input port can seek to very end"
  334. (let ((empty (open-bytevector-input-port '#vu8()))
  335. (not-empty (open-bytevector-input-port '#vu8(1 2 3))))
  336. (and (begin (set-port-position! empty (port-position empty))
  337. (= 0 (port-position empty)))
  338. (begin (get-bytevector-n not-empty 3)
  339. (set-port-position! not-empty (port-position not-empty))
  340. (= 3 (port-position not-empty))))))
  341. (pass-if-exception "make-custom-binary-input-port [wrong-num-args]"
  342. exception:wrong-num-args
  343. ;; Prior to Guile-R6RS-Libs 0.2, the last 3 arguments were wrongfully
  344. ;; optional.
  345. (make-custom-binary-input-port "port" (lambda args #t)))
  346. (pass-if "make-custom-binary-input-port"
  347. (let* ((source (make-bytevector 7777))
  348. (read! (let ((pos 0)
  349. (len (bytevector-length source)))
  350. (lambda (bv start count)
  351. (let ((amount (min count (- len pos))))
  352. (if (> amount 0)
  353. (bytevector-copy! source pos
  354. bv start amount))
  355. (set! pos (+ pos amount))
  356. amount))))
  357. (port (make-custom-binary-input-port "the port" read!
  358. #f #f #f)))
  359. (and (binary-port? port)
  360. (input-port? port)
  361. (bytevector=? (get-bytevector-all port) source))))
  362. (pass-if-equal "make-custom-binary-input-port uses ISO-8859-1 (Guile extension)"
  363. "©©"
  364. (with-fluids ((%default-port-encoding "UTF-8"))
  365. (let* ((source #vu8(194 169 194 169))
  366. (read! (let ((pos 0)
  367. (len (bytevector-length source)))
  368. (lambda (bv start count)
  369. (let ((amount (min count (- len pos))))
  370. (if (> amount 0)
  371. (bytevector-copy! source pos
  372. bv start amount))
  373. (set! pos (+ pos amount))
  374. amount))))
  375. (port (make-custom-binary-input-port "the port" read!
  376. #f #f #f)))
  377. (get-string-all port))))
  378. (pass-if "custom binary input port does not support `port-position'"
  379. (let* ((str "Hello Port!")
  380. (source (open-bytevector-input-port
  381. (u8-list->bytevector
  382. (map char->integer (string->list str)))))
  383. (read! (lambda (bv start count)
  384. (let ((r (get-bytevector-n! source bv start count)))
  385. (if (eof-object? r)
  386. 0
  387. r))))
  388. (port (make-custom-binary-input-port "the port" read!
  389. #f #f #f)))
  390. (not (or (port-has-port-position? port)
  391. (port-has-set-port-position!? port)))))
  392. (pass-if-exception "custom binary input port 'read!' returns too much"
  393. exception:out-of-range
  394. ;; In Guile <= 2.0.9 this would segfault.
  395. (let* ((read! (lambda (bv start count)
  396. (+ count 4242)))
  397. (port (make-custom-binary-input-port "the port" read!
  398. #f #f #f)))
  399. (get-bytevector-all port)))
  400. (pass-if-equal "custom binary input port supports `port-position', \
  401. not `set-port-position!'"
  402. 42
  403. (let ((port (make-custom-binary-input-port "the port" (const 0)
  404. (const 42) #f #f)))
  405. (and (port-has-port-position? port)
  406. (not (port-has-set-port-position!? port))
  407. (port-position port))))
  408. (pass-if "custom binary input port supports `port-position'"
  409. (let* ((str "Hello Port!")
  410. (source (open-bytevector-input-port
  411. (u8-list->bytevector
  412. (map char->integer (string->list str)))))
  413. (read! (lambda (bv start count)
  414. (let ((r (get-bytevector-n! source bv start count)))
  415. (if (eof-object? r)
  416. 0
  417. r))))
  418. (get-pos (lambda ()
  419. (port-position source)))
  420. (set-pos! (lambda (pos)
  421. (set-port-position! source pos)))
  422. (port (make-custom-binary-input-port "the port" read!
  423. get-pos set-pos! #f)))
  424. (and (port-has-port-position? port)
  425. (= 0 (port-position port))
  426. (port-has-set-port-position!? port)
  427. (begin
  428. (set-port-position! port 6)
  429. (= 6 (port-position port)))
  430. (bytevector=? (get-bytevector-all port)
  431. (u8-list->bytevector
  432. (map char->integer (string->list "Port!")))))))
  433. (pass-if-equal "custom binary input port position, long offset"
  434. (expt 2 42)
  435. ;; In Guile <= 2.2.4, 'seek' would throw to 'out-of-range'.
  436. (let* ((port (make-custom-binary-input-port "the port"
  437. (const 0)
  438. (const (expt 2 42))
  439. #f #f)))
  440. (port-position port)))
  441. (pass-if-equal "custom binary input port buffered partial reads"
  442. "Hello Port!"
  443. ;; Check what happens when READ! returns less than COUNT bytes.
  444. (let* ((src (string->utf8 "Hello Port!"))
  445. (chunks '(2 4 5)) ; provide 2 bytes, then 4, etc.
  446. (offset 0)
  447. (read! (lambda (bv start count)
  448. (match chunks
  449. ((count rest ...)
  450. (bytevector-copy! src offset bv start count)
  451. (set! chunks rest)
  452. (set! offset (+ offset count))
  453. count)
  454. (()
  455. 0))))
  456. (port (make-custom-binary-input-port "the port"
  457. read! #f #f #f)))
  458. (get-string-all port)))
  459. (pass-if-equal "custom binary input port unbuffered & 'port-position'"
  460. '(0 2 5 11)
  461. ;; Check that the value returned by 'port-position' is correct, and
  462. ;; that each 'port-position' call leads one call to the
  463. ;; 'get-position' method.
  464. (let* ((str "Hello Port!")
  465. (output (make-bytevector (string-length str)))
  466. (source (with-fluids ((%default-port-encoding "UTF-8"))
  467. (open-string-input-port str)))
  468. (read! (lambda (bv start count)
  469. (let ((r (get-bytevector-n! source bv start count)))
  470. (if (eof-object? r)
  471. 0
  472. r))))
  473. (pos '())
  474. (get-pos (lambda ()
  475. (let ((p (port-position source)))
  476. (set! pos (cons p pos))
  477. p)))
  478. (port (make-custom-binary-input-port "the port" read!
  479. get-pos #f #f)))
  480. (setvbuf port 'none)
  481. (and (= 0 (port-position port))
  482. (begin
  483. (get-bytevector-n! port output 0 2)
  484. (= 2 (port-position port)))
  485. (begin
  486. (get-bytevector-n! port output 2 3)
  487. (= 5 (port-position port)))
  488. (let ((bv (string->utf8 (get-string-all port))))
  489. (bytevector-copy! bv 0 output 5 (bytevector-length bv))
  490. (= (string-length str) (port-position port)))
  491. (bytevector=? output (string->utf8 str))
  492. (reverse pos))))
  493. (pass-if-equal "custom binary input port unbuffered & 'read!' calls"
  494. `((2 "He") (3 "llo") (42 " Port!"))
  495. (let* ((str "Hello Port!")
  496. (source (with-fluids ((%default-port-encoding "UTF-8"))
  497. (open-string-input-port str)))
  498. (reads '())
  499. (read! (lambda (bv start count)
  500. (set! reads (cons count reads))
  501. (let ((r (get-bytevector-n! source bv start count)))
  502. (if (eof-object? r)
  503. 0
  504. r))))
  505. (port (make-custom-binary-input-port "the port" read!
  506. #f #f #f)))
  507. (setvbuf port 'none)
  508. (let ((ret (list (get-bytevector-n port 2)
  509. (get-bytevector-n port 3)
  510. (get-bytevector-n port 42))))
  511. (zip (reverse reads)
  512. (map (lambda (obj)
  513. (if (bytevector? obj)
  514. (utf8->string obj)
  515. obj))
  516. ret)))))
  517. (pass-if-equal "custom binary input port unbuffered & 'get-string-all'"
  518. (make-string 1000 #\a)
  519. ;; In Guile 2.0.11 this test would lead to a buffer overrun followed
  520. ;; by an assertion failure. See <http://bugs.gnu.org/19621>.
  521. (let* ((input (with-fluids ((%default-port-encoding #f))
  522. (open-input-string (make-string 1000 #\a))))
  523. (read! (lambda (bv index count)
  524. (let ((n (get-bytevector-n! input bv index
  525. count)))
  526. (if (eof-object? n) 0 n))))
  527. (port (make-custom-binary-input-port "foo" read!
  528. #f #f #f)))
  529. (setvbuf port 'none)
  530. (get-string-all port)))
  531. (pass-if-equal "custom binary input port unbuffered UTF-8 & 'get-string-all'"
  532. (make-string 1000 #\λ)
  533. ;; In Guile 2.0.11 this test would lead to a buffer overrun followed
  534. ;; by an assertion failure. See <http://bugs.gnu.org/19621>.
  535. (let* ((input (with-fluids ((%default-port-encoding "UTF-8"))
  536. (open-input-string (make-string 1000 #\λ))))
  537. (read! (lambda (bv index count)
  538. (let ((n (get-bytevector-n! input bv index
  539. count)))
  540. (if (eof-object? n) 0 n))))
  541. (port (make-custom-binary-input-port "foo" read!
  542. #f #f #f)))
  543. (setvbuf port 'none)
  544. (set-port-encoding! port "UTF-8")
  545. (get-string-all port)))
  546. (pass-if-equal "custom binary input port, unbuffered then buffered"
  547. `((6 "Lorem ") (12 "ipsum dolor ") (777 "sit amet, consectetur…")
  548. (777 ,(eof-object)))
  549. (let* ((str "Lorem ipsum dolor sit amet, consectetur…")
  550. (source (with-fluids ((%default-port-encoding "UTF-8"))
  551. (open-string-input-port str)))
  552. (reads '())
  553. (read! (lambda (bv start count)
  554. (set! reads (cons count reads))
  555. (let ((r (get-bytevector-n! source bv start count)))
  556. (if (eof-object? r)
  557. 0
  558. r))))
  559. (port (make-custom-binary-input-port "the port" read!
  560. #f #f #f)))
  561. (setvbuf port 'none)
  562. (let ((ret (list (get-bytevector-n port 6)
  563. (get-bytevector-n port 12)
  564. (begin
  565. (setvbuf port 'block 777)
  566. (get-bytevector-n port 42))
  567. (get-bytevector-n port 42))))
  568. (zip (reverse reads)
  569. (map (lambda (obj)
  570. (if (bytevector? obj)
  571. (utf8->string obj)
  572. obj))
  573. ret)))))
  574. (pass-if-equal "custom binary input port, buffered then unbuffered"
  575. `((18
  576. 42 14 ; scm_c_read tries to fill the 42-byte buffer
  577. 42)
  578. ("Lorem " "ipsum dolor " "sit amet, consectetur bla…" ,(eof-object)))
  579. (let* ((str "Lorem ipsum dolor sit amet, consectetur bla…")
  580. (source (with-fluids ((%default-port-encoding "UTF-8"))
  581. (open-string-input-port str)))
  582. (reads '())
  583. (read! (lambda (bv start count)
  584. (set! reads (cons count reads))
  585. (let ((r (get-bytevector-n! source bv start count)))
  586. (if (eof-object? r)
  587. 0
  588. r))))
  589. (port (make-custom-binary-input-port "the port" read!
  590. #f #f #f)))
  591. (setvbuf port 'block 18)
  592. (let ((ret (list (get-bytevector-n port 6)
  593. (get-bytevector-n port 12)
  594. (begin
  595. (setvbuf port 'none)
  596. (get-bytevector-n port 42))
  597. (get-bytevector-n port 42))))
  598. (list (reverse reads)
  599. (map (lambda (obj)
  600. (if (bytevector? obj)
  601. (utf8->string obj)
  602. obj))
  603. ret)))))
  604. (pass-if "custom binary input port `close-proc' is called"
  605. (let* ((closed? #f)
  606. (read! (lambda (bv start count) 0))
  607. (get-pos (lambda () 0))
  608. (set-pos! (lambda (pos) #f))
  609. (close! (lambda () (set! closed? #t)))
  610. (port (make-custom-binary-input-port "the port" read!
  611. get-pos set-pos!
  612. close!)))
  613. (close-port port)
  614. (gc) ; Test for marking a closed port.
  615. closed?))
  616. (pass-if "standard-input-port is binary"
  617. (with-fluids ((%default-port-encoding "UTF-8"))
  618. (binary-port? (standard-input-port)))))
  619. (define (test-output-file-opener open filename)
  620. (with-fluids ((%default-port-encoding "UTF-8"))
  621. (pass-if "opens binary output port"
  622. (call-with-port (open filename)
  623. (lambda (port)
  624. (put-bytevector port '#vu8(1 2 3))
  625. (and (binary-port? port)
  626. (output-port? port))))))
  627. (pass-if-condition "exception: already-exists"
  628. i/o-file-already-exists-error?
  629. (open filename))
  630. (pass-if "no-fail no-truncate"
  631. (and
  632. (call-with-port (open filename (file-options no-fail no-truncate))
  633. (lambda (port)
  634. (= 0 (port-position port))))
  635. (= 3 (stat:size (stat filename)))))
  636. (pass-if "no-fail"
  637. (and
  638. (call-with-port (open filename (file-options no-fail))
  639. binary-port?)
  640. (= 0 (stat:size (stat filename)))))
  641. (pass-if "buffer-mode none"
  642. (call-with-port (open filename (file-options no-fail)
  643. (buffer-mode none))
  644. (lambda (port)
  645. (eq? (output-port-buffer-mode port) 'none))))
  646. (pass-if "buffer-mode line"
  647. (call-with-port (open filename (file-options no-fail)
  648. (buffer-mode line))
  649. (lambda (port)
  650. (eq? (output-port-buffer-mode port) 'line))))
  651. (pass-if "buffer-mode block"
  652. (call-with-port (open filename (file-options no-fail)
  653. (buffer-mode block))
  654. (lambda (port)
  655. (eq? (output-port-buffer-mode port) 'block))))
  656. (delete-file filename)
  657. (pass-if-condition "exception: does-not-exist"
  658. i/o-file-does-not-exist-error?
  659. (open filename (file-options no-create))))
  660. (with-test-prefix "8.2.10 Output ports"
  661. (with-test-prefix "open-file-output-port"
  662. (test-output-file-opener open-file-output-port (test-file)))
  663. (pass-if "open-string-output-port"
  664. (call-with-values open-string-output-port
  665. (lambda (port proc)
  666. (and (port? port) (thunk? proc)))))
  667. (pass-if-equal "calling string output port truncates port"
  668. '("hello" "" "world")
  669. (call-with-values open-string-output-port
  670. (lambda (port proc)
  671. (display "hello" port)
  672. (let* ((s1 (proc))
  673. (s2 (proc)))
  674. (display "world" port)
  675. (list s1 s2 (proc))))))
  676. (pass-if "open-bytevector-output-port"
  677. (let-values (((port get-content)
  678. (open-bytevector-output-port #f)))
  679. (let ((source (make-bytevector 7777)))
  680. (put-bytevector port source)
  681. (and (bytevector=? (get-content) source)
  682. (bytevector=? (get-content) (make-bytevector 0))))))
  683. (pass-if "bytevector-output-port is binary"
  684. (binary-port? (open-bytevector-output-port)))
  685. (pass-if-equal "bytevector-output-port uses ISO-8859-1 (Guile extension)"
  686. #vu8(194 169 194 169)
  687. (with-fluids ((%default-port-encoding "UTF-8"))
  688. (let-values (((port get-content)
  689. (open-bytevector-output-port)))
  690. (put-string port "©©")
  691. (get-content))))
  692. (pass-if "open-bytevector-output-port [extract after close]"
  693. (let-values (((port get-content)
  694. (open-bytevector-output-port)))
  695. (let ((source (make-bytevector 12345 #xFE)))
  696. (put-bytevector port source)
  697. (close-port port)
  698. (bytevector=? (get-content) source))))
  699. (pass-if "open-bytevector-output-port [put-u8]"
  700. (let-values (((port get-content)
  701. (open-bytevector-output-port)))
  702. (put-u8 port 77)
  703. (and (bytevector=? (get-content) (make-bytevector 1 77))
  704. (bytevector=? (get-content) (make-bytevector 0)))))
  705. (pass-if "open-bytevector-output-port [display]"
  706. (let-values (((port get-content)
  707. (open-bytevector-output-port)))
  708. (display "hello" port)
  709. (and (bytevector=? (get-content) (string->utf8 "hello"))
  710. (bytevector=? (get-content) (make-bytevector 0)))))
  711. (pass-if "bytevector output port supports `port-position'"
  712. (let-values (((port get-content)
  713. (open-bytevector-output-port)))
  714. (let ((source (make-bytevector 7777))
  715. (overwrite (make-bytevector 33)))
  716. (and (port-has-port-position? port)
  717. (port-has-set-port-position!? port)
  718. (begin
  719. (put-bytevector port source)
  720. (= (bytevector-length source)
  721. (port-position port)))
  722. (begin
  723. (set-port-position! port 10)
  724. (= 10 (port-position port)))
  725. (begin
  726. (put-bytevector port overwrite)
  727. (bytevector-copy! overwrite 0 source 10
  728. (bytevector-length overwrite))
  729. (= (port-position port)
  730. (+ 10 (bytevector-length overwrite))))
  731. (bytevector=? (get-content) source)
  732. (bytevector=? (get-content) (make-bytevector 0))))))
  733. (pass-if "make-custom-binary-output-port"
  734. (let ((port (make-custom-binary-output-port "cbop"
  735. (lambda (x y z) 0)
  736. #f #f #f)))
  737. (and (output-port? port)
  738. (binary-port? port)
  739. (not (port-has-port-position? port))
  740. (not (port-has-set-port-position!? port)))))
  741. (pass-if "make-custom-binary-output-port [partial writes]"
  742. (let* ((source (uint-list->bytevector (iota 333)
  743. (native-endianness) 2))
  744. (sink (make-bytevector (bytevector-length source)))
  745. (sink-pos 0)
  746. (eof? #f)
  747. (write! (lambda (bv start count)
  748. (if (= 0 count)
  749. (begin
  750. (set! eof? #t)
  751. 0)
  752. (let ((u8 (bytevector-u8-ref bv start)))
  753. ;; Get one byte at a time.
  754. (bytevector-u8-set! sink sink-pos u8)
  755. (set! sink-pos (+ 1 sink-pos))
  756. 1))))
  757. (port (make-custom-binary-output-port "cbop" write!
  758. #f #f #f)))
  759. (put-bytevector port source)
  760. (force-output port)
  761. (and (= sink-pos (bytevector-length source))
  762. (not eof?)
  763. (bytevector=? sink source))))
  764. (pass-if "make-custom-binary-output-port [full writes]"
  765. (let* ((source (uint-list->bytevector (iota 333)
  766. (native-endianness) 2))
  767. (sink (make-bytevector (bytevector-length source)))
  768. (sink-pos 0)
  769. (eof? #f)
  770. (write! (lambda (bv start count)
  771. (if (= 0 count)
  772. (begin
  773. (set! eof? #t)
  774. 0)
  775. (begin
  776. (bytevector-copy! bv start
  777. sink sink-pos
  778. count)
  779. (set! sink-pos (+ sink-pos count))
  780. count))))
  781. (port (make-custom-binary-output-port "cbop" write!
  782. #f #f #f)))
  783. (put-bytevector port source)
  784. (force-output port)
  785. (and (= sink-pos (bytevector-length source))
  786. (not eof?)
  787. (bytevector=? sink source))))
  788. (pass-if-equal "custom-binary-output-port uses ISO-8859-1 (Guile extension)"
  789. '(194 169 194 169)
  790. (with-fluids ((%default-port-encoding "UTF-8"))
  791. (let* ((sink '())
  792. (write! (lambda (bv start count)
  793. (if (= 0 count) ; EOF
  794. 0
  795. (let ((u8 (bytevector-u8-ref bv start)))
  796. ;; Get one byte at a time.
  797. (set! sink (cons u8 sink))
  798. 1))))
  799. (port (make-custom-binary-output-port "cbop" write!
  800. #f #f #f)))
  801. (put-string port "©©")
  802. (force-output port)
  803. (reverse sink))))
  804. (pass-if "standard-output-port is binary"
  805. (with-fluids ((%default-port-encoding "UTF-8"))
  806. (binary-port? (standard-output-port))))
  807. (pass-if "standard-error-port is binary"
  808. (with-fluids ((%default-port-encoding "UTF-8"))
  809. (binary-port? (standard-error-port)))))
  810. (with-test-prefix "8.2.6 Input and output ports"
  811. (pass-if "transcoded-port [output]"
  812. (let ((s "Hello\nÄÖÜ"))
  813. (bytevector=?
  814. (string->utf8 s)
  815. (call-with-bytevector-output-port/transcoded (make-transcoder (utf-8-codec))
  816. (lambda (utf8-port)
  817. (put-string utf8-port s))))))
  818. (pass-if "transcoded-port [input]"
  819. (let ((s "Hello\nÄÖÜ"))
  820. (string=?
  821. s
  822. (get-string-all
  823. (transcoded-port (open-bytevector-input-port (string->utf8 s))
  824. (make-transcoder (utf-8-codec)))))))
  825. (pass-if "transcoded-port [input line]"
  826. (string=? "ÄÖÜ"
  827. (get-line (transcoded-port
  828. (open-bytevector-input-port (string->utf8 "ÄÖÜ\nFooBar"))
  829. (make-transcoder (utf-8-codec))))))
  830. (pass-if "transcoded-port [error handling mode = raise]"
  831. (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
  832. (error-handling-mode raise)))
  833. (b (open-bytevector-input-port #vu8(255 2 1)))
  834. (tp (transcoded-port b t)))
  835. (guard (c ((i/o-decoding-error? c)
  836. (eq? (i/o-error-port c) tp)))
  837. (get-line tp)
  838. #f))) ; fail if we reach this point
  839. (pass-if "transcoded-port [error handling mode = replace]"
  840. (let* ((t (make-transcoder (utf-8-codec) (native-eol-style)
  841. (error-handling-mode replace)))
  842. (b (open-bytevector-input-port #vu8(255 1 2 3 103 110 117)))
  843. (tp (transcoded-port b t)))
  844. (string-suffix? "gnu" (get-line tp))))
  845. (pass-if "transcoded-port, output [error handling mode = raise]"
  846. (let-values (((p get)
  847. (open-bytevector-output-port)))
  848. (let* ((t (make-transcoder (latin-1-codec) (native-eol-style)
  849. (error-handling-mode raise)))
  850. (tp (transcoded-port p t)))
  851. (setvbuf tp 'none)
  852. (guard (c ((i/o-encoding-error? c)
  853. (and (eq? (i/o-error-port c) tp)
  854. (char=? (i/o-encoding-error-char c) #\λ)
  855. (bytevector=? (get) (string->utf8 "The letter ")))))
  856. (put-string tp "The letter λ cannot be represented in Latin-1.")
  857. #f))))
  858. (pass-if "port-transcoder [transcoded port]"
  859. (let* ((p (transcoded-port (open-bytevector-input-port (string->utf8 "foo"))
  860. (make-transcoder (utf-8-codec))))
  861. (t (port-transcoder p)))
  862. (and t
  863. (transcoder-codec t)
  864. (eq? (native-eol-style)
  865. (transcoder-eol-style t))
  866. (eq? (error-handling-mode replace)
  867. (transcoder-error-handling-mode t))))))
  868. (with-test-prefix "8.2.9 Textual input"
  869. (pass-if "get-string-n [short]"
  870. (let ((port (open-input-string "GNU Guile")))
  871. (string=? "GNU " (get-string-n port 4))))
  872. (pass-if "get-string-n [long]"
  873. (let ((port (open-input-string "GNU Guile")))
  874. (string=? "GNU Guile" (get-string-n port 256))))
  875. (pass-if "get-string-n [eof]"
  876. (let ((port (open-input-string "")))
  877. (eof-object? (get-string-n port 4))))
  878. (pass-if "get-string-n! [short]"
  879. (let ((port (open-input-string "GNU Guile"))
  880. (s (string-copy "Isn't XXX great?")))
  881. (and (= 3 (get-string-n! port s 6 3))
  882. (string=? s "Isn't GNU great?"))))
  883. (with-test-prefix "read error"
  884. (pass-if-condition "get-char" i/o-read-error?
  885. (get-char (make-failing-port)))
  886. (pass-if-condition "lookahead-char" i/o-read-error?
  887. (lookahead-char (make-failing-port)))
  888. ;; FIXME: these are not yet exception-correct
  889. #|
  890. (pass-if-condition "get-string-n" i/o-read-error?
  891. (get-string-n (make-failing-port) 5))
  892. (pass-if-condition "get-string-n!" i/o-read-error?
  893. (get-string-n! (make-failing-port) (make-string 5) 0 5))
  894. |#
  895. (pass-if-condition "get-string-all" i/o-read-error?
  896. (get-string-all (make-failing-port 100)))
  897. (pass-if-condition "get-line" i/o-read-error?
  898. (get-line (make-failing-port)))
  899. (pass-if-condition "get-datum" i/o-read-error?
  900. (get-datum (make-failing-port)))))
  901. (define (encoding-error-predicate char)
  902. (lambda (c)
  903. (and (i/o-encoding-error? c)
  904. (char=? char (i/o-encoding-error-char c)))))
  905. (with-test-prefix "8.2.12 Textual Output"
  906. (with-test-prefix "write error"
  907. (pass-if-condition "put-char" i/o-write-error?
  908. (put-char (make-failing-port) #\G))
  909. (pass-if-condition "put-string" i/o-write-error?
  910. (put-string (make-failing-port) "Hello World!"))
  911. (pass-if-condition "put-datum" i/o-write-error?
  912. (put-datum (make-failing-port) '(hello world!))))
  913. (with-test-prefix "encoding error"
  914. (pass-if-condition "put-char" (encoding-error-predicate #\λ)
  915. (call-with-bytevector-output-port/transcoded
  916. (make-transcoder (latin-1-codec)
  917. (native-eol-style)
  918. (error-handling-mode raise))
  919. (lambda (port)
  920. (put-char port #\λ))))
  921. (pass-if-condition "put-string" (encoding-error-predicate #\λ)
  922. (call-with-bytevector-output-port/transcoded
  923. (make-transcoder (latin-1-codec)
  924. (native-eol-style)
  925. (error-handling-mode raise))
  926. (lambda (port)
  927. (put-string port "FooλBar"))))))
  928. (with-test-prefix "8.3 Simple I/O"
  929. (with-test-prefix "read error"
  930. (pass-if-condition "read-char" i/o-read-error?
  931. (read-char (make-failing-port)))
  932. (pass-if-condition "peek-char" i/o-read-error?
  933. (peek-char (make-failing-port)))
  934. (pass-if-condition "read" i/o-read-error?
  935. (read (make-failing-port))))
  936. (with-test-prefix "write error"
  937. (pass-if-condition "display" i/o-write-error?
  938. (display "Hi there!" (make-failing-port)))
  939. (pass-if-condition "write" i/o-write-error?
  940. (write '(hi there!) (make-failing-port)))
  941. (pass-if-condition "write-char" i/o-write-error?
  942. (write-char #\G (make-failing-port)))
  943. (pass-if-condition "newline" i/o-write-error?
  944. (newline (make-failing-port))))
  945. (let ((filename (test-file)))
  946. ;; ensure the test file exists
  947. (call-with-output-file filename
  948. (lambda (port) (write "foo" port)))
  949. (pass-if "call-with-input-file [port is textual]"
  950. (call-with-input-file filename textual-port?))
  951. (pass-if-condition "call-with-input-file [exception: not-found]"
  952. i/o-file-does-not-exist-error?
  953. (call-with-input-file ",this-is-highly-unlikely-to-exist!"
  954. values))
  955. (pass-if-condition "call-with-output-file [exception: already-exists]"
  956. i/o-file-already-exists-error?
  957. (call-with-output-file filename
  958. values))
  959. (delete-file filename)))
  960. ;; Used for a lot of the make-custom-input/output tests to stub out
  961. ;; the read/write section for whatever part we're ignoring
  962. (define dummy-write! (const 0))
  963. (define dummy-read! (const 0))
  964. (with-test-prefix "8.2.13 Input/output ports"
  965. (with-test-prefix "open-file-input/output-port [output]"
  966. (test-output-file-opener open-file-input/output-port (test-file)))
  967. (with-test-prefix "open-file-input/output-port [input]"
  968. (test-input-file-opener open-file-input/output-port (test-file)))
  969. ;; Custom binary input/output tests. Most of these are simple
  970. ;; ports of the custom-binary-input-port tests or custom-binary-ouput-port
  971. ;; tests, simply ported to use a custom-binary-input/output port.
  972. ;; The copy-pasta is strong here; a diet lighter in spaghetti may wish
  973. ;; to make the previous tests more reusable.
  974. (pass-if "make-custom-binary-input/output-port"
  975. (let* ((source (make-bytevector 7777))
  976. (read! (let ((pos 0)
  977. (len (bytevector-length source)))
  978. (lambda (bv start count)
  979. (let ((amount (min count (- len pos))))
  980. (if (> amount 0)
  981. (bytevector-copy! source pos
  982. bv start amount))
  983. (set! pos (+ pos amount))
  984. amount))))
  985. (write! (lambda (x y z) 0))
  986. (port (make-custom-binary-input/output-port
  987. "the port" read! write!
  988. #f #f #f)))
  989. (and (binary-port? port)
  990. (input-port? port)
  991. (output-port? port)
  992. (bytevector=? (get-bytevector-all port) source)
  993. (not (port-has-port-position? port))
  994. (not (port-has-set-port-position!? port)))))
  995. (pass-if-equal "make-custom-binary-input/output-port uses ISO-8859-1 (Guile \
  996. extension) [input]"
  997. "©©"
  998. (with-fluids ((%default-port-encoding "UTF-8"))
  999. (let* ((source #vu8(194 169 194 169))
  1000. (read! (let ((pos 0)
  1001. (len (bytevector-length source)))
  1002. (lambda (bv start count)
  1003. (let ((amount (min count (- len pos))))
  1004. (if (> amount 0)
  1005. (bytevector-copy! source pos
  1006. bv start amount))
  1007. (set! pos (+ pos amount))
  1008. amount))))
  1009. (port (make-custom-binary-input/output-port
  1010. "the port" read! dummy-write!
  1011. #f #f #f)))
  1012. (get-string-all port))))
  1013. (pass-if "custom binary input/output port does not support `port-position'"
  1014. (let* ((str "Hello Port!")
  1015. (source (open-bytevector-input-port
  1016. (u8-list->bytevector
  1017. (map char->integer (string->list str)))))
  1018. (read! (lambda (bv start count)
  1019. (let ((r (get-bytevector-n! source bv start count)))
  1020. (if (eof-object? r)
  1021. 0
  1022. r))))
  1023. (port (make-custom-binary-input/output-port
  1024. "the port" read! dummy-write!
  1025. #f #f #f)))
  1026. (not (or (port-has-port-position? port)
  1027. (port-has-set-port-position!? port)))))
  1028. (pass-if-exception "custom binary input/output port 'read!' returns too much"
  1029. exception:out-of-range
  1030. ;; In Guile <= 2.0.9 this would segfault.
  1031. (let* ((read! (lambda (bv start count)
  1032. (+ count 4242)))
  1033. (port (make-custom-binary-input/output-port
  1034. "the port" read! dummy-write!
  1035. #f #f #f)))
  1036. (get-bytevector-all port)))
  1037. (pass-if-equal "custom binary input/output port supports `port-position', \
  1038. not `set-port-position!'"
  1039. 42
  1040. (let ((port (make-custom-binary-input/output-port
  1041. "the port" (const 0) dummy-write!
  1042. (const 42) #f #f)))
  1043. (and (port-has-port-position? port)
  1044. (not (port-has-set-port-position!? port))
  1045. (port-position port))))
  1046. (pass-if "custom binary input/output port supports `port-position'"
  1047. (let* ((str "Hello Port!")
  1048. (source (open-bytevector-input-port
  1049. (u8-list->bytevector
  1050. (map char->integer (string->list str)))))
  1051. (read! (lambda (bv start count)
  1052. (let ((r (get-bytevector-n! source bv start count)))
  1053. (if (eof-object? r)
  1054. 0
  1055. r))))
  1056. (get-pos (lambda ()
  1057. (port-position source)))
  1058. (set-pos! (lambda (pos)
  1059. (set-port-position! source pos)))
  1060. (port (make-custom-binary-input/output-port
  1061. "the port" read! dummy-write!
  1062. get-pos set-pos! #f)))
  1063. (and (port-has-port-position? port)
  1064. (= 0 (port-position port))
  1065. (port-has-set-port-position!? port)
  1066. (begin
  1067. (set-port-position! port 6)
  1068. (= 6 (port-position port)))
  1069. (bytevector=? (get-bytevector-all port)
  1070. (u8-list->bytevector
  1071. (map char->integer (string->list "Port!")))))))
  1072. (pass-if-equal "custom binary input/output port buffered partial reads"
  1073. "Hello Port!"
  1074. ;; Check what happens when READ! returns less than COUNT bytes.
  1075. (let* ((src (string->utf8 "Hello Port!"))
  1076. (chunks '(2 4 5)) ; provide 2 bytes, then 4, etc.
  1077. (offset 0)
  1078. (read! (lambda (bv start count)
  1079. (match chunks
  1080. ((count rest ...)
  1081. (bytevector-copy! src offset bv start count)
  1082. (set! chunks rest)
  1083. (set! offset (+ offset count))
  1084. count)
  1085. (()
  1086. 0))))
  1087. (port (make-custom-binary-input/output-port
  1088. "the port" read! dummy-write!
  1089. #f #f #f)))
  1090. (get-string-all port)))
  1091. (pass-if-equal "custom binary input/output port unbuffered & 'port-position'"
  1092. '(0 2 5 11)
  1093. ;; Check that the value returned by 'port-position' is correct, and
  1094. ;; that each 'port-position' call leads one call to the
  1095. ;; 'get-position' method.
  1096. (let* ((str "Hello Port!")
  1097. (output (make-bytevector (string-length str)))
  1098. (source (with-fluids ((%default-port-encoding "UTF-8"))
  1099. (open-string-input-port str)))
  1100. (read! (lambda (bv start count)
  1101. (let ((r (get-bytevector-n! source bv start count)))
  1102. (if (eof-object? r)
  1103. 0
  1104. r))))
  1105. (pos '())
  1106. (get-pos (lambda ()
  1107. (let ((p (port-position source)))
  1108. (set! pos (cons p pos))
  1109. p)))
  1110. (port (make-custom-binary-input/output-port
  1111. "the port" read! dummy-write!
  1112. get-pos #f #f)))
  1113. (setvbuf port 'none)
  1114. (and (= 0 (port-position port))
  1115. (begin
  1116. (get-bytevector-n! port output 0 2)
  1117. (= 2 (port-position port)))
  1118. (begin
  1119. (get-bytevector-n! port output 2 3)
  1120. (= 5 (port-position port)))
  1121. (let ((bv (string->utf8 (get-string-all port))))
  1122. (bytevector-copy! bv 0 output 5 (bytevector-length bv))
  1123. (= (string-length str) (port-position port)))
  1124. (bytevector=? output (string->utf8 str))
  1125. (reverse pos))))
  1126. (pass-if-equal "custom binary input/output port unbuffered & 'read!' calls"
  1127. `((2 "He") (3 "llo") (42 " Port!"))
  1128. (let* ((str "Hello Port!")
  1129. (source (with-fluids ((%default-port-encoding "UTF-8"))
  1130. (open-string-input-port str)))
  1131. (reads '())
  1132. (read! (lambda (bv start count)
  1133. (set! reads (cons count reads))
  1134. (let ((r (get-bytevector-n! source bv start count)))
  1135. (if (eof-object? r)
  1136. 0
  1137. r))))
  1138. (port (make-custom-binary-input/output-port
  1139. "the port" read! dummy-write!
  1140. #f #f #f)))
  1141. (setvbuf port 'none)
  1142. (let ((ret (list (get-bytevector-n port 2)
  1143. (get-bytevector-n port 3)
  1144. (get-bytevector-n port 42))))
  1145. (zip (reverse reads)
  1146. (map (lambda (obj)
  1147. (if (bytevector? obj)
  1148. (utf8->string obj)
  1149. obj))
  1150. ret)))))
  1151. (pass-if-equal "custom binary input/output port unbuffered & 'get-string-all'"
  1152. (make-string 1000 #\a)
  1153. ;; In Guile 2.0.11 this test would lead to a buffer overrun followed
  1154. ;; by an assertion failure. See <http://bugs.gnu.org/19621>.
  1155. (let* ((input (with-fluids ((%default-port-encoding #f))
  1156. (open-input-string (make-string 1000 #\a))))
  1157. (read! (lambda (bv index count)
  1158. (let ((n (get-bytevector-n! input bv index
  1159. count)))
  1160. (if (eof-object? n) 0 n))))
  1161. (port (make-custom-binary-input/output-port
  1162. "foo" read! dummy-write!
  1163. #f #f #f)))
  1164. (setvbuf port 'none)
  1165. (get-string-all port)))
  1166. (pass-if-equal "custom binary input/output port unbuffered UTF-8 & \
  1167. 'get-string-all'"
  1168. (make-string 1000 #\λ)
  1169. ;; In Guile 2.0.11 this test would lead to a buffer overrun followed
  1170. ;; by an assertion failure. See <http://bugs.gnu.org/19621>.
  1171. (let* ((input (with-fluids ((%default-port-encoding "UTF-8"))
  1172. (open-input-string (make-string 1000 #\λ))))
  1173. (read! (lambda (bv index count)
  1174. (let ((n (get-bytevector-n! input bv index
  1175. count)))
  1176. (if (eof-object? n) 0 n))))
  1177. (port (make-custom-binary-input/output-port
  1178. "foo" read! dummy-write!
  1179. #f #f #f)))
  1180. (setvbuf port 'none)
  1181. (set-port-encoding! port "UTF-8")
  1182. (get-string-all port)))
  1183. (pass-if-equal "custom binary input/output port, unbuffered then buffered"
  1184. `((6 "Lorem ") (12 "ipsum dolor ") (777 "sit amet, consectetur…")
  1185. (777 ,(eof-object)))
  1186. (let* ((str "Lorem ipsum dolor sit amet, consectetur…")
  1187. (source (with-fluids ((%default-port-encoding "UTF-8"))
  1188. (open-string-input-port str)))
  1189. (reads '())
  1190. (read! (lambda (bv start count)
  1191. (set! reads (cons count reads))
  1192. (let ((r (get-bytevector-n! source bv start count)))
  1193. (if (eof-object? r)
  1194. 0
  1195. r))))
  1196. (port (make-custom-binary-input/output-port
  1197. "the port" read! dummy-write!
  1198. #f #f #f)))
  1199. (setvbuf port 'none)
  1200. (let ((ret (list (get-bytevector-n port 6)
  1201. (get-bytevector-n port 12)
  1202. (begin
  1203. (setvbuf port 'block 777)
  1204. (get-bytevector-n port 42))
  1205. (get-bytevector-n port 42))))
  1206. (zip (reverse reads)
  1207. (map (lambda (obj)
  1208. (if (bytevector? obj)
  1209. (utf8->string obj)
  1210. obj))
  1211. ret)))))
  1212. (pass-if-equal "custom binary input/output port, buffered then unbuffered"
  1213. `((18
  1214. 42 14 ; scm_c_read tries to fill the 42-byte buffer
  1215. 42)
  1216. ("Lorem " "ipsum dolor " "sit amet, consectetur bla…" ,(eof-object)))
  1217. (let* ((str "Lorem ipsum dolor sit amet, consectetur bla…")
  1218. (source (with-fluids ((%default-port-encoding "UTF-8"))
  1219. (open-string-input-port str)))
  1220. (reads '())
  1221. (read! (lambda (bv start count)
  1222. (set! reads (cons count reads))
  1223. (let ((r (get-bytevector-n! source bv start count)))
  1224. (if (eof-object? r)
  1225. 0
  1226. r))))
  1227. (port (make-custom-binary-input/output-port
  1228. "the port" read! dummy-write!
  1229. #f #f #f)))
  1230. (setvbuf port 'block 18)
  1231. (let ((ret (list (get-bytevector-n port 6)
  1232. (get-bytevector-n port 12)
  1233. (begin
  1234. (setvbuf port 'none)
  1235. (get-bytevector-n port 42))
  1236. (get-bytevector-n port 42))))
  1237. (list (reverse reads)
  1238. (map (lambda (obj)
  1239. (if (bytevector? obj)
  1240. (utf8->string obj)
  1241. obj))
  1242. ret)))))
  1243. (pass-if "custom binary input/output port `close-proc' is called"
  1244. (let* ((closed? #f)
  1245. (read! (lambda (bv start count) 0))
  1246. (get-pos (lambda () 0))
  1247. (set-pos! (lambda (pos) #f))
  1248. (close! (lambda () (set! closed? #t)))
  1249. (port (make-custom-binary-input/output-port
  1250. "the port" read! dummy-write!
  1251. get-pos set-pos! close!)))
  1252. (close-port port)
  1253. (gc) ; Test for marking a closed port.
  1254. closed?))
  1255. (pass-if "make-custom-binary-input/output-port [partial writes]"
  1256. (let* ((source (uint-list->bytevector (iota 333)
  1257. (native-endianness) 2))
  1258. (sink (make-bytevector (bytevector-length source)))
  1259. (sink-pos 0)
  1260. (eof? #f)
  1261. (write! (lambda (bv start count)
  1262. (if (= 0 count)
  1263. (begin
  1264. (set! eof? #t)
  1265. 0)
  1266. (let ((u8 (bytevector-u8-ref bv start)))
  1267. ;; Get one byte at a time.
  1268. (bytevector-u8-set! sink sink-pos u8)
  1269. (set! sink-pos (+ 1 sink-pos))
  1270. 1))))
  1271. (port (make-custom-binary-input/output-port
  1272. "cbop" dummy-read! write!
  1273. #f #f #f)))
  1274. (put-bytevector port source)
  1275. (force-output port)
  1276. (and (= sink-pos (bytevector-length source))
  1277. (not eof?)
  1278. (bytevector=? sink source))))
  1279. (pass-if "make-custom-binary-input/output-port [full writes]"
  1280. (let* ((source (uint-list->bytevector (iota 333)
  1281. (native-endianness) 2))
  1282. (sink (make-bytevector (bytevector-length source)))
  1283. (sink-pos 0)
  1284. (eof? #f)
  1285. (write! (lambda (bv start count)
  1286. (if (= 0 count)
  1287. (begin
  1288. (set! eof? #t)
  1289. 0)
  1290. (begin
  1291. (bytevector-copy! bv start
  1292. sink sink-pos
  1293. count)
  1294. (set! sink-pos (+ sink-pos count))
  1295. count))))
  1296. (port (make-custom-binary-input/output-port
  1297. "cbop" dummy-read! write!
  1298. #f #f #f)))
  1299. (put-bytevector port source)
  1300. (force-output port)
  1301. (and (= sink-pos (bytevector-length source))
  1302. (not eof?)
  1303. (bytevector=? sink source))))
  1304. (pass-if-equal "custom-binary-output-port uses ISO-8859-1 (Guile extension)\
  1305. [output]"
  1306. '(194 169 194 169)
  1307. (with-fluids ((%default-port-encoding "UTF-8"))
  1308. (let* ((sink '())
  1309. (write! (lambda (bv start count)
  1310. (if (= 0 count) ; EOF
  1311. 0
  1312. (let ((u8 (bytevector-u8-ref bv start)))
  1313. ;; Get one byte at a time.
  1314. (set! sink (cons u8 sink))
  1315. 1))))
  1316. (port (make-custom-binary-input/output-port
  1317. "cbop" dummy-read! write!
  1318. #f #f #f)))
  1319. (put-string port "©©")
  1320. (force-output port)
  1321. (reverse sink))))
  1322. )
  1323. (define exception:encoding-error
  1324. '(encoding-error . ""))
  1325. (define exception:decoding-error
  1326. '(decoding-error . ""))
  1327. (with-test-prefix "ascii string"
  1328. (let ((s "Hello, World!"))
  1329. ;; For ASCII, all of these encodings should be the same.
  1330. (pass-if "to ascii bytevector"
  1331. (equal? (string->bytevector s (make-transcoder "ASCII"))
  1332. #vu8(72 101 108 108 111 44 32 87 111 114 108 100 33)))
  1333. (pass-if "to ascii bytevector (length check)"
  1334. (equal? (string-length s)
  1335. (bytevector-length
  1336. (string->bytevector s (make-transcoder "ascii")))))
  1337. (pass-if "from ascii bytevector"
  1338. (equal? s
  1339. (bytevector->string
  1340. (string->bytevector s (make-transcoder "ascii"))
  1341. (make-transcoder "ascii"))))
  1342. (pass-if "to utf-8 bytevector"
  1343. (equal? (string->bytevector s (make-transcoder "ASCII"))
  1344. (string->bytevector s (make-transcoder "utf-8"))))
  1345. (pass-if "to UTF-8 bytevector (testing encoding case sensitivity)"
  1346. (equal? (string->bytevector s (make-transcoder "ascii"))
  1347. (string->bytevector s (make-transcoder "UTF-8"))))
  1348. (pass-if "from utf-8 bytevector"
  1349. (equal? s
  1350. (bytevector->string
  1351. (string->bytevector s (make-transcoder "utf-8"))
  1352. (make-transcoder "utf-8"))))
  1353. (pass-if "to latin1 bytevector"
  1354. (equal? (string->bytevector s (make-transcoder "ASCII"))
  1355. (string->bytevector s (make-transcoder "latin1"))))
  1356. (pass-if "from latin1 bytevector"
  1357. (equal? s
  1358. (bytevector->string
  1359. (string->bytevector s (make-transcoder "utf-8"))
  1360. (make-transcoder "utf-8"))))))
  1361. (with-test-prefix "narrow non-ascii string"
  1362. (let ((s "été"))
  1363. (pass-if "to latin1 bytevector"
  1364. (equal? (string->bytevector s (make-transcoder "latin1"))
  1365. #vu8(233 116 233)))
  1366. (pass-if "to latin1 bytevector (length check)"
  1367. (equal? (string-length s)
  1368. (bytevector-length
  1369. (string->bytevector s (make-transcoder "latin1")))))
  1370. (pass-if "from latin1 bytevector"
  1371. (equal? s
  1372. (bytevector->string
  1373. (string->bytevector s (make-transcoder "latin1"))
  1374. (make-transcoder "latin1"))))
  1375. (pass-if "to utf-8 bytevector"
  1376. (equal? (string->bytevector s (make-transcoder "utf-8"))
  1377. #vu8(195 169 116 195 169)))
  1378. (pass-if "from utf-8 bytevector"
  1379. (equal? s
  1380. (bytevector->string
  1381. (string->bytevector s (make-transcoder "utf-8"))
  1382. (make-transcoder "utf-8"))))
  1383. (pass-if-exception "encode latin1 as ascii" exception:encoding-error
  1384. (string->bytevector s (make-transcoder "ascii"
  1385. (native-eol-style)
  1386. (error-handling-mode raise))))
  1387. (pass-if-exception "misparse latin1 as utf8" exception:decoding-error
  1388. (bytevector->string
  1389. (string->bytevector s (make-transcoder "latin1"))
  1390. (make-transcoder "utf-8"
  1391. (native-eol-style)
  1392. (error-handling-mode raise))))
  1393. (pass-if "misparse latin1 as utf8 with substitutions"
  1394. (equal? (bytevector->string
  1395. (string->bytevector s (make-transcoder "latin1"))
  1396. (make-transcoder "utf-8" (native-eol-style)
  1397. (error-handling-mode replace)))
  1398. "\uFFFDt\uFFFD"))
  1399. (pass-if-exception "misparse latin1 as ascii" exception:decoding-error
  1400. (bytevector->string (string->bytevector s (make-transcoder "latin1"))
  1401. (make-transcoder "ascii"
  1402. (native-eol-style)
  1403. (error-handling-mode raise))))))
  1404. (with-test-prefix "wide non-ascii string"
  1405. (let ((s "ΧΑΟΣ"))
  1406. (pass-if "to utf-8 bytevector"
  1407. (equal? (string->bytevector s (make-transcoder "utf-8"))
  1408. #vu8(206 167 206 145 206 159 206 163) ))
  1409. (pass-if "from utf-8 bytevector"
  1410. (equal? s
  1411. (bytevector->string
  1412. (string->bytevector s (make-transcoder "utf-8"))
  1413. (make-transcoder "utf-8"))))
  1414. (pass-if-exception "encode as ascii" exception:encoding-error
  1415. (string->bytevector s (make-transcoder "ascii"
  1416. (native-eol-style)
  1417. (error-handling-mode raise))))
  1418. (pass-if-exception "encode as latin1" exception:encoding-error
  1419. (string->bytevector s (make-transcoder "latin1"
  1420. (native-eol-style)
  1421. (error-handling-mode raise))))
  1422. (pass-if "encode as ascii with substitutions"
  1423. (equal? (make-string (string-length s) #\?)
  1424. (bytevector->string
  1425. (string->bytevector s (make-transcoder
  1426. "ascii"
  1427. (native-eol-style)
  1428. (error-handling-mode replace)))
  1429. (make-transcoder "ascii"))))))
  1430. ;;; Local Variables:
  1431. ;;; mode: scheme
  1432. ;;; eval: (put 'guard 'scheme-indent-function 1)
  1433. ;;; End: