test-ports.scm 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. ;;; Copyright (C) 2023, 2024 Igalia, S.L.
  2. ;;;
  3. ;;; Licensed under the Apache License, Version 2.0 (the "License");
  4. ;;; you may not use this file except in compliance with the License.
  5. ;;; You may obtain a copy of the License at
  6. ;;;
  7. ;;; http://www.apache.org/licenses/LICENSE-2.0
  8. ;;;
  9. ;;; Unless required by applicable law or agreed to in writing, software
  10. ;;; distributed under the License is distributed on an "AS IS" BASIS,
  11. ;;; WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. ;;; See the License for the specific language governing permissions and
  13. ;;; limitations under the License.
  14. ;;; Commentary:
  15. ;;;
  16. ;;; Port tests.
  17. ;;;
  18. ;;; Code:
  19. (use-modules (srfi srfi-64)
  20. (test utils))
  21. (test-begin "test-ports")
  22. (test-call "#vu8(100 120)"
  23. (lambda ()
  24. (let ((p (open-output-bytevector)))
  25. (write-u8 100 p)
  26. (write-u8 120 p)
  27. (get-output-bytevector p))))
  28. (test-call "#vu8(100 120 130 140)"
  29. (lambda ()
  30. (let ((p (open-output-bytevector)))
  31. (write-bytevector #vu8(100 120) p)
  32. (write-bytevector #vu8(130 140) p)
  33. (get-output-bytevector p))))
  34. (test-call "#vu8(104 101 108 108 111 44 32 119 111 114 108 100)"
  35. (lambda ()
  36. (let ((p (open-output-bytevector)))
  37. (write-string "hello, world" p)
  38. (get-output-bytevector p))))
  39. (test-call "#(1 1 2 3 #<eof> #<eof> #<eof>)"
  40. (lambda ()
  41. (let* ((p (open-input-bytevector #vu8(1 2 3)))
  42. (a (peek-u8 p))
  43. (b (read-u8 p))
  44. (c (read-u8 p))
  45. (d (read-u8 p))
  46. (e (read-u8 p))
  47. (f (peek-u8 p))
  48. (g (read-u8 p)))
  49. (vector a b c d e f g))))
  50. (test-call "#(#vu8() #vu8(1) #vu8(1 2) #vu8(1 2 3) #vu8(1 2 3))"
  51. (lambda ()
  52. (define (read-n n)
  53. (read-bytevector n (open-input-bytevector #vu8(1 2 3))))
  54. (vector (read-n 0)
  55. (read-n 1)
  56. (read-n 2)
  57. (read-n 3)
  58. (read-n 4))))
  59. (test-call "#<eof>"
  60. (lambda ()
  61. (read-bytevector 1 (open-input-bytevector #vu8()))))
  62. (test-call "#(#\\h #\\h #\\e #\\l #\\l #\\o #<eof> #<eof> #<eof>)"
  63. (lambda ()
  64. (let* ((p (open-input-bytevector #vu8(104 101 108 108 111)))
  65. (a (peek-char p))
  66. (b (read-char p))
  67. (c (read-char p))
  68. (d (read-char p))
  69. (e (read-char p))
  70. (f (read-char p))
  71. (g (read-char p))
  72. (h (peek-char p))
  73. (i (read-char p)))
  74. (vector a b c d e f g h i))))
  75. (test-call "#(\"\" \"h\" \"he\" \"hel\" \"hell\" \"hello\" \"hello\")"
  76. (lambda ()
  77. (define (read-n n)
  78. (read-string n (open-input-bytevector #vu8(104 101 108 108 111))))
  79. (vector (read-n 0)
  80. (read-n 1)
  81. (read-n 2)
  82. (read-n 3)
  83. (read-n 4)
  84. (read-n 5)
  85. (read-n 6))))
  86. (test-call "#(43 43 70 #(101 101 421) 70)"
  87. (lambda ()
  88. (let* ((p (make-parameter 42 1+))
  89. (a (p))
  90. (b (p 69))
  91. (c (p))
  92. (d (parameterize ((p 100))
  93. (let* ((a (p))
  94. (b (p 420))
  95. (c (p)))
  96. (vector a b c))))
  97. (e (p)))
  98. (vector a b c d e))))
  99. (test-call "#(\"foo\" \"bar\" \"baz\" \"asdfa\" #<eof> #<eof>)"
  100. (lambda ()
  101. (let* ((p (open-input-string "foo\nbar\r\nbaz\rasdfa"))
  102. (a (read-line p))
  103. (b (read-line p))
  104. (c (read-line p))
  105. (d (read-line p))
  106. (e (read-line p))
  107. (f (read-line p)))
  108. (vector a b c d e f))))
  109. ;; Apologies for the wall of text, but this tests that input that
  110. ;; exceeds the default buffer size (1024) comes through correctly.
  111. (test-call "\"This paper would not have happened if Sussman had not been forced to\\nthink about lambda calculus by having to teach 6.031, not would it\\nhave happened had not Steele been forced to understand PLASMA by\\nmorbid curiosity.\\n\\nThis work developed out of an initial attempt to understand the\\nactorness of actors. Steele thought he understood it, but couldn't\\nexplain it; Sussamn suggested the experimental approach of actually\\nbuilding an \\\"ACTORS interpreter\\\". This interpreter attempted to\\nintermix the user of actors and LISP lambda expressions in a clean\\nmanner. When it was completed, we discovered that the \\\"actors\\\" and\\nthe lambda expressions were identical in implementation. Once we had\\ndiscovered this, all the rest fell into place, and it was only natural\\nto begin thinking about actors in terms of lambda calculus. The\\noriginal interpreter was call-by-name for various reasons having to do\\nwith 6.031; we subsequently experimentally discovered how call-by-name\\nscrews iteration, and rewrote it to use call-by-value. Note well that\\nwe did not bring forth a clean implementation in one brilliant flash\\nof understanding; we used an experimental and highly empirical\\napproach to bootstrap our knowledge.\""
  112. (lambda ()
  113. (let ((p (open-input-string
  114. "This paper would not have happened if Sussman had not been forced to
  115. think about lambda calculus by having to teach 6.031, not would it
  116. have happened had not Steele been forced to understand PLASMA by
  117. morbid curiosity.
  118. This work developed out of an initial attempt to understand the
  119. actorness of actors. Steele thought he understood it, but couldn't
  120. explain it; Sussamn suggested the experimental approach of actually
  121. building an \"ACTORS interpreter\". This interpreter attempted to
  122. intermix the user of actors and LISP lambda expressions in a clean
  123. manner. When it was completed, we discovered that the \"actors\" and
  124. the lambda expressions were identical in implementation. Once we had
  125. discovered this, all the rest fell into place, and it was only natural
  126. to begin thinking about actors in terms of lambda calculus. The
  127. original interpreter was call-by-name for various reasons having to do
  128. with 6.031; we subsequently experimentally discovered how call-by-name
  129. screws iteration, and rewrote it to use call-by-value. Note well that
  130. we did not bring forth a clean implementation in one brilliant flash
  131. of understanding; we used an experimental and highly empirical
  132. approach to bootstrap our knowledge.")))
  133. (list->string
  134. (let lp ((char (read-char p)))
  135. (if (eof-object? char)
  136. '()
  137. (cons char (lp (read-char p)))))))))
  138. (test-call "#f"
  139. (lambda (str)
  140. (let ((port (open-input-string str)))
  141. (call-with-port port read-char)
  142. (input-port-open? port)))
  143. "foo")
  144. ;; We cannot test file ports against d8 because it lacks a sufficient
  145. ;; filesystem API.
  146. (define input-fixture
  147. (in-vicinity (getenv "HOOT_TEST_DATA_DIR") "fixtures/hello"))
  148. (parameterize ((use-d8? #f))
  149. (test-call
  150. "(hello and welcome back to scheme)"
  151. (lambda ()
  152. (call-with-input-file ,input-fixture
  153. (lambda (port)
  154. (let loop ()
  155. (match (read port)
  156. ((? eof-object?) '())
  157. (x (cons x (loop)))))))))
  158. (with-additional-imports
  159. ((only (hoot ports) seek))
  160. (test-call
  161. "welcome"
  162. (lambda ()
  163. (call-with-input-file ,input-fixture
  164. (lambda (port)
  165. (seek port 10 'cur)
  166. (read port))))))
  167. ;; Not guaranteed to be a unique name, but 'mkstemp' opens a port
  168. ;; which we don't want since we need Hoot to open the port.
  169. (let ((tmp "/tmp/tmp-hoot-port-test"))
  170. (define-syntax-rule (test-output-file expected expr)
  171. (unwind-protect
  172. (lambda ()
  173. (test-call expected expr))
  174. (lambda ()
  175. (false-if-exception
  176. (delete-file tmp)))))
  177. (test-output-file
  178. "#t"
  179. (lambda ()
  180. (call-with-output-file ,tmp (lambda (port) #t))
  181. (file-exists? ,tmp)))
  182. (test-output-file
  183. "deleted"
  184. (lambda ()
  185. (call-with-output-file ,tmp (lambda (port) #t))
  186. (and (file-exists? ,tmp)
  187. (begin
  188. (delete-file ,tmp)
  189. (file-exists? ,tmp)
  190. 'deleted))))
  191. (test-output-file
  192. "HELLO"
  193. (lambda ()
  194. (call-with-output-file ,tmp
  195. (lambda (port)
  196. (write 'HELLO port)))
  197. (call-with-input-file ,tmp read)))))
  198. (test-end* "test-ports")