Sha256: eb5ef9c9619804d8ee904a8f71aba5754ff6a381fc4622e6d12337d258339842

Contents?: true

Size: 1.63 KB

Versions: 1

Compression:

Stored size: 1.63 KB

Contents

#!/usr/bin/env ruby

require 'test/unit'
require 'rubygems'
require 'pmap'

class Pmap_Test < Test::Unit::TestCase

  def bad_test_noproc_range
    range = (1..10)
    assert_equal(range.map, range.pmap)
  end

  def test_basic_range
    proc = Proc.new {|x| x*x}
    range = (1..10)
    assert_equal(range.map(&proc), range.pmap(&proc))
  end

  def bad_test_noproc_array
    array = (1..10).to_a
    assert_equal(array.map, array.pmap)
  end

  def test_basic_array
    proc = Proc.new {|x| x*x*x}
    array = (1..10).to_a
    assert_equal(array.map(&proc), array.pmap(&proc))
  end

  def test_time_savings
    start = Time.now
    (1..10).pmap{ sleep 1 }
    elapsed = Time.now-start
    assert(elapsed < 2, 'Parallel sleeps too slow: %.1f seconds' % elapsed)
  end

  def test_bad_thread_limits
    assert_raise(ArgumentError) {(1..10).pmap(-1){ sleep 1 }}
    assert_raise(ArgumentError) {(1..10).peach(0){ sleep 1 }}
    assert_raise(ArgumentError) {(1..10).peach(0.99){ sleep 1 }}
    assert_raise(ArgumentError) {(1..10).pmap('a'){ sleep 1 }}
    assert_raise(ArgumentError) {(1..10).peach([1,2,3]){ sleep 1 }}
  end

  def test_thread_limits
    start = Time.now
    (1..10).pmap(5){ sleep 1 }
    elapsed = Time.now-start
    assert(elapsed >= 2, 'Limited threads too fast: %.1f seconds' % elapsed)
    assert(elapsed <  3, 'Parallel sleeps too slow: %.1f seconds' % elapsed)
  end

  def test_defaut_thread_limit
    start = Time.now
    (1..128).pmap{ sleep 1 }
    elapsed = Time.now-start
    assert(elapsed >= 2, 'Limited threads too fast: %.1f seconds' % elapsed)
    assert(elapsed <  3, 'Parallel sleeps too slow: %.1f seconds' % elapsed)
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pmap-0.0.3 test/pmap_test.rb