Skip to content

Commit

Permalink
Improve test coverage; fix a small bug with empty types
Browse files Browse the repository at this point in the history
  • Loading branch information
simonster committed Aug 20, 2014
1 parent 44cb5d4 commit ac7b2a2
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 27 deletions.
22 changes: 1 addition & 21 deletions src/JLD.jl
Original file line number Diff line number Diff line change
Expand Up @@ -631,27 +631,7 @@ function write_compound(parent::Union(JldFile, JldGroup), name::ByteString, s,
end

### Size, length, etc ###
function size(dset::JldDataset)
if !exists(attrs(dset.plain), name_type_attr)
return size(dset.plain)
end
# Read the type
typename = a_read(dset.plain, name_type_attr)
if typename == "Tuple"
return size(dset.plain)
end
# Convert to Julia type
T = julia_type(typename)
if T == CompositeKind || T <: Associative || T == Expr
return ()
elseif T <: Complex
return ()
elseif isarraycomplex(T)
sz = size(dset.plain)
return sz[2:end]
end
size(dset.plain)
end
size(dset::JldDataset) = size(dset.plain)
length(dset::JldDataset) = prod(size(dset))
endof(dset::JldDataset) = length(dset)

Expand Down
9 changes: 4 additions & 5 deletions src/jld_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ else
isleaftype(T) && (!T.mutable || T.size == 0) && T.pointerfree ? h5type(parent, T, commit) : JLD_REF_TYPE
end

h5fieldtype(parent::JldGroup, x, commit::Bool) = h5fieldtype(file(parent), x)

# Write an HDF5 datatype to the file
function commit_datatype(parent::JldFile, dtype::HDF5Datatype, T::ANY)
# Write to HDF5 file
Expand Down Expand Up @@ -367,7 +365,8 @@ function gen_jlconvert(typeinfo::JldTypeInfo, T::ANY)
if T.size == 0
@eval begin
jlconvert(::Type{$T}, ::JldFile, ::Ptr) = $T()
jlconvert!(::Ptr, ::Type{$T}, ::JldFile, ::Ptr) = nothing
jlconvert!(out::Ptr, ::Type{$T}, ::JldFile, ::Ptr) =
unsafe_store!(convert(Ptr{Ptr{Void}}, out), pointer_from_objref($T()))
end
else
@eval begin
Expand Down Expand Up @@ -519,7 +518,7 @@ h5datatype(parent::JldGroup, x) = h5datatype(file(parent), x)
## Get corresponding Julia type for a specific HDF5 type, and define
## jlconvert for that type.

function reconstuct_type(parent::JldFile, dtype::HDF5Datatype, name::Symbol)
function reconstruct_type(parent::JldFile, dtype::HDF5Datatype, name::Symbol)
class_id = HDF5.h5t_get_class(dtype.id)
if class_id == HDF5.H5T_OPAQUE
if exists(dtype, "empty")
Expand Down Expand Up @@ -599,7 +598,7 @@ function jldatatype(parent::JldFile, dtype::HDF5Datatype)
T = julia_type(typename)
if T == UnsupportedType
warn("type $typename not present in workspace; reconstructing")
T = reconstuct_type(parent, dtype, gensym(typename))
T = reconstruct_type(parent, dtype, gensym(typename))
end

if !(T in BUILTIN_TYPES)
Expand Down
37 changes: 36 additions & 1 deletion test/jld.jl
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,23 @@ end
unicodestruct☺ = MyUnicodeStruct☺{Float64}(1.0, -1.0)
# Arrays of matrices (#131)
array_of_matrices = Matrix{Int}[[1 2; 3 4], [5 6; 7 8]]
# Tuple of arrays and bitstype
tup = (1, 2, [1, 2], [1 2; 3 4], bt)
# Empty tuple
empty_tup = ()
# Non-pointer-free immutable
immutable MyImmutable{T}
x::Int
y::Vector{T}
z::Bool
end
nonpointerfree_immutable_1 = MyImmutable(1, [1., 2., 3.], false)
nonpointerfree_immutable_2 = MyImmutable(2, {3., 4., 5.}, true)


iseq(x,y) = isequal(x,y)
iseq(x::MyStruct, y::MyStruct) = (x.len == y.len && x.data == y.data)
iseq(x::MyImmutable, y::MyImmutable) = (isequal(x.x, y.x) && isequal(x.y, y.y) && isequal(x.z, y.z))
iseq(c1::Array{Base.Sys.CPUinfo}, c2::Array{Base.Sys.CPUinfo}) = length(c1) == length(c2) && all([iseq(c1[i], c2[i]) for i = 1:length(c1)])
function iseq(c1::Base.Sys.CPUinfo, c2::Base.Sys.CPUinfo)
for n in Base.Sys.CPUinfo.names
Expand All @@ -112,7 +125,8 @@ macro check(fid, sym)
rethrow(e)
end
if !iseq(tmp, $sym)
error("For ", $(string(sym)), ", read value does not agree with written value")
written = $sym
error("For ", $(string(sym)), ", read value $tmp does not agree with written value $written")
end
written_type = typeof($sym)
if typeof(tmp) != written_type
Expand Down Expand Up @@ -200,6 +214,10 @@ end
@write fid emptytype
@write fid unicodestruct☺
@write fid array_of_matrices
@write fid tup
@write fid empty_tup
@write fid nonpointerfree_immutable_1
@write fid nonpointerfree_immutable_2
# Make sure we can create groups (i.e., use HDF5 features)
g = g_create(fid, "mygroup")
i = 7
Expand Down Expand Up @@ -274,6 +292,10 @@ for mmap = (true, false)
@check fidr emptytype
@check fidr unicodestruct☺
@check fidr array_of_matrices
@check fidr tup
@check fidr empty_tup
@check fidr nonpointerfree_immutable_1
@check fidr nonpointerfree_immutable_2

x1 = read(fidr, "group1/x")
@assert x1 == {1}
Expand Down Expand Up @@ -403,6 +425,12 @@ type TestType5
end
type TestType6 end
bitstype 8 TestType7
immutable TestType8
a::TestType4
b::TestType5
c::TestType6
d::TestType7
end

jldopen(fn, "w") do file
truncate_module_path(file, JLDTemp1)
Expand All @@ -413,6 +441,8 @@ jldopen(fn, "w") do file
write(file, "x5", [TestType5(TestType4(i)) for i = 1:5])
write(file, "x6", TestType6())
write(file, "x7", reinterpret(TestType7, 0x77))
write(file, "x8", TestType8(TestType4(2), TestType5(TestType4(3)),
TestType6(), reinterpret(TestType7, 0x12)))
end
end

Expand All @@ -439,4 +469,9 @@ jldopen(fn, "r") do file
end
@test typeof(read(file, "x6")).names == ()
@test reinterpret(Uint8, read(file, "x7")) == 0x77
x = read(file, "x8")
@test x.a.x == 2
@test x.b.x.x == 3
@test typeof(x.c).names == ()
@test reinterpret(Uint8, x.d) == 0x12
end

0 comments on commit ac7b2a2

Please sign in to comment.