1 require File
.expand_path(File
.dirname(__FILE__
) + '/../spec_helper')
5 let (:v) { Vertex
.new
}
7 describe
"#initialize" do
8 it
"creates an empty vertex" do
10 v
.edges
.should be_empty
13 it
"creates a vertex with some parameters" do
14 v
= Vertex
.new
:value1 => 1, :value2 => "value2", :value3 => :v3
16 v
.value2
.should
== "value2"
17 v
.value3
.should
== :v3
18 v
.value4
.should be_nil
22 describe
"adds attribues" do
23 it
"adds then reports arbitrary attributes" do
30 it
"adds a single edge between vertices" do
31 v
.neighbours
.should be_empty
32 v
.edges
.should be_empty
37 v1
.id
= :v1 # Need this to ensure that v != v1
39 v
.should
have(1).edges
40 v1
.should
have(1).edges
42 v1
.edges
.should
include(e
)
44 v
.should
have(1).neighbours
45 v
.neighbours
.should
include(v1
)
46 v
.neighbours
.should_not
include(v
)
47 v1
.should
have(1).neighbours
48 v1
.neighbours
.should
include(v
)
49 v1
.neighbours
.should_not
include(v1
)
52 it
"adds a single edge as a self-loop" do
53 v
.neighbours
.should be_empty
54 v
.edges
.should be_empty
58 v
.should
have(1).edges
59 v
.should
have(1).neighbours
60 v
.neighbours
.should
include(v
)
65 it
"connects two vertices" do
67 v1
.id
= :v1 # Need this to ensure that v != v1
71 v
.should
have(1).neighbours
72 v
.neighbours
.should
include(v1
)
73 v
.neighbours
.should_not
include(v
)
75 v1
.should
have(1).neighbours
76 v1
.neighbours
.should
include(v
)
77 v1
.neighbours
.should_not
include(v1
)
79 v
.should
have(1).edges
80 v
.edges
.should
include(e
)
81 v1
.should
have(1).edges
82 v1
.edges
.should
include(e
)
84 e
.should
have(2).vertices
85 e
.vertices
.should
include(v
)
86 e
.vertices
.should
include(v1
)
88 e
.should
have(2).connections
91 it
"creates a self-connection" do
94 v
.should
have(1).neighbours
95 v
.neighbours
.should
include(v
)
97 v
.should
have(1).edges
98 v
.edges
.should
include(e
)
100 e
.should
have(2).vertices
101 e
.vertices
.uniq
.length
.should
== 1
102 e
.vertices
.should
include(v
)
104 e
.should
have(2).connections
109 describe
"#neighbours" do
110 it
"finds neighbours of a self loop" do
112 v
.should
have(1).neighbours
113 v
.neighbours
.should
include v
116 it
"finds neighbours on an edge" do
119 v
.should
have(1).neighbours
120 v
.neighbours
.should
include v1
121 v1
.should
have(1).neighbours
122 v1
.neighbours
.should
include v
125 it
"finds neighbours with multiple edges" do
130 v
.should
have(2).neighbours
131 v
.neighbours
.should
include v1
132 v
.neighbours
.should_not
include v
133 v1
.should
have(2).neighbours
134 v1
.neighbours
.should
include v
135 v1
.neighbours
.should_not
include v1
138 it
"finds neighbours with multiple self loops" do
140 v
.should
have(3).neighbours
141 v
.neighbours
.should
include v
142 v
.neighbours
.uniq
.length
.should
== 1
145 it
"finds neighbours with all sorts of edges" do
146 v1
= Vertex
.new
; v1
.id
= :v1
147 v2
= Vertex
.new
; v2
.id
= :v2
148 v3
= Vertex
.new
; v3
.id
= :v3
154 v
.should
have(2).neighbours
155 v
.neighbours
.should
include v1
156 v
.neighbours
.should
include v2
158 v1
.should
have(1).neighbours
159 v1
.neighbours
.should
include v
161 v2
.should
have(3).neighbours
162 v2
.neighbours
.should
include v
163 v2
.neighbours
.should
include v2
164 v2
.neighbours
.should
include v3
166 v3
.should
have(1).neighbours
167 v3
.neighbours
.should
include v2
170 it
"finds neighbours in graphs with several vertices" do
171 v1
= Vertex
.new
; v1
.id
= :v1
172 v2
= Vertex
.new
; v2
.id
= :v2
173 v3
= Vertex
.new
; v3
.id
= :v3
174 v4
= Vertex
.new
; v4
.id
= :v4
180 v
.should
have(2).neighbours
181 v
.neighbours
.should
include v1
182 v
.neighbours
.should
include v3
183 v
.neighbours
.should_not
include v
184 v1
.should
have(2).neighbours
185 v1
.neighbours
.should
include v
186 v1
.neighbours
.should
include v2
187 v1
.neighbours
.should_not
include v1
188 v2
.should
have(2).neighbours
189 v2
.neighbours
.should
include v1
190 v2
.neighbours
.should
include v3
191 v2
.neighbours
.should_not
include v2
192 v3
.should
have(3).neighbours
193 v3
.neighbours
.should
include v
194 v3
.neighbours
.should
include v2
195 v3
.neighbours
.should
include v4
196 v3
.neighbours
.should_not
include v3
197 v4
.should
have(1).neighbours
198 v4
.neighbours
.should
include v3
199 v4
.neighbours
.should_not
include v4
202 it
"finds neighbours with multiple edges between vertices" do
203 v1
= Vertex
.new
; v1
.id
= :v1
204 v2
= Vertex
.new
; v2
.id
= :v2
205 v3
= Vertex
.new
; v3
.id
= :v3
213 v
.should
have(3).neighbours
214 v
.neighbours
.should
include v1
215 v
.neighbours
.should
include v2
216 v
.neighbours
.should
include v
218 v1
.should
have(2).neighbours
219 v1
.neighbours
.should
include v
220 v1
.neighbours
.should
include v1
222 v2
.should
have(4).neighbours
223 v2
.neighbours
.should
include v
224 v2
.neighbours
.should
include v2
225 v2
.neighbours
.should
include v3
226 v2
.neighbours
.uniq
.length
.should
== 3
228 v3
.should
have(2).neighbours
229 v3
.neighbours
.should
include v2
230 v3
.neighbours
.uniq
.length
.should
== 1