-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from Herb-AI/contains-subtree
Add tests for changes in HerbConstraints
- Loading branch information
Showing
7 changed files
with
279 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
using HerbCore, HerbGrammar, HerbConstraints | ||
|
||
@testset verbose=true "Constraints" begin | ||
|
||
function new_grammar() | ||
grammar = @csgrammar begin | ||
Int = 1 | ||
Int = x | ||
Int = -Int | ||
Int = Int + Int | ||
Int = Int * Int | ||
end | ||
clearconstraints!(grammar) | ||
return grammar | ||
end | ||
|
||
contains_subtree = ContainsSubtree(RuleNode(4, [ | ||
RuleNode(1), | ||
RuleNode(1) | ||
])) | ||
|
||
contains_subtree2 = ContainsSubtree(RuleNode(4, [ | ||
RuleNode(4, [ | ||
VarNode(:a), | ||
RuleNode(2) | ||
]), | ||
VarNode(:a) | ||
])) | ||
|
||
contains = Contains(2) | ||
|
||
forbidden_sequence = ForbiddenSequence([4, 5]) | ||
|
||
forbidden_sequence2 = ForbiddenSequence([4, 5], ignore_if=[3]) | ||
|
||
forbidden = Forbidden(RuleNode(3, [RuleNode(3, [VarNode(:a)])])) | ||
|
||
forbidden2 = Forbidden(RuleNode(4, [ | ||
VarNode(:a), | ||
VarNode(:a) | ||
])) | ||
|
||
ordered = Ordered(RuleNode(5, [ | ||
VarNode(:a), | ||
VarNode(:b) | ||
]), [:a, :b]) | ||
|
||
unique = Unique(2) | ||
|
||
@testset "fix_point_running related bug" begin | ||
# post contains_subtree2 | ||
# propagate contains_subtree2 | ||
# schedule forbidden2 | ||
# propagate forbidden2 | ||
|
||
grammar = new_grammar() | ||
addconstraint!(grammar, contains_subtree) | ||
addconstraint!(grammar, contains_subtree2) | ||
addconstraint!(grammar, forbidden2) | ||
|
||
partial_program = UniformHole(BitVector((0, 0, 0, 1, 1)), [ | ||
UniformHole(BitVector((0, 0, 0, 1, 1)), [ | ||
UniformHole(BitVector((1, 1, 0, 0, 0)), []), | ||
UniformHole(BitVector((1, 1, 0, 0, 0)), []) | ||
]), | ||
UniformHole(BitVector((0, 0, 0, 1, 1)), [ | ||
UniformHole(BitVector((0, 0, 0, 1, 1)), [ | ||
UniformHole(BitVector((1, 1, 0, 0, 0)), []), | ||
UniformHole(BitVector((1, 1, 0, 0, 0)), []) | ||
]) | ||
UniformHole(BitVector((1, 1, 0, 0, 0)), []) | ||
]) | ||
]) | ||
|
||
iterator = BFSIterator(grammar, partial_program, max_size=9) | ||
@test length(iterator) == 0 | ||
end | ||
|
||
all_constraints = [ | ||
("ContainsSubtree", contains_subtree), | ||
("ContainsSubtree2", contains_subtree2), | ||
("Contains", contains), | ||
("ForbiddenSequence", forbidden_sequence), | ||
("ForbiddenSequence2", forbidden_sequence2), | ||
("Forbidden", forbidden), | ||
("Forbidden2", forbidden2), | ||
("Ordered", ordered), | ||
("Unique", unique) | ||
] | ||
|
||
@testset "1 constraint" begin | ||
# test all constraints individually, the constraints are chosen to prune the program space non-trivially | ||
@testset "$name" for (name, constraint) ∈ all_constraints | ||
test_constraint!(new_grammar(), constraint, max_size=6, allow_trivial=false) | ||
end | ||
end | ||
|
||
@testset "$n constraints" for n ∈ 2:5 | ||
# test constraint interactions by randomly sampling constraints | ||
for _ ∈ 1:10 | ||
indices = randperm(length(all_constraints))[1:n] | ||
names = [name for (name, _) ∈ all_constraints[indices]] | ||
constraints = [constraint for (_, constraint) ∈ all_constraints[indices]] | ||
|
||
@testset "$names" begin | ||
test_constraints!(new_grammar(), constraints, max_size=6, allow_trivial=true) | ||
end | ||
end | ||
end | ||
|
||
@testset "all constraints" begin | ||
# all constraints combined, no valid solution exists | ||
grammar = new_grammar() | ||
for (_, constraint) ∈ all_constraints | ||
addconstraint!(grammar, constraint) | ||
end | ||
iter = BFSIterator(grammar, :Int, max_size=10) | ||
@test length(iter) == 0 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
using HerbCore, HerbGrammar, HerbConstraints | ||
|
||
@testset verbose=true "ContainsSubtree" begin | ||
@testset "Minimal Example" begin | ||
grammar = @csgrammar begin | ||
Int = x | ||
Int = Int + Int | ||
Int = Int + Int | ||
Int = 1 | ||
end | ||
|
||
constraint = ContainsSubtree( | ||
RuleNode(2, [ | ||
RuleNode(1), | ||
RuleNode(2, [ | ||
RuleNode(1), | ||
RuleNode(1) | ||
]) | ||
]) | ||
) | ||
|
||
test_constraint!(grammar, constraint, max_size=6) | ||
end | ||
|
||
@testset "1 VarNode" begin | ||
grammar = @csgrammar begin | ||
Int = x | ||
Int = Int + Int | ||
Int = Int + Int | ||
Int = 1 | ||
end | ||
|
||
constraint = ContainsSubtree( | ||
RuleNode(2, [ | ||
RuleNode(1), | ||
VarNode(:x) | ||
]) | ||
) | ||
|
||
test_constraint!(grammar, constraint, max_size=6) | ||
end | ||
|
||
@testset "2 VarNodes" begin | ||
grammar = @csgrammar begin | ||
Int = x | ||
Int = Int + Int | ||
Int = Int + Int | ||
Int = 1 | ||
end | ||
|
||
constraint = ContainsSubtree( | ||
RuleNode(2, [ | ||
VarNode(:x), | ||
VarNode(:x) | ||
]) | ||
) | ||
|
||
test_constraint!(grammar, constraint, max_size=6) | ||
end | ||
|
||
|
||
@testset "No StateHoles" begin | ||
grammar = @csgrammar begin | ||
Int = x | ||
Int = Int + Int | ||
end | ||
|
||
constraint = ContainsSubtree( | ||
RuleNode(2, [ | ||
RuleNode(1), | ||
RuleNode(2, [ | ||
RuleNode(1), | ||
RuleNode(1) | ||
]) | ||
]) | ||
) | ||
|
||
test_constraint!(grammar, constraint, max_size=6) | ||
end | ||
|
||
@testset "Permutations" begin | ||
# A grammar that represents all permutations of (1, 2, 3, 4, 5) | ||
grammar = @csgrammar begin | ||
N = |(1:5) | ||
Permutation = (N, N, N, N, N) | ||
end | ||
addconstraint!(grammar, ContainsSubtree(RuleNode(1))) | ||
addconstraint!(grammar, ContainsSubtree(RuleNode(2))) | ||
addconstraint!(grammar, ContainsSubtree(RuleNode(3))) | ||
addconstraint!(grammar, ContainsSubtree(RuleNode(4))) | ||
addconstraint!(grammar, ContainsSubtree(RuleNode(5))) | ||
|
||
# There are 5! = 120 permutations of 5 distinct elements | ||
iter = BFSIterator(grammar, :Permutation) | ||
@test length(iter) == 120 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters