enum.lisp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249
  1. ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
  2. ;;;
  3. ;;; enum.lisp --- Tests on C enums.
  4. ;;;
  5. ;;; Copyright (C) 2005-2006, Luis Oliveira <loliveira(@)common-lisp.net>
  6. ;;;
  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:
  14. ;;;
  15. ;;; The above copyright notice and this permission notice shall be
  16. ;;; included in all copies or substantial portions of the Software.
  17. ;;;
  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.
  26. ;;;
  27. (in-package #:cffi-tests)
  28. (defctype numeros-base-type :int)
  29. (defcenum (numeros numeros-base-type)
  30. (:one 1)
  31. :two
  32. :three
  33. :four
  34. (:forty-one 41)
  35. :forty-two)
  36. (defcfun "check_enums" :int
  37. (%one numeros)
  38. (%two numeros)
  39. (%three numeros)
  40. (%four numeros)
  41. (%forty-one numeros)
  42. (%forty-two numeros))
  43. (deftest enum.1
  44. (check-enums :one :two :three 4 :forty-one :forty-two)
  45. 1)
  46. (defcenum another-boolean :false :true)
  47. (defcfun "return_enum" another-boolean (x :uint))
  48. (deftest enum.2
  49. (and (eq :false (return-enum 0))
  50. (eq :true (return-enum 1)))
  51. t)
  52. (defctype yet-another-boolean another-boolean)
  53. (defcfun ("return_enum" return-enum2) yet-another-boolean
  54. (x yet-another-boolean))
  55. (deftest enum.3
  56. (and (eq :false (return-enum2 :false))
  57. (eq :true (return-enum2 :true)))
  58. t)
  59. (defctype numeros-typedef numeros)
  60. (deftest enum.typedef.1
  61. (eq (foreign-enum-keyword 'numeros-typedef 1)
  62. (foreign-enum-keyword 'numeros 1))
  63. t)
  64. (deftest enum.typedef.2
  65. (eql (foreign-enum-value 'numeros-typedef :four)
  66. (foreign-enum-value 'numeros :four))
  67. t)
  68. (defcenum enum-size.int
  69. (:one 1)
  70. (enum-size-int #.(1- (expt 2 (1- (* (foreign-type-size :unsigned-int) 8)))))
  71. (enum-size-negative-int #.(- (1- (expt 2 (1- (* (foreign-type-size :unsigned-int) 8))))))
  72. (:two 2))
  73. (defcenum enum-size.uint
  74. (:one 1)
  75. (enum-size-uint #.(1- (expt 2 (* (foreign-type-size :unsigned-int) 8))))
  76. (:two 2))
  77. (deftest enum.size
  78. (mapcar (alexandria:compose 'cffi::unparse-type
  79. 'cffi::actual-type
  80. 'cffi::parse-type)
  81. (list 'enum-size.int
  82. 'enum-size.uint))
  83. ;; The C standard only has weak constraints on the size of integer types, so
  84. ;; we cannot really test more than one type in a platform independent way due
  85. ;; to the possible overlaps.
  86. (:int
  87. :unsigned-int))
  88. (deftest enum.size.members
  89. (mapcar (alexandria:conjoin 'boundp 'constantp)
  90. '(enum-size-int enum-size-negative-int enum-size-uint))
  91. (t t t))
  92. (deftest enum.size.error-when-too-large
  93. (expecting-error
  94. (eval '(defcenum enum-size-too-large
  95. (:too-long #.(expt 2 129)))))
  96. :error)
  97. ;; There are some projects that use non-integer base type. It's not in
  98. ;; adherence with the C standard, but we also don't lose much by
  99. ;; allowing it.
  100. (defcenum (enum.double :double)
  101. (:one 1)
  102. (:two 2d0)
  103. (:three 3.42)
  104. :four)
  105. (deftest enum.double
  106. (values-list
  107. (mapcar (alexandria:curry 'foreign-enum-value 'enum.double)
  108. '(:one :two :three :four)))
  109. 1
  110. 2.0d0
  111. 3.42
  112. 4.42)
  113. ;; Test undeclared values
  114. (defcenum (enum.undeclared :int :allow-undeclared-values T)
  115. (:one 1)
  116. (:two 2))
  117. (deftest enum.undeclared
  118. (with-foreign-object (enum 'enum.undeclared 3)
  119. (setf (mem-aref enum 'enum.undeclared 0) 1)
  120. (setf (mem-aref enum 'enum.undeclared 1) 2)
  121. (setf (mem-aref enum 'enum.undeclared 2) 3)
  122. (values (mem-aref enum 'enum.undeclared 0)
  123. (mem-aref enum 'enum.undeclared 1)
  124. (mem-aref enum 'enum.undeclared 2)))
  125. :one
  126. :two
  127. 3)
  128. ;;;# Bitfield tests
  129. ;;; Regression test: defbitfield was misbehaving when the first value
  130. ;;; was provided.
  131. (deftest bitfield.1
  132. (eval '(defbitfield (bf1 :long)
  133. (:foo 0)))
  134. bf1)
  135. (defbitfield bf2
  136. one
  137. two
  138. four
  139. eight
  140. sixteen
  141. (bf2.outlier 42)
  142. thirty-two
  143. sixty-four)
  144. (deftest bitfield.2
  145. (mapcar (lambda (symbol)
  146. (foreign-bitfield-value 'bf2 (list symbol)))
  147. '(one two four eight sixteen thirty-two sixty-four))
  148. (1 2 4 8 16 32 64))
  149. (deftest bitfield.2.outlier
  150. (mapcar (lambda (symbol)
  151. (foreign-bitfield-value 'bf2 (list symbol)))
  152. '(one two four eight sixteen thirty-two sixty-four))
  153. (1 2 4 8 16 32 64))
  154. (defbitfield (bf3 :int)
  155. (three 3)
  156. one
  157. (seven 7)
  158. two
  159. (eight 8)
  160. sixteen)
  161. ;;; Non-single-bit numbers must not influence the progression of
  162. ;;; implicit values. Single bits larger than any before *must*
  163. ;;; influence said progression.
  164. (deftest bitfield.3
  165. (mapcar (lambda (symbol)
  166. (foreign-bitfield-value 'bf3 (list symbol)))
  167. '(one two sixteen))
  168. (1 2 16))
  169. (defbitfield bf4
  170. ;; zero will be a simple enum member because it's not a valid mask
  171. (zero 0)
  172. one
  173. two
  174. four
  175. (three 3)
  176. (sixteen 16))
  177. ;;; Yet another edge case with the 0...
  178. (deftest bitfield.4
  179. ;; These should macroexpand to the literals in Slime
  180. ;; due to the compiler macros. Same below.
  181. (values (foreign-bitfield-value 'bf4 ())
  182. (foreign-bitfield-value 'bf4 'one)
  183. (foreign-bitfield-value 'bf4 '(one two))
  184. (foreign-bitfield-value 'bf4 '(three)) ; or should it signal an error?
  185. (foreign-bitfield-value 'bf4 '(sixteen)))
  186. 0
  187. 1
  188. 3
  189. 3
  190. 16)
  191. (deftest bitfield.4b
  192. (values (foreign-bitfield-symbols 'bf4 0)
  193. (foreign-bitfield-symbols 'bf4 1)
  194. (foreign-bitfield-symbols 'bf4 3)
  195. (foreign-bitfield-symbols 'bf4 8)
  196. (foreign-bitfield-symbols 'bf4 16))
  197. nil
  198. (one)
  199. (one two)
  200. nil
  201. (sixteen))
  202. (deftest bitfield.translators
  203. (with-foreign-object (bf 'bf4 2)
  204. (setf (mem-aref bf 'bf4 0) 1)
  205. (setf (mem-aref bf 'bf4 1) 3)
  206. (values (mem-aref bf 'bf4 0)
  207. (mem-aref bf 'bf4 1)))
  208. (one)
  209. (one two))
  210. #+nil
  211. (deftest bitfield.base-type-error
  212. (expecting-error
  213. (eval '(defbitfield (bf1 :float)
  214. (:foo 0))))
  215. :error)