| 1 |
test_that("ud_are_convertible return the expected value", { |
| 2 |
x <- 1:10 * as_units("m") |
| 3 |
expect_type(ud_are_convertible("m", "km"), "logical") |
| 4 |
expect_true(ud_are_convertible("m", "km")) |
| 5 |
expect_true(ud_are_convertible(units(x), "km")) |
| 6 |
expect_false(ud_are_convertible("s", "kg")) |
| 7 |
|
| 8 |
x <- c("m", "l") |
| 9 |
y <- c("km", "ml", "cm", "kg") |
| 10 |
conv <- c(TRUE, TRUE, TRUE, FALSE) |
| 11 |
expect_equal(ud_are_convertible(x, y), conv) |
| 12 |
expect_equal(ud_are_convertible(y, x), conv) |
| 13 |
}) |
| 14 |
|
| 15 |
test_that("ud_convert works with simple conversions", { |
| 16 |
x <- 1:10 * as_units("m") |
| 17 |
expect_equal(ud_convert(1, "m", "km"), 1/1000) |
| 18 |
expect_equal(ud_convert(as.numeric(x), units(x), "km"), as.numeric(x)/1000) |
| 19 |
expect_equal(ud_convert(1, "km", "m"), 1000) |
| 20 |
expect_equal(ud_convert(32, "degF", "degC"), 0) |
| 21 |
expect_equal(ud_convert(0, "degC", "K"), 273.15) |
| 22 |
}) |
| 23 |
|
| 24 |
test_that("ud_convert works with vectors", { |
| 25 |
expect_equal(ud_convert(1:2, c("m", "mm"), "km"), 1:2/c(1e3,1e6)) |
| 26 |
expect_equal(ud_convert(c(32, 212), "degF", "degC"), c(0, 100)) |
| 27 |
expect_equal(ud_convert(numeric(0), "m", "km"), numeric(0)) |
| 28 |
}) |
| 29 |
|
| 30 |
test_that("ud_convert returns Error for incompatible units", { |
| 31 |
expect_error(ud_convert(100, "m", "kg"), "Units not convertible") |
| 32 |
}) |