Skip to content

Commit

Permalink
Add tests for exceptions
Browse files Browse the repository at this point in the history
  • Loading branch information
traktofon committed Aug 27, 2018
1 parent 29c469b commit 34d0917
Showing 1 changed file with 68 additions and 6 deletions.
74 changes: 68 additions & 6 deletions test/runtests.jl
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Compat: ComplexF32, ComplexF64, undef, popfirst!
using Compat: ComplexF32, ComplexF64, undef, popfirst!, IOBuffer

using FortranFiles
using Compat.Test
Expand Down Expand Up @@ -186,11 +186,73 @@ end

# --- TESTING STARTS HERE ---

@testset "Inhomogeneous arrays" begin
open("/dev/null", "w") do null
ff = FortranFile(null)
inhomA = Integer[1, big(2)]
@test_throws FortranFilesError write(ff, inhomA)
@testset "Exceptions" begin
@testset "Constructor" begin
open("/dev/null", "w") do io
# sequential access file with fixed-length records
@test_throws FortranFilesError FortranFile(io, recl=80)
# direct access file without record length
@test_throws FortranFilesError FortranFile(io, access="direct")
# unknown access mode
@test_throws FortranFilesError FortranFile(io, access="stream")
# unknown byte-order conversion
@test_throws FortranFilesError FortranFile(io, convert="host")
end
end

@testset "Writing" begin
open("/dev/null", "w") do io
# direct access files
fdir = FortranFile(io, access="direct", recl=80)
# no record number
@test_throws FortranFilesError write(fdir, zeros(10))
# write too much
@test_throws FortranFilesError write(fdir, rec=1, zeros(11))

# sequential access files
fseq = FortranFile(io)
# with record number
@test_throws MethodError write(fseq, rec=1, zeros(10))
# inhomogeneous array, see PR#4
inhomA = Integer[1, big(2)]
@test_throws FortranFilesError write(fseq, inhomA)
end
end

@testset "Reading" begin
let
# direct access files
data = UInt8[]
io = IOBuffer(data, write=true)
fdir = FortranFile(io, access="direct", recl=80)
write(fdir, rec=1, ones(10))
close(fdir)
io = IOBuffer(data)
fdir = FortranFile(io, access="direct", recl=80)
# no record number
@test_throws FortranFilesError A = read(fdir, (Float64,10))
# macro version
@test_throws FortranFilesError @fread fdir A::(Float64,10)
# read too much
@test_throws FortranFilesError A = read(fdir, rec=1, (Float64,11))
close(fdir)

# sequential access files
data = UInt8[]
io = IOBuffer(data, write=true)
fseq = FortranFile(io)
write(fseq, ones(10))
close(fseq)
io = IOBuffer(data)
fseq = FortranFile(io)
# with record number
@test_throws MethodError A = read(fseq, rec=1, (Float64,10))
# macro version
@test_throws FortranFilesError @fread fseq rec=1 A::(Float64,10)
# read too much
@test_throws FortranFilesError A = read(fseq, (Float64,11))
close(fseq)
end
end
end

Expand Down

0 comments on commit 34d0917

Please sign in to comment.