abstract_graph 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +18 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.rdoc +43 -0
- data/Rakefile +14 -0
- data/abstract_graph.gemspec +22 -0
- data/lib/abstract_graph.rb +10 -0
- data/lib/abstract_graph/composition.rb +11 -0
- data/lib/abstract_graph/composition/edge.rb +21 -0
- data/lib/abstract_graph/composition/edge/initialize.rb +18 -0
- data/lib/abstract_graph/composition/edge/is_coincident.rb +15 -0
- data/lib/abstract_graph/composition/uniquenamecollection.rb +21 -0
- data/lib/abstract_graph/composition/uniquenamecollection/add.rb +19 -0
- data/lib/abstract_graph/composition/uniquenamecollection/dup.rb +20 -0
- data/lib/abstract_graph/composition/uniquenamecollection/initialize.rb +14 -0
- data/lib/abstract_graph/composition/uniquenamecollection/link.rb +32 -0
- data/lib/abstract_graph/composition/uniquenamecollection/method_missing.rb +14 -0
- data/lib/abstract_graph/composition/vertex.rb +21 -0
- data/lib/abstract_graph/composition/vertex/delete.rb +12 -0
- data/lib/abstract_graph/composition/vertex/initialize.rb +14 -0
- data/lib/abstract_graph/graph.rb +25 -0
- data/lib/abstract_graph/graph/add_edge.rb +30 -0
- data/lib/abstract_graph/graph/add_vertex.rb +16 -0
- data/lib/abstract_graph/graph/delete_edge.rb +21 -0
- data/lib/abstract_graph/graph/delete_vertex.rb +21 -0
- data/lib/abstract_graph/graph/dup.rb +14 -0
- data/lib/abstract_graph/graph/has_edge.rb +13 -0
- data/lib/abstract_graph/graph/has_vertex.rb +13 -0
- data/lib/abstract_graph/graph/initialize.rb +16 -0
- data/lib/abstract_graph/version.rb +5 -0
- data/spec/abstract_graph/composition/edge/initialize_spec.rb +38 -0
- data/spec/abstract_graph/composition/edge/is_coincident_spec.rb +33 -0
- data/spec/abstract_graph/composition/edge/name_spec.rb +53 -0
- data/spec/abstract_graph/composition/edge/vertices_spec.rb +32 -0
- data/spec/abstract_graph/composition/edge_spec.rb +8 -0
- data/spec/abstract_graph/composition/uniquenamecollection/add_spec.rb +41 -0
- data/spec/abstract_graph/composition/uniquenamecollection/dup_spec.rb +29 -0
- data/spec/abstract_graph/composition/uniquenamecollection/initialize_spec.rb +21 -0
- data/spec/abstract_graph/composition/uniquenamecollection/link_spec.rb +67 -0
- data/spec/abstract_graph/composition/uniquenamecollection/method_missing_spec.rb +50 -0
- data/spec/abstract_graph/composition/uniquenamecollection_spec.rb +8 -0
- data/spec/abstract_graph/composition/vertex/delete_spec.rb +12 -0
- data/spec/abstract_graph/composition/vertex/initialize_spec.rb +45 -0
- data/spec/abstract_graph/composition/vertex/name_spec.rb +47 -0
- data/spec/abstract_graph/composition/vertex/value_spec.rb +46 -0
- data/spec/abstract_graph/composition/vertex_spec.rb +8 -0
- data/spec/abstract_graph/graph/add_edge_spec.rb +46 -0
- data/spec/abstract_graph/graph/add_vertex_spec.rb +29 -0
- data/spec/abstract_graph/graph/delete_edge_spec.rb +56 -0
- data/spec/abstract_graph/graph/delete_vertex_spec.rb +48 -0
- data/spec/abstract_graph/graph/dup_spec.rb +37 -0
- data/spec/abstract_graph/graph/has_edge_spec.rb +31 -0
- data/spec/abstract_graph/graph/has_vertex_spec.rb +27 -0
- data/spec/abstract_graph/graph/intialize_spec.rb +17 -0
- data/spec/abstract_graph/graph_spec.rb +6 -0
- data/spec/abstract_graph_spec.rb +7 -0
- data/spec/dummy_helper.rb +4 -0
- data/spec/spec_helper.rb +11 -0
- metadata +149 -0
@@ -0,0 +1,50 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe UniqueNameCollection do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@collection = UniqueNameCollection.new
|
9
|
+
@dummies = ('a'..'z').map do |c|
|
10
|
+
d = Dummy.new
|
11
|
+
d.name = c
|
12
|
+
d
|
13
|
+
end
|
14
|
+
@dummies.each do |d|
|
15
|
+
@collection.add d
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#size" do
|
20
|
+
|
21
|
+
it "should return the size of the collection" do
|
22
|
+
@collection.size.should == @dummies.count
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
describe "#has_key?(String)" do
|
28
|
+
|
29
|
+
it "should return whether the string is an included name" do
|
30
|
+
@collection.has_key?('d').should be_true
|
31
|
+
@collection.has_key?('saddy').should be_false
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#each_key" do
|
37
|
+
|
38
|
+
it "goes through every name in the collection" do
|
39
|
+
alphabet = ""
|
40
|
+
@collection.each_key do |c|
|
41
|
+
alphabet += c
|
42
|
+
end
|
43
|
+
alphabet.should == "abcdefghijklmnopqrstuvwxyz"
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,45 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Vertex do
|
6
|
+
|
7
|
+
describe "#new" do
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@vertex = Vertex.new
|
11
|
+
end
|
12
|
+
|
13
|
+
it "returns an object of class Vertex" do
|
14
|
+
@vertex.should be_an_instance_of(Vertex)
|
15
|
+
end
|
16
|
+
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#new(String)" do
|
20
|
+
|
21
|
+
before (:each) do
|
22
|
+
@vertex = Vertex.new("v1")
|
23
|
+
end
|
24
|
+
|
25
|
+
it "allows vertices to be initiated with a string name" do
|
26
|
+
@vertex.should_not be_nil
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "#new(String, Object)" do
|
32
|
+
|
33
|
+
before (:each) do
|
34
|
+
@vertex = Vertex.new("v1", "Hello")
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows vertices to be initiated with name and value" do
|
38
|
+
@vertex.should_not be_nil
|
39
|
+
end
|
40
|
+
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Vertex do
|
6
|
+
|
7
|
+
describe "#name" do
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@arbitraryString = "abitraryString"
|
11
|
+
@vertex = Vertex.new(@arbitraryString)
|
12
|
+
end
|
13
|
+
|
14
|
+
it "returns a String (Always)" do
|
15
|
+
@vertex.name.should be_an_instance_of(String)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "equals to the String in #new(String)" do
|
19
|
+
@vertex.name.should eql(@arbitraryString)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "equals to \"\" if nothing was passed as first param" do
|
23
|
+
@vertex2 = Vertex.new()
|
24
|
+
@vertex2.name.should eql("")
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#name=" do
|
30
|
+
|
31
|
+
before (:each) do
|
32
|
+
@vertex = Vertex.new()
|
33
|
+
end
|
34
|
+
|
35
|
+
it "names the vertex and retrieved by #name" do
|
36
|
+
@vertex.name = "Hello World"
|
37
|
+
@vertex.name.should eql("Hello World")
|
38
|
+
end
|
39
|
+
|
40
|
+
it "doesn't allow a non-String input" do
|
41
|
+
expect { @vertex.name = 100 }.to raise_error
|
42
|
+
end
|
43
|
+
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Vertex do
|
6
|
+
|
7
|
+
describe "#value" do
|
8
|
+
|
9
|
+
before (:each) do
|
10
|
+
@arbitraryString = "arbitraryString"
|
11
|
+
@arbitraryObject = Object.new
|
12
|
+
end
|
13
|
+
|
14
|
+
it "equals the object passed in #new(String, Object)" do
|
15
|
+
vertex = Vertex.new(@arbitraryString, @arbitraryObject)
|
16
|
+
vertex.value.should eql(@arbitraryObject)
|
17
|
+
end
|
18
|
+
|
19
|
+
it "equals to nil if nothing was passed as second param" do
|
20
|
+
vertex = Vertex.new()
|
21
|
+
vertex.value.should be_nil
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "#value=" do
|
27
|
+
|
28
|
+
before (:each) do
|
29
|
+
@vertex = Vertex.new()
|
30
|
+
end
|
31
|
+
|
32
|
+
it "allows us to change the value of the vertex" do
|
33
|
+
@vertex.value = 2
|
34
|
+
@vertex.value.should eql(2)
|
35
|
+
end
|
36
|
+
|
37
|
+
it "allows us to create two vertices of the same object" do
|
38
|
+
@vertex.value = "Hello"
|
39
|
+
@vertex2 = Vertex.new("v2", "Hello")
|
40
|
+
@vertex.value.should eql(@vertex2.value)
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@vertex1 = "Vertex1"
|
8
|
+
@vertex2 = "Vertex2"
|
9
|
+
@vertex3 = "Vertex3"
|
10
|
+
@graph = Graph.new
|
11
|
+
@graph.add_vertex @vertex1
|
12
|
+
@graph.add_vertex @vertex2
|
13
|
+
@graph.add_vertex @vertex3
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#add_edge(String, String, String)" do
|
17
|
+
|
18
|
+
it "returns an object of class Graph" do
|
19
|
+
@graph.add_edge( "MyEdge", @vertex1, @vertex2 ).should be_an_instance_of(Graph)
|
20
|
+
end
|
21
|
+
|
22
|
+
it "throws an exception when adding edge of existing name" do
|
23
|
+
@graph.add_edge( "MyEdge", @vertex1, @vertex2 ).should be_an_instance_of(Graph)
|
24
|
+
expect { @graph.add_edge( "MyEdge", @vertex1, @vertex3 ) }.to raise_error
|
25
|
+
expect { @graph.add_edge( @vertex1, @vertex1, @vertex3) }.to raise_error
|
26
|
+
end
|
27
|
+
|
28
|
+
it "doesn't allow an edge to be a loop" do
|
29
|
+
expect { @graph.add_edge( "MyEdge", @vertex1, @vertex1 ) }.to raise_error
|
30
|
+
end
|
31
|
+
|
32
|
+
it "doesn't allow multiple edges" do
|
33
|
+
@graph.add_edge( "MyEdge", @vertex1, @vertex2 )
|
34
|
+
expect { @graph.add_edge( "MyEdge2", @vertex1, @vertex2 ) }.to raise_error
|
35
|
+
end
|
36
|
+
|
37
|
+
it "returns nil if any of the vertices don't exist" do
|
38
|
+
@graph.add_edge( "MyEdge", @vertex1, "randomString" ).should be_nil
|
39
|
+
@graph.add_edge( "MyEdge", "randomString", @vertex1 ).should be_nil
|
40
|
+
@graph.edges["MyEdge"].should be_nil
|
41
|
+
end
|
42
|
+
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@graph = Graph.new
|
8
|
+
end
|
9
|
+
|
10
|
+
describe "#add_vertex(String)" do
|
11
|
+
|
12
|
+
it "returns an object of class Graph" do
|
13
|
+
@graph.add_vertex( "MyVertex" ).should be_an_instance_of(Graph)
|
14
|
+
end
|
15
|
+
|
16
|
+
it "throws an exception when adding vertex of existing name" do
|
17
|
+
@graph.add_vertex( "MyVertex" )
|
18
|
+
expect { @graph.add_vertex( "MyVertex" ) }.to raise_error
|
19
|
+
end
|
20
|
+
|
21
|
+
it "allows two different graphs to have the same names" do
|
22
|
+
graph2 = Graph.new
|
23
|
+
@graph.add_vertex( "MyVertex" )
|
24
|
+
expect { graph2.add_vertex( "MyVertex" ) }.to_not raise_error
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :all do
|
7
|
+
@vertex1 = "Vertex1"
|
8
|
+
@vertex2 = "Vertex2"
|
9
|
+
@edge = "MyEdge"
|
10
|
+
end
|
11
|
+
|
12
|
+
before :each do
|
13
|
+
@graph = Graph.new
|
14
|
+
@graph.add_vertex @vertex1
|
15
|
+
@graph.add_vertex @vertex2
|
16
|
+
@graph.add_edge @edge, @vertex1, @vertex2
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#delete_edge(String)" do
|
20
|
+
|
21
|
+
it "returns an object of class Graph" do
|
22
|
+
@graph.delete_edge( @edge ).should be_an_instance_of(Graph)
|
23
|
+
end
|
24
|
+
|
25
|
+
it "returns an object without the edge" do
|
26
|
+
nextGraph = @graph.delete_edge( @edge )
|
27
|
+
nextGraph.has_edge?( @edge ).should be_false
|
28
|
+
end
|
29
|
+
|
30
|
+
it "does not modify the original graph" do
|
31
|
+
@graph.delete_edge( @edge )
|
32
|
+
@graph.has_edge?( @edge ).should be_true
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#delete_edge!(String)" do
|
38
|
+
|
39
|
+
it "returns an object of class Graph" do
|
40
|
+
@graph.delete_edge!( @edge ).should be_an_instance_of(Graph)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "returns an object without the edge" do
|
44
|
+
nextGraph = @graph.delete_edge!( @edge )
|
45
|
+
nextGraph.has_edge?( @edge ).should be_false
|
46
|
+
end
|
47
|
+
|
48
|
+
it "removes the edge from the original graph" do
|
49
|
+
@graph.delete_edge!( @edge )
|
50
|
+
@graph.has_edge?( @edge ).should be_false
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
describe Graph do
|
5
|
+
|
6
|
+
before :each do
|
7
|
+
@graph = Graph.new
|
8
|
+
@graph.add_vertex "MyVertex"
|
9
|
+
end
|
10
|
+
|
11
|
+
describe "#delete_vertex(String)" do
|
12
|
+
|
13
|
+
it "returns an object of class Graph" do
|
14
|
+
@graph.delete_vertex( "MyVertex" ).should be_an_instance_of(Graph)
|
15
|
+
end
|
16
|
+
|
17
|
+
it "returns an object without the vertex" do
|
18
|
+
nextGraph = @graph.delete_vertex( "MyVertex" )
|
19
|
+
nextGraph.has_vertex?( "MyVertex" ).should be_false
|
20
|
+
end
|
21
|
+
|
22
|
+
it "does not modify the original graph" do
|
23
|
+
@graph.delete_vertex( "MyVertex" )
|
24
|
+
@graph.has_vertex?( "MyVertex" ).should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#delete_vertex!(String)" do
|
30
|
+
|
31
|
+
it "returns an object of class Graph" do
|
32
|
+
@graph.delete_vertex!( "MyVertex" ).should be_an_instance_of(Graph)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "returns an object without the vertex" do
|
36
|
+
nextGraph = @graph.delete_vertex!( "MyVertex" )
|
37
|
+
nextGraph.has_vertex?( "MyVertex" ).should be_false
|
38
|
+
end
|
39
|
+
|
40
|
+
it "removes the vertex from the original graph" do
|
41
|
+
@graph.delete_vertex!( "MyVertex" )
|
42
|
+
@graph.has_vertex?( "MyVertex" ).should be_false
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
module AbstractGraph
|
4
|
+
module Composition
|
5
|
+
describe Graph do
|
6
|
+
|
7
|
+
before :each do
|
8
|
+
@graph = Graph.new
|
9
|
+
@graph.add_vertex "MyVertex"
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#dup" do
|
13
|
+
|
14
|
+
it "returns a graph object" do
|
15
|
+
@graph.dup.should be_an_instance_of(Graph)
|
16
|
+
end
|
17
|
+
|
18
|
+
it "has a different id than the original graph" do
|
19
|
+
@graph.object_id.should_not == @graph.dup.object_id
|
20
|
+
end
|
21
|
+
|
22
|
+
it "copies over the existing vertices" do
|
23
|
+
graphdup = @graph.dup
|
24
|
+
graphdup.has_vertex?( "MyVertex" ).should be_true
|
25
|
+
end
|
26
|
+
|
27
|
+
it "will not modify the original if it is modified" do
|
28
|
+
graphdup = @graph.dup
|
29
|
+
graphdup.vertices.clear
|
30
|
+
@graph.vertices.size.should == 1
|
31
|
+
end
|
32
|
+
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|