Skip to content

Commit 5830249

Browse files
committed
Update tests to avoid duplicate definitions
1 parent 0610a69 commit 5830249

File tree

12 files changed

+754
-711
lines changed

12 files changed

+754
-711
lines changed

.github/workflows/CI.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@ jobs:
3737
version: ${{ matrix.version }}
3838
arch: ${{ matrix.arch }}
3939
- uses: julia-actions/julia-buildpkg@latest
40+
with:
41+
ignore-no-cache: true
4042
- uses: julia-actions/julia-runtest@latest

test/accessors.jl

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,42 @@
11

22

33
@testset "type accessors" begin
4-
@eval c``
5-
6-
@eval c"""
7-
struct AccessorInner {
8-
int i;
9-
int j:12;
10-
};
11-
"""
12-
13-
@eval c"""
14-
struct AccessorOuter {
15-
int i;
16-
int j:12;
17-
struct AccessorInner inner;
18-
struct AccessorInner *ptr;
19-
struct {
20-
struct AccessorInner innerer;
4+
@eval module CBinding_accessors
5+
using CBinding
6+
c``
7+
8+
c"""
9+
struct AccessorInner {
10+
int i;
11+
int j:12;
2112
};
22-
};
23-
"""
13+
14+
struct AccessorOuter {
15+
int i;
16+
int j:12;
17+
struct AccessorInner inner;
18+
struct AccessorInner *ptr;
19+
struct {
20+
struct AccessorInner innerer;
21+
};
22+
};
23+
"""
24+
end
25+
26+
mod = @eval CBinding_accessors
2427

25-
ptr = Libc.malloc(c"struct AccessorOuter")
28+
ptr = Libc.malloc(@eval mod c"struct AccessorOuter")
2629
@test ptr != C_NULL
2730

2831
x = reinterpret(Cptr{UInt8}, ptr)
2932
@test ptr.i == x
3033
@test ptr.inner == x+sizeof(Cint)*2
3134
@test ptr.inner.i == x+sizeof(Cint)*2
32-
@test ptr.ptr == x+sizeof(Cint)*2+sizeof(c"struct AccessorInner")
33-
@test ptr.innerer == x+sizeof(Cint)*2+sizeof(c"struct AccessorInner")+sizeof(c"struct AccessorInner *")
34-
@test ptr.innerer.i == x+sizeof(Cint)*2+sizeof(c"struct AccessorInner")+sizeof(c"struct AccessorInner *")
35+
@test ptr.ptr == x+sizeof(Cint)*2+sizeof(@eval mod c"struct AccessorInner")
36+
@test ptr.innerer == x+sizeof(Cint)*2+sizeof(@eval mod c"struct AccessorInner")+sizeof(@eval mod c"struct AccessorInner *")
37+
@test ptr.innerer.i == x+sizeof(Cint)*2+sizeof(@eval mod c"struct AccessorInner")+sizeof(@eval mod c"struct AccessorInner *")
3538

36-
obj = c"struct AccessorOuter"()
39+
obj = (@eval mod c"struct AccessorOuter")()
3740
@test obj.i == 0
3841
@test obj.j == 0
3942
@test obj.inner.i == 0

test/arrays.jl

Lines changed: 61 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,94 @@
11

22

33
@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")()
3448
@test sizeof(pa) == sizeof(Cptr)*3
3549
@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 *")
3852

3953
for i in eachindex(pa)
40-
pa = c"PtrArray"(pa, i => C_NULL)
54+
pa = (@eval mod c"PtrArray")(pa, i => C_NULL)
4155
@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))
4357
@test pa[i] != C_NULL
4458
end
4559

46-
pa = c"PtrArray"()
60+
pa = (@eval mod c"PtrArray")()
4761
for ptr in pa
4862
@test ptr == C_NULL
4963
end
5064

51-
ca = c"CStructArray"()
65+
ca = (@eval mod c"CStructArray")()
5266
@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")
5569

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
6273

63-
ca = c"CarrayArray"()
74+
ca = (@eval mod c"CarrayArray")()
6475
@test length(ca) == 4
6576
@test length(ca[1]) == 2
6677
for i in eachindex(ca), j in eachindex(ca[i])
6778
@test ca[i][j] == 0
6879
end
6980

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)
7782

78-
ula = c"struct UnknownLengthArray"()
83+
ula = (@eval mod c"struct UnknownLengthArray")()
7984
@test ula.len == 0
8085
@test ula.str isa Carray{Cchar}
8186
@test length(ula.str) == 0
8287

8388
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))
8590

86-
ula = Cptr{c"struct UnknownLengthArray"}(ptr)
91+
ula = Cptr{@eval mod c"struct UnknownLengthArray"}(ptr)
8792
@test ula.len isa Cptr{Cint}
8893
@test ula.len == ptr
8994

@@ -103,12 +108,7 @@
103108
Libc.free(ptr)
104109

105110
# 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")
112112
@test x.Header[2] == Int('e')
113113
@test x.Header[2000] == 0
114114
end

test/contexts.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,30 @@
11

22

33
@testset "c`...`" begin
4-
@eval c``
5-
@test haskey(CBinding.CONTEXT_CACHE, @__MODULE__)
6-
ctx = CBinding.CONTEXT_CACHE[@__MODULE__]
7-
@test ctx isa CBinding.Context{:c}
8-
@test length(ctx.libs) == 1 # the julia process is the only lib
4+
@eval module CBinding_contexts_1
5+
using CBinding
6+
c``
7+
end
98

10-
@eval begin
9+
@eval module CBinding_contexts_2
10+
using CBinding
1111
lib = startswith(Base.libm_name, "lib") ? Base.libm_name[4:end] : Base.libm_name
1212
libpath = normpath(joinpath(dirname(Base.julia_cmd().exec[1]), Base.LIBDIR, "julia"))
1313
c`-L$(libpath) -l$(lib)`
1414
end
15-
@test ctx !== CBinding.CONTEXT_CACHE[@__MODULE__]
16-
ctx = CBinding.CONTEXT_CACHE[@__MODULE__]
15+
16+
17+
mod = @eval CBinding_contexts_1
18+
cache = mod.CBinding.CONTEXT_CACHE
19+
@test haskey(cache, mod)
20+
ctx = cache[mod]
21+
@test ctx isa CBinding.Context{:c}
22+
@test length(ctx.libs) == 1 # the julia process is the only lib
23+
24+
mod = @eval CBinding_contexts_2
25+
cache = mod.CBinding.CONTEXT_CACHE
26+
@test ctx !== cache[mod]
27+
ctx = cache[mod]
1728
@test ctx isa CBinding.Context{:c}
1829
@test length(ctx.libs) == 2
1930
end

0 commit comments

Comments
 (0)