Skip to content

Instantly share code, notes, and snippets.

@kylessnell
Forked from dbc-challenges/rspec_binary_search.rb
Created October 18, 2012 16:51
Show Gist options
  • Save kylessnell/3913212 to your computer and use it in GitHub Desktop.
Save kylessnell/3913212 to your computer and use it in GitHub Desktop.
Rspec Binary Search
describe 'binatry_search' do
let(:test_array_1) { [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] }
let(:test_array_2) { (1..100).to_a }
let(:test_object_1) { 3 }
let(:test_object_2) { 10 }
let(:test_object_3) { 39 }
let(:test_object_4) { 97 }
it 'is defined' do
defined?(binary_search).should eq 'method'
end
it 'requires two arguements' do
method(:binary_search).arity.should eq 2
end
it 'raises an error if 2nd argument is not an array' do
expect { binary_search("a", "b") }.to raise_error
end
it 'returns the correct object in a small array' do
binary_search(test_object_1, test_array_1).should eq 2
binary_search(test_object_2, test_array_1).should eq 9
end
it 'returns the correct object in a larger array' do
binary_search(test_object_3, test_array_2).should eq 38
binary_search(test_object_4, test_array_2).should eq 96
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment