[前][次][番号順一覧][スレッド一覧]

ruby-changes:41627

From: nobu <ko1@a...>
Date: Sun, 31 Jan 2016 12:33:19 +0900 (JST)
Subject: [ruby-changes:41627] nobu:r53701 (trunk): additional math operations

nobu	2016-01-31 12:34:21 +0900 (Sun, 31 Jan 2016)

  New Revision: 53701

  https://svn.ruby-lang.org/cgi-bin/viewvc.cgi?view=revision&revision=53701

  Log:
    additional math operations
    
    * test/drb/ut_large.rb (multiply, avg, median): add additional
      math operations to DRbLarge.  [Fix GH-1086]

  Modified files:
    trunk/ChangeLog
    trunk/test/drb/test_drb.rb
    trunk/test/drb/ut_large.rb
Index: test/drb/ut_large.rb
===================================================================
--- test/drb/ut_large.rb	(revision 53700)
+++ test/drb/ut_large.rb	(revision 53701)
@@ -13,11 +13,29 @@ class DRbLarge https://github.com/ruby/ruby/blob/trunk/test/drb/ut_large.rb#L13
   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)
Index: test/drb/test_drb.rb
===================================================================
--- test/drb/test_drb.rb	(revision 53700)
+++ test/drb/test_drb.rb	(revision 53701)
@@ -306,11 +306,18 @@ class TestDRbLarge < Test::Unit::TestCas https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L306
     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::TestCas https://github.com/ruby/ruby/blob/trunk/test/drb/test_drb.rb#L341
     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
Index: ChangeLog
===================================================================
--- ChangeLog	(revision 53700)
+++ ChangeLog	(revision 53701)
@@ -1,3 +1,8 @@ https://github.com/ruby/ruby/blob/trunk/ChangeLog#L1
+Sun Jan 31 12:33:13 2016  Dan Kreiger  <dan@d...>
+
+	* 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@g...>
 
 	* test/ruby/test_file_exhaustive.rb (test_lstat): Add lacking test

--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]