mirror of
https://github.com/ruby/ruby.git
synced 2022-11-09 12:17:21 -05:00
additional math operations
* test/drb/ut_large.rb (multiply, avg, median): add additional math operations to DRbLarge. [Fix GH-1086] git-svn-id: svn+ssh://ci.ruby-lang.org/ruby/trunk@53701 b2dd03c8-39d4-4d8f-98ff-823fe69b080e
This commit is contained in:
parent
8b9cd1dbb2
commit
c2e998d6a3
3 changed files with 69 additions and 4 deletions
|
@ -1,3 +1,8 @@
|
|||
Sun Jan 31 12:33:13 2016 Dan Kreiger <dan@dankreiger.com>
|
||||
|
||||
* test/drb/ut_large.rb (multiply, avg, median): add additional
|
||||
math operations to DRbLarge. [Fix GH-1086]
|
||||
|
||||
Sun Jan 31 12:19:15 2016 Kuniaki IGARASHI <igaiga@gmail.com>
|
||||
|
||||
* test/ruby/test_file_exhaustive.rb (test_lstat): Add lacking test
|
||||
|
|
|
@ -306,11 +306,18 @@ class TestDRbLarge < Test::Unit::TestCase
|
|||
ary = [2] * 10240
|
||||
assert_equal(10240, @there.size(ary))
|
||||
assert_equal(20480, @there.sum(ary))
|
||||
assert_equal(2 ** 10240, @there.multiply(ary))
|
||||
assert_equal(2, @there.avg(ary))
|
||||
assert_equal(2, @there.median(ary))
|
||||
end
|
||||
|
||||
def test_02_large_ary
|
||||
ary = ["Hello, World"] * 10240
|
||||
assert_equal(10240, @there.size(ary))
|
||||
assert_equal(ary[0..ary.length].inject(:+), @there.sum(ary))
|
||||
assert_raise (TypeError) {@there.multiply(ary)}
|
||||
assert_raise (TypeError) {@there.avg(ary)}
|
||||
assert_raise (TypeError) {@there.median(ary)}
|
||||
end
|
||||
|
||||
def test_03_large_ary
|
||||
|
@ -334,6 +341,41 @@ class TestDRbLarge < Test::Unit::TestCase
|
|||
end
|
||||
assert_kind_of(StandardError, exception)
|
||||
end
|
||||
|
||||
def test_06_array_operations
|
||||
ary = [1,50,3,844,7,45,23]
|
||||
assert_equal(7, @there.size(ary))
|
||||
assert_equal(973, @there.sum(ary))
|
||||
assert_equal(917217000, @there.multiply(ary))
|
||||
assert_equal(139.0, @there.avg(ary))
|
||||
assert_equal(23.0, @there.median(ary))
|
||||
|
||||
ary2 = [1,2,3,4]
|
||||
assert_equal(4, @there.size(ary2))
|
||||
assert_equal(10, @there.sum(ary2))
|
||||
assert_equal(24, @there.multiply(ary2))
|
||||
assert_equal(2.5, @there.avg(ary2))
|
||||
assert_equal(2.5, @there.median(ary2))
|
||||
|
||||
end
|
||||
|
||||
def test_07_one_element_array
|
||||
ary = [50]
|
||||
assert_equal(1, @there.size(ary))
|
||||
assert_equal(50, @there.sum(ary))
|
||||
assert_equal(50, @there.multiply(ary))
|
||||
assert_equal(50.0, @there.avg(ary))
|
||||
assert_equal(50.0, @there.median(ary))
|
||||
end
|
||||
|
||||
def test_08_empty_array
|
||||
ary = []
|
||||
assert_equal(0, @there.size(ary))
|
||||
assert_equal(nil, @there.sum(ary))
|
||||
assert_equal(nil, @there.multiply(ary))
|
||||
assert_equal(nil, @there.avg(ary))
|
||||
assert_equal(nil, @there.median(ary))
|
||||
end
|
||||
end
|
||||
|
||||
class TestBug4409 < Test::Unit::TestCase
|
||||
|
|
|
@ -13,11 +13,29 @@ class DRbLarge
|
|||
end
|
||||
|
||||
def sum(ary)
|
||||
sum = 0
|
||||
ary.each do |e|
|
||||
sum += e.to_i
|
||||
ary.inject(:+)
|
||||
end
|
||||
|
||||
def multiply(ary)
|
||||
ary.inject(:*)
|
||||
end
|
||||
|
||||
def avg(ary)
|
||||
return if ary.empty?
|
||||
if ary.any? {|n| n.is_a? String}
|
||||
raise TypeError
|
||||
else
|
||||
sum(ary).to_f / ary.count
|
||||
end
|
||||
end
|
||||
|
||||
def median(ary)
|
||||
return if ary.empty?
|
||||
if ary.any? {|n| n.is_a? String}
|
||||
raise TypeError
|
||||
else
|
||||
avg ary.sort[((ary.length - 1) / 2)..(ary.length / 2)]
|
||||
end
|
||||
sum
|
||||
end
|
||||
|
||||
def arg_test(*arg)
|
||||
|
|
Loading…
Reference in a new issue