27 lines
697 B
Crystal
27 lines
697 B
Crystal
|
require "./spec_helper"
|
||
|
|
||
|
describe Graph do
|
||
|
describe "#find_path" do
|
||
|
it "reports no paths in a graph with no edges" do
|
||
|
gr = Graph(String).new
|
||
|
elems = ["a", "b", "c", "d"]
|
||
|
elems.each &->gr.add_node(String)
|
||
|
elems.each do |elem|
|
||
|
elems.each do |other|
|
||
|
next if elem == other
|
||
|
gr.find_path(elem, other).should be_nil
|
||
|
end
|
||
|
end
|
||
|
end
|
||
|
|
||
|
it "computes the shortest path" do
|
||
|
gr = Graph(String).new
|
||
|
[{"a", "b", 1}, {"a", "c", 1}, {"c", "b", 1}, {"b", "d", 1}].each do |t|
|
||
|
from, to, cost = t
|
||
|
gr.add_edge(from, to, cost)
|
||
|
end
|
||
|
gr.find_path("a", "d").should eq({["a", "b", "d"], 2})
|
||
|
end
|
||
|
end
|
||
|
end
|