tests/testthat/test-png_write.R

test_that("a written PNG decodes to the codes it was given", {
    skip_if_not_installed("png")
    set.seed(5)
    m <- matrix(sample(0:255, 40 * 60, replace = TRUE), 40, 60)
    f <- tempfile(fileext = ".png")
    volumr:::write_png(m, f)
    back <- round(png::readPNG(f) * 255)
    expect_identical(as.integer(back), as.integer(m))
})

test_that("a truecolour PNG decodes to the array it was given", {
    skip_if_not_installed("png")
    set.seed(6)
    a <- array(sample(0:255, 5 * 16 * 3, replace = TRUE), c(5L, 16L, 3L))
    f <- tempfile(fileext = ".png")
    volumr:::write_png(a, f)
    back <- round(png::readPNG(f) * 255)
    expect_identical(as.integer(back), as.integer(a))
})

test_that("the encoder is deterministic", {
    m <- matrix(rep(1:100, 4), 40, 10)
    f1 <- tempfile(fileext = ".png"); f2 <- tempfile(fileext = ".png")
    volumr:::write_png(m, f1); volumr:::write_png(m, f2)
    expect_identical(readBin(f1, "raw", file.size(f1)),
                     readBin(f2, "raw", file.size(f2)))
})

test_that("a constant image is written and read back", {
    skip_if_not_installed("png")
    m <- matrix(0L, 8, 8)
    f <- tempfile(fileext = ".png")
    volumr:::write_png(m, f)
    expect_identical(as.integer(round(png::readPNG(f) * 255)), as.integer(m))
})

Try the volumr package in your browser

Any scripts or data that you put into this service are public.

volumr documentation built on Dec. 2, 2026, 5:07 p.m.