I just got a new computer and need to get stuff moved over to it.
Among “stuff” is Ruby, which mediates building my web site.
I had taken to building my little projects with a project file that
looks like this:
project.rb:
···
==========
require 'rubyunit’
require “binsrch.rb”
With the actual program and tests (for simple examples) in the second
required file, as shown below.
I used to reference TestCase thus:
class TestBinsrch < TestCase
in Ruby 1.6, but now in 1.8 I have to say
class TestBinsrch < Test::Unit::TestCase
When I make that change, things seem to be working OK. What happened?
What would I have to do to make it go back to the other way?
Thanks!!
Ron Jeffries
www.XProgramming.com
===============================
binsrch.rb:
def binsrch ( array, item )
low = 0
high = array.length - 1
while low <= high
half = low + (high-low)/2
puts "item #{item} low #{low} half #{half} high #{high} compare
#{array[half]}"
return half if array[half] == item
if array[half] < item
puts “changing low”
low = half + 1
else
puts “changing high”
high = half - 1
end
puts “new low #{low} high #{high}”
end
return -1
end
class TestBinsrch < TestCase
def test1000
array = []
(1…1000).each { | i | array << i }
assert_equal(76, binsrch(array,77))
assert_equal(75, binsrch(array,76))
assert_equal(77, binsrch(array,78))
(1…1000).each { | item |
assert_equal(item-1, binsrch(array,item))
}
end
def test3
array = [1,2,3,4,5,6,7,8,9]
item = 11
assert_equal(-1, binsrch(array,item))
end
def test4
array = [1,2,3,4,5,6,7,8,9]
item = 1
assert_equal(0, binsrch(array,item))
end
def test1
array = [1,2,3,4,5,6,7,8,9]
item = 5
assert_equal(4, binsrch(array,item))
end
def test2
array = [1,2,3,4,5,6,7,8,9]
item = 0
assert_equal(-1, binsrch(array,item))
end
def test5
array = [1,2,3,4,5,6,7,8,9]
item = 9
assert_equal(8, binsrch(array,item))
end
def test6
array = [1,2,3,4,5,6,7,8,9]
item = 3
assert_equal(2, binsrch(array,item))
end
end