2015-12-16 00:07:31 -05:00
|
|
|
# frozen_string_literal: false
|
2007-05-28 21:59:53 -04:00
|
|
|
require 'test/unit'
|
2014-11-28 19:36:33 -05:00
|
|
|
EnvUtil.suppress_warning {require 'continuation'}
|
2007-08-24 22:03:44 -04:00
|
|
|
require 'fiber'
|
2007-05-28 21:59:53 -04:00
|
|
|
|
|
|
|
class TestContinuation < Test::Unit::TestCase
|
|
|
|
def test_create
|
|
|
|
assert_equal(:ok, callcc{:ok})
|
|
|
|
assert_equal(:ok, callcc{|c| c.call :ok})
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_call
|
|
|
|
assert_equal(:ok, callcc{|c| c.call :ok})
|
|
|
|
|
|
|
|
ary = []
|
|
|
|
ary << callcc{|c|
|
|
|
|
@cont = c
|
|
|
|
:a
|
|
|
|
}
|
|
|
|
@cont.call :b if ary.length < 3
|
|
|
|
assert_equal([:a, :b, :b], ary)
|
|
|
|
end
|
|
|
|
|
2007-05-30 01:56:13 -04:00
|
|
|
def test_check_localvars
|
|
|
|
vv = 0
|
|
|
|
@v = 0
|
|
|
|
@ary = []
|
|
|
|
[1, 2, 3].each{|i|
|
|
|
|
callcc {|k| @k = k}
|
|
|
|
@v += 1
|
|
|
|
vv += 1
|
|
|
|
}
|
|
|
|
@ary << [vv, @v]
|
|
|
|
@k.call if @v < 10
|
|
|
|
assert_equal((3..10).map{|e| [e, e]}, @ary)
|
|
|
|
end
|
|
|
|
|
2007-05-28 21:59:53 -04:00
|
|
|
def test_error
|
|
|
|
cont = callcc{|c| c}
|
2017-12-12 13:44:49 -05:00
|
|
|
Thread.new{
|
|
|
|
assert_raise(RuntimeError){
|
|
|
|
cont.call
|
|
|
|
}
|
|
|
|
}.join
|
2007-05-28 21:59:53 -04:00
|
|
|
assert_raise(LocalJumpError){
|
|
|
|
callcc
|
|
|
|
}
|
2007-06-05 21:56:49 -04:00
|
|
|
assert_raise(RuntimeError){
|
|
|
|
c = nil
|
|
|
|
Fiber.new do
|
|
|
|
callcc {|c2| c = c2 }
|
2007-08-25 23:31:20 -04:00
|
|
|
end.resume
|
2007-06-05 21:56:49 -04:00
|
|
|
c.call
|
|
|
|
}
|
2007-05-28 21:59:53 -04:00
|
|
|
end
|
2008-05-21 22:40:50 -04:00
|
|
|
|
|
|
|
def test_ary_flatten
|
|
|
|
assert_normal_exit %q{
|
|
|
|
require 'continuation'
|
|
|
|
n = 0
|
|
|
|
o = Object.new
|
|
|
|
def o.to_ary() callcc {|k| $k = k; [1,2,3]} end
|
|
|
|
[10,20,o,30,o,40].flatten.inspect
|
|
|
|
n += 1
|
|
|
|
$k.call if n < 100
|
|
|
|
}, '[ruby-dev:34798]'
|
|
|
|
end
|
2008-05-22 04:41:07 -04:00
|
|
|
|
|
|
|
def test_marshal_dump
|
|
|
|
assert_normal_exit %q{
|
|
|
|
require 'continuation'
|
|
|
|
n = 0
|
|
|
|
o = Object.new
|
|
|
|
def o.marshal_dump() callcc {|k| $k = k }; "fofof" end
|
|
|
|
a = [1,2,3,o,4,5,6]
|
|
|
|
Marshal.dump(a).inspect
|
|
|
|
n += 1
|
|
|
|
$k.call if n < 100
|
|
|
|
}, '[ruby-dev:34802]'
|
|
|
|
end
|
|
|
|
|
2011-07-21 07:09:52 -04:00
|
|
|
def tracing_with_set_trace_func
|
2012-04-30 10:09:36 -04:00
|
|
|
orig_thread = Thread.current
|
2011-07-21 07:09:52 -04:00
|
|
|
cont = nil
|
|
|
|
func = lambda do |*args|
|
2012-04-30 10:09:36 -04:00
|
|
|
if orig_thread == Thread.current
|
2012-08-16 07:41:24 -04:00
|
|
|
if cont
|
|
|
|
@memo += 1
|
|
|
|
c = cont
|
|
|
|
cont = nil
|
2018-11-26 14:59:08 -05:00
|
|
|
begin
|
|
|
|
c.call(nil)
|
|
|
|
rescue RuntimeError
|
|
|
|
set_trace_func(nil)
|
|
|
|
end
|
2012-08-16 07:41:24 -04:00
|
|
|
end
|
2012-04-30 10:09:36 -04:00
|
|
|
end
|
2011-07-21 07:09:52 -04:00
|
|
|
end
|
|
|
|
cont = callcc { |cc| cc }
|
2018-11-26 14:59:08 -05:00
|
|
|
|
2011-07-21 07:09:52 -04:00
|
|
|
if cont
|
|
|
|
set_trace_func(func)
|
|
|
|
else
|
|
|
|
set_trace_func(nil)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-11-26 14:59:08 -05:00
|
|
|
def _test_tracing_with_set_trace_func
|
2011-07-21 07:09:52 -04:00
|
|
|
@memo = 0
|
|
|
|
tracing_with_set_trace_func
|
|
|
|
tracing_with_set_trace_func
|
|
|
|
tracing_with_set_trace_func
|
2018-11-26 14:59:08 -05:00
|
|
|
assert_equal 0, @memo
|
2011-07-21 07:09:52 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def tracing_with_thread_set_trace_func
|
|
|
|
cont = nil
|
|
|
|
func = lambda do |*args|
|
2012-08-16 07:41:24 -04:00
|
|
|
if cont
|
|
|
|
@memo += 1
|
|
|
|
c = cont
|
|
|
|
cont = nil
|
2018-11-26 14:59:08 -05:00
|
|
|
begin
|
|
|
|
c.call(nil)
|
|
|
|
rescue RuntimeError
|
|
|
|
Thread.current.set_trace_func(nil)
|
|
|
|
end
|
2012-08-16 07:41:24 -04:00
|
|
|
end
|
2011-07-21 07:09:52 -04:00
|
|
|
end
|
|
|
|
cont = callcc { |cc| cc }
|
|
|
|
if cont
|
|
|
|
Thread.current.set_trace_func(func)
|
|
|
|
else
|
2012-08-20 21:41:07 -04:00
|
|
|
Thread.current.set_trace_func(nil)
|
2011-07-21 07:09:52 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_tracing_with_thread_set_trace_func
|
|
|
|
@memo = 0
|
|
|
|
tracing_with_thread_set_trace_func
|
|
|
|
tracing_with_thread_set_trace_func
|
|
|
|
tracing_with_thread_set_trace_func
|
|
|
|
assert_equal 3, @memo
|
|
|
|
end
|
2007-05-28 21:59:53 -04:00
|
|
|
end
|