aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/appendud.lua58
-rw-r--r--tests/basic.lua52
-rw-r--r--tests/func_is_string.lua12
3 files changed, 96 insertions, 26 deletions
diff --git a/tests/appendud.lua b/tests/appendud.lua
new file mode 100644
index 0000000..afea0e9
--- /dev/null
+++ b/tests/appendud.lua
@@ -0,0 +1,58 @@
1--
2-- APPENDUD.LUA
3--
4-- Lanes version for John Belmonte's challenge on Lua list (about finalizers):
5-- <http://lua-users.org/lists/lua-l/2008-02/msg00243.html>
6--
7-- Needs Lanes >= 2.0.3
8--
9require "lanes"
10
11local _tab = {
12 beginupdate = function (this) print('tab.beginupdate') end;
13 endupdate = function (this) print('tab.endupdate') end;
14}
15local _ud = {
16 lock = function (this) print('ud.lock') end;
17 unlock = function (this) print('ud.unlock') end;
18 1,2,3,4,5;
19}
20
21--
22-- This sample is with the 'finalize/guard' patch applied (new keywords):
23--
24--function appendud(tab, ud)
25-- tab:beginupdate() finalize tab:endupdate() end
26-- ud:lock() finalize ud:unlock() end
27-- for i = 1,#ud do
28-- tab[#tab+1] = ud[i]
29-- end
30--end
31
32
33function appendud(tab, ud)
34io.stderr:write "Starting"
35 tab:beginupdate() set_finalizer( function() tab:endupdate() end )
36 ud:lock() set_finalizer( function() ud:unlock() end )
37 for i = 1,#ud do
38 tab[#tab+1] = ud[i]
39 end
40io.stderr:write "Ending"
41 return tab -- need to return 'tab' since we're running in a separate thread
42 -- ('tab' is passed over lanes by value, not by reference)
43end
44
45local t,err= lanes.gen( appendud )( _tab, _ud ) -- create & launch a thread
46assert(t)
47assert(not err)
48
49-- test
50
51t:join() -- Need to explicitly wait for the thread, since 'ipairs()' does not
52 -- value the '__index' metamethod (wouldn't it be cool if it did..?)
53
54io.stderr:write(t[1])
55
56for k,v in ipairs(t) do
57 print(k,v)
58end
diff --git a/tests/basic.lua b/tests/basic.lua
index ee31ed1..b1e8fd3 100644
--- a/tests/basic.lua
+++ b/tests/basic.lua
@@ -2,7 +2,7 @@
2-- BASIC.LUA Copyright (c) 2007-08, Asko Kauppi <akauppi@gmail.com> 2-- BASIC.LUA Copyright (c) 2007-08, Asko Kauppi <akauppi@gmail.com>
3-- 3--
4-- Selftests for Lua Lanes 4-- Selftests for Lua Lanes
5-- 5--
6-- To do: 6-- To do:
7-- - ... 7-- - ...
8-- 8--
@@ -20,7 +20,7 @@ local function PRINT(...)
20 for i=1,select('#',...) do 20 for i=1,select('#',...) do
21 str= str..tostring(select(i,...)).."\t" 21 str= str..tostring(select(i,...)).."\t"
22 end 22 end
23 if io then 23 if io then
24 io.stderr:write(str.."\n") 24 io.stderr:write(str.."\n")
25 end 25 end
26end 26end
@@ -55,7 +55,7 @@ tables_match= function( a, b )
55end 55end
56 56
57 57
58---=== Tasking (basic) ===--- 58PRINT( "---=== Tasking (basic) ===---")
59 59
60local function task( a, b, c ) 60local function task( a, b, c )
61 --error "111" -- testing error messages 61 --error "111" -- testing error messages
@@ -98,7 +98,7 @@ assert( lane1.status == "done" )
98assert( lane1.status == "done" ) 98assert( lane1.status == "done" )
99 99
100 100
101---=== Tasking (cancelling) ===--- 101PRINT( "---=== Tasking (cancelling) ===---")
102 102
103local task_launch2= lanes_gen( "", { cancelstep=100, globals={hey=true} }, task ) 103local task_launch2= lanes_gen( "", { cancelstep=100, globals={hey=true} }, task )
104 104
@@ -136,7 +136,7 @@ PRINT(" "..st)
136assert( st == "cancelled" ) 136assert( st == "cancelled" )
137 137
138 138
139---=== Communications ===--- 139PRINT( "---=== Communications ===---")
140 140
141local function WR(...) io.stderr:write(...) end 141local function WR(...) io.stderr:write(...) end
142 142
@@ -157,7 +157,7 @@ local chunk= function( linda )
157 send { 'a', 'b', 'c', d=10 }; WR( "{'a','b','c',d=10} sent\n" ) 157 send { 'a', 'b', 'c', d=10 }; WR( "{'a','b','c',d=10} sent\n" )
158 158
159 v=receive(); WR( v.." received\n" ); assert( v==4 ) 159 v=receive(); WR( v.." received\n" ); assert( v==4 )
160 160
161 WR( "Lane ends!\n" ) 161 WR( "Lane ends!\n" )
162end 162end
163 163
@@ -195,7 +195,7 @@ assert( PEEK() == nil )
195SEND(4) 195SEND(4)
196 196
197 197
198---=== Stdlib naming ===--- 198PRINT( "---=== Stdlib naming ===---")
199 199
200local function io_os_f() 200local function io_os_f()
201 assert(io) 201 assert(io)
@@ -215,7 +215,7 @@ assert( f2()[1] )
215assert( f3()[1] ) 215assert( f3()[1] )
216 216
217 217
218---=== Comms criss cross ===--- 218PRINT( "---=== Comms criss cross ===---")
219 219
220-- We make two identical lanes, which are using the same Linda channel. 220-- We make two identical lanes, which are using the same Linda channel.
221-- 221--
@@ -241,7 +241,7 @@ local a,b= tc(linda, "A","B"), tc(linda, "B","A") -- launching two lanes, twis
241local _= a[1],b[1] -- waits until they are both ready 241local _= a[1],b[1] -- waits until they are both ready
242 242
243 243
244---=== Receive & send of code ===--- 244PRINT( "---=== Receive & send of code ===---")
245 245
246local upvalue="123" 246local upvalue="123"
247 247
@@ -251,21 +251,21 @@ local function chunk2( linda )
251 -- function name & line number should be there even as separate thread 251 -- function name & line number should be there even as separate thread
252 -- 252 --
253 local info= debug.getinfo(1) -- 1 = us 253 local info= debug.getinfo(1) -- 1 = us
254 -- 254 --
255 for k,v in pairs(info) do PRINT(k,v) end 255 for k,v in pairs(info) do PRINT(k,v) end
256 256
257 assert( info.nups == 2 ) -- one upvalue + PRINT 257 assert( info.nups == 2 ) -- one upvalue + PRINT
258 assert( info.what == "Lua" ) 258 assert( info.what == "Lua" )
259 259
260 --assert( info.name == "chunk2" ) -- name does not seem to come through 260 --assert( info.name == "chunk2" ) -- name does not seem to come through
261 assert( string.match( info.source, "^@tests[/\\]basic.lua$" ) ) 261 assert( string.match( info.source, "^@basic.lua$" ) )
262 assert( string.match( info.short_src, "^tests[/\\]basic.lua$" ) ) 262 assert( string.match( info.short_src, "^basic.lua$" ) )
263 263
264 -- These vary so let's not be picky (they're there..) 264 -- These vary so let's not be picky (they're there..)
265 -- 265 --
266 assert( info.linedefined > 200 ) -- start of 'chunk2' 266 assert( info.linedefined > 200 ) -- start of 'chunk2'
267 assert( info.currentline > info.linedefined ) -- line of 'debug.getinfo' 267 assert( info.currentline > info.linedefined ) -- line of 'debug.getinfo'
268 assert( info.lastlinedefined > info.currentline ) -- end of 'chunk2' 268 assert( info.lastlinedefined > info.currentline ) -- end of 'chunk2'
269 269
270 local func,k= linda:receive( "down" ) 270 local func,k= linda:receive( "down" )
271 assert( type(func)=="function" ) 271 assert( type(func)=="function" )
@@ -275,7 +275,7 @@ local function chunk2( linda )
275 275
276 local str= linda:receive( "down" ) 276 local str= linda:receive( "down" )
277 assert( str=="ok" ) 277 assert( str=="ok" )
278 278
279 linda:send( "up", function() return ":)" end, "ok2" ) 279 linda:send( "up", function() return ":)" end, "ok2" )
280end 280end
281 281
@@ -304,7 +304,7 @@ local ok2= linda:receive( "up" )
304assert( ok2 == "ok2" ) 304assert( ok2 == "ok2" )
305 305
306 306
307---=== :join test ===--- 307PRINT( "---=== :join test ===---")
308 308
309-- NOTE: 'unpack()' cannot be used on the lane handle; it will always return nil 309-- NOTE: 'unpack()' cannot be used on the lane handle; it will always return nil
310-- (unless [1..n] has been read earlier, in which case it would seemingly 310-- (unless [1..n] has been read earlier, in which case it would seemingly
diff --git a/tests/func_is_string.lua b/tests/func_is_string.lua
new file mode 100644
index 0000000..98ea62b
--- /dev/null
+++ b/tests/func_is_string.lua
@@ -0,0 +1,12 @@
1require "lanes"
2
3local options = {globals = { b = 666 }}
4
5-- local gen1 = lanes.gen("*", "dofile('fibonacci.lua')")
6local gen2 = lanes.gen(options, "return b")
7
8-- fibLane = gen1()
9retLane1, retLane2 = gen2(), gen2()
10-- fibLane:join()
11
12print( retLane1[1], retLane2[1])