Skip to content

Commit

Permalink
Merge pull request #15 from ikirill/fix-11
Browse files Browse the repository at this point in the history
Add a unit test for #11.
  • Loading branch information
timholy authored Oct 2, 2018
2 parents 2eff75d + e54ca12 commit 77194f9
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 23 deletions.
35 changes: 12 additions & 23 deletions src/Netpbm.jl
Original file line number Diff line number Diff line change
Expand Up @@ -140,33 +140,22 @@ end
end

function parse_netpbm_size(stream::IO)
szline = strip(readline(stream))
while isempty(szline) || szline[1] == '#'
szline = strip(readline(stream))
end
parseints(szline, 2)
(parsenextint(stream), parsenextint(stream))
end

function parse_netpbm_maxval(stream::IO)
skipchars(isspace, stream, linecomment='#')
maxvalline = strip(readline(stream))
parse(Int, maxvalline)
end

function parseints(line, n)
ret = Vector{Int}(undef, n)
pos = 1
for i = 1:n
pos2 = findnext(isequal(' '), line, pos)
pos2 = pos2 === nothing ? length(line)+1 : pos2
ret[i] = parse(Int, line[pos:pos2-1])
pos = pos2+1
if pos > length(line) && i < n
error("Line terminated without finding all ", n, " integers")
end
end
tuple(ret...)
parsenextint(stream)
end

function parsenextint(stream::IO)
# ikirill: ugly, but I can't figure out a better way
skipchars(isspace, stream, linecomment='#')
from = position(stream)
mark(stream)
skipchars(isdigit, stream)
to = position(stream)
reset(stream)
parse(Int, String(read(stream, to-from+1)))
end

function pnmmax(img::AbstractArray{T}) where {T}
Expand Down
3 changes: 3 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ using Test
b = Netpbm.load(fn)
@test b == a8
end

# Issue #11
@test_nowarn load("test_header_space.pgm")
end

@testset "Color ppm" begin
Expand Down
2 changes: 2 additions & 0 deletions test/test_header_space.pgm
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
P5 2 2 255
hhhh

0 comments on commit 77194f9

Please sign in to comment.