| 1 |
test_that("global options are respected", { |
| 2 |
# rt: round trip |
| 3 |
rt <- function(x) as.character(units(as_units(x))) |
| 4 |
|
| 5 |
expect_equal("g", rt("grams")) |
| 6 |
|
| 7 |
#op <- units_options(auto_convert_names_to_symbols = FALSE) |
| 8 |
units_options(auto_convert_names_to_symbols = FALSE) |
| 9 |
expect_equal("grams", rt("grams")) |
| 10 |
|
| 11 |
units_options(auto_convert_names_to_symbols = TRUE) |
| 12 |
expect_equal("g", rt("grams")) |
| 13 |
|
| 14 |
#units_options(op) |
| 15 |
expect_equal("g", rt("grams")) |
| 16 |
|
| 17 |
o = units_options("set_units_mode") |
| 18 |
g = set_units(1, g) |
| 19 |
units_options(set_units_mode = "standard") |
| 20 |
expect_equal(set_units(1, "g"), g) |
| 21 |
units_options(set_units_mode = o) |
| 22 |
|
| 23 |
}) |
| 24 |
|
| 25 |
|
| 26 |
test_that("various forms of unit creation and destruction work", { |
| 27 |
|
| 28 |
#ox <- x <- 1L:4L # why should ints be preserved? |
| 29 |
ox <- x <- as.numeric(1:4) |
| 30 |
|
| 31 |
units(x) <- "m/s" |
| 32 |
expect_s3_class(x,"units") |
| 33 |
|
| 34 |
units(x) <- NULL |
| 35 |
expect_identical(x, ox) |
| 36 |
|
| 37 |
units(x) <- character(0) |
| 38 |
expect_identical(x, ox) |
| 39 |
|
| 40 |
ox <- x <- matrix(ox, ncol = 2) |
| 41 |
|
| 42 |
units(x) <- "m/s" |
| 43 |
expect_s3_class(x,"units") |
| 44 |
|
| 45 |
units(x) <- NULL |
| 46 |
expect_identical(x, ox) |
| 47 |
|
| 48 |
units(x) <- character(0) |
| 49 |
expect_identical(x, ox) |
| 50 |
|
| 51 |
ox <- y <- x <- as.numeric(1:4) |
| 52 |
units(x) <- "m/s" |
| 53 |
|
| 54 |
expect_identical(x, set_units(y, m/s)) |
| 55 |
expect_identical(x, set_units(y, "m/s", mode = "standard")) |
| 56 |
|
| 57 |
expect_identical(ox, set_units(x, NULL)) |
| 58 |
expect_identical(ox, set_units(x, NULL, mode = "standard")) |
| 59 |
|
| 60 |
|
| 61 |
ox <- x <- as.numeric(1:4) |
| 62 |
|
| 63 |
units(x) <- "m/s" |
| 64 |
expect_s3_class(x, "units") |
| 65 |
|
| 66 |
x <- drop_units(x) |
| 67 |
expect_identical(x, ox) |
| 68 |
expect_error(drop_units(x)) |
| 69 |
|
| 70 |
|
| 71 |
ox <- x <- data.frame(x=ox, y=ox) |
| 72 |
|
| 73 |
units(x[[1]]) <- "m/s" |
| 74 |
expect_s3_class(x[[1]], "units") |
| 75 |
|
| 76 |
x <- drop_units(x) |
| 77 |
expect_identical(x, ox) |
| 78 |
|
| 79 |
|
| 80 |
meter <- units:::symbolic_unit("m") |
| 81 |
|
| 82 |
expect_identical(meter, make_units(m)) |
| 83 |
expect_identical(meter, as_units("m")) |
| 84 |
expect_identical(meter, as_units(quote(m))) |
| 85 |
expect_identical(meter, as_units(expression(m))) |
| 86 |
|
| 87 |
meter_per_sec <- meter/units:::symbolic_unit("s") |
| 88 |
|
| 89 |
expect_identical(meter_per_sec, make_units(m/s)) |
| 90 |
expect_identical(meter_per_sec, as_units("m/s")) |
| 91 |
expect_identical(meter_per_sec, as_units(" m / s ")) |
| 92 |
expect_identical(meter_per_sec, as_units(quote(m/s))) |
| 93 |
expect_identical(meter_per_sec, as_units(expression(m/s))) |
| 94 |
expect_identical(meter_per_sec, as_units("m s-1")) |
| 95 |
|
| 96 |
}) |
| 97 |
|
| 98 |
test_that("unitless objects can be created", { |
| 99 |
unit <- 1.0 |
| 100 |
units(unit) <- units:::unitless |
| 101 |
|
| 102 |
xul <- x <- as.numeric(2:5) |
| 103 |
units(xul) <- units:::unitless |
| 104 |
|
| 105 |
|
| 106 |
expect_identical(unit, make_units()) |
| 107 |
expect_identical(unit, make_units(1)) |
| 108 |
|
| 109 |
expect_identical(unit, as_units(1)) |
| 110 |
expect_identical(unit, as_units("1")) |
| 111 |
|
| 112 |
expect_identical(xul, set_units(x, 1)) |
| 113 |
expect_identical(xul, set_units(x)) |
| 114 |
expect_identical(xul, set_units(x, "1")) |
| 115 |
expect_identical(xul, set_units(x, "1", mode = "standard")) |
| 116 |
expect_identical(xul, set_units(x, mode = "standard")) |
| 117 |
}) |
| 118 |
|
| 119 |
test_that("set_units default enforces NSE", { |
| 120 |
expect_error(set_units(1:3, as_units("m"))) |
| 121 |
expect_error(set_units(1:3, as_units("m/s"))) |
| 122 |
expect_error(set_units(1:3, make_units(m))) |
| 123 |
expect_error(set_units(1:3, make_units(m/s))) |
| 124 |
|
| 125 |
# is it bad if this works? |
| 126 |
# expect_error(set_units(1:3, "m/s")) |
| 127 |
}) |
| 128 |
|
| 129 |
expect_symbolic <- function(u, n, d) |
| 130 |
expect_equal(units(as_units(u)), units:::.symbolic_units(n, d)) |
| 131 |
expect_symbolic_nocheck <- function(u, n, d) |
| 132 |
expect_equal(units(as_units(u, check_is_valid=FALSE)), units:::.symbolic_units(n, d)) |
| 133 |
|
| 134 |
test_that("exotic units work", { |
| 135 |
# check what udunits support |
| 136 |
# units:::R_ut_format(units:::R_ut_parse(some_string)) |
| 137 |
|
| 138 |
expect_symbolic("2.2 m s", c("2.2", "m", "s"), character(0)) |
| 139 |
expect_symbolic("2.2*m*s", c("2.2", "m", "s"), character(0)) |
| 140 |
#expect_symbolic("2.2.m.s", c("2.2", "m", "s"), character(0)) |
| 141 |
|
| 142 |
expect_symbolic("m2/s", c("m", "m"), "s") |
| 143 |
expect_symbolic("m20/s", rep("m", 20), "s") |
| 144 |
expect_symbolic("m^2/s", c("m", "m"), "s") |
| 145 |
expect_symbolic("m 2/s", c("m", "2"), "s") |
| 146 |
expect_symbolic("m-2/s", character(0), c("m", "m", "s")) |
| 147 |
expect_symbolic("m^-2/s", character(0), c("m", "m", "s")) |
| 148 |
expect_symbolic("m/s2", "m", c("s", "s")) |
| 149 |
expect_symbolic("m/s^2", "m", c("s", "s")) |
| 150 |
expect_symbolic("m/s 2", c("m", "2"), "s") |
| 151 |
expect_symbolic("m/s-2", c("m", "s", "s"), character(0)) |
| 152 |
expect_symbolic("m/s^-2", c("m", "s", "s"), character(0)) |
| 153 |
|
| 154 |
expect_symbolic("ml/min/(1.73m^2)", "ml", c("min", "1.73", "m", "m")) |
| 155 |
expect_symbolic("ml/min/1.73/m^2", "ml", c("min", "1.73", "m", "m")) |
| 156 |
expect_symbolic("ml/min/1.73m^2", "ml", c("min", "1.73", "m", "m")) |
| 157 |
expect_symbolic("ml/min/1.73m-2", c("ml", "m", "m"), c("min", "1.73")) |
| 158 |
|
| 159 |
old <- unlist(units_options(strict_tokenizer=TRUE)) |
| 160 |
expect_symbolic("ml/min/1.73m^2", c("ml", "m", "m"), c("min", "1.73")) |
| 161 |
expect_symbolic("ml/min/1.73m-2", "ml", c("min", "1.73", "m", "m")) |
| 162 |
units_options(strict_tokenizer=old) |
| 163 |
|
| 164 |
expect_symbolic_nocheck("inH2O", "inH2O", character(0)) |
| 165 |
expect_symbolic_nocheck("inH2O2", c("inH2O", "inH2O"), character(0)) |
| 166 |
|
| 167 |
expect_symbolic("m/(g/(s/L))", c("m", "s"), c("g", "L")) |
| 168 |
expect_error(expect_warning(as_units("m/(g/s"))) |
| 169 |
expect_error(expect_warning(as_units("m^m"))) |
| 170 |
}) |