github.com/hirochachacha/plua@v0.0.0-20170217012138-c82f520cc725/stdlib/coroutine/testdata/coroutine.lua (about)

     1  co = coroutine.create(function(x)
     2    assert(x == 10)
     3    local a, b = coroutine.yield(x)
     4    assert(a == 111 and b == 120)
     5    return x
     6  end)
     7  
     8  ok, ret = coroutine.resume(co, 10)
     9  assert(ok and ret == 10)
    10  
    11  ok, ret = coroutine.resume(co, 111, 120)
    12  assert(ok and ret == 10)
    13  
    14  ok, ret = coroutine.resume(co, 1)
    15  assert(not ok and ret == "cannot resume dead coroutine")
    16  
    17  
    18  co = coroutine.create(function()
    19  	error("test")
    20  end)
    21  
    22  ok, ret = coroutine.resume(co)
    23  assert(not ok and (ret == "testdata/coroutine.lua:19: test" or ret == "testdata\\coroutine.lua:19: test"))
    24  
    25  iter = coroutine.wrap(function()
    26  	for i = 0, 3 do
    27  		coroutine.yield(i)
    28  	end
    29  end)
    30  
    31  assert(iter() == 0)
    32  assert(iter() == 1)
    33  assert(iter() == 2)
    34  assert(iter() == 3)
    35  assert(iter() == nil)