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