1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319
|
$LOAD_PATH.unshift File.dirname(__FILE__) + "/../../helpers/ir"
require 'vertex_helpers'
require 'edge_helpers'
import 'org.jruby.ir.util.DirectedGraph'
import 'org.jruby.ir.util.Vertex'
describe "Vertex" do
before do
@graph = DirectedGraph.new
@source = Vertex.new(@graph, "foo", 1)
@dest = Vertex.new(@graph, "bar", 2)
end
describe "Adding an edge from source to destination" do
before do
@source.add_edge(:to => @dest)
end
it "adds outgoing edge to source" do
expect(@source).to have_out_degree 1
end
it "adds incoming edge to destination" do
expect(@dest).to have_in_degree 1
end
it "adds the edge to the graph containing source" do
expect(@graph.edges()).not_to be nil
end
it "sets edge type to null if is not provided" do
expect(@graph.edges().first).to have_type(nil)
end
it "sets edge type to the given value if is provided" do
@source.remove_edge(:to => @dest)
@source.add_edge(:to => @dest, :type => "foobar")
expect(@graph.edges.first).to have_type("foobar")
end
end
describe "Removing an outgoing edge from current vertex" do
before do
@source.add_edge(:to => @dest)
end
context "Destination of any one of the outgoing edges from the current vertex matched with given destination" do
it "removes an edge from outgoing edges of the source vertex" do
@source.remove_edge(:to => @dest)
expect(@source).to have_out_degree 0
end
it "removes an edge from incoming edges of the destination vertex" do
@source.remove_edge(:to => @dest)
expect(@dest).to have_in_degree 0
end
end
context "Destination of all of the outgoing edges from the current vertex doesn't match with given destination" do
it "returns false" do
non_existent_destination = Vertex.new(@graph, "baz", 3)
expect(@source.remove_edge(:to => non_existent_destination)).to be false
end
end
end
describe "Remove all incoming edges" do
before do
@interim = Vertex.new(@graph, "interim", 3)
@dest.add_edge(:to => @source)
@interim.add_edge(:to => @source)
end
it "removes all incoming edges to the vertex" do
@source.remove_edges(:direction => :in)
expect(@source).to have_in_degree 0
end
end
describe "Remove all outgoing edges" do
before do
@interim = Vertex.new(@graph, "interim", 3)
@source.add_edge(:to => @dest)
@source.add_edge(:to => @interim)
end
it "removes all outgoing edges from the vertex" do
@source.remove_edges(:direction => :out)
expect(@source).to have_out_degree 0
end
end
describe "Remove all edges" do
before do
@interim = Vertex.new(@graph, "interim", 3)
@source.add_edge(:to => @dest)
@source.add_edge(:to => @interim)
@dest.add_edge(:to => @source)
@interim.add_edge(:to => @source)
end
it "removes all edges from the vertex" do
@source.remove_edges
expect(@source).to have_out_degree 0
expect(@source).to have_in_degree 0
end
end
describe "getOutGoingEdge" do
before do
@null_vertex = Vertex.new(@graph, "null", 3)
end
it "returns first outgoing edge from the vertex not of type 'null'" do
@source.add_edge(:to => @dest, :type => "not_null")
@source.add_edge(:to => @null_vertex, :type => nil)
expect(@source.outgoing_edge).to have_type("not_null")
end
it "returns null when all outgoing edges from the vertex are of type 'null'" do
@source.add_edge(:to => @dest)
@source.add_edge(:to => @null_vertex, :type => nil)
expect(@source.outgoing_edge).to be nil
end
end
describe "getIncomingEdge" do
before do
@null_vertex = Vertex.new(@graph, "null", 3)
end
it "returns first incoming edge to the vertex not of type 'null'" do
@source.add_edge(:to => @dest, :type => "not_null")
@null_vertex.add_edge(:to => @dest, :type => nil)
expect(@dest.incoming_edge).to have_type("not_null")
end
it "returns null when all incoming edges to the vertex are of type 'null'" do
@source.add_edge(:to => @dest)
@null_vertex.add_edge(:to => @dest, :type => nil)
expect(@dest.incoming_edge).to be nil
end
end
describe "getOutGoingEdgeOfType" do
context "when the edge of given type exists" do
it "returns first outgoing edge of the given type" do
@source.add_edge(:to => @dest, :type => "baz")
expect(@source.outgoing_edge(:type => "baz")).to have_type("baz")
end
end
context "when the edge of given type does not exist" do
it "returns null" do
@source.add_edge(:to => @dest, :type => "foobarbaz")
expect(@source.outgoing_edge(:type => "foo-bar-baz")).to be nil
end
end
end
describe "getIncomingEdgeOfType" do
context "when the edge of given type exists" do
it "returns first incoming edge of the given type" do
@source.add_edge(:to => @dest, :type => "baz")
expect(@dest.incoming_edge(:type => "baz")).to have_type("baz")
end
end
context "when the edge of given type does not exist" do
it "returns null" do
@source.add_edge(:to => @dest, :type => "foobarbaz")
expect(@dest.incoming_edge(:type => "foo-bar-baz")).to be nil
end
end
end
describe "getIncomingSourceData" do
context "when there is atleast one incoming edge to the current vertex" do
it "returns data of the source of that first incoming edge" do
@source.add_edge(:to => @dest)
expect(@dest.data(:direction => :in)).to eq "foo"
end
end
context "when there is no incoming edge to the current vertex" do
it "returns null" do
@source.add_edge(:to => @dest)
expect(@source.data(:direction => :in)).to be nil
end
end
end
describe "getIncomingSourceDataOfType" do
context "when there is atleast one incoming edge to the current vertex of the given type" do
it "returns data of the source of that first incoming edge of given type" do
@source.add_edge(:to => @dest)
expect(@dest.data(:direction => :in, :type => nil)).to eq "foo"
end
end
context "when there is no incoming edge to the current vertex of given type" do
it "returns null" do
@source.add_edge(:to => @dest, :type => "foo")
expect(@dest.incoming_edge(:type => nil)).to eq nil
end
end
end
describe "getOutgoingDestinationData" do
context "when there is atleast one outgoing edge from the current vertex" do
it "returns data of the destination of that first outgoing edge" do
@source.add_edge(:to => @dest)
expect(@source.data(:direction => :out)).to eq "bar"
end
end
context "when there is no outgoing edge from the current vertex" do
it "returns null" do
@source.add_edge(:to => @dest)
expect(@dest.data(:direction => :out)).to be nil
end
end
end
describe "getOutgoingDestinationDataOfType" do
context "when there is atleast one outgoing edge from the current vertex of the given type" do
it "returns data of the source of that first outgoing edge of given type" do
@source.add_edge(:to => @dest)
expect(@source.data(:direction => :out, :type => nil)).to eq "bar"
end
end
context "when there is no outgoing edge from the current vertex of given type" do
it "returns null" do
@source.add_edge(:to => @dest, :type => "foo")
expect(@source.data(:direction => :out, :type => nil)).to be nil
end
end
end
describe "toString" do
before do
@interim = Vertex.new(@graph, "interim", 3)
end
context "when vertex has no edges" do
it "returns string representation of the vertex" do
expect(@source.toString).to eq "foo:\n"
end
end
context "when vertex has only one outgoing edge" do
it "returns string representation of the vertex" do
@source.add_edge(:to => @dest)
expect(@source.toString).to eq "foo:>[2]\n"
end
end
context "when vertex has many outgoing edges" do
it "returns string representation of the vertex" do
@source.add_edge(:to => @dest)
@source.add_edge(:to => @interim)
expect(["foo:>[2,3]\n", "foo:>[3,2]\n"]).to include @source.toString
end
end
context "when vertex has only one incoming edge" do
it "returns string representation of the vertex" do
@source.add_edge(:to => @dest)
expect(@dest.toString).to eq "bar:<[1]\n"
end
end
context "when vertex has many incoming edges" do
it "returns string representation of the vertex" do
@source.add_edge(:to => @dest)
@interim.add_edge(:to => @dest)
expect(["bar:<[1,3]\n", "bar:<[3,1]\n"]).to include @dest.toString
end
end
context "when vertex has both incoming and outgoing edges" do
it "returns string representation of the vertex" do
@source.add_edge(:to => @interim)
@interim.add_edge(:to => @dest)
expect(@interim.toString).to eq "interim:>[2], <[1]\n"
end
end
end
end
|