Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Directed graph library specs #428

Merged
merged 4 commits into from Dec 5, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
24 changes: 24 additions & 0 deletions spec/ir/directed_graph/directed_graph_spec.rb
Expand Up @@ -26,6 +26,8 @@
@graph.addEdge(4,5,'bar')
@graph.removeEdge(4,5)
@graph.edges.size.should be 1
@graph.removeEdge(@graph.edges.to_a.last)
@graph.edges.size.should be 0
end

it "should not delete a non-existent edge from the graph" do
Expand Down Expand Up @@ -70,4 +72,26 @@
@graph.size.should be 4
end

it "should give all data in the graph" do
@graph.allData.size.should be 0
@graph.addEdge(1,2,'baz')
@graph.allData.each do |key|
@graph.findVertexFor(key).should_not be_nil
end
@graph.removeVertexFor(1)
@graph.allData.each do |key|
@graph.findVertexFor(key).should_not be_nil
end
end

it "should give data in the graph in the order in which it was inserted" do
@graph.getInorderData.to_a.size.should be 0
@graph.vertexFor(1)
@graph.getInorderData.to_a.should eq [1]
@graph.addEdge('foo','bar','baz')
@graph.getInorderData.to_a.should eq [1,'foo','bar']
@graph.removeVertexFor('foo')
@graph.getInorderData.to_a.should eq [1,'bar']
end

end
3 changes: 3 additions & 0 deletions src/org/jruby/ir/util/DirectedGraph.java
Expand Up @@ -64,6 +64,9 @@ public Vertex<T> findVertexFor(T data) {
return vertices.get(data);
}

/**
* @return vertex for given data. If vertex is not present it creates vertex and returns it.
*/
public Vertex<T> vertexFor(T data) {
Vertex vertex = vertices.get(data);

Expand Down