tests/testthat/test-format-s.R

context("format s")

test_that(paste("fmt_new(\"s\") outputs SI-prefix notation with",
                "default precision 6"), {
  f <- fmt_new("s")
  expect_equal(f(0), "0.00000")
  expect_equal(f(1), "1.00000")
  expect_equal(f(10), "10.0000")
  expect_equal(f(100), "100.000")
  expect_equal(f(999.5), "999.500")
  expect_equal(f(999500), "999.500k")
  expect_equal(f(1000), "1.00000k")
  expect_equal(f(100), "100.000")
  expect_equal(f(1400), "1.40000k")
  expect_equal(f(1500.5), "1.50050k")
  expect_equal(f(.00001), "10.0000\u03BC")
  expect_equal(f(.000001), "1.00000\u03BC")
})

test_that(paste("fmt_new(\"[.precision]s\") outputs SI-prefix",
                "notation with precision significant digits"), {
  f <- fmt_new(".3s")
  expect_equal(f(0), "0.00")
  expect_equal(f(1), "1.00")
  expect_equal(f(10), "10.0")
  expect_equal(f(100), "100")
  expect_equal(f(999.5), "1.00k")
  expect_equal(f(999500), "1.00M")
  expect_equal(f(1000), "1.00k")
  expect_equal(f(1500.5), "1.50k")
  expect_equal(f(145500000), "146M")
  expect_equal(f(145999999.999999347), "146M")
  expect_equal(f(1e26), "100Y")
  expect_equal(f(.000001), "1.00\u03BC")
  expect_equal(f(.009995), "10.0m")
  f <- fmt_new(".4s")
  expect_equal(f(999.5), "999.5")
  expect_equal(f(999500), "999.5k")
  expect_equal(f(.009995), "9.995m")

})

test_that("fmt_new(\"s\") formats numbers smaller than 1e-24 with yocto", {
  f <- fmt_new(".8s")
  expect_equal(f(1.29e-30), "0.0000013y") # Note: rounded!
  expect_equal(f(1.29e-29), "0.0000129y")
  expect_equal(f(1.29e-28), "0.0001290y")
  expect_equal(f(1.29e-27), "0.0012900y")
  expect_equal(f(1.29e-26), "0.0129000y")
  expect_equal(f(1.29e-25), "0.1290000y")
  expect_equal(f(1.29e-24), "1.2900000y")
  expect_equal(f(1.29e-23), "12.900000y")
  expect_equal(f(1.29e-22), "129.00000y")
  expect_equal(f(1.29e-21), "1.2900000z")
  expect_equal(f(-1.29e-30), "-0.0000013y") # Note: rounded!
  expect_equal(f(-1.29e-29), "-0.0000129y")
  expect_equal(f(-1.29e-28), "-0.0001290y")
  expect_equal(f(-1.29e-27), "-0.0012900y")
  expect_equal(f(-1.29e-26), "-0.0129000y")
  expect_equal(f(-1.29e-25), "-0.1290000y")
  expect_equal(f(-1.29e-24), "-1.2900000y")
  expect_equal(f(-1.29e-23), "-12.900000y")
  expect_equal(f(-1.29e-22), "-129.00000y")
  expect_equal(f(-1.29e-21), "-1.2900000z")

})

