aboutsummaryrefslogtreecommitdiff
path: root/test/ltn12test.lua
diff options
context:
space:
mode:
Diffstat (limited to 'test/ltn12test.lua')
-rw-r--r--test/ltn12test.lua275
1 files changed, 275 insertions, 0 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 @@
1local ltn12 = require("ltn12")
2
3dofile("testsupport.lua")
4
5local function format(chunk)
6 if chunk then
7 if chunk == "" then return "''"
8 else return string.len(chunk) end
9 else return "nil" end
10end
11
12local function show(name, input, output)
13 local sin = format(input)
14 local sout = format(output)
15 io.write(name, ": ", sin, " -> ", sout, "\n")
16end
17
18local 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
30end
31
32local function named(f, name)
33 return function(chunk)
34 local ret = f(chunk)
35 show(name, chunk, ret)
36 return ret
37 end
38end
39
40--------------------------------
41local 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
77end
78
79--------------------------------
80local function format(chunk)
81 if chunk then
82 if chunk == "" then return "''"
83 else return string.len(chunk) end
84 else return "nil" end
85end
86
87--------------------------------
88local 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
129end
130
131--------------------------------
132io.write("testing sink.table: ")
133local sink, t = ltn12.sink.table()
134local s, c = "", ""
135for i = 0, 10 do
136 c = string.rep(string.char(i), i)
137 s = s .. c
138 assert(sink(c), "returned error")
139end
140assert(sink(nil), "returned error")
141assert(table.concat(t) == s, "mismatch")
142print("ok")
143
144--------------------------------
145io.write("testing sink.chain (with split): ")
146sink, t = ltn12.sink.table()
147local filter = split(3)
148sink = ltn12.sink.chain(filter, sink)
149s = "123456789012345678901234567890"
150assert(sink(s), "returned error")
151assert(sink(s), "returned error")
152assert(sink(nil), "returned error")
153assert(table.concat(t) == s .. s, "mismatch")
154assert(filter(nil, 1), "filter not empty")
155print("ok")
156
157--------------------------------
158io.write("testing sink.chain (with merge): ")
159sink, t = ltn12.sink.table()
160filter = merge(10)
161sink = ltn12.sink.chain(filter, sink)
162s = string.rep("123", 30)
163s = s .. string.rep("4321", 30)
164for i = 1, 30 do
165 assert(sink("123"), "returned error")
166end
167for i = 1, 30 do
168 assert(sink("4321"), "returned error")
169end
170assert(sink(nil), "returned error")
171assert(filter(nil, 1), "filter not empty")
172assert(table.concat(t) == s, "mismatch")
173print("ok")
174
175--------------------------------
176io.write("testing source.string and pump.all: ")
177local source = ltn12.source.string(s)
178sink, t = ltn12.sink.table()
179assert(ltn12.pump.all(source, sink), "returned error")
180assert(table.concat(t) == s, "mismatch")
181print("ok")
182
183--------------------------------
184io.write("testing source.chain (with split): ")
185source = ltn12.source.string(s)
186filter = split(5)
187source = ltn12.source.chain(source, filter)
188sink, t = ltn12.sink.table()
189assert(ltn12.pump.all(source, sink), "returned error")
190assert(table.concat(t) == s, "mismatch")
191assert(filter(nil, 1), "filter not empty")
192print("ok")
193
194--------------------------------
195io.write("testing source.chain (with split) and sink.chain (with merge): ")
196source = ltn12.source.string(s)
197filter = split(5)
198source = ltn12.source.chain(source, filter)
199local filter2 = merge(13)
200sink, t = ltn12.sink.table()
201sink = ltn12.sink.chain(filter2, sink)
202assert(ltn12.pump.all(source, sink), "returned error")
203assert(table.concat(t) == s, "mismatch")
204assert(filter(nil, 1), "filter not empty")
205assert(filter2(nil, 1), "filter2 not empty")
206print("ok")
207
208--------------------------------
209io.write("testing filter.chain (and sink.chain, with split, merge): ")
210source = ltn12.source.string(s)
211filter = split(5)
212filter2 = merge(13)
213local chain = ltn12.filter.chain(filter, filter2)
214sink, t = ltn12.sink.table()
215sink = ltn12.sink.chain(chain, sink)
216assert(ltn12.pump.all(source, sink), "returned error")
217assert(table.concat(t) == s, "mismatch")
218assert(filter(nil, 1), "filter not empty")
219assert(filter2(nil, 1), "filter2 not empty")
220print("ok")
221
222--------------------------------
223io.write("testing filter.chain (and sink.chain, a bunch): ")
224source = ltn12.source.string(s)
225filter = split(5)
226filter2 = merge(13)
227local filter3 = split(7)
228local filter4 = merge(11)
229local filter5 = split(10)
230chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5)
231sink, t = ltn12.sink.table()
232sink = ltn12.sink.chain(chain, sink)
233assert(ltn12.pump.all(source, sink))
234assert(table.concat(t) == s, "mismatch")
235assert(filter(nil, 1), "filter not empty")
236assert(filter2(nil, 1), "filter2 not empty")
237assert(filter3(nil, 1), "filter3 not empty")
238assert(filter4(nil, 1), "filter4 not empty")
239assert(filter5(nil, 1), "filter5 not empty")
240print("ok")
241
242--------------------------------
243io.write("testing filter.chain (and source.chain, with split, merge): ")
244source = ltn12.source.string(s)
245filter = split(5)
246filter2 = merge(13)
247local chain = ltn12.filter.chain(filter, filter2)
248sink, t = ltn12.sink.table()
249source = ltn12.source.chain(source, chain)
250assert(ltn12.pump.all(source, sink), "returned error")
251assert(table.concat(t) == s, "mismatch")
252assert(filter(nil, 1), "filter not empty")
253assert(filter2(nil, 1), "filter2 not empty")
254print("ok")
255
256--------------------------------
257io.write("testing filter.chain (and source.chain, a bunch): ")
258source = ltn12.source.string(s)
259filter = split(5)
260filter2 = merge(13)
261local filter3 = split(7)
262local filter4 = merge(11)
263local filter5 = split(10)
264chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5)
265sink, t = ltn12.sink.table()
266source = ltn12.source.chain(source, chain)
267assert(ltn12.pump.all(source, sink))
268assert(table.concat(t) == s, "mismatch")
269assert(filter(nil, 1), "filter not empty")
270assert(filter2(nil, 1), "filter2 not empty")
271assert(filter3(nil, 1), "filter3 not empty")
272assert(filter4(nil, 1), "filter4 not empty")
273assert(filter5(nil, 1), "filter5 not empty")
274print("ok")
275