test_range.rb: all assertions

* test/ruby/test_range.rb (test_range_bsearch_for_floats): test
  all assertions.

git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@56295 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
nobu 2016-09-30 04:02:17 +00:00
parent c249453c2a
commit d84d0f27b9

View File

@ -521,20 +521,22 @@ class TestRange < Test::Unit::TestCase
assert_in_delta(7.0, (0.0..10).bsearch {|x| 7.0 - x }) assert_in_delta(7.0, (0.0..10).bsearch {|x| 7.0 - x })
end end
def check_bsearch_values(range, search) def check_bsearch_values(range, search, a)
from, to = range.begin, range.end from, to = range.begin, range.end
cmp = range.exclude_end? ? :< : :<= cmp = range.exclude_end? ? :< : :<=
r = nil
# (0) trivial test a.for "(0) trivial test" do
r = Range.new(to, from, range.exclude_end?).bsearch do |x| r = Range.new(to, from, range.exclude_end?).bsearch do |x|
fail "#{to}, #{from}, #{range.exclude_end?}, #{x}" fail "#{to}, #{from}, #{range.exclude_end?}, #{x}"
end end
assert_equal nil, r assert_nil r
r = (to...to).bsearch do r = (to...to).bsearch do
fail fail
end end
assert_equal nil, r assert_nil r
end
# prepare for others # prepare for others
yielded = [] yielded = []
@ -543,27 +545,31 @@ class TestRange < Test::Unit::TestCase
val >= search val >= search
end end
# (1) log test a.for "(1) log test" do
max = case from max = case from
when Float then 65 when Float then 65
when Integer then Math.log(to-from+(range.exclude_end? ? 0 : 1), 2).to_i + 1 when Integer then Math.log(to-from+(range.exclude_end? ? 0 : 1), 2).to_i + 1
end end
assert_operator yielded.size, :<=, max assert_operator yielded.size, :<=, max
end
# (2) coverage test a.for "(2) coverage test" do
expect = if search < from expect = case
when search < from
from from
elsif search.send(cmp, to) when search.send(cmp, to)
search search
else else
nil nil
end end
assert_equal expect, r assert_equal expect, r
end
# (3) uniqueness test a.for "(3) uniqueness test" do
assert_equal nil, yielded.uniq! assert_nil yielded.uniq!
end
# (4) end of range test a.for "(4) end of range test" do
case case
when range.exclude_end? when range.exclude_end?
assert_not_include yielded, to assert_not_include yielded, to
@ -572,28 +578,33 @@ class TestRange < Test::Unit::TestCase
assert_include yielded, to assert_include yielded, to
assert_equal search == to ? to : nil, r assert_equal search == to ? to : nil, r
end end
end
# start of range test a.for "(5) start of range test" do
if search <= from if search <= from
assert_include yielded, from assert_include yielded, from
assert_equal from, r assert_equal from, r
end end
end
# (5) out of range test a.for "(6) out of range test" do
yielded.each do |val| yielded.each do |val|
assert_operator from, :<=, val assert_operator from, :<=, val
assert_send [val, cmp, to] assert_send [val, cmp, to]
end end
end end
end
def test_range_bsearch_for_floats def test_range_bsearch_for_floats
ints = [-1 << 100, -123456789, -42, -1, 0, 1, 42, 123456789, 1 << 100] ints = [-1 << 100, -123456789, -42, -1, 0, 1, 42, 123456789, 1 << 100]
floats = [-Float::INFINITY, -Float::MAX, -42.0, -4.2, -Float::EPSILON, -Float::MIN, 0.0, Float::MIN, Float::EPSILON, Math::PI, 4.2, 42.0, Float::MAX, Float::INFINITY] floats = [-Float::INFINITY, -Float::MAX, -42.0, -4.2, -Float::EPSILON, -Float::MIN, 0.0, Float::MIN, Float::EPSILON, Math::PI, 4.2, 42.0, Float::MAX, Float::INFINITY]
all_assertions do |a|
[ints, floats].each do |values| [ints, floats].each do |values|
values.combination(2).to_a.product(values).each do |(from, to), search| values.combination(2).to_a.product(values).each do |(from, to), search|
check_bsearch_values(from..to, search) check_bsearch_values(from..to, search, a)
check_bsearch_values(from...to, search) check_bsearch_values(from...to, search, a)
end
end end
end end
end end