diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-04-01 07:32:53 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-04-01 07:32:53 +0000 |
| commit | d92132e87a48368db849f8ba8f30ff8ce5de6156 (patch) | |
| tree | e88deb991cbe2592b6ba5a1519519384c64ff85c | |
| parent | e5a090b01cd5b490f7331235b7c58c36c05bb7b6 (diff) | |
| download | luasocket-d92132e87a48368db849f8ba8f30ff8ce5de6156.tar.gz luasocket-d92132e87a48368db849f8ba8f30ff8ce5de6156.tar.bz2 luasocket-d92132e87a48368db849f8ba8f30ff8ce5de6156.zip | |
complicated bug in ltn12.filter.chain...
| -rw-r--r-- | src/ltn12.lua | 17 | ||||
| -rw-r--r-- | test/mimetest.lua | 55 | ||||
| -rw-r--r-- | test/testsupport.lua | 2 |
3 files changed, 65 insertions, 9 deletions
diff --git a/src/ltn12.lua b/src/ltn12.lua index ed3449b..dac932b 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
| @@ -34,8 +34,23 @@ end | |||
| 34 | local function chain2(f1, f2) | 34 | local function chain2(f1, f2) |
| 35 | if type(f1) ~= 'function' then error('invalid filter', 2) end | 35 | if type(f1) ~= 'function' then error('invalid filter', 2) end |
| 36 | if type(f2) ~= 'function' then error('invalid filter', 2) end | 36 | if type(f2) ~= 'function' then error('invalid filter', 2) end |
| 37 | local co = coroutine.create(function(chunk) | ||
| 38 | while true do | ||
| 39 | local filtered1 = f1(chunk) | ||
| 40 | local filtered2 = f2(filtered1) | ||
| 41 | local done2 = filtered1 and "" | ||
| 42 | while true do | ||
| 43 | if filtered2 == "" or filtered2 == nil then break end | ||
| 44 | coroutine.yield(filtered2) | ||
| 45 | filtered2 = f2(done2) | ||
| 46 | end | ||
| 47 | if filtered1 == "" then chunk = coroutine.yield(filtered1) | ||
| 48 | elseif filtered1 == nil then return nil | ||
| 49 | else chunk = chunk and "" end | ||
| 50 | end | ||
| 51 | end) | ||
| 37 | return function(chunk) | 52 | return function(chunk) |
| 38 | return f2(f1(chunk)) | 53 | return shift(coroutine.resume(co, chunk)) |
| 39 | end | 54 | end |
| 40 | end | 55 | end |
| 41 | 56 | ||
diff --git a/test/mimetest.lua b/test/mimetest.lua index 4a0a20a..3e57557 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua | |||
| @@ -34,11 +34,52 @@ local mao = [[ | |||
| 34 | local function random(handle, io_err) | 34 | local function random(handle, io_err) |
| 35 | if handle then | 35 | if handle then |
| 36 | return function() | 36 | return function() |
| 37 | local chunk = handle:read(math.random(0, 1024)) | 37 | local len = math.random(0, 1024) |
| 38 | local chunk = handle:read(len) | ||
| 38 | if not chunk then handle:close() end | 39 | if not chunk then handle:close() end |
| 39 | return chunk | 40 | return chunk |
| 40 | end | 41 | end |
| 41 | else source.empty(io_err or "unable to open file") end | 42 | else return ltn12.source.empty(io_err or "unable to open file") end |
| 43 | end | ||
| 44 | |||
| 45 | local function format(chunk) | ||
| 46 | if chunk then | ||
| 47 | if chunk == "" then return "''" | ||
| 48 | else return string.len(chunk) end | ||
| 49 | else return "nil" end | ||
| 50 | end | ||
| 51 | |||
| 52 | local function show(name, input, output) | ||
| 53 | local sin = format(input) | ||
| 54 | local sout = format(output) | ||
| 55 | io.write(name, ": ", sin, " -> ", sout, "\n") | ||
| 56 | end | ||
| 57 | |||
| 58 | local function chunked(length) | ||
| 59 | local tmp | ||
| 60 | return function(chunk) | ||
| 61 | local ret | ||
| 62 | if chunk and chunk ~= "" then | ||
| 63 | tmp = chunk | ||
| 64 | end | ||
| 65 | ret = string.sub(tmp, 1, length) | ||
| 66 | tmp = string.sub(tmp, length+1) | ||
| 67 | if not chunk and ret == "" then ret = nil end | ||
| 68 | return ret | ||
| 69 | end | ||
| 70 | end | ||
| 71 | |||
| 72 | --[[ | ||
| 73 | local function named(f, name) | ||
| 74 | return function(chunk) | ||
| 75 | local ret = f(chunk) | ||
| 76 | show(name, chunk, ret) | ||
| 77 | return ret | ||
| 78 | end | ||
| 79 | end | ||
| 80 | ]] | ||
| 81 | local function named(f) | ||
| 82 | return f | ||
| 42 | end | 83 | end |
| 43 | 84 | ||
| 44 | local what = nil | 85 | local what = nil |
| @@ -153,11 +194,11 @@ local function encode_b64test() | |||
| 153 | end | 194 | end |
| 154 | 195 | ||
| 155 | local function decode_b64test() | 196 | local function decode_b64test() |
| 156 | local d1 = mime.decode("base64") | 197 | local d1 = named(mime.decode("base64"), "d1") |
| 157 | local d2 = mime.decode("base64") | 198 | local d2 = named(mime.decode("base64"), "d2") |
| 158 | local d3 = mime.decode("base64") | 199 | local d3 = named(mime.decode("base64"), "d3") |
| 159 | local d4 = mime.decode("base64") | 200 | local d4 = named(mime.decode("base64"), "d4") |
| 160 | local chain = ltn12.filter.chain(d1, d2, d3, d4) | 201 | local chain = named(ltn12.filter.chain(d1, d2, d3, d4), "chain") |
| 161 | transform(eb64test, db64test, chain) | 202 | transform(eb64test, db64test, chain) |
| 162 | end | 203 | end |
| 163 | 204 | ||
diff --git a/test/testsupport.lua b/test/testsupport.lua index ca3cd95..acad8f5 100644 --- a/test/testsupport.lua +++ b/test/testsupport.lua | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | function readfile(name) | 1 | function readfile(name) |
| 2 | local f = io.open(name, "r") | 2 | local f = io.open(name, "rb") |
| 3 | if not f then return nil end | 3 | if not f then return nil end |
| 4 | local s = f:read("*a") | 4 | local s = f:read("*a") |
| 5 | f:close() | 5 | f:close() |
