summaryrefslogtreecommitdiff
path: root/Build/source/texk/web2c/luatexdir/luasocket/test/ltn12test.lua
blob: e3f85fb40a43832b754729fc43a7ca4765ce5dd5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
local ltn12 = require("ltn12")

dofile("testsupport.lua")

local function format(chunk)
    if chunk then
        if chunk == "" then return "''"
        else return string.len(chunk) end
    else return "nil" end
end

local function show(name, input, output)
    local sin = format(input)
    local sout = format(output)
    io.write(name, ": ", sin, " -> ", sout, "\n")
end

local function chunked(length)
    local tmp
    return function(chunk)
        local ret
        if chunk and chunk ~= "" then
            tmp = chunk
        end
        ret = string.sub(tmp, 1, length)
        tmp = string.sub(tmp, length+1)
        if not chunk and ret == "" then ret = nil end
        return ret
    end
end

local function named(f, name)
    return function(chunk)
        local ret = f(chunk)
        show(name, chunk, ret)
        return ret
    end
end

--------------------------------
local function split(size) 
    local buffer = ""
    local last_out = ""
    local last_in = ""
    local function output(chunk)
        local part = string.sub(buffer, 1, size)
        buffer = string.sub(buffer, size+1)
        last_out = (part ~= "" or chunk) and part
        last_in = chunk
        return last_out
    end
    return function(chunk, done)
        if done then 
            return not last_in and not last_out 
        end
        -- check if argument is consistent with state
        if not chunk then
            if last_in and last_in ~= "" and last_out ~= "" then 
                error("nil chunk following data chunk", 2)
            end
            if not last_out then error("extra nil chunk", 2) end
            return output(chunk)
        elseif chunk == "" then
            if last_out == "" then error('extra "" chunk', 2) end
            if not last_out then error('"" chunk following nil return', 2) end
            if not last_in then error('"" chunk following nil chunk', 2) end
            return output(chunk)
        else
            if not last_in  then error("data chunk following nil chunk", 2) end
            if last_in ~= "" and last_out ~= "" then 
                error("data chunk following data chunk", 2) 
            end
            buffer = chunk
            return output(chunk)
        end
    end
end

--------------------------------
local function format(chunk)
    if chunk then
        if chunk == "" then return "''"
        else return string.len(chunk) end
    else return "nil" end
end

--------------------------------
local function merge(size) 
    local buffer = ""
    local last_out = ""
    local last_in = ""
    local function output(chunk)
        local part
        if string.len(buffer) >= size or not chunk then
            part = buffer
            buffer = ""
        else
            part = ""
        end
        last_out = (part ~= "" or chunk) and part
        last_in = chunk
        return last_out
    end
    return function(chunk, done)
        if done then 
            return not last_in and not last_out 
        end
        -- check if argument is consistent with state
        if not chunk then
            if last_in and last_in ~= "" and last_out ~= "" then 
                error("nil chunk following data chunk", 2)
            end
            if not last_out then error("extra nil chunk", 2) end
            return output(chunk)
        elseif chunk == "" then
            if last_out == "" then error('extra "" chunk', 2) end
            if not last_out then error('"" chunk following nil return', 2) end
            if not last_in then error('"" chunk following nil chunk', 2) end
            return output(chunk)
        else
            if not last_in  then error("data chunk following nil chunk", 2) end
            if last_in ~= "" and last_out ~= "" then 
                error("data chunk following data chunk", 2) 
            end
            buffer = buffer .. chunk
            return output(chunk)
        end
    end
end

--------------------------------
io.write("testing sink.table: ")
local sink, t = ltn12.sink.table()
local s, c = "", ""
for i = 0, 10 do
    c = string.rep(string.char(i), i)
    s = s .. c
    assert(sink(c), "returned error")
end
assert(sink(nil), "returned error")
assert(table.concat(t) == s, "mismatch")
print("ok")

--------------------------------
io.write("testing sink.chain (with split): ")
sink, t = ltn12.sink.table()
local filter = split(3)
sink = ltn12.sink.chain(filter, sink)
s = "123456789012345678901234567890"
assert(sink(s), "returned error")
assert(sink(s), "returned error")
assert(sink(nil), "returned error")
assert(table.concat(t) == s .. s, "mismatch")
assert(filter(nil, 1), "filter not empty")
print("ok")

