7
(test-start "c-wrapper")
10
(test-module 'c-wrapper)
12
(c-load-library "./ffitest")
13
(c-include "./ffitest.h")
15
(define-syntax test-cfunc
17
((_ expected func v1 v2)
18
(test (symbol->string 'func)
23
(test-cfunc 3 add_uchar 2 1)
24
(test-cfunc 3 add_ushort 2 1)
25
(test-cfunc 3 add_uint 2 1)
26
(test-cfunc 3 add_ulong 2 1)
27
(test-cfunc 3 add_ulonglong 2 1)
28
(test-cfunc -1 add_schar -2 1)
29
(test-cfunc -1 add_sshort -2 1)
30
(test-cfunc -1 add_sint -2 1)
31
(test-cfunc -1 add_slong -2 1)
32
(test-cfunc -1 add_slonglong -2 1)
33
(test-cfunc 2.5 add_float 5 -2.5)
34
(test-cfunc 2.5 add_double 5 -2.5)
38
(x->string (add_string "Hello, " "world"))))
40
(define-syntax test-carray
42
((_ expected func c-type class v1 v2)
43
(test (symbol->string 'func)
46
(map (cut cast class <>)
47
(cast (c-type (length v1))
48
(func (length v1) v1 v2))))))))
50
(test-carray '(5 7 9) add_array_uchar <c-uchar> <real> '(1 2 3) '(4 5 6))
51
(test-carray '(5 7 9) add_array_ushort <c-ushort> <real> '(1 2 3) '(4 5 6))
52
(test-carray '(5 7 9) add_array_uint <c-uint> <real> '(1 2 3) '(4 5 6))
53
(test-carray '(5 7 9) add_array_ulong <c-ulong> <real> '(1 2 3) '(4 5 6))
54
(test-carray '(5 7 9) add_array_ulonglong <c-ulonglong> <real>
57
(test-carray '(5 -7 9) add_array_schar <c-char> <real> '(1 2 3) '(4 -9 6))
58
(test-carray '(5 -7 9) add_array_sshort <c-short> <real> '(1 2 3) '(4 -9 6))
59
(test-carray '(5 -7 9) add_array_sint <c-int> <real> '(1 2 3) '(4 -9 6))
60
(test-carray '(5 -7 9) add_array_slong <c-long> <real> '(1 2 3) '(4 -9 6))
61
(test-carray '(5 -7 9) add_array_slonglong <c-longlong> <real>
63
(test-carray '(-0.5 0.0 0.5) add_array_float <c-float> <real>
64
'(-1 1.5 1.75) '(0.5 -1.5 -1.25))
65
(test-carray '(-0.5 0.0 0.5) add_array_double <c-double> <real>
66
'(-1 1.5 1.75) '(0.5 -1.5 -1.25))
67
(test-carray '("foo1" "bar2" "baz3") add_array_string (ptr <c-uchar>) <string>
68
'("foo" "bar" "baz") '("1" "2" "3"))
70
(define-syntax test-cstruct
72
((_ expected func tagname class v1 v2)
73
(test (symbol->string 'func)
76
(let ((s1 (make tagname))
78
(set! (ref s1 'value) v1)
79
(set! (ref s2 'value) v2)
80
(cast class (ref (func s1 s2) 'value))))))))
82
(test-cstruct 3 add_struct_uchar (c-struct 'test_uchar) <real> 2 1)
83
(test-cstruct 3 add_struct_ushort (c-struct 'test_ushort) <real> 2 1)
84
(test-cstruct 3 add_struct_uint (c-struct 'test_uint) <real> 2 1)
85
(test-cstruct 3 add_struct_ulong (c-struct 'test_ulong) <real> 2 1)
86
(test-cstruct 3 add_struct_ulonglong (c-struct 'test_ulonglong) <real> 2 1)
87
(test-cstruct -1 add_struct_schar (c-struct 'test_schar) <real> -2 1)
88
(test-cstruct -1 add_struct_sshort (c-struct 'test_sshort) <real> -2 1)
89
(test-cstruct -1 add_struct_sint (c-struct 'test_sint) <real> -2 1)
90
(test-cstruct -1 add_struct_slong (c-struct 'test_slong) <real> -2 1)
91
(test-cstruct -1 add_struct_slonglong (c-struct 'test_slonglong) <real> -2 1)
92
(test-cstruct 1.5 add_struct_float (c-struct 'test_float) <real> 1.75 -0.25)
93
(test-cstruct 1.5 add_struct_double (c-struct 'test_double) <real> 1.75 -0.25)
94
(test-cstruct "Hello, world" add_struct_string (c-struct 'test_string) <string>
97
(define-syntax test-cstruct-array
99
((_ expected func tagname class v1 v2)
100
(test (symbol->string 'func)
103
(let* ((s1 (make tagname))
105
(set! (ref s1 'value) v1)
106
(set! (ref s2 'value) v2)
107
(map (cut cast class <>)
108
(ref (func s1 s2) 'value))))))))
110
(test-cstruct-array '(5 7 9) add_struct_array_ushort
111
(c-struct 'test_array_ushort)
114
(test-cstruct-array '(5 7 9) add_struct_array_uint
115
(c-struct 'test_array_uint)
118
(test-cstruct-array '(5 7 9) add_struct_array_ulong
119
(c-struct 'test_array_ulong)
122
(test-cstruct-array '(5 7 9) add_struct_array_ulonglong
123
(c-struct 'test_array_ulonglong)
126
(test-cstruct-array '(5 -7 9) add_struct_array_schar
127
(c-struct 'test_array_schar)
130
(test-cstruct-array '(5 -7 9) add_struct_array_sshort
131
(c-struct 'test_array_sshort)
134
(test-cstruct-array '(5 -7 9) add_struct_array_sint
135
(c-struct 'test_array_sint)
138
(test-cstruct-array '(5 -7 9) add_struct_array_slong
139
(c-struct 'test_array_slong)
142
(test-cstruct-array '(5 -7 9) add_struct_array_slonglong
143
(c-struct 'test_array_slonglong)
146
(test-cstruct-array '(-0.5 0.0 0.5) add_struct_array_float
147
(c-struct 'test_array_float)
149
'(-1 1.5 1.75) '(0.5 -1.5 -1.25))
150
(test-cstruct-array '(-0.5 0.0 0.5) add_struct_array_double
151
(c-struct 'test_array_double)
153
'(-1 1.5 1.75) '(0.5 -1.5 -1.25))
154
(test-cstruct-array '("foo1" "bar2" "baz3") add_struct_array_string
155
(c-struct 'test_array_string)
157
'("foo" "bar" "baz") '("1" "2" "3"))
159
(define-syntax test-cclosure
161
((_ expected func v1 v2)
162
(test (symbol->string 'func)
167
(test-cclosure 3 callback_uchar 2 1)
168
(test-cclosure 3 callback_ushort 2 1)
169
(test-cclosure 3 callback_uint 2 1)
170
(test-cclosure 3 callback_ulong 2 1)
171
(test-cclosure 3 callback_ulonglong 2 1)
172
(test-cclosure -1 callback_schar -2 1)
173
(test-cclosure -1 callback_sshort -2 1)
174
(test-cclosure -1 callback_sint -2 1)
175
(test-cclosure -1 callback_slong -2 1)
176
(test-cclosure -1 callback_slonglong -2 1)
177
(test-cclosure 2.5 callback_float 5 -2.5)
178
(test-cclosure 2.5 callback_double 5 -2.5)
179
(test "callback_string"
182
(x->string (callback_string (lambda (a1 a2)
183
(string-append (x->string a1)
185
"Hello, " "world"))))
187
(define-syntax test-vaarg
189
((_ expected func in-c-type out-c-type class)
190
(test (symbol->string 'func)
193
(let* ((array-class (out-c-type (length expected))))
194
(map (cut cast class <>)
196
(apply func (length expected)
197
(map (cut cast in-c-type <>)
200
(test-vaarg '(1 2 3) vaarg_uint <c-uint> <c-uint> <real>)
201
(test-vaarg '(1 2 3) vaarg_ulong <c-ulong> <c-ulong> <real>)
202
(test-vaarg '(1 2 3) vaarg_ulonglong <c-ulonglong> <c-ulonglong> <real>)
203
(test-vaarg '(1 -2 3) vaarg_sint <c-int> <c-int> <real>)
204
(test-vaarg '(1 -2 3) vaarg_slong <c-long> <c-long> <real>)
205
(test-vaarg '(1 -2 3) vaarg_slonglong <c-longlong> <c-longlong> <real>)
206
(test-vaarg '(-0.5 0.0 0.5) vaarg_double <c-double> <c-double> <real>)
207
(test-vaarg '("foo" "bar" "baz") vaarg_string
208
(ptr <c-uchar>) (ptr <c-uchar>) <string>)
210
(define-syntax test-union
212
((_ expected func class name v1 v2)
213
(test (symbol->string 'func)
216
(let ((u1 (make (c-union 'test)))
217
(u2 (make (c-union 'test))))
218
(set! (ref u1 name) v1)
219
(set! (ref u2 name) v2)
220
(cast class (ref (func u1 u2) name))))))))
222
(test-union -10 test_union_c <real> 'c -15 5)
223
(test-union -10 test_union_s <real> 's -9 -1)
224
(test-union -10 test_union_i <real> 'i 20 -30)
225
(test-union -10 test_union_l <real> 'l 10000000 -10000010)
226
(test-union -10 test_union_ll <real> 'll 0 -10)
227
(test-union -1.75 test_union_f <real> 'f -1 -0.75)
228
(test-union 0.5 test_union_d <real> 'd 3.75 -3.25)
229
(test-union "foobar" test_union_str <string> 'str "foo" "bar")
232
'(-1 -2 -3 -4 -5 5 4 3 2 1 -0.5 0.5 "foobar")
234
(set! (ref var_char) -2)
235
(set! (ref var_short) -3)
236
(set! (ref var_int) -4)
237
(set! (ref var_long) -5)
238
(set! (ref var_longlong) -6)
239
(set! (ref var_uchar) 4)
240
(set! (ref var_ushort) 3)
241
(set! (ref var_uint) 2)
242
(set! (ref var_ulong) 1)
243
(set! (ref var_ulonglong) 0)
244
(set! (ref var_float) -1.75)
245
(set! (ref var_double) -0.75)
246
(set! (ref var_string) "foo")
260
(x->string var_string))))
262
(test "incomplete_array(ref)"
265
(list (ref incomplete_array 0)
266
(ref incomplete_array 1)
267
(ref incomplete_array 2))))
269
(test "incomplete_array(set!)"
272
(set! (ref incomplete_array 0) 5)
273
(set! (ref incomplete_array 2) -321)
274
(list (ref incomplete_array 0)
275
(ref incomplete_array 1)
276
(ref incomplete_array 2))))
278
(test "test_null_ptr (make-null-ptr)"
281
(not (= 0 (test_null_ptr (make-null-ptr))))))
283
(test "test_null_ptr (0)"
286
(not (= 0 (test_null_ptr 0)))))
288
(test "test_null_func_ptr (make-null-ptr)"
291
(not (= 0 (test_null_func_ptr (make-null-ptr))))))
293
(test "test_null_func_ptr (0)"
296
(not (= 0 (test_null_func_ptr 0)))))
301
(c-value-set! test_val 3)
302
(let ((x (post_pp test_val)))
303
(list x (test_val)))))
308
(c-value-set! test_val 3)
309
(let ((x (post_mm test_val)))
310
(list x (test_val)))))
315
(c-value-set! test_val 3)
316
(let ((x (pre_pp test_val)))
317
(list x (test_val)))))
322
(c-value-set! test_val 3)
323
(let ((x (pre_mm test_val)))
324
(list x (test_val)))))
326
(test "test post++ (pointer)"
330
(let ((x (post_pp test_ptr)))
331
(list ((deref x)) ((deref test_ptr))))))
333
(test "test post-- (pointer)"
338
(let ((x (post_mm test_ptr)))
339
(list ((deref x)) ((deref test_ptr))))))
341
(test "test pre++ (pointer)"
345
(let ((x (pre_pp test_ptr)))
346
(list ((deref x)) ((deref test_ptr))))))
348
(test "test pre-- (pointer)"
353
(let ((x (pre_mm test_ptr)))
354
(list ((deref x)) ((deref test_ptr))))))
356
(test "test plus (value)"
361
(test "test minus (value)"
364
(let ((v1 (make <c-int>))
370
(test "test plus (pointer)"
374
((deref (plus test_ptr 3)))))
376
(test "test minus (pointer)"
380
((deref (minus (plus test_ptr 3) 2)))))
382
(test "test bitfield"
385
(let ((dat (make (c-struct 'bitfield_rec))))
386
(c-struct-set! dat 'v1 -1)
387
(c-struct-set! dat 'v2 1)
388
(c-struct-set! dat 'v3 1)
389
(c-struct-set! dat 'v4 -30000)
390
(c-struct-set! dat 'v5 2)
391
(let ((newdat (test_bitfield dat)))
392
(list (c-struct-ref newdat 'v1)
393
(c-struct-ref newdat 'v2)
394
(c-struct-ref newdat 'v3)
395
(c-struct-ref newdat 'v4)
396
(c-struct-ref newdat 'v5))))))
398
(test "test macro function #1"
401
(let ((v1 (make <c-int>))
405
(IFMAC 1 (post++ v1) (post++ v2))
408
(test "test macro function #2"
411
(let ((v1 (make <c-int>))
415
(IFMAC 0 (post++ v1) (post++ v2))
418
(test "test function parameter"
421
(x->string (param_func_test malloc))))
423
(test "test dereference of a function pointer"
426
((deref (get_fptr)) 1 2)))
428
(test "object-apply of a function pointer"