Skip to content

Commit

Permalink
tests for read
Browse files Browse the repository at this point in the history
  • Loading branch information
samoconnor committed Jan 28, 2016
1 parent 154bac7 commit 3407e50
Show file tree
Hide file tree
Showing 2 changed files with 213 additions and 8 deletions.
25 changes: 17 additions & 8 deletions src/Compat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -755,21 +755,30 @@ if VERSION < v"0.5.0-dev+2228"
const readstring = readall
export readstring

read(s::IO, nb::Integer=typemax(Int)) = readbytes(s, nb)
Base.read(s::IO) = readbytes(s)
Base.read(s::IO, nb) = readbytes(s, nb)

write(filename::AbstractString, args...) = open(io->write(io, args...), filename, "w")
Base.write(filename::AbstractString, args...) = open(io->write(io, args...), filename, "w")

read(filename::AbstractString, args...) = open(io->read(io, args...), filename)
read!(filename::AbstractString, a) = open(io->read!(io, a), filename)
readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename)
readline(filename::AbstractString) = open(readline, filename)
readlines(filename::AbstractString) = open(readlines, filename)
Base.read(filename::AbstractString, args...) = open(io->read(io, args...), filename)
Base.read!(filename::AbstractString, a) = open(io->read!(io, a), filename)
Base.readuntil(filename::AbstractString, args...) = open(io->readuntil(io, args...), filename)
Base.readline(filename::AbstractString) = open(readline, filename)
Base.readlines(filename::AbstractString) = open(readlines, filename)
Base.readavailable(s::IOStream) = read!(s, Vector{UInt8}(nb_available(s)))
Base.readavailable(s::IOBuffer) = read(s)

function write(to::IO, from::IO)
function Base.write(to::IO, from::IO)
while !eof(from)
write(to, readavailable(from))
end
end

function Base.eachline(filename::AbstractString)
s = open(filename)
EachLine(s, ()->close(s))
end

end

end # module
196 changes: 196 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -560,3 +560,199 @@ Base.remote_do(() -> true, 1) # Doesn't return anything so cannot be `@test`ed b
@test "1234" == @compat withenv("_TESTVAR" => 1234) do
return ENV["_TESTVAR"]
end

# https://github.com/JuliaLang/julia/pull/14660
mktempdir() do dir

verbose = false

# Create test file...
filename = joinpath(dir, "file.txt")
text = "C1,C2\n1,2\na,b\n"

# List of IO producers...
l = [
("IOStream", (text) -> begin
write(filename, text)
open(filename)
end),
("IOBuffer", (text)->IOBuffer(text))
]

open_streams = []
function cleanup()
for s in open_streams
try close(s) end
end
end

for (name, f) in l

io = ()->(s=f(text); push!(open_streams, s); s)

write(filename, text)

verbose && println("$name read...")
@test read(io(), UInt8) == read(IOBuffer(text), UInt8)
@test read(io(), UInt8) == read(filename, UInt8)
@test read(io(), Int) == read(IOBuffer(text), Int)
@test read(io(), Int) == read(filename,Int)
s1 = io()
s2 = IOBuffer(text)
@test read(s1, UInt32, 2) == read(s2, UInt32, 2)
@test !eof(s1)
@test read(s1, UInt8, 5) == read(s2, UInt8, 5)
@test !eof(s1)
@test read(s1, UInt8, 1) == read(s2, UInt8, 1)
@test eof(s1)
@test_throws EOFError read(s1, UInt8)
@test eof(s1)
close(s1)
close(s2)

verbose && println("$name eof...")
n = length(text) - 1
@test read!(io(), Vector{UInt8}(n)) ==
read!(IOBuffer(text), Vector{UInt8}(n))
@test (s = io(); read!(s, Vector{UInt8}(n)); !eof(s))
n = length(text)
@test read!(io(), Vector{UInt8}(n)) ==
read!(IOBuffer(text), Vector{UInt8}(n))
@test (s = io(); read!(s, Vector{UInt8}(n)); eof(s))
n = length(text) + 1
@test_throws EOFError read!(io(), Vector{UInt8}(n))
@test_throws EOFError read!(io(), Vector{UInt8}(n))