test_that("fmt_new(\"s\") formats numbers larger than 1e24 with yotta", {
  f <- fmt_new(".8s")
  expect_equal(f(1.23e+21), "1.2300000Z")
  expect_equal(f(1.23e+22), "12.300000Z")
  expect_equal(f(1.23e+23), "123.00000Z")
  expect_equal(f(1.23e+24), "1.2300000Y")
  expect_equal(f(1.23e+25), "12.300000Y")
  expect_equal(f(1.23e+26), "123.00000Y")
  expect_equal(f(1.23e+27), "1230.0000Y")
  expect_equal(f(1.23e+28), "12300.000Y")
  expect_equal(f(1.23e+29), "123000.00Y")
  expect_equal(f(1.23e+30), "1230000.0Y")
  expect_equal(f(-1.23e+21), "-1.2300000Z")
  expect_equal(f(-1.23e+22), "-12.300000Z")
  expect_equal(f(-1.23e+23), "-123.00000Z")
  expect_equal(f(-1.23e+24), "-1.2300000Y")
  expect_equal(f(-1.23e+25), "-12.300000Y")
  expect_equal(f(-1.23e+26), "-123.00000Y")
  expect_equal(f(-1.23e+27), "-1230.0000Y")
  expect_equal(f(-1.23e+28), "-12300.000Y")
  expect_equal(f(-1.23e+29), "-123000.00Y")
  expect_equal(f(-1.23e+30), "-1230000.0Y")
})

test_that("fmt_new(\"$s\") outputs SI-prefix notation with a currency symbol", {
  f <- fmt_new("$.2s")
  expect_equal(f(0), "$0.0")
  expect_equal(f(2.5e5), "$250k")
  expect_equal(f(-2.5e8), "-$250M")
  expect_equal(f(2.5e11), "$250G")
  f <- fmt_new("$.3s")
  expect_equal(f(0), "$0.00")
  expect_equal(f(1), "$1.00")
  expect_equal(f(10), "$10.0")
  expect_equal(f(100), "$100")
  expect_equal(f(999.5), "$1.00k")
  expect_equal(f(999500), "$1.00M")
  expect_equal(f(1000), "$1.00k")
  expect_equal(f(1500.5), "$1.50k")
  expect_equal(f(145500000), "$146M")
  expect_equal(f(145999999.999999347), "$146M")
  expect_equal(f(1e26), "$100Y")
  expect_equal(f(.000001), "$1.00\u03BC")
  expect_equal(f(.009995), "$10.0m")
  f <- fmt_new("$.4s")
  expect_equal(f(999.5), "$999.5")
  expect_equal(f(999500), "$999.5k")
  expect_equal(f(.009995), "$9.995m")
})

test_that(paste("fmt_new(\"s\") SI-prefix notation precision is",
                "consistent for small and large numbers"), {
  f <- fmt_new(".0s")
  expect_equal(f(1e-5), "10\u03BC")
  expect_equal(f(1e-4), "100\u03BC")
  expect_equal(f(1e-3), "1m")
  expect_equal(f(1e-2), "10m")
  expect_equal(f(1e-1), "100m")
  expect_equal(f(1e+0), "1")
  expect_equal(f(1e+1), "10")
  expect_equal(f(1e+2), "100")
  expect_equal(f(1e+3), "1k")
  expect_equal(f(1e+4), "10k")
  expect_equal(f(1e+5), "100k")
  f <- fmt_new(".4s")
  expect_equal(f(1e-5), "10.00\u03BC")
  expect_equal(f(1e-4), "100.0\u03BC")
  expect_equal(f(1e-3), "1.000m")
  expect_equal(f(1e-2), "10.00m")
  expect_equal(f(1e-1), "100.0m")
  expect_equal(f(1e+0), "1.000")
  expect_equal(f(1e+1), "10.00")
  expect_equal(f(1e+2), "100.0")
  expect_equal(f(1e+3), "1.000k")
  expect_equal(f(1e+4), "10.00k")
  expect_equal(f(1e+5), "100.0k")
})

test_that("fmt_new(\"0[width],s\") will group thousands due to zero fill", {
  f <- fmt_new("020,s")
  expect_equal(f(42),    "000,000,000,042.0000")
  expect_equal(f(42e12), "00,000,000,042.0000T")
})

test_that("fmt_new(\",s\") will group thousands for very large numbers", {
  f <- fmt_new(",s")
  expect_equal(f(42e30), "42,000,000Y")
})
jrnold/fivemat documentation built on May 20, 2019, 1 a.m.