ports.test 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941
  1. ;;;; ports.test --- Guile I/O ports. -*- coding: utf-8; mode: scheme; -*-
  2. ;;;; Jim Blandy <jimb@red-bean.com> --- May 1999
  3. ;;;;
  4. ;;;; Copyright (C) 1999, 2001, 2004, 2006, 2007, 2009, 2010,
  5. ;;;; 2011, 2012, 2013, 2014, 2015 Free Software Foundation, Inc.
  6. ;;;;
  7. ;;;; This library is free software; you can redistribute it and/or
  8. ;;;; modify it under the terms of the GNU Lesser General Public
  9. ;;;; License as published by the Free Software Foundation; either
  10. ;;;; version 3 of the License, or (at your option) any later version.
  11. ;;;;
  12. ;;;; This library is distributed in the hope that it will be useful,
  13. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. ;;;; Lesser General Public License for more details.
  16. ;;;;
  17. ;;;; You should have received a copy of the GNU Lesser General Public
  18. ;;;; License along with this library; if not, write to the Free Software
  19. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. (define-module (test-suite test-ports)
  21. #:use-module (test-suite lib)
  22. #:use-module (test-suite guile-test)
  23. #:use-module (ice-9 popen)
  24. #:use-module (ice-9 rdelim)
  25. #:use-module (ice-9 threads)
  26. #:use-module (rnrs bytevectors)
  27. #:use-module ((ice-9 binary-ports) #:select (open-bytevector-input-port
  28. open-bytevector-output-port
  29. put-bytevector
  30. get-bytevector-n
  31. get-bytevector-all
  32. unget-bytevector)))
  33. (define (display-line . args)
  34. (for-each display args)
  35. (newline))
  36. (define (test-file)
  37. (data-file-name "ports-test.tmp"))
  38. ;;;; Some general utilities for testing ports.
  39. ;; Make sure we are set up for 8-bit Latin-1 data.
  40. (fluid-set! %default-port-encoding "ISO-8859-1")
  41. (for-each (lambda (p)
  42. (set-port-encoding! p (fluid-ref %default-port-encoding)))
  43. (list (current-input-port) (current-output-port)
  44. (current-error-port)))
  45. ;;; Read from PORT until EOF, and return the result as a string.
  46. (define (read-all port)
  47. (let loop ((chars '()))
  48. (let ((char (read-char port)))
  49. (if (eof-object? char)
  50. (list->string (reverse! chars))
  51. (loop (cons char chars))))))
  52. (define (read-file filename)
  53. (let* ((port (open-input-file filename))
  54. (string (read-all port)))
  55. (close-port port)
  56. string))
  57. (with-test-prefix "%default-port-conversion-strategy"
  58. (pass-if "initial value"
  59. (eq? 'substitute (fluid-ref %default-port-conversion-strategy)))
  60. (pass-if "file port"
  61. (let ((strategies '(error substitute escape)))
  62. (equal? (map (lambda (s)
  63. (with-fluids ((%default-port-conversion-strategy s))
  64. (call-with-output-file "/dev/null"
  65. (lambda (p)
  66. (port-conversion-strategy p)))))
  67. strategies)
  68. strategies)))
  69. (pass-if "(set-port-conversion-strategy! #f sym)"
  70. (begin
  71. (set-port-conversion-strategy! #f 'error)
  72. (and (eq? (fluid-ref %default-port-conversion-strategy) 'error)
  73. (begin
  74. (set-port-conversion-strategy! #f 'substitute)
  75. (eq? (fluid-ref %default-port-conversion-strategy)
  76. 'substitute)))))
  77. )
  78. ;;;; Normal file ports.
  79. ;;; Write out an s-expression, and read it back.
  80. (let ((string '("From fairest creatures we desire increase,"
  81. "That thereby beauty's rose might never die,"))
  82. (filename (test-file)))
  83. (let ((port (open-output-file filename)))
  84. (write string port)
  85. (close-port port))
  86. (let ((port (open-input-file filename)))
  87. (let ((in-string (read port)))
  88. (pass-if "file: write and read back list of strings"
  89. (equal? string in-string)))
  90. (close-port port))
  91. (delete-file filename))
  92. ;;; Write out a string, and read it back a character at a time.
  93. (let ((string "This is a test string\nwith no newline at the end")
  94. (filename (test-file)))
  95. (let ((port (open-output-file filename)))
  96. (display string port)
  97. (close-port port))
  98. (let ((in-string (read-file filename)))
  99. (pass-if "file: write and read back characters"
  100. (equal? string in-string)))
  101. (delete-file filename))
  102. ;;; Buffered input/output port with seeking.
  103. (let* ((filename (test-file))
  104. (port (open-file filename "w+")))
  105. (display "J'Accuse" port)
  106. (seek port -1 SEEK_CUR)
  107. (pass-if "file: r/w 1"
  108. (char=? (read-char port) #\e))
  109. (pass-if "file: r/w 2"
  110. (eof-object? (read-char port)))
  111. (seek port -1 SEEK_CUR)
  112. (write-char #\x port)
  113. (seek port 7 SEEK_SET)
  114. (pass-if "file: r/w 3"
  115. (char=? (read-char port) #\x))
  116. (seek port -2 SEEK_END)
  117. (pass-if "file: r/w 4"
  118. (char=? (read-char port) #\s))
  119. (close-port port)
  120. (delete-file filename))
  121. ;;; Unbuffered input/output port with seeking.
  122. (let* ((filename (test-file))
  123. (port (open-file filename "w+0")))
  124. (display "J'Accuse" port)
  125. (seek port -1 SEEK_CUR)
  126. (pass-if "file: ub r/w 1"
  127. (char=? (read-char port) #\e))
  128. (pass-if "file: ub r/w 2"
  129. (eof-object? (read-char port)))
  130. (seek port -1 SEEK_CUR)
  131. (write-char #\x port)
  132. (seek port 7 SEEK_SET)
  133. (pass-if "file: ub r/w 3"
  134. (char=? (read-char port) #\x))
  135. (seek port -2 SEEK_END)
  136. (pass-if "file: ub r/w 4"
  137. (char=? (read-char port) #\s))
  138. (close-port port)
  139. (delete-file filename))
  140. ;;; Buffered output-only and input-only ports with seeking.
  141. (let* ((filename (test-file))
  142. (port (open-output-file filename)))
  143. (display "J'Accuse" port)
  144. (pass-if "file: out tell"
  145. (= (seek port 0 SEEK_CUR) 8))
  146. (seek port -1 SEEK_CUR)
  147. (write-char #\x port)
  148. (close-port port)
  149. (let ((iport (open-input-file filename)))
  150. (pass-if "file: in tell 0"
  151. (= (seek iport 0 SEEK_CUR) 0))
  152. (read-char iport)
  153. (pass-if "file: in tell 1"
  154. (= (seek iport 0 SEEK_CUR) 1))
  155. (unread-char #\z iport)
  156. (pass-if "file: in tell 0 after unread"
  157. (= (seek iport 0 SEEK_CUR) 0))
  158. (pass-if "file: unread char still there"
  159. (char=? (read-char iport) #\z))
  160. (seek iport 7 SEEK_SET)
  161. (pass-if "file: in last char"
  162. (char=? (read-char iport) #\x))
  163. (close-port iport))
  164. (delete-file filename))
  165. ;;; unusual characters.
  166. (let* ((filename (test-file))
  167. (port (open-output-file filename)))
  168. (display (string #\nul (integer->char 255) (integer->char 128)
  169. #\nul) port)
  170. (close-port port)
  171. (let* ((port (open-input-file filename))
  172. (line (read-line port)))
  173. (pass-if "file: read back NUL 1"
  174. (char=? (string-ref line 0) #\nul))
  175. (pass-if "file: read back 255"
  176. (char=? (string-ref line 1) (integer->char 255)))
  177. (pass-if "file: read back 128"
  178. (char=? (string-ref line 2) (integer->char 128)))
  179. (pass-if "file: read back NUL 2"
  180. (char=? (string-ref line 3) #\nul))
  181. (pass-if "file: EOF"
  182. (eof-object? (read-char port)))
  183. (close-port port))
  184. (delete-file filename))
  185. ;;; line buffering mode.
  186. (let* ((filename (test-file))
  187. (port (open-file filename "wl"))
  188. (test-string "one line more or less"))
  189. (write-line test-string port)
  190. (let* ((in-port (open-input-file filename))
  191. (line (read-line in-port)))
  192. (close-port in-port)
  193. (close-port port)
  194. (pass-if "file: line buffering"
  195. (string=? line test-string)))
  196. (delete-file filename))
  197. ;;; read-line should use the port encoding (not the locale encoding).
  198. (let ((str "ĉu bone?"))
  199. (with-locale "C"
  200. (let* ((filename (test-file))
  201. (port (open-file filename "wl")))
  202. (set-port-encoding! port "UTF-8")
  203. (write-line str port)
  204. (let ((in-port (open-input-file filename)))
  205. (set-port-encoding! in-port "UTF-8")
  206. (let ((line (read-line in-port)))
  207. (close-port in-port)
  208. (close-port port)
  209. (pass-if "file: read-line honors port encoding"
  210. (string=? line str))))
  211. (delete-file filename))))
  212. ;;; binary mode ignores port encoding
  213. (pass-if "file: binary mode ignores port encoding"
  214. (with-fluids ((%default-port-encoding "UTF-8"))
  215. (let* ((filename (test-file))
  216. (port (open-file filename "w"))
  217. (test-string "一二三")
  218. (binary-test-string
  219. (apply string
  220. (map integer->char
  221. (array->list
  222. (string->utf8 test-string))))))
  223. (write-line test-string port)
  224. (close-port port)
  225. (let* ((in-port (open-file filename "rb"))
  226. (line (read-line in-port)))
  227. (close-port in-port)
  228. (delete-file filename)
  229. (string=? line binary-test-string)))))
  230. ;;; binary mode ignores file coding declaration
  231. (pass-if "file: binary mode ignores file coding declaration"
  232. (with-fluids ((%default-port-encoding "UTF-8"))
  233. (let* ((filename (test-file))
  234. (port (open-file filename "w"))
  235. (test-string "一二三")
  236. (binary-test-string
  237. (apply string
  238. (map integer->char
  239. (array->list
  240. (string->utf8 test-string))))))
  241. (write-line ";; coding: utf-8" port)
  242. (write-line test-string port)
  243. (close-port port)
  244. (let* ((in-port (open-file filename "rb"))
  245. (line1 (read-line in-port))
  246. (line2 (read-line in-port)))
  247. (close-port in-port)
  248. (delete-file filename)
  249. (string=? line2 binary-test-string)))))
  250. ;; open-file ignores file coding declaration by default
  251. (pass-if "file: open-file ignores coding declaration by default"
  252. (with-fluids ((%default-port-encoding "UTF-8"))
  253. (let* ((filename (test-file))
  254. (port (open-output-file filename))
  255. (test-string "€100"))
  256. (write-line ";; coding: iso-8859-15" port)
  257. (write-line test-string port)
  258. (close-port port)
  259. (let* ((in-port (open-input-file filename))
  260. (line1 (read-line in-port))
  261. (line2 (read-line in-port)))
  262. (close-port in-port)
  263. (delete-file filename)
  264. (string=? line2 test-string)))))
  265. ;; open-input-file with guess-encoding honors coding declaration
  266. (pass-if "file: open-input-file with guess-encoding honors coding declaration"
  267. (with-fluids ((%default-port-encoding "UTF-8"))
  268. (let* ((filename (test-file))
  269. (port (open-output-file filename))
  270. (test-string "€100"))
  271. (set-port-encoding! port "iso-8859-15")
  272. (write-line ";; coding: iso-8859-15" port)
  273. (write-line test-string port)
  274. (close-port port)
  275. (let* ((in-port (open-input-file filename
  276. #:guess-encoding #t))
  277. (line1 (read-line in-port))
  278. (line2 (read-line in-port)))
  279. (close-port in-port)
  280. (delete-file filename)
  281. (string=? line2 test-string)))))
  282. (pass-if-exception "invalid wide mode string"
  283. exception:out-of-range
  284. (open-file "/dev/null" "λ"))
  285. (pass-if "valid wide mode string"
  286. ;; Pass 'open-file' a valid mode string, but as a wide string.
  287. (let ((mode "λ"))
  288. (string-set! mode 0 #\r)
  289. (let ((port (open-file "/dev/null" mode)))
  290. (and (input-port? port)
  291. (begin
  292. (close-port port)
  293. #t)))))
  294. (with-test-prefix "keyword arguments for file openers"
  295. (with-fluids ((%default-port-encoding "UTF-8"))
  296. (let ((filename (test-file)))
  297. (with-test-prefix "write #:encoding"
  298. (pass-if-equal "open-file"
  299. #vu8(116 0 101 0 115 0 116 0)
  300. (let ((port (open-file filename "w"
  301. #:encoding "UTF-16LE")))
  302. (display "test" port)
  303. (close-port port))
  304. (let* ((port (open-file filename "rb"))
  305. (bv (get-bytevector-all port)))
  306. (close-port port)
  307. bv))
  308. (pass-if-equal "open-output-file"
  309. #vu8(116 0 101 0 115 0 116 0)
  310. (let ((port (open-output-file filename
  311. #:encoding "UTF-16LE")))
  312. (display "test" port)
  313. (close-port port))
  314. (let* ((port (open-file filename "rb"))
  315. (bv (get-bytevector-all port)))
  316. (close-port port)
  317. bv))
  318. (pass-if-equal "call-with-output-file"
  319. #vu8(116 0 101 0 115 0 116 0)
  320. (call-with-output-file filename
  321. (lambda (port)
  322. (display "test" port))
  323. #:encoding "UTF-16LE")
  324. (let* ((port (open-file filename "rb"))
  325. (bv (get-bytevector-all port)))
  326. (close-port port)
  327. bv))
  328. (pass-if-equal "with-output-to-file"
  329. #vu8(116 0 101 0 115 0 116 0)
  330. (with-output-to-file filename
  331. (lambda ()
  332. (display "test"))
  333. #:encoding "UTF-16LE")
  334. (let* ((port (open-file filename "rb"))
  335. (bv (get-bytevector-all port)))
  336. (close-port port)
  337. bv))
  338. (pass-if-equal "with-error-to-file"
  339. #vu8(116 0 101 0 115 0 116 0)
  340. (with-error-to-file
  341. filename
  342. (lambda ()
  343. (display "test" (current-error-port)))
  344. #:encoding "UTF-16LE")
  345. (let* ((port (open-file filename "rb"))
  346. (bv (get-bytevector-all port)))
  347. (close-port port)
  348. bv)))
  349. (with-test-prefix "write #:binary"
  350. (pass-if-equal "open-output-file"
  351. "ISO-8859-1"
  352. (let* ((port (open-output-file filename #:binary #t))
  353. (enc (port-encoding port)))
  354. (close-port port)
  355. enc))
  356. (pass-if-equal "call-with-output-file"
  357. "ISO-8859-1"
  358. (call-with-output-file filename port-encoding #:binary #t))
  359. (pass-if-equal "with-output-to-file"
  360. "ISO-8859-1"
  361. (with-output-to-file filename
  362. (lambda () (port-encoding (current-output-port)))
  363. #:binary #t))
  364. (pass-if-equal "with-error-to-file"
  365. "ISO-8859-1"
  366. (with-error-to-file
  367. filename
  368. (lambda () (port-encoding (current-error-port)))
  369. #:binary #t)))
  370. (with-test-prefix "read #:encoding"
  371. (pass-if-equal "open-file read #:encoding"
  372. "test"
  373. (call-with-output-file filename
  374. (lambda (port)
  375. (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
  376. (let* ((port (open-file filename "r" #:encoding "UTF-16LE"))
  377. (str (read-string port)))
  378. (close-port port)
  379. str))
  380. (pass-if-equal "open-input-file #:encoding"
  381. "test"
  382. (call-with-output-file filename
  383. (lambda (port)
  384. (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
  385. (let* ((port (open-input-file filename #:encoding "UTF-16LE"))
  386. (str (read-string port)))
  387. (close-port port)
  388. str))
  389. (pass-if-equal "call-with-input-file #:encoding"
  390. "test"
  391. (call-with-output-file filename
  392. (lambda (port)
  393. (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
  394. (call-with-input-file filename
  395. read-string
  396. #:encoding "UTF-16LE"))
  397. (pass-if-equal "with-input-from-file #:encoding"
  398. "test"
  399. (call-with-output-file filename
  400. (lambda (port)
  401. (put-bytevector port #vu8(116 0 101 0 115 0 116 0))))
  402. (with-input-from-file filename
  403. read-string
  404. #:encoding "UTF-16LE")))
  405. (with-test-prefix "read #:binary"
  406. (pass-if-equal "open-input-file"
  407. "ISO-8859-1"
  408. (let* ((port (open-input-file filename #:binary #t))
  409. (enc (port-encoding port)))
  410. (close-port port)
  411. enc))
  412. (pass-if-equal "call-with-input-file"
  413. "ISO-8859-1"
  414. (call-with-input-file filename port-encoding #:binary #t))
  415. (pass-if-equal "with-input-from-file"
  416. "ISO-8859-1"
  417. (with-input-from-file filename
  418. (lambda () (port-encoding (current-input-port)))
  419. #:binary #t)))
  420. (with-test-prefix "#:guess-encoding with coding declaration"
  421. (pass-if-equal "open-file"
  422. "€100"
  423. (with-output-to-file filename
  424. (lambda ()
  425. (write-line "test")
  426. (write-line "; coding: ISO-8859-15")
  427. (write-line "€100"))
  428. #:encoding "ISO-8859-15")
  429. (let* ((port (open-file filename "r"
  430. #:guess-encoding #t
  431. #:encoding "UTF-16LE"))
  432. (str (begin (read-line port)
  433. (read-line port)
  434. (read-line port))))
  435. (close-port port)
  436. str))
  437. (pass-if-equal "open-input-file"
  438. "€100"
  439. (with-output-to-file filename
  440. (lambda ()
  441. (write-line "test")
  442. (write-line "; coding: ISO-8859-15")
  443. (write-line "€100"))
  444. #:encoding "ISO-8859-15")
  445. (let* ((port (open-input-file filename
  446. #:guess-encoding #t
  447. #:encoding "UTF-16LE"))
  448. (str (begin (read-line port)
  449. (read-line port)
  450. (read-line port))))
  451. (close-port port)
  452. str))
  453. (pass-if-equal "call-with-input-file"
  454. "€100"
  455. (with-output-to-file filename
  456. (lambda ()
  457. (write-line "test")
  458. (write-line "; coding: ISO-8859-15")
  459. (write-line "€100"))
  460. #:encoding "ISO-8859-15")
  461. (call-with-input-file filename
  462. (lambda (port)
  463. (read-line port)
  464. (read-line port)
  465. (read-line port))
  466. #:guess-encoding #t
  467. #:encoding "UTF-16LE"))
  468. (pass-if-equal "with-input-from-file"
  469. "€100"
  470. (with-output-to-file filename
  471. (lambda ()
  472. (write-line "test")
  473. (write-line "; coding: ISO-8859-15")
  474. (write-line "€100"))
  475. #:encoding "ISO-8859-15")
  476. (with-input-from-file filename
  477. (lambda ()
  478. (read-line)
  479. (read-line)
  480. (read-line))
  481. #:guess-encoding #t
  482. #:encoding "UTF-16LE")))
  483. (with-test-prefix "#:guess-encoding without coding declaration"
  484. (pass-if-equal "open-file"
  485. "€100"
  486. (with-output-to-file filename
  487. (lambda () (write-line "€100"))
  488. #:encoding "ISO-8859-15")
  489. (let* ((port (open-file filename "r"
  490. #:guess-encoding #t
  491. #:encoding "ISO-8859-15"))
  492. (str (read-line port)))
  493. (close-port port)
  494. str))
  495. (pass-if-equal "open-input-file"
  496. "€100"
  497. (with-output-to-file filename
  498. (lambda () (write-line "€100"))
  499. #:encoding "ISO-8859-15")
  500. (let* ((port (open-input-file filename
  501. #:guess-encoding #t
  502. #:encoding "ISO-8859-15"))
  503. (str (read-line port)))
  504. (close-port port)
  505. str))
  506. (pass-if-equal "call-with-input-file"
  507. "€100"
  508. (with-output-to-file filename
  509. (lambda () (write-line "€100"))
  510. #:encoding "ISO-8859-15")
  511. (call-with-input-file filename
  512. read-line
  513. #:guess-encoding #t
  514. #:encoding "ISO-8859-15"))
  515. (pass-if-equal "with-input-from-file"
  516. "€100"
  517. (with-output-to-file filename
  518. (lambda () (write-line "€100"))
  519. #:encoding "ISO-8859-15")
  520. (with-input-from-file filename
  521. read-line
  522. #:guess-encoding #t
  523. #:encoding "ISO-8859-15")))
  524. (delete-file filename))))
  525. ;;; ungetting characters and strings.
  526. (with-input-from-string "walk on the moon\nmoon"
  527. (lambda ()
  528. (read-char)
  529. (unread-char #\a (current-input-port))
  530. (pass-if "unread-char"
  531. (char=? (read-char) #\a))
  532. (read-line)
  533. (let ((replacenoid "chicken enchilada"))
  534. (unread-char #\newline (current-input-port))
  535. (unread-string replacenoid (current-input-port))
  536. (pass-if "unread-string"
  537. (string=? (read-line) replacenoid)))
  538. (pass-if "unread residue"
  539. (string=? (read-line) "moon"))))
  540. (when (provided? 'threads)
  541. (let* ((p (pipe))
  542. (r (car p))
  543. (w (cdr p)))
  544. (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
  545. (let ((thread (call-with-new-thread
  546. (lambda ()
  547. (usleep (* 250 1000))
  548. (write-char #\a w)
  549. (force-output w)))))
  550. (pass-if-equal "non-blocking-I/O" #\a (read-char r))
  551. (join-thread thread))))
  552. ;;;; Pipe (popen) ports.
  553. ;;; Run a command, and read its output.
  554. (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
  555. (in-string (read-all pipe)))
  556. (close-pipe pipe)
  557. (pass-if "pipe: read"
  558. (equal? in-string "Howdy there, partner!\n")))
  559. ;;; Run a command, send some output to it, and see if it worked.
  560. (let* ((filename (test-file))
  561. (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
  562. (display "Now Jimmy lives on a mushroom cloud\n" pipe)
  563. (display "Mommy, why does everybody have a bomb?\n" pipe)
  564. (close-pipe pipe)
  565. (let ((in-string (read-file filename)))
  566. (pass-if "pipe: write"
  567. (equal? in-string "Mommy, why does everybody have a bomb?\n")))
  568. (delete-file filename))
  569. (pass-if-equal "pipe, fdopen, and line buffering"
  570. "foo\nbar\n"
  571. (let ((in+out (pipe))
  572. (pid (primitive-fork)))
  573. (if (zero? pid)
  574. (dynamic-wind
  575. (const #t)
  576. (lambda ()
  577. (close-port (car in+out))
  578. (let ((port (cdr in+out)))
  579. (setvbuf port 'line )
  580. ;; Strings containing '\n' or should be flushed; others
  581. ;; should be kept in PORT's buffer.
  582. (display "foo\n" port)
  583. (display "bar\n" port)
  584. (display "this will be kept in PORT's buffer" port)))
  585. (lambda ()
  586. (primitive-_exit 0)))
  587. (begin
  588. (close-port (cdr in+out))
  589. (let ((str (read-all (car in+out))))
  590. (waitpid pid)
  591. str)))))
  592. ;;;; Void ports. These are so trivial we don't test them.
  593. ;;;; String ports.
  594. (with-test-prefix "string ports"
  595. ;; Write text to a string port.
  596. (let* ((string "Howdy there, partner!")
  597. (in-string (call-with-output-string
  598. (lambda (port)
  599. (display string port)
  600. (newline port)))))
  601. (pass-if "display text"
  602. (equal? in-string (string-append string "\n"))))
  603. ;; Write an s-expression to a string port.
  604. (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
  605. (in-sexpr
  606. (call-with-input-string (call-with-output-string
  607. (lambda (port)
  608. (write sexpr port)))
  609. read)))
  610. (pass-if "write/read sexpr"
  611. (equal? in-sexpr sexpr)))
  612. ;; seeking and unreading from an input string.
  613. (let ((text "that text didn't look random to me"))
  614. (call-with-input-string text
  615. (lambda (p)
  616. (pass-if "input tell 0"
  617. (= (seek p 0 SEEK_CUR) 0))
  618. (read-char p)
  619. (pass-if "input tell 1"
  620. (= (seek p 0 SEEK_CUR) 1))
  621. (unread-char #\x p)
  622. (pass-if "input tell back to 0"
  623. (= (seek p 0 SEEK_CUR) 0))
  624. (pass-if "input ungetted char"
  625. (char=? (read-char p) #\x))
  626. (seek p 0 SEEK_END)
  627. (pass-if "input seek to end"
  628. (= (seek p 0 SEEK_CUR)
  629. (string-length text)))
  630. (unread-char #\x p)
  631. (pass-if "input seek to beginning"
  632. (= (seek p 0 SEEK_SET) 0))
  633. (pass-if "input reread first char"
  634. (char=? (read-char p)
  635. (string-ref text 0))))))
  636. ;; seeking an output string.
  637. (let* ((text (string-copy "123456789"))
  638. (len (string-length text))
  639. (result (call-with-output-string
  640. (lambda (p)
  641. (pass-if "output tell 0"
  642. (= (seek p 0 SEEK_CUR) 0))
  643. (display text p)
  644. (pass-if "output tell end"
  645. (= (seek p 0 SEEK_CUR) len))
  646. (pass-if "output seek to beginning"
  647. (= (seek p 0 SEEK_SET) 0))
  648. (write-char #\a p)
  649. (seek p -1 SEEK_END)
  650. (pass-if "output seek to last char"
  651. (= (seek p 0 SEEK_CUR)
  652. (- len 1)))
  653. (write-char #\b p)))))
  654. (string-set! text 0 #\a)
  655. (string-set! text (- len 1) #\b)
  656. (pass-if "output check"
  657. (string=? text result)))
  658. (pass-if-exception "truncating input string fails"
  659. exception:wrong-type-arg
  660. (call-with-input-string "hej"
  661. (lambda (p)
  662. (truncate-file p 0))))
  663. (pass-if-equal "truncating output string" "hej"
  664. (call-with-output-string
  665. (lambda (p)
  666. (truncate-file p 0)
  667. (display "hej" p))))
  668. (pass-if-exception "truncating output string before position"
  669. exception:out-of-range
  670. (call-with-output-string
  671. (lambda (p)
  672. (display "hej" p)
  673. (truncate-file p 0))))
  674. (pass-if-equal "truncating output string at position" "hej"
  675. (call-with-output-string
  676. (lambda (p)
  677. (display "hej" p)
  678. (truncate-file p 3))))
  679. (pass-if-equal "truncating output string after seek" ""
  680. (call-with-output-string
  681. (lambda (p)
  682. (display "hej" p)
  683. (seek p 0 SEEK_SET)
  684. (truncate-file p 0))))
  685. (pass-if-equal "truncating output string after seek to end" "hej"
  686. (call-with-output-string
  687. (lambda (p)
  688. (display "hej" p)
  689. (seek p 0 SEEK_SET)
  690. (truncate-file p 3))))
  691. (pass-if "%default-port-encoding is ignored"
  692. (let ((str "ĉu bone?"))
  693. ;; Latin-1 cannot represent ‘ĉ’.
  694. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  695. (string=? (call-with-output-string
  696. (lambda (p)
  697. (set-port-conversion-strategy! p 'substitute)
  698. (display str p)))
  699. "ĉu bone?"))))
  700. (pass-if "%default-port-conversion-strategy is honored"
  701. (let ((strategies '(error substitute escape)))
  702. (equal? (map (lambda (s)
  703. (with-fluids ((%default-port-conversion-strategy s))
  704. (call-with-output-string
  705. (lambda (p)
  706. (and (eq? s (port-conversion-strategy p))
  707. (begin
  708. (set-port-conversion-strategy! p s)
  709. (display (port-conversion-strategy p)
  710. p)))))))
  711. strategies)
  712. (map symbol->string strategies))))
  713. (pass-if "suitable encoding [latin-1]"
  714. (let ((str "hello, world")
  715. (encoding "ISO-8859-1"))
  716. (equal? str
  717. (call-with-output-string
  718. (lambda (p)
  719. (set-port-encoding! p encoding)
  720. (display str p))))))
  721. (pass-if "suitable encoding [latin-3]"
  722. (let ((str "ĉu bone?")
  723. (encoding "ISO-8859-3"))
  724. (equal? str
  725. (call-with-output-string
  726. (lambda (p)
  727. (set-port-encoding! p encoding)
  728. (display str p))))))
  729. (pass-if "wrong encoding, error"
  730. (let ((str "ĉu bone?"))
  731. (catch 'encoding-error
  732. (lambda ()
  733. (with-fluids ((%default-port-conversion-strategy 'error))
  734. (call-with-output-string
  735. (lambda (p)
  736. ;; Latin-1 cannot represent ‘ĉ’.
  737. (set-port-encoding! p "ISO-8859-1")
  738. (display str p))))
  739. #f) ; so the test really fails here
  740. (lambda (key subr message errno port chr)
  741. (and (eqv? chr #\ĉ)
  742. (string? (strerror errno)))))))
  743. (pass-if "wrong encoding, substitute"
  744. (let ((str "ĉu bone?"))
  745. (string=? (call-with-output-string
  746. (lambda (p)
  747. (set-port-encoding! p "ISO-8859-1")
  748. (set-port-conversion-strategy! p 'substitute)
  749. (display str p)))
  750. "?u bone?")))
  751. (pass-if "wrong encoding, escape"
  752. (let ((str "ĉu bone?"))
  753. (string=? (call-with-output-string
  754. (lambda (p)
  755. (set-port-encoding! p "ISO-8859-1")
  756. (set-port-conversion-strategy! p 'escape)
  757. (display str p)))
  758. "\\u0109u bone?")))
  759. (pass-if "peek-char"
  760. (let ((p (open-input-string "안녕하세요")))
  761. (and (char=? (peek-char p) #\안)
  762. (char=? (peek-char p) #\안)
  763. (char=? (peek-char p) #\안)
  764. (= (port-line p) 0)
  765. (= (port-column p) 0))))
  766. ;; Mini DSL to test decoding error handling.
  767. (letrec-syntax ((decoding-error?
  768. (syntax-rules ()
  769. ((_ port proc)
  770. (catch 'decoding-error
  771. (lambda ()
  772. (pk 'proc (proc port))
  773. #f)
  774. (lambda (key subr message errno p)
  775. (define (skip-over-error)
  776. (let ((strategy (port-conversion-strategy p)))
  777. (set-port-conversion-strategy! p 'substitute)
  778. ;; If `proc' is `read-char', this will
  779. ;; skip over the bad bytes.
  780. (let ((c (proc p)))
  781. (unless (eqv? c #\xFFFD)
  782. (error "unexpected char" c))
  783. (set-port-conversion-strategy! p strategy)
  784. #t)))
  785. (and (eq? p port)
  786. (not (= 0 errno))
  787. (skip-over-error)))))))
  788. (make-check
  789. (syntax-rules (-> error eof)
  790. ((_ port (proc -> error))
  791. (if (eq? 'substitute
  792. (port-conversion-strategy port))
  793. (eqv? (proc port) #\xFFFD)
  794. (decoding-error? port proc)))
  795. ((_ port (proc -> eof))
  796. (eof-object? (proc port)))
  797. ((_ port (proc -> char))
  798. (eqv? (proc port) char))))
  799. (make-checks
  800. (syntax-rules ()
  801. ((_ port check ...)
  802. (and (make-check port check) ...))))
  803. (make-peek+read-checks
  804. (syntax-rules ()
  805. ((_ port (result ...) e1 expected ...)
  806. (make-peek+read-checks port
  807. (result ...
  808. (peek-char -> e1)
  809. (read-char -> e1))
  810. expected ...))
  811. ((_ port (result ...))
  812. (make-checks port result ...))
  813. ((_ port #f e1 expected ...)
  814. (make-peek+read-checks port
  815. ((peek-char -> e1)
  816. (read-char -> e1))
  817. expected ...))))
  818. (test-decoding-error*
  819. (syntax-rules ()
  820. ((_ sequence encoding strategy (expected ...))
  821. (begin
  822. (pass-if (format #f "test-decoding-error: ~s ~s ~s"
  823. 'sequence encoding strategy)
  824. (let ((p (open-bytevector-input-port
  825. (u8-list->bytevector 'sequence))))
  826. (set-port-encoding! p encoding)
  827. (set-port-conversion-strategy! p strategy)
  828. (make-checks p
  829. (read-char -> expected) ...)))
  830. ;; Generate the same test, but with one
  831. ;; `peek-char' call before each `read-char'.
  832. ;; Both should yield the same result.
  833. (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
  834. 'sequence encoding strategy)
  835. (let ((p (open-bytevector-input-port
  836. (u8-list->bytevector 'sequence))))
  837. (set-port-encoding! p encoding)
  838. (set-port-conversion-strategy! p strategy)
  839. (make-peek+read-checks p #f expected
  840. ...)))))))
  841. (test-decoding-error
  842. (syntax-rules ()
  843. ((_ sequence encoding (expected ...))
  844. (begin
  845. (test-decoding-error* sequence encoding 'error
  846. (expected ...))
  847. ;; `escape' should behave exactly like `error'.
  848. (test-decoding-error* sequence encoding 'escape
  849. (expected ...))
  850. (test-decoding-error* sequence encoding 'substitute
  851. (expected ...)))))))
  852. (test-decoding-error (255 65 66 67) "UTF-8"
  853. (error #\A #\B #\C eof))
  854. (test-decoding-error (255 206 187 206 188) "UTF-8"
  855. (error #\λ #\μ eof))
  856. (test-decoding-error (206 187 206) "UTF-8"
  857. ;; Unterminated sequence.
  858. (#\λ error eof))
  859. ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
  860. ;; of the "Conformance" chapter of Unicode 6.0.0.)
  861. (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
  862. (error ;; C0: should be in the C2..DF range
  863. error ;; 80: invalid
  864. #\A
  865. eof))
  866. (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
  867. ;; Section 3.9 of Unicode 6.0.0 reads:
  868. ;; "If the converter encounters an ill-formed UTF-8 code unit
  869. ;; sequence which starts with a valid first byte, but which does
  870. ;; not continue with valid successor bytes (see Table 3-7), it
  871. ;; must not consume the successor bytes".
  872. ;; Glibc/libiconv do not conform to it and instead swallow the
  873. ;; #x41. This example appears literally in Section 3.9.
  874. (error ;; 41: invalid successor
  875. #\A ;; 41: valid starting byte
  876. #\B
  877. eof))
  878. (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
  879. ;; According to Unicode 6.0.0, Section 3.9, "the only formal
  880. ;; requirement mandated by Unicode conformance for a converter is
  881. ;; that the <41> be processed and correctly interpreted as
  882. ;; <U+0041>".
  883. (error ;; 2nd byte should be in the A0..BF range
  884. error ;; 80: not a valid starting byte
  885. error ;; 80: not a valid starting byte
  886. #\A
  887. eof))
  888. (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
  889. (error ;; 3rd byte should be in the 80..BF range
  890. #\A
  891. #\B
  892. eof))
  893. (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
  894. (error ;; 2nd byte should be in the 90..BF range
  895. error ;; 88: not a valid starting byte
  896. error ;; 88: not a valid starting byte
  897. error ;; 88: not a valid starting byte
  898. eof))))
  899. (with-test-prefix "call-with-output-string"
  900. ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
  901. ;; occur.
  902. (pass-if-exception "proc closes port" exception:wrong-type-arg
  903. (call-with-output-string close-port)))
  904. ;;;; Soft ports. No tests implemented yet.
  905. ;;;; Generic operations across all port types.
  906. (let ((port-loop-temp (test-file)))
  907. ;; Return a list of input ports that all return the same text.
  908. ;; We map tests over this list.
  909. (define (input-port-list text)
  910. ;; Create a text file some of the ports will use.
  911. (let ((out-port (open-output-file port-loop-temp)))
  912. (display text out-port)
  913. (close-port out-port))
  914. (list (open-input-file port-loop-temp)
  915. (open-input-pipe (string-append "cat " port-loop-temp))
  916. (call-with-input-string text (lambda (x) x))
  917. ;; We don't test soft ports at the moment.
  918. ))
  919. (define port-list-names '("file" "pipe" "string"))
  920. ;; Test the line counter.
  921. (define (test-line-counter text second-line final-column)
  922. (with-test-prefix "line counter"
  923. (let ((ports (input-port-list text)))
  924. (for-each
  925. (lambda (port port-name)
  926. (with-test-prefix port-name
  927. (pass-if "at beginning of input"
  928. (= (port-line port) 0))
  929. (pass-if "read first character"
  930. (eqv? (read-char port) #\x))
  931. (pass-if "after reading one character"
  932. (= (port-line port) 0))
  933. (pass-if "read first newline"
  934. (eqv? (read-char port) #\newline))
  935. (pass-if "after reading first newline char"
  936. (= (port-line port) 1))
  937. (pass-if "second line read correctly"
  938. (equal? (read-line port) second-line))
  939. (pass-if "read-line increments line number"
  940. (= (port-line port) 2))
  941. (pass-if "read-line returns EOF"
  942. (let loop ((i 0))
  943. (cond
  944. ((eof-object? (read-line port)) #t)
  945. ((> i 20) #f)
  946. (else (loop (+ i 1))))))
  947. (pass-if "line count is 5 at EOF"
  948. (= (port-line port) 5))
  949. (pass-if "column is correct at EOF"
  950. (= (port-column port) final-column))))
  951. ports port-list-names)
  952. (for-each close-port ports)
  953. (delete-file port-loop-temp))))
  954. (with-test-prefix "newline"
  955. (test-line-counter
  956. (string-append "x\n"
  957. "He who receives an idea from me, receives instruction\n"
  958. "himself without lessening mine; as he who lights his\n"
  959. "taper at mine, receives light without darkening me.\n"
  960. " --- Thomas Jefferson\n")
  961. "He who receives an idea from me, receives instruction"
  962. 0))
  963. (with-test-prefix "no newline"
  964. (test-line-counter
  965. (string-append "x\n"
  966. "He who receives an idea from me, receives instruction\n"
  967. "himself without lessening mine; as he who lights his\n"
  968. "taper at mine, receives light without darkening me.\n"
  969. " --- Thomas Jefferson\n"
  970. "no newline here")
  971. "He who receives an idea from me, receives instruction"
  972. 15)))
  973. ;; Test port-line and port-column for output ports
  974. (define (test-output-line-counter text final-column)
  975. (with-test-prefix "port-line and port-column for output ports"
  976. (let ((port (open-output-string)))
  977. (pass-if "at beginning of input"
  978. (and (= (port-line port) 0)
  979. (= (port-column port) 0)))
  980. (write-char #\x port)
  981. (pass-if "after writing one character"
  982. (and (= (port-line port) 0)
  983. (= (port-column port) 1)))
  984. (write-char #\newline port)
  985. (pass-if "after writing first newline char"
  986. (and (= (port-line port) 1)
  987. (= (port-column port) 0)))
  988. (display text port)
  989. (pass-if "line count is 5 at end"
  990. (= (port-line port) 5))
  991. (pass-if "column is correct at end"
  992. (= (port-column port) final-column)))))
  993. (test-output-line-counter
  994. (string-append "He who receives an idea from me, receives instruction\n"
  995. "himself without lessening mine; as he who lights his\n"
  996. "taper at mine, receives light without darkening me.\n"
  997. " --- Thomas Jefferson\n"
  998. "no newline here")
  999. 15)
  1000. (with-test-prefix "port-column"
  1001. (with-test-prefix "output"
  1002. (pass-if "x"
  1003. (let ((port (open-output-string)))
  1004. (display "x" port)
  1005. (= 1 (port-column port))))
  1006. (pass-if "\\a"
  1007. (let ((port (open-output-string)))
  1008. (display "\a" port)
  1009. (= 0 (port-column port))))
  1010. (pass-if "x\\a"
  1011. (let ((port (open-output-string)))
  1012. (display "x\a" port)
  1013. (= 1 (port-column port))))
  1014. (pass-if "\\x08 backspace"
  1015. (let ((port (open-output-string)))
  1016. (display "\x08" port)
  1017. (= 0 (port-column port))))
  1018. (pass-if "x\\x08 backspace"
  1019. (let ((port (open-output-string)))
  1020. (display "x\x08" port)
  1021. (= 0 (port-column port))))
  1022. (pass-if "\\n"
  1023. (let ((port (open-output-string)))
  1024. (display "\n" port)
  1025. (= 0 (port-column port))))
  1026. (pass-if "x\\n"
  1027. (let ((port (open-output-string)))
  1028. (display "x\n" port)
  1029. (= 0 (port-column port))))
  1030. (pass-if "\\r"
  1031. (let ((port (open-output-string)))
  1032. (display "\r" port)
  1033. (= 0 (port-column port))))
  1034. (pass-if "x\\r"
  1035. (let ((port (open-output-string)))
  1036. (display "x\r" port)
  1037. (= 0 (port-column port))))
  1038. (pass-if "\\t"
  1039. (let ((port (open-output-string)))
  1040. (display "\t" port)
  1041. (= 8 (port-column port))))
  1042. (pass-if "x\\t"
  1043. (let ((port (open-output-string)))
  1044. (display "x\t" port)
  1045. (= 8 (port-column port)))))
  1046. (with-test-prefix "input"
  1047. (pass-if "x"
  1048. (let ((port (open-input-string "x")))
  1049. (while (not (eof-object? (read-char port))))
  1050. (= 1 (port-column port))))
  1051. (pass-if "\\a"
  1052. (let ((port (open-input-string "\a")))
  1053. (while (not (eof-object? (read-char port))))
  1054. (= 0 (port-column port))))
  1055. (pass-if "x\\a"
  1056. (let ((port (open-input-string "x\a")))
  1057. (while (not (eof-object? (read-char port))))
  1058. (= 1 (port-column port))))
  1059. (pass-if "\\x08 backspace"
  1060. (let ((port (open-input-string "\x08")))
  1061. (while (not (eof-object? (read-char port))))
  1062. (= 0 (port-column port))))
  1063. (pass-if "x\\x08 backspace"
  1064. (let ((port (open-input-string "x\x08")))
  1065. (while (not (eof-object? (read-char port))))
  1066. (= 0 (port-column port))))
  1067. (pass-if "\\n"
  1068. (let ((port (open-input-string "\n")))
  1069. (while (not (eof-object? (read-char port))))
  1070. (= 0 (port-column port))))
  1071. (pass-if "x\\n"
  1072. (let ((port (open-input-string "x\n")))
  1073. (while (not (eof-object? (read-char port))))
  1074. (= 0 (port-column port))))
  1075. (pass-if "\\r"
  1076. (let ((port (open-input-string "\r")))
  1077. (while (not (eof-object? (read-char port))))
  1078. (= 0 (port-column port))))
  1079. (pass-if "x\\r"
  1080. (let ((port (open-input-string "x\r")))
  1081. (while (not (eof-object? (read-char port))))
  1082. (= 0 (port-column port))))
  1083. (pass-if "\\t"
  1084. (let ((port (open-input-string "\t")))
  1085. (while (not (eof-object? (read-char port))))
  1086. (= 8 (port-column port))))
  1087. (pass-if "x\\t"
  1088. (let ((port (open-input-string "x\t")))
  1089. (while (not (eof-object? (read-char port))))
  1090. (= 8 (port-column port))))))
  1091. (with-test-prefix "port-line"
  1092. ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
  1093. ;; scm_t_port actually holds a long; this restricted the range on 64-bit
  1094. ;; systems
  1095. (pass-if "set most-positive-fixnum/2"
  1096. (let ((n (quotient most-positive-fixnum 2))
  1097. (port (open-output-string)))
  1098. (set-port-line! port n)
  1099. (eqv? n (port-line port)))))
  1100. (with-test-prefix "port-encoding"
  1101. (pass-if-exception "set-port-encoding!, wrong encoding"
  1102. exception:miscellaneous-error
  1103. (let ((p (open-input-string "")))
  1104. (set-port-encoding! p "does-not-exist")
  1105. (read p)))
  1106. (let ((filename (test-file)))
  1107. (with-output-to-file filename (lambda () (write 'test)))
  1108. (pass-if-exception "%default-port-encoding, wrong encoding"
  1109. exception:miscellaneous-error
  1110. (read (with-fluids ((%default-port-encoding "does-not-exist"))
  1111. (open-input-file filename))))
  1112. (delete-file filename)))
  1113. ;;;
  1114. ;;; port-for-each
  1115. ;;;
  1116. (with-test-prefix "port-for-each"
  1117. ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
  1118. ;; its iterator func if a port was inaccessible in the last gc mark but
  1119. ;; the lazy sweeping has not yet reached it to remove it from the port
  1120. ;; table (scm_i_port_table). Provoking those gc conditions is a little
  1121. ;; tricky, but the following code made it happen in 1.8.2.
  1122. (pass-if "passing freed cell"
  1123. (let ((lst '()))
  1124. ;; clear out the heap
  1125. (gc) (gc) (gc)
  1126. ;; allocate cells so the opened ports aren't at the start of the heap
  1127. (make-list 1000)
  1128. (open-input-file "/dev/null")
  1129. (make-list 1000)
  1130. (open-input-file "/dev/null")
  1131. ;; this gc leaves the above ports unmarked, ie. inaccessible
  1132. (gc)
  1133. ;; but they're still in the port table, so this sees them
  1134. (port-for-each (lambda (port)
  1135. (set! lst (cons port lst))))
  1136. ;; this forces completion of the sweeping
  1137. (gc) (gc) (gc)
  1138. ;; and (if the bug is present) the cells accumulated in LST are now
  1139. ;; freed cells, which give #f from `port?'
  1140. (not (memq #f (map port? lst))))))
  1141. (with-test-prefix
  1142. "fdes->port"
  1143. (pass-if "fdes->ports finds port"
  1144. (let* ((port (open-file (test-file) "w"))
  1145. (res (not (not (memq port (fdes->ports (port->fdes port)))))))
  1146. (close-port port)
  1147. res)))
  1148. ;;;
  1149. ;;; seek
  1150. ;;;
  1151. (with-test-prefix "seek"
  1152. (with-test-prefix "file port"
  1153. (pass-if "SEEK_CUR"
  1154. (call-with-output-file (test-file)
  1155. (lambda (port)
  1156. (display "abcde" port)))
  1157. (let ((port (open-file (test-file) "r")))
  1158. (read-char port)
  1159. (seek port 2 SEEK_CUR)
  1160. (let ((res (eqv? #\d (read-char port))))
  1161. (close-port port)
  1162. res)))
  1163. (pass-if "SEEK_SET"
  1164. (call-with-output-file (test-file)
  1165. (lambda (port)
  1166. (display "abcde" port)))
  1167. (let ((port (open-file (test-file) "r")))
  1168. (read-char port)
  1169. (seek port 3 SEEK_SET)
  1170. (let ((res (eqv? #\d (read-char port))))
  1171. (close-port port)
  1172. res)))
  1173. (pass-if "SEEK_END"
  1174. (call-with-output-file (test-file)
  1175. (lambda (port)
  1176. (display "abcde" port)))
  1177. (let ((port (open-file (test-file) "r")))
  1178. (read-char port)
  1179. (seek port -2 SEEK_END)
  1180. (let ((res (eqv? #\d (read-char port))))
  1181. (close-port port)
  1182. res)))))
  1183. ;;;
  1184. ;;; truncate-file
  1185. ;;;
  1186. (with-test-prefix "truncate-file"
  1187. (pass-if-exception "flonum file" exception:wrong-type-arg
  1188. (truncate-file 1.0 123))
  1189. (pass-if-exception "frac file" exception:wrong-type-arg
  1190. (truncate-file 7/3 123))
  1191. (with-test-prefix "filename"
  1192. (pass-if-exception "flonum length" exception:wrong-type-arg
  1193. (call-with-output-file (test-file)
  1194. (lambda (port)
  1195. (display "hello" port)))
  1196. (truncate-file (test-file) 1.0))
  1197. (pass-if "shorten"
  1198. (call-with-output-file (test-file)
  1199. (lambda (port)
  1200. (display "hello" port)))
  1201. (truncate-file (test-file) 1)
  1202. (eqv? 1 (stat:size (stat (test-file)))))
  1203. (pass-if-exception "shorten to current pos" exception:miscellaneous-error
  1204. (call-with-output-file (test-file)
  1205. (lambda (port)
  1206. (display "hello" port)))
  1207. (truncate-file (test-file))))
  1208. (with-test-prefix "file descriptor"
  1209. (pass-if "shorten"
  1210. (call-with-output-file (test-file)
  1211. (lambda (port)
  1212. (display "hello" port)))
  1213. (let ((fd (open-fdes (test-file) O_RDWR)))
  1214. (truncate-file fd 1)
  1215. (close-fdes fd))
  1216. (eqv? 1 (stat:size (stat (test-file)))))
  1217. (pass-if "shorten to current pos"
  1218. (call-with-output-file (test-file)
  1219. (lambda (port)
  1220. (display "hello" port)))
  1221. (let ((fd (open-fdes (test-file) O_RDWR)))
  1222. (seek fd 1 SEEK_SET)
  1223. (truncate-file fd)
  1224. (close-fdes fd))
  1225. (eqv? 1 (stat:size (stat (test-file))))))
  1226. (with-test-prefix "file port"
  1227. (pass-if "shorten"
  1228. (call-with-output-file (test-file)
  1229. (lambda (port)
  1230. (display "hello" port)))
  1231. (let ((port (open-file (test-file) "r+")))
  1232. (truncate-file port 1)
  1233. (close-port port))
  1234. (eqv? 1 (stat:size (stat (test-file)))))
  1235. (pass-if "shorten to current pos"
  1236. (call-with-output-file (test-file)
  1237. (lambda (port)
  1238. (display "hello" port)))
  1239. (let ((port (open-file (test-file) "r+")))
  1240. (read-char port)
  1241. (truncate-file port)
  1242. (close-port port))
  1243. (eqv? 1 (stat:size (stat (test-file)))))))
  1244. ;;;; testing read-delimited and friends
  1245. (with-test-prefix "read-delimited!"
  1246. (let ((c (make-string 20 #\!)))
  1247. (call-with-input-string
  1248. "defdef\nghighi\n"
  1249. (lambda (port)
  1250. (read-delimited! "\n" c port 'concat)
  1251. (pass-if "read-delimited! reads a first line"
  1252. (string=? c "defdef\n!!!!!!!!!!!!!"))
  1253. (read-delimited! "\n" c port 'concat 3)
  1254. (pass-if "read-delimited! reads a first line"
  1255. (string=? c "defghighi\n!!!!!!!!!!"))))))
  1256. ;;;; char-ready?
  1257. (call-with-input-string
  1258. "howdy"
  1259. (lambda (port)
  1260. (pass-if "char-ready? returns true on string port"
  1261. (char-ready? port))))
  1262. ;;; This segfaults on some versions of Guile. We really should run
  1263. ;;; the tests in a subprocess...
  1264. (call-with-input-string
  1265. "howdy"
  1266. (lambda (port)
  1267. (with-input-from-port
  1268. port
  1269. (lambda ()
  1270. (pass-if "char-ready? returns true on string port as default port"
  1271. (char-ready?))))))
  1272. ;;;; pending-eof behavior
  1273. (with-test-prefix "pending EOF behavior"
  1274. ;; Make a test port that will produce the given sequence. Each
  1275. ;; element of 'lst' may be either a character or #f (which means EOF).
  1276. (define (test-soft-port . lst)
  1277. (make-soft-port
  1278. (vector (lambda (c) #f) ; write char
  1279. (lambda (s) #f) ; write string
  1280. (lambda () #f) ; flush
  1281. (lambda () ; read char
  1282. (let ((c (car lst)))
  1283. (set! lst (cdr lst))
  1284. c))
  1285. (lambda () #f)) ; close
  1286. "rw"))
  1287. (define (call-with-port p proc)
  1288. (dynamic-wind
  1289. (lambda () #f)
  1290. (lambda () (proc p))
  1291. (lambda () (close-port p))))
  1292. (define (call-with-test-file str proc)
  1293. (let ((filename (test-file)))
  1294. (dynamic-wind
  1295. (lambda () (call-with-output-file filename
  1296. (lambda (p) (display str p))))
  1297. (lambda () (call-with-input-file filename proc))
  1298. (lambda () (delete-file (test-file))))))
  1299. (pass-if "peek-char does not swallow EOF (soft port)"
  1300. (call-with-port (test-soft-port #\a #f #\b)
  1301. (lambda (p)
  1302. (and (char=? #\a (peek-char p))
  1303. (char=? #\a (read-char p))
  1304. (eof-object? (peek-char p))
  1305. (eof-object? (read-char p))
  1306. (char=? #\b (peek-char p))
  1307. (char=? #\b (read-char p))))))
  1308. (pass-if "unread clears pending EOF (soft port)"
  1309. (call-with-port (test-soft-port #\a #f #\b)
  1310. (lambda (p)
  1311. (and (char=? #\a (read-char p))
  1312. (eof-object? (peek-char p))
  1313. (begin (unread-char #\u p)
  1314. (char=? #\u (read-char p)))))))
  1315. (pass-if "unread clears pending EOF (string port)"
  1316. (call-with-input-string "a"
  1317. (lambda (p)
  1318. (and (char=? #\a (read-char p))
  1319. (eof-object? (peek-char p))
  1320. (begin (unread-char #\u p)
  1321. (char=? #\u (read-char p)))))))
  1322. (pass-if "unread clears pending EOF (file port)"
  1323. (call-with-test-file
  1324. "a"
  1325. (lambda (p)
  1326. (and (char=? #\a (read-char p))
  1327. (eof-object? (peek-char p))
  1328. (begin (unread-char #\u p)
  1329. (char=? #\u (read-char p)))))))
  1330. (pass-if "seek clears pending EOF (string port)"
  1331. (call-with-input-string "a"
  1332. (lambda (p)
  1333. (and (char=? #\a (read-char p))
  1334. (eof-object? (peek-char p))
  1335. (begin (seek p 0 SEEK_SET)
  1336. (char=? #\a (read-char p)))))))
  1337. (pass-if "seek clears pending EOF (file port)"
  1338. (call-with-test-file
  1339. "a"
  1340. (lambda (p)
  1341. (and (char=? #\a (read-char p))
  1342. (eof-object? (peek-char p))
  1343. (begin (seek p 0 SEEK_SET)
  1344. (char=? #\a (read-char p))))))))
  1345. ;;;; Close current-input-port, and make sure everyone can handle it.
  1346. (with-test-prefix "closing current-input-port"
  1347. (for-each (lambda (procedure name)
  1348. (with-input-from-port
  1349. (call-with-input-string "foo" (lambda (p) p))
  1350. (lambda ()
  1351. (close-port (current-input-port))
  1352. (pass-if-exception name
  1353. exception:wrong-type-arg
  1354. (procedure)))))
  1355. (list read read-char read-line)
  1356. '("read" "read-char" "read-line")))
  1357. (with-test-prefix "setvbuf"
  1358. (pass-if-exception "closed port"
  1359. exception:wrong-type-arg
  1360. (let ((port (open-input-file "/dev/null")))
  1361. (close-port port)
  1362. (setvbuf port 'block)))
  1363. (pass-if-exception "string port"
  1364. exception:wrong-type-arg
  1365. (let ((port (open-input-string "Hey!")))
  1366. (close-port port)
  1367. (setvbuf port 'block)))
  1368. (pass-if "line/column number preserved"
  1369. ;; In Guile 2.0.5, `setvbuf' would erroneously decrease the port's
  1370. ;; line and/or column number.
  1371. (call-with-output-file (test-file)
  1372. (lambda (p)
  1373. (display "This is GNU Guile.\nWelcome." p)))
  1374. (call-with-input-file (test-file)
  1375. (lambda (p)
  1376. (and (eqv? #\T (read-char p))
  1377. (let ((line (port-line p))
  1378. (col (port-column p)))
  1379. (and (= line 0) (= col 1)
  1380. (begin
  1381. (setvbuf p 'block 777)
  1382. (let ((line* (port-line p))
  1383. (col* (port-column p)))
  1384. (and (= line line*)
  1385. (= col col*)))))))))))
  1386. (pass-if-equal "unget-bytevector"
  1387. #vu8(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 200 201 202 203
  1388. 1 2 3 4 251 253 254 255)
  1389. (let ((port (open-bytevector-input-port #vu8(1 2 3 4 251 253 254 255))))
  1390. (unget-bytevector port #vu8(200 201 202 203))
  1391. (unget-bytevector port #vu8(20 21 22 23 24))
  1392. (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 4)
  1393. (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 2 2)
  1394. (unget-bytevector port #vu8(10 11))
  1395. (get-bytevector-all port)))
  1396. (with-test-prefix "unicode byte-order marks (BOMs)"
  1397. (define (bv-read-test* encoding bv proc)
  1398. (let ((port (open-bytevector-input-port bv)))
  1399. (set-port-encoding! port encoding)
  1400. (proc port)))
  1401. (define (bv-read-test encoding bv)
  1402. (bv-read-test* encoding bv read-string))
  1403. (define (bv-write-test* encoding proc)
  1404. (call-with-values
  1405. (lambda () (open-bytevector-output-port))
  1406. (lambda (port get-bytevector)
  1407. (set-port-encoding! port encoding)
  1408. (proc port)
  1409. (get-bytevector))))
  1410. (define (bv-write-test encoding str)
  1411. (bv-write-test* encoding
  1412. (lambda (p)
  1413. (display str p))))
  1414. (pass-if-equal "BOM not discarded from Latin-1 stream"
  1415. "\xEF\xBB\xBF\x61"
  1416. (bv-read-test "ISO-8859-1" #vu8(#xEF #xBB #xBF #x61)))
  1417. (pass-if-equal "BOM not discarded from Latin-2 stream"
  1418. "\u010F\u0165\u017C\x61"
  1419. (bv-read-test "ISO-8859-2" #vu8(#xEF #xBB #xBF #x61)))
  1420. (pass-if-equal "BOM not discarded from UTF-16BE stream"
  1421. "\uFEFF\x61"
  1422. (bv-read-test "UTF-16BE" #vu8(#xFE #xFF #x00 #x61)))
  1423. (pass-if-equal "BOM not discarded from UTF-16LE stream"
  1424. "\uFEFF\x61"
  1425. (bv-read-test "UTF-16LE" #vu8(#xFF #xFE #x61 #x00)))
  1426. (pass-if-equal "BOM not discarded from UTF-32BE stream"
  1427. "\uFEFF\x61"
  1428. (bv-read-test "UTF-32BE" #vu8(#x00 #x00 #xFE #xFF
  1429. #x00 #x00 #x00 #x61)))
  1430. (pass-if-equal "BOM not discarded from UTF-32LE stream"
  1431. "\uFEFF\x61"
  1432. (bv-read-test "UTF-32LE" #vu8(#xFF #xFE #x00 #x00
  1433. #x61 #x00 #x00 #x00)))
  1434. (pass-if-equal "BOM not written to UTF-8 stream"
  1435. #vu8(#x61)
  1436. (bv-write-test "UTF-8" "a"))
  1437. (pass-if-equal "BOM not written to UTF-16BE stream"
  1438. #vu8(#x00 #x61)
  1439. (bv-write-test "UTF-16BE" "a"))
  1440. (pass-if-equal "BOM not written to UTF-16LE stream"
  1441. #vu8(#x61 #x00)
  1442. (bv-write-test "UTF-16LE" "a"))
  1443. (pass-if-equal "BOM not written to UTF-32BE stream"
  1444. #vu8(#x00 #x00 #x00 #x61)
  1445. (bv-write-test "UTF-32BE" "a"))
  1446. (pass-if-equal "BOM not written to UTF-32LE stream"
  1447. #vu8(#x61 #x00 #x00 #x00)
  1448. (bv-write-test "UTF-32LE" "a"))
  1449. (pass-if "Don't read from the port unless user asks to"
  1450. (let* ((p (make-soft-port
  1451. (vector
  1452. (lambda (c) #f) ; write char
  1453. (lambda (s) #f) ; write string
  1454. (lambda () #f) ; flush
  1455. (lambda () (throw 'fail)) ; read char
  1456. (lambda () #f))
  1457. "rw")))
  1458. (set-port-encoding! p "UTF-16")
  1459. (display "abc" p)
  1460. (set-port-encoding! p "UTF-32")
  1461. (display "def" p)
  1462. #t))
  1463. ;; TODO: test that input and output streams are independent when
  1464. ;; appropriate, and linked when appropriate.
  1465. (pass-if-equal "BOM discarded from start of UTF-8 stream"
  1466. "a"
  1467. (bv-read-test "Utf-8" #vu8(#xEF #xBB #xBF #x61)))
  1468. (pass-if-equal "BOM discarded from start of UTF-8 stream after seek to 0"
  1469. '(#\a "a")
  1470. (bv-read-test* "uTf-8" #vu8(#xEF #xBB #xBF #x61)
  1471. (lambda (p)
  1472. (let ((c (read-char p)))
  1473. (seek p 0 SEEK_SET)
  1474. (let ((s (read-string p)))
  1475. (list c s))))))
  1476. (pass-if-equal "Only one BOM discarded from start of UTF-8 stream"
  1477. "\uFEFFa"
  1478. (bv-read-test "UTF-8" #vu8(#xEF #xBB #xBF #xEF #xBB #xBF #x61)))
  1479. (pass-if-equal "BOM not discarded from UTF-8 stream after seek to > 0"
  1480. "\uFEFFb"
  1481. (bv-read-test* "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)
  1482. (lambda (p)
  1483. (seek p 1 SEEK_SET)
  1484. (read-string p))))
  1485. (pass-if-equal "BOM not discarded unless at start of UTF-8 stream"
  1486. "a\uFEFFb"
  1487. (bv-read-test "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)))
  1488. (pass-if-equal "BOM (BE) written to start of UTF-16 stream"
  1489. #vu8(#xFE #xFF #x00 #x61 #x00 #x62)
  1490. (bv-write-test "UTF-16" "ab"))
  1491. (pass-if-equal "BOM (BE) written to UTF-16 stream after set-port-encoding!"
  1492. #vu8(#xFE #xFF #x00 #x61 #x00 #x62 #xFE #xFF #x00 #x63 #x00 #x64)
  1493. (bv-write-test* "UTF-16"
  1494. (lambda (p)
  1495. (display "ab" p)
  1496. (set-port-encoding! p "UTF-16")
  1497. (display "cd" p))))
  1498. (pass-if-equal "BOM discarded from start of UTF-16 stream (BE)"
  1499. "a"
  1500. (bv-read-test "UTF-16" #vu8(#xFE #xFF #x00 #x61)))
  1501. (pass-if-equal "BOM discarded from start of UTF-16 stream (BE) after seek to 0"
  1502. '(#\a "a")
  1503. (bv-read-test* "utf-16" #vu8(#xFE #xFF #x00 #x61)
  1504. (lambda (p)
  1505. (let ((c (read-char p)))
  1506. (seek p 0 SEEK_SET)
  1507. (let ((s (read-string p)))
  1508. (list c s))))))
  1509. (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (BE)"
  1510. "\uFEFFa"
  1511. (bv-read-test "Utf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)))
  1512. (pass-if-equal "BOM not discarded from UTF-16 stream (BE) after seek to > 0"
  1513. "\uFEFFa"
  1514. (bv-read-test* "uTf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)
  1515. (lambda (p)
  1516. (seek p 2 SEEK_SET)
  1517. (read-string p))))
  1518. (pass-if-equal "BOM not discarded unless at start of UTF-16 stream"
  1519. "a\uFEFFb"
  1520. (bv-read-test "utf-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)))
  1521. (pass-if-equal "BOM discarded from start of UTF-16 stream (LE)"
  1522. "a"
  1523. (bv-read-test "UTF-16" #vu8(#xFF #xFE #x61 #x00)))
  1524. (pass-if-equal "BOM discarded from start of UTF-16 stream (LE) after seek to 0"
  1525. '(#\a "a")
  1526. (bv-read-test* "Utf-16" #vu8(#xFF #xFE #x61 #x00)
  1527. (lambda (p)
  1528. (let ((c (read-char p)))
  1529. (seek p 0 SEEK_SET)
  1530. (let ((s (read-string p)))
  1531. (list c s))))))
  1532. (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (LE)"
  1533. "\uFEFFa"
  1534. (bv-read-test "UTf-16" #vu8(#xFF #xFE #xFF #xFE #x61 #x00)))
  1535. (pass-if-equal "BOM discarded from start of UTF-32 stream (BE)"
  1536. "a"
  1537. (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
  1538. #x00 #x00 #x00 #x61)))
  1539. (pass-if-equal "BOM discarded from start of UTF-32 stream (BE) after seek to 0"
  1540. '(#\a "a")
  1541. (bv-read-test* "utF-32" #vu8(#x00 #x00 #xFE #xFF
  1542. #x00 #x00 #x00 #x61)
  1543. (lambda (p)
  1544. (let ((c (read-char p)))
  1545. (seek p 0 SEEK_SET)
  1546. (let ((s (read-string p)))
  1547. (list c s))))))
  1548. (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (BE)"
  1549. "\uFEFFa"
  1550. (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
  1551. #x00 #x00 #xFE #xFF
  1552. #x00 #x00 #x00 #x61)))
  1553. (pass-if-equal "BOM not discarded from UTF-32 stream (BE) after seek to > 0"
  1554. "\uFEFFa"
  1555. (bv-read-test* "UtF-32" #vu8(#x00 #x00 #xFE #xFF
  1556. #x00 #x00 #xFE #xFF
  1557. #x00 #x00 #x00 #x61)
  1558. (lambda (p)
  1559. (seek p 4 SEEK_SET)
  1560. (read-string p))))
  1561. (pass-if-equal "BOM discarded within UTF-16 stream (BE) after set-port-encoding!"
  1562. "ab"
  1563. (bv-read-test* "UTF-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)
  1564. (lambda (p)
  1565. (let ((a (read-char p)))
  1566. (set-port-encoding! p "UTF-16")
  1567. (string a (read-char p))))))
  1568. (pass-if-equal "BOM discarded within UTF-16 stream (LE,BE) after set-port-encoding!"
  1569. "ab"
  1570. (bv-read-test* "utf-16" #vu8(#x00 #x61 #xFF #xFE #x62 #x00)
  1571. (lambda (p)
  1572. (let ((a (read-char p)))
  1573. (set-port-encoding! p "UTF-16")
  1574. (string a (read-char p))))))
  1575. (pass-if-equal "BOM discarded within UTF-32 stream (BE) after set-port-encoding!"
  1576. "ab"
  1577. (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1578. #x00 #x00 #xFE #xFF
  1579. #x00 #x00 #x00 #x62)
  1580. (lambda (p)
  1581. (let ((a (read-char p)))
  1582. (set-port-encoding! p "UTF-32")
  1583. (string a (read-char p))))))
  1584. (pass-if-equal "BOM discarded within UTF-32 stream (LE,BE) after set-port-encoding!"
  1585. "ab"
  1586. (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1587. #xFF #xFE #x00 #x00
  1588. #x62 #x00 #x00 #x00)
  1589. (lambda (p)
  1590. (let ((a (read-char p)))
  1591. (set-port-encoding! p "UTF-32")
  1592. (string a (read-char p))))))
  1593. (pass-if-equal "BOM not discarded unless at start of UTF-32 stream"
  1594. "a\uFEFFb"
  1595. (bv-read-test "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1596. #x00 #x00 #xFE #xFF
  1597. #x00 #x00 #x00 #x62)))
  1598. (pass-if-equal "BOM discarded from start of UTF-32 stream (LE)"
  1599. "a"
  1600. (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
  1601. #x61 #x00 #x00 #x00)))
  1602. (pass-if-equal "BOM discarded from start of UTF-32 stream (LE) after seek to 0"
  1603. '(#\a "a")
  1604. (bv-read-test* "UTf-32" #vu8(#xFF #xFE #x00 #x00
  1605. #x61 #x00 #x00 #x00)
  1606. (lambda (p)
  1607. (let ((c (read-char p)))
  1608. (seek p 0 SEEK_SET)
  1609. (let ((s (read-string p)))
  1610. (list c s))))))
  1611. (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (LE)"
  1612. "\uFEFFa"
  1613. (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
  1614. #xFF #xFE #x00 #x00
  1615. #x61 #x00 #x00 #x00))))
  1616. (define-syntax-rule (with-load-path path body ...)
  1617. (let ((new path)
  1618. (old %load-path))
  1619. (dynamic-wind
  1620. (lambda ()
  1621. (set! %load-path new))
  1622. (lambda ()
  1623. body ...)
  1624. (lambda ()
  1625. (set! %load-path old)))))
  1626. (with-test-prefix "%file-port-name-canonicalization"
  1627. (pass-if-equal "absolute file name & empty %load-path entry" "/dev/null"
  1628. ;; In Guile 2.0.5 and earlier, this would return "dev/null" instead
  1629. ;; of "/dev/null". See
  1630. ;; <http://lists.gnu.org/archive/html/guile-devel/2012-05/msg00059.html>
  1631. ;; for a discussion.
  1632. (with-load-path (cons "" (delete "/" %load-path))
  1633. (with-fluids ((%file-port-name-canonicalization 'relative))
  1634. (port-filename (open-input-file "/dev/null")))))
  1635. (pass-if-equal "relative canonicalization with /" "dev/null"
  1636. (with-load-path (cons "/" %load-path)
  1637. (with-fluids ((%file-port-name-canonicalization 'relative))
  1638. (port-filename (open-input-file "/dev/null")))))
  1639. (pass-if-equal "relative canonicalization with /dev/.." "dev/null"
  1640. (with-load-path (cons "/dev/.." %load-path)
  1641. (with-fluids ((%file-port-name-canonicalization 'relative))
  1642. (port-filename (open-input-file "/dev/null")))))
  1643. (pass-if-equal "relative canonicalization from ice-9" "ice-9/q.scm"
  1644. (with-fluids ((%file-port-name-canonicalization 'relative))
  1645. (port-filename
  1646. (open-input-file (%search-load-path "ice-9/q.scm")))))
  1647. (pass-if-equal "absolute canonicalization from ice-9"
  1648. (canonicalize-path
  1649. (string-append (assoc-ref %guile-build-info 'top_srcdir)
  1650. "/module/ice-9/q.scm"))
  1651. (with-fluids ((%file-port-name-canonicalization 'absolute))
  1652. (port-filename (open-input-file (%search-load-path "ice-9/q.scm"))))))
  1653. (with-test-prefix "file name separators"
  1654. (pass-if "no backslash separators in Windows file names"
  1655. ;; In Guile 2.0.11 and earlier, %load-path on Windows could
  1656. ;; include file names with backslashes, and `getcwd' on Windows
  1657. ;; would always return a directory name with backslashes.
  1658. (or (not (file-name-separator? #\\))
  1659. (with-load-path (cons (getcwd) %load-path)
  1660. (not (string-index (%search-load-path (basename (test-file)))
  1661. #\\))))))
  1662. (delete-file (test-file))
  1663. ;;; Local Variables:
  1664. ;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
  1665. ;;; eval: (put 'with-load-path 'scheme-indent-function 1)
  1666. ;;; End: