Tested Array#bsearch more thoroughly
MODIFY
mrbgems/mruby-array-ext/test/array.rb
mrbgems/mruby-array-ext/test/array.rb
@@ -228,18 +228,38 @@ end
assert("Array#bsearch") do
# Find minimum mode
- a = [0, 4, 7, 10, 12]
- assert_include [4, 7], a.bsearch {|x| x >= 4 }
- assert_equal 7, a.bsearch {|x| x >= 6 }
+ a = [0, 2, 4]
assert_equal 0, a.bsearch{ |x| x >= -1 }
- assert_nil a.bsearch {|x| x >= 100 }
+ assert_equal 0, a.bsearch{ |x| x >= 0 }
+ assert_equal 2, a.bsearch{ |x| x >= 1 }
+ assert_equal 2, a.bsearch{ |x| x >= 2 }
+ assert_equal 4, a.bsearch{ |x| x >= 3 }
+ assert_equal 4, a.bsearch{ |x| x >= 4 }
+ assert_nil a.bsearch{ |x| x >= 5 }
# Find any mode
- a = [0, 4, 7, 10, 12]
- assert_include [4, 7], a.bsearch {|x| 1 - (x / 4).truncate }
- assert_nil a.bsearch {|x| 4 - (x / 2).truncate }
- assert_equal(nil, a.bsearch {|x| 1 })
- assert_equal(nil, a.bsearch {|x| -1 })
+ a = [0, 4, 8]
+ def between(lo, x, hi)
+ if x < lo
+ 1
+ elsif x > hi
+ -1
+ else
+ 0
+ end
+ end
+ assert_nil a.bsearch{ |x| between(-3, x, -1) }
+ assert_equal 0, a.bsearch{ |x| between(-1, x, 1) }
+ assert_nil a.bsearch{ |x| between( 1, x, 3) }
+ assert_equal 4, a.bsearch{ |x| between( 3, x, 5) }
+ assert_nil a.bsearch{ |x| between( 5, x, 7) }
+ assert_equal 8, a.bsearch{ |x| between( 7, x, 9) }
+ assert_nil a.bsearch{ |x| between( 9, x, 11) }
+
+ assert_equal 0, a.bsearch{ |x| between( 0, x, 3) }
+ assert_equal 4, a.bsearch{ |x| between( 0, x, 4) }
+ assert_equal 4, a.bsearch{ |x| between( 4, x, 8) }
+ assert_equal 8, a.bsearch{ |x| between( 5, x, 8) }
end
assert("Array#delete_if") do

Tested Array#bsearch more thoroughly