| 1 |
test_that("we can convert numbers to unit-less units", { |
| 2 |
x <- as_units(1:4) |
| 3 |
expect_equal(length(x), 4) |
| 4 |
expect_equal(class(x), "units") |
| 5 |
expect_equal(as.numeric(x), 1:4) |
| 6 |
|
| 7 |
y <- 1:4 |
| 8 |
units(y) <- unitless |
| 9 |
expect_equal(x, y) |
| 10 |
}) |
| 11 |
|
| 12 |
test_that("we can convert numbers to physical units", { |
| 13 |
m <- as_units("m") |
| 14 |
x <- 1:4 * m |
| 15 |
expect_equal(length(x), 4) |
| 16 |
expect_equal(class(x), "units") |
| 17 |
expect_equal(as.character(units(x)), "m") |
| 18 |
expect_equal(as.numeric(x), 1:4) |
| 19 |
|
| 20 |
y <- 1:4 |
| 21 |
units(y) <- m |
| 22 |
expect_equal(x, y) |
| 23 |
|
| 24 |
z <- 1:4 / m |
| 25 |
expect_equal(length(z), 4) |
| 26 |
expect_equal(class(z), "units") |
| 27 |
expect_equal(as.character(units(z)), "1/m") |
| 28 |
expect_equal(as.numeric(z), 1:4) |
| 29 |
}) |
| 30 |
|
| 31 |
test_that("we can convert NA values to physical units", { |
| 32 |
m <- as_units("m") |
| 33 |
x <- NA * m |
| 34 |
expect_equal(class(x), "units") |
| 35 |
expect_equal(as.character(units(x)), "m") |
| 36 |
expect_equal(as.numeric(x), as.numeric(NA)) |
| 37 |
|
| 38 |
x <- set_units(NA,m/s) |
| 39 |
expect_equal(as.character(units(x)), "m/s") |
| 40 |
expect_equal(x + set_units(5,m/s), set_units(NA,m/s)) |
| 41 |
expect_error(x + set_units(5,m)) |
| 42 |
|
| 43 |
x <- NA |
| 44 |
units(x) <- m |
| 45 |
expect_equal(as.character(units(x)), 'm') |
| 46 |
|
| 47 |
x <- rep(NA,5) |
| 48 |
s <- as_units("s") |
| 49 |
units(x) <- make_units(m/s) |
| 50 |
expect_equal(length(x),5) |
| 51 |
expect_equal(units(x),units(m/s)) |
| 52 |
expect_equal(x,5 * x) |
| 53 |
expect_error(x + 1) |
| 54 |
}) |
| 55 |
|
| 56 |
test_that("we can convert between two units that can be converted", { |
| 57 |
m <- as_units("m") |
| 58 |
km <- as_units("km") |
| 59 |
x <- y <- 1:4 * m |
| 60 |
units(x) <- km |
| 61 |
expect_equal(as.numeric(y), 1000 * as.numeric(x)) |
| 62 |
skip_if_not_installed("magrittr") |
| 63 |
`%>%` <- magrittr::`%>%` |
| 64 |
y %>% set_units(km) -> z |
| 65 |
expect_equal(x, z) |
| 66 |
}) |
| 67 |
|
| 68 |
test_that("we can't convert between two units that can't be converted", { |
| 69 |
m <- as_units("m") |
| 70 |
s <- as_units("s") |
| 71 |
expect_error(units(m) <- s) |
| 72 |
}) |
| 73 |
|
| 74 |
test_that("we can convert difftime objects to units", { |
| 75 |
s <- Sys.time() |
| 76 |
d <- s - (s + 1) |
| 77 |
x <- as_units(d) |
| 78 |
expect_equal(as.numeric(x), as.numeric(d)) |
| 79 |
|
| 80 |
week <- as.difftime(1, units = "weeks") |
| 81 |
units_week <- as_units(week) |
| 82 |
expect_equal(as.character(units(units_week)), "d") |
| 83 |
expect_equal(as.numeric(units_week), 7) |
| 84 |
}) |
| 85 |
|
| 86 |
test_that("we can convert units objects to difftime objects", { |
| 87 |
s <- Sys.time() |
| 88 |
d <- s - (s + 1) |
| 89 |
x <- as_units(d) |
| 90 |
y <- as_difftime(x) |
| 91 |
|
| 92 |
expect_equal(d, y) |
| 93 |
}) |
| 94 |
|
| 95 |
#test_that("we can convert units objects to and from hms objects", { |
| 96 |
# s <- Sys.time() |
| 97 |
# library(hms) |
| 98 |
# d <- as.hms(s - (s + 1)) |
| 99 |
# x <- as_units(d) |
| 100 |
# y <- as.hms(x) |
| 101 |
# |
| 102 |
# expect_equal(d, y) |
| 103 |
#}) |
| 104 |
|
| 105 |
test_that("we can subscript units", { |
| 106 |
x <- 1:4 |
| 107 |
y <- x * as_units("m") |
| 108 |
expect_equal(as.numeric(y[1]), x[1]) |
| 109 |
expect_equal(class(y[1]), class(y)) |
| 110 |
expect_equal(as.numeric(y[[1]]), x[[1]]) |
| 111 |
expect_equal(class(y[[1]]), class(y)) |
| 112 |
}) |
| 113 |
|
| 114 |
test_that("m + m*s is an error", { |
| 115 |
m <- as_units("m") |
| 116 |
s <- as_units("s") |
| 117 |
expect_error(m + m * s) |
| 118 |
}) |
| 119 |
|
| 120 |
test_that("we can convert between units that are not simply a scalar from each other", { |
| 121 |
m <- 0 * as_units("degC") |
| 122 |
units(m) <- as_units("degK") |
| 123 |
expect_equal(as.numeric(m), ud_convert(0, "degC", "degK")) |
| 124 |
expect_equal(as.character(units(m)), "K") |
| 125 |
|
| 126 |
## Does this make any sense?? |
| 127 |
# temp <- 75 * as_units('degF') |
| 128 |
# units(temp) <- as_units('degK') |
| 129 |
# result <- temp / as_units('degF') |
| 130 |
# expect_equal(as.numeric(result), 75) |
| 131 |
# expect_equal(units(result), unitless) |
| 132 |
}) |
| 133 |
|
| 134 |
test_that("dim propagates", { |
| 135 |
y = x = set_units(matrix(1:4,2), m) |
| 136 |
units(y) = as_units("mm") |
| 137 |
expect_equal(dim(x), dim(y)) |
| 138 |
}) |
| 139 |
|
| 140 |
test_that("conversion of g/kg to dimensionless is not the default", { |
| 141 |
a_orig <- a <- 1:10 |
| 142 |
units(a) = as_units("mg/kg") |
| 143 |
expect_equal(as.numeric(a), a_orig) |
| 144 |
}) |
| 145 |
|
| 146 |
test_that("conversion to dimensionless with prefix works (g/kg) if simplify=TRUE", { |
| 147 |
a_orig <- a <- 1:10 |
| 148 |
units_options(simplify = TRUE) |
| 149 |
units(a) = as_units("mg/kg") |
| 150 |
expect_equal(as.numeric(a), a_orig/1e6) |
| 151 |
units(a) = as_units("kg/mg") |
| 152 |
expect_equal(a, set_units(a_orig, 1)) |
| 153 |
units(a) = as_units("g/g") |
| 154 |
expect_equal(a, set_units(a_orig, 1)) |
| 155 |
units(a) = as_units("kg/g") |
| 156 |
expect_equal(a, set_units(a_orig * 1000, 1)) |
| 157 |
units_options(simplify = NA) |
| 158 |
}) |
| 159 |
|
| 160 |
test_that("a NULL value returns NULL", { |
| 161 |
expect_null(as_units(NULL)) |
| 162 |
}) |
| 163 |
|
| 164 |
test_that("as.data.frame.units works", { |
| 165 |
x <- matrix(1:9, 3) |
| 166 |
|
| 167 |
df1 <- as.data.frame(x) |
| 168 |
for (col in names(df1)) |
| 169 |
df1[[col]] <- set_units(df1[[col]], m) |
| 170 |
df2 <- as.data.frame(set_units(x, m)) |
| 171 |
|
| 172 |
expect_equal(df1, df2) |
| 173 |
}) |
| 174 |
|
| 175 |
test_that("units.symbolic_units works", { |
| 176 |
m = set_units(1, m) |
| 177 |
expect_equal(units(m), units(units(m))) |
| 178 |
}) |
| 179 |
|
| 180 |
test_that("new base units work", { |
| 181 |
install_unit("person") |
| 182 |
expect_equal(set_units(1, person) + set_units(1, kperson), set_units(1001, person)) |
| 183 |
expect_error(set_units(1, person) + set_units(1, rad), "cannot convert") |
| 184 |
|
| 185 |
# restore |
| 186 |
remove_unit("person") |
| 187 |
}) |
| 188 |
|
| 189 |
test_that("errors are correctly coerced to a data frame", { |
| 190 |
a <- 1:10 |
| 191 |
b <- a * as_units("m") |
| 192 |
|
| 193 |
expect_equal(as.data.frame(b)$b, b) |
| 194 |
x <- data.frame(a, b) |
| 195 |
expect_equal(x$a, a) |
| 196 |
expect_equal(x$b, b) |
| 197 |
x <- cbind(x, a, data.frame(b)) |
| 198 |
expect_equal(x[[3]], a) |
| 199 |
expect_equal(x[[4]], b) |
| 200 |
x <- rbind(x, a[1:4], x[1,]) |
| 201 |
expect_equal(x[[1]], c(a, 1, 1)) |
| 202 |
expect_equal(x[[2]], c(b, c(2, 1) * as_units("m"))) |
| 203 |
expect_equal(x[[3]], c(a, 3, 1)) |
| 204 |
expect_equal(x[[4]], c(b, c(4, 1) * as_units("m"))) |
| 205 |
}) |
| 206 |
|
| 207 |
test_that("units are correctly coerced to a list", { |
| 208 |
x <- 1:10 * as_units("m") |
| 209 |
y <- as.list(x) |
| 210 |
expect_type(y, "list") |
| 211 |
expect_true(all(sapply(seq_along(y), function(i) all.equal(y[[i]], x[i])))) |
| 212 |
}) |
| 213 |
|
| 214 |
test_that("NA as units generate warnings", { |
| 215 |
expect_error(set_units(NA_real_, NA_character_, mode="standard"), "a missing value for units is not allowed") |
| 216 |
expect_error(set_units(NA_real_, NA, mode="standard"), "a missing value for units is not allowed") |
| 217 |
}) |
| 218 |
|
| 219 |
test_that("set_units keeps names even with unit conversion (#305)", { |
| 220 |
expect_named(set_units(c(a=1), "m"), "a") |
| 221 |
expect_named( |
| 222 |
set_units(set_units(c(a=1), "m"), "km"), |
| 223 |
"a" |
| 224 |
) |
| 225 |
}) |
| 226 |
|
| 227 |
test_that("conversion to mixed_units", { |
| 228 |
a <- set_units(1:3, m/s) |
| 229 |
expect_s3_class(a, "units") |
| 230 |
units(a) <- c("m/s", "km/h", "km/h") |
| 231 |
expect_s3_class(a, "mixed_units") |
| 232 |
expect_equal( |
| 233 |
drop_units(a), |
| 234 |
c(1, 0.002*3600, 0.003*3600) |
| 235 |
) |
| 236 |
}) |