about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/task.lua426
1 files changed, 210 insertions, 216 deletions
diff --git a/src/task.lua b/src/task.lua
index 81aa63e..09fa10a 100644
--- a/src/task.lua
+++ b/src/task.lua
@@ -383,250 +383,244 @@ _M.NOP       = NOP
 ----------------------------------------------------------------------------
 ----------------------------------------------------------------------------
 -- Tests
---
--- To run:
---    $ lua task.lua
 
 local task = _M
 
-local tests = {
-   counter = function ()
-      local done
-      local function counter(c)
-         local i = 1
-         while true do
-            c:send(i)
-            i = i + 1
-         end
-      end
-      local function main()
-         local c = task.Channel:new()
-         task.spawn(counter, c)
-         assert(c:recv() == 1)
-         assert(c:recv() == 2)
-         assert(c:recv() == 3)
-         assert(c:recv() == 4)
-         assert(c:recv() == 5)
-         done = true
-      end
-      task.spawn(main)
-      task.scheduler()
-      assert(done)
-   end,
-
-   nonblocking_channel = function()
-      local done
-      local function main()
-         local b = task.Channel:new()
-         assert(b:nbsend(1) == false)
-         assert(b:nbrecv() == false)
-
-         local c = task.Channel:new(1)
-         assert(c:nbrecv() == false)
-         assert(c:nbsend(1) == true)
-         assert(c:nbsend(1) == false)
-         local r, v = c:nbrecv()
-         assert(r == true)
-         assert(v == 1)
-         assert(c:nbrecv() == false)
-         done = true
-      end
-      task.spawn(main)
-      task.scheduler()
-      assert(done)
-   end,
-
-   concurrent_send_and_recv = function()
-      local l = {}
-      local function a(c, name)
-         -- Blocking send and recv from the same process
-         local alt = {{c = c, op = task.SEND, p = 1},
-                      {c = c, op = task.RECV}}
-         local i, v = task.chanalt(alt, true)
-         local k = string.format('%s %s', name, i == 1 and "send" or "recv")
-         l[k] = (l[k] or 0) + 1
+function test_counter()
+   local done
+   local function counter(c)
+      local i = 1
+      while true do
+         c:send(i)
+         i = i + 1
       end
+   end
+   local function main()
+      local c = task.Channel:new()
+      task.spawn(counter, c)
+      assert(c:recv() == 1)
+      assert(c:recv() == 2)
+      assert(c:recv() == 3)
+      assert(c:recv() == 4)
+      assert(c:recv() == 5)
+      done = true
+   end
+   task.spawn(main)
+   task.scheduler()
+   assert(done)
+end
 
-      for i = 0, 1000 do
-         -- On Mac OS X in lua 5.1 initializing seed with a
-         -- predictable value makes no sense. For all seeds from 1 to
-         -- 1000 the result of math.random(1,3) is _exactly_ the same!
-         -- So beware, when seeding!
-         -- math.randomseed(i)
-         local c = task.Channel:new()
-         task.spawn(a, c, "a")
-         task.spawn(a, c, "b")
-         task.scheduler()
-      end
+function test_nonblocking_channel()
+   local done
+   local function main()
+      local b = task.Channel:new()
+      assert(b:nbsend(1) == false)
+      assert(b:nbrecv() == false)
+
+      local c = task.Channel:new(1)
+      assert(c:nbrecv() == false)
+      assert(c:nbsend(1) == true)
+      assert(c:nbsend(1) == false)
+      local r, v = c:nbrecv()
+      assert(r == true)
+      assert(v == 1)
+      assert(c:nbrecv() == false)
+      done = true
+   end
+   task.spawn(main)
+   task.scheduler()
+   assert(done)
+end
 
-      -- Make sure we have randomness, that is: events occur in both
-      -- orders in 1000 runs
-      assert(l['a recv'] > 0)
-      assert(l['a send'] > 0)
-      assert(l['b recv'] > 0)
-      assert(l['b send'] > 0)
-   end,
+function test_concurrent_send_and_recv()
+   local l = {}
+   local function a(c, name)
+      -- Blocking send and recv from the same process
+      local alt = {{c = c, op = task.SEND, p = 1},
+                   {c = c, op = task.RECV}}
+      local i, v = task.chanalt(alt, true)
+      local k = string.format('%s %s', name, i == 1 and "send" or "recv")
+      l[k] = (l[k] or 0) + 1
+   end
 
-   channels_from_a_coroutine = function()
-      local done
+   for i = 0, 1000 do
+      -- On Mac OS X in lua 5.1 initializing seed with a
+      -- predictable value makes no sense. For all seeds from 1 to
+      -- 1000 the result of math.random(1,3) is _exactly_ the same!
+      -- So beware, when seeding!
+      -- math.randomseed(i)
       local c = task.Channel:new()
-      local function a()
-         for i = 1, 100 do
-            c:send(i)
-         end
-      end
-      local function b()
-         assert(c:recv() == 1)
-         assert(c:recv() == 2)
-         assert(c:recv() == 3)
-         assert(c:recv() == 4)
-         assert(c:recv() == 5)
-         done = true
-      end
-      local a_co = coroutine.create(a)
-      local b_co = coroutine.create(b)
-      coroutine.resume(a_co)
-      coroutine.resume(b_co)
+      task.spawn(a, c, "a")
+      task.spawn(a, c, "b")
       task.scheduler()
-      assert(done)
-   end,
+   end
 
-   fibonacci = function()
-      local done
-      local function fib(c)
-         local x, y = 0, 1
-         while true do
-            c:send(x)
-            x, y = y, x + y
-         end
-      end
-      local function main(c)
-         assert(c:recv() == 0)
-         assert(c:recv() == 1)
-         assert(c:recv() == 1)
-         assert(c:recv() == 2)
-         assert(c:recv() == 3)
-         assert(c:recv() == 5)
-         assert(c:recv() == 8)
-         assert(c:recv() == 13)
-         assert(c:recv() == 21)
-         assert(c:recv() == 34)
-         done = true
+   -- Make sure we have randomness, that is: events occur in both
+   -- orders in 1000 runs
+   assert(l['a recv'] > 0)
+   assert(l['a send'] > 0)
+   assert(l['b recv'] > 0)
+   assert(l['b send'] > 0)
+end
+
+function test_channels_from_a_coroutine()
+   local done
+   local c = task.Channel:new()
+   local function a()
+      for i = 1, 100 do
+         c:send(i)
       end
+   end
+   local function b()
+      assert(c:recv() == 1)
+      assert(c:recv() == 2)
+      assert(c:recv() == 3)
+      assert(c:recv() == 4)
+      assert(c:recv() == 5)
+      done = true
+   end
+   local a_co = coroutine.create(a)
+   local b_co = coroutine.create(b)
+   coroutine.resume(a_co)
+   coroutine.resume(b_co)
+   task.scheduler()
+   assert(done)
+end
 
-      local c = task.Channel:new()
-      task.spawn(fib, c)
-      task.spawn(main, c)
-      task.scheduler()
-      assert(done)
-   end,
+function test_fibonacci()
+   local done
+   local function fib(c)
+      local x, y = 0, 1
+      while true do
+         c:send(x)
+         x, y = y, x + y
+      end
+   end
+   local function main(c)
+      assert(c:recv() == 0)
+      assert(c:recv() == 1)
+      assert(c:recv() == 1)
+      assert(c:recv() == 2)
+      assert(c:recv() == 3)
+      assert(c:recv() == 5)
+      assert(c:recv() == 8)
+      assert(c:recv() == 13)
+      assert(c:recv() == 21)
+      assert(c:recv() == 34)
+      done = true
+   end
 
-   non_blocking_chanalt = function()
-      local done
-      local function main()
-         local c = task.Channel:new()
-         local alts = {{c = c, op = task.RECV},
-                       {c = c, op = task.NOP},
-                       {c = c, op = task.SEND, p = 1}}
-         assert(task.chanalt(alts, false) == nil)
-
-         local c = task.Channel:new(1)
-         local alts = {{c = c, op = task.RECV},
-                       {c = c, op = task.NOP},
-                       {c = c, op = task.SEND, p = 1}}
-         assert(task.chanalt(alts, false) == 3)
-         assert(task.chanalt(alts, false) == 1)
-
-         local alts = {{c = c, op = task.NOP}}
-         assert(task.chanalt(alts, false) == nil)
+   local c = task.Channel:new()
+   task.spawn(fib, c)
+   task.spawn(main, c)
+   task.scheduler()
+   assert(done)
+end
 
-         done = true
-      end
-      task.spawn(main)
-      task.scheduler()
-      assert(done)
-   end,
+function test_non_blocking_chanalt()
+   local done
+   local function main()
+      local c = task.Channel:new()
+      local alts = {{c = c, op = task.RECV},
+                    {c = c, op = task.NOP},
+                    {c = c, op = task.SEND, p = 1}}
+      assert(task.chanalt(alts, false) == nil)
+
+      local c = task.Channel:new(1)
+      local alts = {{c = c, op = task.RECV},
+                    {c = c, op = task.NOP},
+                    {c = c, op = task.SEND, p = 1}}
+      assert(task.chanalt(alts, false) == 3)
+      assert(task.chanalt(alts, false) == 1)
+
+      local alts = {{c = c, op = task.NOP}}
+      assert(task.chanalt(alts, false) == nil)
+
+      done = true
+   end
+   task.spawn(main)
+   task.scheduler()
+   assert(done)
+end
 
-   -- Apparently it's not really a Sieve of Eratosthenes:
-   --   http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
-   eratosthenes_sieve = function()
-      local done
-      local function counter(c)
-         local i = 2
-         while true do
-            c:send(i)
-            i = i + 1
-         end
+-- Apparently it's not really a Sieve of Eratosthenes:
+--   http://www.cs.hmc.edu/~oneill/papers/Sieve-JFP.pdf
+function test_eratosthenes_sieve()
+   local done
+   local function counter(c)
+      local i = 2
+      while true do
+         c:send(i)
+         i = i + 1
       end
+   end
 
-      local function filter(p, recv_ch, send_ch)
-         while true do
-            local i = recv_ch:recv()
-            if i % p ~= 0 then
-               send_ch:send(i)
-            end
+   local function filter(p, recv_ch, send_ch)
+      while true do
+         local i = recv_ch:recv()
+         if i % p ~= 0 then
+            send_ch:send(i)
          end
       end
+   end
 
-      local function sieve(primes_ch)
-         local c = task.Channel:new()
-         task.spawn(counter, c)
-         while true do
-            local p, newc = c:recv(), task.Channel:new()
-            primes_ch:send(p)
-            task.spawn(filter, p, c, newc)
-            c = newc
-         end
+   local function sieve(primes_ch)
+      local c = task.Channel:new()
+      task.spawn(counter, c)
+      while true do
+         local p, newc = c:recv(), task.Channel:new()
+         primes_ch:send(p)
+         task.spawn(filter, p, c, newc)
+         c = newc
       end
+   end
 
-      local function main()
-         local primes = task.Channel:new()
-         task.spawn(sieve, primes)
-         assert(primes:recv() == 2)
-         assert(primes:recv() == 3)
-         assert(primes:recv() == 5)
-         assert(primes:recv() == 7)
-         assert(primes:recv() == 11)
-         assert(primes:recv() == 13)
-         done = true
-      end
+   local function main()
+      local primes = task.Channel:new()
+      task.spawn(sieve, primes)
+      assert(primes:recv() == 2)
+      assert(primes:recv() == 3)
+      assert(primes:recv() == 5)
+      assert(primes:recv() == 7)
+      assert(primes:recv() == 11)
+      assert(primes:recv() == 13)
+      done = true
+   end
 
-      task.spawn(main)
-      task.scheduler()
-      assert(done)
-   end,
+   task.spawn(main)
+   task.scheduler()
+   assert(done)
+end
 
-   channel_as_iterator = function()
-      local done
-      local function counter(c)
-         local i = 2
-         while true do
-            c:send(i)
-            i = i + 1
-         end
+function test_channel_as_iterator()
+   local done
+   local function counter(c)
+      local i = 2
+      while true do
+         c:send(i)
+         i = i + 1
       end
+   end
 
-      local function main()
-         local numbers = task.Channel:new()
-         task.spawn(counter, numbers)
-         for _, j in numbers() do
-            if j == 100 then
-               break
-            end
-            done = true
+   local function main()
+      local numbers = task.Channel:new()
+      task.spawn(counter, numbers)
+      for _, j in numbers() do
+         if j == 100 then
+            break
          end
-      end
-      if _VERSION == "Lua 5.1" then
-         -- sorry, this doesn't work in 5.1
-         print('skipping... (5.1 unsupported)')
          done = true
-      else
-         task.spawn(main)
-         task.scheduler()
       end
-      assert(done)
-   end,
-
-}
+   end
+   if _VERSION == "Lua 5.1" then
+      -- sorry, this test doesn't work in 5.1
+      print('skipping... (5.1 unsupported)')
+      done = true
+   else
+      task.spawn(main)
+      task.scheduler()
+   end
+   assert(done)
+end
 
 return _M
