From c2e998d6a3d0f9471677defc39b97ffdc4087404 Mon Sep 17 00:00:00 2001 From: nobu Date: Sun, 31 Jan 2016 03:34:21 +0000 Subject: [PATCH] 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 --- ChangeLog | 5 +++++ test/drb/test_drb.rb | 42 ++++++++++++++++++++++++++++++++++++++++++ test/drb/ut_large.rb | 26 ++++++++++++++++++++++---- 3 files changed, 69 insertions(+), 4 deletions(-) diff --git a/ChangeLog b/ChangeLog index 40fe9f7166..c731d662b6 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Sun Jan 31 12:33:13 2016 Dan Kreiger + + * 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 * test/ruby/test_file_exhaustive.rb (test_lstat): Add lacking test diff --git a/test/drb/test_drb.rb b/test/drb/test_drb.rb index dd2855e3b0..681a26b606 100644 --- a/test/drb/test_drb.rb +++ b/test/drb/test_drb.rb @@ -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 diff --git a/test/drb/ut_large.rb b/test/drb/ut_large.rb index c00279e7cb..9376ff119d 100644 --- a/test/drb/ut_large.rb +++ b/test/drb/ut_large.rb @@ -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)