ports.test 36 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072
  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 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 (rnrs bytevectors)
  26. #:use-module ((rnrs io ports) #:select (open-bytevector-input-port)))
  27. (define (display-line . args)
  28. (for-each display args)
  29. (newline))
  30. (define (test-file)
  31. (data-file-name "ports-test.tmp"))
  32. ;;;; Some general utilities for testing ports.
  33. ;; Make sure we are set up for 8-bit Latin-1 data.
  34. (fluid-set! %default-port-encoding "ISO-8859-1")
  35. (for-each (lambda (p)
  36. (set-port-encoding! p (fluid-ref %default-port-encoding)))
  37. (list (current-input-port) (current-output-port)
  38. (current-error-port)))
  39. ;;; Read from PORT until EOF, and return the result as a string.
  40. (define (read-all port)
  41. (let loop ((chars '()))
  42. (let ((char (read-char port)))
  43. (if (eof-object? char)
  44. (list->string (reverse! chars))
  45. (loop (cons char chars))))))
  46. (define (read-file filename)
  47. (let* ((port (open-input-file filename))
  48. (string (read-all port)))
  49. (close-port port)
  50. string))
  51. ;;;; Normal file ports.
  52. ;;; Write out an s-expression, and read it back.
  53. (let ((string '("From fairest creatures we desire increase,"
  54. "That thereby beauty's rose might never die,"))
  55. (filename (test-file)))
  56. (let ((port (open-output-file filename)))
  57. (write string port)
  58. (close-port port))
  59. (let ((port (open-input-file filename)))
  60. (let ((in-string (read port)))
  61. (pass-if "file: write and read back list of strings"
  62. (equal? string in-string)))
  63. (close-port port))
  64. (delete-file filename))
  65. ;;; Write out a string, and read it back a character at a time.
  66. (let ((string "This is a test string\nwith no newline at the end")
  67. (filename (test-file)))
  68. (let ((port (open-output-file filename)))
  69. (display string port)
  70. (close-port port))
  71. (let ((in-string (read-file filename)))
  72. (pass-if "file: write and read back characters"
  73. (equal? string in-string)))
  74. (delete-file filename))
  75. ;;; Buffered input/output port with seeking.
  76. (let* ((filename (test-file))
  77. (port (open-file filename "w+")))
  78. (display "J'Accuse" port)
  79. (seek port -1 SEEK_CUR)
  80. (pass-if "file: r/w 1"
  81. (char=? (read-char port) #\e))
  82. (pass-if "file: r/w 2"
  83. (eof-object? (read-char port)))
  84. (seek port -1 SEEK_CUR)
  85. (write-char #\x port)
  86. (seek port 7 SEEK_SET)
  87. (pass-if "file: r/w 3"
  88. (char=? (read-char port) #\x))
  89. (seek port -2 SEEK_END)
  90. (pass-if "file: r/w 4"
  91. (char=? (read-char port) #\s))
  92. (close-port port)
  93. (delete-file filename))
  94. ;;; Unbuffered input/output port with seeking.
  95. (let* ((filename (test-file))
  96. (port (open-file filename "w+0")))
  97. (display "J'Accuse" port)
  98. (seek port -1 SEEK_CUR)
  99. (pass-if "file: ub r/w 1"
  100. (char=? (read-char port) #\e))
  101. (pass-if "file: ub r/w 2"
  102. (eof-object? (read-char port)))
  103. (seek port -1 SEEK_CUR)
  104. (write-char #\x port)
  105. (seek port 7 SEEK_SET)
  106. (pass-if "file: ub r/w 3"
  107. (char=? (read-char port) #\x))
  108. (seek port -2 SEEK_END)
  109. (pass-if "file: ub r/w 4"
  110. (char=? (read-char port) #\s))
  111. (close-port port)
  112. (delete-file filename))
  113. ;;; Buffered output-only and input-only ports with seeking.
  114. (let* ((filename (test-file))
  115. (port (open-output-file filename)))
  116. (display "J'Accuse" port)
  117. (pass-if "file: out tell"
  118. (= (seek port 0 SEEK_CUR) 8))
  119. (seek port -1 SEEK_CUR)
  120. (write-char #\x port)
  121. (close-port port)
  122. (let ((iport (open-input-file filename)))
  123. (pass-if "file: in tell 0"
  124. (= (seek iport 0 SEEK_CUR) 0))
  125. (read-char iport)
  126. (pass-if "file: in tell 1"
  127. (= (seek iport 0 SEEK_CUR) 1))
  128. (unread-char #\z iport)
  129. (pass-if "file: in tell 0 after unread"
  130. (= (seek iport 0 SEEK_CUR) 0))
  131. (pass-if "file: unread char still there"
  132. (char=? (read-char iport) #\z))
  133. (seek iport 7 SEEK_SET)
  134. (pass-if "file: in last char"
  135. (char=? (read-char iport) #\x))
  136. (close-port iport))
  137. (delete-file filename))
  138. ;;; unusual characters.
  139. (let* ((filename (test-file))
  140. (port (open-output-file filename)))
  141. (display (string #\nul (integer->char 255) (integer->char 128)
  142. #\nul) port)
  143. (close-port port)
  144. (let* ((port (open-input-file filename))
  145. (line (read-line port)))
  146. (pass-if "file: read back NUL 1"
  147. (char=? (string-ref line 0) #\nul))
  148. (pass-if "file: read back 255"
  149. (char=? (string-ref line 1) (integer->char 255)))
  150. (pass-if "file: read back 128"
  151. (char=? (string-ref line 2) (integer->char 128)))
  152. (pass-if "file: read back NUL 2"
  153. (char=? (string-ref line 3) #\nul))
  154. (pass-if "file: EOF"
  155. (eof-object? (read-char port)))
  156. (close-port port))
  157. (delete-file filename))
  158. ;;; line buffering mode.
  159. (let* ((filename (test-file))
  160. (port (open-file filename "wl"))
  161. (test-string "one line more or less"))
  162. (write-line test-string port)
  163. (let* ((in-port (open-input-file filename))
  164. (line (read-line in-port)))
  165. (close-port in-port)
  166. (close-port port)
  167. (pass-if "file: line buffering"
  168. (string=? line test-string)))
  169. (delete-file filename))
  170. ;;; read-line should use the port encoding (not the locale encoding).
  171. (let ((str "ĉu bone?"))
  172. (with-locale "C"
  173. (let* ((filename (test-file))
  174. (port (open-file filename "wl")))
  175. (set-port-encoding! port "UTF-8")
  176. (write-line str port)
  177. (let ((in-port (open-input-file filename)))
  178. (set-port-encoding! in-port "UTF-8")
  179. (let ((line (read-line in-port)))
  180. (close-port in-port)
  181. (close-port port)
  182. (pass-if "file: read-line honors port encoding"
  183. (string=? line str))))
  184. (delete-file filename))))
  185. ;;; binary mode ignores port encoding
  186. (pass-if "file: binary mode ignores port encoding"
  187. (with-fluids ((%default-port-encoding "UTF-8"))
  188. (let* ((filename (test-file))
  189. (port (open-file filename "w"))
  190. (test-string "一二三")
  191. (binary-test-string
  192. (apply string
  193. (map integer->char
  194. (uniform-vector->list
  195. (string->utf8 test-string))))))
  196. (write-line test-string port)
  197. (close-port port)
  198. (let* ((in-port (open-file filename "rb"))
  199. (line (read-line in-port)))
  200. (close-port in-port)
  201. (delete-file filename)
  202. (string=? line binary-test-string)))))
  203. ;;; binary mode ignores file coding declaration
  204. (pass-if "file: binary mode ignores file coding declaration"
  205. (with-fluids ((%default-port-encoding "UTF-8"))
  206. (let* ((filename (test-file))
  207. (port (open-file filename "w"))
  208. (test-string "一二三")
  209. (binary-test-string
  210. (apply string
  211. (map integer->char
  212. (uniform-vector->list
  213. (string->utf8 test-string))))))
  214. (write-line ";; coding: utf-8" port)
  215. (write-line test-string port)
  216. (close-port port)
  217. (let* ((in-port (open-file filename "rb"))
  218. (line1 (read-line in-port))
  219. (line2 (read-line in-port)))
  220. (close-port in-port)
  221. (delete-file filename)
  222. (string=? line2 binary-test-string)))))
  223. ;; open-file honors file coding declarations
  224. (pass-if "file: open-file honors coding declarations"
  225. (with-fluids ((%default-port-encoding "UTF-8"))
  226. (let* ((filename (test-file))
  227. (port (open-output-file filename))
  228. (test-string "€100"))
  229. (set-port-encoding! port "ISO-8859-15")
  230. (write-line ";; coding: iso-8859-15" port)
  231. (write-line test-string port)
  232. (close-port port)
  233. (let* ((in-port (open-input-file filename))
  234. (line1 (read-line in-port))
  235. (line2 (read-line in-port)))
  236. (close-port in-port)
  237. (delete-file filename)
  238. (string=? line2 test-string)))))
  239. ;;; ungetting characters and strings.
  240. (with-input-from-string "walk on the moon\nmoon"
  241. (lambda ()
  242. (read-char)
  243. (unread-char #\a (current-input-port))
  244. (pass-if "unread-char"
  245. (char=? (read-char) #\a))
  246. (read-line)
  247. (let ((replacenoid "chicken enchilada"))
  248. (unread-char #\newline (current-input-port))
  249. (unread-string replacenoid (current-input-port))
  250. (pass-if "unread-string"
  251. (string=? (read-line) replacenoid)))
  252. (pass-if "unread residue"
  253. (string=? (read-line) "moon"))))
  254. ;;; non-blocking mode on a port. create a pipe and set O_NONBLOCK on
  255. ;;; the reading end. try to read a byte: should get EAGAIN or
  256. ;;; EWOULDBLOCK error.
  257. (let* ((p (pipe))
  258. (r (car p)))
  259. (fcntl r F_SETFL (logior (fcntl r F_GETFL) O_NONBLOCK))
  260. (pass-if "non-blocking-I/O"
  261. (catch 'system-error
  262. (lambda () (read-char r) #f)
  263. (lambda (key . args)
  264. (and (eq? key 'system-error)
  265. (let ((errno (car (list-ref args 3))))
  266. (or (= errno EAGAIN)
  267. (= errno EWOULDBLOCK))))))))
  268. ;;;; Pipe (popen) ports.
  269. ;;; Run a command, and read its output.
  270. (let* ((pipe (open-pipe "echo 'Howdy there, partner!'" "r"))
  271. (in-string (read-all pipe)))
  272. (close-pipe pipe)
  273. (pass-if "pipe: read"
  274. (equal? in-string "Howdy there, partner!\n")))
  275. ;;; Run a command, send some output to it, and see if it worked.
  276. (let* ((filename (test-file))
  277. (pipe (open-pipe (string-append "grep Mommy > " filename) "w")))
  278. (display "Now Jimmy lives on a mushroom cloud\n" pipe)
  279. (display "Mommy, why does everybody have a bomb?\n" pipe)
  280. (close-pipe pipe)
  281. (let ((in-string (read-file filename)))
  282. (pass-if "pipe: write"
  283. (equal? in-string "Mommy, why does everybody have a bomb?\n")))
  284. (delete-file filename))
  285. ;;;; Void ports. These are so trivial we don't test them.
  286. ;;;; String ports.
  287. (with-test-prefix "string ports"
  288. ;; Write text to a string port.
  289. (let* ((string "Howdy there, partner!")
  290. (in-string (call-with-output-string
  291. (lambda (port)
  292. (display string port)
  293. (newline port)))))
  294. (pass-if "display text"
  295. (equal? in-string (string-append string "\n"))))
  296. ;; Write an s-expression to a string port.
  297. (let* ((sexpr '("more utterly random text" 1729 #(a vector) 3.1415926))
  298. (in-sexpr
  299. (call-with-input-string (call-with-output-string
  300. (lambda (port)
  301. (write sexpr port)))
  302. read)))
  303. (pass-if "write/read sexpr"
  304. (equal? in-sexpr sexpr)))
  305. ;; seeking and unreading from an input string.
  306. (let ((text "that text didn't look random to me"))
  307. (call-with-input-string text
  308. (lambda (p)
  309. (pass-if "input tell 0"
  310. (= (seek p 0 SEEK_CUR) 0))
  311. (read-char p)
  312. (pass-if "input tell 1"
  313. (= (seek p 0 SEEK_CUR) 1))
  314. (unread-char #\x p)
  315. (pass-if "input tell back to 0"
  316. (= (seek p 0 SEEK_CUR) 0))
  317. (pass-if "input ungetted char"
  318. (char=? (read-char p) #\x))
  319. (seek p 0 SEEK_END)
  320. (pass-if "input seek to end"
  321. (= (seek p 0 SEEK_CUR)
  322. (string-length text)))
  323. (unread-char #\x p)
  324. (pass-if "input seek to beginning"
  325. (= (seek p 0 SEEK_SET) 0))
  326. (pass-if "input reread first char"
  327. (char=? (read-char p)
  328. (string-ref text 0))))))
  329. ;; seeking an output string.
  330. (let* ((text (string-copy "123456789"))
  331. (len (string-length text))
  332. (result (call-with-output-string
  333. (lambda (p)
  334. (pass-if "output tell 0"
  335. (= (seek p 0 SEEK_CUR) 0))
  336. (display text p)
  337. (pass-if "output tell end"
  338. (= (seek p 0 SEEK_CUR) len))
  339. (pass-if "output seek to beginning"
  340. (= (seek p 0 SEEK_SET) 0))
  341. (write-char #\a p)
  342. (seek p -1 SEEK_END)
  343. (pass-if "output seek to last char"
  344. (= (seek p 0 SEEK_CUR)
  345. (- len 1)))
  346. (write-char #\b p)))))
  347. (string-set! text 0 #\a)
  348. (string-set! text (- len 1) #\b)
  349. (pass-if "output check"
  350. (string=? text result)))
  351. (pass-if "%default-port-encoding is honored"
  352. (let ((encodings '("UTF-8" "UTF-16" "ISO-8859-1" "ISO-8859-3")))
  353. (equal? (map (lambda (e)
  354. (with-fluids ((%default-port-encoding e))
  355. (call-with-output-string
  356. (lambda (p)
  357. (and (string=? e (port-encoding p))
  358. (display (port-encoding p) p))))))
  359. encodings)
  360. encodings)))
  361. (pass-if "suitable encoding [latin-1]"
  362. (let ((str "hello, world"))
  363. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  364. (equal? str
  365. (with-output-to-string
  366. (lambda ()
  367. (display str)))))))
  368. (pass-if "suitable encoding [latin-3]"
  369. (let ((str "ĉu bone?"))
  370. (with-fluids ((%default-port-encoding "ISO-8859-3"))
  371. (equal? str
  372. (with-output-to-string
  373. (lambda ()
  374. (display str)))))))
  375. (pass-if "wrong encoding"
  376. (let ((str "ĉu bone?"))
  377. (catch 'encoding-error
  378. (lambda ()
  379. ;; Latin-1 cannot represent ‘ĉ’.
  380. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  381. (with-output-to-string
  382. (lambda ()
  383. (display str)))))
  384. (lambda (key subr message errno port chr)
  385. (and (eq? chr #\ĉ)
  386. (string? (strerror errno)))))))
  387. (pass-if "wrong encoding, substitute"
  388. (let ((str "ĉu bone?"))
  389. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  390. (string=? (with-output-to-string
  391. (lambda ()
  392. (set-port-conversion-strategy! (current-output-port)
  393. 'substitute)
  394. (display str)))
  395. "?u bone?"))))
  396. (pass-if "wrong encoding, escape"
  397. (let ((str "ĉu bone?"))
  398. (with-fluids ((%default-port-encoding "ISO-8859-1"))
  399. (string=? (with-output-to-string
  400. (lambda ()
  401. (set-port-conversion-strategy! (current-output-port)
  402. 'escape)
  403. (display str)))
  404. "\\u0109u bone?"))))
  405. (pass-if "peek-char [latin-1]"
  406. (let ((p (with-fluids ((%default-port-encoding #f))
  407. (open-input-string "hello, world"))))
  408. (and (char=? (peek-char p) #\h)
  409. (char=? (peek-char p) #\h)
  410. (char=? (peek-char p) #\h)
  411. (= (port-line p) 0)
  412. (= (port-column p) 0))))
  413. (pass-if "peek-char [utf-8]"
  414. (let ((p (with-fluids ((%default-port-encoding "UTF-8"))
  415. (open-input-string "안녕하세요"))))
  416. (and (char=? (peek-char p) #\안)
  417. (char=? (peek-char p) #\안)
  418. (char=? (peek-char p) #\안)
  419. (= (port-line p) 0)
  420. (= (port-column p) 0))))
  421. (pass-if "peek-char [utf-16]"
  422. (let ((p (with-fluids ((%default-port-encoding "UTF-16BE"))
  423. (open-input-string "안녕하세요"))))
  424. (and (char=? (peek-char p) #\안)
  425. (char=? (peek-char p) #\안)
  426. (char=? (peek-char p) #\안)
  427. (= (port-line p) 0)
  428. (= (port-column p) 0))))
  429. ;; Mini DSL to test decoding error handling.
  430. (letrec-syntax ((decoding-error?
  431. (syntax-rules ()
  432. ((_ port exp)
  433. (catch 'decoding-error
  434. (lambda ()
  435. (pk 'exp exp)
  436. #f)
  437. (lambda (key subr message errno p)
  438. (and (eq? p port)
  439. (not (= 0 errno))))))))
  440. (make-check
  441. (syntax-rules (-> error eof)
  442. ((_ port (proc -> error))
  443. (if (eq? 'substitute
  444. (port-conversion-strategy port))
  445. (eq? (proc port) #\?)
  446. (decoding-error? port (proc port))))
  447. ((_ port (proc -> eof))
  448. (eof-object? (proc port)))
  449. ((_ port (proc -> char))
  450. (eq? (proc port) char))))
  451. (make-checks
  452. (syntax-rules ()
  453. ((_ port check ...)
  454. (and (make-check port check) ...))))
  455. (make-peek+read-checks
  456. (syntax-rules ()
  457. ((_ port (result ...) e1 expected ...)
  458. (make-peek+read-checks port
  459. (result ...
  460. (peek-char -> e1)
  461. (read-char -> e1))
  462. expected ...))
  463. ((_ port (result ...))
  464. (make-checks port result ...))
  465. ((_ port #f e1 expected ...)
  466. (make-peek+read-checks port
  467. ((peek-char -> e1)
  468. (read-char -> e1))
  469. expected ...))))
  470. (test-decoding-error*
  471. (syntax-rules ()
  472. ((_ sequence encoding strategy (expected ...))
  473. (begin
  474. (pass-if (format #f "test-decoding-error: ~s ~s ~s"
  475. 'sequence encoding strategy)
  476. (let ((p (open-bytevector-input-port
  477. (u8-list->bytevector 'sequence))))
  478. (set-port-encoding! p encoding)
  479. (set-port-conversion-strategy! p strategy)
  480. (make-checks p
  481. (read-char -> expected) ...)))
  482. ;; Generate the same test, but with one
  483. ;; `peek-char' call before each `read-char'.
  484. ;; Both should yield the same result.
  485. (pass-if (format #f "test-decoding-error: ~s ~s ~s + peek-char"
  486. 'sequence encoding strategy)
  487. (let ((p (open-bytevector-input-port
  488. (u8-list->bytevector 'sequence))))
  489. (set-port-encoding! p encoding)
  490. (set-port-conversion-strategy! p strategy)
  491. (make-peek+read-checks p #f expected
  492. ...)))))))
  493. (test-decoding-error
  494. (syntax-rules ()
  495. ((_ sequence encoding (expected ...))
  496. (begin
  497. (test-decoding-error* sequence encoding 'error
  498. (expected ...))
  499. ;; `escape' should behave exactly like `error'.
  500. (test-decoding-error* sequence encoding 'escape
  501. (expected ...))
  502. (test-decoding-error* sequence encoding 'substitute
  503. (expected ...)))))))
  504. (test-decoding-error (255 65 66 67) "UTF-8"
  505. (error #\A #\B #\C eof))
  506. (test-decoding-error (255 206 187 206 188) "UTF-8"
  507. (error #\λ #\μ eof))
  508. (test-decoding-error (206 187 206) "UTF-8"
  509. ;; Unterminated sequence.
  510. (#\λ error eof))
  511. ;; Check how ill-formed UTF-8 sequences are handled (see Table 3-7
  512. ;; of the "Conformance" chapter of Unicode 6.0.0.)
  513. (test-decoding-error (#xc0 #x80 #x41) "UTF-8"
  514. (error ;; C0: should be in the C2..DF range
  515. error ;; 80: invalid
  516. #\A
  517. eof))
  518. (test-decoding-error (#xc2 #x41 #x42) "UTF-8"
  519. ;; Section 3.9 of Unicode 6.0.0 reads:
  520. ;; "If the converter encounters an ill-formed UTF-8 code unit
  521. ;; sequence which starts with a valid first byte, but which does
  522. ;; not continue with valid successor bytes (see Table 3-7), it
  523. ;; must not consume the successor bytes".
  524. ;; Glibc/libiconv do not conform to it and instead swallow the
  525. ;; #x41. This example appears literally in Section 3.9.
  526. (error ;; 41: invalid successor
  527. #\A ;; 41: valid starting byte
  528. #\B
  529. eof))
  530. (test-decoding-error (#xf0 #x80 #x80 #x41) "UTF-8"
  531. ;; According to Unicode 6.0.0, Section 3.9, "the only formal
  532. ;; requirement mandated by Unicode conformance for a converter is
  533. ;; that the <41> be processed and correctly interpreted as
  534. ;; <U+0041>".
  535. (error ;; 2nd byte should be in the A0..BF range
  536. error ;; 80: not a valid starting byte
  537. error ;; 80: not a valid starting byte
  538. #\A
  539. eof))
  540. (test-decoding-error (#xe0 #xa0 #x41 #x42) "UTF-8"
  541. (error ;; 3rd byte should be in the 80..BF range
  542. #\A
  543. #\B
  544. eof))
  545. (test-decoding-error (#xf0 #x88 #x88 #x88) "UTF-8"
  546. (error ;; 2nd byte should be in the 90..BF range
  547. error ;; 88: not a valid starting byte
  548. error ;; 88: not a valid starting byte
  549. error ;; 88: not a valid starting byte
  550. eof))))
  551. (with-test-prefix "call-with-output-string"
  552. ;; In Guile 1.6.4, closing the port resulted in a segv, check that doesn't
  553. ;; occur.
  554. (pass-if-exception "proc closes port" exception:wrong-type-arg
  555. (call-with-output-string close-port)))
  556. ;;;; Soft ports. No tests implemented yet.
  557. ;;;; Generic operations across all port types.
  558. (let ((port-loop-temp (test-file)))
  559. ;; Return a list of input ports that all return the same text.
  560. ;; We map tests over this list.
  561. (define (input-port-list text)
  562. ;; Create a text file some of the ports will use.
  563. (let ((out-port (open-output-file port-loop-temp)))
  564. (display text out-port)
  565. (close-port out-port))
  566. (list (open-input-file port-loop-temp)
  567. (open-input-pipe (string-append "cat " port-loop-temp))
  568. (call-with-input-string text (lambda (x) x))
  569. ;; We don't test soft ports at the moment.
  570. ))
  571. (define port-list-names '("file" "pipe" "string"))
  572. ;; Test the line counter.
  573. (define (test-line-counter text second-line final-column)
  574. (with-test-prefix "line counter"
  575. (let ((ports (input-port-list text)))
  576. (for-each
  577. (lambda (port port-name)
  578. (with-test-prefix port-name
  579. (pass-if "at beginning of input"
  580. (= (port-line port) 0))
  581. (pass-if "read first character"
  582. (eqv? (read-char port) #\x))
  583. (pass-if "after reading one character"
  584. (= (port-line port) 0))
  585. (pass-if "read first newline"
  586. (eqv? (read-char port) #\newline))
  587. (pass-if "after reading first newline char"
  588. (= (port-line port) 1))
  589. (pass-if "second line read correctly"
  590. (equal? (read-line port) second-line))
  591. (pass-if "read-line increments line number"
  592. (= (port-line port) 2))
  593. (pass-if "read-line returns EOF"
  594. (let loop ((i 0))
  595. (cond
  596. ((eof-object? (read-line port)) #t)
  597. ((> i 20) #f)
  598. (else (loop (+ i 1))))))
  599. (pass-if "line count is 5 at EOF"
  600. (= (port-line port) 5))
  601. (pass-if "column is correct at EOF"
  602. (= (port-column port) final-column))))
  603. ports port-list-names)
  604. (for-each close-port ports)
  605. (delete-file port-loop-temp))))
  606. (with-test-prefix "newline"
  607. (test-line-counter
  608. (string-append "x\n"
  609. "He who receives an idea from me, receives instruction\n"
  610. "himself without lessening mine; as he who lights his\n"
  611. "taper at mine, receives light without darkening me.\n"
  612. " --- Thomas Jefferson\n")
  613. "He who receives an idea from me, receives instruction"
  614. 0))
  615. (with-test-prefix "no newline"
  616. (test-line-counter
  617. (string-append "x\n"
  618. "He who receives an idea from me, receives instruction\n"
  619. "himself without lessening mine; as he who lights his\n"
  620. "taper at mine, receives light without darkening me.\n"
  621. " --- Thomas Jefferson\n"
  622. "no newline here")
  623. "He who receives an idea from me, receives instruction"
  624. 15)))
  625. ;; Test port-line and port-column for output ports
  626. (define (test-output-line-counter text final-column)
  627. (with-test-prefix "port-line and port-column for output ports"
  628. (let ((port (open-output-string)))
  629. (pass-if "at beginning of input"
  630. (and (= (port-line port) 0)
  631. (= (port-column port) 0)))
  632. (write-char #\x port)
  633. (pass-if "after writing one character"
  634. (and (= (port-line port) 0)
  635. (= (port-column port) 1)))
  636. (write-char #\newline port)
  637. (pass-if "after writing first newline char"
  638. (and (= (port-line port) 1)
  639. (= (port-column port) 0)))
  640. (display text port)
  641. (pass-if "line count is 5 at end"
  642. (= (port-line port) 5))
  643. (pass-if "column is correct at end"
  644. (= (port-column port) final-column)))))
  645. (test-output-line-counter
  646. (string-append "He who receives an idea from me, receives instruction\n"
  647. "himself without lessening mine; as he who lights his\n"
  648. "taper at mine, receives light without darkening me.\n"
  649. " --- Thomas Jefferson\n"
  650. "no newline here")
  651. 15)
  652. (with-test-prefix "port-column"
  653. (with-test-prefix "output"
  654. (pass-if "x"
  655. (let ((port (open-output-string)))
  656. (display "x" port)
  657. (= 1 (port-column port))))
  658. (pass-if "\\a"
  659. (let ((port (open-output-string)))
  660. (display "\a" port)
  661. (= 0 (port-column port))))
  662. (pass-if "x\\a"
  663. (let ((port (open-output-string)))
  664. (display "x\a" port)
  665. (= 1 (port-column port))))
  666. (pass-if "\\x08 backspace"
  667. (let ((port (open-output-string)))
  668. (display "\x08" port)
  669. (= 0 (port-column port))))
  670. (pass-if "x\\x08 backspace"
  671. (let ((port (open-output-string)))
  672. (display "x\x08" port)
  673. (= 0 (port-column port))))
  674. (pass-if "\\n"
  675. (let ((port (open-output-string)))
  676. (display "\n" port)
  677. (= 0 (port-column port))))
  678. (pass-if "x\\n"
  679. (let ((port (open-output-string)))
  680. (display "x\n" port)
  681. (= 0 (port-column port))))
  682. (pass-if "\\r"
  683. (let ((port (open-output-string)))
  684. (display "\r" port)
  685. (= 0 (port-column port))))
  686. (pass-if "x\\r"
  687. (let ((port (open-output-string)))
  688. (display "x\r" port)
  689. (= 0 (port-column port))))
  690. (pass-if "\\t"
  691. (let ((port (open-output-string)))
  692. (display "\t" port)
  693. (= 8 (port-column port))))
  694. (pass-if "x\\t"
  695. (let ((port (open-output-string)))
  696. (display "x\t" port)
  697. (= 8 (port-column port)))))
  698. (with-test-prefix "input"
  699. (pass-if "x"
  700. (let ((port (open-input-string "x")))
  701. (while (not (eof-object? (read-char port))))
  702. (= 1 (port-column port))))
  703. (pass-if "\\a"
  704. (let ((port (open-input-string "\a")))
  705. (while (not (eof-object? (read-char port))))
  706. (= 0 (port-column port))))
  707. (pass-if "x\\a"
  708. (let ((port (open-input-string "x\a")))
  709. (while (not (eof-object? (read-char port))))
  710. (= 1 (port-column port))))
  711. (pass-if "\\x08 backspace"
  712. (let ((port (open-input-string "\x08")))
  713. (while (not (eof-object? (read-char port))))
  714. (= 0 (port-column port))))
  715. (pass-if "x\\x08 backspace"
  716. (let ((port (open-input-string "x\x08")))
  717. (while (not (eof-object? (read-char port))))
  718. (= 0 (port-column port))))
  719. (pass-if "\\n"
  720. (let ((port (open-input-string "\n")))
  721. (while (not (eof-object? (read-char port))))
  722. (= 0 (port-column port))))
  723. (pass-if "x\\n"
  724. (let ((port (open-input-string "x\n")))
  725. (while (not (eof-object? (read-char port))))
  726. (= 0 (port-column port))))
  727. (pass-if "\\r"
  728. (let ((port (open-input-string "\r")))
  729. (while (not (eof-object? (read-char port))))
  730. (= 0 (port-column port))))
  731. (pass-if "x\\r"
  732. (let ((port (open-input-string "x\r")))
  733. (while (not (eof-object? (read-char port))))
  734. (= 0 (port-column port))))
  735. (pass-if "\\t"
  736. (let ((port (open-input-string "\t")))
  737. (while (not (eof-object? (read-char port))))
  738. (= 8 (port-column port))))
  739. (pass-if "x\\t"
  740. (let ((port (open-input-string "x\t")))
  741. (while (not (eof-object? (read-char port))))
  742. (= 8 (port-column port))))))
  743. (with-test-prefix "port-line"
  744. ;; in guile 1.8.1 and earlier port-line was truncated to an int, whereas
  745. ;; scm_t_port actually holds a long; this restricted the range on 64-bit
  746. ;; systems
  747. (pass-if "set most-positive-fixnum/2"
  748. (let ((n (quotient most-positive-fixnum 2))
  749. (port (open-output-string)))
  750. (set-port-line! port n)
  751. (eqv? n (port-line port)))))
  752. (with-test-prefix "port-encoding"
  753. (pass-if-exception "set-port-encoding!, wrong encoding"
  754. exception:miscellaneous-error
  755. (set-port-encoding! (open-input-string "") "does-not-exist"))
  756. (pass-if-exception "%default-port-encoding, wrong encoding"
  757. exception:miscellaneous-error
  758. (read (with-fluids ((%default-port-encoding "does-not-exist"))
  759. (open-input-string "")))))
  760. ;;;
  761. ;;; port-for-each
  762. ;;;
  763. (with-test-prefix "port-for-each"
  764. ;; In guile 1.8.0 through 1.8.2, port-for-each could pass a freed cell to
  765. ;; its iterator func if a port was inaccessible in the last gc mark but
  766. ;; the lazy sweeping has not yet reached it to remove it from the port
  767. ;; table (scm_i_port_table). Provoking those gc conditions is a little
  768. ;; tricky, but the following code made it happen in 1.8.2.
  769. (pass-if "passing freed cell"
  770. (let ((lst '()))
  771. ;; clear out the heap
  772. (gc) (gc) (gc)
  773. ;; allocate cells so the opened ports aren't at the start of the heap
  774. (make-list 1000)
  775. (open-input-file "/dev/null")
  776. (make-list 1000)
  777. (open-input-file "/dev/null")
  778. ;; this gc leaves the above ports unmarked, ie. inaccessible
  779. (gc)
  780. ;; but they're still in the port table, so this sees them
  781. (port-for-each (lambda (port)
  782. (set! lst (cons port lst))))
  783. ;; this forces completion of the sweeping
  784. (gc) (gc) (gc)
  785. ;; and (if the bug is present) the cells accumulated in LST are now
  786. ;; freed cells, which give #f from `port?'
  787. (not (memq #f (map port? lst))))))
  788. (with-test-prefix
  789. "fdes->port"
  790. (pass-if "fdes->ports finds port"
  791. (let ((port (open-file (test-file) "w")))
  792. (not (not (memq port (fdes->ports (port->fdes port))))))))
  793. ;;;
  794. ;;; seek
  795. ;;;
  796. (with-test-prefix "seek"
  797. (with-test-prefix "file port"
  798. (pass-if "SEEK_CUR"
  799. (call-with-output-file (test-file)
  800. (lambda (port)
  801. (display "abcde" port)))
  802. (let ((port (open-file (test-file) "r")))
  803. (read-char port)
  804. (seek port 2 SEEK_CUR)
  805. (eqv? #\d (read-char port))))
  806. (pass-if "SEEK_SET"
  807. (call-with-output-file (test-file)
  808. (lambda (port)
  809. (display "abcde" port)))
  810. (let ((port (open-file (test-file) "r")))
  811. (read-char port)
  812. (seek port 3 SEEK_SET)
  813. (eqv? #\d (read-char port))))
  814. (pass-if "SEEK_END"
  815. (call-with-output-file (test-file)
  816. (lambda (port)
  817. (display "abcde" port)))
  818. (let ((port (open-file (test-file) "r")))
  819. (read-char port)
  820. (seek port -2 SEEK_END)
  821. (eqv? #\d (read-char port))))))
  822. ;;;
  823. ;;; truncate-file
  824. ;;;
  825. (with-test-prefix "truncate-file"
  826. (pass-if-exception "flonum file" exception:wrong-type-arg
  827. (truncate-file 1.0 123))
  828. (pass-if-exception "frac file" exception:wrong-type-arg
  829. (truncate-file 7/3 123))
  830. (with-test-prefix "filename"
  831. (pass-if-exception "flonum length" exception:wrong-type-arg
  832. (call-with-output-file (test-file)
  833. (lambda (port)
  834. (display "hello" port)))
  835. (truncate-file (test-file) 1.0))
  836. (pass-if "shorten"
  837. (call-with-output-file (test-file)
  838. (lambda (port)
  839. (display "hello" port)))
  840. (truncate-file (test-file) 1)
  841. (eqv? 1 (stat:size (stat (test-file)))))
  842. (pass-if-exception "shorten to current pos" exception:miscellaneous-error
  843. (call-with-output-file (test-file)
  844. (lambda (port)
  845. (display "hello" port)))
  846. (truncate-file (test-file))))
  847. (with-test-prefix "file descriptor"
  848. (pass-if "shorten"
  849. (call-with-output-file (test-file)
  850. (lambda (port)
  851. (display "hello" port)))
  852. (let ((fd (open-fdes (test-file) O_RDWR)))
  853. (truncate-file fd 1)
  854. (close-fdes fd))
  855. (eqv? 1 (stat:size (stat (test-file)))))
  856. (pass-if "shorten to current pos"
  857. (call-with-output-file (test-file)
  858. (lambda (port)
  859. (display "hello" port)))
  860. (let ((fd (open-fdes (test-file) O_RDWR)))
  861. (seek fd 1 SEEK_SET)
  862. (truncate-file fd)
  863. (close-fdes fd))
  864. (eqv? 1 (stat:size (stat (test-file))))))
  865. (with-test-prefix "file port"
  866. (pass-if "shorten"
  867. (call-with-output-file (test-file)
  868. (lambda (port)
  869. (display "hello" port)))
  870. (let ((port (open-file (test-file) "r+")))
  871. (truncate-file port 1))
  872. (eqv? 1 (stat:size (stat (test-file)))))
  873. (pass-if "shorten to current pos"
  874. (call-with-output-file (test-file)
  875. (lambda (port)
  876. (display "hello" port)))
  877. (let ((port (open-file (test-file) "r+")))
  878. (read-char port)
  879. (truncate-file port))
  880. (eqv? 1 (stat:size (stat (test-file)))))))
  881. ;;;; testing read-delimited and friends
  882. (with-test-prefix "read-delimited!"
  883. (let ((c (make-string 20 #\!)))
  884. (call-with-input-string
  885. "defdef\nghighi\n"
  886. (lambda (port)
  887. (read-delimited! "\n" c port 'concat)
  888. (pass-if "read-delimited! reads a first line"
  889. (string=? c "defdef\n!!!!!!!!!!!!!"))
  890. (read-delimited! "\n" c port 'concat 3)
  891. (pass-if "read-delimited! reads a first line"
  892. (string=? c "defghighi\n!!!!!!!!!!"))))))
  893. ;;;; char-ready?
  894. (call-with-input-string
  895. "howdy"
  896. (lambda (port)
  897. (pass-if "char-ready? returns true on string port"
  898. (char-ready? port))))
  899. ;;; This segfaults on some versions of Guile. We really should run
  900. ;;; the tests in a subprocess...
  901. (call-with-input-string
  902. "howdy"
  903. (lambda (port)
  904. (with-input-from-port
  905. port
  906. (lambda ()
  907. (pass-if "char-ready? returns true on string port as default port"
  908. (char-ready?))))))
  909. ;;;; Close current-input-port, and make sure everyone can handle it.
  910. (with-test-prefix "closing current-input-port"
  911. (for-each (lambda (procedure name)
  912. (with-input-from-port
  913. (call-with-input-string "foo" (lambda (p) p))
  914. (lambda ()
  915. (close-port (current-input-port))
  916. (pass-if-exception name
  917. exception:wrong-type-arg
  918. (procedure)))))
  919. (list read read-char read-line)
  920. '("read" "read-char" "read-line")))
  921. (delete-file (test-file))
  922. ;;; Local Variables:
  923. ;;; eval: (put 'test-decoding-error 'scheme-indent-function 3)
  924. ;;; End: