ffi-misc-check.scm 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. ; Part of Scheme 48 1.9. See file COPYING for notices and license.
  2. ; Authors: Harald Glab-Plhak, Marcus Crestani
  3. ;; test for the new ffi
  4. (define-test-suite ffi-misc-tests)
  5. (define-test-case ffi-call-scheme-test ffi-misc-tests
  6. (check
  7. (let ((result (ffi-call-scheme (lambda args
  8. (apply + args))
  9. 3 4 5 6)))
  10. result)
  11. => 15))
  12. (define-test-case ffi-call-scheme-assertion ffi-misc-tests
  13. (call-with-current-continuation
  14. (lambda (esc)
  15. (with-exception-handler
  16. (lambda (c)
  17. (esc
  18. (call-with-values
  19. (lambda () (decode-condition c))
  20. (lambda (type who message more-stuff)
  21. (check type => 'assertion-violation)
  22. (check who => 'ffi)))))
  23. (lambda ()
  24. (ffi-call-scheme
  25. (lambda (ignore-1 ignore-2 ignore-3)
  26. (assertion-violation 'ffi "Testing if exceptions from externally called Scheme code work.")
  27. 'ignore)
  28. 3 esc 2 3)
  29. (check #f => 'should-never-reach-this-point))))))
  30. (define-test-case ffi-values-test ffi-misc-tests
  31. (check
  32. (let ((value (ffi-make-strange-value 10 "LolliPop")))
  33. (let ((result(ffi-strange-value->list value)))
  34. (ffi-strange-value-free value)
  35. result))
  36. => '(10 . "LolliPop")))
  37. (define-test-case ffi-weak-pointer ffi-misc-tests
  38. (let ((w (make-weak-pointer (cons 23 42))))
  39. (check (equal? (cons 23 42) (weak-pointer-ref w)))
  40. (collect)
  41. (check (not (weak-pointer-ref w)))))
  42. (define-test-case ffi-weak-pointer-2 ffi-misc-tests
  43. (let ((w (ffi-make-weak-pointer (cons 23 42))))
  44. (check (ffi-weak-pointer? w))
  45. (check (equal? (cons 23 42) (ffi-weak-pointer-ref w)))
  46. (collect)
  47. (check (not (ffi-weak-pointer-ref w)))))
  48. (define-test-case ffi-strings-test ffi-misc-tests
  49. (let ((string "Grüße"))
  50. (let ((latin-1 (ffi-check-string-latin-1 string))
  51. (utf-8 (ffi-check-string-utf-8 string)))
  52. (check (eq? (byte-vector-ref latin-1 2) 252))
  53. (check (eq? (byte-vector-ref latin-1 3) 223))
  54. (check (eq? (byte-vector-ref utf-8 3) 188))
  55. (check (eq? (byte-vector-ref utf-8 5) 159)))))
  56. (define-test-case ffi-extract-byte-vector-test ffi-misc-tests
  57. (let ((bv (make-byte-vector 10 97)))
  58. (check (ffi-extract-byte-vector bv))))
  59. (define-test-case ffi-extract-byte-vector-test-readonly ffi-misc-tests
  60. (let ((bv (make-byte-vector 10 97)))
  61. (check (ffi-extract-byte-vector-readonly bv))
  62. (check (byte-vector-ref bv 4) => 97)
  63. (check (byte-vector-ref bv 8) => 97)))
  64. (define-test-case ffi-extract-and-modify-byte-vector-test ffi-misc-tests
  65. (let ((bv (make-byte-vector 10 97)))
  66. (check (ffi-extract-and-modify-byte-vector bv))
  67. (check (eq? (byte-vector-ref bv 5) 53))))
  68. (define-test-case ffi-extract-twice-and-modify-byte-vector-test ffi-misc-tests
  69. (let ((bv (make-byte-vector 10 97)))
  70. (check (ffi-extract-twice-and-modify-byte-vector bv))
  71. (check (eq? (byte-vector-ref bv 4) 52))
  72. (check (eq? (byte-vector-ref bv 8) 56))))
  73. (define (maybe-make-unmovable-byte-vector count init)
  74. (call-with-current-continuation
  75. (lambda (esc)
  76. (with-exception-handler
  77. (lambda (c)
  78. (esc
  79. (call-with-values
  80. (lambda () (decode-condition c))
  81. (lambda (type who message more-stuff)
  82. (check type => 'assertion-violation)
  83. (check who => 'make-unmovable-byte-vector)
  84. (check message => "unimplemented instruction")
  85. (newline)
  86. (display "Warning: Unmovable byte vectors are not supported by the GC.")(newline)
  87. (display " Using a regular byte vector for this test.")(newline)
  88. (make-byte-vector count init)))))
  89. (lambda ()
  90. (make-unmovable-byte-vector count init))))))
  91. (define-test-case ffi-extract-unmovable-byte-vector-test ffi-misc-tests
  92. (let ((bv (maybe-make-unmovable-byte-vector 10 97)))
  93. (check (ffi-extract-byte-vector bv))))
  94. (define-test-case ffi-extract-and-modify-unmovable-byte-vector-test ffi-misc-tests
  95. (let ((bv (maybe-make-unmovable-byte-vector 10 97)))
  96. (check (ffi-extract-and-modify-byte-vector bv))
  97. (check (eq? (byte-vector-ref bv 5) 53))))
  98. (define-test-case ffi-extract-twice-and-modify-unmovable-byte-vector-test ffi-misc-tests
  99. (let ((bv (maybe-make-unmovable-byte-vector 10 97)))
  100. (check (ffi-extract-twice-and-modify-byte-vector bv))
  101. (check (eq? (byte-vector-ref bv 4) 52))
  102. (check (eq? (byte-vector-ref bv 8) 56))))
  103. (define-test-case ffi-extract-byte-vector-and-call-scheme-test ffi-misc-tests
  104. (let* ((bv (make-byte-vector 10 97))
  105. (callback (lambda ()
  106. (check (byte-vector-ref bv 4) => 52)
  107. (check (byte-vector-ref bv 8) => 56)
  108. (byte-vector-set! bv 4 98)
  109. (byte-vector-set! bv 8 98))))
  110. (check (ffi-extract-byte-vector-and-call-scheme bv callback))
  111. (check (byte-vector-ref bv 4) => 56)
  112. (check (byte-vector-ref bv 8) => 52)))
  113. (define-test-case ffi-extract-byte-vector-assertion-test ffi-misc-tests
  114. (let ((bv (make-byte-vector 10 97)))
  115. (call-with-current-continuation
  116. (lambda (esc)
  117. (with-exception-handler
  118. (lambda (c)
  119. (esc
  120. (call-with-values
  121. (lambda () (decode-condition c))
  122. (lambda (type who message more-stuff)
  123. (check type => 'assertion-violation)
  124. (check who => "ffi_extract_byte_vector_assertion")
  125. (check message => "throw back to Scheme")
  126. (check (byte-vector-ref bv 4) => 52)
  127. (check (byte-vector-ref bv 8) => 56)
  128. (let ((thrown-bv (car more-stuff)))
  129. (check (byte-vector-ref thrown-bv 4) => 52)
  130. (check (byte-vector-ref thrown-bv 8) => 56))
  131. (byte-vector-set! bv 4 98)
  132. (byte-vector-set! bv 8 98)))))
  133. (lambda ()
  134. (ffi-extract-byte-vector-assertion bv)
  135. (check #f => 'should-never-reach-this-point)))))))
  136. (define-test-case ffi-extract-unmovable-byte-vector-and-call-scheme-test ffi-misc-tests
  137. (let* ((bv (maybe-make-unmovable-byte-vector 10 97))
  138. (callback (lambda ()
  139. (check (byte-vector-ref bv 4) => 52)
  140. (check (byte-vector-ref bv 8) => 56)
  141. (byte-vector-set! bv 4 98)
  142. (byte-vector-set! bv 8 98))))
  143. (check (ffi-extract-byte-vector-and-call-scheme bv callback))
  144. (check (byte-vector-ref bv 4) => 56)
  145. (check (byte-vector-ref bv 8) => 52)))
  146. (define-test-case ffi-extract-unmovable-byte-vector-assertion-test ffi-misc-tests
  147. (let ((bv (maybe-make-unmovable-byte-vector 10 97)))
  148. (call-with-current-continuation
  149. (lambda (esc)
  150. (with-exception-handler
  151. (lambda (c)
  152. (esc
  153. (call-with-values
  154. (lambda () (decode-condition c))
  155. (lambda (type who message more-stuff)
  156. (check type => 'assertion-violation)
  157. (check who => "ffi_extract_byte_vector_assertion")
  158. (check message => "throw back to Scheme")
  159. (check (byte-vector-ref bv 4) => 52)
  160. (check (byte-vector-ref bv 8) => 56)
  161. (let ((thrown-bv (car more-stuff)))
  162. (check (byte-vector-ref thrown-bv 4) => 52)
  163. (check (byte-vector-ref thrown-bv 8) => 56))
  164. (byte-vector-set! bv 4 98)
  165. (byte-vector-set! bv 8 98)))))
  166. (lambda ()
  167. (ffi-extract-byte-vector-assertion bv)
  168. (check #f => 'should-never-reach-this-point)))))))