| 1 |
local ltn12 = require("ltn12") |
| 2 |
|
| 3 |
dofile("testsupport.lua") |
| 4 |
|
| 5 |
local function format(chunk) |
| 6 |
if chunk then |
| 7 |
if chunk == "" then return "''" |
| 8 |
else return string.len(chunk) end |
| 9 |
else return "nil" end |
| 10 |
end |
| 11 |
|
| 12 |
local function show(name, input, output) |
| 13 |
local sin = format(input) |
| 14 |
local sout = format(output) |
| 15 |
io.write(name, ": ", sin, " -> ", sout, "\n") |
| 16 |
end |
| 17 |
|
| 18 |
local function chunked(length) |
| 19 |
local tmp |
| 20 |
return function(chunk) |
| 21 |
local ret |
| 22 |
if chunk and chunk ~= "" then |
| 23 |
tmp = chunk |
| 24 |
end |
| 25 |
ret = string.sub(tmp, 1, length) |
| 26 |
tmp = string.sub(tmp, length+1) |
| 27 |
if not chunk and ret == "" then ret = nil end |
| 28 |
return ret |
| 29 |
end |
| 30 |
end |
| 31 |
|
| 32 |
local function named(f, name) |
| 33 |
return function(chunk) |
| 34 |
local ret = f(chunk) |
| 35 |
show(name, chunk, ret) |
| 36 |
return ret |
| 37 |
end |
| 38 |
end |
| 39 |
|
| 40 |
|
| 41 |
local function split(size) |
| 42 |
local buffer = "" |
| 43 |
local last_out = "" |
| 44 |
local last_in = "" |
| 45 |
local function output(chunk) |
| 46 |
local part = string.sub(buffer, 1, size) |
| 47 |
buffer = string.sub(buffer, size+1) |
| 48 |
last_out = (part ~= "" or chunk) and part |
| 49 |
last_in = chunk |
| 50 |
return last_out |
| 51 |
end |
| 52 |
return function(chunk, done) |
| 53 |
if done then |
| 54 |
return not last_in and not last_out |
| 55 |
end |
| 56 |
|
| 57 |
if not chunk then |
| 58 |
if last_in and last_in ~= "" and last_out ~= "" then |
| 59 |
error("nil chunk following data chunk", 2) |
| 60 |
end |
| 61 |
if not last_out then error("extra nil chunk", 2) end |
| 62 |
return output(chunk) |
| 63 |
elseif chunk == "" then |
| 64 |
if last_out == "" then error('extra "" chunk', 2) end |
| 65 |
if not last_out then error('"" chunk following nil return', 2) end |
| 66 |
if not last_in then error('"" chunk following nil chunk', 2) end |
| 67 |
return output(chunk) |
| 68 |
else |
| 69 |
if not last_in then error("data chunk following nil chunk", 2) end |
| 70 |
if last_in ~= "" and last_out ~= "" then |
| 71 |
error("data chunk following data chunk", 2) |
| 72 |
end |
| 73 |
buffer = chunk |
| 74 |
return output(chunk) |
| 75 |
end |
| 76 |
end |
| 77 |
end |
| 78 |
|
| 79 |
|
| 80 |
local function format(chunk) |
| 81 |
if chunk then |
| 82 |
if chunk == "" then return "''" |
| 83 |
else return string.len(chunk) end |
| 84 |
else return "nil" end |
| 85 |
end |
| 86 |
|
| 87 |
|
| 88 |
local function merge(size) |
| 89 |
local buffer = "" |
| 90 |
local last_out = "" |
| 91 |
local last_in = "" |
| 92 |
local function output(chunk) |
| 93 |
local part |
| 94 |
if string.len(buffer) >= size or not chunk then |
| 95 |
part = buffer |
| 96 |
buffer = "" |
| 97 |
else |
| 98 |
part = "" |
| 99 |
end |
| 100 |
last_out = (part ~= "" or chunk) and part |
| 101 |
last_in = chunk |
| 102 |
return last_out |
| 103 |
end |
| 104 |
return function(chunk, done) |
| 105 |
if done then |
| 106 |
return not last_in and not last_out |
| 107 |
end |
| 108 |
|
| 109 |
if not chunk then |
| 110 |
if last_in and last_in ~= "" and last_out ~= "" then |
| 111 |
error("nil chunk following data chunk", 2) |
| 112 |
end |
| 113 |
if not last_out then error("extra nil chunk", 2) end |
| 114 |
return output(chunk) |
| 115 |
elseif chunk == "" then |
| 116 |
if last_out == "" then error('extra "" chunk', 2) end |
| 117 |
if not last_out then error('"" chunk following nil return', 2) end |
| 118 |
if not last_in then error('"" chunk following nil chunk', 2) end |
| 119 |
return output(chunk) |
| 120 |
else |
| 121 |
if not last_in then error("data chunk following nil chunk", 2) end |
| 122 |
if last_in ~= "" and last_out ~= "" then |
| 123 |
error("data chunk following data chunk", 2) |
| 124 |
end |
| 125 |
buffer = buffer .. chunk |
| 126 |
return output(chunk) |
| 127 |
end |
| 128 |
end |
| 129 |
end |
| 130 |
|
| 131 |
|
| 132 |
io.write("testing sink.table: ") |
| 133 |
local sink, t = ltn12.sink.table() |
| 134 |
local s, c = "", "" |
| 135 |
for i = 0, 10 do |
| 136 |
c = string.rep(string.char(i), i) |
| 137 |
s = s .. c |
| 138 |
assert(sink(c), "returned error") |
| 139 |
end |
| 140 |
assert(sink(nil), "returned error") |
| 141 |
assert(table.concat(t) == s, "mismatch") |
| 142 |
print("ok") |
| 143 |
|
| 144 |
|
| 145 |
io.write("testing sink.chain (with split): ") |
| 146 |
sink, t = ltn12.sink.table() |
| 147 |
local filter = split(3) |
| 148 |
sink = ltn12.sink.chain(filter, sink) |
| 149 |
s = "123456789012345678901234567890" |
| 150 |
assert(sink(s), "returned error") |
| 151 |
assert(sink(s), "returned error") |
| 152 |
assert(sink(nil), "returned error") |
| 153 |
assert(table.concat(t) == s .. s, "mismatch") |
| 154 |
assert(filter(nil, 1), "filter not empty") |
| 155 |
print("ok") |
| 156 |
|
| 157 |
|
| 158 |
io.write("testing sink.chain (with merge): ") |
| 159 |
sink, t = ltn12.sink.table() |
| 160 |
filter = merge(10) |
| 161 |
sink = ltn12.sink.chain(filter, sink) |
| 162 |
s = string.rep("123", 30) |
| 163 |
s = s .. string.rep("4321", 30) |
| 164 |
for i = 1, 30 do |
| 165 |
assert(sink("123"), "returned error") |
| 166 |
end |
| 167 |
for i = 1, 30 do |
| 168 |
assert(sink("4321"), "returned error") |
| 169 |
end |
| 170 |
assert(sink(nil), "returned error") |
| 171 |
assert(filter(nil, 1), "filter not empty") |
| 172 |
assert(table.concat(t) == s, "mismatch") |
| 173 |
print("ok") |
| 174 |
|
| 175 |
|
| 176 |
io.write("testing source.string and pump.all: ") |
| 177 |
local source = ltn12.source.string(s) |
| 178 |
sink, t = ltn12.sink.table() |
| 179 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 180 |
assert(table.concat(t) == s, "mismatch") |
| 181 |
print("ok") |
| 182 |
|
| 183 |
|
| 184 |
io.write("testing source.chain (with split): ") |
| 185 |
source = ltn12.source.string(s) |
| 186 |
filter = split(5) |
| 187 |
source = ltn12.source.chain(source, filter) |
| 188 |
sink, t = ltn12.sink.table() |
| 189 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 190 |
assert(table.concat(t) == s, "mismatch") |
| 191 |
assert(filter(nil, 1), "filter not empty") |
| 192 |
print("ok") |
| 193 |
|
| 194 |
|
| 195 |
io.write("testing source.chain (with several filters): ") |
| 196 |
local function double(x) |
| 197 |
if not x then return end |
| 198 |
local b={} |
| 199 |
for k in x:gmatch'.' do table.insert(b, k..k) end |
| 200 |
return table.concat(b) |
| 201 |
end |
| 202 |
source = ltn12.source.string(s) |
| 203 |
source = ltn12.source.chain(source, double, double, double) |
| 204 |
sink, t = ltn12.sink.table() |
| 205 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 206 |
assert(table.concat(t) == double(double(double(s))), "mismatch") |
| 207 |
print("ok") |
| 208 |
|
| 209 |
|
| 210 |
io.write("testing source.chain (with split) and sink.chain (with merge): ") |
| 211 |
source = ltn12.source.string(s) |
| 212 |
filter = split(5) |
| 213 |
source = ltn12.source.chain(source, filter) |
| 214 |
local filter2 = merge(13) |
| 215 |
sink, t = ltn12.sink.table() |
| 216 |
sink = ltn12.sink.chain(filter2, sink) |
| 217 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 218 |
assert(table.concat(t) == s, "mismatch") |
| 219 |
assert(filter(nil, 1), "filter not empty") |
| 220 |
assert(filter2(nil, 1), "filter2 not empty") |
| 221 |
print("ok") |
| 222 |
|
| 223 |
|
| 224 |
io.write("testing sink.chain (with several filters): ") |
| 225 |
source = ltn12.source.string(s) |
| 226 |
sink, t = ltn12.sink.table() |
| 227 |
sink = ltn12.sink.chain(double, double, double, sink) |
| 228 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 229 |
assert(table.concat(t) == double(double(double(s))), "mismatch") |
| 230 |
print("ok") |
| 231 |
|
| 232 |
|
| 233 |
io.write("testing filter.chain (and sink.chain, with split, merge): ") |
| 234 |
source = ltn12.source.string(s) |
| 235 |
filter = split(5) |
| 236 |
filter2 = merge(13) |
| 237 |
local chain = ltn12.filter.chain(filter, filter2) |
| 238 |
sink, t = ltn12.sink.table() |
| 239 |
sink = ltn12.sink.chain(chain, sink) |
| 240 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 241 |
assert(table.concat(t) == s, "mismatch") |
| 242 |
assert(filter(nil, 1), "filter not empty") |
| 243 |
assert(filter2(nil, 1), "filter2 not empty") |
| 244 |
print("ok") |
| 245 |
|
| 246 |
|
| 247 |
io.write("testing filter.chain (and sink.chain, a bunch): ") |
| 248 |
source = ltn12.source.string(s) |
| 249 |
filter = split(5) |
| 250 |
filter2 = merge(13) |
| 251 |
local filter3 = split(7) |
| 252 |
local filter4 = merge(11) |
| 253 |
local filter5 = split(10) |
| 254 |
chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5) |
| 255 |
sink, t = ltn12.sink.table() |
| 256 |
sink = ltn12.sink.chain(chain, sink) |
| 257 |
assert(ltn12.pump.all(source, sink)) |
| 258 |
assert(table.concat(t) == s, "mismatch") |
| 259 |
assert(filter(nil, 1), "filter not empty") |
| 260 |
assert(filter2(nil, 1), "filter2 not empty") |
| 261 |
assert(filter3(nil, 1), "filter3 not empty") |
| 262 |
assert(filter4(nil, 1), "filter4 not empty") |
| 263 |
assert(filter5(nil, 1), "filter5 not empty") |
| 264 |
print("ok") |
| 265 |
|
| 266 |
|
| 267 |
io.write("testing filter.chain (and source.chain, with split, merge): ") |
| 268 |
source = ltn12.source.string(s) |
| 269 |
filter = split(5) |
| 270 |
filter2 = merge(13) |
| 271 |
local chain = ltn12.filter.chain(filter, filter2) |
| 272 |
sink, t = ltn12.sink.table() |
| 273 |
source = ltn12.source.chain(source, chain) |
| 274 |
assert(ltn12.pump.all(source, sink), "returned error") |
| 275 |
assert(table.concat(t) == s, "mismatch") |
| 276 |
assert(filter(nil, 1), "filter not empty") |
| 277 |
assert(filter2(nil, 1), "filter2 not empty") |
| 278 |
print("ok") |
| 279 |
|
| 280 |
|
| 281 |
io.write("testing filter.chain (and source.chain, a bunch): ") |
| 282 |
source = ltn12.source.string(s) |
| 283 |
filter = split(5) |
| 284 |
filter2 = merge(13) |
| 285 |
local filter3 = split(7) |
| 286 |
local filter4 = merge(11) |
| 287 |
local filter5 = split(10) |
| 288 |
chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5) |
| 289 |
sink, t = ltn12.sink.table() |
| 290 |
source = ltn12.source.chain(source, chain) |
| 291 |
assert(ltn12.pump.all(source, sink)) |
| 292 |
assert(table.concat(t) == s, "mismatch") |
| 293 |
assert(filter(nil, 1), "filter not empty") |
| 294 |
assert(filter2(nil, 1), "filter2 not empty") |
| 295 |
assert(filter3(nil, 1), "filter3 not empty") |
| 296 |
assert(filter4(nil, 1), "filter4 not empty") |
| 297 |
assert(filter5(nil, 1), "filter5 not empty") |
| 298 |
print("ok") |
| 299 |
|