add a test for ffi and pointers
authorAndy Wingo <wingo@pobox.com>
Wed, 27 Jan 2010 21:25:29 +0000 (22:25 +0100)
committerAndy Wingo <wingo@pobox.com>
Wed, 27 Jan 2010 21:25:29 +0000 (22:25 +0100)
* test-suite/standalone/test-ffi:
* test-suite/standalone/test-ffi-lib.c: Add a pointer test.

test-suite/standalone/test-ffi
test-suite/standalone/test-ffi-lib.c

index 3e3471a..5487625 100755 (executable)
@@ -2,7 +2,8 @@
 exec guile -q -s "$0" "$@"
 !#
 
-(use-modules (system foreign))
+(use-modules (system foreign)
+             (rnrs bytevector))
 
 (define lib
   (dynamic-link (string-append (getenv "builddir") "/libtest-ffi")))
@@ -152,6 +153,20 @@ exec guile -q -s "$0" "$@"
 (test (f-sum-struct (make-c-struct (list int8 int16 int32 int64)
                                    (list -1 2000 -30000 40000000000)))
       (+ -1 2000 -30000 40000000000))
+;;
+;; Structs
+;;
+(define f-memcpy
+  (make-foreign-function '* (dynamic-func "test_ffi_memcpy" lib)
+                         (list '* '* int32)))
+(let* ((src (bytevector->foreign (u8-list->bytevector '(0 1 2 3 4 5 6 7))))
+       (dest (bytevector->foreign (make-bytevector 16 0)))
+       (res (f-memcpy dest src (bytevector-length (foreign->bytevector src)))))
+  (or (= (foreign-ref dest) (foreign-ref res))
+      (error "memcpy res not equal to dest"))
+  (or (equal? (bytevector->u8-list (foreign->bytevector dest))
+              '(0 1 2 3 4 5 6 7 0 0 0 0 0 0 0 0))
+      (error "unexpected dest")))
 
 
 ;; Local Variables:
index d999101..8dec3d3 100644 (file)
@@ -206,3 +206,10 @@ scm_t_int64 test_ffi_sum_struct (struct foo foo)
 {
   return foo.d + foo.c + foo.b + foo.a;
 }
+
+
+void* test_ffi_memcpy (void *dest, void *src, scm_t_int32 n);
+void* test_ffi_memcpy (void *dest, void *src, scm_t_int32 n)
+{
+  return memcpy (dest, src, n);
+}