forked from JuliaIO/TranscodingStreams.jl
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcodecnoop.jl
More file actions
331 lines (289 loc) · 10.8 KB
/
codecnoop.jl
File metadata and controls
331 lines (289 loc) · 10.8 KB
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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
@testset "Noop Codec" begin
source = IOBuffer("")
stream = TranscodingStream(Noop(), source)
@test eof(stream)
@inferred eof(stream)
@test read(stream) == UInt8[]
@test occursin("mode=read", repr(stream))
source = IOBuffer("foo")
stream = TranscodingStream(Noop(), source)
@test !eof(stream)
@test read(stream) == b"foo"
close(stream)
data = rand(UInt8, 100_000)
source = IOBuffer(data)
stream = TranscodingStream(Noop(), source)
@test !eof(stream)
@test read(stream) == data
close(stream)
stream = TranscodingStream(Noop(), IOBuffer())
@test_throws EOFError read(stream, UInt8)
@test_throws EOFError unsafe_read(stream, pointer(Vector{UInt8}(undef, 3)), 3)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer("foobar"), bufsize=1)
@test read(stream, UInt8) === UInt8('f')
data = Vector{UInt8}(undef, 5)
unsafe_read(stream, pointer(data), 5) === nothing
@test data == b"oobar"
close(stream)
sink = IOBuffer()
stream = TranscodingStream(Noop(), sink)
@test write(stream, "foo") === 3
@test occursin("mode=write", repr(stream))
flush(stream)
@test take!(sink) == b"foo"
close(stream)
data = rand(UInt8, 100_000)
sink = IOBuffer()
stream = TranscodingStream(Noop(), sink)
for i in 1:10_000
@assert write(stream, data[10(i-1)+1:10i]) == 10
end
flush(stream)
@test take!(sink) == data
close(stream)
stream = TranscodingStream(Noop(), IOBuffer(b"foobarbaz"))
@test position(stream) === 0
read(stream, UInt8)
@test position(stream) === 1
read(stream)
@test position(stream) === 9
data = collect(0x00:0x0f)
stream = TranscodingStream(Noop(), IOBuffer(data))
@test !ismarked(stream)
mark(stream)
@test ismarked(stream)
@test [read(stream, UInt8) for _ in 1:3] == data[1:3]
reset(stream)
@test !ismarked(stream)
@test [read(stream, UInt8) for _ in 1:3] == data[1:3]
mark(stream)
@test ismarked(stream)
unmark(stream)
@test !ismarked(stream)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer(b"foobarbaz"))
seek(stream, 2)
@test read(stream, 3) == b"oba"
seek(stream, 0)
@test read(stream, 3) == b"foo"
seekstart(stream)
@test read(stream, 3) == b"foo"
seekend(stream)
@test eof(stream)
close(stream)
data = collect(0x00:0x0f)
stream = TranscodingStream(Noop(), IOBuffer(data))
@test read(stream, UInt8) == data[1]
skip(stream, 1)
@test read(stream, UInt8) == data[3]
skip(stream, 5)
@test read(stream, UInt8) == data[9]
skip(stream, 7)
@test eof(stream)
close(stream)
# skip offset > bufsize
data = collect(0x00:0x0f)
stream = TranscodingStream(Noop(), IOBuffer(data), bufsize=2)
@test read(stream, UInt8) == data[1]
skip(stream, 4)
@test read(stream, UInt8) == data[6]
skip(stream, 3)
@test read(stream, UInt8) == data[10]
skip(stream, 6)
@test eof(stream)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer("foo"))
read(stream, UInt8)
@test_throws ArgumentError skip(stream, -1)
skip(stream, 100)
@test eof(stream)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer("foo"))
out = zeros(UInt8, 3)
@test bytesavailable(stream) == 0
@test TranscodingStreams.unsafe_read(stream, pointer(out), 10) == 3
@test out == b"foo"
close(stream)
data = rand(UInt8, 1999)
# unmarked
stream = TranscodingStream(Noop(), IOBuffer(data), bufsize=7)
@test hash(read(stream)) == hash(data)
@test length(stream.state.buffer1.data) == 7
# marked
stream = TranscodingStream(Noop(), IOBuffer(data), bufsize=7)
mark(stream)
@test hash(read(stream)) == hash(data)
@test hash(stream.state.buffer1.data[1:length(data)]) == hash(data)
close(stream)
stream = NoopStream(NoopStream(IOBuffer("foobar")))
@test read(stream) == b"foobar"
close(stream)
stream = NoopStream(NoopStream(NoopStream(IOBuffer("foobar"))))
@test read(stream) == b"foobar"
close(stream)
# Two buffers are the same object.
stream = NoopStream(IOBuffer("foo"))
@test stream.state.buffer1 === stream.state.buffer2
# Nested NoopStreams share the same buffer.
s0 = IOBuffer("foo")
s1 = NoopStream(s0)
s2 = NoopStream(s1)
s3 = NoopStream(s2)
@test s1.state.buffer1 === s2.state.buffer1 === s3.state.buffer1 ===
s1.state.buffer2 === s2.state.buffer2 === s3.state.buffer2
stream = TranscodingStream(Noop(), IOBuffer(b"foobar"))
@test TranscodingStreams.stats(stream).in === Int64(0)
@test TranscodingStreams.stats(stream).out === Int64(0)
read(stream)
@test TranscodingStreams.stats(stream).in === Int64(6)
@test TranscodingStreams.stats(stream).out === Int64(6)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer())
@test TranscodingStreams.stats(stream).in === Int64(0)
@test TranscodingStreams.stats(stream).out === Int64(0)
write(stream, b"foobar")
flush(stream)
@test TranscodingStreams.stats(stream).in === Int64(6)
@test TranscodingStreams.stats(stream).out === Int64(6)
close(stream)
stream = TranscodingStream(Noop(), IOBuffer())
@test stream.state.mode == :idle
@test write(stream) == 0
@test stream.state.mode == :write
close(stream)
stream = NoopStream(IOBuffer("foobar"))
@test bytesavailable(stream) === 0
@test readavailable(stream) == b""
@test read(stream, UInt8) === UInt8('f')
@test bytesavailable(stream) === 5
@test readavailable(stream) == b"oobar"
close(stream)
data = b""
@test transcode(Noop, data) == data
@test transcode(Noop, data) !== data
data = b"foo"
@test transcode(Noop, data) == data
@test transcode(Noop, data) !== data
data = Vector{UInt8}()
@test TranscodingStreams.unsafe_transcode!(Noop(), data, data) == data
@test_throws AssertionError transcode(Noop(), data, data)
data = b""
@test transcode(Noop(), data) == data
@test transcode(Noop(), data) !== data
@test transcode(Noop(), data, Vector{UInt8}()) == data
@test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) == data
@test transcode(Noop(), data, Vector{UInt8}()) !== data
@test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) !== data
output = Vector{UInt8}()
@test transcode(Noop(), data, output) === output
output = TranscodingStreams.Buffer(Vector{UInt8}())
@test transcode(Noop(), data, output) === output.data
data = b"foo"
@test transcode(Noop(), data) == data
@test transcode(Noop(), data) !== data
@test transcode(Noop(), data, Vector{UInt8}()) == data
@test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) == data
@test transcode(Noop(), data, Vector{UInt8}()) !== data
@test transcode(Noop(), data, TranscodingStreams.Buffer(Vector{UInt8}())) !== data
output = Vector{UInt8}()
@test transcode(Noop(), data, output) === output
output = TranscodingStreams.Buffer(Vector{UInt8}())
@test transcode(Noop(), data, output) === output.data
TranscodingStreams.test_roundtrip_transcode(Noop, Noop)
TranscodingStreams.test_roundtrip_read(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_write(NoopStream, NoopStream)
TranscodingStreams.test_roundtrip_lines(NoopStream, NoopStream)
# switch write => read
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
@test_throws ArgumentError begin
write(stream, b"xyz")
read(stream, 3)
end
# switch read => write
stream = NoopStream(IOBuffer(b"foobar", read=true, write=true))
@test_throws ArgumentError begin
read(stream, 3)
write(stream, b"xyz")
end
stream = NoopStream(IOBuffer(""))
@test TranscodingStreams.unread(stream, b"foo") === nothing
@test read(stream, 3) == b"foo"
close(stream)
stream = NoopStream(IOBuffer("foo"))
@test read(stream, 3) == b"foo"
@test TranscodingStreams.unread(stream, b"bar") === nothing
@test read(stream, 3) == b"bar"
close(stream)
stream = NoopStream(IOBuffer("foobar"))
@test TranscodingStreams.unread(stream, b"baz") === nothing
@test read(stream, 3) == b"baz"
@test read(stream, 3) == b"foo"
@test read(stream, 3) == b"bar"
@test eof(stream)
close(stream)
stream = NoopStream(IOBuffer("foobar"))
@test read(stream, 3) == b"foo"
@test TranscodingStreams.unread(stream, b"baz") === nothing
@test read(stream, 3) == b"baz"
@test read(stream, 3) == b"bar"
@test eof(stream)
close(stream)
stream = NoopStream(IOBuffer("foobar"))
@test read(stream, 3) == b"foo"
@test read(stream, 3) == b"bar"
@test TranscodingStreams.unread(stream, b"baz") === nothing
@test read(stream, 3) == b"baz"
@test eof(stream)
close(stream)
stream = NoopStream(IOBuffer("foobar"))
@test_throws ArgumentError TranscodingStreams.unsafe_unread(stream, pointer(b"foo"), -1)
close(stream)
stream = NoopStream(IOBuffer(""))
unsafe_write(stream, C_NULL, 0)
@test eof(stream) # write
close(stream)
@test eof(stream) # close
@testset "readuntil" begin
stream = NoopStream(IOBuffer(""))
data = readuntil(stream, 0x00)
@test data isa Vector{UInt8}
@test isempty(data)
stream = NoopStream(IOBuffer("foo,bar"))
@test readuntil(stream, UInt8(',')) == b"foo"
@test read(stream) == b"bar"
stream = NoopStream(IOBuffer("foo,bar"))
@test readuntil(stream, UInt8(','), keep = false) == b"foo"
@test read(stream) == b"bar"
stream = NoopStream(IOBuffer("foo,bar"))
@test readuntil(stream, UInt8(','), keep = true) == b"foo,"
@test read(stream) == b"bar"
end
@test_throws ArgumentError NoopStream(IOBuffer(), bufsize=0)
@test_throws ArgumentError NoopStream(let s = IOBuffer(); close(s); s; end)
@test_throws ArgumentError TranscodingStream(Noop(), IOBuffer(), bufsize=0)
@test_throws ArgumentError TranscodingStream(Noop(), IOBuffer(), sharedbuf=true)
@testset "position" begin
iob = IOBuffer()
sink = IOBuffer()
stream = NoopStream(sink, bufsize=16)
@test position(stream) == position(iob)
for len in 0:10:100
write(stream, repeat("x", len))
write(iob, repeat("x", len))
@test position(stream) == position(iob)
end
@test position(stream) == position(sink) == position(iob)
close(stream)
close(iob)
mktemp() do path, sink
stream = NoopStream(sink, bufsize=16)
pos = 0
for len in 0:10:100
write(stream, repeat("x", len))
pos += len
@test position(stream) == pos
end
end
end
end