Skip to content

Commit 4b9388f

Browse files
committed
Add sort_declarations! tests
1 parent b3278d0 commit 4b9388f

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed

test/runtests.jl

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
using JuliaLibWrapping
2+
using OrderedCollections
23
using Test
34

5+
import JuliaLibWrapping: StructDesc, FieldDesc, PointerDesc, PrimitiveTypeDesc, TypeDesc
6+
import JuliaLibWrapping: sort_declarations!
7+
48
function onlymatch(f, collection)
59
matches = filter(f, collection)
610
if length(matches) != 1
@@ -70,6 +74,90 @@ end
7074
@test name2idx["CVectorPair{Float32}"] > name2idx["CVector{Float32}"]
7175
end
7276

77+
@testset "sort_declarations!" begin
78+
unsorted = OrderedDict{Int, TypeDesc}(
79+
1 => StructDesc(
80+
"test_struct1",
81+
0, # size
82+
0, # alignment
83+
FieldDesc[
84+
FieldDesc("field1", #= type =# 2, #= offset =# 0),
85+
FieldDesc("field2", #= type =# 3, #= offset =# 0),
86+
],
87+
),
88+
2 => StructDesc(
89+
"test_struct2",
90+
0, # size
91+
0, # alignment
92+
FieldDesc[
93+
FieldDesc("field1", #= type =# 3, #= offset =# 0),
94+
FieldDesc("field2", #= type =# 3, #= offset =# 0),
95+
],
96+
),
97+
3 => PrimitiveTypeDesc("UInt16"),
98+
)
99+
sorted = copy(unsorted)
100+
fwd_decls = sort_declarations!(sorted)
101+
102+
# No recursive types, so this should require no forward declarations
103+
@test isempty(fwd_decls)
104+
# There is only one order that these types could be defined such that
105+
# dependencies are defined before they are used.
106+
@test collect(keys(sorted)) == Int[3,2,1]
107+
108+
unsorted = OrderedDict{Int, TypeDesc}(
109+
1 => StructDesc(
110+
"test_struct1",
111+
0, # size
112+
0, # alignment
113+
FieldDesc[
114+
FieldDesc("field1", #= type =# 2, #= offset =# 0),
115+
FieldDesc("field2", #= type =# 5, #= offset =# 0),
116+
],
117+
),
118+
2 => PointerDesc("pointer1", #= pointee_type =# 3),
119+
3 => StructDesc(
120+
"test_struct2",
121+
0, # size
122+
0, # alignment
123+
FieldDesc[
124+
FieldDesc("field1", #= type =# 5, #= offset =# 0),
125+
FieldDesc("field2", #= type =# 5, #= offset =# 0),
126+
],
127+
),
128+
4 => PointerDesc("pointer2", #= pointee_type =# 1),
129+
5 => PrimitiveTypeDesc("UInt16"),
130+
)
131+
sorted = copy(unsorted)
132+
fwd_decls = sort_declarations!(sorted)
133+
134+
# We added a pointer indirection, but it's non-recursive so we should
135+
# require no forward declarations
136+
@test isempty(fwd_decls)
137+
138+
# Once again, there is only one order that we can emit these definitions
139+
@test collect(keys(sorted)) == Int[5,3,2,1,4]
140+
141+
# Modify the type definitions so that test_struct1 and test_struct2 are
142+
# mutually recursive.
143+
unsorted[3].fields[1] = FieldDesc("field1", #= type =# 4, #= offset =# 0)
144+
145+
sorted = copy(unsorted)
146+
fwd_decls = sort_declarations!(sorted)
147+
148+
# At least one of the struct types should need to be forward-declared
149+
@test !isempty(fwd_decls)
150+
if fwd_decls == BitSet([1])
151+
# If 1 was forward-declared then 3 (and pointer to 1) is defined first
152+
@test collect(keys(sorted)) == Int[5,4,3,2,1]
153+
elseif fwd_decls == BitSet([3])
154+
# If 3 was forward-declared then 1 (and pointer to 3) is defined first
155+
@test collect(keys(sorted)) == Int[5,2,1,4,3]
156+
else
157+
@test false # unexpected forward declarations
158+
end
159+
end
160+
73161
@testset "C wrapper" begin
74162
mktempdir() do path
75163
mkpath(path)

0 commit comments

Comments
 (0)