Skip to content

Instantly share code, notes, and snippets.

@skatenerd
Created November 9, 2012 20:24
Show Gist options
  • Save skatenerd/4048003 to your computer and use it in GitHub Desktop.
Save skatenerd/4048003 to your computer and use it in GitHub Desktop.
Coinfactorizer Spec
require 'coinfactorizer'
describe Coinfactorizer do
context "coins" do
it "changes 0" do
Coinfactorizer.change(0, Coins).should == {}
end
it "changes 1" do
Coinfactorizer.change(1, Coins).should == {1 => 1}
end
it "changes 2" do
Coinfactorizer.change(2, Coins).should == {1 => 2}
end
it "changes 5" do
Coinfactorizer.change(5, Coins).should == {5 => 1}
end
it "changes 6" do
Coinfactorizer.change(6, Coins).should == {5 => 1, 1 => 1}
end
it "changes 10" do
Coinfactorizer.change(10, Coins).should == {
10 => 1
}
end
it "changes 31" do
Coinfactorizer.change(31, Coins).should == {
25 => 1,
5 => 1,
1 => 1
}
end
end
context "factoring" do
context "with dependency injected" do
class MockPrimes
def self.elements(_)
[2,3,5,7,11,13,17]
end
def self.stop?(total)
total==1
end
def self.destroy_domain_element_from_total(total, domain_element)
if total == 1
[1, 0]
elsif total == 2 && domain_element == 2
[1, 1]
elsif total == 3 && domain_element == 3
[1, 1]
elsif total == 4 && domain_element == 2
[1, 2]
else
[total, 0]
end
end
end
it "factors 1" do
Coinfactorizer.change(1, MockPrimes).should == {}
end
it "factors 2" do
Coinfactorizer.change(2, MockPrimes).should == {2 => 1}
end
it "factors 3" do
Coinfactorizer.change(3, MockPrimes).should == {3 => 1}
end
it "factors 4" do
Coinfactorizer.change(4, MockPrimes).should == {2 => 2}
end
end
context "integration" do
it "factors 150" do
Coinfactorizer.change(150, Primes).should == {
2 => 1,
3 => 1,
5 => 2
}
end
end
describe Primes do
it "takes factors of 83 from 1" do
total = 1
factor = 83
expectation = [1,0]
Primes.destroy_domain_element_from_total(total, factor).should == expectation
end
it "takes factors of 2 from 2" do
total = 2
factor = 2
expectation = [1, 1]
Primes.destroy_domain_element_from_total(total, factor).should == expectation
end
it "takes factors of 3 from 3" do
total = 3
factor = 3
expectation = [1, 1]
Primes.destroy_domain_element_from_total(total, factor).should == expectation
end
it "takes factors of 2 from 4" do
total = 4
factor = 2
expectation = [1, 2]
Primes.destroy_domain_element_from_total(total, factor).should == expectation
end
it "takes factors of 5 from 75" do
total = 75
factor = 5
expectation = [3, 2]
Primes.destroy_domain_element_from_total(total, factor).should == expectation
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment