1.0.9.53: trivial typo fixes
[sbcl/lichteblau.git] / tests / foreign.test.sh
blob2f1f40587689f3efb1c6a66d0ff6d1bad8351818
1 #!/bin/sh
3 # tests related to foreign function interface and loading of shared
4 # libraries
6 # This software is part of the SBCL system. See the README file for
7 # more information.
9 # While most of SBCL is derived from the CMU CL system, the test
10 # files (like this one) were written from scratch after the fork
11 # from CMU CL.
13 # This software is in the public domain and is provided with
14 # absolutely no warranty. See the COPYING and CREDITS files for
15 # more information.
17 echo //entering foreign.test.sh
19 # simple way to make sure we're not punting by accident:
20 # setting PUNT to anything other than 104 will make non-dlopen
21 # and non-linkage-table platforms fail this
22 PUNT=104
24 testfilestem=${TMPDIR:-/tmp}/sbcl-foreign-test-$$
26 ## Make some shared object files to test with.
28 build_so() {
29 echo building $1.so
30 if [ "`uname -m`" = x86_64 -o "`uname -m`" = amd64 ]; then
31 CFLAGS="$CFLAGS -fPIC"
33 if [ "`uname`" = Darwin ]; then
34 SO_FLAGS="-bundle"
35 else
36 SO_FLAGS="-shared"
38 cc -c $1.c -o $1.o $CFLAGS
39 ld $SO_FLAGS -o $1.so $1.o
42 cat > $testfilestem.c <<EOF
43 int summish(int x, int y) { return 1 + x + y; }
45 int numberish = 42;
47 int nummish(int x) { return numberish + x; }
49 short negative_short() { return -1; }
50 int negative_int() { return -2; }
51 long negative_long() { return -3; }
53 long long powish(unsigned int x, unsigned int y) {
54 long long acc = 1;
55 long long xx = (long long) x;
56 for(; y != 1; y /= 2) {
57 if (y & 1) {
58 acc *= xx;
59 y -= 1;
61 xx *= xx;
63 return xx*acc;
66 float return9th(float f1, float f2, float f3, float f4, float f5,
67 float f6, float f7, float f8, float f9, float f10,
68 float f11, float f12) {
69 return f9;
72 double return9thd(double f1, double f2, double f3, double f4, double f5,
73 double f6, double f7, double f8, double f9, double f10,
74 double f11, double f12) {
75 return f9;
78 int long_test8(int a1, int a2, int a3, int a4, int a5,
79 int a6, int a7, long long l1) {
80 return (l1 == powish(2,34));
83 int long_test9(int a1, int a2, int a3, int a4, int a5,
84 int a6, int a7, long long l1, int a8) {
85 return (l1 == powish(2,35));
88 int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
89 int i7, int i8, int i9, long long l1, long long l2) {
90 return (l1 == (1 + powish(2,37)));
93 long long return_long_long() {
94 return powish(2,33);
96 EOF
98 build_so $testfilestem
100 echo 'int foo = 13;' > $testfilestem-b.c
101 echo 'int bar() { return 42; }' >> $testfilestem-b.c
102 build_so $testfilestem-b
104 echo 'int foo = 42;' > $testfilestem-b2.c
105 echo 'int bar() { return 13; }' >> $testfilestem-b2.c
106 build_so $testfilestem-b2
108 echo 'int late_foo = 43;' > $testfilestem-c.c
109 echo 'int late_bar() { return 14; }' >> $testfilestem-c.c
110 build_so $testfilestem-c
112 ## Foreign definitions & load
114 cat > $testfilestem.base.lisp <<EOF
115 (define-alien-variable environ (* c-string))
116 (defvar *environ* environ)
117 (eval-when (:compile-toplevel :load-toplevel :execute)
118 (handler-case
119 (progn
120 (load-shared-object "$testfilestem.so")
121 (load-shared-object "$testfilestem-b.so"))
122 (sb-int:unsupported-operator ()
123 ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
124 ;; supported on every OS. In that case, there's nothing to test,
125 ;; and we can just fall through to success.
126 (sb-ext:quit :unix-status 22)))) ; catch that
127 (define-alien-routine summish int (x int) (y int))
128 (define-alien-variable numberish int)
129 (define-alien-routine nummish int (x int))
130 (define-alien-variable "foo" int)
131 (define-alien-routine "bar" int)
133 (define-alien-routine "negative_short" short)
134 (define-alien-routine "negative_int" int)
135 (define-alien-routine "negative_long" long)
137 (define-alien-routine return9th float (input1 float) (input2 float) (input3 float) (input4 float) (input5 float) (input6 float) (input7 float) (input8 float) (input9 float) (input10 float) (input11 float) (input12 float))
138 (define-alien-routine return9thd double (f1 double) (f2 double) (f3 double) (f4 double) (f5 double) (f6 double) (f7 double) (f8 double) (f9 double) (f10 double) (f11 double) (f12 double))
140 (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
141 (define-alien-routine long-test9 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)) (int8 int))
142 (define-alien-routine long-test2 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (int8 int) (int9 int) (long1 (integer 64)) (long2 (integer 64)))
143 (define-alien-routine return-long-long (integer 64))
145 ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
146 ;; linkage-table ports
147 (defvar *extern* (extern-alien "negative_short" short))
149 ;; Test that loading an object file didn't screw up our records
150 ;; of variables visible in runtime. (This was a bug until
151 ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
153 ;; This cannot be tested in a saved core, as there is no guarantee
154 ;; that the location will be the same.
155 (assert (= (sb-sys:sap-int (alien-sap *environ*))
156 (sb-sys:sap-int (alien-sap environ))))
158 ;; automagic restarts
159 (setf *invoke-debugger-hook*
160 (lambda (condition hook)
161 (princ condition)
162 (let ((cont (find-restart 'continue condition)))
163 (when cont
164 (invoke-restart cont)))
165 (print :fell-through)
166 (invoke-debugger condition)))
169 echo "(declaim (optimize speed))" > $testfilestem.fast.lisp
170 cat $testfilestem.base.lisp >> $testfilestem.fast.lisp
172 echo "(declaim (optimize space))" > $testfilestem.small.lisp
173 cat $testfilestem.base.lisp >> $testfilestem.small.lisp
175 # Test code
176 cat > $testfilestem.test.lisp <<EOF
177 ;; FIXME: currently the start/small case fails on x86/Darwin. Moving
178 ;; this NOTE definition to the base.lisp file fixes that, but obviously
179 ;; it is better fo figure out what is going on instead of doing that...
181 ;; Other trivialish changes that mask the error include:
182 ;; * loading the .lisp file instead of the .fasl at the save test
183 ;; * --eval 'nil' before loading the .fasl at the save test
185 ;; HATE.
186 (defun note (x)
187 (write-line x *standard-output*)
188 (force-output *standard-output*))
189 (note "/initial assertions")
190 (assert (= 31 (summish 10 20)))
191 (assert (= 42 numberish))
192 (setf numberish 13)
193 (assert (= 13 numberish))
194 (assert (= 14 (nummish 1)))
196 (assert (= -1 (negative-short)))
197 (assert (= -2 (negative-int)))
198 (assert (= -3 (negative-long)))
200 (assert (= 9.0s0 (return9th 1.0s0 2.0s0 3.0s0 4.0s0 5.0s0 6.0s0 7.0s0 8.0s0 9.0s0 10.0s0 11.0s0 12.0s0)))
201 (assert (= 9.0d0 (return9thd 1.0d0 2.0d0 3.0d0 4.0d0 5.0d0 6.0d0 7.0d0 8.0d0 9.0d0 10.0d0 11.0d0 12.0d0)))
203 (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
204 (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
205 (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
206 (assert (= (ash 1 33) (return-long-long)))
208 (note "/initial assertions ok")
210 ;; test reloading object file with new definitions
211 (assert (= 13 foo))
212 (assert (= 42 (bar)))
213 (note "/original definitions ok")
214 (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
215 (rename-file "$testfilestem-b2.so" "$testfilestem-b.so")
216 (load-shared-object "$testfilestem-b.so")
217 (note "/reloading ok")
218 (assert (= 42 foo))
219 (assert (= 13 (bar)))
220 (note "/redefined versions ok")
221 (rename-file "$testfilestem-b.so" "$testfilestem-b2.so")
222 (rename-file "$testfilestem-b.bak" "$testfilestem-b.so")
223 (note "/renamed back to originals")
225 ;; test late resolution
226 #+linkage-table
227 (progn
228 (note "/starting linkage table tests")
229 (define-alien-variable late-foo int)
230 (define-alien-routine late-bar int)
231 (multiple-value-bind (val err) (ignore-errors late-foo)
232 (assert (not val))
233 (assert (typep err 'undefined-alien-error)))
234 (multiple-value-bind (val err) (ignore-errors (late-bar))
235 (assert (not val))
236 (assert (typep err 'undefined-alien-error)))
237 (load-shared-object "$testfilestem-c.so")
238 (assert (= 43 late-foo))
239 (assert (= 14 (late-bar)))
240 (note "/linkage table ok"))
242 (sb-ext:quit :unix-status 52) ; success convention for Lisp program
245 test_compile() {
246 ${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.$1.lisp\")) (sb-ext:quit :unix-status 52))"
247 if [ $? = 52 ]; then
248 echo test compile $1 ok
249 else
250 # we can't compile the test file. something's wrong.
251 # rm $testfilestem.*
252 echo test compile $1 failed: $?
253 exit 1
257 test_compile fast
258 test_compile small
260 test_use() {
261 ${SBCL:-sbcl} --load $testfilestem.$1.fasl --load $testfilestem.test.lisp
262 RET=$?
263 if [ $RET = 22 ]; then
264 rm $testfilestem.*
265 exit $PUNT # success -- load-shared-object not supported
266 elif [ $RET != 52 ]; then
267 rm $testfilestem.*
268 echo test use $1 failed: $?
269 exit 1
270 else
271 echo test use $1 ok
275 test_use small
276 test_use fast
278 test_save() {
279 echo testing save $1
280 ${SBCL:-sbcl} --load $testfilestem.$1.fasl --eval "#+linkage-table (save-lisp-and-die \"$testfilestem.$1.core\") #-linkage-table nil" <<EOF
281 (sb-ext:quit :unix-status 22) ; catch this
283 if [ $? = 22 ]; then
284 rm $testfilestem.*
285 exit $PUNT # success -- linkage-table not available
286 else
287 echo save $1 ok
291 test_save small
292 test_save fast
294 test_start() {
295 echo testing start $1
296 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.$1.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
297 if [ $? != 52 ]; then
298 rm $testfilestem.*
299 echo test failed: $?
300 exit 1 # Failure
301 else
302 echo test start $1 ok
306 test_start fast
307 test_start small
309 # missing object file
310 rm $testfilestem-b.so $testfilestem-b2.so
311 ${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.fast.core --sysinit /dev/null --userinit /dev/null <<EOF
312 (assert (= 22 (summish 10 11)))
313 (multiple-value-bind (val err) (ignore-errors (eval 'foo))
314 (assert (not val))
315 (assert (typep err 'undefined-alien-error)))
316 (multiple-value-bind (val err) (ignore-errors (eval '(bar)))
317 (assert (not val))
318 (assert (typep err 'undefined-alien-error)))
319 (quit :unix-status 52)
321 if [ $? != 52 ]; then
322 rm $testfilestem.*
323 echo test failed: $?
324 exit 1 # Failure
327 echo missing .so ok
329 rm -f $testfilestem.* $testfilestem-*
331 # success convention for script
332 exit 104