0.8.14.5: Join the foreign legion!
[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.deflisp <<EOF
35   (define-alien-variable environ (* c-string))
36   (defvar *environ* environ)
37   (handler-case 
38       (load-shared-object "$testfilestem.so")
39     (sb-int:unsupported-operator ()
40      ;; At least as of sbcl-0.7.0.5, LOAD-SHARED-OBJECT isn't
41      ;; supported on every OS. In that case, there's nothing to test,
42      ;; and we can just fall through to success.
43      (sb-ext:quit :unix-status 22))) ; catch that
44   (define-alien-routine summish int (x int) (y int))
45   (define-alien-variable numberish int)
46   (define-alien-routine nummish int (x int))
47
48   ;; Test that loading an object file didn't screw up our records
49   ;; of variables visible in runtime. (This was a bug until 
50   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
51   ;;
52   ;; This cannot be tested in a saved core, as there is no guarantee
53   ;; that the location will be the same.
54   (assert (= (sb-sys:sap-int (alien-sap *environ*))
55              (sb-sys:sap-int (alien-sap environ))))
56 EOF
57
58 # Test code
59 cat > $testfilestem.testlisp <<EOF
60   (assert (= (summish 10 20) 31))
61   (assert (= 42 numberish))
62   (setf numberish 13)
63   (assert (= 13 numberish))
64   (assert (= 14 (nummish 1)))
65   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
66 EOF
67
68 ${SBCL:-sbcl} --load $testfilestem.deflisp --load $testfilestem.testlisp
69 if [ $? = 22 ]; then
70     rm $testfilestem.*
71     exit $PUNT # success -- load-shared-object not supported
72 elif [ $? != 52]; then
73     rm $testfilestem.*
74     echo test failed: $?
75     exit 1 
76 fi
77
78 ${SBCL:-sbcl} --load $testfilestem.deflisp --eval "(when (member :linkage-table *features*) (save-lisp-and-die \"$testfilestem.core\"))" <<EOF
79   (sb-ext:quit :unix-status 22) ; catch this
80 EOF
81 if [ $? = 22 ]; then
82     rm $testfilestem.*
83     exit $PUNT # success -- linkage-table not available
84 fi
85
86 $SBCL_ALLOWING_CORE --core $testfilestem.core --load $testfilestem.testlisp
87 if [ $? != 52 ]; then
88     rm $testfilestem.*
89     echo test failed: $?
90     exit 1 # Failure
91 fi
92
93 rm $testfilestem.*
94
95 # success convention for script
96 exit 104