aboutsummaryrefslogtreecommitdiff
path: root/tests/cyclic.lua
diff options
context:
space:
mode:
Diffstat (limited to 'tests/cyclic.lua')
-rw-r--r--tests/cyclic.lua64
1 files changed, 64 insertions, 0 deletions
diff --git a/tests/cyclic.lua b/tests/cyclic.lua
new file mode 100644
index 0000000..06452bd
--- /dev/null
+++ b/tests/cyclic.lua
@@ -0,0 +1,64 @@
1--
2-- CYCLIC.LUA
3--
4-- Test program for Lua Lanes
5--
6
7require "lanes"
8
9local table_concat= assert(table.concat)
10
11local function WR(str,...)
12 for i=1,select('#',...) do
13 str= str.."\t"..tostring( select(i,...) )
14 end
15 io.stderr:write( str..'\n' )
16end
17
18local function same(k,l)
19 return k==l and "same" or ("NOT SAME: "..k.." "..l)
20end
21
22local a= {}
23local b= {a}
24a[1]= b
25
26-- Getting the tables as upvalues should still have the <-> linkage
27--
28local function lane1()
29 WR( "Via upvalue: ", same(a,b[1]), same(a[1],b) )
30 assert( a[1]==b )
31 assert( b[1]==a )
32end
33local L1= lanes.gen( "io", lane1 )()
34 -- ...running
35
36-- Getting the tables as parameters should also keep the linkage
37--
38local function lane2( aa, bb )
39 WR( "Via parameters:", same(aa,bb[1]), same(aa[1],bb) )
40 assert( aa[1]==bb )
41 assert( bb[1]==aa )
42end
43local L2= lanes.gen( "io", lane2 )( a, b )
44 -- ...running
45
46-- Really unnecessary, but let's try a directly recursive table
47--
48c= {}
49c.a= c
50
51local function lane3( cc )
52 WR( "Directly recursive: ", same(cc, cc.a) )
53 assert( cc and cc.a==cc )
54end
55local L3= lanes.gen("io", lane3)(c)
56
57-- Without a wait, exit from the main lane will close the process
58--
59-- Waiting for multiple lanes at once could be done using a Linda
60-- (but we're okay waiting them in order)
61--
62L1:join()
63L2:join()
64L3:join()