File: peach_test.rb

package info (click to toggle)
ruby-peach 0.5.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bullseye, forky, sid, trixie
  • size: 152 kB
  • sloc: ruby: 174; makefile: 4
file content (46 lines) | stat: -rw-r--r-- 1,243 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require File.join(File.dirname(__FILE__), "test_helper")

require "peach"

require 'thread'

class PeachTest < Test::Unit::TestCase
  [:peach, :pmap, :pselect].each do |f|
    context "Parallel function #{f}" do
      normal_f = f.to_s[1..-1].to_sym

      setup do
        @data = [1, 2, 3, 5, 8]*1001
        @block = lambda{|i| i**2}
      end
      should "return the same result as #{normal_f}" do
        assert_equal @data.send(normal_f, &@block),
          @data.send(f, 100, &@block)
      end

      should "return the same result as #{normal_f} for empty list" do
        assert_equal [].send(normal_f, &@block),
          [].send(f, nil, &@block)
      end
    end
  end

  [:peach, :pmap, :pselect].each do |f|
    context "#{f}" do
      [nil, 101, 99, 51, 49, 20, 5, 1].each do |pool|
        should "invoke the block exactly once for each array item," +
          " with thread pool size of #{pool.inspect}" do
          source_array = (0...100).to_a
          q = Queue.new()
          source_array.send(f, pool) {|i| q.push(i)}
          result_array = []
          until q.empty?
            result_array << q.pop
          end
          assert_equal source_array, result_array.sort
        end
      end
    end
  end

end