diff options
| author | ruki <[email protected]> | 2023-09-29 00:43:55 +0800 |
|---|---|---|
| committer | ruki <[email protected]> | 2023-09-29 00:43:55 +0800 |
| commit | 1ad7b371e69832be96345d91fb73cd21209803cf (patch) | |
| tree | 3680d5e89df9c3cfc454eeb42c2340a6a32ba668 | |
| parent | 2f77dcb4ce35c909ee289bf47c3411b0aa7b0d21 (diff) | |
add graph test
| -rw-r--r-- | tests/modules/graph/test.lua | 31 | ||||
| -rw-r--r-- | xmake/core/base/graph.lua | 6 |
2 files changed, 35 insertions, 2 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 + diff --git a/xmake/core/base/graph.lua b/xmake/core/base/graph.lua index c4b28cd37..86746dfa9 100644 --- a/xmake/core/base/graph.lua +++ b/xmake/core/base/graph.lua @@ -127,9 +127,11 @@ function graph:topological_sort() table.insert(order_vertices, v) end for _, v in ipairs(self:vertices()) do - graph_topological_sort_dfs(v) + if marked[v] == false then + graph_topological_sort_dfs(v) + end end - return order_vertices + return table.reverse(order_vertices) end -- get edges |