--------------------------------
io.write("testing sink.chain (with merge): ")
sink, t = ltn12.sink.table()
filter = merge(10)
sink = ltn12.sink.chain(filter, sink)
s = string.rep("123", 30)
s = s .. string.rep("4321", 30)
for i = 1, 30 do
    assert(sink("123"), "returned error")
end
for i = 1, 30 do
    assert(sink("4321"), "returned error")
end
assert(sink(nil), "returned error")
assert(filter(nil, 1), "filter not empty")
assert(table.concat(t) == s, "mismatch")
print("ok")

--------------------------------
io.write("testing source.string and pump.all: ")
local source = ltn12.source.string(s)
sink, t = ltn12.sink.table()
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == s, "mismatch")
print("ok")

--------------------------------
io.write("testing source.chain (with split): ")
source = ltn12.source.string(s)
filter = split(5)
source = ltn12.source.chain(source, filter)
sink, t = ltn12.sink.table()
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
print("ok")

--------------------------------
io.write("testing source.chain (with several filters): ")
local function double(x) -- filter turning "ABC" into "AABBCC"
    if not x then return end
    local b={}
    for k in x:gmatch'.' do table.insert(b, k..k) end
    return table.concat(b)
end
source = ltn12.source.string(s)
source = ltn12.source.chain(source, double, double, double)
sink, t = ltn12.sink.table()
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == double(double(double(s))), "mismatch")
print("ok")

--------------------------------
io.write("testing source.chain (with split) and sink.chain (with merge): ")
source = ltn12.source.string(s)
filter = split(5)
source = ltn12.source.chain(source, filter)
local filter2 = merge(13)
sink, t = ltn12.sink.table()
sink = ltn12.sink.chain(filter2, sink)
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
assert(filter2(nil, 1), "filter2 not empty")
print("ok")

--------------------------------
io.write("testing sink.chain (with several filters): ")
source = ltn12.source.string(s)
sink, t = ltn12.sink.table()
sink = ltn12.sink.chain(double, double, double, sink)
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == double(double(double(s))), "mismatch")
print("ok")

--------------------------------
io.write("testing filter.chain (and sink.chain, with split, merge): ")
source = ltn12.source.string(s)
filter = split(5)
filter2 = merge(13)
local chain = ltn12.filter.chain(filter, filter2)
sink, t = ltn12.sink.table()
sink = ltn12.sink.chain(chain, sink)
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
assert(filter2(nil, 1), "filter2 not empty")
print("ok")

--------------------------------
io.write("testing filter.chain (and sink.chain, a bunch): ")
source = ltn12.source.string(s)
filter = split(5)
filter2 = merge(13)
local filter3 = split(7)
local filter4 = merge(11)
local filter5 = split(10)
chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5)
sink, t = ltn12.sink.table()
sink = ltn12.sink.chain(chain, sink)
assert(ltn12.pump.all(source, sink))
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
assert(filter2(nil, 1), "filter2 not empty")
assert(filter3(nil, 1), "filter3 not empty")
assert(filter4(nil, 1), "filter4 not empty")
assert(filter5(nil, 1), "filter5 not empty")
print("ok")

--------------------------------
io.write("testing filter.chain (and source.chain, with split, merge): ")
source = ltn12.source.string(s)
filter = split(5)
filter2 = merge(13)
local chain = ltn12.filter.chain(filter, filter2)
sink, t = ltn12.sink.table()
source = ltn12.source.chain(source, chain)
assert(ltn12.pump.all(source, sink), "returned error")
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
assert(filter2(nil, 1), "filter2 not empty")
print("ok")

--------------------------------
io.write("testing filter.chain (and source.chain, a bunch): ")
source = ltn12.source.string(s)
filter = split(5)
filter2 = merge(13)
local filter3 = split(7)
local filter4 = merge(11)
local filter5 = split(10)
chain = ltn12.filter.chain(filter, filter2, filter3, filter4, filter5)
sink, t = ltn12.sink.table()
source = ltn12.source.chain(source, chain)
assert(ltn12.pump.all(source, sink))
assert(table.concat(t) == s, "mismatch")
assert(filter(nil, 1), "filter not empty")
assert(filter2(nil, 1), "filter2 not empty")
assert(filter3(nil, 1), "filter3 not empty")
assert(filter4(nil, 1), "filter4 not empty")
assert(filter5(nil, 1), "filter5 not empty")
print("ok")