Skip to content

Commit

Permalink
add some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
mcabbott committed May 20, 2021
1 parent 84e816e commit 9ad56f4
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
70 changes: 70 additions & 0 deletions test/layers/show.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@

@testset "layer printing" begin # 2-arg show, defined with layes

@test repr(Dense(2,3)) == "Dense(2, 3)"
@test repr(Chain(Dense(2,3))) == "Chain(Dense(2, 3))"

end
@testset "nested model printing" begin # 3-arg show, defined in show.jl

# Dense -- has parameter count, but not inside a matrix

toplevel_dense = repr("text/plain", Dense(2,3))
@test occursin("Dense(2, 3)", toplevel_dense)
@test occursin("# 9 parameters", toplevel_dense)

@test Meta.isexpr(Meta.parse(toplevel_dense), :call) # comment is ignored

vector_dense = repr("text/plain", [Dense(2,3), Dense(2,3)])
@test occursin("Dense(2, 3)", vector_dense)
@test occursin("# 9 parameters", vector_dense)

matrix_dense = repr("text/plain", fill(Dense(2,3), 3, 3))
@test occursin("Dense(2, 3)", matrix_dense)
@test !occursin("# 9 parameters", matrix_dense)

tuple_dense = repr("text/plain", tuple(Dense(2,3)))
@test occursin("Dense(2, 3)", tuple_dense)
@test !occursin("# 9 parameters", tuple_dense)

# Chain -- gets split over lines at top level only

toplevel_chain = repr("text/plain", Chain(Dense(2,3)))
@test occursin("Chain(\n Dense(2, 3)", toplevel_chain)
@test occursin("# 9 parameters", toplevel_chain)
@test !occursin("# Total:", toplevel_chain)

vector_chain = repr("text/plain", [Chain(Dense(2,3)), Chain(Dense(2,3))])
@test occursin("Chain(Dense(2, 3))", vector_chain)
@test occursin("# 9 parameters", vector_chain)
@test !occursin("# Total:", vector_chain)

matrix_chain = repr("text/plain", fill(Chain(Dense(2,3)), 3,3))
@test occursin("Chain(Dense(2, 3))", matrix_chain)
@test !occursin("# 9 parameters", matrix_chain)
@test !occursin("# Total:", matrix_chain)

# ... and only long enough chains get

longchain = Chain(Dense(2, 3), Dense(3, 4), Dense(4, 5), softmax)

toplevel_longchain = repr("text/plain", longchain)
@test occursin("Chain(\n Dense(2, 3)", toplevel_longchain)
@test occursin("# 9 parameters", toplevel_longchain)
@test occursin("# Total: 6 arrays, 50 parameters", toplevel_longchain)

vector_longchain = repr("text/plain", [longchain, longchain]) # pretty ugly in reality
@test occursin("Chain(Dense(2, 3)", vector_longchain)
@test occursin("# 50 parameters", vector_longchain)
@test !occursin("# 9 parameters", vector_longchain)
@test !occursin("# Total:", vector_longchain)

matrix_longchain = repr("text/plain", fill(longchain, 3,3))
@test occursin("Chain(Dense(2, 3)", matrix_longchain)
@test !occursin("# 9 parameters", matrix_longchain)
@test !occursin("# Total:", matrix_longchain)

@test Meta.isexpr(Meta.parse(toplevel_longchain), :call) # comments are ignored
@test Meta.parse(toplevel_longchain).args[1] == :Chain

end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ end
include("layers/recurrent.jl")
include("layers/conv.jl")
include("layers/upsample.jl")
include("layers/show.jl")
end

@testset "outputsize" begin
Expand Down

0 comments on commit 9ad56f4

Please sign in to comment.