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