graph.njae 0.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -0
- data/.rspec +1 -0
- data/Gemfile +14 -0
- data/Gemfile.lock +30 -0
- data/LICENSE.txt +20 -0
- data/README.rdoc +19 -0
- data/Rakefile +51 -0
- data/VERSION +1 -0
- data/graph.njae.gemspec +68 -0
- data/lib/graph.njae.rb +3 -0
- data/lib/graph/edge.rb +46 -0
- data/lib/graph/graph.rb +27 -0
- data/lib/graph/vertex.rb +41 -0
- data/spec/graph/edge_spec.rb +100 -0
- data/spec/graph/graph_spec.rb +65 -0
- data/spec/graph/vertex_spec.rb +101 -0
- data/spec/spec_helper.rb +12 -0
- metadata +124 -0
data/.document
ADDED
data/.rspec
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
--color
|
data/Gemfile
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
source "http://rubygems.org"
|
2
|
+
# Add dependencies required to use your gem here.
|
3
|
+
# Example:
|
4
|
+
# gem "activesupport", ">= 2.3.5"
|
5
|
+
|
6
|
+
# Add dependencies to develop your gem here.
|
7
|
+
# Include everything needed to run rake, tests, features, etc.
|
8
|
+
group :development do
|
9
|
+
gem "rspec", "~> 2.6.0"
|
10
|
+
gem "bundler", "~> 1.0.0"
|
11
|
+
gem "jeweler", "~> 1.6.2"
|
12
|
+
gem "rcov", ">= 0"
|
13
|
+
gem "rdoc"
|
14
|
+
end
|
data/Gemfile.lock
ADDED
@@ -0,0 +1,30 @@
|
|
1
|
+
GEM
|
2
|
+
remote: http://rubygems.org/
|
3
|
+
specs:
|
4
|
+
diff-lcs (1.1.2)
|
5
|
+
git (1.2.5)
|
6
|
+
jeweler (1.6.2)
|
7
|
+
bundler (~> 1.0)
|
8
|
+
git (>= 1.2.5)
|
9
|
+
rake
|
10
|
+
rake (0.9.2)
|
11
|
+
rcov (0.9.9)
|
12
|
+
rdoc (3.7)
|
13
|
+
rspec (2.6.0)
|
14
|
+
rspec-core (~> 2.6.0)
|
15
|
+
rspec-expectations (~> 2.6.0)
|
16
|
+
rspec-mocks (~> 2.6.0)
|
17
|
+
rspec-core (2.6.4)
|
18
|
+
rspec-expectations (2.6.0)
|
19
|
+
diff-lcs (~> 1.1.2)
|
20
|
+
rspec-mocks (2.6.0)
|
21
|
+
|
22
|
+
PLATFORMS
|
23
|
+
ruby
|
24
|
+
|
25
|
+
DEPENDENCIES
|
26
|
+
bundler (~> 1.0.0)
|
27
|
+
jeweler (~> 1.6.2)
|
28
|
+
rcov
|
29
|
+
rdoc
|
30
|
+
rspec (~> 2.6.0)
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2011 Neil Smith
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.rdoc
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
= graph.njae
|
2
|
+
|
3
|
+
A simple graph library in Ruby.
|
4
|
+
|
5
|
+
== Contributing to graph.njae
|
6
|
+
|
7
|
+
* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
|
8
|
+
* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
|
9
|
+
* Fork the project
|
10
|
+
* Start a feature/bugfix branch
|
11
|
+
* Commit and push until you are happy with your contribution
|
12
|
+
* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
|
13
|
+
* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.
|
14
|
+
|
15
|
+
== Copyright
|
16
|
+
|
17
|
+
Copyright (c) 2011 Neil Smith. See LICENSE.txt for
|
18
|
+
further details.
|
19
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "graph.njae"
|
18
|
+
gem.homepage = "http://github.com/NeilNjae/graph.njae"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{A simple graph library}
|
21
|
+
gem.description = %Q{A simple graph library}
|
22
|
+
gem.email = "neil.github@njae.me.uk"
|
23
|
+
gem.authors = ["Neil Smith"]
|
24
|
+
# dependencies defined in Gemfile
|
25
|
+
end
|
26
|
+
Jeweler::RubygemsDotOrgTasks.new
|
27
|
+
|
28
|
+
require 'rspec/core'
|
29
|
+
require 'rspec/core/rake_task'
|
30
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
31
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
32
|
+
end
|
33
|
+
|
34
|
+
RSpec::Core::RakeTask.new(:rcov) do |spec|
|
35
|
+
spec.pattern = 'spec/**/*_spec.rb'
|
36
|
+
spec.rcov = true
|
37
|
+
end
|
38
|
+
|
39
|
+
task :default => :spec
|
40
|
+
|
41
|
+
# require 'rake/rdoctask'
|
42
|
+
# Rake::RDocTask.new do |rdoc|
|
43
|
+
require 'rdoc/task'
|
44
|
+
RDoc::Task.new do |rdoc|
|
45
|
+
version = File.exist?('VERSION') ? File.read('VERSION') : ""
|
46
|
+
|
47
|
+
rdoc.rdoc_dir = 'rdoc'
|
48
|
+
rdoc.title = "graph.njae #{version}"
|
49
|
+
rdoc.rdoc_files.include('README*')
|
50
|
+
rdoc.rdoc_files.include('lib/**/*.rb')
|
51
|
+
end
|
data/VERSION
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
0.0.0
|
data/graph.njae.gemspec
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
# Generated by jeweler
|
2
|
+
# DO NOT EDIT THIS FILE DIRECTLY
|
3
|
+
# Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
|
4
|
+
# -*- encoding: utf-8 -*-
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = %q{graph.njae}
|
8
|
+
s.version = "0.0.0"
|
9
|
+
|
10
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
|
+
s.authors = ["Neil Smith"]
|
12
|
+
s.date = %q{2011-09-23}
|
13
|
+
s.description = %q{A simple graph library}
|
14
|
+
s.email = %q{neil.github@njae.me.uk}
|
15
|
+
s.extra_rdoc_files = [
|
16
|
+
"LICENSE.txt",
|
17
|
+
"README.rdoc"
|
18
|
+
]
|
19
|
+
s.files = [
|
20
|
+
".document",
|
21
|
+
".rspec",
|
22
|
+
"Gemfile",
|
23
|
+
"Gemfile.lock",
|
24
|
+
"LICENSE.txt",
|
25
|
+
"README.rdoc",
|
26
|
+
"Rakefile",
|
27
|
+
"VERSION",
|
28
|
+
"graph.njae.gemspec",
|
29
|
+
"lib/graph.njae.rb",
|
30
|
+
"lib/graph/edge.rb",
|
31
|
+
"lib/graph/graph.rb",
|
32
|
+
"lib/graph/vertex.rb",
|
33
|
+
"spec/graph/edge_spec.rb",
|
34
|
+
"spec/graph/graph_spec.rb",
|
35
|
+
"spec/graph/vertex_spec.rb",
|
36
|
+
"spec/spec_helper.rb"
|
37
|
+
]
|
38
|
+
s.homepage = %q{http://github.com/NeilNjae/graph.njae}
|
39
|
+
s.licenses = ["MIT"]
|
40
|
+
s.require_paths = ["lib"]
|
41
|
+
s.rubygems_version = %q{1.6.2}
|
42
|
+
s.summary = %q{A simple graph library}
|
43
|
+
|
44
|
+
if s.respond_to? :specification_version then
|
45
|
+
s.specification_version = 3
|
46
|
+
|
47
|
+
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
48
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.6.0"])
|
49
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.0"])
|
50
|
+
s.add_development_dependency(%q<jeweler>, ["~> 1.6.2"])
|
51
|
+
s.add_development_dependency(%q<rcov>, [">= 0"])
|
52
|
+
s.add_development_dependency(%q<rdoc>, [">= 0"])
|
53
|
+
else
|
54
|
+
s.add_dependency(%q<rspec>, ["~> 2.6.0"])
|
55
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
56
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
57
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
58
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
59
|
+
end
|
60
|
+
else
|
61
|
+
s.add_dependency(%q<rspec>, ["~> 2.6.0"])
|
62
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.0"])
|
63
|
+
s.add_dependency(%q<jeweler>, ["~> 1.6.2"])
|
64
|
+
s.add_dependency(%q<rcov>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rdoc>, [">= 0"])
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
data/lib/graph.njae.rb
ADDED
data/lib/graph/edge.rb
ADDED
@@ -0,0 +1,46 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# A simple graph library
|
4
|
+
|
5
|
+
module Graph
|
6
|
+
|
7
|
+
# An edge (or multiedge) in a graph. The edge can have arbitrary attributes,
|
8
|
+
# treated as method names.
|
9
|
+
#
|
10
|
+
# Each connection is handled by a Graph::Connection object, so that each end
|
11
|
+
# of the Edge can have it's own attributes.
|
12
|
+
class Edge < OpenStruct
|
13
|
+
def initialize
|
14
|
+
super
|
15
|
+
self.connections = []
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
# Connect this edge to a vertex
|
20
|
+
def <<(other)
|
21
|
+
c = Connection.new
|
22
|
+
c.end = other
|
23
|
+
self.connections << c
|
24
|
+
self
|
25
|
+
end
|
26
|
+
|
27
|
+
# Return the set of vertices this edge connects.
|
28
|
+
def vertices
|
29
|
+
self.connections.map {|c| c.end}
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the connection object that joins this Edge to the specified Vertex
|
33
|
+
def connection_at(vertex)
|
34
|
+
self.connections.select {|c| c.end.equal? vertex}.first
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
# A connection between an Edge and a Vertex.The connection can have arbitrary attributes,
|
39
|
+
# treated as method names.
|
40
|
+
class Connection < OpenStruct
|
41
|
+
def initialize
|
42
|
+
super
|
43
|
+
self
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
data/lib/graph/graph.rb
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# A simple graph library
|
4
|
+
|
5
|
+
module Graph
|
6
|
+
|
7
|
+
# A container for all the parts of a graph. The graph can have arbitrary attributes,
|
8
|
+
# treated as method names.
|
9
|
+
class Graph < OpenStruct
|
10
|
+
def initialize
|
11
|
+
super
|
12
|
+
self.edges = Array.new
|
13
|
+
self.vertices = Array.new
|
14
|
+
self
|
15
|
+
end
|
16
|
+
|
17
|
+
# Add a Vertex or Edge to the graph.
|
18
|
+
def <<(other)
|
19
|
+
if other.class == Vertex
|
20
|
+
self.vertices << other
|
21
|
+
elsif
|
22
|
+
self.edges << other
|
23
|
+
end
|
24
|
+
self
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
data/lib/graph/vertex.rb
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
require 'ostruct'
|
2
|
+
|
3
|
+
# A simple graph library
|
4
|
+
|
5
|
+
module Graph
|
6
|
+
# A vertex in a graph. The edge can have arbitrary attributes,treated as
|
7
|
+
# method names.
|
8
|
+
class Vertex < OpenStruct
|
9
|
+
def initialize
|
10
|
+
super
|
11
|
+
self.edges = []
|
12
|
+
self
|
13
|
+
end
|
14
|
+
|
15
|
+
# Connect this vertex to another, creating an Edge to do so, and returning
|
16
|
+
# the Edge
|
17
|
+
def connect(other)
|
18
|
+
e = Edge.new
|
19
|
+
e << self << other
|
20
|
+
self.edges << e
|
21
|
+
other.edges << e unless self === other
|
22
|
+
e
|
23
|
+
end
|
24
|
+
|
25
|
+
# Connect this vertex to another, creating an Edge to do so, and returning
|
26
|
+
# this Vertex
|
27
|
+
def <<(other)
|
28
|
+
connect(other)
|
29
|
+
self
|
30
|
+
end
|
31
|
+
|
32
|
+
# Return the set of neighbouring vertices
|
33
|
+
def neighbours
|
34
|
+
vertices = self.edges.map {|e| e.vertices}.flatten
|
35
|
+
vertices_to_me = vertices.select {|v| v == self}
|
36
|
+
other_vertices = vertices.select {|v| v != self}
|
37
|
+
(vertices_to_me[1..-1] || []) + other_vertices
|
38
|
+
end
|
39
|
+
|
40
|
+
end
|
41
|
+
end
|
@@ -0,0 +1,100 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module Graph
|
4
|
+
describe Edge do
|
5
|
+
let (:e) { Edge.new }
|
6
|
+
describe "#initialize" do
|
7
|
+
it "creates an empty edge" do
|
8
|
+
e = Edge.new
|
9
|
+
e.connections.should be_empty
|
10
|
+
end
|
11
|
+
end # #initialize
|
12
|
+
|
13
|
+
describe "adds attribues" do
|
14
|
+
it "adds then reports arbitrary attributes" do
|
15
|
+
e.score = 15
|
16
|
+
e.score.should == 15
|
17
|
+
end
|
18
|
+
end # adds attributes
|
19
|
+
|
20
|
+
describe "#<<" do
|
21
|
+
it "adds a new vertex to an edge (with a connection)" do
|
22
|
+
e.connections.should be_empty
|
23
|
+
v1 = Vertex.new
|
24
|
+
v2 = Vertex.new
|
25
|
+
e << v1
|
26
|
+
e.should have(1).connections
|
27
|
+
e.should have(1).vertices
|
28
|
+
e.vertices.should include(v1)
|
29
|
+
e << v2
|
30
|
+
e.should have(2).connections
|
31
|
+
e.should have(2).vertices
|
32
|
+
e.vertices.should include(v1)
|
33
|
+
e.vertices.should include(v2)
|
34
|
+
end
|
35
|
+
|
36
|
+
it "adds several vertices to an edge" do
|
37
|
+
e.connections.should be_empty
|
38
|
+
v1 = Vertex.new
|
39
|
+
v2 = Vertex.new
|
40
|
+
e << v1 << v2
|
41
|
+
e.vertices.should include(v1)
|
42
|
+
e.vertices.should include(v2)
|
43
|
+
e.should have(2).vertices
|
44
|
+
end
|
45
|
+
|
46
|
+
it "adds a self-loop" do
|
47
|
+
e.connections.should be_empty
|
48
|
+
v1 = Vertex.new
|
49
|
+
e << v1 << v1
|
50
|
+
e.vertices.should include(v1)
|
51
|
+
e.should have(2).vertices
|
52
|
+
e.vertices.uniq.length.should == 1
|
53
|
+
end
|
54
|
+
end # #<<
|
55
|
+
|
56
|
+
describe "connection_at" do
|
57
|
+
it "returns the connection that links to a vertex" do
|
58
|
+
e.connections.should be_empty
|
59
|
+
v1 = Vertex.new
|
60
|
+
v2 = Vertex.new
|
61
|
+
e << v1 << v2
|
62
|
+
|
63
|
+
e.connection_at(v1).end.should be v1
|
64
|
+
e.connection_at(v2).end.should be v2
|
65
|
+
end
|
66
|
+
|
67
|
+
it "returns nil if there is no connection to that vertex" do
|
68
|
+
e.connections.should be_empty
|
69
|
+
v1 = Vertex.new
|
70
|
+
v2 = Vertex.new
|
71
|
+
v3 = Vertex.new
|
72
|
+
e << v1 << v2
|
73
|
+
|
74
|
+
e.connection_at(v3).should be nil
|
75
|
+
end
|
76
|
+
|
77
|
+
it "returns the vertex for a self-loop" do
|
78
|
+
e.connections.should be_empty
|
79
|
+
v1 = Vertex.new
|
80
|
+
e << v1 << v1
|
81
|
+
|
82
|
+
e.connection_at(v1).end.should be v1
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
end # #connection_at
|
87
|
+
end # Edge
|
88
|
+
|
89
|
+
describe Connection do
|
90
|
+
let (:c) {Connection.new }
|
91
|
+
|
92
|
+
describe "adds attribues" do
|
93
|
+
it "adds then reports arbitrary attributes" do
|
94
|
+
c.score = 15
|
95
|
+
c.score.should == 15
|
96
|
+
end
|
97
|
+
end # adds attributes
|
98
|
+
end # Connection
|
99
|
+
|
100
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module Graph
|
4
|
+
describe Graph do
|
5
|
+
let (:g) { Graph.new }
|
6
|
+
describe "#initialize" do
|
7
|
+
it "creates an empty graph" do
|
8
|
+
g = Graph.new
|
9
|
+
g.edges.should be_empty
|
10
|
+
g.vertices.should be_empty
|
11
|
+
end
|
12
|
+
end # #initialize
|
13
|
+
|
14
|
+
describe "adds attribues" do
|
15
|
+
it "adds then reports arbitrary attributes" do
|
16
|
+
g.score = 15
|
17
|
+
g.score == 15
|
18
|
+
end
|
19
|
+
end # adds attributes
|
20
|
+
|
21
|
+
describe "#<<" do
|
22
|
+
it "adds a set of vertices" do
|
23
|
+
g.vertices.should be_empty
|
24
|
+
v1 = Vertex.new
|
25
|
+
v2 = Vertex.new
|
26
|
+
g << v1 << v2
|
27
|
+
g.should have(2).vertices
|
28
|
+
g.vertices.should include(v1)
|
29
|
+
g.vertices.should include(v2)
|
30
|
+
end
|
31
|
+
|
32
|
+
it "adds a set of edges" do
|
33
|
+
g.edges.should be_empty
|
34
|
+
e1 = Edge.new
|
35
|
+
e2 = Edge.new
|
36
|
+
g << e1 << e2
|
37
|
+
g.should have(2).edges
|
38
|
+
g.edges.should include(e1)
|
39
|
+
g.edges.should include(e2)
|
40
|
+
end
|
41
|
+
|
42
|
+
it "adds a mixed set of vertices and edges" do
|
43
|
+
g.vertices.should be_empty
|
44
|
+
g.edges.should be_empty
|
45
|
+
v1 = Vertex.new
|
46
|
+
v2 = Vertex.new
|
47
|
+
e1 = Edge.new
|
48
|
+
e2 = Edge.new
|
49
|
+
g << v1 << e1 << v2 << e2
|
50
|
+
g.should have(2).vertices
|
51
|
+
g.vertices.should include(v1)
|
52
|
+
g.vertices.should include(v2)
|
53
|
+
g.should have(2).edges
|
54
|
+
g.edges.should include(e1)
|
55
|
+
g.edges.should include(e2)
|
56
|
+
end
|
57
|
+
end # #<<
|
58
|
+
|
59
|
+
describe "connect" do
|
60
|
+
it "adds and records an edge between vertices" do
|
61
|
+
end
|
62
|
+
end # #connect
|
63
|
+
|
64
|
+
end
|
65
|
+
end
|
@@ -0,0 +1,101 @@
|
|
1
|
+
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
|
2
|
+
|
3
|
+
module Graph
|
4
|
+
describe Vertex do
|
5
|
+
let (:v) { Vertex.new }
|
6
|
+
|
7
|
+
describe "#initialize" do
|
8
|
+
it "creates an empty vertex" do
|
9
|
+
v = Vertex.new
|
10
|
+
v.edges.should be_empty
|
11
|
+
end
|
12
|
+
end # #initialize
|
13
|
+
|
14
|
+
describe "adds attribues" do
|
15
|
+
it "adds then reports arbitrary attributes" do
|
16
|
+
v.score = 15
|
17
|
+
v.score.should == 15
|
18
|
+
end
|
19
|
+
end # adds attributes
|
20
|
+
|
21
|
+
describe "#<<" do
|
22
|
+
it "adds a single edge between vertices" do
|
23
|
+
v.neighbours.should be_empty
|
24
|
+
v.edges.should be_empty
|
25
|
+
|
26
|
+
v1 = Vertex.new
|
27
|
+
v << v1
|
28
|
+
|
29
|
+
v1.id = :v1 # Need this to ensure that v != v1
|
30
|
+
|
31
|
+
v.should have(1).edges
|
32
|
+
v1.should have(1).edges
|
33
|
+
e = v.edges[0]
|
34
|
+
v1.edges.should include(e)
|
35
|
+
|
36
|
+
v.should have(1).neighbours
|
37
|
+
v.neighbours.should include(v1)
|
38
|
+
v.neighbours.should_not include(v)
|
39
|
+
v1.should have(1).neighbours
|
40
|
+
v1.neighbours.should include(v)
|
41
|
+
v1.neighbours.should_not include(v1)
|
42
|
+
end
|
43
|
+
|
44
|
+
it "adds a single edge as a self-loop" do
|
45
|
+
v.neighbours.should be_empty
|
46
|
+
v.edges.should be_empty
|
47
|
+
|
48
|
+
v << v
|
49
|
+
|
50
|
+
v.should have(1).edges
|
51
|
+
v.should have(1).neighbours
|
52
|
+
v.neighbours.should include(v)
|
53
|
+
end
|
54
|
+
end # #<<
|
55
|
+
|
56
|
+
describe "connect" do
|
57
|
+
it "connects two vertices" do
|
58
|
+
v1 = Vertex.new
|
59
|
+
v1.id = :v1 # Need this to ensure that v != v1
|
60
|
+
|
61
|
+
e = v.connect v1
|
62
|
+
|
63
|
+
v.should have(1).neighbours
|
64
|
+
v.neighbours.should include(v1)
|
65
|
+
v.neighbours.should_not include(v)
|
66
|
+
|
67
|
+
v1.should have(1).neighbours
|
68
|
+
v1.neighbours.should include(v)
|
69
|
+
v1.neighbours.should_not include(v1)
|
70
|
+
|
71
|
+
v.should have(1).edges
|
72
|
+
v.edges.should include(e)
|
73
|
+
v1.should have(1).edges
|
74
|
+
v1.edges.should include(e)
|
75
|
+
|
76
|
+
e.should have(2).vertices
|
77
|
+
e.vertices.should include(v)
|
78
|
+
e.vertices.should include(v1)
|
79
|
+
|
80
|
+
e.should have(2).connections
|
81
|
+
end
|
82
|
+
|
83
|
+
it "creates a self-connection" do
|
84
|
+
e = v.connect v
|
85
|
+
|
86
|
+
v.should have(1).neighbours
|
87
|
+
v.neighbours.should include(v)
|
88
|
+
|
89
|
+
v.should have(1).edges
|
90
|
+
v.edges.should include(e)
|
91
|
+
|
92
|
+
e.should have(2).vertices
|
93
|
+
e.vertices.uniq.length.should == 1
|
94
|
+
e.vertices.should include(v)
|
95
|
+
|
96
|
+
e.should have(2).connections
|
97
|
+
end
|
98
|
+
|
99
|
+
end # #connect
|
100
|
+
end
|
101
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
|
2
|
+
$LOAD_PATH.unshift(File.dirname(__FILE__))
|
3
|
+
require 'rspec'
|
4
|
+
require 'graph.njae'
|
5
|
+
|
6
|
+
# Requires supporting files with custom matchers and macros, etc,
|
7
|
+
# in ./support/ and its subdirectories.
|
8
|
+
Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each {|f| require f}
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
|
12
|
+
end
|
metadata
ADDED
@@ -0,0 +1,124 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: graph.njae
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Neil Smith
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-09-23 00:00:00.000000000 +01:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: rspec
|
17
|
+
requirement: &77209130 !ruby/object:Gem::Requirement
|
18
|
+
none: false
|
19
|
+
requirements:
|
20
|
+
- - ~>
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 2.6.0
|
23
|
+
type: :development
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: *77209130
|
26
|
+
- !ruby/object:Gem::Dependency
|
27
|
+
name: bundler
|
28
|
+
requirement: &77208840 !ruby/object:Gem::Requirement
|
29
|
+
none: false
|
30
|
+
requirements:
|
31
|
+
- - ~>
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: 1.0.0
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: *77208840
|
37
|
+
- !ruby/object:Gem::Dependency
|
38
|
+
name: jeweler
|
39
|
+
requirement: &77208540 !ruby/object:Gem::Requirement
|
40
|
+
none: false
|
41
|
+
requirements:
|
42
|
+
- - ~>
|
43
|
+
- !ruby/object:Gem::Version
|
44
|
+
version: 1.6.2
|
45
|
+
type: :development
|
46
|
+
prerelease: false
|
47
|
+
version_requirements: *77208540
|
48
|
+
- !ruby/object:Gem::Dependency
|
49
|
+
name: rcov
|
50
|
+
requirement: &77208290 !ruby/object:Gem::Requirement
|
51
|
+
none: false
|
52
|
+
requirements:
|
53
|
+
- - ! '>='
|
54
|
+
- !ruby/object:Gem::Version
|
55
|
+
version: '0'
|
56
|
+
type: :development
|
57
|
+
prerelease: false
|
58
|
+
version_requirements: *77208290
|
59
|
+
- !ruby/object:Gem::Dependency
|
60
|
+
name: rdoc
|
61
|
+
requirement: &77208030 !ruby/object:Gem::Requirement
|
62
|
+
none: false
|
63
|
+
requirements:
|
64
|
+
- - ! '>='
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '0'
|
67
|
+
type: :development
|
68
|
+
prerelease: false
|
69
|
+
version_requirements: *77208030
|
70
|
+
description: A simple graph library
|
71
|
+
email: neil.github@njae.me.uk
|
72
|
+
executables: []
|
73
|
+
extensions: []
|
74
|
+
extra_rdoc_files:
|
75
|
+
- LICENSE.txt
|
76
|
+
- README.rdoc
|
77
|
+
files:
|
78
|
+
- .document
|
79
|
+
- .rspec
|
80
|
+
- Gemfile
|
81
|
+
- Gemfile.lock
|
82
|
+
- LICENSE.txt
|
83
|
+
- README.rdoc
|
84
|
+
- Rakefile
|
85
|
+
- VERSION
|
86
|
+
- graph.njae.gemspec
|
87
|
+
- lib/graph.njae.rb
|
88
|
+
- lib/graph/edge.rb
|
89
|
+
- lib/graph/graph.rb
|
90
|
+
- lib/graph/vertex.rb
|
91
|
+
- spec/graph/edge_spec.rb
|
92
|
+
- spec/graph/graph_spec.rb
|
93
|
+
- spec/graph/vertex_spec.rb
|
94
|
+
- spec/spec_helper.rb
|
95
|
+
has_rdoc: true
|
96
|
+
homepage: http://github.com/NeilNjae/graph.njae
|
97
|
+
licenses:
|
98
|
+
- MIT
|
99
|
+
post_install_message:
|
100
|
+
rdoc_options: []
|
101
|
+
require_paths:
|
102
|
+
- lib
|
103
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
104
|
+
none: false
|
105
|
+
requirements:
|
106
|
+
- - ! '>='
|
107
|
+
- !ruby/object:Gem::Version
|
108
|
+
version: '0'
|
109
|
+
segments:
|
110
|
+
- 0
|
111
|
+
hash: -557460077
|
112
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
113
|
+
none: false
|
114
|
+
requirements:
|
115
|
+
- - ! '>='
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
requirements: []
|
119
|
+
rubyforge_project:
|
120
|
+
rubygems_version: 1.6.2
|
121
|
+
signing_key:
|
122
|
+
specification_version: 3
|
123
|
+
summary: A simple graph library
|
124
|
+
test_files: []
|