1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
3 ;;; enum.lisp --- Tests on C enums.
5 ;;; Copyright (C) 2005-2006, Luis Oliveira <loliveira(@)common-lisp.net>
7 ;;; Permission is hereby granted, free of charge, to any person
8 ;;; obtaining a copy of this software and associated documentation
9 ;;; files (the "Software"), to deal in the Software without
10 ;;; restriction, including without limitation the rights to use, copy,
11 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
12 ;;; of the Software, and to permit persons to whom the Software is
13 ;;; furnished to do so, subject to the following conditions:
15 ;;; The above copyright notice and this permission notice shall be
16 ;;; included in all copies or substantial portions of the Software.
18 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
25 ;;; DEALINGS IN THE SOFTWARE.
28 (in-package #:cffi-tests
)
30 (defctype numeros-base-type
:int
)
32 (defcenum (numeros numeros-base-type
)
40 (defcfun "check_enums" :int
49 (check-enums :one
:two
:three
4 :forty-one
:forty-two
)
52 (defcenum another-boolean
:false
:true
)
53 (defcfun "return_enum" another-boolean
(x :int
))
56 (and (eq :false
(return-enum 0))
57 (eq :true
(return-enum 1)))
60 (defctype yet-another-boolean another-boolean
)
61 (defcfun ("return_enum" return-enum2
) yet-another-boolean
62 (x yet-another-boolean
))
65 (and (eq :false
(return-enum2 :false
))
66 (eq :true
(return-enum2 :true
)))
69 (defctype numeros-typedef numeros
)
71 (deftest enum.typedef
.1
72 (eq (foreign-enum-keyword 'numeros-typedef
1)
73 (foreign-enum-keyword 'numeros
1))
76 (deftest enum.typedef
.2
77 (eql (foreign-enum-value 'numeros-typedef
:four
)
78 (foreign-enum-value 'numeros
:four
))
81 (defcenum enum-size.int
83 (enum-size-int #.
(1- (expt 2 (1- (* (foreign-type-size :unsigned-int
) 8)))))
84 (enum-size-negative-int #.
(- (1- (expt 2 (1- (* (foreign-type-size :unsigned-int
) 8))))))
87 (defcenum enum-size.uint
89 (enum-size-uint #.
(1- (expt 2 (* (foreign-type-size :unsigned-int
) 8))))
93 (mapcar (alexandria:compose
'cffi
::unparse-type
98 ;; The C standard only has weak constraints on the size of integer types, so
99 ;; we cannot really test more than one type in a platform independent way due
100 ;; to the possible overlaps.
104 (deftest enum.size.members
105 (mapcar (alexandria:conjoin
'boundp
'constantp
)
106 '(enum-size-int enum-size-negative-int enum-size-uint
))
109 (deftest enum.size.error-when-too-large
111 (eval '(defcenum enum-size-too-large
112 (:too-long
#.
(expt 2 129)))))
115 ;; There are some projects that use non-integer base type. It's not in
116 ;; adherence with the C standard, but we also don't lose much by
118 (defcenum (enum.double
:double
)
126 (mapcar (alexandria:curry
'foreign-enum-value
'enum.double
)
127 '(:one
:two
:three
:four
)))
135 ;;; Regression test: defbitfield was misbehaving when the first value
138 (eval '(defbitfield (bf1 :long
)
153 (mapcar (lambda (symbol)
154 (foreign-bitfield-value 'bf2
(list symbol
)))
155 '(one two four eight sixteen thirty-two sixty-four
))
158 (deftest bitfield
.2.outlier
159 (mapcar (lambda (symbol)
160 (foreign-bitfield-value 'bf2
(list symbol
)))
161 '(one two four eight sixteen thirty-two sixty-four
))
164 (defbitfield (bf3 :int
)
172 ;;; Non-single-bit numbers must not influence the progression of
173 ;;; implicit values. Single bits larger than any before *must*
174 ;;; influence said progression.
176 (mapcar (lambda (symbol)
177 (foreign-bitfield-value 'bf3
(list symbol
)))
182 ;; zero will be a simple enum member because it's not a valid mask
190 ;;; Yet another edge case with the 0...
192 ;; These should macroexpand to the literals in Slime
193 ;; due to the compiler macros. Same below.
194 (values (foreign-bitfield-value 'bf4
())
195 (foreign-bitfield-value 'bf4
'one
)
196 (foreign-bitfield-value 'bf4
'(one two
))
197 (foreign-bitfield-value 'bf4
'(three)) ; or should it signal an error?
198 (foreign-bitfield-value 'bf4
'(sixteen)))
206 (values (foreign-bitfield-symbols 'bf4
0)
207 (foreign-bitfield-symbols 'bf4
1)
208 (foreign-bitfield-symbols 'bf4
3)
209 (foreign-bitfield-symbols 'bf4
8)
210 (foreign-bitfield-symbols 'bf4
16))
217 (deftest bitfield.translators
218 (with-foreign-object (bf 'bf4
2)
219 (setf (mem-aref bf
'bf4
0) 1)
220 (setf (mem-aref bf
'bf4
1) 3)
221 (values (mem-aref bf
'bf4
0)
222 (mem-aref bf
'bf4
1)))
227 (deftest bitfield.base-type-error
229 (eval '(defbitfield (bf1 :float
)