| 1 |
test_that("output test", { |
| 2 |
skip_if_not_installed("tibble") |
| 3 |
|
| 4 |
expect_snapshot({ |
| 5 |
tibble::tibble( |
| 6 |
x0 = num(9:11 * 100 + 0.5, sigfig = 3), |
| 7 |
x1 = num(9:11 * 100 + 0.5, sigfig = 4), |
| 8 |
x2 = num(9:11 * 100 + 0.5, sigfig = 5), |
| 9 |
) |
| 10 |
|
| 11 |
# Maximum digits after the decimal points |
| 12 |
tibble::tibble( |
| 13 |
x3 = num(9:11 * 100 + 0.5, digits = 0), |
| 14 |
x4 = num(9:11 * 100 + 0.5, digits = -1), |
| 15 |
x5 = num(9:11 * 100 + 0.5, digits = -2), |
| 16 |
) |
| 17 |
|
| 18 |
# Exact digits after the decimal point, with label |
| 19 |
tibble::tibble( |
| 20 |
usd = num(9:11 * 100 + 0.5, digits = 2, label = "USD"), |
| 21 |
gbp = num(9:11 * 100 + 0.5, digits = 2, label = "£"), |
| 22 |
chf = num(9:11 * 100 + 0.5, digits = 2, label = "SFr") |
| 23 |
) |
| 24 |
|
| 25 |
# Scale |
| 26 |
tibble::tibble( |
| 27 |
small = num(9:11 / 1000 + 0.00005, label = "%", scale = 100), |
| 28 |
medium = num(9:11 / 100 + 0.0005, label = "%", scale = 100), |
| 29 |
large = num(9:11 / 10 + 0.005, label = "%", scale = 100) |
| 30 |
) |
| 31 |
|
| 32 |
# Notation |
| 33 |
tibble::tibble( |
| 34 |
sci = num(10^(-13:6), notation = "sci"), |
| 35 |
eng = num(10^(-13:6), notation = "eng"), |
| 36 |
dec = num(10^(-13:6), notation = "dec"), |
| 37 |
si = num(10^(-13:6), notation = "si"), |
| 38 |
) |
| 39 |
|
| 40 |
# Fixed exponent notation |
| 41 |
tibble::tibble( |
| 42 |
scimin = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -Inf), |
| 43 |
engmin = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = -Inf), |
| 44 |
simin = num(10^(-7:6) * 123, notation = "si", fixed_exponent = -Inf) |
| 45 |
) |
| 46 |
|
| 47 |
tibble::tibble( |
| 48 |
scismall = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = -3), |
| 49 |
engsmall = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = -3), |
| 50 |
sismall = num(10^(-7:6) * 123, notation = "si", fixed_exponent = -3) |
| 51 |
) |
| 52 |
|
| 53 |
tibble::tibble( |
| 54 |
scilarge = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = 3), |
| 55 |
englarge = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = 3), |
| 56 |
silarge = num(10^(-7:6) * 123, notation = "si", fixed_exponent = 3) |
| 57 |
) |
| 58 |
|
| 59 |
tibble::tibble( |
| 60 |
scimax = num(10^(-7:6) * 123, notation = "sci", fixed_exponent = Inf), |
| 61 |
engmax = num(10^(-7:6) * 123, notation = "eng", fixed_exponent = Inf), |
| 62 |
simax = num(10^(-7:6) * 123, notation = "si", fixed_exponent = Inf) |
| 63 |
) |
| 64 |
|
| 65 |
# Extra significant figures |
| 66 |
tibble::tibble( |
| 67 |
default = num(100 + 1:3 * 0.001), |
| 68 |
extra1 = num(100 + 1:3 * 0.001, extra_sigfig = TRUE), |
| 69 |
extra2 = num(100 + 1:3 * 0.0001, extra_sigfig = TRUE), |
| 70 |
extra3 = num(10000 + 1:3 * 0.00001, extra_sigfig = TRUE) |
| 71 |
) |
| 72 |
}) |
| 73 |
}) |
| 74 |
|
| 75 |
test_that("many digits", { |
| 76 |
expect_snapshot({ |
| 77 |
num(123456789 * 10^(-9:0)) |
| 78 |
num(123456789 * 10^(-9:1)) |
| 79 |
num(123456789 * 10^(-9:1), notation = "dec") |
| 80 |
num(123456789 * 10^(-9:1), notation = "sci") |
| 81 |
num(123456789 * 10^(-9:1), notation = "eng") |
| 82 |
num(123456789 * 10^(-9:1), notation = "si") |
| 83 |
num(123456789 * 10^(-9:1), notation = "sci", fixed_exponent = -Inf) |
| 84 |
num(123456789 * 10^(-9:1), notation = "eng", fixed_exponent = -Inf) |
| 85 |
num(123456789 * 10^(-9:1), notation = "si", fixed_exponent = -Inf) |
| 86 |
num(123456789 * 10^(-9:1), notation = "sci", fixed_exponent = -3) |
| 87 |
num(123456789 * 10^(-9:1), notation = "sci", fixed_exponent = 3) |
| 88 |
num(123456789 * 10^(-9:1), notation = "sci", fixed_exponent = Inf) |
| 89 |
}) |
| 90 |
}) |
| 91 |
|
| 92 |
test_that("sigfig and digits", { |
| 93 |
expect_snapshot({ |
| 94 |
num(c(578890.23, 240234.131, 40234.1)) |
| 95 |
num(c(578890.23, 240234.131, 40234.1), sigfig = 6) |
| 96 |
num(c(578890.23, 240234.131, 40234.1), sigfig = 7) |
| 97 |
num(c(578890.23, 240234.131, 40234.1), sigfig = 8) |
| 98 |
num(c(578890.23, 240234.131, 40234.1), sigfig = 9) |
| 99 |
num(c(578890.23, 240234.131, 40234.1), digits = 2) |
| 100 |
num(c(578890.23, 240234.131, 40234.1), digits = 3) |
| 101 |
num(c(578890.23, 240234.131, 40234.1), digits = 4) |
| 102 |
num(c(578890.23, 240234.131, 40234.1), digits = -2) |
| 103 |
num(c(578890.23, 240234.131, 40234.1), digits = -3) |
| 104 |
num(c(578890.23, 240234.131, 40234.1), digits = -4) |
| 105 |
}) |
| 106 |
}) |
| 107 |
|
| 108 |
test_that("forced digits", { |
| 109 |
expect_snapshot({ |
| 110 |
pillar(num(1:3, digits = 2)) |
| 111 |
pillar(num(1:3, digits = 5)) |
| 112 |
}) |
| 113 |
}) |
| 114 |
|
| 115 |
test_that("all NA", { |
| 116 |
expect_snapshot({ |
| 117 |
pillar(num(NA_real_, digits = 2)) |
| 118 |
pillar(num(NA_real_, notation = "si")) |
| 119 |
pillar(num(NA_real_, notation = "sci")) |
| 120 |
pillar(num(NA_real_, notation = "eng")) |
| 121 |
pillar(num(NA_real_, notation = "sci", fixed_exponent = -1)) |
| 122 |
pillar(num(NA_real_, notation = "sci", fixed_exponent = -Inf)) |
| 123 |
}) |
| 124 |
}) |
| 125 |
|
| 126 |
test_that("some NA", { |
| 127 |
expect_snapshot({ |
| 128 |
pillar(num(c(NA_real_, 1000), digits = 2)) |
| 129 |
pillar(num(c(NA_real_, 1000), notation = "si")) |
| 130 |
pillar(num(c(NA_real_, 1000), notation = "sci")) |
| 131 |
pillar(num(c(NA_real_, 1000), notation = "eng")) |
| 132 |
pillar(num(c(NA_real_, 1000), notation = "sci", fixed_exponent = -1)) |
| 133 |
pillar(num(c(NA_real_, 1000), notation = "sci", fixed_exponent = -Inf)) |
| 134 |
}) |
| 135 |
}) |
| 136 |
|
| 137 |
test_that("coercion", { |
| 138 |
expect_identical( |
| 139 |
vec_c(num(1), num(2)), |
| 140 |
num(as.numeric(1:2)) |
| 141 |
) |
| 142 |
expect_identical( |
| 143 |
vec_c(num(1, sigfig = 4), num(2, sigfig = 2, notation = "si")), |
| 144 |
num(as.numeric(1:2), sigfig = 4) |
| 145 |
) |
| 146 |
expect_identical( |
| 147 |
vec_c(num(1, label = "a"), 2), |
| 148 |
num(as.numeric(1:2), label = "a") |
| 149 |
) |
| 150 |
expect_identical( |
| 151 |
vec_c(1, num(2, label = "a")), |
| 152 |
num(as.numeric(1:2), label = "a") |
| 153 |
) |
| 154 |
expect_error( |
| 155 |
vec_c(num(1, label = "a"), num(2, label = "b")) |
| 156 |
) |
| 157 |
}) |
| 158 |
|
| 159 |
test_that("arithmetics", { |
| 160 |
expect_snapshot({ |
| 161 |
num(1) + 2 |
| 162 |
1 + num(2) |
| 163 |
1L + num(2) |
| 164 |
num(3.23456, sigfig = 4) - num(2) |
| 165 |
num(3, digits = 2) * num(4, sigfig = 2) |
| 166 |
-num(2) |
| 167 |
}) |
| 168 |
}) |
| 169 |
|
| 170 |
test_that("mathematics", { |
| 171 |
expect_snapshot({ |
| 172 |
min(num(1:3, label = "$")) |
| 173 |
mean(num(1:3, notation = "eng")) |
| 174 |
sin(num(1:3, label = "%", scale = 100)) |
| 175 |
sum(num(c(1:3, NA)), na.rm = TRUE) |
| 176 |
}) |
| 177 |
}) |
| 178 |
|
| 179 |
test_that("formatting", { |
| 180 |
expect_snapshot({ |
| 181 |
format(num(-1:3)) |
| 182 |
format(num(-1:3), trim = TRUE) |
| 183 |
}) |
| 184 |
}) |
| 185 |
|
| 186 |
test_that("attribute", { |
| 187 |
expect_snapshot({ |
| 188 |
set_num_opts(1, sigfig = 2, fixed_exponent = -Inf) |
| 189 |
set_num_opts(1000, digits = 2, notation = "eng") |
| 190 |
}) |
| 191 |
}) |