From ecfe3298c5a4519040a090f79ba332277029594d Mon Sep 17 00:00:00 2001 From: Allen Hill Date: Fri, 9 Aug 2024 19:03:12 -0700 Subject: [PATCH] Refactor and add data write roundtripping tests --- test/testutils.jl | 21 +++++++++++++++++++++ test/write.jl | 32 +++++++++++++++++++++++++------- 2 files changed, 46 insertions(+), 7 deletions(-) diff --git a/test/testutils.jl b/test/testutils.jl index fefeb40..21fcca7 100644 --- a/test/testutils.jl +++ b/test/testutils.jl @@ -28,6 +28,27 @@ function readdir_recursive(dir; join=false) return outfiles end +function comparedata(fn) + f = readc3d(fn) + datastart = (f.header.datastart - 1)*512 + fio = open(fn) + seek(fio, datastart) + refdata = read(fio) + seek(fio, datastart) + + format = f.groups[:POINT][Float32, :SCALE] > 0 ? Int16 : eltype(endianness(f))::Type + ref = C3D.readdata(fio, f.header, f.groups, format) + + compio = IOBuffer() + nb = C3D.writedata(compio, f) + compdata = take!(copy(compio)) + + seekstart(compio) + comp = C3D.readdata(compio, C3D.Header(f), f.groups, format) + + return (refdata, ref), (compdata, comp) +end + function comparefiles(reference, candidate) @test_nothrow readc3d(reference; missingpoints=false) ref = readc3d(reference; missingpoints=false) diff --git a/test/write.jl b/test/write.jl index 777ec6a..6c513c8 100644 --- a/test/write.jl +++ b/test/write.jl @@ -74,13 +74,31 @@ end end end - @testset "Whole file: artifact\"$art\"" for art in ["sample01"]#, "sample02", "sample08"] - @testset failfast=true "File \"$(replace(fn, @artifact_str(art)*'/' => ""))\"" for fn in filter(contains(r".c3d$"i), readdir_recursive(@artifact_str(art); join=true)) - p, io = mktemp() - writec3d(io, readc3d(fn)) - flush(io) - close(io) - comparefiles(fn, p) + @testset "Data:" begin + @testset "artifact\"$art\"" for art in artifacts + allfiles = filter(contains(r".c3d$"i), readdir_recursive(@artifact_str(art); join=true)) + @testset "File \"$(replace(fn, @artifact_str(art)*'/' => ""))\"" for fn in allfiles + # these 2 artifact"sample30" files have incorrectly formated residuals that we don't + # intend to replicate/match + fn == artifact"sample30/admarche2.c3d" && continue + fn == artifact"sample30/marche281.c3d" && continue + + ref, comp = comparedata(fn) + refdata, (refpoint, refresidual, refanalog) = ref + compdata, (comppoint, compresidual, companalog) = comp + + passpoint = @test refpoint == comppoint + passresidual = @test refresidual == compresidual + passanalog = @test refanalog == companalog + fails = any(p -> p isa Test.Fail, (passpoint, passresidual, passanalog)) + + nb = length(compdata) + if checkindex(Bool, eachindex(refdata), nb) + @test @view(refdata[begin:nb]) == compdata broken=fails + elseif checkindex(Bool, eachindex(compdata), length(refdata)) + @test refdata == @view(compdata[begin:length(refdata)]) broken=fails end end + end + end end