diff --git a/Gemfile.lock b/Gemfile.lock index f9141d2..becd703 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -6,15 +6,29 @@ PATH GEM remote: http://rubygems.org/ specs: - rake (0.9.2.2) - shoulda (3.0.1) - shoulda-context (~> 1.0.0) - shoulda-matchers (~> 1.0.0) - shoulda-context (1.0.0) - shoulda-matchers (1.0.0) + activesupport (4.0.2) + i18n (~> 0.6, >= 0.6.4) + minitest (~> 4.2) + multi_json (~> 1.3) + thread_safe (~> 0.1) + tzinfo (~> 0.3.37) + atomic (1.1.14-java) + i18n (0.6.9) + minitest (4.7.5) + multi_json (1.8.2) + rake (10.1.1) + shoulda (3.5.0) + shoulda-context (~> 1.0, >= 1.0.1) + shoulda-matchers (>= 1.4.1, < 3.0) + shoulda-context (1.1.6) + shoulda-matchers (2.4.0) + activesupport (>= 3.0.0) + thread_safe (0.1.3-java) + atomic + tzinfo (0.3.38) PLATFORMS - ruby + java DEPENDENCIES peach! diff --git a/lib/peach.rb b/lib/peach.rb index c1e8aa9..c338eb6 100644 --- a/lib/peach.rb +++ b/lib/peach.rb @@ -39,7 +39,7 @@ def pmap(pool = nil, &b) end def pselect(pool = nil, &b) - results, result = [],[] + results = [] lock = Mutex.new _peach_run(pool) do |thread_slice, idx, div| @@ -48,7 +48,14 @@ def pselect(pool = nil, &b) results[idx] = local_result end end - results.each {|x| result += x if x} - result + results.reduce([]) {|result, x| result += x if x} + end +end + +class Hash + alias_method :_pselect, :pselect + + def pselect(pool = nil, &b) + Hash[_pselect(pool, &b)] end end diff --git a/test/peach_test.rb b/test/peach_test.rb index 4a9c695..5910ecd 100644 --- a/test/peach_test.rb +++ b/test/peach_test.rb @@ -43,4 +43,23 @@ class PeachTest < Test::Unit::TestCase end end + [:peach, :pmap, :pselect].each do |f| + context "Parallel function Hash##{f}" do + normal_f = f.to_s[1..-1].to_sym + + setup do + @data = ([1, 2, 3, 5, 8]*1001).each_with_object({}){|n, out| out[n] = n} + @block = lambda{|k,v| v**2} + end + should "return the same result as Hash##{normal_f}" do + assert_equal @data.send(normal_f, &@block), + @data.send(f, 100, &@block) + end + + should "return the same result as Hash##{normal_f} when empty" do + assert_equal Hash.new.send(normal_f, &@block), + Hash.new.send(f, nil, &@block) + end + end + end end