Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 47 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,53 @@ const init_code = quote
println(" type: $(typeof(data))")
return println(" size: $(size(data))")
end

function make_raw_header(;
incomplete = false,
magic = ASDF.block_magic_token,
header_size = UInt16(48),
flags = UInt32(0),
compression = ASDF.compression_keys[ASDF.C_None],
allocated_size = UInt64(0),
used_size = UInt64(0),
data_size = UInt64(0),
checksum = zeros(UInt8, 16),
)
if incomplete
hdr = zeros(UInt8, 10) # Incomplete, invalid file
else
hdr = zeros(UInt8, 6 + 48)
hdr[1:4] .= magic
hdr[5:6] .= ASDF.native2big_U16(header_size)
hdr[7:10] .= ASDF.native2big_U32(flags)
hdr[11:14] .= compression
hdr[15:22] .= ASDF.native2big_U64(allocated_size)
hdr[23:30] .= ASDF.native2big_U64(used_size)
hdr[31:38] .= ASDF.native2big_U64(data_size)
hdr[39:54] .= checksum
end
return hdr
end

function make_block_header(data_bytes::AbstractVector{UInt8};
used_size = UInt64(length(data_bytes)),
allocated_size = used_size,
data_size = UInt64(length(data_bytes)),
compression = ASDF.C_None,
checksum = zeros(UInt8, 16),
)
io = IOBuffer(read=true, write=true)
write(io, make_raw_header(;
compression = ASDF.compression_keys[compression],
allocated_size,
used_size,
data_size,
checksum,
))
write(io, data_bytes)
seekstart(io)
return ASDF.read_block_header(io, Int64(0))
end
end

args = parse_args(Base.ARGS)
Expand Down
46 changes: 9 additions & 37 deletions test/test-blocks.jl
Original file line number Diff line number Diff line change
@@ -1,46 +1,11 @@
function make_raw_header(;
incomplete = false,
magic = ASDF.block_magic_token,
header_size = UInt16(48),
flags = UInt32(0),
compression = ASDF.compression_keys[ASDF.C_None],
allocated_size = UInt64(0),
used_size = UInt64(0),
data_size = UInt64(0),
checksum = zeros(UInt8, 16),
)
if incomplete
hdr = zeros(UInt8, 10) # Incomplete, invalid file
else
hdr = zeros(UInt8, 6 + 48)
hdr[1:4] .= magic
hdr[5:6] .= ASDF.native2big_U16(header_size)
hdr[7:10] .= ASDF.native2big_U32(flags)
hdr[11:14] .= compression
hdr[15:22] .= ASDF.native2big_U64(allocated_size)
hdr[23:30] .= ASDF.native2big_U64(used_size)
hdr[31:38] .= ASDF.native2big_U64(data_size)
hdr[39:54] .= checksum
end
return hdr
end

function make_block_header(; kwargs...)
io = IOBuffer()
write(io, make_raw_header(; kwargs...))
write(io, zeros(UInt8)) # Hard-coded payload
seekstart(io)
return ASDF.read_block_header(io, Int64(0))
end

function test_read_block_header(msg; kwargs...)
io = make_raw_header(; kwargs...) |> IOBuffer
@test_throws msg ASDF.read_block_header(io, Int64(0))
end

function test_read_block(msg; kwargs...)
@test_throws msg begin
header = make_block_header(; kwargs...)
header = make_block_header(UInt8[5, 6, 7, 8]; kwargs...)
ASDF.read_block(header)
end
end
Expand Down Expand Up @@ -71,9 +36,16 @@ end
checksum = fill(0xFF, 16),
)
test_read_block("Invalid compression format found: C_Blosc2";
compression = ASDF.compression_keys[ASDF.C_Blosc2],
compression = ASDF.C_Blosc2,
)
test_read_block("Actual data size different from declared data size in header.";
data_size = UInt64(9),
)
end

@testset "construction" begin
b = ASDF.Blocks()
@test isempty(b)
push!(b.arrays, ASDF.NDArrayWrapper(ones(Float32, 2)))
@test !isempty(b)
end
26 changes: 15 additions & 11 deletions test/test-ndarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,8 @@ function make_ndarray(;
return nd
end

# TODO: Can combine these into a single call in Julia v1.13
# https://github.com/JuliaLang/julia/pull/59117
function test_ndarray(error_type, error_message; kwargs...)
@test_throws error_type begin
make_ndarray(; kwargs...)
end &&
@test_throws error_message begin
make_ndarray(; kwargs...)
end
@test_throws error_type(error_message) make_ndarray(; kwargs...)
end

@testset "construction" begin
Expand Down Expand Up @@ -76,6 +69,8 @@ end
test_ndarray(
ArgumentError,
"`shape` cannot have negative elements.";
source = Int64(0),
data = nothing,
shape = Int64[-1],
)
test_ndarray(
Expand All @@ -86,13 +81,22 @@ end
end

@testset "getindex" begin
nd = make_ndarray(;
byteorder = ASDF.host_byteorder == ASDF.Byteorder_little ? ASDF.Byteorder_big : ASDF.Byteorder_little
)
opposite = ASDF.host_byteorder == ASDF.Byteorder_little ? ASDF.Byteorder_big : ASDF.Byteorder_little

nd = make_ndarray(; byteorder = opposite)
@test_throws "ndarray byteorder does not match system byteorder; byteorder swapping not yet implemented." begin
nd[]
end

@testset "byteorder swap on source path" begin
expected = Int32[1, 2, 3, 4]
disk_bytes = collect(reinterpret(UInt8, bswap.(expected)))
lbh = ASDF.LazyBlockHeaders()
push!(lbh.block_headers, make_block_header(disk_bytes))
nd = make_ndarray(; lazy_block_headers = lbh, source = Int64(0), data = nothing, byteorder = opposite)
@test nd[] == expected
end

nd = make_ndarray(; strides = Int64[5])
@test_throws "`data` has different stride from `ndarray.strides`" begin
nd[]
Expand Down
15 changes: 15 additions & 0 deletions test/test-read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,18 @@
43 54
]
end

@testset "find_first_block" begin
io = IOBuffer([zeros(UInt8, 10); ASDF.block_magic_token; zeros(UInt8, 50)])
@test ASDF.find_first_block(io) == Int64(10)
end

@testset "find_first_block with token beyond first 10 MB buffer" begin
preamble = zeros(UInt8, 10_000_000)
io = IOBuffer([preamble; ASDF.block_magic_token; zeros(UInt8, 50)])
@test ASDF.find_first_block(io) == Int64(10_000_000)
end

@testset "helper functions" begin
@test ASDF.big2native_U8(UInt8[5, 6, 7]) == 0x05
end
11 changes: 11 additions & 0 deletions test/test-write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,14 @@
ASDF.write_file(filename, doc)
end
end

@testset "Write `ASDFFile`" begin
file = ASDF.ASDFFile("my_file.asdf", Dict{Any, Any}("x" => 1), ASDF.LazyBlockHeaders())
result = YAML.write(file)
@test result == "[ASDF file \"my_file.asdf\"]\nx: 1\n"
end

@testset "helper functions" begin
@test ASDF.native2big_U8(0x05) == [0x05]
@test ASDF.native2big_U8(5) == [0x05]
end
Loading