a45dda9f23be766e78e84feb9616ec8a28fd700c
[uffi.git] / tests / c-test-fns.cl
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          c-test-fns.cl
6 ;;;; Purpose:       UFFI Example file for zlib compression
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Mar 2002
9 ;;;;
10 ;;;; $Id: c-test-fns.cl,v 1.3 2002/03/31 23:05:07 kevin Exp $
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; UFFI users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package :cl-user)
20
21 (unless (uffi:load-foreign-library 
22          (make-pathname :name "c-test-fns" 
23                         :type #+(or linux unix)"so"
24                         #+(or win32 mswindows) "dll"
25                         #+freebsd "a"
26                         :defaults *load-truename*)
27          :supporting-libraries '("c")
28          :force-load t)
29   (warn "Unable to load c-test-fns library"))
30
31 (uffi:def-function ("cs_to_upper" cs-to-upper)
32   ((input (* :unsigned-char)))
33   :returning :void
34   )
35
36 (defun string-to-upper (str)
37   (uffi:with-foreign-string (str-foreign str)
38     (cs-to-upper str-foreign)
39     (uffi:convert-from-foreign-string str-foreign)))
40
41 (uffi:def-function ("cs_count_upper" cs-count-upper)
42   ((input :cstring))
43   :returning :int
44   )
45
46 (defun string-count-upper (str)
47   (uffi:with-cstring (str-cstring str)
48     (cs-count-upper str-cstring)))
49
50 (uffi:def-function ("half_double_vector" half-double-vector)
51     ((size :int)
52      (vec (* :double)))
53   :returning :void)
54
55 (uffi:def-constant +double-vec-length+ 10)
56 (defun test-half-double-vector ()
57   (let ((vec (uffi:allocate-foreign-object :double +double-vec-length+))
58         results)
59     (dotimes (i +double-vec-length+)
60       (setf (uffi:deref-array vec '(:array :double) i) 
61             (coerce i 'double-float)))
62     (half-double-vector +double-vec-length+ vec)
63     (dotimes (i +double-vec-length+)
64       (push (uffi:deref-array vec '(:array :double) i) results))
65     (uffi:free-foreign-object vec)
66     (nreverse results)))
67
68 (defun t2 ()
69   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
70     (dotimes (i +double-vec-length+)
71       (setf (aref vec i) (coerce i 'double-float)))
72     (half-double-vector +double-vec-length+ vec)
73     vec))
74
75 #+cmu
76 (defun t3 ()
77   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
78     (dotimes (i +double-vec-length+)
79       (setf (aref vec i) (coerce i 'double-float)))
80     (system:without-gcing
81      (half-double-vector +double-vec-length+ (system:vector-sap vec)))
82     vec))
83     
84 #+test-uffi
85 (format t "~&(string-to-upper \"this is a test\") => ~A" 
86         (string-to-upper "this is a test"))
87
88 #+test-uffi
89 (format t "~&(string-to-upper nil) => ~A" 
90         (string-to-upper nil))
91
92 #+test-uffi
93 (format t "~&(string-count-upper \"This is a Test\") => ~A" 
94         (string-count-upper "This is a Test"))
95
96 #+test-uffi
97 (format t "~&(string-count-upper nil) => ~A" 
98         (string-count-upper nil))
99
100 #+test-uffi
101 (format t "~&Half vector: ~S" (test-half-double-vector))
102