Created
July 4, 2013 13:29
-
-
Save ackintosh/5927727 to your computer and use it in GitHub Desktop.
Fibonacci number
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Fib | |
def at(n) | |
raise "invalid index" unless n > 0 | |
if n == 1 | |
0 | |
elsif n == 2 | |
1 | |
else | |
at(n - 1) + at(n - 2) | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require './fib' | |
describe Fib do | |
describe '#at' do | |
context 'with 1' do | |
it 'should return 0' do | |
Fib.new.at(1).should == 0 | |
end | |
end | |
context 'with 2' do | |
it 'should return 1' do | |
Fib.new.at(2).should == 1 | |
end | |
end | |
context 'with 0' do | |
it 'should raise RuntimeError' do | |
lambda { Fib.new.at(0) }.should raise_error(RuntimeError) | |
end | |
end | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'test/unit' | |
require './fib' | |
class FibTest < Test::Unit::TestCase | |
def setup | |
@fib = Fib.new | |
end | |
def test_at1 | |
r = @fib.at 1 | |
assert_equal(0, r) | |
end | |
def test_at7 | |
r = @fib.at 7 | |
assert_equal(8, r) | |
end | |
def test_at0 | |
assert_raise RuntimeError do | |
@fib.at 0 | |
end | |
end | |
def teardown; end; | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment