1
0
Fork 0
mirror of https://github.com/ruby/ruby.git synced 2022-11-09 12:17:21 -05:00

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 })
end
def check_bsearch_values(range, search)
def check_bsearch_values(range, search, a)
from, to = range.begin, range.end
cmp = range.exclude_end? ? :< : :<=
r = nil
# (0) trivial test
r = Range.new(to, from, range.exclude_end?).bsearch do |x|
fail "#{to}, #{from}, #{range.exclude_end?}, #{x}"
end
assert_equal nil, r
a.for "(0) trivial test" do
r = Range.new(to, from, range.exclude_end?).bsearch do |x|
fail "#{to}, #{from}, #{range.exclude_end?}, #{x}"
end
assert_nil r
r = (to...to).bsearch do
fail
r = (to...to).bsearch do
fail
end
assert_nil r
end
assert_equal nil, r
# prepare for others
yielded = []
@ -543,46 +545,53 @@ class TestRange < Test::Unit::TestCase
val >= search
end
# (1) log test
max = case from
when Float then 65
when Integer then Math.log(to-from+(range.exclude_end? ? 0 : 1), 2).to_i + 1
end
assert_operator yielded.size, :<=, max
# (2) coverage test
expect = if search < from
from
elsif search.send(cmp, to)
search
else
nil
end
assert_equal expect, r
# (3) uniqueness test
assert_equal nil, yielded.uniq!
# (4) end of range test
case
when range.exclude_end?
assert_not_include yielded, to
assert_not_equal r, to
when search >= to
assert_include yielded, to
assert_equal search == to ? to : nil, r
a.for "(1) log test" do
max = case from
when Float then 65
when Integer then Math.log(to-from+(range.exclude_end? ? 0 : 1), 2).to_i + 1
end
assert_operator yielded.size, :<=, max
end
# start of range test
if search <= from
assert_include yielded, from
assert_equal from, r
a.for "(2) coverage test" do
expect = case
when search < from
from
when search.send(cmp, to)
search
else
nil
end
assert_equal expect, r
end
# (5) out of range test
yielded.each do |val|
assert_operator from, :<=, val
assert_send [val, cmp, to]
a.for "(3) uniqueness test" do
assert_nil yielded.uniq!
end
a.for "(4) end of range test" do
case
when range.exclude_end?
assert_not_include yielded, to
assert_not_equal r, to
when search >= to
assert_include yielded, to
assert_equal search == to ? to : nil, r
end
end
a.for "(5) start of range test" do
if search <= from
assert_include yielded, from
assert_equal from, r
end
end
a.for "(6) out of range test" do
yielded.each do |val|
assert_operator from, :<=, val
assert_send [val, cmp, to]
end
end
end
@ -590,10 +599,12 @@ class TestRange < Test::Unit::TestCase
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]
[ints, floats].each do |values|
values.combination(2).to_a.product(values).each do |(from, to), search|
check_bsearch_values(from..to, search)
check_bsearch_values(from...to, search)
all_assertions do |a|
[ints, floats].each do |values|
values.combination(2).to_a.product(values).each do |(from, to), search|
check_bsearch_values(from..to, search, a)
check_bsearch_values(from...to, search, a)
end
end
end
end