ports.test 72 KB

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