about summary refs log tree commit diff stats
path: root/test/core/graph.py
diff options
context:
space:
mode:
authorAjax <commial@gmail.com>2016-01-20 10:11:41 +0100
committerAjax <commial@gmail.com>2016-01-27 11:12:18 +0100
commit51ec4ba8e1d9c0ddfaf23a25b3827c2bbff4e067 (patch)
tree2628c7521725100c8975d5c6a1733a4cd9abfbfe /test/core/graph.py
parentc309d19b1de9aa1bf0bcc56169531fcdce99219c (diff)
downloadmiasm-51ec4ba8e1d9c0ddfaf23a25b3827c2bbff4e067.tar.gz
miasm-51ec4ba8e1d9c0ddfaf23a25b3827c2bbff4e067.zip
Add regression tests for MatchGraph
Conflicts:
	test/core/graph.py
Diffstat (limited to 'test/core/graph.py')
-rw-r--r--test/core/graph.py68
1 files changed, 68 insertions, 0 deletions
diff --git a/test/core/graph.py b/test/core/graph.py
index 33a2fc6f..e148d70f 100644
--- a/test/core/graph.py
+++ b/test/core/graph.py
@@ -217,3 +217,71 @@ for edge in graph3.edges():
     assert edge in graph4.edges()
 assert graph4.nodes() == graph.nodes().union(graph3.nodes())
 assert sorted(graph4.edges()) == sorted(graph.edges() + graph3.edges())
+
+# MatchGraph
+
+## Build a MatchGraph using MatchGraphJoker
+j1 = MatchGraphJoker(name="dad")
+j2 = MatchGraphJoker(name="son")
+### Check '>>' helper
+matcher = j1 >> j2 >> j1
+### Check __str__
+print matcher
+### Ensure form
+assert isinstance(matcher, MatchGraph)
+assert len(matcher.nodes()) == 2
+assert len(matcher.edges()) == 2
+
+## Match a simple graph
+graph = DiGraph()
+graph.add_edge(1, 2)
+graph.add_edge(2, 1)
+graph.add_edge(2, 3)
+sols = list(matcher.match(graph))
+assert len(sols) == 0
+
+## Modify restrictions
+j2 = MatchGraphJoker(name="son", restrict_out=False)
+matcher = j1 >> j2 >> j1
+sols = list(matcher.match(graph))
+assert len(sols) == 1
+assert sols[0] == {j1: 1,
+                   j2: 2}
+
+## Check solution combinaison (ie a -> b and b -> a)
+j1 = MatchGraphJoker(name="dad", restrict_out=False)
+matcher = j1 >> j2 >> j1
+sols = list(matcher.match(graph))
+assert len(sols) == 2
+assert len([sol for sol in sols if sol[j1] == 1]) == 1
+assert len([sol for sol in sols if sol[j1] == 2]) == 1
+
+## Check filter
+j2 = MatchGraphJoker(name="son", restrict_out=False, filt=lambda node: node < 2)
+matcher = j1 >> j2 >> j1
+sols = list(matcher.match(graph))
+assert len(sols) == 1
+assert sols[0] == {j1: 2,
+                   j2: 1}
+
+## Check building with 'add' helper
+j1 = MatchGraphJoker(name="dad")
+j2 = MatchGraphJoker(name="son")
+j3 = MatchGraphJoker(name="sonson", restrict_in=False)
+matcher = j1 >> j2
+matcher += j2 >> j3
+assert isinstance(matcher, MatchGraph)
+assert len(matcher.nodes()) == 3
+assert len(matcher.edges()) == 2
+
+## Check restrict_in
+graph = DiGraph()
+graph.add_edge(1, 2)
+graph.add_edge(2, 3)
+graph.add_edge(4, 3)
+sols = list(matcher.match(graph))
+assert len(sols) == 1
+assert sols[0] == {j1: 1,
+                   j2: 2,
+                   j3: 3}
+