f51e1b01c9222'>^
a05beb6 ^
321e8d5 ^
28ffff8 ^
b355755 ^
1076f2b
72707c2 ^
15abade ^
bf35794 ^
1076f2b

281f098 ^
ceea528 ^
3c35b90 ^
6fc8a63 ^
b003a35 ^
0b80d18 ^

281f098 ^
5983c00 ^
bf35794 ^
87836d7 ^
7d7cde0 ^
bf35794 ^
d2d394e ^
bf35794 ^
bf35794 ^
39677ec ^
439e15d ^
b4d53bf ^
281f098 ^

28ffff8 ^
281f098 ^

b233089 ^
9056d7e ^
d2d394e ^
281f098 ^
3399650 ^
d7e1708 ^
6b25d06 ^
281f098 ^

868159f ^
d7e1708 ^
dba2306 ^
6b25d06 ^

b9da4b0 ^
9e8b325 ^
281f098 ^


9e8b325 ^
dba2306 ^
6b25d06 ^
281f098 ^

d2d394e ^
868159f ^

29355bd ^
8b59083 ^
281f098 ^
868159f ^
868159f ^
aa13727 ^

281f098 ^
5983c00 ^

281f098 ^

0b80d18 ^
281f098 ^
4b5b3d9 ^
6b25d06 ^
8dc8605 ^
868159f ^


fee8df6 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155