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