95b411e53a05aec48be467d9bc3b4dd56132d1ad
[uffi.git] / tests / uffi-c-test-lib.lisp
1 ;;;; -*- Mode: Lisp; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          uffi-c-test-lib.lisp
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-2003 by Kevin M. Rosenberg
13 ;;;;
14 ;;;; *************************************************************************
15
16 (in-package #:uffi-tests)
17
18
19 (uffi:def-function ("cs_to_upper" cs-to-upper)
20   ((input (* :unsigned-char)))
21   :returning :void
22   )
23
24 (defun string-to-upper (str)
25   (uffi:with-foreign-string (str-foreign str)
26     (cs-to-upper str-foreign)
27     (uffi:convert-from-foreign-string str-foreign)))
28
29 (uffi:def-function ("cs_count_upper" cs-count-upper)
30   ((input :cstring))
31   :returning :int)
32
33 (defun string-count-upper (str)
34   (uffi:with-cstring (str-cstring str)
35     (cs-count-upper str-cstring))) 
36
37 (uffi:def-function ("half_double_vector" half-double-vector)
38     ((size :int)
39      (vec (* :double)))
40   :returning :void)
41
42 (uffi:def-function ("return_long_negative_one" return-long-negative-one)
43     ()
44   :returning :long)
45
46 (uffi:def-function ("return_int_negative_one" return-int-negative-one)
47     ()
48   :returning :int)
49
50 (uffi:def-function ("return_short_negative_one" return-short-negative-one)
51     ()
52   :returning :short)
53
54 (uffi:def-constant +double-vec-length+ 10)
55 (defun test-half-double-vector ()
56   (let ((vec (uffi:allocate-foreign-object :double +double-vec-length+))
57         results)
58     (dotimes (i +double-vec-length+)
59       (setf (uffi:deref-array vec '(:array :double) i) 
60             (coerce i 'double-float)))
61     (half-double-vector +double-vec-length+ vec)
62     (dotimes (i +double-vec-length+)
63       (push (uffi:deref-array vec '(:array :double) i) results))
64     (uffi:free-foreign-object vec)
65     (nreverse results)))
66
67 (defun t2 ()
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     (half-double-vector +double-vec-length+ vec)
72     vec))
73
74 #+(or cmu scl)
75 (defun t3 ()
76   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
77     (dotimes (i +double-vec-length+)
78       (setf (aref vec i) (coerce i 'double-float)))
79     (system:without-gcing
80      (half-double-vector +double-vec-length+ (system:vector-sap vec)))
81     vec))
82     
83 (deftest c-test.1 (string-to-upper "this is a test") "THIS IS A TEST")
84 (deftest c-test.2 (string-to-upper nil) nil)
85 (deftest c-test.3 (string-count-upper "This is a Test") 2)
86 (deftest c-test.4 (string-count-upper nil) -1)
87 (deftest c-test.5 (test-half-double-vector)
88   (0.0d0 0.5d0 1.0d0 1.5d0 2.0d0 2.5d0 3.0d0 3.5d0 4.0d0 4.5d0))
89 (deftest c-test.6 (return-long-negative-one) -1)
90 (deftest c-test.7 (return-int-negative-one) -1)
91 (deftest c-test.8 (return-short-negative-one) -1)
92