summaryrefslogtreecommitdiff
path: root/tests/modules/graph/test.lua
diff options
context:
space:
mode:
authorruki <[email protected]>2023-09-30 18:26:05 +0800
committerGitHub <[email protected]>2023-09-30 18:26:05 +0800
commit23f598d853e16c25360b70a082629bdd0d35eedd (patch)
tree165fd007f051fdc5ba623f72d026003b8f222657 /tests/modules/graph/test.lua
parent1c934c2e16c05bdfee56de5c187fdcc5d94fac98 (diff)
parentf584fb389af355484c7bbb257c0c5cd77c7f6c31 (diff)
Merge pull request #4250 from xmake-io/links
Improve link mechanism and order
Diffstat (limited to 'tests/modules/graph/test.lua')
-rw-r--r--tests/modules/graph/test.lua56
1 files changed, 56 insertions, 0 deletions
diff --git a/tests/modules/graph/test.lua b/tests/modules/graph/test.lua
new file mode 100644
index 000000000..63095be12
--- /dev/null
+++ b/tests/modules/graph/test.lua
@@ -0,0 +1,56 @@
+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
+
+ dag = dag:reverse()
+ order_path = dag:topological_sort()
+ 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
+
+function test_find_cycle(t)
+ local edges = {
+ {9, 1},
+ {1, 6},
+ {6, 0},
+ {0, 1},
+ {4, 5}
+ }
+ local dag = graph.new(true)
+ for _, e in ipairs(edges) do
+ dag:add_edge(e[1], e[2])
+ end
+ local cycle = dag:find_cycle()
+ t:are_equal(cycle, {1, 6, 0})
+end
+