Skip to content

Commit

Permalink
New test/read.jl
Browse files Browse the repository at this point in the history
Add eof(::File), seek(::File) etc
  • Loading branch information
samoconnor committed Jan 17, 2016
1 parent 09ae7ee commit 1061c85
Show file tree
Hide file tree
Showing 3 changed files with 187 additions and 3 deletions.
35 changes: 33 additions & 2 deletions base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ export File,
S_IROTH, S_IWOTH, S_IXOTH, S_IRWXO

import Base: uvtype, uvhandle, eventloop, fd, position, stat, close,
write, read, read!, readbytes, isopen, show,
check_open, _sizeof_uv_fs, uv_error, UVError
write, read, read!, isopen, show,
seek, seekend, skip, eof,
check_open, _sizeof_uv_fs, uv_error, UVError

include("path.jl")
include("stat.jl")
Expand Down Expand Up @@ -190,13 +191,43 @@ const SEEK_SET = Int32(0)
const SEEK_CUR = Int32(1)
const SEEK_END = Int32(2)

function seek(f::File, n::Integer)
ret = ccall(:jl_lseek, Int64, (Int32, Int64, Int32), f.handle, n, SEEK_SET)
systemerror("seek", ret == -1)
return f
end

function seekend(f::File)
ret = ccall(:jl_lseek, Int64, (Int32, Int64, Int32), f.handle, 0, SEEK_END)
systemerror("seekend", ret == -1)
return f
end

function skip(f::File, n::Integer)
ret = ccall(:jl_lseek, Int64, (Int32, Int64, Int32), f.handle, n, SEEK_CUR)
systemerror("skip", ret == -1)
return f
end

function position(f::File)
check_open(f)
ret = ccall(:jl_lseek, Int64, (Int32, Int64, Int32), f.handle, 0, SEEK_CUR)
systemerror("lseek", ret == -1)
return ret
end

function eof(f::File)
a = Vector{UInt8}(1)
ret = ccall(:jl_fs_read, Int32, (Int32, Ptr{Void}, Csize_t), f.handle, a, 1)
systemerror("eof", ret == -1)
if ret == 1
skip(f, -1)
return false
else
return true
end
end

fd(f::File) = f.handle
stat(f::File) = stat(f.handle)

Expand Down
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ function choosetests(choices = [])
"bitarray", "copy", "math", "fastmath", "functional",
"operators", "path", "ccall", "parse", "loading",
"bigint", "sorting", "statistics", "spawn", "backtrace",
"priorityqueue", "file", "mmap", "version", "resolve",
"priorityqueue", "file", "read", "mmap", "version", "resolve",
"pollfd", "mpfr", "broadcast", "complex", "socket",
"floatapprox", "datafmt", "reflection", "regex", "float16",
"combinatorics", "sysinfo", "rounding", "ranges", "mod2pi",
Expand Down
153 changes: 153 additions & 0 deletions test/read.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
mktempdir() do dir

# Create test file...
filename = joinpath(dir, "file.txt")
text = "C1,C2\n1,2\na,b\n"
open(io-> write(io, text), filename, "w")

# List of IO producers...
l = Vector{Tuple{AbstractString,Function}}()


# File
io = ()->Base.Filesystem.open(filename, Base.Filesystem.JL_O_RDONLY)
@test isa(io(), IO)
@test isa(io(), Base.Filesystem.File)
push!(l, ("File", io))


# IOStream
io = ()->open(filename)
@test isa(io(), IO)
@test isa(io(), IOStream)
push!(l, ("IOStream", io))


# IOBuffer
io = ()->IOBuffer(text)
@test isa(io(), IO)
@test isa(io(), IOBuffer)
push!(l, ("IOBuffer", io))


# PipeEndpoint
socketname = joinpath(dir, "socket")
tasks = []
io = ()-> begin
c = Base.Condition()
tsk = @async begin
s = listen(socketname)
Base.notify(c)
sock = accept(s)
write(sock,text)
close(s)
close(sock)
end
push!(tasks, tsk)
wait(c)
connect(socketname)
end
@test isa(io(), IO)
@test isa(io(), Base.PipeEndpoint)
push!(l, ("PipeEndpoint", io))


# Pipe
io = () -> open(`echo -n $text`)[1]
@test isa(io(), IO)
@test isa(io(), Pipe)
push!(l, ("Pipe", io))


for (name, io) in l

#println("$name readall...")
@test readall(io()) == text
@test readall(io()) == readall(filename)

#println("$name read...")
@test readbytes(io()) == Vector{UInt8}(text)
@test readbytes(io()) == open(readbytes,filename)
@test read(io(), UInt8) == read(IOBuffer(text), UInt8)
@test read(io(), UInt8) == open(io->read(io, UInt8), filename)
@test read(io(), Int) == read(IOBuffer(text), Int)
@test read(io(), Int) == open(io->read(io,Int),filename)
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)

#println("$name readuntil...")
@test readuntil(io(), '\n') == open(io->readuntil(io,'\n'),filename)
@test readuntil(io(), "\n") == open(io->readuntil(io,"\n"),filename)
@test readuntil(io(), ',') == open(io->readuntil(io,','),filename)

#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))

#println("$name read!...")
for n = 1:length(text)
@test read!(io(), Vector{UInt8}(n)) ==
read!(IOBuffer(text), Vector{UInt8}(n))
@test read!(io(), Vector{UInt8}(n)) ==
open(io->read!(io, Vector{UInt8}(n)), filename)
end
@test_throws EOFError read!(io(), Vector{UInt8}(length(text)+1))

#println("$name readline...")
@test readline(io()) == readline(IOBuffer(text))
@test readline(io()) == open(readline,filename)

#println("$name readlines...")
@test readlines(io()) == readlines(IOBuffer(text))
@test readlines(io()) == open(readlines,filename)
@test collect(eachline(io())) == collect(eachline(IOBuffer(text)))

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

#println("$name readcsv...")
@test readcsv(io()) == readcsv(IOBuffer(text))

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

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

#println("$name seek...")
for n = 0:length(text)-1
@test readlines(seek(io(), n)) == readlines(seek(IOBuffer(text), n))
end
#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))
end
#println("$name seekend...")
@test readall(seekend(io())) == ""
end
end

for tsk in tasks
wait(tsk)
end

end

0 comments on commit 1061c85

Please sign in to comment.