foreign.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  1. ;;;; foreign.test --- FFI. -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2010, 2011, 2012, 2013 Free Software Foundation, Inc.
  4. ;;;;
  5. ;;;; This library is free software; you can redistribute it and/or
  6. ;;;; modify it under the terms of the GNU Lesser General Public
  7. ;;;; License as published by the Free Software Foundation; either
  8. ;;;; version 3 of the License, or (at your option) any later version.
  9. ;;;;
  10. ;;;; This library is distributed in the hope that it will be useful,
  11. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;;;; Lesser General Public License for more details.
  14. ;;;;
  15. ;;;; You should have received a copy of the GNU Lesser General Public
  16. ;;;; License along with this library; if not, write to the Free Software
  17. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. ;;;
  19. ;;; See also ../standalone/test-ffi for FFI tests.
  20. ;;;
  21. (define-module (test-foreign)
  22. #:use-module (system foreign)
  23. #:use-module (rnrs bytevectors)
  24. #:use-module (srfi srfi-1)
  25. #:use-module (srfi srfi-26)
  26. #:use-module (ice-9 format)
  27. #:use-module (test-suite lib))
  28. (with-test-prefix "dynamic-pointer"
  29. (pass-if-exception
  30. "error message"
  31. '(misc-error . "^Symbol not found")
  32. (dynamic-func "does_not_exist___" (dynamic-link))))
  33. (with-test-prefix "null pointer"
  34. (pass-if "pointer?"
  35. (pointer? %null-pointer))
  36. (pass-if "zero"
  37. (= 0 (pointer-address %null-pointer)))
  38. (pass-if "null pointer identity"
  39. (eq? %null-pointer (make-pointer 0)))
  40. (pass-if "null-pointer? %null-pointer"
  41. (null-pointer? %null-pointer))
  42. (pass-if-exception "dereference-pointer %null-pointer"
  43. exception:null-pointer-error
  44. (dereference-pointer %null-pointer))
  45. (pass-if-exception "pointer->bytevector %null-pointer"
  46. exception:null-pointer-error
  47. (pointer->bytevector %null-pointer 7)))
  48. (with-test-prefix "make-pointer"
  49. (pass-if "pointer?"
  50. (pointer? (make-pointer 123)))
  51. (pass-if "address preserved"
  52. (= 123 (pointer-address (make-pointer 123))))
  53. (pass-if "equal?"
  54. (equal? (make-pointer 123) (make-pointer 123)))
  55. (pass-if "equal? modulo finalizer"
  56. (let ((finalizer (false-if-exception
  57. (dynamic-func "scm_is_pair" (dynamic-link)))))
  58. (if (not finalizer)
  59. (throw 'unresolved) ; Windows or a static build
  60. (equal? (make-pointer 123)
  61. (make-pointer 123 finalizer)))))
  62. (pass-if "equal? modulo finalizer (set-pointer-finalizer!)"
  63. (let ((finalizer (false-if-exception
  64. (dynamic-func "scm_is_pair" (dynamic-link))))
  65. (ptr (make-pointer 123)))
  66. (if (not finalizer)
  67. (throw 'unresolved) ; Windows or a static build
  68. (begin
  69. (set-pointer-finalizer! ptr finalizer)
  70. (equal? (make-pointer 123) ptr)))))
  71. (pass-if "not equal?"
  72. (not (equal? (make-pointer 123) (make-pointer 456)))))
  73. (with-test-prefix "pointer<->scm"
  74. (pass-if "immediates"
  75. (equal? (pointer->scm (scm->pointer #\newline))
  76. #\newline))
  77. (pass-if "non-immediates"
  78. (equal? (pointer->scm (scm->pointer "Hello, world!"))
  79. "Hello, world!")))
  80. (define-wrapped-pointer-type foo
  81. foo?
  82. wrap-foo unwrap-foo
  83. (lambda (x p)
  84. (format p "#<foo! ~a>" (pointer-address (unwrap-foo x)))))
  85. (with-test-prefix "define-wrapped-pointer-type"
  86. (pass-if "foo?"
  87. (foo? (wrap-foo %null-pointer)))
  88. (pass-if "unwrap-foo"
  89. (let ((p (make-pointer 123)))
  90. (eq? p (unwrap-foo (wrap-foo p)))))
  91. (pass-if "identity"
  92. (let ((p1 (make-pointer 123))
  93. (p2 (make-pointer 123)))
  94. (eq? (wrap-foo p1)
  95. (wrap-foo p2))))
  96. (pass-if "printer"
  97. (string=? "#<foo! 123>"
  98. (with-output-to-string
  99. (lambda ()
  100. (write (wrap-foo (make-pointer 123))))))))
  101. (with-test-prefix "pointer<->bytevector"
  102. (pass-if "bijection"
  103. (let ((bv #vu8(0 1 2 3 4 5 6 7)))
  104. (equal? (pointer->bytevector (bytevector->pointer bv)
  105. (bytevector-length bv))
  106. bv)))
  107. (pass-if "pointer from bits"
  108. (let* ((bytes (iota (sizeof '*)))
  109. (bv (u8-list->bytevector bytes))
  110. (fold (case (native-endianness)
  111. ((little) fold-right)
  112. ((big) fold)
  113. (else (error "unsupported endianness")))))
  114. (= (pointer-address
  115. (make-pointer (bytevector-uint-ref bv 0 (native-endianness)
  116. (sizeof '*))))
  117. (fold (lambda (byte address)
  118. (+ byte (* 256 address)))
  119. 0
  120. bytes))))
  121. (pass-if "dereference-pointer"
  122. (let* ((bytes (iota (sizeof '*)))
  123. (bv (u8-list->bytevector bytes))
  124. (fold (case (native-endianness)
  125. ((little) fold-right)
  126. ((big) fold)
  127. (else (error "unsupported endianness")))))
  128. (= (pointer-address
  129. (dereference-pointer (bytevector->pointer bv)))
  130. (fold (lambda (byte address)
  131. (+ byte (* 256 address)))
  132. 0
  133. bytes)))))
  134. (with-test-prefix "pointer<->string"
  135. (pass-if-exception "%default-port-conversion-strategy is error"
  136. exception:encoding-error
  137. (let ((s "χαοσ"))
  138. (with-fluids ((%default-port-conversion-strategy 'error))
  139. (string->pointer s "ISO-8859-1"))))
  140. (pass-if "%default-port-conversion-strategy is escape"
  141. (let ((s "teĥniko"))
  142. (equal? (with-fluids ((%default-port-conversion-strategy 'escape))
  143. (pointer->string (string->pointer s "ISO-8859-1")))
  144. (format #f "te\\u~4,'0xniko"
  145. (char->integer #\ĥ)))))
  146. (pass-if "%default-port-conversion-strategy is substitute"
  147. (let ((s "teĥniko")
  148. (member (negate (negate member))))
  149. (member (with-fluids ((%default-port-conversion-strategy 'substitute))
  150. (pointer->string (string->pointer s "ISO-8859-1")))
  151. '("te?niko"
  152. ;; This form is found on FreeBSD 8.2 and Darwin 10.8.0.
  153. "te^hniko"))))
  154. (pass-if "bijection"
  155. (let ((s "hello, world"))
  156. (string=? s (pointer->string (string->pointer s)))))
  157. (pass-if "bijection [latin1]"
  158. (with-latin1-locale
  159. (let ((s "Szép jó napot!"))
  160. (string=? s (pointer->string (string->pointer s))))))
  161. (pass-if "bijection, utf-8"
  162. (let ((s "hello, world"))
  163. (string=? s (pointer->string (string->pointer s "utf-8")
  164. -1 "utf-8"))))
  165. (pass-if "bijection, utf-8 [latin1]"
  166. (let ((s "Szép jó napot!"))
  167. (string=? s (pointer->string (string->pointer s "utf-8")
  168. -1 "utf-8")))))
  169. (with-test-prefix "pointer->procedure"
  170. (pass-if-exception "object instead of pointer"
  171. exception:wrong-type-arg
  172. (let ((p (pointer->procedure '* %null-pointer '(*))))
  173. (p #t))))
  174. (with-test-prefix "procedure->pointer"
  175. (define qsort
  176. ;; Bindings for libc's `qsort' function.
  177. ;; On some platforms, such as MinGW, `qsort' is visible only if
  178. ;; linking with `-export-dynamic'. Just skip these tests when it's
  179. ;; not visible.
  180. (false-if-exception
  181. (pointer->procedure void
  182. (dynamic-func "qsort" (dynamic-link))
  183. (list '* size_t size_t '*))))
  184. (define (dereference-pointer-to-byte ptr)
  185. (let ((b (pointer->bytevector ptr 1)))
  186. (bytevector-u8-ref b 0)))
  187. (define input
  188. '(7 1 127 3 5 4 77 2 9 0))
  189. (pass-if "qsort"
  190. (if (and qsort (defined? 'procedure->pointer))
  191. (let* ((called? #f)
  192. (cmp (lambda (x y)
  193. (set! called? #t)
  194. (- (dereference-pointer-to-byte x)
  195. (dereference-pointer-to-byte y))))
  196. (ptr (procedure->pointer int cmp (list '* '*)))
  197. (bv (u8-list->bytevector input)))
  198. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  199. (procedure->pointer int cmp (list '* '*)))
  200. (and called?
  201. (equal? (bytevector->u8-list bv)
  202. (sort input <))))
  203. (throw 'unresolved)))
  204. (pass-if-exception "qsort, wrong return type"
  205. exception:wrong-type-arg
  206. (if (and qsort (defined? 'procedure->pointer))
  207. (let* ((cmp (lambda (x y) #f)) ; wrong return type
  208. (ptr (procedure->pointer int cmp (list '* '*)))
  209. (bv (u8-list->bytevector input)))
  210. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  211. (procedure->pointer int cmp (list '* '*)))
  212. #f)
  213. (throw 'unresolved)))
  214. (pass-if-exception "qsort, wrong arity"
  215. exception:wrong-num-args
  216. (if (and qsort (defined? 'procedure->pointer))
  217. (let* ((cmp (lambda (x y z) #f)) ; wrong arity
  218. (ptr (procedure->pointer int cmp (list '* '*)))
  219. (bv (u8-list->bytevector input)))
  220. (qsort (bytevector->pointer bv) (bytevector-length bv) 1
  221. (procedure->pointer int cmp (list '* '*)))
  222. #f)
  223. (throw 'unresolved)))
  224. (pass-if "bijection"
  225. (if (defined? 'procedure->pointer)
  226. (let* ((proc (lambda (x y z)
  227. (+ x y z 0.0)))
  228. (ret double)
  229. (args (list float int16 double))
  230. (proc* (pointer->procedure ret
  231. (procedure->pointer ret proc args)
  232. args))
  233. (arg1 (map (cut / <> 2.0) (iota 123)))
  234. (arg2 (iota 123 32000))
  235. (arg3 (map (cut / <> 4.0) (iota 123 100 4))))
  236. (equal? (map proc arg1 arg2 arg3)
  237. (map proc* arg1 arg2 arg3)))
  238. (throw 'unresolved)))
  239. (pass-if "procedures returning a pointer"
  240. (if (defined? 'procedure->pointer)
  241. (let* ((called? #f)
  242. (proc (lambda (i) (set! called? #t) (make-pointer i)))
  243. (pointer (procedure->pointer '* proc (list int)))
  244. (proc* (pointer->procedure '* pointer (list int)))
  245. (result (proc* 777)))
  246. (and called? (equal? result (make-pointer 777))))
  247. (throw 'unresolved)))
  248. (pass-if "procedures returning void"
  249. (if (defined? 'procedure->pointer)
  250. (let* ((called? #f)
  251. (proc (lambda () (set! called? #t)))
  252. (pointer (procedure->pointer void proc '()))
  253. (proc* (pointer->procedure void pointer '())))
  254. (proc*)
  255. called?)
  256. (throw 'unresolved)))
  257. (pass-if "procedure is retained"
  258. ;; The lambda passed to `procedure->pointer' must remain live.
  259. (if (defined? 'procedure->pointer)
  260. (let* ((ptr (procedure->pointer int
  261. (lambda (x) (+ x 7))
  262. (list int)))
  263. (procs (unfold (cut >= <> 10000)
  264. (lambda (i)
  265. (pointer->procedure int ptr (list int)))
  266. 1+
  267. 0)))
  268. (gc) (gc) (gc)
  269. (every (cut = <> 9)
  270. (map (lambda (f) (f 2)) procs)))
  271. (throw 'unresolved)))
  272. (pass-if "arity"
  273. (if (and qsort (defined? 'procedure->pointer))
  274. (equal? '(4 0 #f) (procedure-minimum-arity qsort))
  275. (throw 'unresolved))))
  276. (with-test-prefix "structs"
  277. (pass-if "sizeof { int8, double }"
  278. (= (sizeof (list int8 double))
  279. (+ (alignof double) (sizeof double))))
  280. (pass-if "sizeof { double, int8 }"
  281. (= (sizeof (list double int8))
  282. (+ (alignof double) (sizeof double))))
  283. (pass-if "sizeof { short, int, long, pointer }"
  284. (let ((layout (list short int long '*)))
  285. (>= (sizeof layout)
  286. (reduce + 0.0 (map sizeof layout)))))
  287. (pass-if "alignof { int8, double, int8 }"
  288. ;; alignment of the most strictly aligned component
  289. (let ((layout (list int8 double int8)))
  290. (= (alignof layout) (alignof double))))
  291. (pass-if "parse-c-struct"
  292. (let ((layout (list int64 uint8))
  293. (data (list -300 43)))
  294. (equal? (parse-c-struct (make-c-struct layout data)
  295. layout)
  296. data)))
  297. (pass-if "alignment constraints honored"
  298. (let ((layout (list int8 double))
  299. (data (list -7 3.14)))
  300. (equal? (parse-c-struct (make-c-struct layout data)
  301. layout)
  302. data)))
  303. (pass-if "int8, pointer"
  304. (let ((layout (list uint8 '*))
  305. (data (list 222 (make-pointer 7777))))
  306. (equal? (parse-c-struct (make-c-struct layout data)
  307. layout)
  308. data)))
  309. (pass-if "unsigned-long, int8, size_t"
  310. (let ((layout (list unsigned-long int8 size_t))
  311. (data (list (expt 2 17) -128 (expt 2 18))))
  312. (equal? (parse-c-struct (make-c-struct layout data)
  313. layout)
  314. data)))
  315. (pass-if "long, int, pointer"
  316. (let ((layout (list long int '*))
  317. (data (list (- (expt 2 17)) -222 (make-pointer 777))))
  318. (equal? (parse-c-struct (make-c-struct layout data)
  319. layout)
  320. data)))
  321. (pass-if "int8, pointer, short, double"
  322. (let ((layout (list int8 '* short double))
  323. (data (list 77 %null-pointer -42 3.14)))
  324. (equal? (parse-c-struct (make-c-struct layout data)
  325. layout)
  326. data)))
  327. (pass-if "int8, { int8, double, int8 }, int16"
  328. (let ((layout (list int8 (list int8 double int8) int16))
  329. (data (list 77 (list 42 4.2 55) 88)))
  330. (equal? (parse-c-struct (make-c-struct layout data)
  331. layout)
  332. data))))