Skip to content

Commit c23720c

Browse files
authored
clean up tests (#32)
1 parent 250e852 commit c23720c

File tree

6 files changed

+471
-430
lines changed

6 files changed

+471
-430
lines changed

src/buffer.jl

+54-77
Original file line numberDiff line numberDiff line change
@@ -39,32 +39,6 @@ function Base.length(buf::Buffer)
3939
return length(buf.data)
4040
end
4141

42-
function Base.endof(buf::Buffer)
43-
return buffersize(buf)
44-
end
45-
46-
function Base.getindex(buf::Buffer, i::Integer)
47-
@boundscheck checkbounds(buf, i)
48-
@inbounds return buf.data[i+buf.bufferpos-1]
49-
end
50-
51-
function Base.checkbounds(buf::Buffer, i::Integer)
52-
if !(1 i endof(buf))
53-
throw(BoundsError(buf, i))
54-
end
55-
end
56-
57-
function Base.getindex(buf::Buffer, r::UnitRange{<:Integer})
58-
@boundscheck checkbounds(buf, r)
59-
@inbounds return buf.data[r+buf.bufferpos-1]
60-
end
61-
62-
function Base.checkbounds(buf::Buffer, r::UnitRange{<:Integer})
63-
if !isempty(r) && !(1 first(r) && last(r) endof(buf))
64-
throw(BoundsError(buf, r))
65-
end
66-
end
67-
6842
function bufferptr(buf::Buffer)
6943
return pointer(buf.data, buf.bufferpos)
7044
end
@@ -77,42 +51,6 @@ function buffermem(buf::Buffer)
7751
return Memory(bufferptr(buf), buffersize(buf))
7852
end
7953

80-
# Notify that `n` bytes are consumed from `buf`.
81-
function consumed!(buf::Buffer, n::Integer)
82-
buf.bufferpos += n
83-
return buf
84-
end
85-
86-
# Notify that `n` bytes are supplied to `buf`.
87-
function supplied!(buf::Buffer, n::Integer)
88-
buf.marginpos += n
89-
return buf
90-
end
91-
92-
function consumed2!(buf::Buffer, n::Integer)
93-
buf.bufferpos += n
94-
buf.total += n
95-
return buf
96-
end
97-
98-
function supplied2!(buf::Buffer, n::Integer)
99-
buf.marginpos += n
100-
buf.total += n
101-
return buf
102-
end
103-
104-
function readbyte!(buf::Buffer)
105-
b = buf.data[buf.bufferpos]
106-
consumed!(buf, 1)
107-
return b
108-
end
109-
110-
function writebyte!(buf::Buffer, b::UInt8)
111-
buf.data[buf.marginpos] = b
112-
supplied!(buf, 1)
113-
return 1
114-
end
115-
11654
function marginptr(buf::Buffer)
11755
return pointer(buf.data, buf.marginpos)
11856
end
@@ -149,6 +87,44 @@ function reset!(buf::Buffer)
14987
return buf.bufferpos
15088
end
15189

90+
# Notify that `n` bytes are consumed from `buf`.
91+
function consumed!(buf::Buffer, n::Integer)
92+
buf.bufferpos += n
93+
return buf
94+
end
95+
96+
# Notify that `n` bytes are supplied to `buf`.
97+
function supplied!(buf::Buffer, n::Integer)
98+
buf.marginpos += n
99+
return buf
100+
end
101+
102+
function consumed2!(buf::Buffer, n::Integer)
103+
buf.bufferpos += n
104+
buf.total += n
105+
return buf
106+
end
107+
108+
function supplied2!(buf::Buffer, n::Integer)
109+
buf.marginpos += n
110+
buf.total += n
111+
return buf
112+
end
113+
114+
# Discard buffered data and initialize positions.
115+
function initbuffer!(buf::Buffer)
116+
buf.markpos = 0
117+
buf.bufferpos = buf.marginpos = 1
118+
buf.total = 0
119+
return buf
120+
end
121+
122+
# Remove all buffered data.
123+
function emptybuffer!(buf::Buffer)
124+
buf.marginpos = buf.bufferpos
125+
return buf
126+
end
127+
152128
# Make margin with ≥`minsize` and return the size of it.
153129
function makemargin!(buf::Buffer, minsize::Integer)
154130
@assert minsize 0
@@ -165,11 +141,12 @@ function makemargin!(buf::Buffer, minsize::Integer)
165141
datasize = buf.marginpos - buf.markpos
166142
end
167143
copy!(buf.data, 1, buf.data, datapos, datasize)
144+
shift = datapos - 1
168145
if buf.markpos > 0
169-
buf.markpos -= datapos - 1
146+
buf.markpos -= shift
170147
end
171-
buf.bufferpos -= datapos - 1
172-
buf.marginpos -= datapos - 1
148+
buf.bufferpos -= shift
149+
buf.marginpos -= shift
173150
end
174151
if marginsize(buf) < minsize
175152
# expand data buffer
@@ -179,10 +156,18 @@ function makemargin!(buf::Buffer, minsize::Integer)
179156
return marginsize(buf)
180157
end
181158

182-
# Remove all buffered data.
183-
function emptybuffer!(buf::Buffer)
184-
buf.marginpos = buf.bufferpos
185-
return buf
159+
# Read a byte.
160+
function readbyte!(buf::Buffer)
161+
b = buf.data[buf.bufferpos]
162+
consumed!(buf, 1)
163+
return b
164+
end
165+
166+
# Write a byte.
167+
function writebyte!(buf::Buffer, b::UInt8)
168+
buf.data[buf.marginpos] = b
169+
supplied!(buf, 1)
170+
return 1
186171
end
187172

188173
# Skip `n` bytes in the buffer.
@@ -192,14 +177,6 @@ function skipbuffer!(buf::Buffer, n::Integer)
192177
return buf
193178
end
194179

195-
# Discard buffered data and initialize positions.
196-
function initbuffer!(buf::Buffer)
197-
buf.markpos = 0
198-
buf.bufferpos = buf.marginpos = 1
199-
buf.total = 0
200-
return buf
201-
end
202-
203180
# Take the ownership of the marked data.
204181
function takemarked!(buf::Buffer)
205182
@assert buf.markpos > 0

test/codecidentity.jl

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# TODO: Remove this file in the future.
2+
using TranscodingStreams.CodecIdentity
3+
@testset "Identity Codec (deprecated)" begin
4+
TranscodingStreams.test_roundtrip_transcode(Identity, Identity)
5+
TranscodingStreams.test_roundtrip_read(IdentityStream, IdentityStream)
6+
TranscodingStreams.test_roundtrip_write(IdentityStream, IdentityStream)
7+
TranscodingStreams.test_roundtrip_lines(IdentityStream, IdentityStream)
8+
end

test/codecinvalid.jl

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# This does not implement necessary interface methods.
2+
struct InvalidCodec <: TranscodingStreams.Codec end
3+
4+
@testset "Invalid Codec" begin
5+
@test_throws MethodError read(TranscodingStream(InvalidCodec(), IOBuffer()))
6+
end

0 commit comments

Comments
 (0)