Fix / Improve with-foreign-pointer-as-string in manual
[cffi.git] / tests / misc.lisp
bloba57925ee5b590b2e82881bf8de1e3be4abb011fe
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; misc.lisp --- Miscellaneous tests.
4 ;;;
5 ;;; Copyright (C) 2006, Luis Oliveira <loliveira@common-lisp.net>
6 ;;;
7 ;;; Permission is hereby granted, free of charge, to any person
8 ;;; obtaining a copy of this software and associated documentation
9 ;;; files (the "Software"), to deal in the Software without
10 ;;; restriction, including without limitation the rights to use, copy,
11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
12 ;;; of the Software, and to permit persons to whom the Software is
13 ;;; furnished to do so, subject to the following conditions:
14 ;;;
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
17 ;;;
18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;;; DEALINGS IN THE SOFTWARE.
26 ;;;
28 (in-package #:cffi-tests)
30 ;;;# foreign-symbol-pointer tests
32 ;;; This might be useful for some libraries that compare function
33 ;;; pointers. http://thread.gmane.org/gmane.lisp.cffi.devel/694
34 (defcfun "compare_against_abs" :boolean (p :pointer))
36 (deftest foreign-symbol-pointer.1
37 (compare-against-abs (foreign-symbol-pointer "abs"))
40 (defcfun "compare_against_xpto_fun" :boolean (p :pointer))
42 (deftest foreign-symbol-pointer.2
43 (compare-against-xpto-fun (foreign-symbol-pointer "xpto_fun"))
46 ;;;# Library tests
47 ;;;
48 ;;; Need to figure out a way to test this. CLISP, for instance, will
49 ;;; automatically reopen the foreign-library when we call a foreign
50 ;;; function so we can't test CLOSE-FOREIGN-LIBRARY this way.
51 ;;;
52 ;;; IIRC, GCC has some extensions to have code run when a library is
53 ;;; loaded and stuff like that. That could work.
55 #||
56 #-(and ecl (not dffi))
57 (deftest library.close.2
58 (unwind-protect
59 (progn
60 (close-foreign-library 'libtest)
61 (ignore-errors (my-sqrtf 16.0)))
62 (load-test-libraries))
63 nil)
65 #-(or (and ecl (not dffi))
66 cffi-sys::flat-namespace
67 cffi-sys::no-foreign-funcall)
68 (deftest library.close.2
69 (unwind-protect
70 (values
71 (foreign-funcall ("ns_function" :library libtest) :boolean)
72 (close-foreign-library 'libtest)
73 (foreign-funcall "ns_function" :boolean)
74 (close-foreign-library 'libtest2)
75 (close-foreign-library 'libtest2)
76 (ignore-errors (foreign-funcall "ns_function" :boolean)))
77 (load-test-libraries))
78 t t nil t nil nil)
79 ||#
81 (deftest library.error.1
82 (handler-case (load-foreign-library "libdoesnotexistimsure")
83 (load-foreign-library-error () 'error))
84 error)
86 (define-foreign-library pseudo-library
87 (t pseudo-library-spec))
89 ;;; RT: T clause was being handled as :T by FEATUREP.
90 ;;;
91 ;;; We might want to export (and clean up) the API used in this test
92 ;;; when the need arises.
93 (deftest library.t-clause
94 (eq (cffi::foreign-library-spec
95 (cffi::get-foreign-library 'pseudo-library))
96 'pseudo-library-spec)
99 (define-foreign-library library-with-pathname
100 (t #p"libdoesnotexistimsure"))
102 ;;; RT: we were mishandling pathnames within libraries. (lp#1720626)
103 (deftest library.error.2
104 (handler-case (load-foreign-library 'library-with-pathname)
105 (load-foreign-library-error () 'error))
106 error)
108 (deftest library.error.3
109 (handler-case (load-foreign-library #p"libdoesnotexistimsure")
110 (load-foreign-library-error () 'error))
111 error)
113 ;;;# Shareable Byte Vector Tests
115 #+ecl
116 (mapc (lambda (x) (pushnew x rtest::*expected-failures*))
117 '(shareable-vector.1 shareable-vector.2))
119 (deftest shareable-vector.1
120 (let ((vector (cffi-sys::make-shareable-byte-vector 5)))
121 (cffi::with-pointer-to-vector-data (pointer vector)
122 (strcpy pointer "xpto"))
123 vector)
124 #(120 112 116 111 0))
126 (deftest shareable-vector.2
127 (block nil
128 (let ((vector (cffi-sys::make-shareable-byte-vector 5)))
129 (cffi::with-pointer-to-vector-data (pointer vector)
130 (strcpy pointer "xpto")
131 (return vector))))
132 #(120 112 116 111 0))
134 ;;; Inline foreign-type-size
136 (deftest foreign-type-size.inline.int
137 (eql (foreign-type-size :int) (locally (declare (notinline foreign-type-size)) (foreign-type-size :int)))
140 (deftest foreign-type-size.inline.uint32
141 (eql (foreign-type-size :uint32) (locally (declare (notinline foreign-type-size)) (foreign-type-size :uint32)))
144 (deftest foreign-type-size.inline.ptrdiff
145 (eql (foreign-type-size :ptrdiff) (locally (declare (notinline foreign-type-size)) (foreign-type-size :ptrdiff)))
148 (deftest foreign-type-size.inline.size
149 (eql (foreign-type-size :size) (locally (declare (notinline foreign-type-size)) (foreign-type-size :size)))
152 (deftest foreign-type-size.inline.offset
153 (eql (foreign-type-size :offset) (locally (declare (notinline foreign-type-size)) (foreign-type-size :offset)))
156 (deftest foreign-type-size.inline.uintptr
157 (eql (foreign-type-size :uintptr) (locally (declare (notinline foreign-type-size)) (foreign-type-size :uintptr)))
160 (deftest foreign-type-size.inline.intptr
161 (eql (foreign-type-size :intptr) (locally (declare (notinline foreign-type-size)) (foreign-type-size :intptr)))