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