Debian Codegraph @

tests/testthat/test_mixed.R

raw
1 test_that("mixed units work", {
2 m = c(set_units(1:3, km), set_units(4:6, g), allow_mixed = TRUE)
3
4 # print returns the same object
5 expect_output(m1 <- print(m))
6 expect_equal(m, m1)
7
8 # select a subset
9 expect_s3_class(m[3:4], "mixed_units")
10 expect_s3_class(m[3], "mixed_units")
11
12 # select a single units object:
13 expect_s3_class(m[[3]], "units")
14
15 m <- set_units(m, c(rep(c("m", "kg"), each = 3)))
16 expect_s3_class(m, "mixed_units")
17 units(m) = rep(c("mm", "mg"), each = 3)
18 expect_s3_class(m, "mixed_units")
19 # does the value get recycled?
20 expect_s3_class(set_units(m[1:3], "m"), "mixed_units")
21
22 # convert to units:
23 expect_s3_class(as_units( m[1:3] ), "units")
24
25 # round-trip via units:
26 m0 <- mixed_units(as_units(m[1:3]))
27 expect_identical(m[1:3], m0)
28
29 # Ops using by single unit: needs to be explicitly coerced to mixed_units:
30 expect_s3_class(m[1:3] * mixed_units(set_units(1, mm)), "mixed_units")
31 expect_s3_class(m[1:3] / mixed_units(set_units(1, mm)), "mixed_units")
32 expect_s3_class(m[1:3] + mixed_units(set_units(1, mm)), "mixed_units")
33 expect_s3_class(m[1:3] - mixed_units(set_units(1, mm)), "mixed_units")
34 expect_type(m[1:3] == mixed_units(set_units(1, mm)), "logical")
35 expect_type(m[1:3] != mixed_units(set_units(1, mm)), "logical")
36 expect_error(m[1:3] ^ mixed_units(set_units(1, mm)))
37
38 # FIXME: Ops.mixed_units and Ops.units must be the same method
39 # to avoid the warning and the error.
40 # We can discriminate by switchpatching.
41 expect_error(expect_warning(m[1:3] * set_units(1, mm)))
42
43 expect_s3_class(units(m), "mixed_symbolic_units")
44 expect_type(format(m), "character")
45 expect_type(as.character(units(m)), "character")
46 expect_equal(drop_units(m), sapply(m, as.numeric))
47
48 units_options(allow_mixed = TRUE)
49 m = c(set_units(1:3, km), set_units(4:6, g))
50 expect_s3_class(m, "mixed_units")
51 expect_equal(m, mixed_units(1:6, c(rep("km", 3), rep("g", 3))))
52 units_options(allow_mixed = FALSE)
53 })
54
55 test_that("order is preserved", {
56 x <- 1:10
57 u <- rep(c("m", "l"), 5)
58 m <- mixed_units(x, u)
59 m <- set_units(m, paste0("k", u), mode = "standard")
60
61 expect_equal(as.numeric(m), x / 1000)
62 })
63
64 test_that("unique.mixed_units works", {
65 x <- c(set_units(c(1, 1, 2), kg), set_units(c(4, 4, 5), s), allow_mixed = TRUE)
66 expect_equal(unique(x), c(set_units(c(1, 2), kg), set_units(c(4, 5), s), allow_mixed = TRUE))
67
68 y <- c(set_units(c(1, 1, 1), m/s), set_units(c(1, 1, 1), kg/s), allow_mixed = TRUE)
69 expect_equal(unique(y), c(set_units(1, m/s), set_units(1, kg/s), allow_mixed = TRUE))
70
71 z <- c(set_units(c(1, 2), kg), set_units(c(3, 4), s), set_units(c(2, 3), kg), allow_mixed = TRUE)
72 expect_equal(unique(z), c(set_units(c(1, 2), kg), set_units(c(3, 4), s), set_units(3, kg), allow_mixed = TRUE))
73 })
74
75 test_that("as.data.frame.mixed_units works", {
76 x <- 1:3
77 x_units <- set_units(x, "m")
78 x_mixed <- mixed_units(x_units)
79
80 df1 <- data.frame(x)
81 df1$x_units <- x_units
82 df1$x_mixed <- x_mixed
83 df2 <- data.frame(x, x_units, x_mixed)
84
85 expect_equal(df1, df2)
86 })