ports.test 72 KB

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