File: enum.lisp

package info (click to toggle)
acl2 8.6%2Bdfsg-2
  • links: PTS
  • area: main
  • in suites: trixie
  • size: 1,111,420 kB
  • sloc: lisp: 17,818,294; java: 125,359; python: 28,122; javascript: 23,458; cpp: 18,851; ansic: 11,569; perl: 7,678; xml: 5,591; sh: 3,976; makefile: 3,833; ruby: 2,633; yacc: 1,126; ml: 763; awk: 295; csh: 233; lex: 197; php: 178; tcl: 49; asm: 23; haskell: 17
file content (248 lines) | stat: -rw-r--r-- 6,793 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
;;;
;;; enum.lisp --- Tests on C enums.
;;;
;;; Copyright (C) 2005-2006, Luis Oliveira  <loliveira(@)common-lisp.net>
;;;
;;; Permission is hereby granted, free of charge, to any person
;;; obtaining a copy of this software and associated documentation
;;; files (the "Software"), to deal in the Software without
;;; restriction, including without limitation the rights to use, copy,
;;; modify, merge, publish, distribute, sublicense, and/or sell copies
;;; of the Software, and to permit persons to whom the Software is
;;; furnished to do so, subject to the following conditions:
;;;
;;; The above copyright notice and this permission notice shall be
;;; included in all copies or substantial portions of the Software.
;;;
;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
;;; NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
;;; DEALINGS IN THE SOFTWARE.
;;;

