| 1 |
split_decimal_3 <- function(x, sigfig = 3, ...) { |
| 2 |
split_decimal(x, sigfig = sigfig, ...) |
| 3 |
} |
| 4 |
|
| 5 |
test_that("compute_rhs_digits() works", { |
| 6 |
x <- c(NA, NaN, Inf, 0, 1, 100, 1e10, 0.001, 1e-14) |
| 7 |
expect_equal( |
| 8 |
compute_rhs_digits(x, 3), |
| 9 |
c(0, 0, 0, 0, 0, 0, 0, 3, 14) |
| 10 |
) |
| 11 |
expect_equal( |
| 12 |
compute_rhs_digits(x + 1e-8, 3), |
| 13 |
c(0, 0, 0, 8, 2, 0, 0, 5, 10) |
| 14 |
) |
| 15 |
expect_equal( |
| 16 |
compute_rhs_digits(x, 7), |
| 17 |
c(0, 0, 0, 0, 0, 0, 0, 3, 14) |
| 18 |
) |
| 19 |
expect_equal( |
| 20 |
compute_rhs_digits(x, 7 + 1e-8), |
| 21 |
c(0, 0, 0, 0, 6, 4, 0, 9, 20) |
| 22 |
) |
| 23 |
}) |
| 24 |
|
| 25 |
test_that("compute_exp_display() returns NA if not relevant", { |
| 26 |
x <- c(NA, NaN, Inf, 0, 1, 100, 0.001) |
| 27 |
expect_equal(compute_exp_display(x, 6), c(NA, NA, NA, NA, 0, 2, -3)) |
| 28 |
}) |
| 29 |
|
| 30 |
test_that("compute_exp() returns NA if not relevant", { |
| 31 |
x <- c(NA, NaN, Inf, 0, 1, 100, 0.001) |
| 32 |
expect_equal(compute_exp(x, 6), c(NA, NA, NA, NA, 0, 2, -3)) |
| 33 |
}) |
| 34 |
|
| 35 |
test_that("compute_exp_display() respects significant digits (#174)", { |
| 36 |
x <- c(0.9, 0.99, 0.999, 0.99949, 0.9995, 0.99951, 0.9999, 0.99999, 0.999999) |
| 37 |
expect_equal(compute_exp_display(x, 3), c(-1, -1, -1, -1, 0, 0, 0, 0, 0)) |
| 38 |
}) |
| 39 |
|
| 40 |
test_that("compute_exp() respects significant digits for rhs computation (#1648)", { |
| 41 |
x <- c(0.9, 0.99, 0.999, 0.99949, 0.9995, 0.99951, 0.9999, 0.99999, 0.999999) |
| 42 |
expect_equal(compute_exp(x, 3), c(-1, -1, -1, -1, -1, -1, -1, -1, -1)) |
| 43 |
}) |
| 44 |
|
| 45 |
test_that("scientific notation handles rounding edge cases correctly", { |
| 46 |
# 9.995 with 4 sigfig -> 9.99, should be displayed as 9.999e0, not 10.0e0 |
| 47 |
f <- split_decimal(9.995, sigfig = 4, sci_mod = 1) |
| 48 |
expect_equal(f$exp, 0) # Should be 0 for 9.99e0 display |
| 49 |
|
| 50 |
# 9.9995 with 4 sigfig -> 10.0, should be displayed as 1.00e1 |
| 51 |
f2 <- split_decimal(9.9995, sigfig = 4, sci_mod = 1) |
| 52 |
expect_equal(f2$exp, 1) # Should be 1 for 1.00e1 display |
| 53 |
}) |
| 54 |
|
| 55 |
test_that("special values appear in LHS", { |
| 56 |
x <- c(NA, NaN, Inf) |
| 57 |
f <- split_decimal_3(x) |
| 58 |
|
| 59 |
expect_equal(format_lhs(f), format(x, trim = TRUE)) |
| 60 |
}) |
| 61 |
|
| 62 |
test_that("all-positive values get nothing in neg", { |
| 63 |
f <- split_decimal_3(c(0, Inf)) |
| 64 |
expect_equal(format_neg(f), c("", "")) |
| 65 |
}) |
| 66 |
|
| 67 |
test_that("negative values get - in neg", { |
| 68 |
f <- split_decimal_3(c(-Inf, Inf)) |
| 69 |
expect_equal(format_neg(f), c("-", "")) |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("trailing zeros removed if whole decimal fraction", { |
| 73 |
f <- split_decimal_3(c(1.5, 0.5)) |
| 74 |
expect_equal(format_lhs(f), c("1", "0")) |
| 75 |
expect_equal(format_rhs(f), c("5", "5")) |
| 76 |
}) |
| 77 |
|
| 78 |
test_that("trailing zeros pad to sigfigs if data not shown", { |
| 79 |
f <- split_decimal_3(c(1.5, 0.5) + 1e-6) |
| 80 |
expect_equal(format_lhs(f), c("1", "0")) |
| 81 |
expect_equal(format_rhs(f), c("50 ", "500")) |
| 82 |
}) |
| 83 |
|
| 84 |
test_that("sigfigs split between lhs and rhs", { |
| 85 |
x <- c(1.43, 10.43, 100.43) |
| 86 |
f <- split_decimal_3(x) |
| 87 |
|
| 88 |
expect_equal(format_lhs(f), as.character(trunc(x))) |
| 89 |
expect_equal(format_rhs(f), c("43", "4 ", " ")) |
| 90 |
}) |
| 91 |
|
| 92 |
test_that("leading 0 added to rhs", { |
| 93 |
f <- split_decimal_3(1.01) |
| 94 |
|
| 95 |
expect_equal(format_lhs(f), "1") |
| 96 |
expect_equal(format_rhs(f), "01") |
| 97 |
}) |
| 98 |
|
| 99 |
test_that("values rounded up as expect", { |
| 100 |
f <- split_decimal_3(c(18.9, 18.99)) |
| 101 |
|
| 102 |
expect_equal(format_lhs(f), c("18", "19")) |
| 103 |
expect_equal(format_rhs(f), c("9", "0")) |
| 104 |
}) |
| 105 |
|
| 106 |
test_that("values on LHS not rounded", { |
| 107 |
f <- format_lhs(split_decimal(123456, 3)) |
| 108 |
expect_equal(f, "123456") |
| 109 |
}) |
| 110 |
|
| 111 |
test_that("corner cases", { |
| 112 |
expect_equal(format_lhs(split_decimal_3(numeric())), character()) |
| 113 |
}) |
| 114 |
|
| 115 |
test_that("output test", { |
| 116 |
expect_snapshot({ |
| 117 |
pillar(add_special((10^(-3:4)) * c(-1, 1))) |
| 118 |
pillar(add_special(1.23456 * 10^(-3:3))) |
| 119 |
}) |
| 120 |
withr::with_options( |
| 121 |
list(pillar.sigfig = 5), |
| 122 |
expect_snapshot({ |
| 123 |
pillar(add_special((10^(c(-2:4, 0L))) * c(-1, 1))) |
| 124 |
}) |
| 125 |
) |
| 126 |
withr::with_options( |
| 127 |
list(pillar.sigfig = 7), |
| 128 |
expect_snapshot({ |
| 129 |
pillar(add_special((10^(0:-5)) * c(-1, 1)), width = 20) |
| 130 |
}) |
| 131 |
) |
| 132 |
expect_snapshot({ |
| 133 |
pillar(add_special((10^(4:-4)) + 1e-6), width = 20) |
| 134 |
}) |
| 135 |
}) |
| 136 |
|
| 137 |
expect_decimal_width <- function(x) { |
| 138 |
get_formatted_width <- function(x) { |
| 139 |
get_max_extent(assemble_decimal(x)) |
| 140 |
} |
| 141 |
|
| 142 |
expect_equal( |
| 143 |
get_formatted_width(split_decimal(!!x, 3)), |
| 144 |
!!get_width(split_decimal(x, 3)) |
| 145 |
) |
| 146 |
} |
| 147 |
|
| 148 |
test_that("width computation", { |
| 149 |
expect_decimal_width(c(1, 10, 100)) |
| 150 |
expect_decimal_width(c(0, NA)) |
| 151 |
expect_decimal_width(c(1, NaN)) |
| 152 |
expect_decimal_width(c(-12, 3)) |
| 153 |
expect_decimal_width(c(-1, 23)) |
| 154 |
expect_decimal_width(c(1.01, 10.1)) |
| 155 |
expect_decimal_width(c(1.01, -10.1)) |
| 156 |
expect_decimal_width(c(NA_integer_, NA_integer_)) |
| 157 |
expect_decimal_width(c(1.234, 1.2345)) |
| 158 |
expect_decimal_width(c(1.2, -Inf)) |
| 159 |
expect_decimal_width(c(1, Inf)) |
| 160 |
}) |
| 161 |
|
| 162 |
test_that("9.99...95 (tidyverse/tibble#1648)", { |
| 163 |
# Declaring the constants inside expect_snapshot() perturbs the input |
| 164 |
x <- c( |
| 165 |
0x1.3fd70a3d70a3dp+3, |
| 166 |
0x1.3ffff583a53b8p+3, |
| 167 |
0x1.3ffffef39085ep+3, |
| 168 |
0x1.3ffffffff920cp+3, |
| 169 |
0x1.3ffffffffffe3p+3 |
| 170 |
) |
| 171 |
|
| 172 |
expect_snapshot({ |
| 173 |
format(num(x[1], sigfig = 3)) |
| 174 |
format(num(x[2], sigfig = 6)) |
| 175 |
format(num(x[3], sigfig = 7)) |
| 176 |
format(num(x[4], sigfig = 11)) |
| 177 |
format(num(x[5], sigfig = 14, notation = "dec")) |
| 178 |
}) |
| 179 |
}) |
| 180 |
|
| 181 |
test_that("sigfig > 15 fails", { |
| 182 |
expect_snapshot(error = TRUE, { |
| 183 |
format(num(0, sigfig = 16)) |
| 184 |
}) |
| 185 |
}) |