Skip to content

Commit

Permalink
Merge 2d4e122 into 81d960c
Browse files Browse the repository at this point in the history
  • Loading branch information
stecrotti authored Apr 17, 2024
2 parents 81d960c + 2d4e122 commit 81910a1
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/FactorGraphs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ include("factorgraph.jl")
include("generators.jl")
include("infinite_regular_factorgraph.jl")

export FactorGraph, nvariables, nfactors, variables, factors, factor, variable,
export AbstractFactorGraph, FactorGraph, nvariables, nfactors, variables, factors, factor, variable,
pairwise_interaction_graph,
neighbors, edges, src, dst, idx, ne, nv, degree,
edge_indices, inedges, outedges,
Expand Down
14 changes: 9 additions & 5 deletions test/factorgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ A = sprand(rng, m, n, 0.1)
g = FactorGraph(A)

@testset "Basics" begin
@test nfactors(g) == m
@test nvariables(g) == n
@test all(degree(g, factor(a)) == length(neighbors(g,factor(a))) for a in factors(g))
@test all(degree(g, variable(i)) == length(neighbors(g,variable(i))) for i in variables(g))
@test @inferred nfactors(g) == m
@test @inferred nvariables(g) == n
@test all(@inferred degree(g, factor(a)) == length(@inferred neighbors(g,factor(a))) for a in factors(g))
@test all(@inferred degree(g, variable(i)) == length(@inferred neighbors(g,variable(i))) for i in variables(g))

@test length(collect(edges(g))) == ne(g)
@test length(collect(@inferred edges(g))) == @inferred ne(g)

@test all(all(src(e)==a for (e,a) in zip(inedges(g, variable(i)), neighbors(g, variable(i)))) for i in variables(g))
@test all(all(src(e)==i for (e,i) in zip(inedges(g, factor(a)), neighbors(g, factor(a)))) for a in factors(g))
Expand All @@ -24,6 +24,10 @@ g = FactorGraph(A)
@test_throws ArgumentError edge_indices(g, 1)
end

@testset "Type inference" begin
test_type_inference(g)
end

@testset "Broadcasting" begin
ids = [variable(i) for i in rand(1:nvariables(g), 5)]
@test degree.(g, ids) == degree.((g,), ids)
Expand Down
4 changes: 4 additions & 0 deletions test/infinite_regular_factorgraph.jl
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ g = InfiniteRegularFactorGraph(kᵢ, kₐ)
end
end

@testset "Type inference" begin
test_type_inference(g)
end

@testset "Edge indices" begin
@test all(variables(g)) do i
idx.(collect(inedges(g, variable(i)))) == edge_indices(g, variable(i))
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,22 @@ using Random
Aqua.test_ambiguities(FactorGraphs)
end

"""
Check type inference for functions that are likely to be called in hot loops
"""
function test_type_inference(g::AbstractFactorGraph)
i = rand(rng, @inferred variables(g))
a = rand(rng, @inferred factors(g))
di = @inferred degree(g, @inferred variable(i))
da = @inferred degree(g, @inferred factor(a))
∂i = @inferred neighbors(g, variable(i))
inei = @inferred inedges(g, variable(i))
outei = @inferred outedges(g, variable(i))
∂a = @inferred neighbors(g, factor(a))
inea = @inferred inedges(g, factor(a))
outea = @inferred outedges(g, factor(a))
end

include("factorgraph.jl")
include("generators.jl")
include("infinite_regular_factorgraph.jl")
Expand Down

0 comments on commit 81910a1

Please sign in to comment.