| 1 |
test_that("we can compute summary functions on units", { |
| 2 |
x <- 1:4 |
| 3 |
ux <- x * as_units("m") |
| 4 |
|
| 5 |
expect_equal(as.numeric(sum(ux)), sum(x)) |
| 6 |
expect_equal(as.numeric(min(ux)), min(x)) |
| 7 |
expect_equal(as.numeric(max(ux)), max(x)) |
| 8 |
expect_equal(as.numeric(range(ux)), range(x)) |
| 9 |
|
| 10 |
expect_equal(units(sum(ux)), units(ux)) |
| 11 |
expect_equal(units(min(ux)), units(ux)) |
| 12 |
expect_equal(units(max(ux)), units(ux)) |
| 13 |
expect_equal(units(range(ux)), units(ux)) |
| 14 |
|
| 15 |
expect_error(all(ux)) |
| 16 |
expect_error(sum(ux, x)) |
| 17 |
|
| 18 |
y <- 1:4 |
| 19 |
uy <- y * as_units("km") |
| 20 |
expect_equal(as.numeric(sum(ux, uy)), sum(c(x, 1000*y))) |
| 21 |
expect_equal(as.numeric(min(ux, uy)), min(c(x, 1000*y))) |
| 22 |
expect_equal(as.numeric(max(ux, uy)), max(c(x, 1000*y))) |
| 23 |
expect_equal(as.numeric(range(ux, uy)), range(c(x, 1000*y))) |
| 24 |
z <- 1:4 * as_units("s") |
| 25 |
expect_error(sum(ux, z)) |
| 26 |
}) |
| 27 |
|
| 28 |
test_that("we can compute means and medians and quantiles", { |
| 29 |
x <- 1:4 |
| 30 |
ux <- set_units(x, "lb/ton") |
| 31 |
w <- x / 5:8 |
| 32 |
uw <- w * as_units("g") |
| 33 |
|
| 34 |
expect_equal(as.numeric(mean(ux)), mean(x)) |
| 35 |
expect_equal(as.numeric(median(ux)), median(x)) |
| 36 |
expect_equal(as.numeric(quantile(ux)), quantile(x), ignore_attr = TRUE) |
| 37 |
expect_equal(as.numeric(weighted.mean(ux, w)), weighted.mean(x, w)) |
| 38 |
expect_equal(as.numeric(weighted.mean(ux, uw)), weighted.mean(x, w)) |
| 39 |
}) |
| 40 |
|
| 41 |
test_that("we can format units", { |
| 42 |
x <- 1:4 |
| 43 |
ux <- x * as_units("m") |
| 44 |
|
| 45 |
expect_equal(format(ux), c("1 [m]", "2 [m]", "3 [m]", "4 [m]")) |
| 46 |
|
| 47 |
# print returns the same object |
| 48 |
expect_output(ux1 <- print(ux)) |
| 49 |
expect_equal(ux, ux1) |
| 50 |
}) |
| 51 |
|
| 52 |
test_that("summary works with NA values", { |
| 53 |
x <- c(1, NA) * as_units("g") |
| 54 |
expect_equal(summary(x), summary(as.numeric(x))) |
| 55 |
}) |