summaryrefslogtreecommitdiff
path: root/tests/modules/graph/test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/modules/graph/test.lua')
-rw-r--r--tests/modules/graph/test.lua31
1 files changed, 31 insertions, 0 deletions
diff --git a/tests/modules/graph/test.lua b/tests/modules/graph/test.lua
new file mode 100644
index 000000000..7f0a253f2
--- /dev/null
+++ b/tests/modules/graph/test.lua
@@ -0,0 +1,31 @@
+import("core.base.graph")
+
+function test_topological_sort(t)
+ local edges = {
+ {0, 5},
+ {0, 2},
+ {0, 1},
+ {3, 6},
+ {3, 5},
+ {3, 4},
+ {5, 4},
+ {6, 4},
+ {6, 0},
+ {3, 2},
+ {1, 4},
+ }
+ local dag = graph.new(true)
+ for _, e in ipairs(edges) do
+ dag:add_edge(e[1], e[2])
+ end
+ local order_path = dag:topological_sort()
+ local orders = {}
+ for i, v in ipairs(order_path) do
+ orders[v] = i
+ end
+
+ for _, e in ipairs(edges) do
+ t:require(orders[e[1]] < orders[e[2]])
+ end
+end
+