4e87608f0bb39d675bd5c60a209e3694a667b1bc
[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   :module "uffi-c-test"
23   )
24
25 (defun string-to-upper (str)
26   (uffi:with-foreign-string (str-foreign str)
27     (cs-to-upper str-foreign)
28     (uffi:convert-from-foreign-string str-foreign)))
29
30 (uffi:def-function ("cs_count_upper" cs-count-upper)
31   ((input :cstring))
32   :returning :int
33   :module "uffi-c-test")
34
35 (defun string-count-upper (str)
36   (uffi:with-cstring (str-cstring str)
37     (cs-count-upper str-cstring))) 
38
39 (uffi:def-function ("half_double_vector" half-double-vector)
40     ((size :int)
41      (vec (* :double)))
42   :returning :void
43   :module "uffi-c-test")
44
45 (uffi:def-function ("return_long_negative_one" return-long-negative-one)
46     ()
47   :returning :long
48   :module "uffi-c-test")
49
50 (uffi:def-function ("return_int_negative_one" return-int-negative-one)
51     ()
52   :returning :int
53   :module "uffi-c-test")
54
55 (uffi:def-function ("return_short_negative_one" return-short-negative-one)
56     ()
57   :returning :short
58   :module "uffi-c-test")
59
60
61 (uffi:def-constant +double-vec-length+ 10)
62 (defun test-half-double-vector ()
63   (let ((vec (uffi:allocate-foreign-object :double +double-vec-length+))
64         results)
65     (dotimes (i +double-vec-length+)
66       (setf (uffi:deref-array vec '(:array :double) i) 
67             (coerce i 'double-float)))
68     (half-double-vector +double-vec-length+ vec)
69     (dotimes (i +double-vec-length+)
70       (push (uffi:deref-array vec '(:array :double) i) results))
71     (uffi:free-foreign-object vec)
72     (nreverse results)))
73
74 (defun t2 ()
75   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
76     (dotimes (i +double-vec-length+)
77       (setf (aref vec i) (coerce i 'double-float)))
78     (half-double-vector +double-vec-length+ vec)
79     vec))
80
81 #+(or cmu scl)
82 (defun t3 ()
83   (let ((vec (make-array +double-vec-length+ :element-type 'double-float)))
84     (dotimes (i +double-vec-length+)
85       (setf (aref vec i) (coerce i 'double-float)))
86     (system:without-gcing
87      (half-double-vector +double-vec-length+ (system:vector-sap vec)))
88     vec))
89     
90 (deftest c-test.1 (string-to-upper "this is a test") "THIS IS A TEST")
91 (deftest c-test.2 (string-to-upper nil) nil)
92 (deftest c-test.3 (string-count-upper "This is a Test") 2)
93 (deftest c-test.4 (string-count-upper nil) -1)
94 (deftest c-test.5 (test-half-double-vector)
95   (0.0d0 0.5d0 1.0d0 1.5d0 2.0d0 2.5d0 3.0d0 3.5d0 4.0d0 4.5d0))
96 (deftest c-test.6 (return-long-negative-one) -1)
97 (deftest c-test.7 (return-int-negative-one) -1)
98 (deftest c-test.8 (return-short-negative-one) -1)
99