File: cwrappertest.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 (557 lines) | stat: -rw-r--r-- 17,712 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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
;;;
;;; Test c-wrapper
;;;

(use gauche.test)

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

(c-load-library "./ffitest")
(c-include "./ffitest.h")

(define-syntax test-cfunc
  (syntax-rules ()
    ((_ expected func v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
               (func v1 v2))))))

(test-cfunc 3 add_uchar 2 1)
(test-cfunc 3 add_ushort 2 1)
(test-cfunc 3 add_uint 2 1)
(test-cfunc 3 add_ulong 2 1)
(test-cfunc 3 add_ulonglong 2 1)
(test-cfunc -1 add_schar -2 1)
(test-cfunc -1 add_sshort -2 1)
(test-cfunc -1 add_sint -2 1)
(test-cfunc -1 add_slong -2 1)
(test-cfunc -1 add_slonglong -2 1)
(test-cfunc 2.5 add_float 5 -2.5)
(test-cfunc 2.5 add_double 5 -2.5)
(test "add_string"
      "Hello, world"
      (lambda ()
        (x->string (add_string "Hello, " "world"))))

(define-syntax test-carray
  (syntax-rules ()
    ((_ expected func c-type class v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
             (map (cut cast class <>)
                  (cast (c-type (length v1))
                        (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 tagname class v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
             (let ((s1 (make tagname))
                   (s2 (make tagname)))
               (set! (ref s1 'value) v1)
               (set! (ref s2 'value) v2)
               (cast class (ref (func s1 s2) 'value))))))))

(test-cstruct 3 add_struct_uchar (c-struct 'test_uchar) <real> 2 1)
(test-cstruct 3 add_struct_ushort (c-struct 'test_ushort) <real> 2 1)
(test-cstruct 3 add_struct_uint (c-struct 'test_uint) <real> 2 1)
(test-cstruct 3 add_struct_ulong (c-struct 'test_ulong) <real> 2 1)
(test-cstruct 3 add_struct_ulonglong (c-struct 'test_ulonglong) <real> 2 1)
(test-cstruct -1 add_struct_schar (c-struct 'test_schar) <real> -2 1)
(test-cstruct -1 add_struct_sshort (c-struct 'test_sshort) <real> -2 1)
(test-cstruct -1 add_struct_sint (c-struct 'test_sint) <real> -2 1)
(test-cstruct -1 add_struct_slong (c-struct 'test_slong) <real> -2 1)
(test-cstruct -1 add_struct_slonglong (c-struct 'test_slonglong) <real> -2 1)
(test-cstruct 1.5 add_struct_float (c-struct 'test_float) <real> 1.75 -0.25)
(test-cstruct 1.5 add_struct_double (c-struct 'test_double) <real> 1.75 -0.25)
(test-cstruct "Hello, world" add_struct_string (c-struct 'test_string) <string>
              "Hello, " "world")

(define-syntax test-cstruct-array
  (syntax-rules ()
    ((_ expected func tagname class v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
             (let* ((s1 (make tagname))
                    (s2 (make tagname)))
               (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_ushort
                    (c-struct 'test_array_ushort)
                    <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) add_struct_array_uint
                    (c-struct 'test_array_uint)
                    <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) add_struct_array_ulong
                    (c-struct 'test_array_ulong)
                    <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 7 9) add_struct_array_ulonglong
                    (c-struct 'test_array_ulonglong)
                    <real>
                    '(1 2 3) '(4 5 6))
(test-cstruct-array '(5 -7 9) add_struct_array_schar
                    (c-struct 'test_array_schar)
                    <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) add_struct_array_sshort
                    (c-struct 'test_array_sshort)
                    <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) add_struct_array_sint
                    (c-struct 'test_array_sint)
                    <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) add_struct_array_slong
                    (c-struct 'test_array_slong)
                    <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(5 -7 9) add_struct_array_slonglong
                    (c-struct 'test_array_slonglong)
                    <real>
                    '(1 2 3) '(4 -9 6))
(test-cstruct-array '(-0.5 0.0 0.5) add_struct_array_float
                    (c-struct 'test_array_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-struct 'test_array_double)
                    <real>
                    '(-1 1.5 1.75) '(0.5 -1.5 -1.25))
(test-cstruct-array '("foo1" "bar2" "baz3") add_struct_array_string
                    (c-struct 'test_array_string)
                    <string>
                    '("foo" "bar" "baz") '("1" "2" "3"))

(define-syntax test-cclosure
  (syntax-rules ()
    ((_ expected func v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
               (func + v1 v2))))))

(test-cclosure 3 callback_uchar 2 1)
(test-cclosure 3 callback_ushort 2 1)
(test-cclosure 3 callback_uint 2 1)
(test-cclosure 3 callback_ulong 2 1)
(test-cclosure 3 callback_ulonglong 2 1)
(test-cclosure -1 callback_schar -2 1)
(test-cclosure -1 callback_sshort -2 1)
(test-cclosure -1 callback_sint -2 1)
(test-cclosure -1 callback_slong -2 1)
(test-cclosure -1 callback_slonglong -2 1)
(test-cclosure 2.5 callback_float 5 -2.5)
(test-cclosure 2.5 callback_double 5 -2.5)
(test "callback_string"
      "Hello, world"
      (lambda ()
        (x->string (callback_string (lambda (a1 a2)
                                      (string-append (x->string a1)
                                                     (x->string a2)))
                                    "Hello, " "world"))))

(define-syntax test-vaarg
  (syntax-rules ()
    ((_ expected func in-c-type out-c-type class)
     (test (symbol->string 'func)
           expected
           (lambda ()
             (let* ((array-class (out-c-type (length expected))))
               (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>)

(define-syntax test-union
  (syntax-rules ()
    ((_ expected func class name v1 v2)
     (test (symbol->string 'func)
           expected
           (lambda ()
             (let ((u1 (make (c-union 'test)))
                   (u2 (make (c-union 'test))))
               (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_var"
      '(-1 -2 -3 -4 -5 5 4 3 2 1 -0.5 0.5 "foobar")
      (lambda ()
        (set! (ref var_char) -2)
        (set! (ref var_short) -3)
        (set! (ref var_int) -4)
        (set! (ref var_long) -5)
        (set! (ref var_longlong) -6)
        (set! (ref var_uchar) 4)
        (set! (ref var_ushort) 3)
        (set! (ref var_uint) 2)
        (set! (ref var_ulong) 1)
        (set! (ref var_ulonglong) 0)
        (set! (ref var_float) -1.75)
        (set! (ref var_double) -0.75)
        (set! (ref var_string) "foo")
        (test_var)
        (list (ref var_char)
              (ref var_short)
              (ref var_int)
              (ref var_long)
              (ref var_longlong)
              (ref var_uchar)
              (ref var_ushort)
              (ref var_uint)
              (ref var_ulong)
              (ref var_ulonglong)
              (ref var_float)
              (ref var_double)
              (x->string var_string))))

(test "incomplete_array(ref)"
      '(1 -2 123)
      (lambda ()
        (list (ref incomplete_array 0)
              (ref incomplete_array 1)
              (ref incomplete_array 2))))

(test "incomplete_array(set!)"
      '(5 -2 -321)
      (lambda ()
        (set! (ref incomplete_array 0) 5)
        (set! (ref incomplete_array 2) -321)
        (list (ref incomplete_array 0)
              (ref incomplete_array 1)
              (ref incomplete_array 2))))

(test "test_null_ptr (make-null-ptr)"
      #t
      (lambda ()
        (not (= 0 (test_null_ptr (make-null-ptr))))))

(test "test_null_ptr (0)"
      #t
      (lambda ()
        (not (= 0 (test_null_ptr 0)))))

(test "test_null_func_ptr (make-null-ptr)"
      #t
      (lambda ()
        (not (= 0 (test_null_func_ptr (make-null-ptr))))))

(test "test_null_func_ptr (0)"
      #t
      (lambda ()
        (not (= 0 (test_null_func_ptr 0)))))

(test "test post++"
      '(3 4)
      (lambda ()
        (c-value-set! test_val 3)
        (let ((x (post_pp test_val)))
          (list x (test_val)))))

(test "test post--"
      '(3 2)
      (lambda ()
        (c-value-set! test_val 3)
        (let ((x (post_mm test_val)))
          (list x (test_val)))))

(test "test pre++"
      '(4 4)
      (lambda ()
        (c-value-set! test_val 3)
        (let ((x (pre_pp test_val)))
          (list x (test_val)))))

(test "test pre--"
      '(2 2)
      (lambda ()
        (c-value-set! test_val 3)
        (let ((x (pre_mm test_val)))
          (list x (test_val)))))

(test "test post++ (pointer)"
      '(-2 -1)
      (lambda ()
        (init_test_ptr)
        (let ((x (post_pp test_ptr)))
          (list ((deref x)) ((deref test_ptr))))))

(test "test post-- (pointer)"
      '(-1 -2)
      (lambda ()
        (init_test_ptr)
        (post++ test_ptr)
        (let ((x (post_mm test_ptr)))
          (list ((deref x)) ((deref test_ptr))))))

(test "test pre++ (pointer)"
      '(-1 -1)
      (lambda ()
        (init_test_ptr)
        (let ((x (pre_pp test_ptr)))
          (list ((deref x)) ((deref test_ptr))))))

(test "test pre-- (pointer)"
      '(-2 -2)
      (lambda ()
        (init_test_ptr)
        (pre++ test_ptr)
        (let ((x (pre_mm test_ptr)))
          (list ((deref x)) ((deref test_ptr))))))

(test "test plus (value)"
      5
      (lambda ()
        (plus 2 3)))

(test "test minus (value)"
      3
      (lambda ()
        (let ((v1 (make <c-int>))
              (v2 (make <c-int>)))
          (v1 5)
          (v2 2)
        (minus v1 v2))))

(test "test plus (pointer)"
      1
      (lambda ()
        (init_test_ptr)
        ((deref (plus test_ptr 3)))))

(test "test minus (pointer)"
      -1
      (lambda ()
        (init_test_ptr)
        ((deref (minus (plus test_ptr 3) 2)))))

(test "test bitfield"
      '(-2 2 2 -30001 3)
      (lambda ()
        (let ((dat (make (c-struct 'bitfield_rec))))
          (c-struct-set! dat 'v1 -1)
          (c-struct-set! dat 'v2 1)
          (c-struct-set! dat 'v3 1)
          (c-struct-set! dat 'v4 -30000)
          (c-struct-set! dat 'v5 2)
          (let ((newdat (test_bitfield dat)))
            (list (c-struct-ref newdat 'v1)
                  (c-struct-ref newdat 'v2)
                  (c-struct-ref newdat 'v3)
                  (c-struct-ref newdat 'v4)
                  (c-struct-ref newdat 'v5))))))

(test "test sizeof struct which has bitfields"
      (expected_sizeof_bitfield_rec2)
      (lambda ()
        (c-sizeof (c-struct 'bitfield_rec2))))

(test "test bitfield2"
      '(8191 2)
      (lambda ()
        (let ((dat (make (c-struct 'bitfield_rec2))))
          (c-struct-set! dat 'v1 8190)
          (c-struct-set! dat 'v2 1)
          (let ((newdat (test_bitfield2 dat)))
            (list (c-struct-ref newdat 'v1)
                  (c-struct-ref newdat 'v2))))))
               
(test "test macro function #1"
      '(1 0)
      (lambda ()
        (let ((v1 (make <c-int>))
              (v2 (make <c-int>)))
          (v1 0)
          (v2 0)
          (IFMAC 1 (post++ v1) (post++ v2))
          (list (v1) (v2)))))

(test "test macro function #2"
      '(0 1)
      (lambda ()
        (let ((v1 (make <c-int>))
              (v2 (make <c-int>)))
          (v1 0)
          (v2 0)
          (IFMAC 0 (post++ v1) (post++ v2))
          (list (v1) (v2)))))

(test "ignore function-like macro when an argument is used as a type."
      #f
      (lambda ()
        (global-variable-bound? (current-module) 'FUNCLIKE_MACRO_USING_TYPE_PARAM)))

(test "test function parameter"
      "foobar"
      (lambda ()
        (x->string (param_func_test malloc))))

(test "test dereference of a function pointer"
      3
      (lambda ()
        ((deref (get_fptr)) 1 2)))

(test "object-apply of a function pointer"
      3
      (lambda ()
        ((get_fptr) 1 2)))

(test "test (setter ref) for a pointer"
      3
      (lambda ()
        (let* ((v1 (make (c-array <c-int> 10)))
               (v2 (cast (ptr <c-int>) v1)))
          (set! (ref v2 5) 3)
          (ref v1 5))))

(test "test variable attribute 'mode' QI"
      <c-char>
      (lambda ()
        (class-of test_qi)))

(test "test variable attribute 'mode' HI"
      <c-short>
      (lambda ()
        (class-of test_hi)))

(test "test variable attribute 'mode' SI"
      <c-int>
      (lambda ()
        (class-of test_si)))

(test "test variable attribute 'mode' DI"
      <c-longlong>
      (lambda ()
        (class-of test_di)))

(test "test variable attribute 'mode' QI (unsigned)"
      <c-uchar>
      (lambda ()
        (class-of test_uqi)))

(test "test variable attribute 'mode' HI (unsigned)"
      <c-ushort>
      (lambda ()
        (class-of test_uhi)))

(test "test variable attribute 'mode' SI (unsigned)"
      <c-uint>
      (lambda ()
        (class-of test_usi)))

(test "test variable attribute 'mode' DI (unsigned)"
      <c-ulonglong>
      (lambda ()
        (class-of test_udi)))

(test "test variable attribute 'mode' SF"
      <c-float>
      (lambda ()
        (class-of test_sf)))

(test "test variable attribute 'mode' DF"
      <c-double>
      (lambda ()
        (class-of test_df)))

(test "test variable attribute 'mode' for struct"
      (list <c-char> <c-short> <c-int> <c-longlong>
            <c-uchar> <c-ushort> <c-uint> <c-ulonglong>
            <c-float> <c-double>)
      (lambda ()
        (let ((s (make (c-struct 'test_attr_mode_rec))))
          (map (lambda (name)
                 (class-of (raw-ref s name)))
               '(test_qi test_hi test_si test_di test_uqi test_uhi test_usi test_udi
                         test_sf test_df)))))

(test "test variable attribute 'mode' for function argument"
      '(127                  ; 2^7  - 1
        32767                ; 2^15 - 1
        2147483647           ; 2^31 - 1
        9223372036854775807  ; 2^63 - 1
        255                  ; 2^8  - 1
        65535                ; 2^16 - 1
        4294967295           ; 2^32 - 1
        18446744073709551615 ; 2^64 - 1
        0.5
        1.0e300
        )
      (lambda ()
        (test_attr_mode 127 32767 2147483647 9223372036854775807
                        255 65535 4294967295 18446744073709551615
                        0.5 1.0e300)
        (map scm-cast (list test_qi test_hi test_si test_di
                            test_uqi test_uhi test_usi test_udi
                            test_sf test_df))))

(test "test variable-like macro"
      '(1 2 3)
      (lambda ()
        (reset_counter)
        (do ((lst '() (cons (COUNTER) lst))
             (i 0 (+ i 1)))
            ((<= 3 i) (reverse lst)))))

(test "test suppress using function-like macro"
      3
      (lambda ()
        (confusing_func2 1 2)))

;; epilogue
(test-end)