Skip to content

Commit

Permalink
Fix "bytesize not defined" and add tests. (#19)
Browse files Browse the repository at this point in the history
* Fix "bytesize not defined" and add tests.

* Format the codes.
  • Loading branch information
shuuul authored Jul 16, 2023
1 parent 28aa5ca commit f82d9f5
Show file tree
Hide file tree
Showing 4 changed files with 135 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "MRCFile"
uuid = "c18c01d3-0ab9-49c3-bd2d-8f2e64a2b7a5"
authors = ["Seth Axen <[email protected]>"]
version = "0.1.0"
version = "0.1.1"

[deps]
CodecBzip2 = "523fee87-0ab8-5b00-afb7-3ecf72e48cfd"
Expand Down
2 changes: 1 addition & 1 deletion src/data.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ function updateheader!(d::MRCData; statistics=true)

# misc
h.mode = TYPE_TO_MODE[eltype(d)]
h.nsymbt = bytesize(extendedheader(d))
h.nsymbt = sizeof(extendedheader(d))
h.nlabl = sum(s -> length(rstrip(s)) > 0, h.label)

# update statistics
Expand Down
132 changes: 132 additions & 0 deletions test/data.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
@testset "MRCData" begin
@testset "MRCData(header, extendedheader, data)" begin
h = MRCHeader()
exh = MRCExtendedHeader()
data = Array{Float32,3}(undef, (2, 2, 2))
d = MRCData(h, exh, data)
@test d.header === h
@test d.extendedheader === exh
@test d.data === data
@test eltype(d) === Float32
@test ndims(d) === 3
end

@testset "MRCData(header, extendedheader)" begin
h = MRCHeader()
exh = MRCExtendedHeader()
d = MRCData(h, exh)
@test d.header === h
@test d.extendedheader === exh
@test ndims(d) == ndims(h)
@test size(d) == size(h)
end

@testset "MRCData(size::NTuple{3,<:Integer})" begin
d = MRCData((10, 20, 30))
@test size(d) == (10, 20, 30)
@test size(d.data) == (10, 20, 30)
@test ndims(d) == 3
@test ndims(d.data) == 3

@testset "header(d::MRCData)" begin
h = header(d)
@test h.nx == 10
@test h.ny == 20
@test h.nz == 30
end
end

@testset "MRCData(size::Integer...)" begin
d1 = MRCData(10, 20, 30)
d2 = MRCData((10, 20, 30))
@test size(d1) == size(d2)
end

data = fill(1.0f0, (2, 2, 2))
h = MRCHeader()
exh = MRCExtendedHeader()
d = MRCData(h, exh, data)

@testset "getindex" begin
@test d[1, 1, 1] == 1.0f0
@test d[2, 1, 1] == 1.0f0
@test d[1, 2, 1] == 1.0f0
@test d[2, 2, 1] == 1.0f0
@test d[1, 1, 2] == 1.0f0
@test d[2, 1, 2] == 1.0f0
@test d[1, 2, 2] == 1.0f0
@test d[2, 2, 2] == 1.0f0
end

@testset "setindex!" begin
d[1, 1, 1] = 10
d[2, 1, 1] = 20
d[1, 2, 1] = 30
d[2, 2, 1] = 40
d[1, 1, 2] = 50
d[2, 1, 2] = 60
d[1, 2, 2] = 70
d[2, 2, 2] = 80
@test d[1, 1, 1] == 10
@test d[2, 1, 1] == 20
@test d[1, 2, 1] == 30
@test d[2, 2, 1] == 40
@test d[1, 1, 2] == 50
@test d[2, 1, 2] == 60
@test d[1, 2, 2] == 70
@test d[2, 2, 2] == 80
end

@testset "size" begin
@test size(d) == (2, 2, 2)
@test size(d, 1) == 2
@test size(d, 2) == 2
@test size(d, 3) == 2
end

@testset "ndims" begin
@test ndims(d) == 3
end

@testset "length" begin
@test length(d) == 8
end

@testset "iterate" begin
iter = iterate(d)
@test iter !== nothing && first(iter) == 10
end

@testset "lastindex" begin
@test lastindex(d) == 8
end

@testset "similar" begin
d_similar = similar(d)
@test typeof(d_similar) === typeof(d)
@test size(d_similar) == size(d)
@test eltype(d_similar) == eltype(d)
end

@testset "IndexStyle" begin
@test Base.IndexStyle(typeof(d)) == Base.IndexStyle(typeof(d.data))
end
end

@testset "updateheader!(d::MRCData; statistics=true)" begin
data = fill(1.0f0, (2, 2, 2))
h = MRCHeader()
@test h.nx == 0
@test h.ny == 0
@test h.nz == 0
exh = MRCExtendedHeader()
d = MRCData(h, exh, data)
updateheader!(d)
@test d.header.dmin == 1.0f0
@test d.header.dmax == 1.0f0
@test d.header.dmean == 1.0f0
@test d.header.rms == 0.0f0
@test d.header.nx == 2
@test d.header.ny == 2
@test d.header.nz == 2
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ using Test
include("utils.jl")
include("header.jl")
include("io.jl")
include("data.jl")
end

2 comments on commit f82d9f5

@sethaxen
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/87589

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.1 -m "<description of version>" f82d9f544f79f594229b5149d0759996d0c5b7c5
git push origin v0.1.1

Please sign in to comment.