|
1 | 1 |
|
2 | 2 |
|
3 | 3 | @testset "c\"... x[N];\"" begin
|
4 |
| - c`` |
5 |
| - |
6 |
| - @eval c""" |
7 |
| - typedef char CcharArray[2]; |
8 |
| - """ |
9 |
| - @test sizeof(c"CcharArray") == sizeof(Cchar)*2 |
10 |
| - |
11 |
| - @eval c""" |
12 |
| - typedef struct { |
13 |
| - int i; |
14 |
| - } CstructArray[10]; |
15 |
| - """ |
16 |
| - @test sizeof(c"CstructArray") == sizeof(Cint)*10 |
17 |
| - |
18 |
| - @eval c""" |
19 |
| - struct CStruct { |
20 |
| - char c; |
21 |
| - int i; |
22 |
| - } __attribute__((packed)); |
23 |
| - typedef struct CStruct CStructArray[2]; |
24 |
| - """ |
25 |
| - @test sizeof(c"CStructArray") == (sizeof(Cchar)+sizeof(Cint))*2 |
26 |
| - |
27 |
| - @eval c""" |
28 |
| - struct Opaque; |
29 |
| - typedef struct Opaque *PtrArray[3]; |
30 |
| - """ |
31 |
| - @test sizeof(c"PtrArray") == sizeof(Cptr)*3 |
32 |
| - |
33 |
| - pa = c"PtrArray"() |
| 4 | + @eval module CBinding_arrays |
| 5 | + using CBinding |
| 6 | + c`` |
| 7 | + |
| 8 | + c""" |
| 9 | + typedef char CcharArray[2]; |
| 10 | + |
| 11 | + typedef struct { |
| 12 | + int i; |
| 13 | + } CstructArray[10]; |
| 14 | + |
| 15 | + struct CStruct { |
| 16 | + char c; |
| 17 | + int i; |
| 18 | + } __attribute__((packed)); |
| 19 | + typedef struct CStruct CStructArray[2]; |
| 20 | + |
| 21 | + struct Opaque; |
| 22 | + typedef struct Opaque *PtrArray[3]; |
| 23 | + |
| 24 | + typedef int CarrayArray[4][2]; |
| 25 | + |
| 26 | + struct UnknownLengthArray { |
| 27 | + int len; |
| 28 | + char str[]; |
| 29 | + }; |
| 30 | + |
| 31 | + struct file_format { |
| 32 | + unsigned char Header[2000]; |
| 33 | + }; |
| 34 | + """ |
| 35 | + end |
| 36 | + |
| 37 | + mod = @eval CBinding_arrays |
| 38 | + |
| 39 | + @test sizeof(@eval mod c"CcharArray") == sizeof(Cchar)*2 |
| 40 | + |
| 41 | + @test sizeof(@eval mod c"CstructArray") == sizeof(Cint)*10 |
| 42 | + |
| 43 | + @test sizeof(@eval mod c"CStructArray") == (sizeof(Cchar)+sizeof(Cint))*2 |
| 44 | + |
| 45 | + @test sizeof(@eval mod c"PtrArray") == sizeof(Cptr)*3 |
| 46 | + |
| 47 | + pa = (@eval mod c"PtrArray")() |
34 | 48 | @test sizeof(pa) == sizeof(Cptr)*3
|
35 | 49 | @test length(pa) == 3
|
36 |
| - @test eltype(pa) <: c"struct Opaque *" |
37 |
| - @test pa[1] isa c"struct Opaque *" |
| 50 | + @test eltype(pa) <: (@eval mod c"struct Opaque *") |
| 51 | + @test pa[1] isa (@eval mod c"struct Opaque *") |
38 | 52 |
|
39 | 53 | for i in eachindex(pa)
|
40 |
| - pa = c"PtrArray"(pa, i => C_NULL) |
| 54 | + pa = (@eval mod c"PtrArray")(pa, i => C_NULL) |
41 | 55 | @test pa[i] == C_NULL
|
42 |
| - pa = c"PtrArray"(pa, i => c"struct Opaque *"(1234)) |
| 56 | + pa = (@eval mod c"PtrArray")(pa, i => (@eval mod c"struct Opaque *")(1234)) |
43 | 57 | @test pa[i] != C_NULL
|
44 | 58 | end
|
45 | 59 |
|
46 |
| - pa = c"PtrArray"() |
| 60 | + pa = (@eval mod c"PtrArray")() |
47 | 61 | for ptr in pa
|
48 | 62 | @test ptr == C_NULL
|
49 | 63 | end
|
50 | 64 |
|
51 |
| - ca = c"CStructArray"() |
| 65 | + ca = (@eval mod c"CStructArray")() |
52 | 66 | @test length(ca) == 2
|
53 |
| - @test eltype(ca) <: c"struct CStruct" |
54 |
| - @test ca[2] isa c"struct CStruct" |
| 67 | + @test eltype(ca) <: (@eval mod c"struct CStruct") |
| 68 | + @test ca[2] isa (@eval mod c"struct CStruct") |
55 | 69 |
|
56 |
| - @eval c""" |
57 |
| - typedef int CarrayArray[4][2]; |
58 |
| - """ |
59 |
| - @test sizeof(c"CarrayArray") == sizeof(Cint)*4*2 |
60 |
| - @test eltype(c"CarrayArray") <: c"int[2]" |
61 |
| - @test eltype(eltype(c"CarrayArray")) <: Cint |
| 70 | + @test sizeof(@eval mod c"CarrayArray") == sizeof(Cint)*4*2 |
| 71 | + @test eltype(@eval mod c"CarrayArray") <: (@eval mod c"int[2]") |
| 72 | + @test eltype(eltype(@eval mod c"CarrayArray")) <: Cint |
62 | 73 |
|
63 |
| - ca = c"CarrayArray"() |
| 74 | + ca = (@eval mod c"CarrayArray")() |
64 | 75 | @test length(ca) == 4
|
65 | 76 | @test length(ca[1]) == 2
|
66 | 77 | for i in eachindex(ca), j in eachindex(ca[i])
|
67 | 78 | @test ca[i][j] == 0
|
68 | 79 | end
|
69 | 80 |
|
70 |
| - @eval c""" |
71 |
| - struct UnknownLengthArray { |
72 |
| - int len; |
73 |
| - char str[]; |
74 |
| - }; |
75 |
| - """ |
76 |
| - @test sizeof(c"struct UnknownLengthArray") == sizeof(Cint) |
| 81 | + @test sizeof(@eval mod c"struct UnknownLengthArray") == sizeof(Cint) |
77 | 82 |
|
78 |
| - ula = c"struct UnknownLengthArray"() |
| 83 | + ula = (@eval mod c"struct UnknownLengthArray")() |
79 | 84 | @test ula.len == 0
|
80 | 85 | @test ula.str isa Carray{Cchar}
|
81 | 86 | @test length(ula.str) == 0
|
82 | 87 |
|
83 | 88 | len = 10
|
84 |
| - ptr = Libc.malloc(sizeof(c"struct UnknownLengthArray") + len*sizeof(Cchar)) |
| 89 | + ptr = Libc.malloc(sizeof(@eval mod c"struct UnknownLengthArray") + len*sizeof(Cchar)) |
85 | 90 |
|
86 |
| - ula = Cptr{c"struct UnknownLengthArray"}(ptr) |
| 91 | + ula = Cptr{@eval mod c"struct UnknownLengthArray"}(ptr) |
87 | 92 | @test ula.len isa Cptr{Cint}
|
88 | 93 | @test ula.len == ptr
|
89 | 94 |
|
|
103 | 108 | Libc.free(ptr)
|
104 | 109 |
|
105 | 110 | # https://github.com/analytech-solutions/CBinding.jl/issues/66
|
106 |
| - @eval c""" |
107 |
| - struct file_format { |
108 |
| - unsigned char Header[2000]; |
109 |
| - }; |
110 |
| - """ |
111 |
| - x = c"struct file_format"(Header = "test") |
| 111 | + x = (@eval mod c"struct file_format")(Header = "test") |
112 | 112 | @test x.Header[2] == Int('e')
|
113 | 113 | @test x.Header[2000] == 0
|
114 | 114 | end
|
|
0 commit comments