cffi-toolchain: don't reintroduce bugs to ECL's ASDF
[cffi.git] / tests / arrays.lisp
blob1e96514da973f9b148454574042d37756a08912a
1 ;;;; -*- Mode: lisp; indent-tabs-mode: nil -*-
2 ;;;
3 ;;; arrays.lisp --- Tests for foreign arrays.
4 ;;;
5 ;;; Copyright (C) 2005-2006, James Bielman <jamesjb@jamesjb.com>
6 ;;; Copyright (C) 2005-2007, Luis Oliveira <loliveira@common-lisp.net>
7 ;;;
8 ;;; Permission is hereby granted, free of charge, to any person
9 ;;; obtaining a copy of this software and associated documentation
10 ;;; files (the "Software"), to deal in the Software without
11 ;;; restriction, including without limitation the rights to use, copy,
12 ;;; modify, merge, publish, distribute, sublicense, and/or sell copies
13 ;;; of the Software, and to permit persons to whom the Software is
14 ;;; furnished to do so, subject to the following conditions:
15 ;;;
16 ;;; The above copyright notice and this permission notice shall be
17 ;;; included in all copies or substantial portions of the Software.
18 ;;;
19 ;;; THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 ;;; EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 ;;; MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 ;;; NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23 ;;; HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24 ;;; WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25 ;;; OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26 ;;; DEALINGS IN THE SOFTWARE.
27 ;;;
29 ;;;#Foreign Array Conversion Tests
30 ;;;
32 (in-package #:cffi-tests)
34 (deftest array.foreign-to-lisp.basic
35 (with-foreign-array (ptr #(1 2 3 4 5) '(:array :int32 5))
36 (foreign-array-to-lisp ptr '(:array :int32 5)))
37 #(1 2 3 4 5))
39 (deftest array.foreign-to-lisp.adjustable
40 (with-foreign-array (ptr #(1 2 3 4 5) '(:array :int32 5))
41 (let ((array (foreign-array-to-lisp ptr '(:array :int32 5)
42 :adjustable t)))
43 (adjustable-array-p array)))
46 (deftest array.foreign-to-lisp.displaced
47 (let ((array (make-array 10 :initial-contents '(1 2 3 4 5 6 7 8 9 0))))
48 (with-foreign-array (ptr #(10 20 30 40 50) '(:array :int32 5))
49 (let ((displaced (foreign-array-to-lisp ptr '(:array :int32 5)
50 :displaced-to array
51 :displaced-index-offset 5)))
52 array)))
53 #(1 2 3 4 5 10 20 30 40 50))
55 ;;; Implementation detail: 15.1.2.2 of the CL standard states that the only
56 ;;; truly portable array specializations are for bits (bit-vectors) and
57 ;;; characters (strings). Since char-codes are implementation-dependent, it
58 ;;; would be tricky to write a portable test for them without generating
59 ;;; characters at runtime. So, for a truly portable test, we are only left with
60 ;;; bits, which are luckily numeric, and equal to (UNSIGNED-BYTE 1).
61 ;;; This is why the below test is so terribly wasteful, spending a whole byte
62 ;;; for a single bit - CFFI has no capabilities for dealing with single bits,
63 ;;; and this test is only meant to check correctness of the :ELEMENT-TYPE
64 ;;; argument to MAKE-ARRAY. In actual use cases of specialized
65 ;;; FOREIGN-ARRAY-TO-LISP, capable implementations will be able to make
66 ;;; specialized arrays of types that are commonly optimized for and/or
67 ;;; representable in hardware, such as (UNSIGNED-BYTE 8) on x86 architectures.
68 (deftest array.foreign-to-lisp.specialized
69 (with-foreign-array (ptr #(1 0 1 0 1 1 1 0) '(:array :int8 8))
70 (foreign-array-to-lisp ptr '(:array :int8 8) :element-type 'bit))
71 #*10101110)