old_text = text
cleanup()

for text in [
old_text,
UTF8String(['A' + i % 52 for i in 1:(div(Base.SZ_UNBUFFERED_IO,2))]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO -1)]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO )]),
UTF8String(['A' + i % 52 for i in 1:( Base.SZ_UNBUFFERED_IO +1)])
]

write(filename, text)

verbose && println("$name readstring...")
@test readstring(io()) == text

@test readstring(io()) == readstring(filename)


verbose && println("$name read...")
@test read(io()) == Vector{UInt8}(text)

@test read(io()) == read(filename)

cleanup()


verbose && println("$name readbytes!...")
l = length(text)
for n = [1, 2, l-2, l-1, l, l+1, l+2]
a1 = Vector{UInt8}(n);
a2 = Vector{UInt8}(n)
s1 = io()
s2 = IOBuffer(text)
n1 = readbytes!(s1, a1)
n2 = readbytes!(s2, a2)
@test n1 == n2
@test length(a1) == length(a2)
@test a1[1:n1] == a2[1:n2]
@test n <= length(text) || eof(s1)
@test n <= length(text) || eof(s2)

cleanup()
end

verbose && println("$name read!...")
l = length(text)
for n = [1, 2, l-2, l-1, l]
@test read!(io(), Vector{UInt8}(n)) ==
read!(IOBuffer(text), Vector{UInt8}(n))
@test read!(io(), Vector{UInt8}(n)) ==
read!(filename, Vector{UInt8}(n))

cleanup()
end
@test_throws EOFError read!(io(), Vector{UInt8}(length(text)+1))


verbose && println("$name readuntil...")
@test readuntil(io(), '\n') == readuntil(IOBuffer(text),'\n')
@test readuntil(io(), '\n') == readuntil(filename,'\n')
@test readuntil(io(), "\n") == readuntil(IOBuffer(text),"\n")
@test readuntil(io(), "\n") == readuntil(filename,"\n")
@test readuntil(io(), ',') == readuntil(IOBuffer(text),',')
@test readuntil(io(), ',') == readuntil(filename,',')

cleanup()

verbose && println("$name readline...")
@test readline(io()) == readline(IOBuffer(text))
@test readline(io()) == readline(filename)

verbose && println("$name readlines...")
@test readlines(io()) == readlines(IOBuffer(text))
@test readlines(io()) == readlines(filename)
@test collect(eachline(io())) == collect(eachline(IOBuffer(text)))
@test collect(eachline(io())) == collect(eachline(filename))

cleanup()

verbose && println("$name countlines...")
@test countlines(io()) == countlines(IOBuffer(text))

verbose && println("$name readcsv...")
@test readcsv(io()) == readcsv(IOBuffer(text))
@test readcsv(io()) == readcsv(filename)

cleanup()
end

text = old_text
write(filename, text)

if !(typeof(io()) in [Base.PipeEndpoint, Pipe, TCPSocket])

verbose && println("$name position...")
@test (s = io(); read!(s, Vector{UInt8}(4)); position(s)) == 4

verbose && println("$name seek...")
for n = 0:length(text)-1
@test readlines(seek(io(), n)) == readlines(seek(IOBuffer(text), n))
cleanup()
end
verbose && println("$name skip...")
for n = 0:length(text)-1
@test readlines(seek(io(), n)) == readlines(seek(IOBuffer(text), n))
@test readlines(skip(io(), n)) == readlines(skip(IOBuffer(text), n))
cleanup()
end
verbose && println("$name seekend...")
@test readstring(seekend(io())) == ""
end


verbose && println("$name write(::IOStream, ...)")
to = open("$filename.to", "w")
write(to, io())
close(to)
@test readstring("$filename.to") == text

verbose && println("$name write(filename, ...)")
write("$filename.to", io())
@test readstring("$filename.to") == text

verbose && println("$name write(::IOBuffer, ...)")
to = IOBuffer(Vector{UInt8}(copy(text)), false, true)
write(to, io())
@test takebuf_string(to) == text

cleanup()
end

end

0 comments on commit 3407e50

Please sign in to comment.