1.0.3.42: two LOOP buglets
[sbcl.git] / tests / foreign.test.sh
index 7d207b4..045d36b 100644 (file)
@@ -9,7 +9,7 @@
 # While most of SBCL is derived from the CMU CL system, the test
 # files (like this one) were written from scratch after the fork
 # from CMU CL.
-# 
+#
 # This software is in the public domain and is provided with
 # absolutely no warranty. See the COPYING and CREDITS files for
 # more information.
@@ -23,17 +23,78 @@ PUNT=104
 
 testfilestem=${TMPDIR:-/tmp}/sbcl-foreign-test-$$
 
-## Make a little shared object files to test with.
+## Make some shared object files to test with.
 
 build_so() {
- echo building $1.so
-  cc -c $1.c -o $1.o
-  ld -shared -o $1.so $1.o
+  echo building $1.so
+  if [ "`uname -m`" = x86_64 ]; then
+    CFLAGS="$CFLAGS -fPIC"
+  fi
+  if [ "`uname`" = Darwin ]; then
+    SO_FLAGS="-bundle"
+  else
+    SO_FLAGS="-shared"
+  fi
+  cc -c $1.c -o $1.o $CFLAGS
+  ld $SO_FLAGS -o $1.so $1.o  
+}
+
+cat > $testfilestem.c <<EOF
+int summish(int x, int y) { return 1 + x + y; }
+
+int numberish = 42;
+
+int nummish(int x) { return numberish + x; }
+
+short negative_short() { return -1; }
+int negative_int()     { return -2; }
+long negative_long()   { return -3; }
+
+long long powish(unsigned int x, unsigned int y) {
+  long long acc = 1;
+  long long xx = (long long) x;
+  for(; y != 1; y /= 2) {
+    if (y & 1) {
+      acc *= xx;
+      y -= 1;
+    }
+    xx *= xx;
+  }
+  return xx*acc;
+}
+
+float return9th(float f1, float f2, float f3, float f4, float f5, 
+               float f6, float f7, float f8, float f9, float f10, 
+               float f11, float f12) { 
+    return f9; 
+}
+
+double 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) { 
+    return f9; 
+}
+
+int long_test8(int a1, int a2, int a3, int a4, int a5, 
+              int a6, int a7, long long l1) { 
+    return (l1 == powish(2,34));
+}
+
+int long_test9(int a1, int a2, int a3, int a4, int a5, 
+              int a6, int a7, long long l1, int a8) { 
+    return (l1 == powish(2,35));
+}
+
+int long_test2(int i1, int i2, int i3, int i4, int i5, int i6,
+              int i7, int i8, int i9, long long l1, long long l2) {
+    return (l1 == (1 + powish(2,37)));
 }
-    
-echo 'int summish(int x, int y) { return 1 + x + y; }' > $testfilestem.c
-echo 'int numberish = 42;' >> $testfilestem.c
-echo 'int nummish(int x) { return numberish + x; }' >> $testfilestem.c
+
+long long return_long_long() {
+    return powish(2,33);
+}
+EOF
+
 build_so $testfilestem
 
 echo 'int foo = 13;' > $testfilestem-b.c
@@ -69,8 +130,24 @@ cat > $testfilestem.def.lisp <<EOF
   (define-alien-variable "foo" int)
   (define-alien-routine "bar" int)
 
+  (define-alien-routine "negative_short" short)
+  (define-alien-routine "negative_int" int)
+  (define-alien-routine "negative_long" long)
+
+  (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))
+  (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))
+
+  (define-alien-routine long-test8 int (int1 int) (int2 int) (int3 int) (int4 int) (int5 int) (int6 int) (int7 int) (long1 (integer 64)))
+  (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))
+  (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)))
+  (define-alien-routine return-long-long (integer 64))
+
+  ;; compiling this gets us the FOP-FOREIGN-DATAREF-FIXUP on
+  ;; linkage-table ports
+  (defvar *extern* (extern-alien "negative_short" short))
+
   ;; Test that loading an object file didn't screw up our records
