0.8.16.35:
[sbcl.git] / tests / foreign.test.sh
1 #!/bin/sh
2
3 # tests related to foreign function interface and loading of shared
4 # libraries
5
6 # This software is part of the SBCL system. See the README file for
7 # more information.
8 #
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.
12
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.
16
17 echo //entering foreign.test.sh
18
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
23
24 testfilestem=${TMPDIR:-/tmp}/sbcl-foreign-test-$$
25
26 # Make a little shared object file to test with.
27 echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
28 echo 'int numberish = 42;' >> $testfilestem.c
29 echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
30 cc -c $testfilestem.c -o $testfilestem.o
31 ld -shared -o $testfilestem.so $testfilestem.o
32
33 # Foreign definitions & load
34 cat > $testfilestem.def.lisp <<EOF
35   (define-alien-variable environ (* c-string))
36   (defvar *environ* environ)
37   (eval-when (:compile-toplevel :load-toplevel :execute)
38     (handler-case 
39         (load-shared-object "$testfilestem.so")
40       (sb-int:unsupported-operator ()
41         ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
42         ;; supported on every OS. In that case, there's nothing to test,
43         ;; and we can just fall through to success.
44         (sb-ext:quit :unix-status 22)))) ; catch that
45   (define-alien-routine summish int (x int) (y int))
46   (define-alien-variable numberish int)
47   (define-alien-routine nummish int (x int))
48
49   ;; Test that loading an object file didn't screw up our records
50   ;; of variables visible in runtime. (This was a bug until 
51   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
52   ;;
53   ;; This cannot be tested in a saved core, as there is no guarantee
54   ;; that the location will be the same.
55   (assert (= (sb-sys:sap-int (alien-sap *environ*))
56              (sb-sys:sap-int (alien-sap environ))))
57 EOF
58
59 # Test code
60 cat > $testfilestem.test.lisp <<EOF
61   (assert (= (summish 10 20) 31))
62   (assert (= 42 numberish))
63   (setf numberish 13)
64   (assert (= 13 numberish))
65   (assert (= 14 (nummish 1)))
66   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
67 EOF
68
69 ${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
70 if [ $? = 52 ] ; then :
71 else
72     # we can't compile the test file. something's wrong.
73     rm $testfilestem.*
74     echo test failed: $?
75     exit 1
76 fi
77
78 ${SBCL:-sbcl} --load $testfilestem.def.fasl --load $testfilestem.test.lisp
79 RET=$?
80 if [ $RET = 22 ]; then
81     rm $testfilestem.*
82     exit $PUNT # success -- load-shared-object not supported
83 elif [ $RET != 52 ]; then
84     rm $testfilestem.*
85     echo test failed: $?
86     exit 1 
87 fi
88
89 ${SBCL:-sbcl} --load $testfilestem.def.fasl --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
90   (sb-ext:quit :unix-status 22) ; catch this
91 EOF
92 if [ $? = 22 ]; then
93     rm $testfilestem.*
94     exit $PUNT # success -- linkage-table not available
95 fi
96
97 $SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
98 if [ $? != 52 ]; then
99     rm $testfilestem.*
100     echo test failed: $?
101     exit 1 # Failure
102 fi
103
104 rm $testfilestem.*
105
106 # success convention for script
107 exit 104