Skip to content

Commit

Permalink
fixed bug. it should be test!!.
Browse files Browse the repository at this point in the history
  • Loading branch information
walf443 committed Sep 12, 2008
1 parent 01925a3 commit 66d08b3
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
11 changes: 8 additions & 3 deletions lib/ipaddr_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,12 +134,17 @@ def include? ip
# binary search
# SEE ALSO: http://dsas.blog.klab.org/archives/51293334.html
def binary_search ip, &block
ipaddr = IPAddr.new(ip)
ipaddr = nil
if ip.kind_of? IPAddr
ipaddr = ip
else
ipaddr = IPAddr.new(ip)
end
min_idx = 0
max_idx = @ip_list.size - 1
if @ip_list[max_idx] > ipaddr
if @ip_list[max_idx] < ipaddr
min_idx = max_idx
elsif @ip_list[min_idx] < ipaddr
elsif @ip_list[min_idx] > ipaddr
max_idx = min_idx
else
span = max_idx - min_idx
Expand Down
23 changes: 23 additions & 0 deletions spec/ipaddr_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,27 @@
require 'ipaddr_list'

describe IPAddrList do
before do
@ipaddr_list = %w( 192.168.0.1 127.0.0.1 )
end

it 'should include? in :BinarySearch' do
ipaddr_list = IPAddrList.new(@ipaddr_list, :BinarySearch)
ipaddr_list.should include('127.0.0.1')
end

it 'should not include? in :BinarySearch' do
ipaddr_list = IPAddrList.new(@ipaddr_list, :BinarySearch)
ipaddr_list.should_not include('192.168.1.1')
end

it 'should include? in :LinearSearch' do
ipaddr_list = IPAddrList.new(@ipaddr_list, :LinearSearch)
ipaddr_list.should include('127.0.0.1')
end

it 'should include? in :LinearSearch' do
ipaddr_list = IPAddrList.new(@ipaddr_list, :LinearSearch)
ipaddr_list.should_not include('192.168.1.1')
end
end

0 comments on commit 66d08b3

Please sign in to comment.