test-ports.scm 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. ;;; Copyright (C) 2023 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. (test-call "#f"
  110. (lambda (str)
  111. (let ((port (open-input-string str)))
  112. (call-with-port port read-char)
  113. (input-port-open? port)))
  114. "foo")
  115. (test-end* "test-ports")