ports.test 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999
  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, 2017, 2019 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 (string-copy "λ")))
  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. (pass-if-equal "close-port & revealed port"
  541. EBADF
  542. (let* ((port (open-file "/dev/null" "r0"))
  543. (fdes (port->fdes port))) ;increments revealed count of PORT
  544. (close-port port) ;closes FDES as a side-effect
  545. (catch 'system-error
  546. (lambda ()
  547. (seek fdes 0 SEEK_CUR)
  548. #f)
  549. (lambda args
  550. (system-error-errno args)))))
  551. (pass-if "revealed port fdes not closed"
  552. (let* ((port (open-file "/dev/null" "r0"))
  553. (fdes (port->fdes port)) ;increments revealed count of PORT
  554. (guardian (make-guardian)))
  555. (guardian port)
  556. (set! port #f)
  557. (gc)
  558. (if (port? (guardian))
  559. (and (zero? (seek fdes 0 SEEK_CUR))
  560. (begin
  561. (close-fdes fdes)
  562. #t))
  563. (begin
  564. (close-fdes fdes)
  565. (throw 'unresolved)))))
  566. (when (provided? 'threads)
  567. (let* ((p (pipe))
  568. (r (car p))
  569. (w (cdr p)))
  570. (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
  571. (let ((thread (call-with-new-thread
  572. (lambda ()
  573. (usleep (* 250 1000))
  574. (write-char #\a w)
  575. (force-output w)))))
  576. (pass-if-equal "non-blocking-I/O" #\a (read-char r))
  577. (join-thread thread))))
  578. ;;;; Pipe (popen) ports.
  579. ;;; Run a command, and read its output.
  580. (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
  581. (in-string (read-all pipe)))
  582. (close-pipe pipe)
  583. (pass-if "pipe: read"
  584. (equal? in-string "Howdy there, partner!\n")))
  585. ;;; Run a command, send some output to it, and see if it worked.
  586. (let* ((filename (test-file))
  587. (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
  588. (display "Now Jimmy lives on a mushroom cloud\n" pipe)
  589. (display "Mommy, why does everybody have a bomb?\n" pipe)
  590. (close-pipe pipe)
  591. (let ((in-string (read-file filename)))
  592. (pass-if "pipe: write"
  593. (equal? in-string "Mommy, why does everybody have a bomb?\n")))
  594. (delete-file filename))
  595. (pass-if-equal "pipe, fdopen, and line buffering"
  596. "foo\nbar\n"
  597. (unless (provided? 'fork)
  598. (throw 'unresolved))
  599. (let ((in+out (pipe))
  600. (pid (primitive-fork)))
  601. (if (zero? pid)
  602. (dynamic-wind
  603. (const #t)
  604. (lambda ()
  605. (close-port (car in+out))
  606. (let ((port (cdr in+out)))
  607. (setvbuf port 'line )
  608. ;; Strings containing '\n' or should be flushed; others
  609. ;; should be kept in PORT's buffer.
  610. (display "foo\n" port)
  611. (display "bar\n" port)
  612. (display "this will be kept in PORT's buffer" port)))
  613. (lambda ()
  614. (primitive-_exit 0)))
  615. (begin
  616. (close-port (cdr in+out))
  617. (let ((str (read-all (car in+out))))
  618. (waitpid pid)
  619. str)))))
  620. ;;;; Void ports. These are so trivial we don't test them.
  621. ;;;; String ports.
  622. (with-test-prefix "string ports"
  623. ;; Write text to a string port.
  624. (let* ((string "Howdy there, partner!")
  625. (in-string (call-with-output-string
  626. (lambda (port)
  627. (display string port)
  628. (newline port)))))
  629. (pass-if "display text"
  630. (equal? in-string (string-append string "\n"))))
  631. ;; Write an s-expression to a string port.
  632. (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
  633. (in-sexpr
  634. (call-with-input-string (call-with-output-string
  635. (lambda (port)
  636. (write sexpr port)))
  637. read)))
  638. (pass-if "write/read sexpr"
  639. (equal? in-sexpr sexpr)))
  640. ;; seeking and unreading from an input string.
  641. (let ((text "that text didn't look random to me"))
  642. (call-with-input-string text
  643. (lambda (p)
  644. (pass-if "input tell 0"
  645. (= (seek p 0 SEEK_CUR) 0))
  646. (read-char p)
  647. (pass-if "input tell 1"
  648. (= (seek p 0 SEEK_CUR) 1))
  649. (unread-char #\x p)
  650. (pass-if "input tell back to 0"
  651. (= (seek p 0 SEEK_CUR) 0))
  652. (pass-if "input ungetted char"
  653. (char=? (read-char p) #\x))
  654. (seek p 0 SEEK_END)
  655. (pass-if "input seek to end"
  656. (= (seek p 0 SEEK_CUR)
  657. (string-length text)))
  658. (unread-char #\x p)
  659. (pass-if "input seek to beginning"
  660. (= (seek p 0 SEEK_SET) 0))
  661. (pass-if "input reread first char"
  662. (char=? (read-char p)
  663. (string-ref text 0))))))
  664. ;; seeking an output string.
  665. (let* ((text (string-copy "123456789"))
  666. (len (string-length text))
  667. (result (call-with-output-string
  668. (lambda (p)
  669. (pass-if "output tell 0"
  670. (= (seek p 0 SEEK_CUR) 0))
  671. (display text p)
  672. (pass-if "output tell end"
  673. (= (seek p 0 SEEK_CUR) len))
  674. (pass-if "output seek to beginning"
  675. (= (seek p 0 SEEK_SET) 0))
  676. (write-char #\a p)
  677. (seek p -1 SEEK_END)
  678. (pass-if "output seek to last char"
  679. (= (seek p 0 SEEK_CUR)
  680. (- len 1)))
  681. (write-char #\b p)))))
  682. (string-set! text 0 #\a)
  683. (string-set! text (- len 1) #\b)
  684. (pass-if "output check"
  685. (string=? text result)))
  686. (pass-if-exception "truncating input string fails"
  687. exception:wrong-type-arg
  688. (call-with-input-string "hej"
  689. (lambda (p)
  690. (truncate-file p 0))))
  691. (pass-if-equal "truncating output string" "hej"
  692. (call-with-output-string
  693. (lambda (p)
  694. (truncate-file p 0)
  695. (display "hej" p))))
  696. (pass-if-exception "truncating output string before position"
  697. exception:out-of-range
  698. (call-with-output-string
  699. (lambda (p)
  700. (display "hej" p)
  701. (truncate-file p 0))))
  702. (pass-if-equal "truncating output string at position" "hej"
  703. (call-with-output-string
  704. (lambda (p)
  705. (display "hej" p)
  706. (truncate-file p 3))))
  707. (pass-if-equal "truncating output string after seek" ""
  708. (call-with-output-string
  709. (lambda (p)
  710. (display "hej" p)
  711. (seek p 0 SEEK_SET)
  712. (truncate-file p 0))))
  713. (pass-if-equal "truncating output string after seek to end" "hej"
  714. (call-with-output-string
  715. (lambda (p)
  716. (display "hej" p)
  717. (seek p 0 SEEK_SET)
  718. (truncate-file p 3))))
  719. (pass-if "%default-port-encoding is ignored"
  720. (let ((str "ĉu bone?"))
  721. ;; Latin-1 cannot represent ‘ĉ’.
  722. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  723. (string=? (call-with-output-string
  724. (lambda (p)
  725. (set-port-conversion-strategy! p 'substitute)
  726. (display str p)))
  727. "ĉu bone?"))))
  728. (pass-if "%default-port-conversion-strategy is honored"
  729. (let ((strategies '(error substitute escape)))
  730. (equal? (map (lambda (s)
  731. (with-fluids ((%default-port-conversion-strategy s))
  732. (call-with-output-string
  733. (lambda (p)
  734. (and (eq? s (port-conversion-strategy p))
  735. (begin
  736. (set-port-conversion-strategy! p s)
  737. (display (port-conversion-strategy p)
  738. p)))))))
  739. strategies)
  740. (map symbol->string strategies))))
  741. (pass-if "suitable encoding [latin-1]"
  742. (let ((str "hello, world")
  743. (encoding "ISO-8859-1"))
  744. (equal? str
  745. (call-with-output-string
  746. (lambda (p)
  747. (set-port-encoding! p encoding)
  748. (display str p))))))
  749. (pass-if "suitable encoding [latin-3]"
  750. (let ((str "ĉu bone?")
  751. (encoding "ISO-8859-3"))
  752. (equal? str
  753. (call-with-output-string
  754. (lambda (p)
  755. (set-port-encoding! p encoding)
  756. (display str p))))))
  757. (pass-if "wrong encoding, error"
  758. (let ((str "ĉu bone?"))
  759. (catch 'encoding-error
  760. (lambda ()
  761. (with-fluids ((%default-port-conversion-strategy 'error))
  762. (call-with-output-string
  763. (lambda (p)
  764. ;; Latin-1 cannot represent ‘ĉ’.
  765. (set-port-encoding! p "ISO-8859-1")
  766. (display str p))))
  767. #f) ; so the test really fails here
  768. (lambda (key subr message errno port chr)
  769. (and (eqv? chr #\ĉ)
  770. (string? (strerror errno)))))))
  771. (pass-if "wrong encoding, substitute"
  772. (let ((str "ĉu bone?"))
  773. (string=? (call-with-output-string
  774. (lambda (p)
  775. (set-port-encoding! p "ISO-8859-1")
  776. (set-port-conversion-strategy! p 'substitute)
  777. (display str p)))
  778. "?u bone?")))
  779. (pass-if "wrong encoding, escape"
  780. (let ((str "ĉu bone?"))
  781. (string=? (call-with-output-string
  782. (lambda (p)
  783. (set-port-encoding! p "ISO-8859-1")
  784. (set-port-conversion-strategy! p 'escape)
  785. (display str p)))
  786. "\\u0109u bone?")))
  787. (pass-if "peek-char"
  788. (let ((p (open-input-string "안녕하세요")))
  789. (and (char=? (peek-char p) #\안)
  790. (char=? (peek-char p) #\안)
  791. (char=? (peek-char p) #\안)
  792. (= (port-line p) 0)
  793. (= (port-column p) 0))))
  794. ;; Mini DSL to test decoding error handling.
  795. (letrec-syntax ((decoding-error?
  796. (syntax-rules ()
  797. ((_ port proc)
  798. (catch 'decoding-error
  799. (lambda ()
  800. (pk 'proc (proc port))
  801. #f)
  802. (lambda (key subr message errno p)
  803. (define (skip-over-error)
  804. (let ((strategy (port-conversion-strategy p)))
  805. (set-port-conversion-strategy! p 'substitute)
  806. ;; If `proc' is `read-char', this will
  807. ;; skip over the bad bytes.
  808. (let ((c (proc p)))
  809. (unless (eqv? c #\xFFFD)
  810. (error "unexpected char" c))
  811. (set-port-conversion-strategy! p strategy)
  812. #t)))
  813. (and (eq? p port)
  814. (not (= 0 errno))
  815. (skip-over-error)))))))
  816. (make-check
  817. (syntax-rules (-> error eof)
  818. ((_ port (proc -> error))
  819. (if (eq? 'substitute
  820. (port-conversion-strategy port))
  821. (eqv? (proc port) #\xFFFD)
  822. (decoding-error? port proc)))
  823. ((_ port (proc -> eof))
  824. (eof-object? (proc port)))
  825. ((_ port (proc -> char))
  826. (eqv? (proc port) char))))
  827. (make-checks
  828. (syntax-rules ()
  829. ((_ port check ...)
  830. (and (make-check port check) ...))))
  831. (make-peek+read-checks
  832. (syntax-rules ()
  833. ((_ port (result ...) e1 expected ...)
  834. (make-peek+read-checks port
  835. (result ...
  836. (peek-char -> e1)
  837. (read-char -> e1))
  838. expected ...))
  839. ((_ port (result ...))
  840. (make-checks port result ...))
  841. ((_ port #f e1 expected ...)
  842. (make-peek+read-checks port
  843. ((peek-char -> e1)
  844. (read-char -> e1))
  845. expected ...))))
  846. (test-decoding-error*
  847. (syntax-rules ()
  848. ((_ sequence encoding strategy (expected ...))
  849. (begin
  850. (pass-if (format #f "test-decoding-error: ~s ~s ~s"
  851. 'sequence encoding strategy)
  852. (let ((p (open-bytevector-input-port
  853. (u8-list->bytevector 'sequence))))
  854. (set-port-encoding! p encoding)
  855. (set-port-conversion-strategy! p strategy)
  856. (make-checks p
  857. (read-char -> expected) ...)))
  858. ;; Generate the same test, but with one
  859. ;; `peek-char' call before each `read-char'.
  860. ;; Both should yield the same result.
  861. (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
  862. 'sequence encoding strategy)
  863. (let ((p (open-bytevector-input-port
  864. (u8-list->bytevector 'sequence))))
  865. (set-port-encoding! p encoding)
  866. (set-port-conversion-strategy! p strategy)
  867. (make-peek+read-checks p #f expected
  868. ...)))))))
  869. (test-decoding-error
  870. (syntax-rules ()
  871. ((_ sequence encoding (expected ...))
  872. (begin
  873. (test-decoding-error* sequence encoding 'error
  874. (expected ...))
  875. ;; `escape' should behave exactly like `error'.
  876. (test-decoding-error* sequence encoding 'escape
  877. (expected ...))
  878. (test-decoding-error* sequence encoding 'substitute
  879. (expected ...)))))))
  880. (test-decoding-error (255 65 66 67) "UTF-8"
  881. (error #\A #\B #\C eof))
  882. (test-decoding-error (255 206 187 206 188) "UTF-8"
  883. (error #\λ #\μ eof))
  884. (test-decoding-error (206 187 206) "UTF-8"
  885. ;; Unterminated sequence.
  886. (#\λ error eof))
  887. ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
  888. ;; of the "Conformance" chapter of Unicode 6.0.0.)
  889. (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
  890. (error ;; C0: should be in the C2..DF range
  891. error ;; 80: invalid
  892. #\A
  893. eof))
  894. (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
  895. ;; Section 3.9 of Unicode 6.0.0 reads:
  896. ;; "If the converter encounters an ill-formed UTF-8 code unit
  897. ;; sequence which starts with a valid first byte, but which does
  898. ;; not continue with valid successor bytes (see Table 3-7), it
  899. ;; must not consume the successor bytes".
  900. ;; Glibc/libiconv do not conform to it and instead swallow the
  901. ;; #x41. This example appears literally in Section 3.9.
  902. (error ;; 41: invalid successor
  903. #\A ;; 41: valid starting byte
  904. #\B
  905. eof))
  906. (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
  907. ;; According to Unicode 6.0.0, Section 3.9, "the only formal
  908. ;; requirement mandated by Unicode conformance for a converter is
  909. ;; that the <41> be processed and correctly interpreted as
  910. ;; <U+0041>".
  911. (error ;; 2nd byte should be in the A0..BF range
  912. error ;; 80: not a valid starting byte
  913. error ;; 80: not a valid starting byte
  914. #\A
  915. eof))
  916. (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
  917. (error ;; 3rd byte should be in the 80..BF range
  918. #\A
  919. #\B
  920. eof))
  921. (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
  922. (error ;; 2nd byte should be in the 90..BF range
  923. error ;; 88: not a valid starting byte
  924. error ;; 88: not a valid starting byte
  925. error ;; 88: not a valid starting byte
  926. eof))))
  927. (with-test-prefix "call-with-output-string"
  928. ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
  929. ;; occur.
  930. (pass-if-exception "proc closes port" exception:wrong-type-arg
  931. (call-with-output-string close-port)))
  932. ;;;; Soft ports. No tests implemented yet.
  933. ;;;; Generic operations across all port types.
  934. (let ((port-loop-temp (test-file)))
  935. ;; Return a list of input ports that all return the same text.
  936. ;; We map tests over this list.
  937. (define (input-port-list text)
  938. ;; Create a text file some of the ports will use.
  939. (let ((out-port (open-output-file port-loop-temp)))
  940. (display text out-port)
  941. (close-port out-port))
  942. (list (open-input-file port-loop-temp)
  943. (open-input-pipe (string-append "cat " port-loop-temp))
  944. (call-with-input-string text (lambda (x) x))
  945. ;; We don't test soft ports at the moment.
  946. ))
  947. (define port-list-names '("file" "pipe" "string"))
  948. ;; Test the line counter.
  949. (define (test-line-counter text second-line final-column)
  950. (with-test-prefix "line counter"
  951. (let ((ports (input-port-list text)))
  952. (for-each
  953. (lambda (port port-name)
  954. (with-test-prefix port-name
  955. (pass-if "at beginning of input"
  956. (= (port-line port) 0))
  957. (pass-if "read first character"
  958. (eqv? (read-char port) #\x))
  959. (pass-if "after reading one character"
  960. (= (port-line port) 0))
  961. (pass-if "read first newline"
  962. (eqv? (read-char port) #\newline))
  963. (pass-if "after reading first newline char"
  964. (= (port-line port) 1))
  965. (pass-if "second line read correctly"
  966. (equal? (read-line port) second-line))
  967. (pass-if "read-line increments line number"
  968. (= (port-line port) 2))
  969. (pass-if "read-line returns EOF"
  970. (let loop ((i 0))
  971. (cond
  972. ((eof-object? (read-line port)) #t)
  973. ((> i 20) #f)
  974. (else (loop (+ i 1))))))
  975. (pass-if "line count is 5 at EOF"
  976. (= (port-line port) 5))
  977. (pass-if "column is correct at EOF"
  978. (= (port-column port) final-column))))
  979. ports port-list-names)
  980. (for-each close-port ports)
  981. (delete-file port-loop-temp))))
  982. (with-test-prefix "newline"
  983. (test-line-counter
  984. (string-append "x\n"
  985. "He who receives an idea from me, receives instruction\n"
  986. "himself without lessening mine; as he who lights his\n"
  987. "taper at mine, receives light without darkening me.\n"
  988. " --- Thomas Jefferson\n")
  989. "He who receives an idea from me, receives instruction"
  990. 0))
  991. (with-test-prefix "no newline"
  992. (test-line-counter
  993. (string-append "x\n"
  994. "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. "He who receives an idea from me, receives instruction"
  1000. 15)))
  1001. ;; Test port-line and port-column for output ports
  1002. (define (test-output-line-counter text final-column)
  1003. (with-test-prefix "port-line and port-column for output ports"
  1004. (let ((port (open-output-string)))
  1005. (pass-if "at beginning of input"
  1006. (and (= (port-line port) 0)
  1007. (= (port-column port) 0)))
  1008. (write-char #\x port)
  1009. (pass-if "after writing one character"
  1010. (and (= (port-line port) 0)
  1011. (= (port-column port) 1)))
  1012. (write-char #\newline port)
  1013. (pass-if "after writing first newline char"
  1014. (and (= (port-line port) 1)
  1015. (= (port-column port) 0)))
  1016. (display text port)
  1017. (pass-if "line count is 5 at end"
  1018. (= (port-line port) 5))
  1019. (pass-if "column is correct at end"
  1020. (= (port-column port) final-column)))))
  1021. (test-output-line-counter
  1022. (string-append "He who receives an idea from me, receives instruction\n"
  1023. "himself without lessening mine; as he who lights his\n"
  1024. "taper at mine, receives light without darkening me.\n"
  1025. " --- Thomas Jefferson\n"
  1026. "no newline here")
  1027. 15)
  1028. (with-test-prefix "port-column"
  1029. (with-test-prefix "output"
  1030. (pass-if "x"
  1031. (let ((port (open-output-string)))
  1032. (display "x" port)
  1033. (= 1 (port-column port))))
  1034. (pass-if "\\a"
  1035. (let ((port (open-output-string)))
  1036. (display "\a" port)
  1037. (= 0 (port-column port))))
  1038. (pass-if "x\\a"
  1039. (let ((port (open-output-string)))
  1040. (display "x\a" port)
  1041. (= 1 (port-column port))))
  1042. (pass-if "\\x08 backspace"
  1043. (let ((port (open-output-string)))
  1044. (display "\x08" port)
  1045. (= 0 (port-column port))))
  1046. (pass-if "x\\x08 backspace"
  1047. (let ((port (open-output-string)))
  1048. (display "x\x08" port)
  1049. (= 0 (port-column port))))
  1050. (pass-if "\\n"
  1051. (let ((port (open-output-string)))
  1052. (display "\n" port)
  1053. (= 0 (port-column port))))
  1054. (pass-if "x\\n"
  1055. (let ((port (open-output-string)))
  1056. (display "x\n" port)
  1057. (= 0 (port-column port))))
  1058. (pass-if "\\r"
  1059. (let ((port (open-output-string)))
  1060. (display "\r" port)
  1061. (= 0 (port-column port))))
  1062. (pass-if "x\\r"
  1063. (let ((port (open-output-string)))
  1064. (display "x\r" port)
  1065. (= 0 (port-column port))))
  1066. (pass-if "\\t"
  1067. (let ((port (open-output-string)))
  1068. (display "\t" port)
  1069. (= 8 (port-column port))))
  1070. (pass-if "x\\t"
  1071. (let ((port (open-output-string)))
  1072. (display "x\t" port)
  1073. (= 8 (port-column port)))))
  1074. (with-test-prefix "input"
  1075. (pass-if "x"
  1076. (let ((port (open-input-string "x")))
  1077. (while (not (eof-object? (read-char port))))
  1078. (= 1 (port-column port))))
  1079. (pass-if "\\a"
  1080. (let ((port (open-input-string "\a")))
  1081. (while (not (eof-object? (read-char port))))
  1082. (= 0 (port-column port))))
  1083. (pass-if "x\\a"
  1084. (let ((port (open-input-string "x\a")))
  1085. (while (not (eof-object? (read-char port))))
  1086. (= 1 (port-column port))))
  1087. (pass-if "\\x08 backspace"
  1088. (let ((port (open-input-string "\x08")))
  1089. (while (not (eof-object? (read-char port))))
  1090. (= 0 (port-column port))))
  1091. (pass-if "x\\x08 backspace"
  1092. (let ((port (open-input-string "x\x08")))
  1093. (while (not (eof-object? (read-char port))))
  1094. (= 0 (port-column port))))
  1095. (pass-if "\\n"
  1096. (let ((port (open-input-string "\n")))
  1097. (while (not (eof-object? (read-char port))))
  1098. (= 0 (port-column port))))
  1099. (pass-if "x\\n"
  1100. (let ((port (open-input-string "x\n")))
  1101. (while (not (eof-object? (read-char port))))
  1102. (= 0 (port-column port))))
  1103. (pass-if "\\r"
  1104. (let ((port (open-input-string "\r")))
  1105. (while (not (eof-object? (read-char port))))
  1106. (= 0 (port-column port))))
  1107. (pass-if "x\\r"
  1108. (let ((port (open-input-string "x\r")))
  1109. (while (not (eof-object? (read-char port))))
  1110. (= 0 (port-column port))))
  1111. (pass-if "\\t"
  1112. (let ((port (open-input-string "\t")))
  1113. (while (not (eof-object? (read-char port))))
  1114. (= 8 (port-column port))))
  1115. (pass-if "x\\t"
  1116. (let ((port (open-input-string "x\t")))
  1117. (while (not (eof-object? (read-char port))))
  1118. (= 8 (port-column port))))))
  1119. (with-test-prefix "port-line"
  1120. ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
  1121. ;; scm_t_port actually holds a long; this restricted the range on 64-bit
  1122. ;; systems
  1123. (pass-if "set most-positive-fixnum/2"
  1124. (let ((n (quotient most-positive-fixnum 2))
  1125. (port (open-output-string)))
  1126. (set-port-line! port n)
  1127. (eqv? n (port-line port)))))
  1128. (with-test-prefix "port-encoding"
  1129. (pass-if-exception "set-port-encoding!, wrong encoding"
  1130. exception:miscellaneous-error
  1131. (let ((p (open-input-string "")))
  1132. (set-port-encoding! p "does-not-exist")
  1133. (read p)))
  1134. (let ((filename (test-file)))
  1135. (with-output-to-file filename (lambda () (write 'test)))
  1136. (pass-if-exception "%default-port-encoding, wrong encoding"
  1137. exception:miscellaneous-error
  1138. (read (with-fluids ((%default-port-encoding "does-not-exist"))
  1139. (open-input-file filename))))
  1140. (delete-file filename)))
  1141. ;;;
  1142. ;;; port-for-each
  1143. ;;;
  1144. (with-test-prefix "port-for-each"
  1145. ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
  1146. ;; its iterator func if a port was inaccessible in the last gc mark but
  1147. ;; the lazy sweeping has not yet reached it to remove it from the port
  1148. ;; table (scm_i_port_table). Provoking those gc conditions is a little
  1149. ;; tricky, but the following code made it happen in 1.8.2.
  1150. (pass-if "passing freed cell"
  1151. (let ((lst '()))
  1152. ;; clear out the heap
  1153. (gc) (gc) (gc)
  1154. ;; allocate cells so the opened ports aren't at the start of the heap
  1155. (make-list 1000)
  1156. (open-input-file "/dev/null")
  1157. (make-list 1000)
  1158. (open-input-file "/dev/null")
  1159. ;; this gc leaves the above ports unmarked, ie. inaccessible
  1160. (gc)
  1161. ;; but they're still in the port table, so this sees them
  1162. (port-for-each (lambda (port)
  1163. (set! lst (cons port lst))))
  1164. ;; this forces completion of the sweeping
  1165. (gc) (gc) (gc)
  1166. ;; and (if the bug is present) the cells accumulated in LST are now
  1167. ;; freed cells, which give #f from `port?'
  1168. (not (memq #f (map port? lst))))))
  1169. (with-test-prefix
  1170. "fdes->port"
  1171. (pass-if "fdes->ports finds port"
  1172. (let* ((port (open-file (test-file) "w"))
  1173. (res (not (not (memq port (fdes->ports (port->fdes port)))))))
  1174. (close-port port)
  1175. res)))
  1176. ;;;
  1177. ;;; seek
  1178. ;;;
  1179. (with-test-prefix "seek"
  1180. (with-test-prefix "file port"
  1181. (pass-if "SEEK_CUR"
  1182. (call-with-output-file (test-file)
  1183. (lambda (port)
  1184. (display "abcde" port)))
  1185. (let ((port (open-file (test-file) "r")))
  1186. (read-char port)
  1187. (seek port 2 SEEK_CUR)
  1188. (let ((res (eqv? #\d (read-char port))))
  1189. (close-port port)
  1190. res)))
  1191. (pass-if "SEEK_SET"
  1192. (call-with-output-file (test-file)
  1193. (lambda (port)
  1194. (display "abcde" port)))
  1195. (let ((port (open-file (test-file) "r")))
  1196. (read-char port)
  1197. (seek port 3 SEEK_SET)
  1198. (let ((res (eqv? #\d (read-char port))))
  1199. (close-port port)
  1200. res)))
  1201. (pass-if "SEEK_END"
  1202. (call-with-output-file (test-file)
  1203. (lambda (port)
  1204. (display "abcde" port)))
  1205. (let ((port (open-file (test-file) "r")))
  1206. (read-char port)
  1207. (seek port -2 SEEK_END)
  1208. (let ((res (eqv? #\d (read-char port))))
  1209. (close-port port)
  1210. res)))))
  1211. ;;;
  1212. ;;; truncate-file
  1213. ;;;
  1214. (with-test-prefix "truncate-file"
  1215. (pass-if-exception "flonum file" exception:wrong-type-arg
  1216. (truncate-file 1.0 123))
  1217. (pass-if-exception "frac file" exception:wrong-type-arg
  1218. (truncate-file 7/3 123))
  1219. (with-test-prefix "filename"
  1220. (pass-if-exception "flonum length" exception:wrong-type-arg
  1221. (call-with-output-file (test-file)
  1222. (lambda (port)
  1223. (display "hello" port)))
  1224. (truncate-file (test-file) 1.0))
  1225. (pass-if "shorten"
  1226. (call-with-output-file (test-file)
  1227. (lambda (port)
  1228. (display "hello" port)))
  1229. (truncate-file (test-file) 1)
  1230. (eqv? 1 (stat:size (stat (test-file)))))
  1231. (pass-if-exception "shorten to current pos" exception:miscellaneous-error
  1232. (call-with-output-file (test-file)
  1233. (lambda (port)
  1234. (display "hello" port)))
  1235. (truncate-file (test-file))))
  1236. (with-test-prefix "file descriptor"
  1237. (pass-if "shorten"
  1238. (call-with-output-file (test-file)
  1239. (lambda (port)
  1240. (display "hello" port)))
  1241. (let ((fd (open-fdes (test-file) O_RDWR)))
  1242. (truncate-file fd 1)
  1243. (close-fdes fd))
  1244. (eqv? 1 (stat:size (stat (test-file)))))
  1245. (pass-if "shorten to current pos"
  1246. (call-with-output-file (test-file)
  1247. (lambda (port)
  1248. (display "hello" port)))
  1249. (let ((fd (open-fdes (test-file) O_RDWR)))
  1250. (seek fd 1 SEEK_SET)
  1251. (truncate-file fd)
  1252. (close-fdes fd))
  1253. (eqv? 1 (stat:size (stat (test-file))))))
  1254. (with-test-prefix "file port"
  1255. (pass-if "shorten"
  1256. (call-with-output-file (test-file)
  1257. (lambda (port)
  1258. (display "hello" port)))
  1259. (let ((port (open-file (test-file) "r+")))
  1260. (truncate-file port 1)
  1261. (close-port port))
  1262. (eqv? 1 (stat:size (stat (test-file)))))
  1263. (pass-if "shorten to current pos"
  1264. (call-with-output-file (test-file)
  1265. (lambda (port)
  1266. (display "hello" port)))
  1267. (let ((port (open-file (test-file) "r+")))
  1268. (read-char port)
  1269. (truncate-file port)
  1270. (close-port port))
  1271. (eqv? 1 (stat:size (stat (test-file)))))))
  1272. ;;;; testing read-delimited and friends
  1273. (with-test-prefix "read-delimited!"
  1274. (let ((c (make-string 20 #\!)))
  1275. (call-with-input-string
  1276. "defdef\nghighi\n"
  1277. (lambda (port)
  1278. (read-delimited! "\n" c port 'concat)
  1279. (pass-if "read-delimited! reads a first line"
  1280. (string=? c "defdef\n!!!!!!!!!!!!!"))
  1281. (read-delimited! "\n" c port 'concat 3)
  1282. (pass-if "read-delimited! reads a first line"
  1283. (string=? c "defghighi\n!!!!!!!!!!"))))))
  1284. ;;;; char-ready?
  1285. (call-with-input-string
  1286. "howdy"
  1287. (lambda (port)
  1288. (pass-if "char-ready? returns true on string port"
  1289. (char-ready? port))))
  1290. ;;; This segfaults on some versions of Guile. We really should run
  1291. ;;; the tests in a subprocess...
  1292. (call-with-input-string
  1293. "howdy"
  1294. (lambda (port)
  1295. (with-input-from-port
  1296. port
  1297. (lambda ()
  1298. (pass-if "char-ready? returns true on string port as default port"
  1299. (char-ready?))))))
  1300. ;;;; pending-eof behavior
  1301. (with-test-prefix "pending EOF behavior"
  1302. ;; Make a test port that will produce the given sequence. Each
  1303. ;; element of 'lst' may be either a character or #f (which means EOF).
  1304. (define (test-soft-port . lst)
  1305. (make-soft-port
  1306. (vector (lambda (c) #f) ; write char
  1307. (lambda (s) #f) ; write string
  1308. (lambda () #f) ; flush
  1309. (lambda () ; read char
  1310. (let ((c (car lst)))
  1311. (set! lst (cdr lst))
  1312. c))
  1313. (lambda () #f)) ; close
  1314. "rw"))
  1315. (define (call-with-port p proc)
  1316. (dynamic-wind
  1317. (lambda () #f)
  1318. (lambda () (proc p))
  1319. (lambda () (close-port p))))
  1320. (define (call-with-test-file str proc)
  1321. (let ((filename (test-file)))
  1322. (dynamic-wind
  1323. (lambda () (call-with-output-file filename
  1324. (lambda (p) (display str p))))
  1325. (lambda () (call-with-input-file filename proc))
  1326. (lambda () (delete-file (test-file))))))
  1327. (pass-if "peek-char does not swallow EOF (soft port)"
  1328. (call-with-port (test-soft-port #\a #f #\b)
  1329. (lambda (p)
  1330. (and (char=? #\a (peek-char p))
  1331. (char=? #\a (read-char p))
  1332. (eof-object? (peek-char p))
  1333. (eof-object? (read-char p))
  1334. (char=? #\b (peek-char p))
  1335. (char=? #\b (read-char p))))))
  1336. (pass-if "unread clears pending EOF (soft port)"
  1337. (call-with-port (test-soft-port #\a #f #\b)
  1338. (lambda (p)
  1339. (and (char=? #\a (read-char p))
  1340. (eof-object? (peek-char p))
  1341. (begin (unread-char #\u p)
  1342. (char=? #\u (read-char p)))))))
  1343. (pass-if "unread clears pending EOF (string port)"
  1344. (call-with-input-string "a"
  1345. (lambda (p)
  1346. (and (char=? #\a (read-char p))
  1347. (eof-object? (peek-char p))
  1348. (begin (unread-char #\u p)
  1349. (char=? #\u (read-char p)))))))
  1350. (pass-if "unread clears pending EOF (file port)"
  1351. (call-with-test-file
  1352. "a"
  1353. (lambda (p)
  1354. (and (char=? #\a (read-char p))
  1355. (eof-object? (peek-char p))
  1356. (begin (unread-char #\u p)
  1357. (char=? #\u (read-char p)))))))
  1358. (pass-if "seek clears pending EOF (string port)"
  1359. (call-with-input-string "a"
  1360. (lambda (p)
  1361. (and (char=? #\a (read-char p))
  1362. (eof-object? (peek-char p))
  1363. (begin (seek p 0 SEEK_SET)
  1364. (char=? #\a (read-char p)))))))
  1365. (pass-if "seek clears pending EOF (file port)"
  1366. (call-with-test-file
  1367. "a"
  1368. (lambda (p)
  1369. (and (char=? #\a (read-char p))
  1370. (eof-object? (peek-char p))
  1371. (begin (seek p 0 SEEK_SET)
  1372. (char=? #\a (read-char p))))))))
  1373. ;;;; Close current-input-port, and make sure everyone can handle it.
  1374. (with-test-prefix "closing current-input-port"
  1375. (for-each (lambda (procedure name)
  1376. (with-input-from-port
  1377. (call-with-input-string "foo" (lambda (p) p))
  1378. (lambda ()
  1379. (close-port (current-input-port))
  1380. (pass-if-exception name
  1381. exception:wrong-type-arg
  1382. (procedure)))))
  1383. (list read read-char read-line)
  1384. '("read" "read-char" "read-line")))
  1385. (with-test-prefix "setvbuf"
  1386. (pass-if-exception "closed port"
  1387. exception:wrong-type-arg
  1388. (let ((port (open-input-file "/dev/null")))
  1389. (close-port port)
  1390. (setvbuf port 'block)))
  1391. (pass-if-exception "string port"
  1392. exception:wrong-type-arg
  1393. (let ((port (open-input-string "Hey!")))
  1394. (close-port port)
  1395. (setvbuf port 'block)))
  1396. (pass-if "line/column number preserved"
  1397. ;; In Guile 2.0.5, `setvbuf' would erroneously decrease the port's
  1398. ;; line and/or column number.
  1399. (call-with-output-file (test-file)
  1400. (lambda (p)
  1401. (display "This is GNU Guile.\nWelcome." p)))
  1402. (call-with-input-file (test-file)
  1403. (lambda (p)
  1404. (and (eqv? #\T (read-char p))
  1405. (let ((line (port-line p))
  1406. (col (port-column p)))
  1407. (and (= line 0) (= col 1)
  1408. (begin
  1409. (setvbuf p 'block 777)
  1410. (let ((line* (port-line p))
  1411. (col* (port-column p)))
  1412. (and (= line line*)
  1413. (= col col*)))))))))))
  1414. (pass-if-equal "unget-bytevector"
  1415. #vu8(10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 200 201 202 203
  1416. 1 2 3 4 251 253 254 255)
  1417. (let ((port (open-bytevector-input-port #vu8(1 2 3 4 251 253 254 255))))
  1418. (unget-bytevector port #vu8(200 201 202 203))
  1419. (unget-bytevector port #vu8(20 21 22 23 24))
  1420. (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 4)
  1421. (unget-bytevector port #vu8(10 11 12 13 14 15 16 17 18 19) 2 2)
  1422. (unget-bytevector port #vu8(10 11))
  1423. (get-bytevector-all port)))
  1424. (with-test-prefix "unicode byte-order marks (BOMs)"
  1425. (define (bv-read-test* encoding bv proc)
  1426. (let ((port (open-bytevector-input-port bv)))
  1427. (set-port-encoding! port encoding)
  1428. (proc port)))
  1429. (define (bv-read-test encoding bv)
  1430. (bv-read-test* encoding bv read-string))
  1431. (define (bv-write-test* encoding proc)
  1432. (call-with-values
  1433. (lambda () (open-bytevector-output-port))
  1434. (lambda (port get-bytevector)
  1435. (set-port-encoding! port encoding)
  1436. (proc port)
  1437. (get-bytevector))))
  1438. (define (bv-write-test encoding str)
  1439. (bv-write-test* encoding
  1440. (lambda (p)
  1441. (display str p))))
  1442. (pass-if-equal "BOM not discarded from Latin-1 stream"
  1443. "\xEF\xBB\xBF\x61"
  1444. (bv-read-test "ISO-8859-1" #vu8(#xEF #xBB #xBF #x61)))
  1445. (pass-if-equal "BOM not discarded from Latin-2 stream"
  1446. "\u010F\u0165\u017C\x61"
  1447. (bv-read-test "ISO-8859-2" #vu8(#xEF #xBB #xBF #x61)))
  1448. (pass-if-equal "BOM not discarded from UTF-16BE stream"
  1449. "\uFEFF\x61"
  1450. (bv-read-test "UTF-16BE" #vu8(#xFE #xFF #x00 #x61)))
  1451. (pass-if-equal "BOM not discarded from UTF-16LE stream"
  1452. "\uFEFF\x61"
  1453. (bv-read-test "UTF-16LE" #vu8(#xFF #xFE #x61 #x00)))
  1454. (pass-if-equal "BOM not discarded from UTF-32BE stream"
  1455. "\uFEFF\x61"
  1456. (bv-read-test "UTF-32BE" #vu8(#x00 #x00 #xFE #xFF
  1457. #x00 #x00 #x00 #x61)))
  1458. (pass-if-equal "BOM not discarded from UTF-32LE stream"
  1459. "\uFEFF\x61"
  1460. (bv-read-test "UTF-32LE" #vu8(#xFF #xFE #x00 #x00
  1461. #x61 #x00 #x00 #x00)))
  1462. (pass-if-equal "BOM not written to UTF-8 stream"
  1463. #vu8(#x61)
  1464. (bv-write-test "UTF-8" "a"))
  1465. (pass-if-equal "BOM not written to UTF-16BE stream"
  1466. #vu8(#x00 #x61)
  1467. (bv-write-test "UTF-16BE" "a"))
  1468. (pass-if-equal "BOM not written to UTF-16LE stream"
  1469. #vu8(#x61 #x00)
  1470. (bv-write-test "UTF-16LE" "a"))
  1471. (pass-if-equal "BOM not written to UTF-32BE stream"
  1472. #vu8(#x00 #x00 #x00 #x61)
  1473. (bv-write-test "UTF-32BE" "a"))
  1474. (pass-if-equal "BOM not written to UTF-32LE stream"
  1475. #vu8(#x61 #x00 #x00 #x00)
  1476. (bv-write-test "UTF-32LE" "a"))
  1477. (pass-if "Don't read from the port unless user asks to"
  1478. (let* ((p (make-soft-port
  1479. (vector
  1480. (lambda (c) #f) ; write char
  1481. (lambda (s) #f) ; write string
  1482. (lambda () #f) ; flush
  1483. (lambda () (throw 'fail)) ; read char
  1484. (lambda () #f))
  1485. "rw")))
  1486. (set-port-encoding! p "UTF-16")
  1487. (display "abc" p)
  1488. (set-port-encoding! p "UTF-32")
  1489. (display "def" p)
  1490. #t))
  1491. ;; TODO: test that input and output streams are independent when
  1492. ;; appropriate, and linked when appropriate.
  1493. (pass-if-equal "BOM discarded from start of UTF-8 stream"
  1494. "a"
  1495. (bv-read-test "Utf-8" #vu8(#xEF #xBB #xBF #x61)))
  1496. (pass-if-equal "BOM discarded from start of UTF-8 stream after seek to 0"
  1497. '(#\a "a")
  1498. (bv-read-test* "uTf-8" #vu8(#xEF #xBB #xBF #x61)
  1499. (lambda (p)
  1500. (let ((c (read-char p)))
  1501. (seek p 0 SEEK_SET)
  1502. (let ((s (read-string p)))
  1503. (list c s))))))
  1504. (pass-if-equal "Only one BOM discarded from start of UTF-8 stream"
  1505. "\uFEFFa"
  1506. (bv-read-test "UTF-8" #vu8(#xEF #xBB #xBF #xEF #xBB #xBF #x61)))
  1507. (pass-if-equal "BOM not discarded from UTF-8 stream after seek to > 0"
  1508. "\uFEFFb"
  1509. (bv-read-test* "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)
  1510. (lambda (p)
  1511. (seek p 1 SEEK_SET)
  1512. (read-string p))))
  1513. (pass-if-equal "BOM not discarded unless at start of UTF-8 stream"
  1514. "a\uFEFFb"
  1515. (bv-read-test "UTF-8" #vu8(#x61 #xEF #xBB #xBF #x62)))
  1516. (pass-if-equal "BOM (BE) written to start of UTF-16 stream"
  1517. #vu8(#xFE #xFF #x00 #x61 #x00 #x62)
  1518. (bv-write-test "UTF-16" "ab"))
  1519. (pass-if-equal "BOM (BE) written to UTF-16 stream after set-port-encoding!"
  1520. #vu8(#xFE #xFF #x00 #x61 #x00 #x62 #xFE #xFF #x00 #x63 #x00 #x64)
  1521. (bv-write-test* "UTF-16"
  1522. (lambda (p)
  1523. (display "ab" p)
  1524. (set-port-encoding! p "UTF-16")
  1525. (display "cd" p))))
  1526. (pass-if-equal "BOM discarded from start of UTF-16 stream (BE)"
  1527. "a"
  1528. (bv-read-test "UTF-16" #vu8(#xFE #xFF #x00 #x61)))
  1529. (pass-if-equal "BOM discarded from start of UTF-16 stream (BE) after seek to 0"
  1530. '(#\a "a")
  1531. (bv-read-test* "utf-16" #vu8(#xFE #xFF #x00 #x61)
  1532. (lambda (p)
  1533. (let ((c (read-char p)))
  1534. (seek p 0 SEEK_SET)
  1535. (let ((s (read-string p)))
  1536. (list c s))))))
  1537. (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (BE)"
  1538. "\uFEFFa"
  1539. (bv-read-test "Utf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)))
  1540. (pass-if-equal "BOM not discarded from UTF-16 stream (BE) after seek to > 0"
  1541. "\uFEFFa"
  1542. (bv-read-test* "uTf-16" #vu8(#xFE #xFF #xFE #xFF #x00 #x61)
  1543. (lambda (p)
  1544. (seek p 2 SEEK_SET)
  1545. (read-string p))))
  1546. (pass-if-equal "BOM not discarded unless at start of UTF-16 stream"
  1547. "a\uFEFFb"
  1548. (bv-read-test "utf-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)))
  1549. (pass-if-equal "BOM discarded from start of UTF-16 stream (LE)"
  1550. "a"
  1551. (bv-read-test "UTF-16" #vu8(#xFF #xFE #x61 #x00)))
  1552. (pass-if-equal "BOM discarded from start of UTF-16 stream (LE) after seek to 0"
  1553. '(#\a "a")
  1554. (bv-read-test* "Utf-16" #vu8(#xFF #xFE #x61 #x00)
  1555. (lambda (p)
  1556. (let ((c (read-char p)))
  1557. (seek p 0 SEEK_SET)
  1558. (let ((s (read-string p)))
  1559. (list c s))))))
  1560. (pass-if-equal "Only one BOM discarded from start of UTF-16 stream (LE)"
  1561. "\uFEFFa"
  1562. (bv-read-test "UTf-16" #vu8(#xFF #xFE #xFF #xFE #x61 #x00)))
  1563. (pass-if-equal "BOM discarded from start of UTF-32 stream (BE)"
  1564. "a"
  1565. (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
  1566. #x00 #x00 #x00 #x61)))
  1567. (pass-if-equal "BOM discarded from start of UTF-32 stream (BE) after seek to 0"
  1568. '(#\a "a")
  1569. (bv-read-test* "utF-32" #vu8(#x00 #x00 #xFE #xFF
  1570. #x00 #x00 #x00 #x61)
  1571. (lambda (p)
  1572. (let ((c (read-char p)))
  1573. (seek p 0 SEEK_SET)
  1574. (let ((s (read-string p)))
  1575. (list c s))))))
  1576. (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (BE)"
  1577. "\uFEFFa"
  1578. (bv-read-test "UTF-32" #vu8(#x00 #x00 #xFE #xFF
  1579. #x00 #x00 #xFE #xFF
  1580. #x00 #x00 #x00 #x61)))
  1581. (pass-if-equal "BOM not discarded from UTF-32 stream (BE) after seek to > 0"
  1582. "\uFEFFa"
  1583. (bv-read-test* "UtF-32" #vu8(#x00 #x00 #xFE #xFF
  1584. #x00 #x00 #xFE #xFF
  1585. #x00 #x00 #x00 #x61)
  1586. (lambda (p)
  1587. (seek p 4 SEEK_SET)
  1588. (read-string p))))
  1589. (pass-if-equal "BOM discarded within UTF-16 stream (BE) after set-port-encoding!"
  1590. "ab"
  1591. (bv-read-test* "UTF-16" #vu8(#x00 #x61 #xFE #xFF #x00 #x62)
  1592. (lambda (p)
  1593. (let ((a (read-char p)))
  1594. (set-port-encoding! p "UTF-16")
  1595. (string a (read-char p))))))
  1596. (pass-if-equal "BOM discarded within UTF-16 stream (LE,BE) after set-port-encoding!"
  1597. "ab"
  1598. (bv-read-test* "utf-16" #vu8(#x00 #x61 #xFF #xFE #x62 #x00)
  1599. (lambda (p)
  1600. (let ((a (read-char p)))
  1601. (set-port-encoding! p "UTF-16")
  1602. (string a (read-char p))))))
  1603. (pass-if-equal "BOM discarded within UTF-32 stream (BE) after set-port-encoding!"
  1604. "ab"
  1605. (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1606. #x00 #x00 #xFE #xFF
  1607. #x00 #x00 #x00 #x62)
  1608. (lambda (p)
  1609. (let ((a (read-char p)))
  1610. (set-port-encoding! p "UTF-32")
  1611. (string a (read-char p))))))
  1612. (pass-if-equal "BOM discarded within UTF-32 stream (LE,BE) after set-port-encoding!"
  1613. "ab"
  1614. (bv-read-test* "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1615. #xFF #xFE #x00 #x00
  1616. #x62 #x00 #x00 #x00)
  1617. (lambda (p)
  1618. (let ((a (read-char p)))
  1619. (set-port-encoding! p "UTF-32")
  1620. (string a (read-char p))))))
  1621. (pass-if-equal "BOM not discarded unless at start of UTF-32 stream"
  1622. "a\uFEFFb"
  1623. (bv-read-test "UTF-32" #vu8(#x00 #x00 #x00 #x61
  1624. #x00 #x00 #xFE #xFF
  1625. #x00 #x00 #x00 #x62)))
  1626. (pass-if-equal "BOM discarded from start of UTF-32 stream (LE)"
  1627. "a"
  1628. (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
  1629. #x61 #x00 #x00 #x00)))
  1630. (pass-if-equal "BOM discarded from start of UTF-32 stream (LE) after seek to 0"
  1631. '(#\a "a")
  1632. (bv-read-test* "UTf-32" #vu8(#xFF #xFE #x00 #x00
  1633. #x61 #x00 #x00 #x00)
  1634. (lambda (p)
  1635. (let ((c (read-char p)))
  1636. (seek p 0 SEEK_SET)
  1637. (let ((s (read-string p)))
  1638. (list c s))))))
  1639. (pass-if-equal "Only one BOM discarded from start of UTF-32 stream (LE)"
  1640. "\uFEFFa"
  1641. (bv-read-test "UTF-32" #vu8(#xFF #xFE #x00 #x00
  1642. #xFF #xFE #x00 #x00
  1643. #x61 #x00 #x00 #x00))))
  1644. (define-syntax-rule (with-load-path path body ...)
  1645. (let ((new path)
  1646. (old %load-path))
  1647. (dynamic-wind
  1648. (lambda ()
  1649. (set! %load-path new))
  1650. (lambda ()
  1651. body ...)
  1652. (lambda ()
  1653. (set! %load-path old)))))
  1654. (define %temporary-directory
  1655. (string-append (or (getenv "TMPDIR") "/tmp") "/guile-ports-test."
  1656. (number->string (getpid))))
  1657. (with-test-prefix "%file-port-name-canonicalization"
  1658. (pass-if-equal "absolute file name & empty %load-path entry" "/dev/null"
  1659. ;; In Guile 2.0.5 and earlier, this would return "dev/null" instead
  1660. ;; of "/dev/null". See
  1661. ;; <http://lists.gnu.org/archive/html/guile-devel/2012-05/msg00059.html>
  1662. ;; for a discussion.
  1663. (with-load-path (cons "" (delete "/" %load-path))
  1664. (with-fluids ((%file-port-name-canonicalization 'relative))
  1665. (port-filename (open-input-file "/dev/null")))))
  1666. (pass-if-equal "relative canonicalization with /" "dev/null"
  1667. (with-load-path (cons "/" %load-path)
  1668. (with-fluids ((%file-port-name-canonicalization 'relative))
  1669. (port-filename (open-input-file "/dev/null")))))
  1670. (pass-if-equal "relative canonicalization with /dev/.." "dev/null"
  1671. (with-load-path (cons "/dev/.." %load-path)
  1672. (with-fluids ((%file-port-name-canonicalization 'relative))
  1673. (port-filename (open-input-file "/dev/null")))))
  1674. (pass-if-equal "relative canonicalization from ice-9" "ice-9/q.scm"
  1675. (with-fluids ((%file-port-name-canonicalization 'relative))
  1676. (port-filename
  1677. (open-input-file (%search-load-path "ice-9/q.scm")))))
  1678. (pass-if-equal "relative canonicalization with common prefixes"
  1679. "x.scm"
  1680. ;; In Guile up to 2.2.2, this would return "wrong/x.scm'.
  1681. (let* ((dir1 (string-append %temporary-directory "/something"))
  1682. (dir2 (string-append dir1 "-wrong")))
  1683. (with-load-path (append (list dir1 dir2) %load-path)
  1684. (dynamic-wind
  1685. (lambda ()
  1686. (mkdir %temporary-directory)
  1687. (mkdir dir1)
  1688. (mkdir dir2)
  1689. (call-with-output-file (string-append dir2 "/x.scm")
  1690. (const #t)))
  1691. (lambda ()
  1692. (with-fluids ((%file-port-name-canonicalization 'relative))
  1693. (port-filename
  1694. (open-input-file (string-append dir2 "/x.scm")))))
  1695. (lambda ()
  1696. (delete-file (string-append dir2 "/x.scm"))
  1697. (rmdir dir2)
  1698. (rmdir dir1)
  1699. (rmdir %temporary-directory))))))
  1700. (pass-if-equal "absolute canonicalization from ice-9"
  1701. (canonicalize-path
  1702. (string-append (assoc-ref %guile-build-info 'top_srcdir)
  1703. "/module/ice-9/q.scm"))
  1704. (with-fluids ((%file-port-name-canonicalization 'absolute))
  1705. (port-filename (open-input-file (%search-load-path "ice-9/q.scm"))))))
  1706. (with-test-prefix "file name separators"
  1707. (pass-if "no backslash separators in Windows file names"
  1708. ;; In Guile 2.0.11 and earlier, %load-path on Windows could
  1709. ;; include file names with backslashes, and `getcwd' on Windows
  1710. ;; would always return a directory name with backslashes.
  1711. (or (not (file-name-separator? #\\))
  1712. (with-load-path (cons (getcwd) %load-path)
  1713. (not (string-index (%search-load-path (basename (test-file)))
  1714. #\\))))))
  1715. (delete-file (test-file))
  1716. ;;; Local Variables:
  1717. ;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
  1718. ;;; eval: (put 'with-load-path 'scheme-indent-function 1)
  1719. ;;; End: