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