diff options
Diffstat (limited to 'tests/cyclic.lua')
-rw-r--r-- | tests/cyclic.lua | 64 |
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 | |||
7 | require "lanes" | ||
8 | |||
9 | local table_concat= assert(table.concat) | ||
10 | |||
11 | local function WR(str,...) | ||
12 | for i=1,select('#',...) do | ||
13 | str= str.."\t"..tostring( select(i,...) ) | ||
14 | end | ||
15 | io.stderr:write( str..'\n' ) | ||
16 | end | ||
17 | |||
18 | local function same(k,l) | ||
19 | return k==l and "same" or ("NOT SAME: "..k.." "..l) | ||
20 | end | ||
21 | |||
22 | local a= {} | ||
23 | local b= {a} | ||
24 | a[1]= b | ||
25 | |||
26 | -- Getting the tables as upvalues should still have the <-> linkage | ||
27 | -- | ||
28 | local function lane1() | ||
29 | WR( "Via upvalue: ", same(a,b[1]), same(a[1],b) ) | ||
30 | assert( a[1]==b ) | ||
31 | assert( b[1]==a ) | ||
32 | end | ||
33 | local L1= lanes.gen( "io", lane1 )() | ||
34 | -- ...running | ||
35 | |||
36 | -- Getting the tables as parameters should also keep the linkage | ||
37 | -- | ||
38 | local 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 ) | ||
42 | end | ||
43 | local L2= lanes.gen( "io", lane2 )( a, b ) | ||
44 | -- ...running | ||
45 | |||
46 | -- Really unnecessary, but let's try a directly recursive table | ||
47 | -- | ||
48 | c= {} | ||
49 | c.a= c | ||
50 | |||
51 | local function lane3( cc ) | ||
52 | WR( "Directly recursive: ", same(cc, cc.a) ) | ||
53 | assert( cc and cc.a==cc ) | ||
54 | end | ||
55 | local 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 | -- | ||
62 | L1:join() | ||
63 | L2:join() | ||
64 | L3:join() | ||