Skip to content
This repository has been archived by the owner on May 4, 2024. It is now read-only.

Commit

Permalink
Add tests for graph tsort
Browse files Browse the repository at this point in the history
  • Loading branch information
floriandejonckheere committed Mar 30, 2024
1 parent 884a045 commit e437a35
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/mosaik/graph/graph.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,13 @@ def remove_vertex(vertex)
end

def tsort_each_node(&)
vertices.keys.each(&)
vertices.values.each(&)
end

def tsort_each_child(vertex, &)
vertices[vertex].edges.each(&)
vertices[vertex.value].edges.each_key do |key|
yield vertices[key]
end
end

def inspect
Expand Down
19 changes: 19 additions & 0 deletions spec/mosaik/graph/graph_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -247,4 +247,23 @@
expect(graph.vertices).to be_empty
end
end

describe "#tsort" do
it "topologically sorts the vertices" do
vertex1 = build(:vertex, value: "vertex1")
vertex2 = build(:vertex, value: "vertex2")
vertex3 = build(:vertex, value: "vertex3")

graph.add_vertex(vertex1)
graph.add_vertex(vertex2)
graph.add_vertex(vertex3)

expect(graph.tsort.map(&:value)).to eq ["vertex1", "vertex2", "vertex3"]

graph.add_directed_edge(vertex1, vertex2)
graph.add_directed_edge(vertex2, vertex3)

expect(graph.tsort.map(&:value)).to eq ["vertex3", "vertex2", "vertex1"]
end
end
end

0 comments on commit e437a35

Please sign in to comment.