require "test/unit" require "benchmark" module Applanet module Research module Ruby module Runtime module Closure class ClosureTest < Test::Unit::TestCase def test_closure self.assert_equal(0, @total_dispatches) dispatch_yield { @total_dispatches += 1 } self.assert_equal(1, @total_dispatches) end def test_dispatch_yield_with_block dispatch("test_dispatch_yield_with_block") \ { dispatch_yield { @total_dispatches += 1 } } end def test_dispatch_call_with_block dispatch("test_dispatch_call_with_block") \ { dispatch_call { @total_dispatches += 1 } } end def test_dispatch_yield_with_block_from_proc dispatch("test_dispatch_yield_with_block_from_proc") \ { dispatch_yield(&@proc) } end def test_dispatch_call_with_block_from_proc dispatch("test_dispatch_call_with_block_from_proc") \ { dispatch_call(&@proc) } end def test_dispatch_yield_with_block_from_lambda dispatch("test_dispatch_yield_with_block_from_lambda") \ { dispatch_yield(&@lambda) } end def test_dispatch_call_with_block_from_lambda dispatch("test_dispatch_call_with_block_from_lambda") \ { dispatch_call(&@lambda) } end protected def setup @total_dispatches = 0 @lambda = lambda { @total_dispatches += 1 } @proc = Proc.new { @total_dispatches += 1 } end private def dispatch_yield yield end def dispatch_call(&block) block.call end def dispatch(label = "") Benchmark.bmbm { |bmbm| bmbm.report(label) { 10000.times { yield } } } print "total dispatches: #@total_dispatches\n" end end end end end end end