Debian Codegraph @

tests/testthat/test-format_scientific.R

raw
1 split_decimal_bw <- function(x, ...) {
2 ret <- pillar_shaft(x, ...)
3 # Hack: Pretend decimal format requires 100 characters
4 ret$dec <- set_width(ret$dec, 100)
5 ret <- set_width(ret, 100)
6 ret <- set_min_width(ret, min(get_min_widths(ret)))
7 format(ret, width = get_min_width(ret))
8 }
9
10 test_that("negative values displayed correct", {
11 f <- split_decimal_bw(-0.123)
12 expect_equal(unname(format(f)), "-1.23e-1")
13 })
14
15 test_that("exponents correct in presence of NA", {
16 expect_snapshot({
17 format(split_decimal_bw(c(NA, 1e-5)), justify = "right")
18 })
19 })
20
21 test_that("corner cases", {
22 expect_equal(split_decimal_bw(numeric()), character())
23 })
24
25 test_that("output test", {
26 expect_snapshot({
27 pillar(add_special(10^c(-9, -6, 3, 9)), width = 10)
28 pillar(add_special((10^c(3, 9, 15, 22)) * c(-1, 1)), width = 10)
29 pillar(add_special(1.25 * 10^(-309:-319)), width = 10)
30 })
31 })
32
33 expect_scientific_width <- function(x) {
34 get_formatted_width <- function(x) {
35 get_max_extent(assemble_decimal(x))
36 }
37
38 expect_equal(
39 get_formatted_width(split_decimal(!!x, 3)),
40 get_width(split_decimal(x, 3))
41 )
42 }
43
44 test_that("width computation", {
45 expect_scientific_width(c(1, 10, 100))
46 expect_scientific_width(c(0, NA))
47 expect_scientific_width(c(1, NaN))
48 expect_scientific_width(c(-12, 3))
49 expect_scientific_width(c(-1, 23))
50 expect_scientific_width(c(1.01, 10.1))
51 expect_scientific_width(c(1.01, -10.1))
52 expect_scientific_width(c(NA_integer_, NA_integer_))
53 expect_scientific_width(c(1e5, 1e15, 1e150))
54 expect_scientific_width(c(1e5, -1e15, 1e150))
55 expect_scientific_width(c(1e5, 1e15, -1e150))
56 expect_scientific_width(c(1e5, NaN))
57 expect_scientific_width(c(1e5, 1.23e15, 1.23e150))
58 expect_scientific_width(c(1e5, 1.234e15, 1.234e150))
59 expect_scientific_width(c(1.2, -Inf))
60 expect_scientific_width(c(1, Inf))
61 expect_scientific_width(c(1e-5, 1e15))
62 })