diff options
author | Benoit Germain <bnt.germain@gmail.com> | 2011-01-04 21:31:17 +0100 |
---|---|---|
committer | Benoit Germain <bnt.germain@gmail.com> | 2011-01-04 21:31:17 +0100 |
commit | 79e46938c5d8daf164ab2d934f668fa27b32e4cf (patch) | |
tree | 407761f25bbdc3d5b2066a705dcbcf8711690242 /tests | |
parent | ed07b457b6b45ece85d367dc8b89bf3c040abd9a (diff) | |
download | lanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.tar.gz lanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.tar.bz2 lanes-79e46938c5d8daf164ab2d934f668fa27b32e4cf.zip |
Take all code from Asko Kauppi's SVN server, and push it here so that the github repository becomes the official Lanes source codebase.
Note that Asko's SVN server holds version 2.0.9, whereas this is version 2.0.10, but I don't see any real need to update SVN if it is to become deprecated.
Next steps:
- upgrade the rockspec to the latest version
- make the html help available online somewhere
Signed-off-by: Benoit Germain <bnt.germain@gmail.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/appendud.lua | 58 | ||||
-rw-r--r-- | tests/basic.lua | 52 | ||||
-rw-r--r-- | tests/func_is_string.lua | 12 |
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 | -- | ||
9 | require "lanes" | ||
10 | |||
11 | local _tab = { | ||
12 | beginupdate = function (this) print('tab.beginupdate') end; | ||
13 | endupdate = function (this) print('tab.endupdate') end; | ||
14 | } | ||
15 | local _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 | |||
33 | function appendud(tab, ud) | ||
34 | io.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 | ||
40 | io.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) | ||
43 | end | ||
44 | |||
45 | local t,err= lanes.gen( appendud )( _tab, _ud ) -- create & launch a thread | ||
46 | assert(t) | ||
47 | assert(not err) | ||
48 | |||
49 | -- test | ||
50 | |||
51 | t: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 | |||
54 | io.stderr:write(t[1]) | ||
55 | |||
56 | for k,v in ipairs(t) do | ||
57 | print(k,v) | ||
58 | end | ||
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 |
26 | end | 26 | end |
@@ -55,7 +55,7 @@ tables_match= function( a, b ) | |||
55 | end | 55 | end |
56 | 56 | ||
57 | 57 | ||
58 | ---=== Tasking (basic) ===--- | 58 | PRINT( "---=== Tasking (basic) ===---") |
59 | 59 | ||
60 | local function task( a, b, c ) | 60 | local 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" ) | |||
98 | assert( lane1.status == "done" ) | 98 | assert( lane1.status == "done" ) |
99 | 99 | ||
100 | 100 | ||
101 | ---=== Tasking (cancelling) ===--- | 101 | PRINT( "---=== Tasking (cancelling) ===---") |
102 | 102 | ||
103 | local task_launch2= lanes_gen( "", { cancelstep=100, globals={hey=true} }, task ) | 103 | local task_launch2= lanes_gen( "", { cancelstep=100, globals={hey=true} }, task ) |
104 | 104 | ||
@@ -136,7 +136,7 @@ PRINT(" "..st) | |||
136 | assert( st == "cancelled" ) | 136 | assert( st == "cancelled" ) |
137 | 137 | ||
138 | 138 | ||
139 | ---=== Communications ===--- | 139 | PRINT( "---=== Communications ===---") |
140 | 140 | ||
141 | local function WR(...) io.stderr:write(...) end | 141 | local 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" ) |
162 | end | 162 | end |
163 | 163 | ||
@@ -195,7 +195,7 @@ assert( PEEK() == nil ) | |||
195 | SEND(4) | 195 | SEND(4) |
196 | 196 | ||
197 | 197 | ||
198 | ---=== Stdlib naming ===--- | 198 | PRINT( "---=== Stdlib naming ===---") |
199 | 199 | ||
200 | local function io_os_f() | 200 | local function io_os_f() |
201 | assert(io) | 201 | assert(io) |
@@ -215,7 +215,7 @@ assert( f2()[1] ) | |||
215 | assert( f3()[1] ) | 215 | assert( f3()[1] ) |
216 | 216 | ||
217 | 217 | ||
218 | ---=== Comms criss cross ===--- | 218 | PRINT( "---=== 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 | |||
241 | local _= a[1],b[1] -- waits until they are both ready | 241 | local _= a[1],b[1] -- waits until they are both ready |
242 | 242 | ||
243 | 243 | ||
244 | ---=== Receive & send of code ===--- | 244 | PRINT( "---=== Receive & send of code ===---") |
245 | 245 | ||
246 | local upvalue="123" | 246 | local 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" ) |
280 | end | 280 | end |
281 | 281 | ||
@@ -304,7 +304,7 @@ local ok2= linda:receive( "up" ) | |||
304 | assert( ok2 == "ok2" ) | 304 | assert( ok2 == "ok2" ) |
305 | 305 | ||
306 | 306 | ||
307 | ---=== :join test ===--- | 307 | PRINT( "---=== :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 @@ | |||
1 | require "lanes" | ||
2 | |||
3 | local options = {globals = { b = 666 }} | ||
4 | |||
5 | -- local gen1 = lanes.gen("*", "dofile('fibonacci.lua')") | ||
6 | local gen2 = lanes.gen(options, "return b") | ||
7 | |||
8 | -- fibLane = gen1() | ||
9 | retLane1, retLane2 = gen2(), gen2() | ||
10 | -- fibLane:join() | ||
11 | |||
12 | print( retLane1[1], retLane2[1]) | ||