Skip to content

Instantly share code, notes, and snippets.

@semmons99
Created February 11, 2011 13:34
Show Gist options
  • Save semmons99/822343 to your computer and use it in GitHub Desktop.
Save semmons99/822343 to your computer and use it in GitHub Desktop.
require "spec_helper"
describe AdjacencyMatrix do
context "undirected graph" do
let(:pitt2pitt) {
AdjacencyMatrix.connections({"Pittsburgh" => "Pittsburgh"}, false)
}
let(:pitt2clev) {
AdjacencyMatrix.connections({"Pittsburgh" => "Cleveland"}, false)
}
describe "#connection_exists?" do
it "should return true if a connection exists" do
pitt2pitt.connection_exists?("Pittsburgh", "Pittsburgh").should be_true
pitt2clev.connection_exists?("Pittsburgh", "Cleveland" ).should be_true
pitt2clev.connection_exists?("Cleveland", "Pittsburgh").should be_true
end
it "should return false if a connection doesn't exist" do
pitt2pitt.connection_exists?("Pittsburgh", "Cleveland" ).should be_false
pitt2clev.connection_exists?("Pittsburgh", "Pittsburgh").should be_false
pitt2clev.connection_exists?("Cleveland", "Cleveland" ).should be_false
end
end
describe "#incoming_connections" do
it "should return the number of incoming connections" do
pitt2pitt.incoming_connections("Pittsburgh").should == 1
pitt2clev.incoming_connections("Pittsburgh").should == 1
pitt2clev.incoming_connections("Cleveland" ).should == 1
end
end
describe "#outgoing_connections" do
it "should return the number of outgoing connections" do
pitt2pitt.outgoing_connections("Pittsburgh").should == 1
pitt2clev.outgoing_connections("Pittsburgh").should == 1
pitt2clev.outgoing_connections("Cleveland" ).should == 1
end
end
end
context "directed graph" do
let(:pitt2pitt) {
AdjacencyMatrix.connections({"Pittsburgh" => "Pittsburgh"})
}
let(:pitt2clev) {
AdjacencyMatrix.connections({"Pittsburgh" => "Cleveland"})
}
let(:pitt2clev2chic) {
AdjacencyMatrix.connections({"Pittsburgh" => "Cleveland",
"Cleveland" => "Chicago"})
}
describe "#connection_exists?" do
it "should return true if a connection exists" do
pitt2pitt.connection_exists?("Pittsburgh", "Pittsburgh").should be_true
pitt2clev.connection_exists?("Pittsburgh", "Cleveland" ).should be_true
end
it "should return false if a connection doesn't exist" do
pitt2clev.connection_exists?("Pittsburgh", "Pittsburgh").should be_false
pitt2clev.connection_exists?("Cleveland", "Pittsburgh").should be_false
pitt2clev.connection_exists?("Cleveland", "Cleveland" ).should be_false
end
end
describe "#row" do
it "should return the row of an item" do
pitt2pitt.row("Pittsburgh").should == [1]
pitt2clev.row("Pittsburgh").should == [0, 1]
pitt2clev.row("Cleveland" ).should == [0, 0]
end
end
describe "#column" do
it "should return the column of an item" do
pitt2pitt.column("Pittsburgh").should == [1]
pitt2clev.column("Pittsburgh").should == [0, 0]
pitt2clev.column("Cleveland" ).should == [1, 0]
end
end
describe "#incoming_connections" do
it "should return the number of incoming connections" do
pitt2pitt.incoming_connections("Pittsburgh").should == 1
pitt2clev.incoming_connections("Pittsburgh").should == 0
pitt2clev.incoming_connections("Cleveland" ).should == 1
end
end
describe "#outgoing_connections" do
it "should return the number of outgoing connections" do
pitt2pitt.outgoing_connections("Pittsburgh").should == 1
pitt2clev.outgoing_connections("Pittsburgh").should == 1
pitt2clev.outgoing_connections("Cleveland" ).should == 0
end
end
context "squaring the matrix when the longest link == 1" do
let(:sqrd) { pitt2clev ** 2 }
it "should negate all connections" do
sqrd.connection_exists?("Pittsburg", "Cleveland").should be_false
end
end
context "squaring the matrix when the longest link == 2" do
let(:sqrd) { pitt2clev2chic ** 2 }
it "should negate connections of length 1" do
sqrd.connection_exists?("Pittsburgh", "Cleveland").should be_false
sqrd.connection_exists?("Cleveland", "Chicago" ).should be_false
end
it "should keep connection of length 2" do
sqrd.connection_exists?("Pittsburgh", "Chicago").should be_true
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment