r10608: update license
[uffi.git] / examples / c-test-fns.lisp
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$
11 ;;;;
12 ;;;; This file, part of UFFI, is Copyright (c) 2002-2005 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package :cl-user)
17
18 (unless (uffi:load-foreign-library 
19          (uffi:find-foreign-library "c-test-fns" 
20                                     (list *load-truename* "/home/kevin/debian/src/uffi/examples/"))
21          :supporting-libraries '("c"))
22   (warn "Unable to load c-test-fns library"))
23
24 (uffi:def-function ("cs_to_upper" cs-to-upper)
25   ((input (* :unsigned-char)))
26   :returning :void
27   )
28
29 (defun string-to-upper (str)
30   (uffi:with-foreign-string (str-foreign str)
31     (cs-to-upper str-foreign)
32     (uffi:convert-from-foreign-string str-foreign)))
33
34 (uffi:def-function ("cs_count_upper" cs-count-upper)
35   ((input :cstring))
36   :returning :int
37   )
38
39 (defun string-count-upper (str)
40   (uffi:with-cstring (str-cstring str)
41     (cs-count-upper str-cstring)))
42
43 (uffi:def-function ("half_double_vector" half-double-vector)
44     ((size :int)
45      (vec (* :double)))
46   :returning :void)
47
48 (uffi:def-constant +double-vec-length+ 10)
49 (defun test-half-double-vector ()
50   (let ((vec (uffi:allocate-foreign-object :double +double-vec-length+))
51         results)
52     (dotimes (i +double-vec-length+)
53       (setf (uffi:deref-array vec '(:array :double) i) 
54             (coerce i 'double-float)))
55     (half-double-vector +double-vec-length+ vec)
56     (dotimes (i +double-vec-length+)
57       (push (uffi:deref-array vec '(:array :double) i) results))
58     (uffi:free-foreign-object vec)
59     (nreverse results)))
60
61 (defun t2 ()
62   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
63     (dotimes (i +double-vec-length+)
64       (setf (aref vec i) (coerce i 'double-float)))
65     (half-double-vector +double-vec-length+ vec)
66     vec))
67
68 #+(or cmu scl)
69 (defun t3 ()
70   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
71     (dotimes (i +double-vec-length+)
72       (setf (aref vec i) (coerce i 'double-float)))
73     (system:without-gcing
74      (half-double-vector +double-vec-length+ (system:vector-sap vec)))
75     vec))
76     
77 #+examples-uffi
78 (format t "~&(string-to-upper \"this is a test\") => ~A" 
79         (string-to-upper "this is a test"))
80
81 #+examples-uffi
82 (format t "~&(string-to-upper nil) => ~A" 
83         (string-to-upper nil))
84
85 #+examples-uffi
86 (format t "~&(string-count-upper \"This is a Test\") => ~A" 
87         (string-count-upper "This is a Test"))
88
89 #+examples-uffi
90 (format t "~&(string-count-upper nil) => ~A" 
91         (string-count-upper nil))
92
93 #+examples-uffi
94 (format t "~&Half vector: ~S" (test-half-double-vector))
95
96
97
98 #+test-uffi
99 (progn
100   (util.test:test (string= (string-to-upper "this is a test") "THIS IS A TEST")
101                   t
102                   :test #'eql
103                   :fail-info "Error with string-to-upper")
104   (util.test:test (string-to-upper nil) nil
105                   :fail-info "string-to-upper with nil failed")
106   (util.test:test (string-count-upper "This is a Test")
107                   2
108                   :test #'eql
109                   :fail-info "Error with string-count-upper")
110   (util.test:test (string-count-upper nil) -1
111                   :test #'eql
112                   :fail-info "string-count-upper with nil failed")
113
114   (util.test:test (test-half-double-vector)
115                   '(0.0d0 0.5d0 1.0d0 1.5d0 2.0d0 2.5d0 3.0d0 3.5d0 4.0d0 4.5d0)
116                   :test #'equal
117                   :fail-info "Error comparing half-double-vector")
118   )