File: ctypestest.scm

package info (click to toggle)
gauche-c-wrapper 0.6.1-18
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 7,440 kB
  • sloc: ansic: 17,899; sh: 14,025; asm: 6,456; lisp: 5,485; yacc: 2,109; makefile: 520; exp: 194; cpp: 157; objc: 144; perl: 2
file content (293 lines) | stat: -rw-r--r-- 12,797 bytes parent folder | download | duplicates (7)
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;;;
;;; Test ctypes
;;;

(use gauche.test)

(test-start "c-types")
(use c-wrapper)
(use gauche.sequence)
(use gauche.config)
(test-module 'c-wrapper)

(define DYLIB (string-append "./ffitest." (gauche-config "--dylib-suffix")))
(c-load-library DYLIB)

(define (make-c-struct tagname decl-alist)
  (eval `(define-c-struct ,tagname) (current-module))
  (init-c-struct! (c-struct tagname) decl-alist)
  (c-struct tagname))

(define (make-c-union tagname decl-alist)
  (eval `(define-c-union ,tagname) (current-module))
  (init-c-union! (c-union tagname) decl-alist)
  (c-union tagname))

(define-syntax test-cfunc
  (syntax-rules ()
    ((_ expected func-name c-type v1 v2)
     (test func-name
           expected
           (lambda ()
             (let ((func (make-c-func (string->symbol func-name)
                                      c-type
                                      (list c-type c-type))))
               (func v1 v2)))))))

(test-cfunc 3 "add_uchar" <c-uchar> 2 1)
(test-cfunc 3 "add_ushort" <c-ushort> 2 1)
(test-cfunc 3 "add_uint" <c-uint> 2 1)
(test-cfunc 3 "add_ulong" <c-ulong> 2 1)
(test-cfunc 3 "add_ulonglong" <c-ulonglong> 2 1)
(test-cfunc -1 "add_schar" <c-char> -2 1)
(test-cfunc -1 "add_sshort" <c-short> -2 1)
(test-cfunc -1 "add_sint" <c-int> -2 1)
(test-cfunc -1 "add_slong" <c-long> -2 1)
(test-cfunc -1 "add_slonglong" <c-longlong> -2 1)
(test-cfunc 2.5 "add_float" <c-float> 5 -2.5)
(test-cfunc 2.5 "add_double" <c-double> 5 -2.5)
(test "add_string"
      "Hello, world"
      (lambda ()
        (let ((func (make-c-func 'add_string
                                 (ptr <c-uchar>)
                                 (list (ptr <c-uchar>) (ptr <c-uchar>)))))
          (cast <string> (func "Hello, " "world")))))

(define-syntax test-carray
  (syntax-rules ()
    ((_ expected func-name c-type class v1 v2)
     (test func-name
           expected
           (lambda ()
             (let* ((array-class (c-type (length v1)))
                    (func (make-c-func (string->symbol func-name)
                                       (ptr c-type)
                                       (list <c-int> (ptr c-type) (ptr c-type)))))
               (map (cut cast class <>)
                    (cast array-class
                          (func (length v1) v1 v2)))))))))

(test-carray '(5 7 9) "add_array_uchar" <c-uchar> <real> '(1 2 3) '(4 5 6))
(test-carray '(5 7 9) "add_array_ushort" <c-ushort> <real> '(1 2 3) '(4 5 6))
(test-carray '(5 7 9) "add_array_uint" <c-uint> <real> '(1 2 3) '(4 5 6))
(test-carray '(5 7 9) "add_array_ulong" <c-ulong> <real> '(1 2 3) '(4 5 6))
(test-carray '(5 7 9) "add_array_ulonglong" <c-ulonglong> <real>
             '(1 2 3) '(4 5 6))
(test-carray '(5 -7 9) "add_array_schar" <c-char> <real> '(1 2 3) '(4 -9 6))
(test-carray '(5 -7 9) "add_array_sshort" <c-short> <real> '(1 2 3) '(4 -9 6))
(test-carray '(5 -7 9) "add_array_sint" <c-int> <real> '(1 2 3) '(4 -9 6))
(test-carray '(5 -7 9) "add_array_slong" <c-long> <real> '(1 2 3) '(4 -9 6))
(test-carray '(5 -7 9) "add_array_slonglong" <c-longlong> <real>
             '(1 2 3) '(4 -9 6))
(test-carray '(-0.5 0.0 0.5) "add_array_float" <c-float> <real>
             '(-1 1.5 1.75) '(0.5 -1.5 -1.25))
(test-carray '(-0.5 0.0 0.5) "add_array_double" <c-double> <real>
             '(-1 1.5 1.75) '(0.5 -1.5 -1.25))
(test-carray '("foo1" "bar2" "baz3") "add_array_string" (ptr <c-uchar>) <string>
             '("foo" "bar" "baz") '("1" "2" "3"))

(define-syntax test-cstruct
  (syntax-rules ()
    ((_ expected func-name c-type class v1 v2)
     (test func-name
           expected
           (lambda ()
             (let* ((struct-class (make-c-struct (gensym)
                                                 (list (cons 'dummy <c-char>)
                                                       (cons 'value c-type))))
                    (func (make-c-func (string->symbol func-name)
                                       struct-class
                                       (list struct-class struct-class)))
                    (s1 (make struct-class))
                    (s2 (make struct-class)))
               (set! (ref s1 'value) v1)
               (set! (ref s2 'value) v2)
               (cast class (ref (func s1 s2) 'value))))))))

(test-cstruct 3 "add_struct_uchar" <c-uchar> <real> 2 1)
(test-cstruct 3 "add_struct_ushort" <c-ushort> <real> 2 1)
(test-cstruct 3 "add_struct_uint" <c-uint> <real> 2 1)
(test-cstruct 3 "add_struct_ulong" <c-ulong> <real> 2 1)
(test-cstruct 3 "add_struct_ulonglong" <c-ulonglong> <real> 2 1)
(test-cstruct -1 "add_struct_schar" <c-char> <real> -2 1)
(test-cstruct -1 "add_struct_sshort" <c-short> <real> -2 1)
(test-cstruct -1 "add_struct_sint" <c-int> <real> -2 1)
(test-cstruct -1 "add_struct_slong" <c-long> <real> -2 1)
(test-cstruct -1 "add_struct_slonglong" <c-longlong> <real> -2 1)
(test-cstruct 1.5 "add_struct_float" <c-float> <real> 1.75 -0.25)
(test-cstruct 1.5 "add_struct_double" <c-double> <real> 1.75 -0.25)
(test-cstruct "Hello, world" "add_struct_string" (ptr <c-uchar>) <string>
              "Hello, " "world")

(define-syntax test-cstruct-array
  (syntax-rules ()
    ((_ expected func-name c-type class v1 v2)
     (test func-name
           expected
           (lambda ()
             (let* ((array-class (c-type (length v1)))
                    (struct-class (make-c-struct (gensym)
                                                 (list (cons 'dummy <c-char>)
                                                       (cons 'value array-class))))
                    (func (make-c-func (string->symbol func-name)
                                       struct-class
                                       (list struct-class struct-class)))
                    (s1 (make struct-class))
                    (s2 (make struct-class)))
               (set! (ref s1 'value) v1)
               (set! (ref s2 'value) v2)
               (map (cut cast class <>)
                    (ref (func s1 s2) 'value))))))))

(test-cstruct-array '(5 7 9) "add_struct_array_uchar" <c-uchar> <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) "add_struct_array_ushort" <c-ushort> <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) "add_struct_array_uint" <c-uint> <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) "add_struct_array_ulong" <c-ulong> <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) "add_struct_array_ulonglong" <c-ulonglong> <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 -7 9) "add_struct_array_schar" <c-char> <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) "add_struct_array_sshort" <c-short> <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) "add_struct_array_sint" <c-int> <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) "add_struct_array_slong" <c-long> <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) "add_struct_array_slonglong" <c-longlong> <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(-0.5 0.0 0.5) "add_struct_array_float" <c-float> <real>
                    '(-1 1.5 1.75) '(0.5 -1.5 -1.25))
(test-cstruct-array '(-0.5 0.0 0.5) "add_struct_array_double" <c-double> <real>
                    '(-1 1.5 1.75) '(0.5 -1.5 -1.25))
(test-cstruct-array '("foo1" "bar2" "baz3") "add_struct_array_string"
                    (ptr <c-uchar>) <string>
                    '("foo" "bar" "baz") '("1" "2" "3"))

(define-syntax test-cclosure
  (syntax-rules ()
    ((_ expected func-name c-type v1 v2)
     (test func-name
           expected
           (lambda ()
             (let* ((fp-class (c-func-ptr
                               c-type
                               (list c-type c-type)))
                    (func (make-c-func (string->symbol func-name)
                                       c-type
                                       (list fp-class c-type c-type))))
               (func + v1 v2)))))))

(test-cclosure 3 "callback_uchar" <c-uchar> 2 1)
(test-cclosure 3 "callback_ushort" <c-ushort> 2 1)
(test-cclosure 3 "callback_uint" <c-uint> 2 1)
(test-cclosure 3 "callback_ulong" <c-ulong> 2 1)
(test-cclosure 3 "callback_ulonglong" <c-ulonglong> 2 1)
(test-cclosure -1 "callback_schar" <c-char> -2 1)
(test-cclosure -1 "callback_sshort" <c-short> -2 1)
(test-cclosure -1 "callback_sint" <c-int> -2 1)
(test-cclosure -1 "callback_slong" <c-long> -2 1)
(test-cclosure -1 "callback_slonglong" <c-longlong> -2 1)
(test-cclosure 2.5 "callback_float" <c-float> 5 -2.5)
(test-cclosure 2.5 "callback_double" <c-double> 5 -2.5)
(test "callback_string"
      "Hello, world"
      (lambda ()
        (let* ((fp-class (c-func-ptr
                          (ptr <c-uchar>)
                          (list (ptr <c-uchar>) (ptr <c-uchar>))))
               (func (make-c-func 'callback_string
                                  (ptr <c-uchar>)
                                  (list fp-class (ptr <c-uchar>) (ptr <c-uchar>)))))
          (cast <string> (func (lambda (a1 a2)
                                 (string-append (cast <string> a1)
                                                (cast <string> a2)))
                               "Hello, " "world")))))

(define-syntax test-vaarg
  (syntax-rules ()
    ((_ expected func-name in-c-type out-c-type class)
     (test (format "~a (~a)" func-name (class-name in-c-type))
           expected
           (lambda ()
             (let* ((array-class (out-c-type (length expected)))
                    (func (make-c-func-vaargs (string->symbol func-name)
                                              (ptr out-c-type)
                                              (list <c-int>))))
               (map (cut cast class <>)
                    (cast array-class
                          (apply func (length expected)
                                 (map (cut cast in-c-type <>)
                                      expected))))))))))

(test-vaarg '(1 2 3) "vaarg_uint" <c-uint> <c-uint> <real>)
(test-vaarg '(1 2 3) "vaarg_ulong" <c-ulong> <c-ulong> <real>)
(test-vaarg '(1 2 3) "vaarg_ulonglong" <c-ulonglong> <c-ulonglong> <real>)
(test-vaarg '(1 -2 3) "vaarg_sint" <c-int> <c-int> <real>)
(test-vaarg '(1 -2 3) "vaarg_slong" <c-long> <c-long> <real>)
(test-vaarg '(1 -2 3) "vaarg_slonglong" <c-longlong> <c-longlong> <real>)
(test-vaarg '(-0.5 0.0 0.5) "vaarg_double" <c-double> <c-double> <real>)
(test-vaarg '("foo" "bar" "baz") "vaarg_string" (ptr <c-uchar>) (ptr <c-uchar>) <string>)

;; promote type
(test-vaarg '(1 2 3) "vaarg_uint" <c-uchar> <c-uint> <real>)
(test-vaarg '(1 2 3) "vaarg_uint" <c-ushort> <c-uint> <real>)
(test-vaarg '(1 -2 3) "vaarg_sint" <c-char> <c-int> <real>)
(test-vaarg '(1 -2 3) "vaarg_sint" <c-short> <c-int> <real>)
(test-vaarg '(-0.5 0.0 0.5) "vaarg_double" <c-float> <c-double> <real>)

(define-syntax test-union
  (syntax-rules ()
    ((_ expected func-name class name v1 v2)
     (test func-name
           expected
           (lambda ()
             (let* ((union-class (make-c-union
                                  (gensym)
                                  `((c . ,<c-char>)
                                    (s . ,<c-short>)
                                    (i . ,<c-int>)
                                    (l . ,<c-long>)
                                    (ll . ,<c-longlong>)
                                    (f . ,<c-float>)
                                    (d . ,<c-double>)
                                    (str . ,(ptr <c-uchar>)))))
                    (func (make-c-func (string->symbol func-name)
                                       union-class
                                       (list union-class union-class)))
                    (u1 (make union-class))
                    (u2 (make union-class)))
               (set! (ref u1 name) v1)
               (set! (ref u2 name) v2)
               (cast class (ref (func u1 u2) name))))))))

(test-union -10 "test_union_c" <real> 'c -15 5)
(test-union -10 "test_union_s" <real> 's -9 -1)
(test-union -10 "test_union_i" <real> 'i 20 -30)
(test-union -10 "test_union_l" <real> 'l 10000000 -10000010)
(test-union -10 "test_union_ll" <real> 'll 0 -10)
(test-union -1.75 "test_union_f" <real> 'f -1 -0.75)
(test-union 0.5 "test_union_d" <real> 'd 3.75 -3.25)
(test-union "foobar" "test_union_str" <string> 'str "foo" "bar")

(test "test_modify" 
      2
      (lambda ()
        (let ((test_modify (make-c-func 'test_modify <c-void> (list (ptr <c-int>))))
              (v (make <c-int>)))
          (set! (ref v) 1)
          (test_modify (ptr v))
          (ref v))))

(test "null-ptr?"
      #t
      (lambda ()
        (null-ptr? (make-null-ptr))))

;; epilogue
(test-end)