metagraphoptimization.jl/test/known_graphs.jl

97 lines
2.4 KiB
Julia
Raw Normal View History

2023-06-27 20:05:27 +02:00
using Random
2023-06-22 17:24:35 +02:00
function test_known_graph(name::String, n, fusion_test=true)
@testset "Test $name Graph ($n)" begin
2023-08-23 13:38:02 +02:00
graph = parse_abc(joinpath(@__DIR__, "..", "input", "$name.txt"))
props = graph_properties(graph)
2023-06-26 14:24:14 +02:00
if (fusion_test)
test_node_fusion(graph)
end
test_random_walk(graph, n)
end
2023-06-26 14:24:14 +02:00
end
2023-06-27 20:05:27 +02:00
function test_node_fusion(g::DAG)
@testset "Test Node Fusion" begin
2023-06-26 14:24:14 +02:00
props = graph_properties(g)
2023-06-29 13:57:45 +02:00
options = get_operations(g)
2023-06-26 14:24:14 +02:00
nodes_number = length(g.nodes)
data = props.data
compute_effort = props.compute_effort
2023-06-27 18:38:26 +02:00
while !isempty(options.nodeFusions)
fusion = first(options.nodeFusions)
2023-06-27 18:38:26 +02:00
@test typeof(fusion) <: NodeFusion
push_operation!(g, fusion)
2023-06-26 14:24:14 +02:00
props = graph_properties(g)
@test props.data < data
@test props.compute_effort == compute_effort
nodes_number = length(g.nodes)
data = props.data
compute_effort = props.compute_effort
2023-06-29 13:57:45 +02:00
options = get_operations(g)
2023-06-26 14:24:14 +02:00
end
2023-06-27 20:05:27 +02:00
end
end
2023-06-27 20:05:27 +02:00
function test_random_walk(g::DAG, n::Int64)
@testset "Test Random Walk ($n)" begin
2023-06-27 20:05:27 +02:00
# the purpose here is to do "random" operations and reverse them again and validate that the graph stays the same and doesn't diverge
reset_graph!(g)
2023-08-24 14:44:21 +02:00
@test is_valid(g)
2023-06-27 20:05:27 +02:00
properties = graph_properties(g)
for i = 1:n
# choose push or pop
if rand(Bool)
# push
2023-06-29 13:57:45 +02:00
opt = get_operations(g)
2023-06-27 20:05:27 +02:00
# choose one of fuse/split/reduce
option = rand(1:3)
if option == 1 && !isempty(opt.nodeFusions)
2023-07-03 01:15:04 +02:00
push_operation!(g, rand(collect(opt.nodeFusions)))
2023-06-27 20:05:27 +02:00
elseif option == 2 && !isempty(opt.nodeReductions)
2023-07-03 01:15:04 +02:00
push_operation!(g, rand(collect(opt.nodeReductions)))
2023-08-17 21:53:55 +02:00
elseif option == 3 && !isempty(opt.nodeSplits)
2023-07-03 01:15:04 +02:00
push_operation!(g, rand(collect(opt.nodeSplits)))
2023-06-27 20:05:27 +02:00
else
i = i - 1
2023-06-27 20:05:27 +02:00
end
else
# pop
if (can_pop(g))
pop_operation!(g)
else
i = i - 1
2023-06-27 20:05:27 +02:00
end
end
end
reset_graph!(g)
2023-06-26 14:24:14 +02:00
2023-08-24 14:44:21 +02:00
@test is_valid(g)
2023-06-27 20:05:27 +02:00
@test properties == graph_properties(g)
2023-06-22 17:24:35 +02:00
end
end
Random.seed!(0)
@testset "Test Known ABC-Graphs" begin
test_known_graph("AB->AB", 10000)
test_known_graph("AB->ABBB", 10000)
test_known_graph("AB->ABBBBB", 1000, false)
end
println("Known Graph Testing Complete!")