diff options
| author | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-28 00:59:12 +0000 |
|---|---|---|
| committer | Diego Nehab <diego@tecgraf.puc-rio.br> | 2004-11-28 00:59:12 +0000 |
| commit | 05e8f243851049cebda6c5b690d3cde0f1e2c874 (patch) | |
| tree | 9799a322fee3aefb07eaafa00875f214ad2b7ea6 /test | |
| parent | 50da56dbeeec0cc7856ac06057cb778b502e087b (diff) | |
| download | luasocket-05e8f243851049cebda6c5b690d3cde0f1e2c874.tar.gz luasocket-05e8f243851049cebda6c5b690d3cde0f1e2c874.tar.bz2 luasocket-05e8f243851049cebda6c5b690d3cde0f1e2c874.zip | |
New LTN12 test procedures (still short, but growing)
LTN12 avoids coroutines.
Diffstat (limited to 'test')
| -rw-r--r-- | test/ltn12test.lua | 275 | ||||
| -rw-r--r-- | test/mimetest.lua | 36 |
2 files changed, 275 insertions, 36 deletions
diff --git a/test/ltn12test.lua b/test/ltn12test.lua new file mode 100644 index 0000000..7922bf1 --- /dev/null +++ b/test/ltn12test.lua | |||
| @@ -0,0 +1,275 @@ | |||
| 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 | -- check if argument is consistent with state | ||
| 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 | -- check if argument is consistent with state | ||
| 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 split) and sink.chain (with merge): ") | ||
| 196 | source = ltn12.source.string(s) | ||
| 197 | filter = split(5) | ||
| 198 | source = ltn12.source.chain(source, filter) | ||
| 199 | local filter2 = merge(13) | ||
| 200 | sink, t = ltn12.sink.table() | ||
| 201 | sink = ltn12.sink.chain(filter2, sink) | ||
| 202 | assert(ltn12.pump.all(source, sink), "returned error") | ||
| 203 | assert(table.concat(t) == s, "mismatch") | ||
| 204 | assert(filter(nil, 1), "filter not empty") | ||
| 205 | assert(filter2(nil, 1), "filter2 not empty") | ||
| 206 | print("ok") | ||
| 207 | |||
| 208 | -------------------------------- | ||
| 209 | io.write("testing filter.chain (and sink.chain, with split, merge): ") | ||
| 210 | source = ltn12.source.string(s) | ||
| 211 | filter = split(5) | ||
| 212 | filter2 = merge(13) | ||
| 213 | local chain = ltn12.filter.chain(filter, filter2) | ||
| 214 | sink, t = ltn12.sink.table() | ||
| 215 | sink = ltn12.sink.chain(chain, sink) | ||
| 216 | assert(ltn12.pump.all(source, sink), "returned error") | ||
| 217 | assert(table.concat(t) == s, "mismatch") | ||
| 218 | assert(filter(nil, 1), "filter not empty") | ||
| 219 | assert(filter2(nil, 1), "filter2 not empty") | ||
| 220 | print("ok") | ||
| 221 | |||
| 222 | -------------------------------- | ||
| 223 | io.write("testing filter.chain (and sink.chain, a bunch): ") | ||
| 224 | source = ltn12.source.string(s) | ||
| 225 | filter = split(5) | ||
| 226 | filter2 = merge(13) | ||
| 227 | local filter3 = split(7) | ||
| 228 | local filter4 = merge(11) | ||
| 229 | local filter5 = split(10) | ||
| 230 | chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5) | ||
| 231 | sink, t = ltn12.sink.table() | ||
| 232 | sink = ltn12.sink.chain(chain, sink) | ||
| 233 | assert(ltn12.pump.all(source, sink)) | ||
| 234 | assert(table.concat(t) == s, "mismatch") | ||
| 235 | assert(filter(nil, 1), "filter not empty") | ||
| 236 | assert(filter2(nil, 1), "filter2 not empty") | ||
| 237 | assert(filter3(nil, 1), "filter3 not empty") | ||
| 238 | assert(filter4(nil, 1), "filter4 not empty") | ||
| 239 | assert(filter5(nil, 1), "filter5 not empty") | ||
| 240 | print("ok") | ||
| 241 | |||
| 242 | -------------------------------- | ||
| 243 | io.write("testing filter.chain (and source.chain, with split, merge): ") | ||
| 244 | source = ltn12.source.string(s) | ||
| 245 | filter = split(5) | ||
| 246 | filter2 = merge(13) | ||
| 247 | local chain = ltn12.filter.chain(filter, filter2) | ||
| 248 | sink, t = ltn12.sink.table() | ||
| 249 | source = ltn12.source.chain(source, chain) | ||
| 250 | assert(ltn12.pump.all(source, sink), "returned error") | ||
| 251 | assert(table.concat(t) == s, "mismatch") | ||
| 252 | assert(filter(nil, 1), "filter not empty") | ||
| 253 | assert(filter2(nil, 1), "filter2 not empty") | ||
| 254 | print("ok") | ||
| 255 | |||
| 256 | -------------------------------- | ||
| 257 | io.write("testing filter.chain (and source.chain, a bunch): ") | ||
| 258 | source = ltn12.source.string(s) | ||
| 259 | filter = split(5) | ||
| 260 | filter2 = merge(13) | ||
| 261 | local filter3 = split(7) | ||
| 262 | local filter4 = merge(11) | ||
| 263 | local filter5 = split(10) | ||
| 264 | chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5) | ||
| 265 | sink, t = ltn12.sink.table() | ||
| 266 | source = ltn12.source.chain(source, chain) | ||
| 267 | assert(ltn12.pump.all(source, sink)) | ||
| 268 | assert(table.concat(t) == s, "mismatch") | ||
| 269 | assert(filter(nil, 1), "filter not empty") | ||
| 270 | assert(filter2(nil, 1), "filter2 not empty") | ||
| 271 | assert(filter3(nil, 1), "filter3 not empty") | ||
| 272 | assert(filter4(nil, 1), "filter4 not empty") | ||
| 273 | assert(filter5(nil, 1), "filter5 not empty") | ||
| 274 | print("ok") | ||
| 275 | |||
diff --git a/test/mimetest.lua b/test/mimetest.lua index 834d99f..2f6b7a8 100644 --- a/test/mimetest.lua +++ b/test/mimetest.lua | |||
| @@ -47,42 +47,6 @@ local function random(handle, io_err) | |||
| 47 | else return ltn12.source.empty(io_err or "unable to open file") end | 47 | else return ltn12.source.empty(io_err or "unable to open file") end |
| 48 | end | 48 | end |
| 49 | 49 | ||
| 50 | local function format(chunk) | ||
| 51 | if chunk then | ||
| 52 | if chunk == "" then return "''" | ||
| 53 | else return string.len(chunk) end | ||
| 54 | else return "nil" end | ||
| 55 | end | ||
| 56 | |||
| 57 | local function show(name, input, output) | ||
| 58 | local sin = format(input) | ||
| 59 | local sout = format(output) | ||
| 60 | io.write(name, ": ", sin, " -> ", sout, "\n") | ||
| 61 | end | ||
| 62 | |||
| 63 | local function chunked(length) | ||
| 64 | local tmp | ||
| 65 | return function(chunk) | ||
| 66 | local ret | ||
| 67 | if chunk and chunk ~= "" then | ||
| 68 | tmp = chunk | ||
| 69 | end | ||
| 70 | ret = string.sub(tmp, 1, length) | ||
| 71 | tmp = string.sub(tmp, length+1) | ||
| 72 | if not chunk and ret == "" then ret = nil end | ||
| 73 | return ret | ||
| 74 | end | ||
| 75 | end | ||
| 76 | |||
| 77 | --[[ | ||
| 78 | local function named(f, name) | ||
| 79 | return function(chunk) | ||
| 80 | local ret = f(chunk) | ||
| 81 | show(name, chunk, ret) | ||
| 82 | return ret | ||
| 83 | end | ||
| 84 | end | ||
| 85 | ]] | ||
| 86 | 50 | ||
| 87 | local function named(f) | 51 | local function named(f) |
| 88 | return f | 52 | return f |
