-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathtest.el
127 lines (94 loc) · 4.11 KB
/
test.el
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
;;; test.el --- FFI tests for Emacs
;; This is is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this. If not, see <https://www.gnu.org/licenses/>.
(require 'ffi)
(define-ffi-library test.so "test")
(define-ffi-function test-function "test_function" :int nil test.so)
(define-ffi-function test-function-char "test_function" :char nil test.so)
(ert-deftest ffi-basic ()
(should (= (test-function) 27))
(should (= (test-function-char) 27)))
(define-ffi-function test-c-string "test_c_string" :pointer nil test.so)
(ert-deftest ffi-pointer-type ()
(should (eq (type-of (test-c-string)) 'user-ptr)))
(ert-deftest ffi-c-string ()
(should (equal (ffi-get-c-string (test-c-string)) "hello")))
(defun callback (arg)
(1+ arg))
(define-ffi-function test-call-callback "test_call_callback"
:int [:pointer] test.so)
(ert-deftest ffi-call-callback ()
(let* ((cif (ffi--prep-cif :int [:int]))
(pointer-to-callback (ffi-make-closure cif #'callback)))
(should (eq (test-call-callback pointer-to-callback) 23))))
(define-ffi-function test-add "test_add" :int [:int :int] test.so)
(ert-deftest ffi-add ()
(should (eq (test-add 23 -23) 0)))
(ert-deftest ffi-struct-layout ()
(let ((struct-type (ffi--define-struct :int)))
(should (eq (ffi--type-size struct-type)
(ffi--type-size :int)))
(should (eq (ffi--type-alignment struct-type)
(ffi--type-alignment :int)))))
(ert-deftest ffi-struct-layout-offsets ()
(let* ((types '(:pointer :int))
(struct-type (apply #'ffi--define-struct types)))
(should (equal (ffi--lay-out-struct types)
(list 0 (ffi--type-size :pointer))))))
(ert-deftest ffi-struct-layout-offsets-2 ()
(let* ((types '(:char :pointer))
(struct-type (apply #'ffi--define-struct types)))
(should (equal (ffi--lay-out-struct types)
(list 0 (ffi--type-alignment :pointer))))))
(define-ffi-struct test-struct
(stringval :type :pointer)
(intval :type :int))
(define-ffi-function test-get-struct "test_get_struct"
test-struct nil test.so)
(ert-deftest ffi-structure-return ()
(let ((struct-value (test-get-struct)))
(should (equal (ffi-get-c-string (test-struct-stringval struct-value))
"string"))
(should (eq (test-struct-intval struct-value) 23))))
(define-ffi-function test-get-struct-int "test_get_struct_int"
:int (test-struct) test.so)
(ert-deftest ffi-struct-modification ()
(let ((struct-value (test-get-struct)))
(should (eq (test-get-struct-int struct-value) 23))
(cl-incf (test-struct-intval struct-value))
(should (eq (test-get-struct-int struct-value) 24))))
(define-ffi-union test-union
(cval :type :uchar)
(ival :type :int))
(define-ffi-function test-get-union "test_get_union"
test-union nil test.so)
(ert-deftest ffi-union ()
(let ((object (test-get-union)))
(should (eq (test-union-ival object) -1))
(should (eq (test-union-cval object) 255))))
(ert-deftest ffi-null ()
(should (ffi-pointer-null-p (ffi-null-pointer))))
(define-ffi-function test-not "test_not" :bool (:bool) test.so)
(ert-deftest ffi-boolean ()
(should (eq (test-not nil) t))
(should (eq (test-not t) nil))
(should (eq (test-not 0) nil))
(should (eq (test-not "hi") nil)))
(defconst test-user-defined-char :char)
(ert-deftest ffi-array ()
(should (eq (define-ffi-array arr1 :char 1024) 'arr1))
(should (eq (define-ffi-array arr2 test-user-defined-char 1024) 'arr2)))
(ert-deftest ffi-with-ffi-multi-stat ()
(should (eq (with-ffi-temporary (a :int) 1 2 3) 3))
(should (eq (with-ffi-temporaries ((a :int) (b :int)) 1 2 3) 3))
(should (eq (with-ffi-string (a "test") 1 2 3) 3))
(should (eq (with-ffi-strings ((a "s1") (b "s2")) 1 2 3) 3)))
;;; test.el ends here