Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make graph dup do a deep copy. Closes #6
- Loading branch information
Showing
3 changed files
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
# required in "abstract_graph/graph" | ||
|
||
module AbstractGraph | ||
class Graph | ||
|
||
# does a deep copy of the graph | ||
def dup | ||
other = Graph.new | ||
other.vertices = @vertices.dup | ||
other | ||
end | ||
|
||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
require 'spec_helper' | ||
|
||
module AbstractGraph | ||
module Composition | ||
describe Graph do | ||
|
||
before :each do | ||
@graph = Graph.new | ||
@graph.add_vertex "MyVertex" | ||
end | ||
|
||
describe "#dup" do | ||
|
||
it "returns a graph object" do | ||
@graph.dup.should be_an_instance_of(Graph) | ||
end | ||
|
||
it "has a different id than the original graph" do | ||
@graph.object_id.should_not == @graph.dup.object_id | ||
end | ||
|
||
it "copies over the existing vertices" do | ||
graphdup = @graph.dup | ||
graphdup.has_vertex?( "MyVertex" ).should be_true | ||
end | ||
|
||
it "will not modify the original if it is modified" do | ||
graphdup = @graph.dup | ||
graphdup.vertices.clear | ||
@graph.vertices.size.should == 1 | ||
end | ||
|
||
end | ||
|
||
end | ||
end | ||
end |