Skip to content

Commit 033e2d4

Browse files
committed
WIP: Add some more tests
1 parent a2d698e commit 033e2d4

File tree

2 files changed

+149
-11
lines changed

2 files changed

+149
-11
lines changed

base/iobuffer.jl

+12-11
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,15 @@ function IOBuffer(;
191191
truncate::Union{Bool,Nothing}=true,
192192
maxsize::Integer=typemax(Int),
193193
sizehint::Union{Integer,Nothing}=nothing)
194-
size = sizehint !== nothing ? Int(sizehint) : maxsize != typemax(Int) ? Int(maxsize) : 32
194+
size = if sizehint != nothing
195+
max(0, Int(sizehint))
196+
else
197+
if maxsize == typemax(Int)
198+
32
199+
else
200+
max(0, Int(maxsize))
201+
end
202+
end
195203
flags = open_flags(read=read, write=write, append=append, truncate=truncate)
196204
buf = IOBuffer(
197205
# A common usecase of IOBuffer is to incrementally construct strings. By using StringMemory
@@ -302,21 +310,14 @@ function read(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int3
302310
return x
303311
end
304312

305-
function read_sub(from::GenericIOBuffer, a::AbstractArray{T}, offs, nel) where T
313+
function read_sub(from::GenericIOBuffer, a::MutableDenseArrayType{T}, offs, nel) where T
306314
require_one_based_indexing(a)
307315
from.readable || _throw_not_readable()
308316
if offs+nel-1 > length(a) || offs < 1 || nel < 0
309317
throw(BoundsError())
310318
end
311-
# Use memcpy where applicable for performance
312-
if isa(a, MutableDenseArrayType{UInt8})
313-
nb = UInt(nel * sizeof(T))
314-
GC.@preserve a unsafe_read(from, pointer(a, offs), nb)
315-
else
316-
for i = offs:offs+nel-1
317-
a[i] = read(from, T)
318-
end
319-
end
319+
nb = UInt(nel * sizeof(T))
320+
GC.@preserve a unsafe_read(from, pointer(a, offs), nb)
320321
return a
321322
end
322323

test/iobuffer.jl

+137
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,143 @@ ioslength(io::IOBuffer) = (io.seekable ? io.size : bytesavailable(io))
66

77
bufcontents(io::Base.GenericIOBuffer) = unsafe_string(pointer(io.data), io.size)
88

9+
@testset "Basic tests" begin
10+
@test_throws ArgumentError IOBuffer(;maxsize=-1)
11+
@test_throws ArgumentError IOBuffer([0x01]; maxsize=-1)
12+
13+
# Test that sizehint actually will sizehint the vector,
14+
v = UInt8[]
15+
buf = IOBuffer(v; sizehint=64, write=true)
16+
@test length(v.ref.mem) >= 64
17+
18+
19+
end
20+
21+
@testset "Basic reading" begin
22+
# Readavailable is equal to read
23+
buf = IOBuffer("abcdef")
24+
@test read(buf, UInt8) == UInt8('a')
25+
@test bytesavailable(buf) == 5
26+
@test readavailable(buf) == b"bcdef"
27+
28+
# Reading less than all the bytes
29+
buf = IOBuffer(b"ABCDEFGHIJ")
30+
@test read(buf, 1) == b"A"
31+
@test read(buf, 3) == b"BCD"
32+
33+
# Reading more bytes than available will not error
34+
@test read(buf, 100) == b"EFGHIJ"
35+
end
36+
37+
@testset "Byte occursin GenericIOBuffer" begin
38+
buf = IOBuffer(@view(collect(0x1f:0x3d)[1:end]))
39+
@test occursin(0x1f, buf)
40+
@test occursin(0x3d, buf)
41+
@test occursin(0x2a, buf)
42+
43+
@test !occursin(0xff, buf)
44+
@test !occursin(0x00, buf)
45+
46+
v = Vector{UInt8}("bcdefg")
47+
pushfirst!(v, UInt8('a'))
48+
buf = IOBuffer(v)
49+
@test occursin(UInt8('a'), buf)
50+
read(buf, UInt8)
51+
@test !occursin(UInt8('a'), buf)
52+
@test !occursin(0x00, buf)
53+
54+
buf = IOBuffer("abcdefg")
55+
@test occursin(UInt8('a'), buf)
56+
end
57+
58+
@testset "Non-Memory backed IOBuffer" begin
59+
buf = IOBuffer(Test.GenericArray(collect(0x02:0x0d)), read=true)
60+
@test read(buf) == 0x02:0x0d
61+
62+
buf = IOBuffer(Test.GenericArray(collect(0x02:0x0d)), read=true)
63+
@test read(buf, UInt8) == 0x02
64+
@test read(buf) == 0x03:0x0d
65+
66+
v = view(collect(UInt8('a'):UInt8('z')), 4:10)
67+
buf = IOBuffer(v, read=true, write=true)
68+
@test read(buf, UInt8) == UInt8('d')
69+
@test read(buf) == UInt8('e'):UInt8('j')
70+
seekstart(buf)
71+
@test read(buf, UInt8) == UInt8('d')
72+
write(buf, UInt8('x'))
73+
write(buf, "ABC")
74+
seekstart(buf)
75+
@test read(buf) == b"dxABCij"
76+
end
77+
78+
@testset "Copying" begin
79+
# Test offset is preserved when copying
80+
v = UInt8[]
81+
pushfirst!(v, UInt8('a'), UInt8('b'), UInt8('c'))
82+
buf = IOBuffer(v; write=true, read=true, append=true)
83+
write(buf, "def")
84+
read(buf, UInt16)
85+
buf2 = copy(buf)
86+
@test String(read(buf)) == "cdef"
87+
@test String(read(buf2)) == "cdef"
88+
89+
# Test copying with non-Memory backed GenericIOBuffer
90+
buf = IOBuffer(Test.GenericArray(collect(0x02:0x0d)), read=true)
91+
@test_broken read(buf, UInt16)
92+
buf2 = copy(buf)
93+
@test isreadable(buf2)
94+
@test !iswritable(buf2)
95+
@test_broken read(buf2) == 0x04:0x0d
96+
end
97+
98+
@testset "copyuntil" begin
99+
a = IOBuffer(b"abcdeajdgabdfg")
100+
b = IOBuffer(collect(b"xx"); write=true, read=true, append=true)
101+
copyuntil(b, a, UInt8('a'))
102+
@test read(b) == b"xx"
103+
seekstart(b)
104+
copyuntil(b, a, UInt8('a'); keep=true)
105+
@test read(b) == b"xxbcdea"
106+
seekstart(b)
107+
copyuntil(b, a, UInt('w'))
108+
@test read(b) == b"xxbcdeajdgabdfg"
109+
end
110+
111+
@testset "copyline" begin
112+
a = IOBuffer(b"abcde\nabc\r\nabc\n\r\nac")
113+
b = IOBuffer()
114+
copyline(b, a)
115+
@test take!(copy(b)) == b"abcde"
116+
copyline(b, a)
117+
@test take!(copy(b)) == b"abcdeabc"
118+
copyline(b, a; keep=true)
119+
@test take!(copy(b)) == b"abcdeabcabc\n"
120+
copyline(b, a; keep=false)
121+
@test take!(copy(b)) == b"abcdeabcabc\n"
122+
copyline(b, a; keep=false)
123+
@test take!(copy(b)) == b"abcdeabcabc\nac"
124+
end
125+
126+
@testset "take!" begin
127+
a = IOBuffer("abc")
128+
@test take!(a) == b"abc"
129+
130+
v = UInt8[]
131+
pushfirst!(v, 0x0a)
132+
buf = IOBuffer(v; write=true, append=true)
133+
write(buf, "def")
134+
@test take!(buf) == b"\ndef"
135+
136+
v = view(collect(b"abcdefghij"), 3:9)
137+
buf = IOBuffer(v; write=true, read=true)
138+
read(buf, UInt8)
139+
write(buf, "xxy")
140+
@test take!(buf) == b"cxxyghi"
141+
142+
v = view(collect(b"abcdefghij"), 3:9)
143+
buf = IOBuffer(v; write=true, read=true)
144+
end
145+
9146
@testset "Read/write empty IOBuffer" begin
10147
io = IOBuffer()
11148
@test eof(io)

0 commit comments

Comments
 (0)