diff --git a/test/runtests.jl b/test/runtests.jl index 760c4b2..145877a 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) diff --git a/test/test-blocks.jl b/test/test-blocks.jl index 1570b6f..3f529bf 100644 --- a/test/test-blocks.jl +++ b/test/test-blocks.jl @@ -1,38 +1,3 @@ -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)) @@ -40,7 +5,7 @@ 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 @@ -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 diff --git a/test/test-ndarray.jl b/test/test-ndarray.jl index beab44f..702eb82 100644 --- a/test/test-ndarray.jl +++ b/test/test-ndarray.jl @@ -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 @@ -76,6 +69,8 @@ end test_ndarray( ArgumentError, "`shape` cannot have negative elements."; + source = Int64(0), + data = nothing, shape = Int64[-1], ) test_ndarray( @@ -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[] diff --git a/test/test-read.jl b/test/test-read.jl index 94f6bc3..bd25b6a 100644 --- a/test/test-read.jl +++ b/test/test-read.jl @@ -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 diff --git a/test/test-write.jl b/test/test-write.jl index 613a1b5..57d935a 100644 --- a/test/test-write.jl +++ b/test/test-write.jl @@ -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