| 1 |
test_that("we can compare vectors with equal units", { |
| 2 |
x <- 1:4 * as_units("m") |
| 3 |
y <- 1:4 * as_units("m") |
| 4 |
z <- 2 * y |
| 5 |
|
| 6 |
expect_true(all(x == y)) |
| 7 |
expect_true(all(x <= y)) |
| 8 |
expect_true(all(x >= y)) |
| 9 |
|
| 10 |
expect_true(all(x < z)) |
| 11 |
expect_true(all(x <= z)) |
| 12 |
expect_true(all(z > x)) |
| 13 |
expect_true(all(z >= x)) |
| 14 |
|
| 15 |
expect_false(any(x > y)) |
| 16 |
expect_false(any(x != y)) |
| 17 |
expect_true(all(x != z)) |
| 18 |
|
| 19 |
expect_error(x == set_units(1, "kg")) |
| 20 |
}) |
| 21 |
|
| 22 |
test_that("comparing special values gives correct results", { |
| 23 |
x <- c(-Inf, NaN, NA, Inf) * as_units("m") |
| 24 |
expect_equal(x == x, drop_units(x) == drop_units(x)) |
| 25 |
expect_equal(x > x, drop_units(x) > drop_units(x)) |
| 26 |
expect_equal(x < x, drop_units(x) < drop_units(x)) |
| 27 |
expect_equal(x == rev(x), drop_units(x) == rev(drop_units(x))) |
| 28 |
expect_equal(x > rev(x), drop_units(x) > rev(drop_units(x))) |
| 29 |
expect_equal(x < rev(x), drop_units(x) < rev(drop_units(x))) |
| 30 |
}) |
| 31 |
|
| 32 |
test_that("vectors are correctly recycled in comparisons", { |
| 33 |
x <- 0:3 * as_units("m") |
| 34 |
y <- 0:1 * as_units("m") |
| 35 |
res <- drop_units(x) == drop_units(y) |
| 36 |
expect_equal(x == y, res) |
| 37 |
expect_equal(y == x, res) |
| 38 |
|
| 39 |
y <- 0:2 * as_units("m") |
| 40 |
expect_warning(res <- drop_units(x) == drop_units(y)) |
| 41 |
expect_warning(expect_equal(x == y, res)) |
| 42 |
expect_warning(expect_equal(y == x, res)) |
| 43 |
}) |
| 44 |
|
| 45 |
test_that("aliases are correctly handled in comparisons (#339)", { |
| 46 |
expect_true(as_units("foot") == as_units("feet")) |
| 47 |
expect_true(as_units("foot") == as_units("ft")) |
| 48 |
}) |
| 49 |
|
| 50 |
test_that("we can scale units with scalars", { |
| 51 |
x <- 1:4 |
| 52 |
ux <- x * as_units("m") |
| 53 |
|
| 54 |
expect_equal(as.numeric(10 * ux), 10 * x) |
| 55 |
expect_equal(as.numeric(ux / 10), x / 10) |
| 56 |
}) |
| 57 |
|
| 58 |
test_that("we can multiply and divide units", { |
| 59 |
x <- 1:4 ; y <- 5:8 |
| 60 |
m <- x * as_units("m") |
| 61 |
s <- y * as_units("s") |
| 62 |
|
| 63 |
expect_equal(as.numeric(m * s), x * y) |
| 64 |
expect_equal(as.numeric(m / s), x / y) |
| 65 |
|
| 66 |
# FIXME: There ought to be a test that the expressions get the right units |
| 67 |
# but I am not entirely sure how that should be wrapped. Just checking string |
| 68 |
# equality would give problems if units are equivalent but needs to be converted |
| 69 |
# first... |
| 70 |
}) |
| 71 |
|
| 72 |
test_that("we can take powers of units", { |
| 73 |
x <- 1:4 |
| 74 |
ux <- x * as_units("m") |
| 75 |
|
| 76 |
expect_equal(as.numeric(ux ** 2), x ** 2) |
| 77 |
expect_equal(as.numeric(ux ^ 2), x ^ 2) |
| 78 |
expect_equal(as.character(units(ux ** 2)), "m^2") |
| 79 |
expect_equal(as.character(units(ux ^ 2)), "m^2") |
| 80 |
|
| 81 |
expect_error(ux ^ 1.3) |
| 82 |
expect_error(ux ^ 0.3) |
| 83 |
expect_error(ux ^ ux) |
| 84 |
# expect_error(ux ^ x) --> see below: gives mixed_units |
| 85 |
expect_silent(ux ^ x) |
| 86 |
|
| 87 |
expect_equal(as.numeric(ux ** -2), x ** -2) |
| 88 |
expect_equal(as.numeric(ux ^ -2), x ^ -2) |
| 89 |
expect_equal(as.character(units(ux ** -2)), "1/m^2") |
| 90 |
expect_equal(as.character(units(ux ^ -2)), "1/m^2") |
| 91 |
|
| 92 |
expect_equal(as.numeric(ux ** 0), x ** 0) |
| 93 |
expect_equal(as.numeric(ux ^ 0), x ^ 0) |
| 94 |
expect_identical(units(ux ** 0), units(as_units(1))) |
| 95 |
expect_identical(units(ux ^ 0), units(as_units(1))) |
| 96 |
}) |
| 97 |
|
| 98 |
test_that("we support unary +/-", { |
| 99 |
expect_equal(-set_units(10, m), set_units(-10, m)) |
| 100 |
expect_equal(+set_units(10, m), set_units(10, m)) |
| 101 |
}) |
| 102 |
|
| 103 |
test_that("we can convert units and simplify after multiplication", { |
| 104 |
x <- 1:4 |
| 105 |
y <- 1:4 |
| 106 |
z <- 1:4 |
| 107 |
m <- as_units("m") |
| 108 |
s <- as_units("s") |
| 109 |
km <- as_units("km") |
| 110 |
ux <- x * m |
| 111 |
uy <- y * s |
| 112 |
uz <- z * km |
| 113 |
|
| 114 |
expect_equal(as.numeric(ux/ux), x/x) |
| 115 |
expect_equal(as.character(units(ux/ux)), units_options("unitless_symbol")) |
| 116 |
|
| 117 |
expect_equal(as.numeric(ux*uy), x*y) |
| 118 |
expect_equal(as.character(units(ux*uy)), "m*s") |
| 119 |
expect_equal(as.numeric(ux*uz), x*z) |
| 120 |
expect_equal(as.character(units(ux*uz)), "km*m") |
| 121 |
expect_equal(as.numeric(set_units(ux*uz, km * km)), (x/1000)*z) |
| 122 |
expect_equal(as.character(units(set_units(ux*uz, km * km))), "km^2") |
| 123 |
|
| 124 |
expect_equal(as.numeric(ux/ux), x/x) |
| 125 |
expect_equal(as.character(units(ux/ux)), units_options("unitless_symbol")) |
| 126 |
expect_equal(as.numeric(ux/uy), x/y) |
| 127 |
expect_equal(as.character(units(ux/uy)), "m/s") |
| 128 |
expect_equal(as.numeric(ux/uz), x/(1000*z)) |
| 129 |
expect_equal(as.character(units(ux/uz)), units_options("unitless_symbol")) |
| 130 |
expect_equal(as.numeric(ux/uy/uz), x/y/z/1000) |
| 131 |
expect_equal(as.character(units(ux/uy/uz)), "1/s") |
| 132 |
}) |
| 133 |
|
| 134 |
test_that("inverse units are not simplified", { |
| 135 |
x <- 1:4 |
| 136 |
s <- as_units("s") |
| 137 |
Hz <- as_units("Hz") |
| 138 |
ux <- x * s |
| 139 |
uy <- x * Hz |
| 140 |
|
| 141 |
expect_equal(as.character(units(ux/uy)), "s/Hz") |
| 142 |
expect_equal(as.character(units(uy/ux)), "Hz/s") |
| 143 |
}) |
| 144 |
|
| 145 |
test_that("unit one is handled correctly", { |
| 146 |
one <- set_units(1) |
| 147 |
onem <- set_units(1, m) |
| 148 |
|
| 149 |
expect_equal(one * one, one) |
| 150 |
expect_equal(onem * one, onem) |
| 151 |
expect_equal(one * one * one, one) |
| 152 |
expect_equal(onem * one * one, onem) |
| 153 |
expect_equal(one / one, one) |
| 154 |
expect_equal(onem / one, onem) |
| 155 |
expect_equal(one ^ 3, one) |
| 156 |
# expect_equal(one ^ pi, one) |
| 157 |
# expect_equal(one ^ -pi, one) |
| 158 |
}) |
| 159 |
|
| 160 |
test_that("we can compute powers if the result is an integer", { |
| 161 |
expect_equal(set_units(1:10, m^0), set_units(1:10)) |
| 162 |
expect_equal(as.numeric(set_units(1:10, m^2) ^ 0.5), (1:10) ^ .5) |
| 163 |
expect_equal(as.numeric(set_units(1:10, m^2) ^ 3/2), (1:10) ^ 3/2) |
| 164 |
expect_error(as.numeric(set_units(1:10, m^3) ^ 0.5), "powers not divisible") |
| 165 |
expect_error(as.numeric(set_units(1:10, m^3) ^ 1.5)) |
| 166 |
expect_equal(as.numeric(set_units(1:10, m^-3) ^ (-1/3)), (1:10) ^ (-1/3)) |
| 167 |
expect_equal(as.numeric(set_units(1:10, m^-2) ^ (-1/2)), (1:10) ^ (-1/2)) |
| 168 |
expect_equal(as.numeric(set_units(1:10, m^-2) ^ (1/2)), (1:10) ^ (1/2)) |
| 169 |
expect_error(set_units(1:10, m^2) ^ (1/pi), "powers not divisible") |
| 170 |
expect_error(set_units(1:10, m^3) ^ -1.5, "powers not divisible") |
| 171 |
}) |
| 172 |
|
| 173 |
test_that("we can undo logatithms", { |
| 174 |
x <- set_units(1:5, cm^2) |
| 175 |
y <- set_units(1e6*x, dam^2) |
| 176 |
|
| 177 |
expect_equal(exp(10^log10(log(x))), set_units(x, m^2)) |
| 178 |
expect_equal(exp(set_units(10)^log10(log(x))), set_units(x, m^2)) |
| 179 |
expect_equal(exp(10^log10(log(y))), set_units(y, m^2)) |
| 180 |
expect_equal(expm1(3^log(log1p(x), base=3)), set_units(x, m^2)) |
| 181 |
expect_equal(expm1(3^log(log1p(y), base=3)), set_units(y, m^2)) |
| 182 |
expect_error(exp(log10(x)), "wrong base in power operation") |
| 183 |
expect_error(exp(x), "only allowed with logarithmic unit") |
| 184 |
expect_error(exp(set_units(1, 1)), "only allowed with logarithmic unit") |
| 185 |
}) |
| 186 |
|
| 187 |
test_that("%/% and %% work", { |
| 188 |
x <- set_units(1:5, m^2) |
| 189 |
y <- set_units(1.4, foot) |
| 190 |
|
| 191 |
expect_true(all.equal(x, y * (x %/% y) + x %% y)) |
| 192 |
|
| 193 |
z <- set_units(drop_units(x) %% drop_units(set_units(y, m)), m^2) |
| 194 |
expect_equal(x %% y, z) |
| 195 |
expect_equal(x %% set_units(y, m), z) |
| 196 |
expect_equal(x %% drop_units(set_units(y, m)), z) |
| 197 |
expect_equal(x %% set_units(drop_units(set_units(y, m)), 1), z) |
| 198 |
expect_equal(x %% set_units(drop_units(set_units(y, m)), 1), z) |
| 199 |
expect_error(drop_units(x) %% set_units(y, m)) |
| 200 |
|
| 201 |
z <- set_units(drop_units(set_units(x, foot^2)) %% drop_units(y), foot^2) |
| 202 |
expect_equal(set_units(x, foot^2) %% y, z) |
| 203 |
expect_equal(set_units(x, foot^2) %% set_units(y, m), z) |
| 204 |
expect_equal(set_units(x, foot^2) %% drop_units(y), z) |
| 205 |
expect_equal(set_units(x, foot^2) %% set_units(drop_units(y), 1), z) |
| 206 |
expect_error(drop_units(set_units(x, foot^2)) %% y) |
| 207 |
|
| 208 |
expect_true(all(x %% y >= set_units(0, m^2))) |
| 209 |
}) |
| 210 |
|
| 211 |
test_that("%*% work", { |
| 212 |
skip_if_not_installed("base", "4.3.0") |
| 213 |
mat = set_units(matrix(1:5, 1), m) |
| 214 |
expect_equal(mat %*% t(mat), set_units(matrix(55), m^2)) |
| 215 |
expect_equal(t(mat) %*% mat, set_units(t(unclass(mat)) %*% unclass(mat), m^2)) |
| 216 |
}) |
| 217 |
|
| 218 |
test_that("The order of multiplication for basic units is commutative", { |
| 219 |
a <- set_units(1:4, m^-3) |
| 220 |
b <- set_units(1:4, mm) |
| 221 |
|
| 222 |
expect_equal(a * b, b * a) |
| 223 |
expect_true(units(a * b) == units(b * a)) |
| 224 |
|
| 225 |
a <- set_units(1:4, m) |
| 226 |
b <- set_units(1:4, mm) |
| 227 |
expect_equal(a * b, b * a) |
| 228 |
expect_true(units(a * b) == units(b * a)) |
| 229 |
}) |
| 230 |
|
| 231 |
test_that("Division gets the right scaling and units", { |
| 232 |
a <- set_units(1:4, m^-3) |
| 233 |
b <- set_units(1:4, mm) |
| 234 |
|
| 235 |
expect_true(units(a / b) != units(b / a)) |
| 236 |
expect_equal(a / b, 1/(b / a)) |
| 237 |
}) |
| 238 |
|
| 239 |
test_that("we obtain mixed units when taking powers of multiple integers", { |
| 240 |
a = set_units(1:4, m) |
| 241 |
p = 4:1 |
| 242 |
expect_equal(a ^ p, c(set_units(1, m^4), set_units(8, m^3), set_units(9, m^2), set_units(4, m), allow_mixed=TRUE)) |
| 243 |
}) |
| 244 |
|
| 245 |
test_that("identical units can always be divided and return unitless (#310)", { |
| 246 |
x1 <- log(set_units(100, "g")) |
| 247 |
x2 <- log(set_units(4, "g")) |
| 248 |
expect_equal( |
| 249 |
x1/x2, |
| 250 |
set_units(log(100)/log(4), "") |
| 251 |
) |
| 252 |
expect_equal( |
| 253 |
x1 %/% x2, |
| 254 |
set_units(log(100) %/% log(4), "") |
| 255 |
) |
| 256 |
}) |
| 257 |
|
| 258 |
test_that("inverse units can always be multiplied and return unitless (related to #310)", { |
| 259 |
x1 <- log(set_units(100, "g")) |
| 260 |
x2 <- log(set_units(4, "g")) |
| 261 |
expect_equal( |
| 262 |
x1*(1/x2), |
| 263 |
set_units(log(100)/log(4), "") |
| 264 |
) |
| 265 |
}) |
| 266 |
|
| 267 |
test_that("identical unit multiplication and division respect 'simplify' option (#355)", { |
| 268 |
initial_unit_simplify <- units_options("simplify") |
| 269 |
on.exit(units_options(simplify=initial_unit_simplify)) |
| 270 |
|
| 271 |
gg <- structure(list(numerator="g", denominator="g"), class="symbolic_units") |
| 272 |
x_gg <- set_units(1, gg, mode="standard") |
| 273 |
x_ul <- set_units(1, unitless, mode="standard") |
| 274 |
|
| 275 |
units_options(simplify = FALSE) |
| 276 |
expect_equal(set_units(1, "g/g"), x_gg) |
| 277 |
expect_equal(set_units(1, "g") / set_units(1, "g"), x_gg) |
| 278 |
|
| 279 |
units_options(simplify = TRUE) |
| 280 |
expect_equal(set_units(1, "g/g"), x_ul) |
| 281 |
expect_equal(set_units(1, "g") / set_units(1, "g"), x_ul) |
| 282 |
|
| 283 |
units_options(simplify = NA) |
| 284 |
expect_equal(set_units(1, "g/g"), x_gg) |
| 285 |
expect_equal(set_units(1, "g") / set_units(1, "g"), x_ul) |
| 286 |
}) |