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
14 describe
"adds attribues" do
15 it
"adds then reports arbitrary attributes" do
22 it
"adds a single edge between vertices" do
23 v
.neighbours
.should be_empty
24 v
.edges
.should be_empty
29 v1
.id
= :v1 # Need this to ensure that v != v1
31 v
.should
have(1).edges
32 v1
.should
have(1).edges
34 v1
.edges
.should
include(e
)
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
)
44 it
"adds a single edge as a self-loop" do
45 v
.neighbours
.should be_empty
46 v
.edges
.should be_empty
50 v
.should
have(1).edges
51 v
.should
have(1).neighbours
52 v
.neighbours
.should
include(v
)
57 it
"connects two vertices" do
59 v1
.id
= :v1 # Need this to ensure that v != v1
63 v
.should
have(1).neighbours
64 v
.neighbours
.should
include(v1
)
65 v
.neighbours
.should_not
include(v
)
67 v1
.should
have(1).neighbours
68 v1
.neighbours
.should
include(v
)
69 v1
.neighbours
.should_not
include(v1
)
71 v
.should
have(1).edges
72 v
.edges
.should
include(e
)
73 v1
.should
have(1).edges
74 v1
.edges
.should
include(e
)
76 e
.should
have(2).vertices
77 e
.vertices
.should
include(v
)
78 e
.vertices
.should
include(v1
)
80 e
.should
have(2).connections
83 it
"creates a self-connection" do
86 v
.should
have(1).neighbours
87 v
.neighbours
.should
include(v
)
89 v
.should
have(1).edges
90 v
.edges
.should
include(e
)
92 e
.should
have(2).vertices
93 e
.vertices
.uniq
.length
.should
== 1
94 e
.vertices
.should
include(v
)
96 e
.should
have(2).connections
101 describe
"#neighbours" do
102 it
"finds neighbours of a self loop" do
104 v
.should
have(1).neighbours
105 v
.neighbours
.should
include v
108 it
"finds neighbours on an edge" do
111 v
.should
have(1).neighbours
112 v
.neighbours
.should
include v1
113 v1
.should
have(1).neighbours
114 v1
.neighbours
.should
include v
117 it
"finds neighbours with multiple edges" do
122 v
.should
have(2).neighbours
123 v
.neighbours
.should
include v1
124 v
.neighbours
.should_not
include v
125 v1
.should
have(2).neighbours
126 v1
.neighbours
.should
include v
127 v1
.neighbours
.should_not
include v1
130 it
"finds neighbours with multiple self loops" do
132 v
.should
have(3).neighbours
133 v
.neighbours
.should
include v
134 v
.neighbours
.uniq
.length
.should
== 1
137 it
"finds neighbours with all sorts of edges" do
138 v1
= Vertex
.new
; v1
.id
= :v1
139 v2
= Vertex
.new
; v2
.id
= :v2
140 v3
= Vertex
.new
; v3
.id
= :v3
146 v
.should
have(2).neighbours
147 v
.neighbours
.should
include v1
148 v
.neighbours
.should
include v2
150 v1
.should
have(1).neighbours
151 v1
.neighbours
.should
include v
153 v2
.should
have(3).neighbours
154 v2
.neighbours
.should
include v
155 v2
.neighbours
.should
include v2
156 v2
.neighbours
.should
include v3
158 v3
.should
have(1).neighbours
159 v3
.neighbours
.should
include v2
162 it
"finds neighbours in graphs with several vertices" do
163 v1
= Vertex
.new
; v1
.id
= :v1
164 v2
= Vertex
.new
; v2
.id
= :v2
165 v3
= Vertex
.new
; v3
.id
= :v3
166 v4
= Vertex
.new
; v4
.id
= :v4
172 v
.should
have(2).neighbours
173 v
.neighbours
.should
include v1
174 v
.neighbours
.should
include v3
175 v
.neighbours
.should_not
include v
176 v1
.should
have(2).neighbours
177 v1
.neighbours
.should
include v
178 v1
.neighbours
.should
include v2
179 v1
.neighbours
.should_not
include v1
180 v2
.should
have(2).neighbours
181 v2
.neighbours
.should
include v1
182 v2
.neighbours
.should
include v3
183 v2
.neighbours
.should_not
include v2
184 v3
.should
have(3).neighbours
185 v3
.neighbours
.should
include v
186 v3
.neighbours
.should
include v2
187 v3
.neighbours
.should
include v4
188 v3
.neighbours
.should_not
include v3
189 v4
.should
have(1).neighbours
190 v4
.neighbours
.should
include v3
191 v4
.neighbours
.should_not
include v4
194 it
"finds neighbours with multiple edges between vertices" do
195 v1
= Vertex
.new
; v1
.id
= :v1
196 v2
= Vertex
.new
; v2
.id
= :v2
197 v3
= Vertex
.new
; v3
.id
= :v3
205 v
.should
have(3).neighbours
206 v
.neighbours
.should
include v1
207 v
.neighbours
.should
include v2
208 v
.neighbours
.should
include v
210 v1
.should
have(2).neighbours
211 v1
.neighbours
.should
include v
212 v1
.neighbours
.should
include v1
214 v2
.should
have(4).neighbours
215 v2
.neighbours
.should
include v
216 v2
.neighbours
.should
include v2
217 v2
.neighbours
.should
include v3
218 v2
.neighbours
.uniq
.length
.should
== 3
220 v3
.should
have(2).neighbours
221 v3
.neighbours
.should
include v2
222 v3
.neighbours
.uniq
.length
.should
== 1