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 | |
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.
-rw-r--r-- | FIX | 1 | ||||
-rw-r--r-- | src/ltn12.lua | 131 | ||||
-rw-r--r-- | src/wsocket.c | 2 | ||||
-rw-r--r-- | test/ltn12test.lua | 275 | ||||
-rw-r--r-- | test/mimetest.lua | 36 |
5 files changed, 334 insertions, 111 deletions
@@ -1,3 +1,4 @@ | |||
1 | ltn12 avoids coroutines (so you can go wild on the C side) | ||
1 | automated tests for ftp now in use | 2 | automated tests for ftp now in use |
2 | new compat-5.1 distribution | 3 | new compat-5.1 distribution |
3 | instalation should use new directory structure | 4 | instalation should use new directory structure |
diff --git a/src/ltn12.lua b/src/ltn12.lua index 43c2755..5216ff6 100644 --- a/src/ltn12.lua +++ b/src/ltn12.lua | |||
@@ -35,64 +35,40 @@ function filter.cycle(low, ctx, extra) | |||
35 | end | 35 | end |
36 | end | 36 | end |
37 | 37 | ||
38 | --[[ | 38 | -- chains a bunch of filters together |
39 | local function chain2(f1, f2) | 39 | -- (thanks to Wim Couwenberg) |
40 | local ff1, ff2 = "", "" | 40 | function filter.chain(...) |
41 | local n = table.getn(arg) | ||
42 | local top, index = 1, 1 | ||
41 | return function(chunk) | 43 | return function(chunk) |
42 | local rf1 = chunk and "" | 44 | while true do |
43 | local rf2 = ff1 and "" | 45 | if index == top then |
44 | -- if f2 still has pending data, get it and return it | 46 | chunk = arg[index](chunk) |
45 | if ff2 ~= rf2 then | 47 | if chunk == "" or top == n then |
46 | ff2 = f2(rf2) | 48 | return chunk |
47 | if ff2 ~= "" then return ff2 end | 49 | elseif chunk then |
48 | end | 50 | index = index + 1 |
49 | -- here we know f2 needs more data | 51 | else |
50 | -- we try to get it from f1 | 52 | top = top+1 |
51 | ff1 = f1(chunk) | 53 | index = top |
52 | while 1 do | 54 | end |
53 | -- if f1 can't produce data, we need more data from the user | 55 | else |
54 | if ff1 == "" then return "" end | 56 | local original = chunk |
55 | -- otherwise we pass new data to f2 until it produces something | 57 | chunk = arg[index](original or "") |
56 | -- or f1 runs out of data too | 58 | if chunk == "" then |
57 | ff2 = f2(ff1) | 59 | index = index - 1 |
58 | if ff2 ~= "" then return ff2 end | 60 | chunk = original and chunk |
59 | ff1 = f1(rf1) | 61 | elseif chunk then |
60 | end | 62 | if index == n then return chunk |
61 | end | 63 | else index = index + 1 end |
62 | end | 64 | else |
63 | ]] | 65 | base.error("filter returned inappropriate nil") |
64 | 66 | end | |
65 | local function chain2(f1, f2) | ||
66 | local co = coroutine.create(function(chunk) | ||
67 | while true do | ||
68 | local filtered1 = f1(chunk) | ||
69 | local filtered2 = f2(filtered1) | ||
70 | local done2 = filtered1 and "" | ||
71 | while true do | ||
72 | if filtered2 == "" or filtered2 == nil then break end | ||
73 | coroutine.yield(filtered2) | ||
74 | filtered2 = f2(done2) | ||
75 | end | 67 | end |
76 | if filtered1 == "" then chunk = coroutine.yield(filtered1) | ||
77 | elseif filtered1 == nil then return nil | ||
78 | else chunk = chunk and "" end | ||
79 | end | 68 | end |
80 | end) | ||
81 | return function(chunk) | ||
82 | local _, res = coroutine.resume(co, chunk) | ||
83 | return res | ||
84 | end | 69 | end |
85 | end | 70 | end |
86 | 71 | ||
87 | -- chains a bunch of filters together | ||
88 | function filter.chain(...) | ||
89 | local f = arg[1] | ||
90 | for i = 2, table.getn(arg) do | ||
91 | f = chain2(f, arg[i]) | ||
92 | end | ||
93 | return f | ||
94 | end | ||
95 | |||
96 | ----------------------------------------------------------------------------- | 72 | ----------------------------------------------------------------------------- |
97 | -- Source stuff | 73 | -- Source stuff |
98 | ----------------------------------------------------------------------------- | 74 | ----------------------------------------------------------------------------- |
@@ -165,23 +141,28 @@ end | |||
165 | -- chains a source with a filter | 141 | -- chains a source with a filter |
166 | function source.chain(src, f) | 142 | function source.chain(src, f) |
167 | base.assert(src and f) | 143 | base.assert(src and f) |
168 | local co = coroutine.create(function() | 144 | local last_in, last_out = "", "" |
169 | while true do | 145 | return function() |
170 | local chunk, err = src() | 146 | if last_out == "" then |
171 | if err then return nil, err end | ||
172 | local filtered = f(chunk) | ||
173 | local done = chunk and "" | ||
174 | while true do | 147 | while true do |
175 | coroutine.yield(filtered) | 148 | local err |
176 | if filtered == done then break end | 149 | last_in, err = src() |
177 | filtered = f(done) | 150 | if err then return nil, err end |
151 | last_out = f(last_in) | ||
152 | if last_out ~= "" then return last_out end | ||
153 | if not last_in then | ||
154 | error('filter returned inappropriate ""') | ||
155 | end | ||
178 | end | 156 | end |
157 | elseif last_out then | ||
158 | last_out = f(last_in and "") | ||
159 | if last_in and not last_out then | ||
160 | error('filter returned inappropriate nil') | ||
161 | end | ||
162 | return last_out | ||
163 | else | ||
164 | base.error("source is empty", 2) | ||
179 | end | 165 | end |
180 | end) | ||
181 | return function() | ||
182 | local ret, a, b = coroutine.resume(co) | ||
183 | if ret then return a, b | ||
184 | else return nil, a end | ||
185 | end | 166 | end |
186 | end | 167 | end |
187 | 168 | ||
@@ -260,14 +241,16 @@ end | |||
260 | function sink.chain(f, snk) | 241 | function sink.chain(f, snk) |
261 | base.assert(f and snk) | 242 | base.assert(f and snk) |
262 | return function(chunk, err) | 243 | return function(chunk, err) |
263 | local filtered = f(chunk) | 244 | if chunk ~= "" then |
264 | local done = chunk and "" | 245 | local filtered = f(chunk) |
265 | while true do | 246 | local done = chunk and "" |
266 | local ret, snkerr = snk(filtered, err) | 247 | while true do |
267 | if not ret then return nil, snkerr end | 248 | local ret, snkerr = snk(filtered, err) |
268 | if filtered == done then return 1 end | 249 | if not ret then return nil, snkerr end |
269 | filtered = f(done) | 250 | if filtered == done then return 1 end |
270 | end | 251 | filtered = f(done) |
252 | end | ||
253 | else return 1 end | ||
271 | end | 254 | end |
272 | end | 255 | end |
273 | 256 | ||
diff --git a/src/wsocket.c b/src/wsocket.c index 0294dce..69fac4d 100644 --- a/src/wsocket.c +++ b/src/wsocket.c | |||
@@ -193,7 +193,7 @@ int sock_send(p_sock ps, const char *data, size_t count, size_t *sent, p_tm tm) | |||
193 | *sent = 0; | 193 | *sent = 0; |
194 | for ( ;; ) { | 194 | for ( ;; ) { |
195 | /* try to send something */ | 195 | /* try to send something */ |
196 | int put = send(*ps, data, count, 0); | 196 | int put = send(*ps, data, (int) count, 0); |
197 | /* if we sent something, we are done */ | 197 | /* if we sent something, we are done */ |
198 | if (put > 0) { | 198 | if (put > 0) { |
199 | *sent = put; | 199 | *sent = put; |
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 |