(in-package #:cffi-tests)

(defctype numeros-base-type :int)

(defcenum (numeros numeros-base-type)
  (:one 1)
  :two
  :three
  :four
  (:forty-one 41)
  :forty-two)

(defcfun "check_enums" :int
  (%one numeros)
  (%two numeros)
  (%three numeros)
  (%four numeros)
  (%forty-one numeros)
  (%forty-two numeros))

(deftest enum.1
    (check-enums :one :two :three 4 :forty-one :forty-two)
  1)

(defcenum another-boolean :false :true)
(defcfun "return_enum" another-boolean (x :uint))

(deftest enum.2
    (and (eq :false (return-enum 0))
         (eq :true (return-enum 1)))
  t)

(defctype yet-another-boolean another-boolean)
(defcfun ("return_enum" return-enum2) yet-another-boolean
  (x yet-another-boolean))

(deftest enum.3
    (and (eq :false (return-enum2 :false))
         (eq :true (return-enum2 :true)))
  t)

(defctype numeros-typedef numeros)

(deftest enum.typedef.1
    (eq (foreign-enum-keyword 'numeros-typedef 1)
        (foreign-enum-keyword 'numeros 1))
  t)

(deftest enum.typedef.2
    (eql (foreign-enum-value 'numeros-typedef :four)
         (foreign-enum-value 'numeros :four))
  t)

(defcenum enum-size.int
  (:one 1)
  (enum-size-int #.(1- (expt 2 (1- (* (foreign-type-size :unsigned-int) 8)))))
  (enum-size-negative-int #.(- (1- (expt 2 (1- (* (foreign-type-size :unsigned-int) 8))))))
  (:two 2))

(defcenum enum-size.uint
  (:one 1)
  (enum-size-uint #.(1- (expt 2 (* (foreign-type-size :unsigned-int) 8))))
  (:two 2))

(deftest enum.size
    (mapcar (alexandria:compose 'cffi::unparse-type
                                'cffi::actual-type
                                'cffi::parse-type)
            (list 'enum-size.int
                  'enum-size.uint))
  ;; The C standard only has weak constraints on the size of integer types, so
  ;; we cannot really test more than one type in a platform independent way due
  ;; to the possible overlaps.
  (:int
   :unsigned-int))

(deftest enum.size.members
    (mapcar (alexandria:conjoin 'boundp 'constantp)
            '(enum-size-int enum-size-negative-int enum-size-uint))
  (t t t))

(deftest enum.size.error-when-too-large
    (expecting-error
      (eval '(defcenum enum-size-too-large
              (:too-long #.(expt 2 129)))))
  :error)

;; There are some projects that use non-integer base type. It's not in
;; adherence with the C standard, but we also don't lose much by
;; allowing it.
(defcenum (enum.double :double)
  (:one 1)
  (:two 2d0)
  (:three 3.42)
  :four)

(deftest enum.double
    (values-list
     (mapcar (alexandria:curry 'foreign-enum-value 'enum.double)
             '(:one :two :three :four)))
  1
  2.0d0
  3.42
  4.42)

;; Test undeclared values
(defcenum (enum.undeclared :int :allow-undeclared-values T)
  (:one 1)
  (:two 2))

(deftest enum.undeclared
    (with-foreign-object (enum 'enum.undeclared 3)
      (setf (mem-aref enum 'enum.undeclared 0) 1)
      (setf (mem-aref enum 'enum.undeclared 1) 2)
      (setf (mem-aref enum 'enum.undeclared 2) 3)
      (values (mem-aref enum 'enum.undeclared 0)
              (mem-aref enum 'enum.undeclared 1)
              (mem-aref enum 'enum.undeclared 2)))
  :one
  :two
  3)

;;;# Bitfield tests

;;; Regression test: defbitfield was misbehaving when the first value
;;; was provided.
(deftest bitfield.1
    (eval '(defbitfield (bf1 :long)
             (:foo 0)))
  bf1)

(defbitfield bf2
  one
  two
  four
  eight
  sixteen
  (bf2.outlier 42)
  thirty-two
  sixty-four)

(deftest bitfield.2
    (mapcar (lambda (symbol)
              (foreign-bitfield-value 'bf2 (list symbol)))
            '(one two four eight sixteen thirty-two sixty-four))
  (1 2 4 8 16 32 64))

(deftest bitfield.2.outlier
    (mapcar (lambda (symbol)
              (foreign-bitfield-value 'bf2 (list symbol)))
            '(one two four eight sixteen thirty-two sixty-four))
  (1 2 4 8 16 32 64))

(defbitfield (bf3 :int)
  (three 3)
  one
  (seven 7)
  two
  (eight 8)
  sixteen)

;;; Non-single-bit numbers must not influence the progression of
;;; implicit values.  Single bits larger than any before *must*
;;; influence said progression.
(deftest bitfield.3
    (mapcar (lambda (symbol)
              (foreign-bitfield-value 'bf3 (list symbol)))
            '(one two sixteen))
  (1 2 16))

(defbitfield bf4
  ;; zero will be a simple enum member because it's not a valid mask
  (zero 0)
  one
  two
  four
  (three 3)
  (sixteen 16))

;;; Yet another edge case with the 0...
(deftest bitfield.4
    ;; These should macroexpand to the literals in Slime
    ;; due to the compiler macros. Same below.
    (values (foreign-bitfield-value 'bf4 ())
            (foreign-bitfield-value 'bf4 'one)
            (foreign-bitfield-value 'bf4 '(one two))
            (foreign-bitfield-value 'bf4 '(three)) ; or should it signal an error?
            (foreign-bitfield-value 'bf4 '(sixteen)))
  0
  1
  3
  3
  16)

(deftest bitfield.4b
    (values (foreign-bitfield-symbols 'bf4 0)
            (foreign-bitfield-symbols 'bf4 1)
            (foreign-bitfield-symbols 'bf4 3)
            (foreign-bitfield-symbols 'bf4 8)
            (foreign-bitfield-symbols 'bf4 16))
  nil
  (one)
  (one two)
  nil
  (sixteen))

(deftest bitfield.translators
    (with-foreign-object (bf 'bf4 2)
      (setf (mem-aref bf 'bf4 0) 1)
      (setf (mem-aref bf 'bf4 1) 3)
      (values (mem-aref bf 'bf4 0)
              (mem-aref bf 'bf4 1)))
  (one)
  (one two))

#+nil
(deftest bitfield.base-type-error
    (expecting-error
      (eval '(defbitfield (bf1 :float)
              (:foo 0))))
  :error)