1test_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
12test_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
31test_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
56test_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  library(magrittr)
64  y %>% set_units(km) -> z
65  expect_equal(x, z)
66})
67
68test_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
74test_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
86test_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
105test_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
114test_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
120test_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), units:::ud_convert(0, "degC", "degK"))
124  expect_equal(as.character(units(m)), "K")
125
126  temp <- 75 * as_units('degF')
127  units(temp) <- as_units('degK')
128  result <- temp / as_units('degF')
129  expect_equal(as.numeric(result), 75)
130  expect_equal(units(result), unitless)
131})
132
133test_that("dim propagates", {
134  y = x = set_units(matrix(1:4,2), m)
135  units(y) = as_units("mm")
136  expect_equal(dim(x), dim(y))
137})
138
139test_that("conversion of g/kg to dimensionless is not the default", {
140	a_orig <- a <- 1:10
141	units(a) = as_units("mg/kg")
142	expect_equal(as.numeric(a), a_orig)
143})
144
145test_that("conversion to dimensionless with prefix works (g/kg) if simplify=TRUE", {
146	a_orig <- a <- 1:10
147	units_options(simplify = TRUE)
148	units(a) = as_units("mg/kg")
149	expect_equal(as.numeric(a), a_orig/1e6)
150	units(a) = as_units("kg/mg")
151	expect_equal(a, set_units(a_orig, 1))
152	units(a) = as_units("g/g")
153	expect_equal(a, set_units(a_orig, 1))
154	units(a) = as_units("kg/g")
155	expect_equal(a, set_units(a_orig * 1000, 1))
156	units_options(simplify = NA)
157})
158
159test_that("a NULL value returns NULL", {
160  expect_null(as_units(NULL))
161})
162
163#test_that("as.data.frame.units works", {
164#  expect_silent(as.data.frame(set_units(matrix(1:9,3), m)))
165#})
166
167test_that("units.symbolic_units works", {
168  m = set_units(1, m)
169  expect_equal(units(m), units(units(m)))
170})
171
172test_that("new base units work", {
173  install_unit("person")
174  expect_equal(set_units(1, person) + set_units(1, kperson), set_units(1001, person))
175  expect_error(set_units(1, person) + set_units(1, rad), "cannot convert")
176
177  # restore
178  remove_unit("person")
179})
180
181test_that("errors are correctly coerced to a data frame", {
182  a <- 1:10
183  b <- a * as_units("m")
184
185  expect_equal(as.data.frame(b)$b, b)
186  x <- data.frame(a, b)
187  expect_equal(x$a, a)
188  expect_equal(x$b, b)
189  x <- cbind(x, a, data.frame(b))
190  expect_equal(x[[3]], a)
191  expect_equal(x[[4]], b)
192  x <- rbind(x, a[1:4], x[1,])
193  expect_equal(x[[1]], c(a, 1, 1))
194  expect_equal(x[[2]], c(b, c(2, 1) * as_units("m")))
195  expect_equal(x[[3]], c(a, 3, 1))
196  expect_equal(x[[4]], c(b, c(4, 1) * as_units("m")))
197})
198
199test_that("units are correctly coerced to a list", {
200  x <- 1:10 * as_units("m")
201  y <- as.list(x)
202  expect_type(y, "list")
203  expect_true(all(sapply(seq_along(y), function(i) all.equal(y[[i]], x[i]))))
204})
205
206test_that("NA as units generate warnings", {
207  expect_error(set_units(NA_real_, NA_character_, mode="standard"), "a missing value for units is not allowed")
208  expect_error(set_units(NA_real_, NA, mode="standard"), "a missing value for units is not allowed")
209})
210
211test_that("ud_are_convertible return the expected value", {
212  x <- 1:10 * as_units("m")
213  expect_type(ud_are_convertible("m", "km"), "logical")
214  expect_true(ud_are_convertible("m", "km"))
215  expect_true(ud_are_convertible(units(x), "km"))
216  expect_false(ud_are_convertible("s", "kg"))
217
218})
219