-  ;; of variables visible in runtime. (This was a bug until 
+  ;; of variables visible in runtime. (This was a bug until
   ;; Nikodemus Siivola's patch in sbcl-0.8.5.50.)
   ;;
   ;; This cannot be tested in a saved core, as there is no guarantee
@@ -78,12 +155,13 @@ cat > $testfilestem.def.lisp <<EOF
   (assert (= (sb-sys:sap-int (alien-sap *environ*))
              (sb-sys:sap-int (alien-sap environ))))
 
+  (enable-debugger)
   ;; automagic restarts
-  (setf *debugger-hook* 
+  (setf *debugger-hook*
         (lambda (condition hook)
           (print (list :debugger-hook condition))
           (let ((cont (find-restart 'continue condition)))
-            (when cont 
+            (when cont
               (invoke-restart cont)))
           (print :fell-through)
           (invoke-debugger condition)))
@@ -97,9 +175,21 @@ cat > $testfilestem.test.lisp <<EOF
   (assert (= 13 numberish))
   (assert (= 14 (nummish 1)))
 
+  (assert (= -1 (negative-short)))
+  (assert (= -2 (negative-int)))
+  (assert (= -3 (negative-long)))
+
+  (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)))
+  (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)))
+
+  (assert (= 1 (long-test8 1 2 3 4 5 6 7 (ash 1 34))))
+  (assert (= 1 (long-test9 1 2 3 4 5 6 7 (ash 1 35) 8)))
+  (assert (= 1 (long-test2 1 2 3 4 5 6 7 8 9 (+ 1 (ash 1 37)) 15)))
+  (assert (= (ash 1 33) (return-long-long)))
+
   (print :stage-1)
 
-  ;; test realoading object file with new definitions
+  ;; test reloading object file with new definitions
   (assert (= 13 foo))
   (assert (= 42 (bar)))
   (rename-file "$testfilestem-b.so" "$testfilestem-b.bak")
@@ -113,28 +203,31 @@ cat > $testfilestem.test.lisp <<EOF
   (print :stage-2)
 
   ;; test late resolution
-  (define-alien-variable late-foo int)
-  (define-alien-routine late-bar int)
-  (multiple-value-bind (val err) (ignore-errors late-foo)
-    (assert (not val))
-    (assert (typep err 'undefined-alien-error)))
-  (multiple-value-bind (val err) (ignore-errors (late-bar))
-    (assert (not val))
-    (assert (typep err 'undefined-alien-error)))
-  (load-shared-object "$testfilestem-c.so")
-  (assert (= 43 late-foo))
-  (assert (= 14 (late-bar)))
+  #+linkage-table
+  (progn
+    (define-alien-variable late-foo int)
+    (define-alien-routine late-bar int)
+    (multiple-value-bind (val err) (ignore-errors late-foo)
+      (assert (not val))
+      (assert (typep err 'undefined-alien-error)))
+    (multiple-value-bind (val err) (ignore-errors (late-bar))
+      (assert (not val))
+      (assert (typep err 'undefined-alien-error)))
+    (load-shared-object "$testfilestem-c.so")
+    (assert (= 43 late-foo))
+    (assert (= 14 (late-bar))))
 
   (print :stage-3)
 
   (sb-ext:quit :unix-status 52) ; success convention for Lisp program
 EOF
 
-${SBCL:-sbcl} --eval "(progn (compile-file #p\"$testfilestem.def.lisp\") (sb-ext:quit :unix-status 52))"
-if [ $? = 52 ] ; then :
+${SBCL:-sbcl} --eval "(progn (load (compile-file #p\"$testfilestem.def.lisp\")) (sb-ext:quit :unix-status 52))"
+if [ $? = 52 ]; then
+    true # nop
 else
     # we can't compile the test file. something's wrong.
-    rm $testfilestem.*
+    # rm $testfilestem.*
     echo test failed: $?
     exit 1
 fi
@@ -149,7 +242,7 @@ if [ $RET = 22 ]; then
 elif [ $RET != 52 ]; then
     rm $testfilestem.*
     echo test failed: $?
-    exit 1 
+    exit 1
 fi
 
 echo load ok
@@ -164,7 +257,7 @@ fi
 
 echo table ok
 
-$SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
+${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null --load $testfilestem.test.lisp
 if [ $? != 52 ]; then
     rm $testfilestem.*
     echo test failed: $?
@@ -175,7 +268,7 @@ echo start ok
 
 # missing object file
 rm $testfilestem-b.so $testfilestem-b2.so
-$SBCL_ALLOWING_CORE --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
+${SBCL_ALLOWING_CORE:-sbcl} --core $testfilestem.core --sysinit /dev/null --userinit /dev/null <<EOF
   (assert (= 22 (summish 10 11)))
   (multiple-value-bind (val err) (ignore-errors (eval 'foo))
     (assert (not val))
@@ -193,7 +286,7 @@ fi
 
 echo missing ok
 
-rm $testfilestem.*
+rm -f $testfilestem.* $testfilestem-*
 
 # success convention for script
-exit 104 
+exit 104